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:
danilob
Date:
Thu Mar 05 23:12:09 2015 +0000
Revision:
7:6c0b6ab3cafe
Parent:
6:3ccc86304c2c
Child:
12:f3ccc43c4d3c
v6;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
danilob 0:41904adca656 1 #include "GSMLibrary.h"
danilob 0:41904adca656 2 #include "gsmqueue.h"
danilob 2:8352ad91f2ee 3 #include <string.h>
danilob 0:41904adca656 4
danilob 2:8352ad91f2ee 5 #define TIME_CONST 1
danilob 2:8352ad91f2ee 6 #define SECONDS_TIMEOUT 10
danilob 2:8352ad91f2ee 7 #define TIMEOUTLIMIT TIME_CONST //$change check with main code this will set up condition fior timeout.
danilob 0:41904adca656 8
danilob 0:41904adca656 9 //definition for AT comands
danilob 0:41904adca656 10 #define AT_OK "AT"
danilob 0:41904adca656 11 #define AT_CSQ "AT+CSQ"
danilob 0:41904adca656 12 #define AT_CREG "AT+CREG?"
danilob 0:41904adca656 13 #define AT_CMGF "AT+CMGF=1"
danilob 0:41904adca656 14 #define RECEIVER_PHONE_NUMBER "\"+18014722842\""
danilob 0:41904adca656 15 #define AT_CMGS "AT+CMGS=" RECEIVER_PHONE_NUMBER
danilob 0:41904adca656 16
danilob 0:41904adca656 17 //Definition for at repsonses
danilob 0:41904adca656 18 //Please notice that after ":" the gsm will usually send aditional information
danilob 0:41904adca656 19 #define AT_OK_RESPONSE "OK" //Response after sending "AT" message
es_marble 6:3ccc86304c2c 20 #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
es_marble 6:3ccc86304c2c 21 #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
danilob 0:41904adca656 22 #define AT_CMGF_RESPONSE "OK"
danilob 0:41904adca656 23 #define AT_CMGS_RESPONSE ">" //Message is written aftersymbol
danilob 0:41904adca656 24 #define AT_SENDSMS_RESPONSE "+CMGS:" // +CMGS: <id> this will include the message id. CMGS ERROR for error and
danilob 0:41904adca656 25 #define AT_SUCCESS_REPSONSE "OK"
danilob 0:41904adca656 26
danilob 0:41904adca656 27
danilob 0:41904adca656 28 extern Serial pc;
danilob 0:41904adca656 29 extern Serial gsm;
danilob 0:41904adca656 30 extern uint8_t buffer[BUFFER_LENGTH];//buffer storing char
danilob 0:41904adca656 31 gsm_states gsm_current_state = GSM_INITIALIZE;
danilob 0:41904adca656 32
danilob 0:41904adca656 33 char correct = 0;
danilob 0:41904adca656 34 char send = 0;
danilob 2:8352ad91f2ee 35 char timeout_count = 0;
danilob 0:41904adca656 36 char received = 0;
danilob 2:8352ad91f2ee 37 char timeout_limit = TIMEOUTLIMIT;
danilob 0:41904adca656 38
danilob 0:41904adca656 39 void gsm_tick(){
danilob 0:41904adca656 40
danilob 0:41904adca656 41 //post action
danilob 0:41904adca656 42 switch(gsm_current_state){
danilob 2:8352ad91f2ee 43 //when send flag is on , send AT_OK message to gsm.
danilob 0:41904adca656 44 case GSM_INITIALIZE:
danilob 2:8352ad91f2ee 45 pc.printf("gsm_initilize state\r\n");//&debug
danilob 2:8352ad91f2ee 46 correct = 0;
danilob 2:8352ad91f2ee 47 timeout_count = 0;
danilob 2:8352ad91f2ee 48 received = 0;
danilob 2:8352ad91f2ee 49 if(send){ //send first at_ok message
danilob 2:8352ad91f2ee 50 resetGSMIdleBit();
danilob 2:8352ad91f2ee 51 pc.printf("sending AT_OK\r\n");//&debug
danilob 0:41904adca656 52 gsm.puts(AT_OK);
danilob 0:41904adca656 53 gsm.puts("\r\n");
danilob 2:8352ad91f2ee 54 gsm_current_state = GSM_AT_OK;
danilob 0:41904adca656 55 }
danilob 0:41904adca656 56 else
danilob 0:41904adca656 57 gsm_current_state = GSM_INITIALIZE;
danilob 0:41904adca656 58 break;
danilob 2:8352ad91f2ee 59 // check for repsonse to AT and if correct send AT+CSQ message
danilob 0:41904adca656 60 case GSM_AT_OK:
danilob 2:8352ad91f2ee 61 timeout_count++;
danilob 2:8352ad91f2ee 62 pc.printf("inside AT_OK state\r\n");//&debug
danilob 2:8352ad91f2ee 63 if(getGSMIdleBit()){
danilob 2:8352ad91f2ee 64 printQueue(); //$debug
danilob 2:8352ad91f2ee 65 if(findInQueue(AT_OK)){
danilob 2:8352ad91f2ee 66 timeout_count = 0;
danilob 2:8352ad91f2ee 67 resetGSMIdleBit();
danilob 2:8352ad91f2ee 68 pc.printf("sending AT_CSQ\r\n");//&debug
danilob 2:8352ad91f2ee 69 gsm.puts(AT_CSQ);
danilob 2:8352ad91f2ee 70 gsm.puts("\r\n");
danilob 2:8352ad91f2ee 71 gsm_current_state = GSM_AT_CSQ;
danilob 2:8352ad91f2ee 72 }
danilob 2:8352ad91f2ee 73 else
danilob 2:8352ad91f2ee 74 gsm_current_state = GSM_INITIALIZE;
danilob 2:8352ad91f2ee 75 }
danilob 2:8352ad91f2ee 76 if(timeout_count >= timeout_limit)
danilob 2:8352ad91f2ee 77 gsm_current_state = GSM_INITIALIZE;
danilob 2:8352ad91f2ee 78 break;
danilob 0:41904adca656 79
danilob 0:41904adca656 80 case GSM_AT_CSQ:
danilob 7:6c0b6ab3cafe 81 pc.printf("gsm_csq state\r\n");//&debug
danilob 0:41904adca656 82 gsm_current_state = GSM_AT_CREG;
danilob 0:41904adca656 83 break;
danilob 0:41904adca656 84 case GSM_AT_CREG:
danilob 7:6c0b6ab3cafe 85 pc.printf("gsm_creg state\r\n");//&debug
danilob 0:41904adca656 86 gsm_current_state = GSM_AT_CMGF;
danilob 0:41904adca656 87 break;
danilob 0:41904adca656 88 case GSM_AT_CMGF:
danilob 7:6c0b6ab3cafe 89 pc.printf("gsm_cmgf state\r\n");//&debug
danilob 0:41904adca656 90 gsm_current_state = GSM_AT_CMGS;
danilob 0:41904adca656 91 break;
danilob 0:41904adca656 92 case GSM_AT_CMGS:
danilob 7:6c0b6ab3cafe 93 pc.printf("gsm_cmgs state\r\n");//&debug
danilob 0:41904adca656 94 gsm_current_state = GSM_AT_SENDSMS;
danilob 0:41904adca656 95 break;
danilob 0:41904adca656 96 case GSM_AT_SENDSMS:
danilob 7:6c0b6ab3cafe 97 pc.printf("gsm_send_sms state\r\n");//&debug
danilob 0:41904adca656 98 gsm_current_state = GSM_SUCCESS;
danilob 0:41904adca656 99 break;
danilob 0:41904adca656 100 case GSM_SUCCESS:
danilob 7:6c0b6ab3cafe 101 pc.printf("gsm_success state\r\n");//&debug
danilob 0:41904adca656 102 gsm_current_state = GSM_INITIALIZE;
danilob 0:41904adca656 103 break;
danilob 0:41904adca656 104 default:
danilob 0:41904adca656 105 pc.printf("This is a state error");
danilob 0:41904adca656 106 }
danilob 0:41904adca656 107
danilob 0:41904adca656 108 }
danilob 7:6c0b6ab3cafe 109 //set send falg on
danilob 7:6c0b6ab3cafe 110 void gsm_send_sms(){
danilob 7:6c0b6ab3cafe 111 send = 1;
danilob 7:6c0b6ab3cafe 112 }
danilob 0:41904adca656 113 //
danilob 0:41904adca656 114 void gsm_reset();
danilob 0:41904adca656 115
danilob 0:41904adca656 116
danilob 0:41904adca656 117 //
danilob 0:41904adca656 118 void gsm_initialize(){
danilob 7:6c0b6ab3cafe 119 SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK; //enabling dmamux clock
danilob 7:6c0b6ab3cafe 120 //SIM_SCGC7 |= SIM_SCGC7_DMA_MASK; // enebaling dma clock
danilob 0:41904adca656 121 pc.printf("initializing tregisters...!\r\n");
danilob 0:41904adca656 122 // control register mux, enabling uart3 receive
danilob 0:41904adca656 123 DMAMUX_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK|DMAMUX_CHCFG_SOURCE(8);
danilob 0:41904adca656 124
danilob 0:41904adca656 125 // Enable request signal for channel 0
danilob 0:41904adca656 126 DMA_ERQ = DMA_ERQ_ERQ0_MASK;
danilob 0:41904adca656 127
danilob 0:41904adca656 128 // select round-robin arbitration priority
danilob 0:41904adca656 129 DMA_CR |= DMA_CR_ERCA_MASK;
danilob 0:41904adca656 130
danilob 0:41904adca656 131 //enabled error interrupt for DMA0
danilob 0:41904adca656 132 //DMA_EEI = DMA_EEI_EEI0_MASK ;
danilob 0:41904adca656 133 //Addres for buffer
danilob 0:41904adca656 134 DMA_TCD0_SADDR = (uint32_t) &UART_D_REG(UART3_BASE_PTR);
danilob 0:41904adca656 135 DMA_TCD0_DADDR = (uint32_t) buffer;
danilob 0:41904adca656 136 // Set an offset for source and destination address
danilob 0:41904adca656 137 DMA_TCD0_SOFF = 0x00;
danilob 0:41904adca656 138 DMA_TCD0_DOFF = 0x01; // Destination address offset of 1 byte per transaction
danilob 0:41904adca656 139
danilob 0:41904adca656 140 // Set source and destination data transfer size
danilob 0:41904adca656 141 DMA_TCD0_ATTR = DMA_ATTR_SSIZE(0) | DMA_ATTR_DSIZE(0);
danilob 0:41904adca656 142
danilob 0:41904adca656 143 // Number of bytes to be transfered in each service request of the channel
danilob 0:41904adca656 144 DMA_TCD0_NBYTES_MLNO = 0x01;
danilob 0:41904adca656 145 // Current major iteration count
danilob 0:41904adca656 146 DMA_TCD0_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(BUFFER_LENGTH);
danilob 0:41904adca656 147 DMA_TCD0_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(BUFFER_LENGTH);
danilob 0:41904adca656 148 // Adjustment value used to restore the source and destiny address to the initial value
danilob 0:41904adca656 149 // 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)
danilob 0:41904adca656 150 DMA_TCD0_SLAST = 0; // Source address adjustment
danilob 0:41904adca656 151 DMA_TCD0_DLASTSGA = -BUFFER_LENGTH; // Destination address adjustment
danilob 0:41904adca656 152 // Setup control and status register
danilob 0:41904adca656 153 DMA_TCD0_CSR = 0;
danilob 0:41904adca656 154
danilob 0:41904adca656 155 // enable interrupt call at end of major loop
danilob 0:41904adca656 156 DMA_TCD0_CSR |= DMA_CSR_INTMAJOR_MASK;
danilob 0:41904adca656 157
danilob 0:41904adca656 158 //Activate dma trasnfer rx interrupt
danilob 0:41904adca656 159 UART_C2_REG(UART3) |= UART_C2_RIE_MASK;
danilob 0:41904adca656 160 UART_C5_REG(UART3) |= UART_C5_RDMAS_MASK | UART_C5_ILDMAS_MASK | UART_C5_LBKDDMAS_MASK;
danilob 0:41904adca656 161 //activate p fifo
danilob 0:41904adca656 162 UART_PFIFO_REG(UART3) |= UART_PFIFO_RXFE_MASK; //RXFE and buffer size of 1 word
danilob 7:6c0b6ab3cafe 163 queueInit();
danilob 0:41904adca656 164 pc.printf("Initialization done...\n\r");
danilob 0:41904adca656 165 }
danilob 0:41904adca656 166
danilob 0:41904adca656 167
danilob 0:41904adca656 168
danilob 0:41904adca656 169 //initialization debuging purposes
danilob 0:41904adca656 170 void print_registers() {
danilob 0:41904adca656 171
danilob 0:41904adca656 172
danilob 0:41904adca656 173 pc.printf("\n\rDMA REGISTERS\n\r");
danilob 0:41904adca656 174 pc.printf("DMA_MUX: 0x%08x\r\n",DMAMUX_CHCFG0);
danilob 0:41904adca656 175 pc.printf("SADDR0: 0x%08x\r\n",DMA_TCD0_SADDR);
danilob 0:41904adca656 176 pc.printf("DADDR0: 0x%08x\r\n",DMA_TCD0_DADDR);
danilob 0:41904adca656 177 pc.printf("CITER0: 0x%08x\r\n",DMA_TCD0_CITER_ELINKNO);
danilob 0:41904adca656 178 pc.printf("BITER0: 0x%08x\r\n",DMA_TCD0_BITER_ELINKNO);
danilob 0:41904adca656 179 pc.printf("DMA_CR: %08x\r\n", DMA_CR);
danilob 0:41904adca656 180 pc.printf("DMA_ES: %08x\r\n", DMA_ES);
danilob 0:41904adca656 181 pc.printf("DMA_ERQ: %08x\r\n", DMA_ERQ);
danilob 0:41904adca656 182 pc.printf("DMA_EEI: %08x\r\n", DMA_EEI);
danilob 0:41904adca656 183 pc.printf("DMA_CEEI: %02x\r\n", DMA_CEEI);
danilob 0:41904adca656 184 pc.printf("DMA_SEEI: %02x\r\n", DMA_SEEI);
danilob 0:41904adca656 185 pc.printf("DMA_CERQ: %02x\r\n", DMA_CERQ);
danilob 0:41904adca656 186 pc.printf("DMA_SERQ: %02x\r\n", DMA_SERQ);
danilob 0:41904adca656 187 pc.printf("DMA_CDNE: %02x\r\n", DMA_CDNE);
danilob 0:41904adca656 188 pc.printf("DMA_SSRT: %02x\r\n", DMA_SSRT);
danilob 0:41904adca656 189 pc.printf("DMA_CERR: %02x\r\n", DMA_CERR);
danilob 0:41904adca656 190 pc.printf("DMA_CINT: %02x\r\n", DMA_CINT);
danilob 0:41904adca656 191 pc.printf("DMA_INT: %08x\r\n", DMA_INT);
danilob 0:41904adca656 192 pc.printf("DMA_ERR: %08x\r\n", DMA_ERR);
danilob 0:41904adca656 193 pc.printf("DMA_HRS: %08x\r\n", DMA_HRS);
danilob 0:41904adca656 194 pc.printf("DMA_TCD0_DOFF: %08x\r\n",DMA_TCD0_DOFF);
danilob 0:41904adca656 195 pc.printf("\n\rUART REGISTERS\n\r");
danilob 0:41904adca656 196 pc.printf("UART_BDH_REG: %08x\r\n",UART_BDH_REG(UART3));
danilob 0:41904adca656 197 pc.printf("UART_C1_REG: %08x\r\n",UART_C1_REG(UART3));
danilob 0:41904adca656 198 pc.printf("UART_C2_REG: %08x\r\n",UART_C2_REG(UART3));
danilob 0:41904adca656 199 pc.printf("UART_S1_REG: %08x\r\n",UART_S1_REG(UART3));
danilob 0:41904adca656 200 pc.printf("UART_s2_REG: %08x\r\n",UART_S2_REG(UART3));
danilob 0:41904adca656 201 pc.printf("UART_C3_REG: %08x\r\n",UART_C3_REG(UART3));
danilob 0:41904adca656 202 pc.printf("UART_D_REG: %08x\r\n",UART_D_REG(UART3));
danilob 0:41904adca656 203 pc.printf("UART_MA1_REG: %08x\r\n",UART_MA1_REG(UART3));
danilob 0:41904adca656 204 pc.printf("UART_MA2_REG: %08x\r\n",UART_MA2_REG(UART3));
danilob 0:41904adca656 205 pc.printf("UART_C4_REG: %08x\r\n",UART_C4_REG(UART3));
danilob 0:41904adca656 206 pc.printf("UART_C5_REG: %08x\r\n",UART_C5_REG(UART3));
danilob 0:41904adca656 207 pc.printf("UART_ED_REG: %08x\r\n",UART_ED_REG(UART3));
danilob 0:41904adca656 208 pc.printf("UART_MODEM_REG: %08x\r\n",UART_MODEM_REG(UART3));
danilob 0:41904adca656 209 pc.printf("UART_IR_REG: %08x\r\n",UART_IR_REG(UART3));
danilob 0:41904adca656 210 pc.printf("UART_PFIFO_REG: %08x\r\n",UART_PFIFO_REG(UART3));
danilob 0:41904adca656 211 pc.printf("UART_CFIFO_REG: %08x\r\n",UART_CFIFO_REG(UART3));
danilob 0:41904adca656 212 pc.printf("UART_SFIFO_REG: %08x\r\n",UART_SFIFO_REG(UART3));
danilob 0:41904adca656 213 pc.printf("UART_TWFIFO_REG: %08x\r\n",UART_TWFIFO_REG(UART3));
danilob 0:41904adca656 214 pc.printf("UART_TCFIFO_REG: %08x\r\n",UART_TCFIFO_REG(UART3));
danilob 0:41904adca656 215 pc.printf("UART_RWFIFO_REG: %08x\r\n",UART_RWFIFO_REG(UART3));
danilob 0:41904adca656 216 pc.printf("UART_RCFIFO_REG: %08x\r\n",UART_RCFIFO_REG(UART3));
danilob 0:41904adca656 217
danilob 0:41904adca656 218 }