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

Fork of GSM_Library by DCS_TEAM

Committer:
es_marble
Date:
Thu Apr 23 02:06:55 2015 +0000
Revision:
28:81f1c8bd3299
Parent:
27:fe1c7eaf5b88
Child:
29:bc5f53f2922a
Added wait statement for initialization

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