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:
Sat Apr 11 23:21:46 2015 +0000
Revision:
24:7d2ff444d6d8
Parent:
23:5227fb014aad
Child:
25:9de265c5bb28
added final GSM_Library code

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.
es_marble 24:7d2ff444d6d8 8 #define BEGIN_PHOTODIODE_DATA "CLS:"
es_marble 24:7d2ff444d6d8 9 #define BEGIN_GPS_DATA "GPS:"
danilob 0:41904adca656 10
es_marble 24:7d2ff444d6d8 11 //Extra defines for transmitter
es_marble 24:7d2ff444d6d8 12 #define START_SMS_TRANSMISSION "START"
es_marble 24:7d2ff444d6d8 13 #define STOP_SMS_TRANSMISSION "STOP"
es_marble 24:7d2ff444d6d8 14 #define GPS_SMS_TRANSMISSION "GPS"
es_marble 24:7d2ff444d6d8 15 #define RECEIVER_PHONE_NUMBER "\"+13853357314\""
es_marble 24:7d2ff444d6d8 16 #define AT_CMGS "AT+CMGS=" RECEIVER_PHONE_NUMBER
es_marble 24:7d2ff444d6d8 17 #define NUM_SIZE 25
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 24:7d2ff444d6d8 91 //Concatenate photodiode data
es_marble 24:7d2ff444d6d8 92 gsm_msg[0] = NULL;
es_marble 24:7d2ff444d6d8 93 strcat(gsm_msg, BEGIN_PHOTODIODE_DATA);
es_marble 24:7d2ff444d6d8 94 snprintf(num, NUM_SIZE, ",%d:%d:%d,", hh, mm, ss);
es_marble 24:7d2ff444d6d8 95 strcat(gsm_msg, num);
es_marble 24:7d2ff444d6d8 96 snprintf(num, NUM_SIZE, "%f,", L);
es_marble 24:7d2ff444d6d8 97 strcat(gsm_msg, num);
es_marble 24:7d2ff444d6d8 98 snprintf(num, NUM_SIZE, "%f,", Lref);
es_marble 24:7d2ff444d6d8 99 strcat(gsm_msg, num);
es_marble 24:7d2ff444d6d8 100 snprintf(num, NUM_SIZE, "%f,", S);
es_marble 24:7d2ff444d6d8 101 strcat(gsm_msg, num);
es_marble 24:7d2ff444d6d8 102 snprintf(num, NUM_SIZE, "%f;\n", Sref);
es_marble 24:7d2ff444d6d8 103 strcat(gsm_msg, num);
es_marble 24:7d2ff444d6d8 104
es_marble 24:7d2ff444d6d8 105 //Concatenate gps data
es_marble 24:7d2ff444d6d8 106 strcat(gsm_msg, BEGIN_GPS_DATA);
es_marble 24:7d2ff444d6d8 107 if (ns != NULL) //If there is a gps fix, ns will be set
es_marble 24:7d2ff444d6d8 108 {
es_marble 24:7d2ff444d6d8 109 snprintf(num, NUM_SIZE, ",%.4f%c,%.4f%c;", lat, ns, lon, we);
es_marble 24:7d2ff444d6d8 110 strcat(gsm_msg, num);
es_marble 24:7d2ff444d6d8 111 }
es_marble 24:7d2ff444d6d8 112 else strcat(gsm_msg, ",NONE;");
es_marble 24:7d2ff444d6d8 113
es_marble 24:7d2ff444d6d8 114 send = true; //Mark that we are currently sending a message
es_marble 24:7d2ff444d6d8 115 strcat(gsm_msg, SMS_END_CHAR); //Add SMS end char
es_marble 18:7642909bfcfc 116 }
es_marble 24:7d2ff444d6d8 117
es_marble 24:7d2ff444d6d8 118 //Return true if gsm is ready to send sms
es_marble 24:7d2ff444d6d8 119 //This only occurs if send = false (not currently sending a message) AND gsm received start sequence
es_marble 18:7642909bfcfc 120 bool gsm_ready()
es_marble 18:7642909bfcfc 121 {
es_marble 24:7d2ff444d6d8 122 return ((!send) && send_enable) ? true : false;
es_marble 18:7642909bfcfc 123 }
es_marble 24:7d2ff444d6d8 124
es_marble 24:7d2ff444d6d8 125 //Reset the gsm. Currently this only resets the state and whether we are currently sending a message
es_marble 24:7d2ff444d6d8 126 //It does not reset send_enable or undeleted_msgs
es_marble 18:7642909bfcfc 127 void gsm_reset()
es_marble 18:7642909bfcfc 128 {
es_marble 24:7d2ff444d6d8 129 //If we are in the middle of sending a text message, exit this loop
es_marble 24:7d2ff444d6d8 130 if (gsm_current_state == GSM_AT_CMGS)
es_marble 24:7d2ff444d6d8 131 sendCommand(SMS_ESCAPE_CHAR);
es_marble 24:7d2ff444d6d8 132
es_marble 18:7642909bfcfc 133 gsm_current_state = GSM_INITIALIZE;
es_marble 24:7d2ff444d6d8 134 undeleted_msgs = true;
es_marble 24:7d2ff444d6d8 135 send = false;
es_marble 18:7642909bfcfc 136 }
es_marble 24:7d2ff444d6d8 137
es_marble 16:6807d437cd48 138 //Next state logic -----------------------------------------------------
es_marble 16:6807d437cd48 139 void gsm_nextStateLogic()
es_marble 16:6807d437cd48 140 {
es_marble 24:7d2ff444d6d8 141 //printQueue(); //$debug
es_marble 24:7d2ff444d6d8 142
es_marble 16:6807d437cd48 143 switch(gsm_current_state)
es_marble 16:6807d437cd48 144 {
danilob 0:41904adca656 145 case GSM_INITIALIZE:
danilob 2:8352ad91f2ee 146 timeout_count = 0;
es_marble 24:7d2ff444d6d8 147 gsm_current_state = GSM_AT_OK; //unconditional (check it)
danilob 0:41904adca656 148 break;
danilob 0:41904adca656 149 case GSM_AT_OK:
es_marble 24:7d2ff444d6d8 150 if (findInQueue(AT_OK_RESPONSE, true))
es_marble 16:6807d437cd48 151 gsm_current_state = GSM_AT_CSQ;
danilob 12:f3ccc43c4d3c 152 break;
danilob 12:f3ccc43c4d3c 153 case GSM_AT_CSQ:
es_marble 24:7d2ff444d6d8 154 if(findInQueue(AT_CSQ_RESPONSE, true))
es_marble 16:6807d437cd48 155 gsm_current_state = GSM_AT_CREG;
danilob 2:8352ad91f2ee 156 break;
danilob 0:41904adca656 157 case GSM_AT_CREG:
es_marble 24:7d2ff444d6d8 158 if(findInQueue(AT_CREG_RESPONSE, true))
es_marble 16:6807d437cd48 159 {
es_marble 24:7d2ff444d6d8 160 parseInt();
es_marble 24:7d2ff444d6d8 161 if(parseInt() == 1)
es_marble 24:7d2ff444d6d8 162 gsm_current_state = GSM_AT_CNMI;
danilob 12:f3ccc43c4d3c 163 }
danilob 0:41904adca656 164 break;
es_marble 24:7d2ff444d6d8 165 case GSM_AT_CNMI:
es_marble 24:7d2ff444d6d8 166 if(findInQueue(AT_CNMI_RESPONSE, true))
es_marble 24:7d2ff444d6d8 167 gsm_current_state = GSM_AT_CMGF;
es_marble 24:7d2ff444d6d8 168 break;
danilob 0:41904adca656 169 case GSM_AT_CMGF:
es_marble 24:7d2ff444d6d8 170 if(findInQueue(AT_CMGF_RESPONSE, true))
es_marble 24:7d2ff444d6d8 171 gsm_current_state = GSM_READ_MSG;
es_marble 24:7d2ff444d6d8 172 break;
es_marble 24:7d2ff444d6d8 173 case GSM_READ_MSG:
es_marble 24:7d2ff444d6d8 174 if(send_enable) //Check if we need to stop transmission of SMS
es_marble 24:7d2ff444d6d8 175 {
es_marble 24:7d2ff444d6d8 176 if(findInQueue(STOP_SMS_TRANSMISSION, true)) //Always stop sending if stop sequence received by SMS
es_marble 24:7d2ff444d6d8 177 {
es_marble 24:7d2ff444d6d8 178 undeleted_msgs = true;
es_marble 24:7d2ff444d6d8 179 send_enable = false; //Set send_enable to indicate we will stop sending SMS
es_marble 24:7d2ff444d6d8 180 }
es_marble 24:7d2ff444d6d8 181 else if (send) //Only continue sending if we didn't find stop sequence AND user requested that we send sms
es_marble 24:7d2ff444d6d8 182 gsm_current_state = GSM_AT_CMGS; //Continue sending SMS (change state accordingly)
es_marble 24:7d2ff444d6d8 183 //Implicit: otherwise we will continue checking text messages in this state.
es_marble 24:7d2ff444d6d8 184 }
es_marble 24:7d2ff444d6d8 185 else
es_marble 24:7d2ff444d6d8 186 {
es_marble 24:7d2ff444d6d8 187 if(findInQueue(START_SMS_TRANSMISSION, true))
es_marble 24:7d2ff444d6d8 188 {
es_marble 24:7d2ff444d6d8 189 undeleted_msgs = true;
es_marble 24:7d2ff444d6d8 190 send_enable = true;
es_marble 24:7d2ff444d6d8 191 if (send) //Only continue sending if we found start sequence AND user requested that we send sms
es_marble 24:7d2ff444d6d8 192 gsm_current_state = GSM_AT_CMGS; //Start sending SMS (change state accordingly)
es_marble 24:7d2ff444d6d8 193 }
es_marble 24:7d2ff444d6d8 194 }
danilob 0:41904adca656 195 break;
danilob 0:41904adca656 196 case GSM_AT_CMGS:
es_marble 24:7d2ff444d6d8 197 if(findInQueue(AT_CMGS_RESPONSE, true))
es_marble 16:6807d437cd48 198 gsm_current_state = GSM_AT_SENDSMS;
danilob 0:41904adca656 199 break;
danilob 0:41904adca656 200 case GSM_AT_SENDSMS:
es_marble 24:7d2ff444d6d8 201 if(findInQueue(AT_SENDSMS_RESPONSE, true))
es_marble 18:7642909bfcfc 202 {
es_marble 24:7d2ff444d6d8 203 pc.printf("(Wid%i)",parseInt());//&debug
es_marble 24:7d2ff444d6d8 204 timeout_count = 0; //Reset timeout count
es_marble 24:7d2ff444d6d8 205 send = 0; //Indicate we are done sending the text message
es_marble 24:7d2ff444d6d8 206 if (undeleted_msgs) //Check if we need to delete read messages
es_marble 24:7d2ff444d6d8 207 gsm_current_state = GSM_DEL_R_MSGS; //Only delete messages if there are no unread messages
es_marble 24:7d2ff444d6d8 208 else
es_marble 18:7642909bfcfc 209 {
es_marble 24:7d2ff444d6d8 210 gsm_current_state = GSM_READ_MSG; //Otherwise read text messages again
es_marble 24:7d2ff444d6d8 211 pc.printf("(Dnone)");//&debug
es_marble 18:7642909bfcfc 212 }
es_marble 18:7642909bfcfc 213 }
es_marble 16:6807d437cd48 214 else
es_marble 24:7d2ff444d6d8 215 {
es_marble 24:7d2ff444d6d8 216 pc.printf("(Werr)"); //&debug
es_marble 24:7d2ff444d6d8 217 gsm_current_state = GSM_AT_CMGS; //If failed, try resending the message (i.e. continue until timeout)
es_marble 24:7d2ff444d6d8 218 }
danilob 0:41904adca656 219 break;
es_marble 24:7d2ff444d6d8 220 case GSM_DEL_R_MSGS:
es_marble 24:7d2ff444d6d8 221 if (findInQueue(AT_DEL_R_MSGS_RESPONSE, true))
es_marble 18:7642909bfcfc 222 {
es_marble 24:7d2ff444d6d8 223 undeleted_msgs = false;
es_marble 24:7d2ff444d6d8 224 pc.printf("(Dsucc)"); //&debug
es_marble 18:7642909bfcfc 225 }
es_marble 24:7d2ff444d6d8 226 else
es_marble 24:7d2ff444d6d8 227 pc.printf("(Derr)"); //&debug
es_marble 24:7d2ff444d6d8 228 gsm_current_state = GSM_READ_MSG;
danilob 0:41904adca656 229 break;
danilob 0:41904adca656 230 default:
es_marble 24:7d2ff444d6d8 231 pc.printf("This is a state error\r\n");
danilob 0:41904adca656 232 }
danilob 0:41904adca656 233 }
es_marble 24:7d2ff444d6d8 234
es_marble 16:6807d437cd48 235 //Mealy output logic ------------------------------------------------------
es_marble 16:6807d437cd48 236 void gsm_mealyOutputs()
es_marble 16:6807d437cd48 237 {
es_marble 16:6807d437cd48 238 switch(gsm_current_state)
es_marble 16:6807d437cd48 239 {
es_marble 16:6807d437cd48 240 case GSM_INITIALIZE:
es_marble 16:6807d437cd48 241 break;
es_marble 16:6807d437cd48 242 case GSM_AT_OK:
es_marble 24:7d2ff444d6d8 243 sendCommand(AT_OK);
es_marble 16:6807d437cd48 244 break;
es_marble 16:6807d437cd48 245 case GSM_AT_CSQ:
es_marble 24:7d2ff444d6d8 246 sendCommand(AT_CSQ);
es_marble 16:6807d437cd48 247 break;
es_marble 16:6807d437cd48 248 case GSM_AT_CREG:
es_marble 24:7d2ff444d6d8 249 sendCommand(AT_CREG);
es_marble 16:6807d437cd48 250 break;
es_marble 24:7d2ff444d6d8 251 case GSM_AT_CNMI:
es_marble 24:7d2ff444d6d8 252 sendCommand(AT_CNMI);
es_marble 24:7d2ff444d6d8 253 break;
es_marble 24:7d2ff444d6d8 254 case GSM_AT_CMGF:
es_marble 24:7d2ff444d6d8 255 sendCommand(AT_CMGF);
es_marble 24:7d2ff444d6d8 256 break;
es_marble 24:7d2ff444d6d8 257 case GSM_READ_MSG:
es_marble 24:7d2ff444d6d8 258 sendCommand(AT_READ_MSG);
es_marble 24:7d2ff444d6d8 259 break;
es_marble 16:6807d437cd48 260 case GSM_AT_CMGS:
es_marble 24:7d2ff444d6d8 261 sendCommand(AT_CMGS);
es_marble 16:6807d437cd48 262 break;
es_marble 16:6807d437cd48 263 case GSM_AT_SENDSMS:
es_marble 24:7d2ff444d6d8 264 sendCommand(gsm_msg); //end char included
es_marble 16:6807d437cd48 265 break;
es_marble 24:7d2ff444d6d8 266 case GSM_DEL_R_MSGS:
es_marble 24:7d2ff444d6d8 267 sendCommand(AT_DEL_R_MSGS);
danilob 19:a442b5a0116f 268 break;
es_marble 16:6807d437cd48 269 default:
es_marble 24:7d2ff444d6d8 270 pc.printf("This is a state error\r\n");
es_marble 16:6807d437cd48 271 }
es_marble 16:6807d437cd48 272 }
es_marble 18:7642909bfcfc 273 //Initialize the GSM
es_marble 16:6807d437cd48 274 void gsm_initialize(){
danilob 7:6c0b6ab3cafe 275 SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK; //enabling dmamux clock
danilob 12:f3ccc43c4d3c 276 SIM_SCGC7 |= SIM_SCGC7_DMA_MASK; // enebaling dma clock
es_marble 24:7d2ff444d6d8 277 pc.printf("initializing DMA...\r\n");
danilob 0:41904adca656 278 // control register mux, enabling uart3 receive
danilob 0:41904adca656 279 DMAMUX_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK|DMAMUX_CHCFG_SOURCE(8);
danilob 0:41904adca656 280
danilob 0:41904adca656 281 // Enable request signal for channel 0
danilob 0:41904adca656 282 DMA_ERQ = DMA_ERQ_ERQ0_MASK;
danilob 0:41904adca656 283
danilob 0:41904adca656 284 // select round-robin arbitration priority
danilob 0:41904adca656 285 DMA_CR |= DMA_CR_ERCA_MASK;
danilob 0:41904adca656 286
danilob 0:41904adca656 287 //enabled error interrupt for DMA0
danilob 0:41904adca656 288 //DMA_EEI = DMA_EEI_EEI0_MASK ;
danilob 0:41904adca656 289 //Addres for buffer
danilob 0:41904adca656 290 DMA_TCD0_SADDR = (uint32_t) &UART_D_REG(UART3_BASE_PTR);
danilob 0:41904adca656 291 DMA_TCD0_DADDR = (uint32_t) buffer;
danilob 0:41904adca656 292 // Set an offset for source and destination address
danilob 0:41904adca656 293 DMA_TCD0_SOFF = 0x00;
danilob 0:41904adca656 294 DMA_TCD0_DOFF = 0x01; // Destination address offset of 1 byte per transaction
danilob 0:41904adca656 295
danilob 0:41904adca656 296 // Set source and destination data transfer size
danilob 0:41904adca656 297 DMA_TCD0_ATTR = DMA_ATTR_SSIZE(0) | DMA_ATTR_DSIZE(0);
danilob 0:41904adca656 298
danilob 0:41904adca656 299 // Number of bytes to be transfered in each service request of the channel
danilob 0:41904adca656 300 DMA_TCD0_NBYTES_MLNO = 0x01;
danilob 0:41904adca656 301 // Current major iteration count
danilob 0:41904adca656 302 DMA_TCD0_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(BUFFER_LENGTH);
danilob 0:41904adca656 303 DMA_TCD0_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(BUFFER_LENGTH);
danilob 0:41904adca656 304 // Adjustment value used to restore the source and destiny address to the initial value
danilob 0:41904adca656 305 // 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 306 DMA_TCD0_SLAST = 0; // Source address adjustment
es_marble 16:6807d437cd48 307 DMA_TCD0_DLASTSGA = -BUFFER_LENGTH; // Destination address adjustment
danilob 0:41904adca656 308 // Setup control and status register
danilob 0:41904adca656 309 DMA_TCD0_CSR = 0;
danilob 0:41904adca656 310
danilob 0:41904adca656 311 // enable interrupt call at end of major loop
danilob 0:41904adca656 312 DMA_TCD0_CSR |= DMA_CSR_INTMAJOR_MASK;
danilob 0:41904adca656 313
danilob 0:41904adca656 314 //Activate dma trasnfer rx interrupt
danilob 0:41904adca656 315 UART_C2_REG(UART3) |= UART_C2_RIE_MASK;
danilob 0:41904adca656 316 UART_C5_REG(UART3) |= UART_C5_RDMAS_MASK | UART_C5_ILDMAS_MASK | UART_C5_LBKDDMAS_MASK;
danilob 0:41904adca656 317 //activate p fifo
danilob 0:41904adca656 318 UART_PFIFO_REG(UART3) |= UART_PFIFO_RXFE_MASK; //RXFE and buffer size of 1 word
danilob 7:6c0b6ab3cafe 319 queueInit();
es_marble 24:7d2ff444d6d8 320 pc.printf("done...\n\r");
danilob 0:41904adca656 321 }
es_marble 24:7d2ff444d6d8 322
es_marble 24:7d2ff444d6d8 323
es_marble 24:7d2ff444d6d8 324
danilob 0:41904adca656 325 //initialization debuging purposes
danilob 0:41904adca656 326 void print_registers() {
danilob 0:41904adca656 327
danilob 0:41904adca656 328
danilob 0:41904adca656 329 pc.printf("\n\rDMA REGISTERS\n\r");
danilob 0:41904adca656 330 pc.printf("DMA_MUX: 0x%08x\r\n",DMAMUX_CHCFG0);
danilob 0:41904adca656 331 pc.printf("SADDR0: 0x%08x\r\n",DMA_TCD0_SADDR);
danilob 0:41904adca656 332 pc.printf("DADDR0: 0x%08x\r\n",DMA_TCD0_DADDR);
danilob 0:41904adca656 333 pc.printf("CITER0: 0x%08x\r\n",DMA_TCD0_CITER_ELINKNO);
danilob 0:41904adca656 334 pc.printf("BITER0: 0x%08x\r\n",DMA_TCD0_BITER_ELINKNO);
danilob 0:41904adca656 335 pc.printf("DMA_CR: %08x\r\n", DMA_CR);
danilob 0:41904adca656 336 pc.printf("DMA_ES: %08x\r\n", DMA_ES);
danilob 0:41904adca656 337 pc.printf("DMA_ERQ: %08x\r\n", DMA_ERQ);
danilob 0:41904adca656 338 pc.printf("DMA_EEI: %08x\r\n", DMA_EEI);
danilob 0:41904adca656 339 pc.printf("DMA_CEEI: %02x\r\n", DMA_CEEI);
danilob 0:41904adca656 340 pc.printf("DMA_SEEI: %02x\r\n", DMA_SEEI);
danilob 0:41904adca656 341 pc.printf("DMA_CERQ: %02x\r\n", DMA_CERQ);
danilob 0:41904adca656 342 pc.printf("DMA_SERQ: %02x\r\n", DMA_SERQ);
danilob 0:41904adca656 343 pc.printf("DMA_CDNE: %02x\r\n", DMA_CDNE);
danilob 0:41904adca656 344 pc.printf("DMA_SSRT: %02x\r\n", DMA_SSRT);
danilob 0:41904adca656 345 pc.printf("DMA_CERR: %02x\r\n", DMA_CERR);
danilob 0:41904adca656 346 pc.printf("DMA_CINT: %02x\r\n", DMA_CINT);
danilob 0:41904adca656 347 pc.printf("DMA_INT: %08x\r\n", DMA_INT);
danilob 0:41904adca656 348 pc.printf("DMA_ERR: %08x\r\n", DMA_ERR);
danilob 0:41904adca656 349 pc.printf("DMA_HRS: %08x\r\n", DMA_HRS);
danilob 0:41904adca656 350 pc.printf("DMA_TCD0_DOFF: %08x\r\n",DMA_TCD0_DOFF);
danilob 0:41904adca656 351 pc.printf("\n\rUART REGISTERS\n\r");
danilob 0:41904adca656 352 pc.printf("UART_BDH_REG: %08x\r\n",UART_BDH_REG(UART3));
danilob 0:41904adca656 353 pc.printf("UART_C1_REG: %08x\r\n",UART_C1_REG(UART3));
danilob 0:41904adca656 354 pc.printf("UART_C2_REG: %08x\r\n",UART_C2_REG(UART3));
danilob 0:41904adca656 355 pc.printf("UART_S1_REG: %08x\r\n",UART_S1_REG(UART3));
es_marble 16:6807d437cd48 356 pc.printf("UART_s2_REG: %08x\r\n",UART_S2_REG(UART3));
danilob 0:41904adca656 357 pc.printf("UART_C3_REG: %08x\r\n",UART_C3_REG(UART3));
danilob 0:41904adca656 358 pc.printf("UART_D_REG: %08x\r\n",UART_D_REG(UART3));
danilob 0:41904adca656 359 pc.printf("UART_MA1_REG: %08x\r\n",UART_MA1_REG(UART3));
danilob 0:41904adca656 360 pc.printf("UART_MA2_REG: %08x\r\n",UART_MA2_REG(UART3));
danilob 0:41904adca656 361 pc.printf("UART_C4_REG: %08x\r\n",UART_C4_REG(UART3));
danilob 0:41904adca656 362 pc.printf("UART_C5_REG: %08x\r\n",UART_C5_REG(UART3));
es_marble 16:6807d437cd48 363 pc.printf("UART_ED_REG: %08x\r\n",UART_ED_REG(UART3));
danilob 0:41904adca656 364 pc.printf("UART_MODEM_REG: %08x\r\n",UART_MODEM_REG(UART3));
danilob 0:41904adca656 365 pc.printf("UART_IR_REG: %08x\r\n",UART_IR_REG(UART3));
danilob 0:41904adca656 366 pc.printf("UART_PFIFO_REG: %08x\r\n",UART_PFIFO_REG(UART3));
danilob 0:41904adca656 367 pc.printf("UART_CFIFO_REG: %08x\r\n",UART_CFIFO_REG(UART3));
danilob 0:41904adca656 368 pc.printf("UART_SFIFO_REG: %08x\r\n",UART_SFIFO_REG(UART3));
danilob 0:41904adca656 369 pc.printf("UART_TWFIFO_REG: %08x\r\n",UART_TWFIFO_REG(UART3));
danilob 0:41904adca656 370 pc.printf("UART_TCFIFO_REG: %08x\r\n",UART_TCFIFO_REG(UART3));
danilob 0:41904adca656 371 pc.printf("UART_RWFIFO_REG: %08x\r\n",UART_RWFIFO_REG(UART3));
danilob 0:41904adca656 372 pc.printf("UART_RCFIFO_REG: %08x\r\n",UART_RCFIFO_REG(UART3));
danilob 0:41904adca656 373
danilob 0:41904adca656 374 }