uses pushing box to publish to google spreadsheets with a state machine instead of a while loop
Fork of GSM_PUSHING_BOX_STATE_MACHINE by
GSMLibrary.cpp@32:424896b5adbe, 2015-10-21 (annotated)
- Committer:
- DeWayneDennis
- Date:
- Wed Oct 21 19:44:15 2015 +0000
- Revision:
- 32:424896b5adbe
- Parent:
- 30:421aae087064
- Child:
- 33:2ae9a4eb6433
added GSM Library;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
es_marble | 24:7d2ff444d6d8 | 1 | //Libraries |
danilob | 0:41904adca656 | 2 | #include "GSMLibrary.h" |
DeWayneDennis | 32:424896b5adbe | 3 | //#include "gsmqueue.h" |
danilob | 2:8352ad91f2ee | 4 | #include <string.h> |
DeWayneDennis | 32:424896b5adbe | 5 | #include "GPRSInterface.h" |
DeWayneDennis | 32:424896b5adbe | 6 | #include "gsmqueue.h" |
es_marble | 24:7d2ff444d6d8 | 7 | //Global defines |
es_marble | 29:bc5f53f2922a | 8 | #define TIMEOUTLIMIT SECONDS_TIMEOUT/TIME_CONST //Defines how many "ticks" of the GSM will constitute "timeout" of our "watchdog timer" |
DeWayneDennis | 32:424896b5adbe | 9 | #define NUM_SIZE 250 |
danilob | 0:41904adca656 | 10 | |
es_marble | 16:6807d437cd48 | 11 | //External variables |
es_marble | 29:bc5f53f2922a | 12 | extern Serial pc; //To print output to computer |
DeWayneDennis | 32:424896b5adbe | 13 | //extern Serial gsm; //To communicate with GSM |
es_marble | 29:bc5f53f2922a | 14 | extern uint8_t buffer[BUFFER_LENGTH]; //DMA queue |
DeWayneDennis | 32:424896b5adbe | 15 | /************************************************** |
DeWayneDennis | 32:424896b5adbe | 16 | ** GPRS ** |
DeWayneDennis | 32:424896b5adbe | 17 | **************************************************/ |
DeWayneDennis | 32:424896b5adbe | 18 | /** |
DeWayneDennis | 32:424896b5adbe | 19 | * D1 - TX pin (RX on the WiFi side) |
DeWayneDennis | 32:424896b5adbe | 20 | * D0 - RX pin (TX on the WiFi side) |
DeWayneDennis | 32:424896b5adbe | 21 | * 19200 - Baud rate |
DeWayneDennis | 32:424896b5adbe | 22 | * "apn" - APN name |
DeWayneDennis | 32:424896b5adbe | 23 | * "username" - APN username |
DeWayneDennis | 32:424896b5adbe | 24 | * "password" - APN passowrd |
DeWayneDennis | 32:424896b5adbe | 25 | */ |
DeWayneDennis | 32:424896b5adbe | 26 | GPRSInterface eth(D1,D0, 19200, "ndo","",""); |
es_marble | 16:6807d437cd48 | 27 | |
es_marble | 16:6807d437cd48 | 28 | //Internal variables |
danilob | 0:41904adca656 | 29 | gsm_states gsm_current_state = GSM_INITIALIZE; |
danilob | 12:f3ccc43c4d3c | 30 | int timeout_count = 0; |
DeWayneDennis | 32:424896b5adbe | 31 | char state_chars[] = "iSJICS"; //For debugging - 1 char to represent each state: init, ok, signalstrength, network, turn off notifications, messagemode, read, phone, writesms, del |
DeWayneDennis | 32:424896b5adbe | 32 | char* serverIP; |
DeWayneDennis | 32:424896b5adbe | 33 | TCPSocketConnection sock; |
es_marble | 24:7d2ff444d6d8 | 34 | |
es_marble | 24:7d2ff444d6d8 | 35 | //Extras for transmitter |
DeWayneDennis | 32:424896b5adbe | 36 | char gsm_msg[250]; //String storing SMS message that will be sent (add 250 length to give leeway) |
DeWayneDennis | 32:424896b5adbe | 37 | char gsm_header[250]; //for the http header information |
DeWayneDennis | 32:424896b5adbe | 38 | char num[NUM_SIZE]; //Temporary string storage to help with concatenation of strings |
es_marble | 24:7d2ff444d6d8 | 39 | char send = false; //if true => we will send something (only if send_enable is true) |
es_marble | 29:bc5f53f2922a | 40 | char send_enable = false; //Sending start and stop commands to GSM via SMS changes this variable. If true, we will send SMS messages of our data received |
DeWayneDennis | 32:424896b5adbe | 41 | |
danilob | 0:41904adca656 | 42 | |
es_marble | 29:bc5f53f2922a | 43 | //"Tick" of the GSM (i.e. this is a state machine) |
es_marble | 16:6807d437cd48 | 44 | void gsm_tick() |
es_marble | 16:6807d437cd48 | 45 | { |
es_marble | 29:bc5f53f2922a | 46 | //Don't do anything, unless i) we received a response from the GSM, ii) the watchdog timer timed out, or iii) or we are initializing the GSM |
es_marble | 24:7d2ff444d6d8 | 47 | if (queueHasResponse() || gsm_timeOut() || gsm_current_state == GSM_INITIALIZE) |
es_marble | 16:6807d437cd48 | 48 | { |
es_marble | 27:fe1c7eaf5b88 | 49 | //gsm_printState(); //&debug |
es_marble | 27:fe1c7eaf5b88 | 50 | //printQueue(); //&debug |
es_marble | 16:6807d437cd48 | 51 | gsm_nextStateLogic(); //Next state |
DeWayneDennis | 32:424896b5adbe | 52 | //gsm_mealyOutputs(); //Mealy outputs. This state machine is a little different because Mealy outputs come after the next state logic |
es_marble | 16:6807d437cd48 | 53 | } |
es_marble | 16:6807d437cd48 | 54 | } |
es_marble | 29:bc5f53f2922a | 55 | |
es_marble | 29:bc5f53f2922a | 56 | //Prints the current state. To save time printing, simply prints 1 character to indicate the current state. |
es_marble | 24:7d2ff444d6d8 | 57 | void gsm_printState() |
es_marble | 24:7d2ff444d6d8 | 58 | { |
es_marble | 24:7d2ff444d6d8 | 59 | pc.printf("S:%c;", state_chars[gsm_current_state]); |
es_marble | 24:7d2ff444d6d8 | 60 | } |
es_marble | 24:7d2ff444d6d8 | 61 | |
es_marble | 16:6807d437cd48 | 62 | //Advance timeout counter; if timeout, return true |
es_marble | 16:6807d437cd48 | 63 | bool gsm_timeOut() |
es_marble | 16:6807d437cd48 | 64 | { |
es_marble | 16:6807d437cd48 | 65 | if(++timeout_count >= TIMEOUTLIMIT){ |
es_marble | 24:7d2ff444d6d8 | 66 | timeout_count = 0; |
es_marble | 24:7d2ff444d6d8 | 67 | gsm_reset(); |
es_marble | 16:6807d437cd48 | 68 | return true; |
danilob | 15:19ae032e2e54 | 69 | } |
es_marble | 16:6807d437cd48 | 70 | else |
es_marble | 16:6807d437cd48 | 71 | return false; |
es_marble | 16:6807d437cd48 | 72 | } |
es_marble | 16:6807d437cd48 | 73 | |
es_marble | 24:7d2ff444d6d8 | 74 | //Have the GSM send data - L = long, S = short, hh/mm/ss for time, "lat ns" for latitute, "lon we" for longitude |
DeWayneDennis | 32:424896b5adbe | 75 | void gsm_send_data(float L, float Lref, int hh, int mm, int ss, float lat, char ns, float lon, char we) |
es_marble | 18:7642909bfcfc | 76 | { |
es_marble | 26:838a9d26e8e9 | 77 | //Concatenate data |
es_marble | 24:7d2ff444d6d8 | 78 | gsm_msg[0] = NULL; |
DeWayneDennis | 32:424896b5adbe | 79 | gsm_header[0] = NULL; |
DeWayneDennis | 32:424896b5adbe | 80 | int contentLength = 0; |
DeWayneDennis | 32:424896b5adbe | 81 | snprintf(num, NUM_SIZE, "&phone=%s", "3852368101"); |
DeWayneDennis | 32:424896b5adbe | 82 | contentLength += strlen(num); |
es_marble | 24:7d2ff444d6d8 | 83 | strcat(gsm_msg, num); |
DeWayneDennis | 32:424896b5adbe | 84 | snprintf(num, NUM_SIZE, "&data=%f", L); |
DeWayneDennis | 32:424896b5adbe | 85 | contentLength += strlen(num); |
es_marble | 24:7d2ff444d6d8 | 86 | strcat(gsm_msg, num); |
DeWayneDennis | 32:424896b5adbe | 87 | snprintf(num, NUM_SIZE, "&dataRef=%f", Lref); |
DeWayneDennis | 32:424896b5adbe | 88 | contentLength += strlen(num); |
es_marble | 24:7d2ff444d6d8 | 89 | strcat(gsm_msg, num); |
DeWayneDennis | 32:424896b5adbe | 90 | snprintf(num, NUM_SIZE, "&dataRatio=%f", (Lref ? (L/Lref) : 0)); |
DeWayneDennis | 32:424896b5adbe | 91 | contentLength += strlen(num); |
es_marble | 24:7d2ff444d6d8 | 92 | strcat(gsm_msg, num); |
DeWayneDennis | 32:424896b5adbe | 93 | snprintf(num, NUM_SIZE, "&time=%02d:%02d:%02d", hh, mm, ss); //If there is no data from GPS, the time will just be "00:00:00" (that is okay) |
DeWayneDennis | 32:424896b5adbe | 94 | contentLength += strlen(num); |
es_marble | 26:838a9d26e8e9 | 95 | strcat(gsm_msg, num); |
es_marble | 29:bc5f53f2922a | 96 | if (ns != NULL) //If there is a gps fix (i.e. the gps has data on our location), ns will be set |
es_marble | 24:7d2ff444d6d8 | 97 | { |
DeWayneDennis | 32:424896b5adbe | 98 | snprintf(num, NUM_SIZE, "&latitude=%.4f&longitude=%.4f", (ns == 'N') ? lat : -lat, (we == 'E') ? lon : -lon); //Use + or - rather than N/S, E/W |
DeWayneDennis | 32:424896b5adbe | 99 | contentLength += strlen(num); |
es_marble | 24:7d2ff444d6d8 | 100 | strcat(gsm_msg, num); |
es_marble | 24:7d2ff444d6d8 | 101 | } |
DeWayneDennis | 32:424896b5adbe | 102 | else { |
DeWayneDennis | 32:424896b5adbe | 103 | snprintf(num, NUM_SIZE,"&latitude=0&longitude=0"); |
DeWayneDennis | 32:424896b5adbe | 104 | strcat(gsm_msg, num); //Otherwise just send 0's for latitude and longitude |
DeWayneDennis | 32:424896b5adbe | 105 | contentLength += strlen(num); |
DeWayneDennis | 32:424896b5adbe | 106 | } |
DeWayneDennis | 32:424896b5adbe | 107 | |
es_marble | 24:7d2ff444d6d8 | 108 | |
DeWayneDennis | 32:424896b5adbe | 109 | //header information |
DeWayneDennis | 32:424896b5adbe | 110 | snprintf(num, NUM_SIZE, "%s", "POST /pushingbox?devid=v941C443DE0C7B14"); |
DeWayneDennis | 32:424896b5adbe | 111 | strcat(gsm_header, num); |
DeWayneDennis | 32:424896b5adbe | 112 | strcat(gsm_header, gsm_msg); |
DeWayneDennis | 32:424896b5adbe | 113 | snprintf(num, NUM_SIZE, "%s"," HTTP/1.1\r\n"); |
DeWayneDennis | 32:424896b5adbe | 114 | strcat(gsm_header, num); |
DeWayneDennis | 32:424896b5adbe | 115 | snprintf(num, NUM_SIZE, "%s","Host: api.pushingbox.com\r\n"); |
DeWayneDennis | 32:424896b5adbe | 116 | strcat(gsm_header, num); |
DeWayneDennis | 32:424896b5adbe | 117 | snprintf(num, NUM_SIZE, "%s","Connection: close\r\n"); |
DeWayneDennis | 32:424896b5adbe | 118 | strcat(gsm_header, num); |
DeWayneDennis | 32:424896b5adbe | 119 | snprintf(num, NUM_SIZE, "%s","User-Agent: FRDM-KD64\r\n"); |
DeWayneDennis | 32:424896b5adbe | 120 | strcat(gsm_header, num); |
DeWayneDennis | 32:424896b5adbe | 121 | //must have two blank lines after so the server knows that this is the end of headers |
DeWayneDennis | 32:424896b5adbe | 122 | snprintf(num, NUM_SIZE, "Content-Length: %d\r\n\r\n", contentLength); |
DeWayneDennis | 32:424896b5adbe | 123 | strcat(gsm_header, num); |
es_marble | 24:7d2ff444d6d8 | 124 | send = true; //Mark that we are currently sending a message |
es_marble | 18:7642909bfcfc | 125 | } |
es_marble | 24:7d2ff444d6d8 | 126 | |
DeWayneDennis | 32:424896b5adbe | 127 | //Return true if gsm is ready to send via tcp |
DeWayneDennis | 32:424896b5adbe | 128 | //This only occurs if gsm received start sequence and responded appropriately |
es_marble | 18:7642909bfcfc | 129 | bool gsm_ready() |
es_marble | 18:7642909bfcfc | 130 | { |
es_marble | 24:7d2ff444d6d8 | 131 | return ((!send) && send_enable) ? true : false; |
es_marble | 18:7642909bfcfc | 132 | } |
es_marble | 24:7d2ff444d6d8 | 133 | |
es_marble | 29:bc5f53f2922a | 134 | //Reset the gsm. Currently this only resets the state, whether we are currently sending a message, and whether there are messages to delete. |
es_marble | 29:bc5f53f2922a | 135 | //It does not reset send_enable |
es_marble | 18:7642909bfcfc | 136 | void gsm_reset() |
es_marble | 18:7642909bfcfc | 137 | { |
es_marble | 18:7642909bfcfc | 138 | gsm_current_state = GSM_INITIALIZE; |
es_marble | 24:7d2ff444d6d8 | 139 | send = false; |
es_marble | 18:7642909bfcfc | 140 | } |
es_marble | 24:7d2ff444d6d8 | 141 | |
es_marble | 16:6807d437cd48 | 142 | //Next state logic ----------------------------------------------------- |
es_marble | 29:bc5f53f2922a | 143 | //Note how each state (except init) checks the response received to make sure |
es_marble | 29:bc5f53f2922a | 144 | //GSM has properly executed the command. If the response is correct, it changes |
es_marble | 29:bc5f53f2922a | 145 | //the state so that the next command will be sent in the gsm_mealyOutputs function |
es_marble | 29:bc5f53f2922a | 146 | //below. |
es_marble | 16:6807d437cd48 | 147 | void gsm_nextStateLogic() |
es_marble | 16:6807d437cd48 | 148 | { |
es_marble | 16:6807d437cd48 | 149 | switch(gsm_current_state) |
es_marble | 16:6807d437cd48 | 150 | { |
danilob | 0:41904adca656 | 151 | case GSM_INITIALIZE: |
es_marble | 29:bc5f53f2922a | 152 | timeout_count = 0; //No AT commands have been sent: this will send the first one |
DeWayneDennis | 32:424896b5adbe | 153 | printf(">>>INIT\r\n"); |
DeWayneDennis | 32:424896b5adbe | 154 | if (eth.init() != NULL) { |
DeWayneDennis | 32:424896b5adbe | 155 | printf(">>> Could not initialise. Halting!\n"); |
DeWayneDennis | 32:424896b5adbe | 156 | exit(0); |
DeWayneDennis | 32:424896b5adbe | 157 | } |
DeWayneDennis | 32:424896b5adbe | 158 | gsm_current_state = GSM_CHECK_SIM; |
danilob | 0:41904adca656 | 159 | break; |
DeWayneDennis | 32:424896b5adbe | 160 | case GSM_CHECK_SIM: |
DeWayneDennis | 32:424896b5adbe | 161 | printf(">>>CHECK SIM\r\n"); |
DeWayneDennis | 32:424896b5adbe | 162 | if (eth.preInit() == true){ |
DeWayneDennis | 32:424896b5adbe | 163 | gsm_current_state = GSM_JOIN; |
DeWayneDennis | 32:424896b5adbe | 164 | } |
es_marble | 24:7d2ff444d6d8 | 165 | break; |
DeWayneDennis | 32:424896b5adbe | 166 | case GSM_JOIN: |
DeWayneDennis | 32:424896b5adbe | 167 | printf(">>>JOIN\r\n"); |
DeWayneDennis | 32:424896b5adbe | 168 | int join = eth.connect(); |
DeWayneDennis | 32:424896b5adbe | 169 | if (join == false || join < 0){ |
DeWayneDennis | 32:424896b5adbe | 170 | //stay here |
es_marble | 24:7d2ff444d6d8 | 171 | } |
DeWayneDennis | 32:424896b5adbe | 172 | else{ |
DeWayneDennis | 32:424896b5adbe | 173 | //possibly send this sms to the main box at the lab |
DeWayneDennis | 32:424896b5adbe | 174 | //eth.send_SMS("17066311506", eth.getIPAddress()); |
DeWayneDennis | 32:424896b5adbe | 175 | gsm_current_state = GSM_SERVER_IP; |
es_marble | 24:7d2ff444d6d8 | 176 | } |
danilob | 0:41904adca656 | 177 | break; |
DeWayneDennis | 32:424896b5adbe | 178 | case GSM_SERVER_IP: |
DeWayneDennis | 32:424896b5adbe | 179 | printf(">>>SERVER IP\r\n"); |
DeWayneDennis | 32:424896b5adbe | 180 | serverIP = "api.pushingbox.com"; |
DeWayneDennis | 32:424896b5adbe | 181 | if(serverIP != NULL) |
es_marble | 18:7642909bfcfc | 182 | { |
DeWayneDennis | 32:424896b5adbe | 183 | gsm_current_state = GSM_CONNECT; |
DeWayneDennis | 32:424896b5adbe | 184 | } |
DeWayneDennis | 32:424896b5adbe | 185 | else{ |
DeWayneDennis | 32:424896b5adbe | 186 | gsm_current_state = GSM_JOIN; |
DeWayneDennis | 32:424896b5adbe | 187 | } |
DeWayneDennis | 32:424896b5adbe | 188 | break; |
DeWayneDennis | 32:424896b5adbe | 189 | case GSM_CONNECT: |
DeWayneDennis | 32:424896b5adbe | 190 | printf("\r\n>>>CONNECT TO: %s\r\n", serverIP); |
DeWayneDennis | 32:424896b5adbe | 191 | //sock.set_blocking(true,5000); |
DeWayneDennis | 32:424896b5adbe | 192 | if (sock.connect(serverIP,80) == true){ |
DeWayneDennis | 32:424896b5adbe | 193 | gsm_current_state = GSM_SEND; |
DeWayneDennis | 32:424896b5adbe | 194 | } |
DeWayneDennis | 32:424896b5adbe | 195 | else{ |
DeWayneDennis | 32:424896b5adbe | 196 | gsm_current_state = GSM_CONNECT; |
es_marble | 18:7642909bfcfc | 197 | } |
DeWayneDennis | 32:424896b5adbe | 198 | break; |
DeWayneDennis | 32:424896b5adbe | 199 | case GSM_SEND: |
DeWayneDennis | 32:424896b5adbe | 200 | printf(">>>READY TO SEND\r\n"); |
DeWayneDennis | 32:424896b5adbe | 201 | if(send){ |
DeWayneDennis | 32:424896b5adbe | 202 | if(sock.send_all(gsm_header, sizeof(gsm_header)-1)){ |
DeWayneDennis | 32:424896b5adbe | 203 | printf("Data succesfully sent to server\r\n"); |
DeWayneDennis | 32:424896b5adbe | 204 | //close the connection so others can send too |
DeWayneDennis | 32:424896b5adbe | 205 | //wait(2); |
DeWayneDennis | 32:424896b5adbe | 206 | //eth.disconnect(); |
DeWayneDennis | 32:424896b5adbe | 207 | } |
DeWayneDennis | 32:424896b5adbe | 208 | else{ |
DeWayneDennis | 32:424896b5adbe | 209 | printf("Reconnecting to Server...\r\n"); |
DeWayneDennis | 32:424896b5adbe | 210 | send = false; |
DeWayneDennis | 32:424896b5adbe | 211 | gsm_current_state = GSM_CONNECT; |
DeWayneDennis | 32:424896b5adbe | 212 | } |
es_marble | 24:7d2ff444d6d8 | 213 | } |
danilob | 0:41904adca656 | 214 | break; |
DeWayneDennis | 32:424896b5adbe | 215 | case GSM_WAIT: |
DeWayneDennis | 32:424896b5adbe | 216 | //check for text message from server asking for data |
DeWayneDennis | 32:424896b5adbe | 217 | gsm_current_state = GSM_SEND; |
danilob | 19:a442b5a0116f | 218 | break; |
es_marble | 16:6807d437cd48 | 219 | default: |
es_marble | 30:421aae087064 | 220 | pc.printf("This is a state error\r\n"); |
es_marble | 16:6807d437cd48 | 221 | } |
es_marble | 16:6807d437cd48 | 222 | } |
es_marble | 29:bc5f53f2922a | 223 | |
es_marble | 18:7642909bfcfc | 224 | //Initialize the GSM |
es_marble | 28:81f1c8bd3299 | 225 | void gsm_initialize(){ |
es_marble | 29:bc5f53f2922a | 226 | wait(2.3); //Wait for the GSM to turn on properly before doing this initialization |
danilob | 7:6c0b6ab3cafe | 227 | SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK; //enabling dmamux clock |
danilob | 12:f3ccc43c4d3c | 228 | SIM_SCGC7 |= SIM_SCGC7_DMA_MASK; // enebaling dma clock |
es_marble | 27:fe1c7eaf5b88 | 229 | //pc.printf("initializing DMA...\r\n"); |
es_marble | 29:bc5f53f2922a | 230 | // control register mux, enabling uart3 receive |
danilob | 0:41904adca656 | 231 | DMAMUX_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK|DMAMUX_CHCFG_SOURCE(8); |
danilob | 0:41904adca656 | 232 | |
danilob | 0:41904adca656 | 233 | // Enable request signal for channel 0 |
danilob | 0:41904adca656 | 234 | DMA_ERQ = DMA_ERQ_ERQ0_MASK; |
danilob | 0:41904adca656 | 235 | |
danilob | 0:41904adca656 | 236 | // select round-robin arbitration priority |
danilob | 0:41904adca656 | 237 | DMA_CR |= DMA_CR_ERCA_MASK; |
danilob | 0:41904adca656 | 238 | |
es_marble | 29:bc5f53f2922a | 239 | //enable error interrupt for DMA0 (commented out because we won't use interrupts for our implementation) |
es_marble | 29:bc5f53f2922a | 240 | //DMA_EEI = DMA_EEI_EEI0_MASK; |
es_marble | 29:bc5f53f2922a | 241 | |
es_marble | 29:bc5f53f2922a | 242 | //Address for buffer |
danilob | 0:41904adca656 | 243 | DMA_TCD0_SADDR = (uint32_t) &UART_D_REG(UART3_BASE_PTR); |
danilob | 0:41904adca656 | 244 | DMA_TCD0_DADDR = (uint32_t) buffer; |
danilob | 0:41904adca656 | 245 | // Set an offset for source and destination address |
danilob | 0:41904adca656 | 246 | DMA_TCD0_SOFF = 0x00; |
danilob | 0:41904adca656 | 247 | DMA_TCD0_DOFF = 0x01; // Destination address offset of 1 byte per transaction |
danilob | 0:41904adca656 | 248 | |
danilob | 0:41904adca656 | 249 | // Set source and destination data transfer size |
danilob | 0:41904adca656 | 250 | DMA_TCD0_ATTR = DMA_ATTR_SSIZE(0) | DMA_ATTR_DSIZE(0); |
danilob | 0:41904adca656 | 251 | |
danilob | 0:41904adca656 | 252 | // Number of bytes to be transfered in each service request of the channel |
danilob | 0:41904adca656 | 253 | DMA_TCD0_NBYTES_MLNO = 0x01; |
danilob | 0:41904adca656 | 254 | // Current major iteration count |
danilob | 0:41904adca656 | 255 | DMA_TCD0_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(BUFFER_LENGTH); |
danilob | 0:41904adca656 | 256 | DMA_TCD0_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(BUFFER_LENGTH); |
danilob | 0:41904adca656 | 257 | // Adjustment value used to restore the source and destiny address to the initial value |
danilob | 0:41904adca656 | 258 | // After reading 'len' number of times, the DMA goes back to the beginning by subtracting len*2 from the address (going back to the original address) |
es_marble | 16:6807d437cd48 | 259 | DMA_TCD0_SLAST = 0; // Source address adjustment |
es_marble | 16:6807d437cd48 | 260 | DMA_TCD0_DLASTSGA = -BUFFER_LENGTH; // Destination address adjustment |
danilob | 0:41904adca656 | 261 | // Setup control and status register |
danilob | 0:41904adca656 | 262 | DMA_TCD0_CSR = 0; |
danilob | 0:41904adca656 | 263 | |
danilob | 0:41904adca656 | 264 | // enable interrupt call at end of major loop |
danilob | 0:41904adca656 | 265 | DMA_TCD0_CSR |= DMA_CSR_INTMAJOR_MASK; |
danilob | 0:41904adca656 | 266 | |
es_marble | 29:bc5f53f2922a | 267 | //Activate dma transfer rx interrupt |
danilob | 0:41904adca656 | 268 | UART_C2_REG(UART3) |= UART_C2_RIE_MASK; |
danilob | 0:41904adca656 | 269 | UART_C5_REG(UART3) |= UART_C5_RDMAS_MASK | UART_C5_ILDMAS_MASK | UART_C5_LBKDDMAS_MASK; |
es_marble | 29:bc5f53f2922a | 270 | //activate p fifo register |
danilob | 0:41904adca656 | 271 | UART_PFIFO_REG(UART3) |= UART_PFIFO_RXFE_MASK; //RXFE and buffer size of 1 word |
danilob | 7:6c0b6ab3cafe | 272 | queueInit(); |
es_marble | 27:fe1c7eaf5b88 | 273 | //pc.printf("done...\n\r"); |
danilob | 0:41904adca656 | 274 | } |
es_marble | 24:7d2ff444d6d8 | 275 | |
es_marble | 29:bc5f53f2922a | 276 | //For debugging: print registers related to DMA and UART setup |
es_marble | 29:bc5f53f2922a | 277 | void print_registers() |
es_marble | 29:bc5f53f2922a | 278 | { |
danilob | 0:41904adca656 | 279 | pc.printf("\n\rDMA REGISTERS\n\r"); |
danilob | 0:41904adca656 | 280 | pc.printf("DMA_MUX: 0x%08x\r\n",DMAMUX_CHCFG0); |
danilob | 0:41904adca656 | 281 | pc.printf("SADDR0: 0x%08x\r\n",DMA_TCD0_SADDR); |
danilob | 0:41904adca656 | 282 | pc.printf("DADDR0: 0x%08x\r\n",DMA_TCD0_DADDR); |
danilob | 0:41904adca656 | 283 | pc.printf("CITER0: 0x%08x\r\n",DMA_TCD0_CITER_ELINKNO); |
danilob | 0:41904adca656 | 284 | pc.printf("BITER0: 0x%08x\r\n",DMA_TCD0_BITER_ELINKNO); |
danilob | 0:41904adca656 | 285 | pc.printf("DMA_CR: %08x\r\n", DMA_CR); |
danilob | 0:41904adca656 | 286 | pc.printf("DMA_ES: %08x\r\n", DMA_ES); |
danilob | 0:41904adca656 | 287 | pc.printf("DMA_ERQ: %08x\r\n", DMA_ERQ); |
danilob | 0:41904adca656 | 288 | pc.printf("DMA_EEI: %08x\r\n", DMA_EEI); |
danilob | 0:41904adca656 | 289 | pc.printf("DMA_CEEI: %02x\r\n", DMA_CEEI); |
danilob | 0:41904adca656 | 290 | pc.printf("DMA_SEEI: %02x\r\n", DMA_SEEI); |
danilob | 0:41904adca656 | 291 | pc.printf("DMA_CERQ: %02x\r\n", DMA_CERQ); |
danilob | 0:41904adca656 | 292 | pc.printf("DMA_SERQ: %02x\r\n", DMA_SERQ); |
danilob | 0:41904adca656 | 293 | pc.printf("DMA_CDNE: %02x\r\n", DMA_CDNE); |
danilob | 0:41904adca656 | 294 | pc.printf("DMA_SSRT: %02x\r\n", DMA_SSRT); |
danilob | 0:41904adca656 | 295 | pc.printf("DMA_CERR: %02x\r\n", DMA_CERR); |
danilob | 0:41904adca656 | 296 | pc.printf("DMA_CINT: %02x\r\n", DMA_CINT); |
danilob | 0:41904adca656 | 297 | pc.printf("DMA_INT: %08x\r\n", DMA_INT); |
danilob | 0:41904adca656 | 298 | pc.printf("DMA_ERR: %08x\r\n", DMA_ERR); |
danilob | 0:41904adca656 | 299 | pc.printf("DMA_HRS: %08x\r\n", DMA_HRS); |
danilob | 0:41904adca656 | 300 | pc.printf("DMA_TCD0_DOFF: %08x\r\n",DMA_TCD0_DOFF); |
danilob | 0:41904adca656 | 301 | pc.printf("\n\rUART REGISTERS\n\r"); |
danilob | 0:41904adca656 | 302 | pc.printf("UART_BDH_REG: %08x\r\n",UART_BDH_REG(UART3)); |
danilob | 0:41904adca656 | 303 | pc.printf("UART_C1_REG: %08x\r\n",UART_C1_REG(UART3)); |
danilob | 0:41904adca656 | 304 | pc.printf("UART_C2_REG: %08x\r\n",UART_C2_REG(UART3)); |
danilob | 0:41904adca656 | 305 | pc.printf("UART_S1_REG: %08x\r\n",UART_S1_REG(UART3)); |
es_marble | 16:6807d437cd48 | 306 | pc.printf("UART_s2_REG: %08x\r\n",UART_S2_REG(UART3)); |
danilob | 0:41904adca656 | 307 | pc.printf("UART_C3_REG: %08x\r\n",UART_C3_REG(UART3)); |
danilob | 0:41904adca656 | 308 | pc.printf("UART_D_REG: %08x\r\n",UART_D_REG(UART3)); |
danilob | 0:41904adca656 | 309 | pc.printf("UART_MA1_REG: %08x\r\n",UART_MA1_REG(UART3)); |
danilob | 0:41904adca656 | 310 | pc.printf("UART_MA2_REG: %08x\r\n",UART_MA2_REG(UART3)); |
danilob | 0:41904adca656 | 311 | pc.printf("UART_C4_REG: %08x\r\n",UART_C4_REG(UART3)); |
danilob | 0:41904adca656 | 312 | pc.printf("UART_C5_REG: %08x\r\n",UART_C5_REG(UART3)); |
es_marble | 16:6807d437cd48 | 313 | pc.printf("UART_ED_REG: %08x\r\n",UART_ED_REG(UART3)); |
danilob | 0:41904adca656 | 314 | pc.printf("UART_MODEM_REG: %08x\r\n",UART_MODEM_REG(UART3)); |
danilob | 0:41904adca656 | 315 | pc.printf("UART_IR_REG: %08x\r\n",UART_IR_REG(UART3)); |
danilob | 0:41904adca656 | 316 | pc.printf("UART_PFIFO_REG: %08x\r\n",UART_PFIFO_REG(UART3)); |
danilob | 0:41904adca656 | 317 | pc.printf("UART_CFIFO_REG: %08x\r\n",UART_CFIFO_REG(UART3)); |
danilob | 0:41904adca656 | 318 | pc.printf("UART_SFIFO_REG: %08x\r\n",UART_SFIFO_REG(UART3)); |
danilob | 0:41904adca656 | 319 | pc.printf("UART_TWFIFO_REG: %08x\r\n",UART_TWFIFO_REG(UART3)); |
danilob | 0:41904adca656 | 320 | pc.printf("UART_TCFIFO_REG: %08x\r\n",UART_TCFIFO_REG(UART3)); |
danilob | 0:41904adca656 | 321 | pc.printf("UART_RWFIFO_REG: %08x\r\n",UART_RWFIFO_REG(UART3)); |
danilob | 0:41904adca656 | 322 | pc.printf("UART_RCFIFO_REG: %08x\r\n",UART_RCFIFO_REG(UART3)); |
es_marble | 29:bc5f53f2922a | 323 | } |