uses pushing box to publish to google spreadsheets with a state machine instead of a while loop

Dependents:   DCS_FINAL_CODE

Fork of GSM_PUSHING_BOX_STATE_MACHINE by DCS_TEAM

Committer:
es_marble
Date:
Sun Apr 12 03:09:15 2015 +0000
Revision:
26:838a9d26e8e9
Parent:
25:9de265c5bb28
Child:
27:fe1c7eaf5b88
Changed SMS data to begin with $ and end with %

Who changed what in which revision?

UserRevisionLine numberNew contents of line
es_marble 24:7d2ff444d6d8 1 //Libraries
danilob 0:41904adca656 2 #include "GSMLibrary.h"
danilob 0:41904adca656 3 #include "gsmqueue.h"
danilob 2:8352ad91f2ee 4 #include <string.h>
danilob 0:41904adca656 5
es_marble 24:7d2ff444d6d8 6 //Global defines
danilob 15:19ae032e2e54 7 #define TIMEOUTLIMIT SECONDS_TIMEOUT/TIME_CONST //$change check with main code this will set up condition fior timeout.
danilob 0:41904adca656 8
es_marble 24:7d2ff444d6d8 9 //Extra defines for transmitter
es_marble 24:7d2ff444d6d8 10 #define START_SMS_TRANSMISSION "START"
es_marble 24:7d2ff444d6d8 11 #define STOP_SMS_TRANSMISSION "STOP"
es_marble 24:7d2ff444d6d8 12 #define GPS_SMS_TRANSMISSION "GPS"
es_marble 24:7d2ff444d6d8 13 #define RECEIVER_PHONE_NUMBER "\"+13853357314\""
es_marble 24:7d2ff444d6d8 14 #define AT_CMGS "AT+CMGS=" RECEIVER_PHONE_NUMBER
es_marble 24:7d2ff444d6d8 15 #define NUM_SIZE 25
es_marble 26:838a9d26e8e9 16 #define BEGIN_SENSOR_DATA "$"
es_marble 26:838a9d26e8e9 17 #define END_SENSOR_DATA "%"
es_marble 24:7d2ff444d6d8 18
es_marble 24:7d2ff444d6d8 19 //definition for AT comands to send
danilob 0:41904adca656 20 #define AT_OK "AT"
danilob 0:41904adca656 21 #define AT_CSQ "AT+CSQ"
danilob 0:41904adca656 22 #define AT_CREG "AT+CREG?"
es_marble 24:7d2ff444d6d8 23 #define AT_CNMI "AT+CNMI=2,0,0,0,0"
danilob 0:41904adca656 24 #define AT_CMGF "AT+CMGF=1"
es_marble 24:7d2ff444d6d8 25 #define AT_READ_MSG "AT+CMGL=\"REC UNREAD\"" //make sure device is in txt mode.
es_marble 24:7d2ff444d6d8 26 #define AT_DEL_R_MSGS "AT+QMGDA=\"DEL READ\""
danilob 22:a5adf9331032 27
es_marble 24:7d2ff444d6d8 28 //Definition for AT responses
danilob 0:41904adca656 29 //Please notice that after ":" the gsm will usually send aditional information
danilob 0:41904adca656 30 #define AT_OK_RESPONSE "OK" //Response after sending "AT" message
es_marble 6:3ccc86304c2c 31 #define AT_CSQ_RESPONSE "+CSQ:" //+CSQ: <arg1>,<arg2> where <arg1> is signal strength arg1 = 0-30 where a number below 10 means low signal strength and 99 is not knwn or detectable signal and arg2 is bit error rate form 0-7, 99 will represent error
danilob 13:9ac5ff131214 32 #define AT_CREG_RESPONSE "+CREG:"//+CREG: <arg1>,<arg2> where <arg1> = 0-2(see AT command descriptions), <arg2> = 0-5, 0 not registered to nework and not looking for one. 1 is conected to network, 2 is not conected but searching
es_marble 24:7d2ff444d6d8 33 #define AT_CNMI_RESPONSE "OK"
danilob 0:41904adca656 34 #define AT_CMGF_RESPONSE "OK"
es_marble 24:7d2ff444d6d8 35 #define AT_CMGS_RESPONSE ">" //Received after you give the GSM the phone number. (We mistakenly thought it was received a second time after SMS was sent... this is not true!)
es_marble 24:7d2ff444d6d8 36 #define AT_SENDSMS_RESPONSE "+CMGS:" // +CMGS: <id> this will include the message id. CMS ERROR for error.
es_marble 24:7d2ff444d6d8 37 #define AT_DEL_R_MSGS_RESPONSE "OK"
danilob 0:41904adca656 38
es_marble 16:6807d437cd48 39 //External variables
danilob 0:41904adca656 40 extern Serial pc;
danilob 0:41904adca656 41 extern Serial gsm;
danilob 0:41904adca656 42 extern uint8_t buffer[BUFFER_LENGTH];//buffer storing char
es_marble 16:6807d437cd48 43
es_marble 16:6807d437cd48 44 //Internal variables
danilob 0:41904adca656 45 gsm_states gsm_current_state = GSM_INITIALIZE;
danilob 12:f3ccc43c4d3c 46 int timeout_count = 0;
es_marble 24:7d2ff444d6d8 47 char state_chars[] = "iosntmRPWD"; //init, ok, signalstrength, network, turn off notifications, messagemode, read, phone, writesms, del
es_marble 24:7d2ff444d6d8 48
es_marble 24:7d2ff444d6d8 49 //Extras for transmitter
es_marble 24:7d2ff444d6d8 50 char send = false; //if true => we will send something (only if send_enable is true)
es_marble 24:7d2ff444d6d8 51 char send_enable = false; //Sending start and stop commands to GSM via SMS changes this variable
es_marble 24:7d2ff444d6d8 52 char undeleted_msgs = true; //At the beginning assume we have undeleted messages
es_marble 24:7d2ff444d6d8 53 char gsm_msg[MAX_SMS_LENGTH + 250]; //String storing SMS message to send to GSMMaximum (add 250 length to give leeway)
es_marble 24:7d2ff444d6d8 54 char num[NUM_SIZE]; //Temporary string storage to convert numbers
danilob 0:41904adca656 55
es_marble 16:6807d437cd48 56 void gsm_tick()
es_marble 16:6807d437cd48 57 {
es_marble 24:7d2ff444d6d8 58
es_marble 24:7d2ff444d6d8 59 if (queueHasResponse() || gsm_timeOut() || gsm_current_state == GSM_INITIALIZE)
es_marble 16:6807d437cd48 60 {
es_marble 24:7d2ff444d6d8 61 gsm_printState(); //&debug
es_marble 24:7d2ff444d6d8 62 printQueue(); //&debug
es_marble 16:6807d437cd48 63 gsm_nextStateLogic(); //Next state
es_marble 16:6807d437cd48 64 gsm_mealyOutputs(); //Mealy outputs
es_marble 16:6807d437cd48 65 }
es_marble 16:6807d437cd48 66 }
es_marble 24:7d2ff444d6d8 67
es_marble 24:7d2ff444d6d8 68 void gsm_printState()
es_marble 24:7d2ff444d6d8 69 {
es_marble 24:7d2ff444d6d8 70 pc.printf("S:%c;", state_chars[gsm_current_state]);
es_marble 24:7d2ff444d6d8 71 }
es_marble 24:7d2ff444d6d8 72
es_marble 16:6807d437cd48 73 //Advance timeout counter; if timeout, return true
es_marble 16:6807d437cd48 74 bool gsm_timeOut()
es_marble 16:6807d437cd48 75 {
es_marble 16:6807d437cd48 76 if(++timeout_count >= TIMEOUTLIMIT){
es_marble 24:7d2ff444d6d8 77 timeout_count = 0;
es_marble 24:7d2ff444d6d8 78 gsm_reset();
es_marble 16:6807d437cd48 79 return true;
danilob 15:19ae032e2e54 80 }
es_marble 16:6807d437cd48 81 else
es_marble 16:6807d437cd48 82 return false;
es_marble 16:6807d437cd48 83 }
es_marble 16:6807d437cd48 84
es_marble 24:7d2ff444d6d8 85 //Have the GSM send data - L = long, S = short, hh/mm/ss for time, "lat ns" for latitute, "lon we" for longitude
es_marble 24:7d2ff444d6d8 86 void gsm_send_data(float L, float Lref, float S, float Sref, int hh, int mm, int ss, float lat, char ns, float lon, char we)
es_marble 18:7642909bfcfc 87 {
es_marble 24:7d2ff444d6d8 88 if (!gsm_ready()) //Don't send if gsm not ready
es_marble 24:7d2ff444d6d8 89 return;
es_marble 24:7d2ff444d6d8 90
es_marble 26:838a9d26e8e9 91 //Concatenate data
es_marble 24:7d2ff444d6d8 92 gsm_msg[0] = NULL;
es_marble 26:838a9d26e8e9 93 strcat(gsm_msg, BEGIN_SENSOR_DATA);
es_marble 24:7d2ff444d6d8 94 snprintf(num, NUM_SIZE, "%f,", L);
es_marble 24:7d2ff444d6d8 95 strcat(gsm_msg, num);
es_marble 24:7d2ff444d6d8 96 snprintf(num, NUM_SIZE, "%f,", Lref);
es_marble 24:7d2ff444d6d8 97 strcat(gsm_msg, num);
es_marble 24:7d2ff444d6d8 98 snprintf(num, NUM_SIZE, "%f,", S);
es_marble 24:7d2ff444d6d8 99 strcat(gsm_msg, num);
es_marble 26:838a9d26e8e9 100 snprintf(num, NUM_SIZE, "%f,", Sref);
es_marble 24:7d2ff444d6d8 101 strcat(gsm_msg, num);
es_marble 26:838a9d26e8e9 102 snprintf(num, NUM_SIZE, "%d:%d:%d,", hh, mm, ss);
es_marble 26:838a9d26e8e9 103 strcat(gsm_msg, num);
es_marble 24:7d2ff444d6d8 104 if (ns != NULL) //If there is a gps fix, ns will be set
es_marble 24:7d2ff444d6d8 105 {
es_marble 26:838a9d26e8e9 106 snprintf(num, NUM_SIZE, "%.4f,%.4f", (ns == 'N') ? lat : -lat, (we == 'E') ? lon : -lon);
es_marble 24:7d2ff444d6d8 107 strcat(gsm_msg, num);
es_marble 24:7d2ff444d6d8 108 }
es_marble 26:838a9d26e8e9 109 else strcat(gsm_msg, "0,0");
es_marble 26:838a9d26e8e9 110 strcat(gsm_msg, END_SENSOR_DATA);
es_marble 24:7d2ff444d6d8 111
es_marble 24:7d2ff444d6d8 112 send = true; //Mark that we are currently sending a message
es_marble 24:7d2ff444d6d8 113 strcat(gsm_msg, SMS_END_CHAR); //Add SMS end char
es_marble 18:7642909bfcfc 114 }
es_marble 24:7d2ff444d6d8 115
es_marble 24:7d2ff444d6d8 116 //Return true if gsm is ready to send sms
es_marble 24:7d2ff444d6d8 117 //This only occurs if send = false (not currently sending a message) AND gsm received start sequence
es_marble 18:7642909bfcfc 118 bool gsm_ready()
es_marble 18:7642909bfcfc 119 {
es_marble 24:7d2ff444d6d8 120 return ((!send) && send_enable) ? true : false;
es_marble 18:7642909bfcfc 121 }
es_marble 24:7d2ff444d6d8 122
es_marble 24:7d2ff444d6d8 123 //Reset the gsm. Currently this only resets the state and whether we are currently sending a message
es_marble 24:7d2ff444d6d8 124 //It does not reset send_enable or undeleted_msgs
es_marble 18:7642909bfcfc 125 void gsm_reset()
es_marble 18:7642909bfcfc 126 {
es_marble 24:7d2ff444d6d8 127 //If we are in the middle of sending a text message, exit this loop
es_marble 24:7d2ff444d6d8 128 if (gsm_current_state == GSM_AT_CMGS)
es_marble 24:7d2ff444d6d8 129 sendCommand(SMS_ESCAPE_CHAR);
es_marble 24:7d2ff444d6d8 130
es_marble 18:7642909bfcfc 131 gsm_current_state = GSM_INITIALIZE;
es_marble 24:7d2ff444d6d8 132 undeleted_msgs = true;
es_marble 24:7d2ff444d6d8 133 send = false;
es_marble 18:7642909bfcfc 134 }
es_marble 24:7d2ff444d6d8 135
es_marble 16:6807d437cd48 136 //Next state logic -----------------------------------------------------
es_marble 16:6807d437cd48 137 void gsm_nextStateLogic()
es_marble 16:6807d437cd48 138 {
es_marble 24:7d2ff444d6d8 139 //printQueue(); //$debug
es_marble 24:7d2ff444d6d8 140
es_marble 16:6807d437cd48 141 switch(gsm_current_state)
es_marble 16:6807d437cd48 142 {
danilob 0:41904adca656 143 case GSM_INITIALIZE:
danilob 2:8352ad91f2ee 144 timeout_count = 0;
es_marble 24:7d2ff444d6d8 145 gsm_current_state = GSM_AT_OK; //unconditional (check it)
danilob 0:41904adca656 146 break;
danilob 0:41904adca656 147 case GSM_AT_OK:
es_marble 24:7d2ff444d6d8 148 if (findInQueue(AT_OK_RESPONSE, true))
es_marble 16:6807d437cd48 149 gsm_current_state = GSM_AT_CSQ;
danilob 12:f3ccc43c4d3c 150 break;
danilob 12:f3ccc43c4d3c 151 case GSM_AT_CSQ:
es_marble 24:7d2ff444d6d8 152 if(findInQueue(AT_CSQ_RESPONSE, true))
es_marble 16:6807d437cd48 153 gsm_current_state = GSM_AT_CREG;
danilob 2:8352ad91f2ee 154 break;
danilob 0:41904adca656 155 case GSM_AT_CREG:
es_marble 24:7d2ff444d6d8 156 if(findInQueue(AT_CREG_RESPONSE, true))
es_marble 16:6807d437cd48 157 {
es_marble 24:7d2ff444d6d8 158 parseInt();
es_marble 24:7d2ff444d6d8 159 if(parseInt() == 1)
es_marble 24:7d2ff444d6d8 160 gsm_current_state = GSM_AT_CNMI;
danilob 12:f3ccc43c4d3c 161 }
danilob 0:41904adca656 162 break;
es_marble 24:7d2ff444d6d8 163 case GSM_AT_CNMI:
es_marble 24:7d2ff444d6d8 164 if(findInQueue(AT_CNMI_RESPONSE, true))
es_marble 24:7d2ff444d6d8 165 gsm_current_state = GSM_AT_CMGF;
es_marble 24:7d2ff444d6d8 166 break;
danilob 0:41904adca656 167 case GSM_AT_CMGF:
es_marble 24:7d2ff444d6d8 168 if(findInQueue(AT_CMGF_RESPONSE, true))
es_marble 24:7d2ff444d6d8 169 gsm_current_state = GSM_READ_MSG;
es_marble 24:7d2ff444d6d8 170 break;
es_marble 24:7d2ff444d6d8 171 case GSM_READ_MSG:
es_marble 24:7d2ff444d6d8 172 if(send_enable) //Check if we need to stop transmission of SMS
es_marble 24:7d2ff444d6d8 173 {
es_marble 24:7d2ff444d6d8 174 if(findInQueue(STOP_SMS_TRANSMISSION, true)) //Always stop sending if stop sequence received by SMS
es_marble 24:7d2ff444d6d8 175 {
es_marble 24:7d2ff444d6d8 176 undeleted_msgs = true;
es_marble 24:7d2ff444d6d8 177 send_enable = false; //Set send_enable to indicate we will stop sending SMS
es_marble 24:7d2ff444d6d8 178 }
es_marble 24:7d2ff444d6d8 179 else if (send) //Only continue sending if we didn't find stop sequence AND user requested that we send sms
es_marble 24:7d2ff444d6d8 180 gsm_current_state = GSM_AT_CMGS; //Continue sending SMS (change state accordingly)
es_marble 24:7d2ff444d6d8 181 //Implicit: otherwise we will continue checking text messages in this state.
es_marble 24:7d2ff444d6d8 182 }
es_marble 24:7d2ff444d6d8 183 else
es_marble 24:7d2ff444d6d8 184 {
es_marble 24:7d2ff444d6d8 185 if(findInQueue(START_SMS_TRANSMISSION, true))
es_marble 24:7d2ff444d6d8 186 {
es_marble 24:7d2ff444d6d8 187 undeleted_msgs = true;
es_marble 24:7d2ff444d6d8 188 send_enable = true;
es_marble 24:7d2ff444d6d8 189 if (send) //Only continue sending if we found start sequence AND user requested that we send sms
es_marble 24:7d2ff444d6d8 190 gsm_current_state = GSM_AT_CMGS; //Start sending SMS (change state accordingly)
es_marble 24:7d2ff444d6d8 191 }
es_marble 24:7d2ff444d6d8 192 }
danilob 0:41904adca656 193 break;
danilob 0:41904adca656 194 case GSM_AT_CMGS:
es_marble 24:7d2ff444d6d8 195 if(findInQueue(AT_CMGS_RESPONSE, true))
es_marble 16:6807d437cd48 196 gsm_current_state = GSM_AT_SENDSMS;
danilob 0:41904adca656 197 break;
danilob 0:41904adca656 198 case GSM_AT_SENDSMS:
es_marble 24:7d2ff444d6d8 199 if(findInQueue(AT_SENDSMS_RESPONSE, true))
es_marble 18:7642909bfcfc 200 {
es_marble 24:7d2ff444d6d8 201 pc.printf("(Wid%i)",parseInt());//&debug
es_marble 24:7d2ff444d6d8 202 timeout_count = 0; //Reset timeout count
es_marble 24:7d2ff444d6d8 203 send = 0; //Indicate we are done sending the text message
es_marble 24:7d2ff444d6d8 204 if (undeleted_msgs) //Check if we need to delete read messages
es_marble 24:7d2ff444d6d8 205 gsm_current_state = GSM_DEL_R_MSGS; //Only delete messages if there are no unread messages
es_marble 24:7d2ff444d6d8 206 else
es_marble 18:7642909bfcfc 207 {
es_marble 24:7d2ff444d6d8 208 gsm_current_state = GSM_READ_MSG; //Otherwise read text messages again
es_marble 24:7d2ff444d6d8 209 pc.printf("(Dnone)");//&debug
es_marble 18:7642909bfcfc 210 }
es_marble 18:7642909bfcfc 211 }
es_marble 16:6807d437cd48 212 else
es_marble 24:7d2ff444d6d8 213 {
es_marble 24:7d2ff444d6d8 214 pc.printf("(Werr)"); //&debug
es_marble 24:7d2ff444d6d8 215 gsm_current_state = GSM_AT_CMGS; //If failed, try resending the message (i.e. continue until timeout)
es_marble 24:7d2ff444d6d8 216 }
danilob 0:41904adca656 217 break;
es_marble 24:7d2ff444d6d8 218 case GSM_DEL_R_MSGS:
es_marble 24:7d2ff444d6d8 219 if (findInQueue(AT_DEL_R_MSGS_RESPONSE, true))
es_marble 18:7642909bfcfc 220 {
es_marble 24:7d2ff444d6d8 221 undeleted_msgs = false;
es_marble 24:7d2ff444d6d8 222 pc.printf("(Dsucc)"); //&debug
es_marble 18:7642909bfcfc 223 }
es_marble 24:7d2ff444d6d8 224 else
es_marble 24:7d2ff444d6d8 225 pc.printf("(Derr)"); //&debug
es_marble 24:7d2ff444d6d8 226 gsm_current_state = GSM_READ_MSG;
danilob 0:41904adca656 227 break;
danilob 0:41904adca656 228 default:
es_marble 24:7d2ff444d6d8 229 pc.printf("This is a state error\r\n");
danilob 0:41904adca656 230 }
danilob 0:41904adca656 231 }
es_marble 24:7d2ff444d6d8 232
es_marble 16:6807d437cd48 233 //Mealy output logic ------------------------------------------------------
es_marble 16:6807d437cd48 234 void gsm_mealyOutputs()
es_marble 16:6807d437cd48 235 {
es_marble 16:6807d437cd48 236 switch(gsm_current_state)
es_marble 16:6807d437cd48 237 {
es_marble 16:6807d437cd48 238 case GSM_INITIALIZE:
es_marble 16:6807d437cd48 239 break;
es_marble 16:6807d437cd48 240 case GSM_AT_OK:
es_marble 24:7d2ff444d6d8 241 sendCommand(AT_OK);
es_marble 16:6807d437cd48 242 break;
es_marble 16:6807d437cd48 243 case GSM_AT_CSQ:
es_marble 24:7d2ff444d6d8 244 sendCommand(AT_CSQ);
es_marble 16:6807d437cd48 245 break;
es_marble 16:6807d437cd48 246 case GSM_AT_CREG:
es_marble 24:7d2ff444d6d8 247 sendCommand(AT_CREG);
es_marble 16:6807d437cd48 248 break;
es_marble 24:7d2ff444d6d8 249 case GSM_AT_CNMI:
es_marble 24:7d2ff444d6d8 250 sendCommand(AT_CNMI);
es_marble 24:7d2ff444d6d8 251 break;
es_marble 24:7d2ff444d6d8 252 case GSM_AT_CMGF:
es_marble 24:7d2ff444d6d8 253 sendCommand(AT_CMGF);
es_marble 24:7d2ff444d6d8 254 break;
es_marble 24:7d2ff444d6d8 255 case GSM_READ_MSG:
es_marble 24:7d2ff444d6d8 256 sendCommand(AT_READ_MSG);
es_marble 24:7d2ff444d6d8 257 break;
es_marble 16:6807d437cd48 258 case GSM_AT_CMGS:
es_marble 24:7d2ff444d6d8 259 sendCommand(AT_CMGS);
es_marble 16:6807d437cd48 260 break;
es_marble 16:6807d437cd48 261 case GSM_AT_SENDSMS:
es_marble 24:7d2ff444d6d8 262 sendCommand(gsm_msg); //end char included
es_marble 16:6807d437cd48 263 break;
es_marble 24:7d2ff444d6d8 264 case GSM_DEL_R_MSGS:
es_marble 24:7d2ff444d6d8 265 sendCommand(AT_DEL_R_MSGS);
danilob 19:a442b5a0116f 266 break;
es_marble 16:6807d437cd48 267 default:
es_marble 24:7d2ff444d6d8 268 pc.printf("This is a state error\r\n");
es_marble 16:6807d437cd48 269 }
es_marble 16:6807d437cd48 270 }
es_marble 18:7642909bfcfc 271 //Initialize the GSM
es_marble 16:6807d437cd48 272 void gsm_initialize(){
danilob 7:6c0b6ab3cafe 273 SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK; //enabling dmamux clock
danilob 12:f3ccc43c4d3c 274 SIM_SCGC7 |= SIM_SCGC7_DMA_MASK; // enebaling dma clock
es_marble 24:7d2ff444d6d8 275 pc.printf("initializing DMA...\r\n");
danilob 0:41904adca656 276 // control register mux, enabling uart3 receive
danilob 0:41904adca656 277 DMAMUX_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK|DMAMUX_CHCFG_SOURCE(8);
danilob 0:41904adca656 278
danilob 0:41904adca656 279 // Enable request signal for channel 0
danilob 0:41904adca656 280 DMA_ERQ = DMA_ERQ_ERQ0_MASK;
danilob 0:41904adca656 281
danilob 0:41904adca656 282 // select round-robin arbitration priority
danilob 0:41904adca656 283 DMA_CR |= DMA_CR_ERCA_MASK;
danilob 0:41904adca656 284
danilob 0:41904adca656 285 //enabled error interrupt for DMA0
danilob 0:41904adca656 286 //DMA_EEI = DMA_EEI_EEI0_MASK ;
danilob 0:41904adca656 287 //Addres for buffer
danilob 0:41904adca656 288 DMA_TCD0_SADDR = (uint32_t) &UART_D_REG(UART3_BASE_PTR);
danilob 0:41904adca656 289 DMA_TCD0_DADDR = (uint32_t) buffer;
danilob 0:41904adca656 290 // Set an offset for source and destination address
danilob 0:41904adca656 291 DMA_TCD0_SOFF = 0x00;
danilob 0:41904adca656 292 DMA_TCD0_DOFF = 0x01; // Destination address offset of 1 byte per transaction
danilob 0:41904adca656 293
danilob 0:41904adca656 294 // Set source and destination data transfer size
danilob 0:41904adca656 295 DMA_TCD0_ATTR = DMA_ATTR_SSIZE(0) | DMA_ATTR_DSIZE(0);
danilob 0:41904adca656 296
danilob 0:41904adca656 297 // Number of bytes to be transfered in each service request of the channel
danilob 0:41904adca656 298 DMA_TCD0_NBYTES_MLNO = 0x01;
danilob 0:41904adca656 299 // Current major iteration count
danilob 0:41904adca656 300 DMA_TCD0_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(BUFFER_LENGTH);
danilob 0:41904adca656 301 DMA_TCD0_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(BUFFER_LENGTH);
danilob 0:41904adca656 302 // Adjustment value used to restore the source and destiny address to the initial value
danilob 0:41904adca656 303 // 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 304 DMA_TCD0_SLAST = 0; // Source address adjustment
es_marble 16:6807d437cd48 305 DMA_TCD0_DLASTSGA = -BUFFER_LENGTH; // Destination address adjustment
danilob 0:41904adca656 306 // Setup control and status register
danilob 0:41904adca656 307 DMA_TCD0_CSR = 0;
danilob 0:41904adca656 308
danilob 0:41904adca656 309 // enable interrupt call at end of major loop
danilob 0:41904adca656 310 DMA_TCD0_CSR |= DMA_CSR_INTMAJOR_MASK;
danilob 0:41904adca656 311
danilob 0:41904adca656 312 //Activate dma trasnfer rx interrupt
danilob 0:41904adca656 313 UART_C2_REG(UART3) |= UART_C2_RIE_MASK;
danilob 0:41904adca656 314 UART_C5_REG(UART3) |= UART_C5_RDMAS_MASK | UART_C5_ILDMAS_MASK | UART_C5_LBKDDMAS_MASK;
danilob 0:41904adca656 315 //activate p fifo
danilob 0:41904adca656 316 UART_PFIFO_REG(UART3) |= UART_PFIFO_RXFE_MASK; //RXFE and buffer size of 1 word
danilob 7:6c0b6ab3cafe 317 queueInit();
es_marble 24:7d2ff444d6d8 318 pc.printf("done...\n\r");
danilob 0:41904adca656 319 }
es_marble 24:7d2ff444d6d8 320
es_marble 24:7d2ff444d6d8 321
es_marble 24:7d2ff444d6d8 322
danilob 0:41904adca656 323 //initialization debuging purposes
danilob 0:41904adca656 324 void print_registers() {
danilob 0:41904adca656 325
danilob 0:41904adca656 326
danilob 0:41904adca656 327 pc.printf("\n\rDMA REGISTERS\n\r");
danilob 0:41904adca656 328 pc.printf("DMA_MUX: 0x%08x\r\n",DMAMUX_CHCFG0);
danilob 0:41904adca656 329 pc.printf("SADDR0: 0x%08x\r\n",DMA_TCD0_SADDR);
danilob 0:41904adca656 330 pc.printf("DADDR0: 0x%08x\r\n",DMA_TCD0_DADDR);
danilob 0:41904adca656 331 pc.printf("CITER0: 0x%08x\r\n",DMA_TCD0_CITER_ELINKNO);
danilob 0:41904adca656 332 pc.printf("BITER0: 0x%08x\r\n",DMA_TCD0_BITER_ELINKNO);
danilob 0:41904adca656 333 pc.printf("DMA_CR: %08x\r\n", DMA_CR);
danilob 0:41904adca656 334 pc.printf("DMA_ES: %08x\r\n", DMA_ES);
danilob 0:41904adca656 335 pc.printf("DMA_ERQ: %08x\r\n", DMA_ERQ);
danilob 0:41904adca656 336 pc.printf("DMA_EEI: %08x\r\n", DMA_EEI);
danilob 0:41904adca656 337 pc.printf("DMA_CEEI: %02x\r\n", DMA_CEEI);
danilob 0:41904adca656 338 pc.printf("DMA_SEEI: %02x\r\n", DMA_SEEI);
danilob 0:41904adca656 339 pc.printf("DMA_CERQ: %02x\r\n", DMA_CERQ);
danilob 0:41904adca656 340 pc.printf("DMA_SERQ: %02x\r\n", DMA_SERQ);
danilob 0:41904adca656 341 pc.printf("DMA_CDNE: %02x\r\n", DMA_CDNE);
danilob 0:41904adca656 342 pc.printf("DMA_SSRT: %02x\r\n", DMA_SSRT);
danilob 0:41904adca656 343 pc.printf("DMA_CERR: %02x\r\n", DMA_CERR);
danilob 0:41904adca656 344 pc.printf("DMA_CINT: %02x\r\n", DMA_CINT);
danilob 0:41904adca656 345 pc.printf("DMA_INT: %08x\r\n", DMA_INT);
danilob 0:41904adca656 346 pc.printf("DMA_ERR: %08x\r\n", DMA_ERR);
danilob 0:41904adca656 347 pc.printf("DMA_HRS: %08x\r\n", DMA_HRS);
danilob 0:41904adca656 348 pc.printf("DMA_TCD0_DOFF: %08x\r\n",DMA_TCD0_DOFF);
danilob 0:41904adca656 349 pc.printf("\n\rUART REGISTERS\n\r");
danilob 0:41904adca656 350 pc.printf("UART_BDH_REG: %08x\r\n",UART_BDH_REG(UART3));
danilob 0:41904adca656 351 pc.printf("UART_C1_REG: %08x\r\n",UART_C1_REG(UART3));
danilob 0:41904adca656 352 pc.printf("UART_C2_REG: %08x\r\n",UART_C2_REG(UART3));
danilob 0:41904adca656 353 pc.printf("UART_S1_REG: %08x\r\n",UART_S1_REG(UART3));
es_marble 16:6807d437cd48 354 pc.printf("UART_s2_REG: %08x\r\n",UART_S2_REG(UART3));
danilob 0:41904adca656 355 pc.printf("UART_C3_REG: %08x\r\n",UART_C3_REG(UART3));
danilob 0:41904adca656 356 pc.printf("UART_D_REG: %08x\r\n",UART_D_REG(UART3));
danilob 0:41904adca656 357 pc.printf("UART_MA1_REG: %08x\r\n",UART_MA1_REG(UART3));
danilob 0:41904adca656 358 pc.printf("UART_MA2_REG: %08x\r\n",UART_MA2_REG(UART3));
danilob 0:41904adca656 359 pc.printf("UART_C4_REG: %08x\r\n",UART_C4_REG(UART3));
danilob 0:41904adca656 360 pc.printf("UART_C5_REG: %08x\r\n",UART_C5_REG(UART3));
es_marble 16:6807d437cd48 361 pc.printf("UART_ED_REG: %08x\r\n",UART_ED_REG(UART3));
danilob 0:41904adca656 362 pc.printf("UART_MODEM_REG: %08x\r\n",UART_MODEM_REG(UART3));
danilob 0:41904adca656 363 pc.printf("UART_IR_REG: %08x\r\n",UART_IR_REG(UART3));
danilob 0:41904adca656 364 pc.printf("UART_PFIFO_REG: %08x\r\n",UART_PFIFO_REG(UART3));
danilob 0:41904adca656 365 pc.printf("UART_CFIFO_REG: %08x\r\n",UART_CFIFO_REG(UART3));
danilob 0:41904adca656 366 pc.printf("UART_SFIFO_REG: %08x\r\n",UART_SFIFO_REG(UART3));
danilob 0:41904adca656 367 pc.printf("UART_TWFIFO_REG: %08x\r\n",UART_TWFIFO_REG(UART3));
danilob 0:41904adca656 368 pc.printf("UART_TCFIFO_REG: %08x\r\n",UART_TCFIFO_REG(UART3));
danilob 0:41904adca656 369 pc.printf("UART_RWFIFO_REG: %08x\r\n",UART_RWFIFO_REG(UART3));
danilob 0:41904adca656 370 pc.printf("UART_RCFIFO_REG: %08x\r\n",UART_RCFIFO_REG(UART3));
danilob 0:41904adca656 371
danilob 0:41904adca656 372 }