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