DCS_TEAM / Mbed 2 deprecated DCS_FINAL_CODE

Dependencies:   Chemical_Sensor_DMA GPRS DPG_FINAL MBed_Adafruit-GPS-Library SDFileSystem Socket mbed

Files at this revision

API Documentation at this revision

Comitter:
danilob
Date:
Sat Mar 07 23:57:06 2015 +0000
Parent:
4:35d92f290cfe
Child:
6:e51f5c732a80
Commit message:
GSM added and ready maybe

Changed in this revision

GSMLibrary.cpp Show diff for this revision Revisions of this file
GSMLibrary.h Show diff for this revision Revisions of this file
GSMLibrary/GSMLibrary.cpp Show annotated file Show diff for this revision Revisions of this file
GSMLibrary/GSMLibrary.h Show annotated file Show diff for this revision Revisions of this file
GSMLibrary/gsmqueue.cpp Show annotated file Show diff for this revision Revisions of this file
GSMLibrary/gsmqueue.h Show annotated file Show diff for this revision Revisions of this file
Sensor.cpp Show annotated file Show diff for this revision Revisions of this file
gsmqueue.cpp Show diff for this revision Revisions of this file
gsmqueue.h Show diff for this revision Revisions of this file
--- a/GSMLibrary.cpp	Sat Mar 07 23:34:49 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,285 +0,0 @@
-#include "GSMLibrary.h"
-#include "gsmqueue.h"
-#include <string.h>
-
-#define TIME_CONST .3
-#define SECONDS_TIMEOUT 40
-#define TIMEOUTLIMIT SECONDS_TIMEOUT/TIME_CONST //$change check with main code this will set up condition fior timeout.
-
-//definition for AT comands
-#define AT_OK "AT"
-#define AT_CSQ "AT+CSQ" 
-#define AT_CREG "AT+CREG?"
-#define AT_CMGF "AT+CMGF=1"
-#define RECEIVER_PHONE_NUMBER "\"+18014722842\""
-#define AT_CMGS "AT+CMGS=" RECEIVER_PHONE_NUMBER 
-#define MESSAGE_BODY "stress test\32\32"
-
-//Definition for AT repsonses
-//Please notice that after ":" the gsm will usually send aditional information
-#define AT_OK_RESPONSE "OK" //Response after sending "AT" message
-#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 
-#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
-#define AT_CMGF_RESPONSE "OK"
-#define AT_CMGS_RESPONSE ">"  //Message is written aftersymbol
-#define AT_SENDSMS_RESPONSE ">" // +CMGS: <id> this will include the message id. CMGS ERROR for error and 
-#define AT_SUCCESS_REPSONSE "+CMGS:"
-
-//External variables
-extern Serial pc;
-extern Serial gsm;
-extern uint8_t buffer[BUFFER_LENGTH];//buffer storing char
-char gsm_message[MESSAGE_SIZE];
-
-//Internal variables
-gsm_states gsm_current_state = GSM_INITIALIZE;
-char send = 0;
-int timeout_count = 0;
-
-bool gsm_ready(){
-    if(send)
-        return false;
-    return true;    
-}
-
-void gsm_tick()
-{
-    if (getGSMIdleBit() || gsm_timeOut() || (send && gsm_current_state == GSM_INITIALIZE))  //question with send...
-    {
-        resetGSMIdleBit();  //reset GSM idle bit
-        gsm_nextStateLogic();   //Next state
-        gsm_mealyOutputs(); //Mealy outputs
-        flushQueue();       //Flush the queue
-    }
-}
-
-//Advance timeout counter; if timeout, return true
-bool gsm_timeOut()
-{
-    if(++timeout_count >= TIMEOUTLIMIT){
-        timeout_count=0; 
-        gsm_current_state = GSM_INITIALIZE;
-        return true;
-    }
-    else
-        return false;     
-}
-
-//Next state logic -----------------------------------------------------
-void gsm_nextStateLogic()
-{
-    printQueue(); //$debug
-    switch(gsm_current_state)
-    {
-        case GSM_INITIALIZE:
-            pc.printf("gsm_initialize state\r\n");//&debug
-            timeout_count = 0;
-            if (send)
-                gsm_current_state = GSM_AT_OK;   //unconditional (check it)
-            break;
-        case GSM_AT_OK:
-            pc.printf("inside AT_OK state\r\n");//&debug
-            if (findInQueue(AT_OK_RESPONSE))
-                gsm_current_state = GSM_AT_CSQ;
-            break;
-        case GSM_AT_CSQ:
-            pc.printf("inside AT_CSQ state \r\n");//&debug
-            if(findInQueue(AT_CSQ_RESPONSE))
-                gsm_current_state = GSM_AT_CREG;
-            break;
-        case GSM_AT_CREG:
-            pc.printf("gsm_creg state\r\n");//&debug
-            if(findInQueue(AT_CREG_RESPONSE))
-            {
-                pc.printf("creg parse Int1: %d\r\n",parseInt());//&debug
-                int q = parseInt();
-                pc.printf("creg parse Int2: %d\r\n",q);//&debug
-                if(q == 1)
-                    gsm_current_state = GSM_AT_CMGF;
-            } 
-            break;
-        case GSM_AT_CMGF:
-            pc.printf("gsm_cmgf state\r\n");//&debug
-            if(findInQueue(AT_CMGF_RESPONSE))
-                gsm_current_state = GSM_AT_CMGS;
-            break;
-        case GSM_AT_CMGS:
-            pc.printf("gsm_cmgs state\r\n");//&debug
-            if(findInQueue(AT_CMGS_RESPONSE))
-                gsm_current_state = GSM_AT_SENDSMS;
-            break;
-        case GSM_AT_SENDSMS:
-            pc.printf("gsm_send_sms state\r\n");//&debug
-            if(findInQueue(AT_SENDSMS_RESPONSE))
-                gsm_current_state = GSM_SUCCESS;
-            else
-                gsm_current_state = GSM_AT_CMGS;        //The only spot we can go backwards
-            break;
-        case GSM_SUCCESS:
-            pc.printf("gsm_success state\r\n");//&debug
-            if(findInQueue(AT_SENDSMS_RESPONSE))         //This appears to be a bug. It was in Danilo's original code as well.
-                pc.printf("Message SENT! msgID: %iY\r\n",parseInt());//&debug
-            gsm_current_state = GSM_INITIALIZE;//Confusing part. Do we always go backwards here?
-            send =0;
-            break;
-        default:
-            pc.printf("This is a state error");
-    }
-}
-
-//Mealy output logic ------------------------------------------------------
-void gsm_mealyOutputs()
-{
-    switch(gsm_current_state)
-    {
-        case GSM_INITIALIZE:
-            pc.printf("No Mealy initialize state output\r\n");//&debug
-            break;
-        case GSM_AT_OK:
-            pc.printf("sending AT_OK\r\n");//&debug
-            gsm.puts(AT_OK);
-            gsm.puts("\r\n"); 
-            break;
-        case GSM_AT_CSQ:
-            pc.printf("sending AT_CSQ\r\n");//&debug
-            gsm.puts(AT_CSQ);
-            gsm.puts("\r\n"); 
-            break;
-        case GSM_AT_CREG:
-            pc.printf("sending AT_CREG\r\n");//&debug
-            gsm.puts(AT_CREG);
-            gsm.puts("\r\n"); 
-            break;
-        case GSM_AT_CMGF:   
-            pc.printf("sending AT_CMGF\r\n");//&debug
-            gsm.puts(AT_CMGF);
-            gsm.puts("\r\n"); 
-            break;
-        case GSM_AT_CMGS:   
-            pc.printf("sending AT_CMGS\r\n");//&debug
-            gsm.puts(AT_CMGS);
-            gsm.puts("\r\n"); 
-            break;
-        case GSM_AT_SENDSMS:
-            pc.printf("sending MESSAGE_BODY\r\n");//&debug
-            gsm.puts(gsm_message); //substitute char included
-            gsm.puts("\r\n"); 
-            break;
-        case GSM_SUCCESS:
-            pc.printf("No Mealy success state output\r\n");//&debug 
-        default:
-            pc.printf("This is a state error");
-    }
-}
-
-//set send falg on
-void gsm_send_sms(char message[MESSAGE_SIZE]){
-    gsm_message = message;
-    send = 1;
-}
-
-//
-void gsm_reset();
-
-//
-void gsm_initialize(){  
-      SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK; //enabling dmamux clock
-      SIM_SCGC7 |= SIM_SCGC7_DMA_MASK;  // enebaling dma clock
-      pc.printf("initializing registers...!\r\n");
-     // control register mux, enabling uart3 receive        
-     DMAMUX_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK|DMAMUX_CHCFG_SOURCE(8); 
-     
-     // Enable request signal for channel 0 
-     DMA_ERQ = DMA_ERQ_ERQ0_MASK;
-     
-      // select round-robin arbitration priority
-     DMA_CR |= DMA_CR_ERCA_MASK;
-     
-     //enabled error interrupt for DMA0
-     //DMA_EEI = DMA_EEI_EEI0_MASK ;
-     //Addres for buffer
-     DMA_TCD0_SADDR = (uint32_t) &UART_D_REG(UART3_BASE_PTR);
-     DMA_TCD0_DADDR = (uint32_t) buffer;
-     // Set an offset for source and destination address
-     DMA_TCD0_SOFF = 0x00; 
-     DMA_TCD0_DOFF = 0x01; // Destination address offset of 1 byte per transaction
-     
-     // Set source and destination data transfer size
-     DMA_TCD0_ATTR = DMA_ATTR_SSIZE(0) | DMA_ATTR_DSIZE(0);
-     
-     // Number of bytes to be transfered in each service request of the channel
-     DMA_TCD0_NBYTES_MLNO = 0x01;
-     // Current major iteration count
-    DMA_TCD0_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(BUFFER_LENGTH);
-    DMA_TCD0_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(BUFFER_LENGTH);
-    // Adjustment value used to restore the source and destiny address to the initial value
-    // 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)
-    DMA_TCD0_SLAST = 0;   // Source address adjustment
-    DMA_TCD0_DLASTSGA = -BUFFER_LENGTH;  // Destination address adjustment   
-    // Setup control and status register
-    DMA_TCD0_CSR = 0;
-       
-    // enable interrupt call at end of major loop
-    DMA_TCD0_CSR |= DMA_CSR_INTMAJOR_MASK;
-    
-    //Activate dma trasnfer rx interrupt
-    UART_C2_REG(UART3) |= UART_C2_RIE_MASK;
-    UART_C5_REG(UART3) |= UART_C5_RDMAS_MASK | UART_C5_ILDMAS_MASK | UART_C5_LBKDDMAS_MASK;
-    //activate p fifo   
-    UART_PFIFO_REG(UART3) |= UART_PFIFO_RXFE_MASK; //RXFE and buffer size of 1 word
-    queueInit();
-    pc.printf("Initialization done...\n\r");
-}
-
-
-
-//initialization debuging purposes
-void print_registers() {
-    
-   
-    pc.printf("\n\rDMA REGISTERS\n\r");
-    pc.printf("DMA_MUX: 0x%08x\r\n",DMAMUX_CHCFG0);
-    pc.printf("SADDR0: 0x%08x\r\n",DMA_TCD0_SADDR);
-    pc.printf("DADDR0: 0x%08x\r\n",DMA_TCD0_DADDR);
-    pc.printf("CITER0: 0x%08x\r\n",DMA_TCD0_CITER_ELINKNO);
-    pc.printf("BITER0: 0x%08x\r\n",DMA_TCD0_BITER_ELINKNO);
-    pc.printf("DMA_CR: %08x\r\n", DMA_CR);
-    pc.printf("DMA_ES: %08x\r\n", DMA_ES);
-    pc.printf("DMA_ERQ: %08x\r\n", DMA_ERQ);
-    pc.printf("DMA_EEI: %08x\r\n", DMA_EEI);
-    pc.printf("DMA_CEEI: %02x\r\n", DMA_CEEI);
-    pc.printf("DMA_SEEI: %02x\r\n", DMA_SEEI);
-    pc.printf("DMA_CERQ: %02x\r\n", DMA_CERQ);
-    pc.printf("DMA_SERQ: %02x\r\n", DMA_SERQ);
-    pc.printf("DMA_CDNE: %02x\r\n", DMA_CDNE);
-    pc.printf("DMA_SSRT: %02x\r\n", DMA_SSRT);
-    pc.printf("DMA_CERR: %02x\r\n", DMA_CERR);
-    pc.printf("DMA_CINT: %02x\r\n", DMA_CINT);
-    pc.printf("DMA_INT: %08x\r\n", DMA_INT);
-    pc.printf("DMA_ERR: %08x\r\n", DMA_ERR);
-    pc.printf("DMA_HRS: %08x\r\n", DMA_HRS);
-    pc.printf("DMA_TCD0_DOFF: %08x\r\n",DMA_TCD0_DOFF);
-    pc.printf("\n\rUART REGISTERS\n\r");
-    pc.printf("UART_BDH_REG: %08x\r\n",UART_BDH_REG(UART3)); 
-    pc.printf("UART_C1_REG: %08x\r\n",UART_C1_REG(UART3));
-    pc.printf("UART_C2_REG: %08x\r\n",UART_C2_REG(UART3));
-    pc.printf("UART_S1_REG: %08x\r\n",UART_S1_REG(UART3));
-    pc.printf("UART_s2_REG: %08x\r\n",UART_S2_REG(UART3));  
-    pc.printf("UART_C3_REG: %08x\r\n",UART_C3_REG(UART3));
-    pc.printf("UART_D_REG: %08x\r\n",UART_D_REG(UART3));
-    pc.printf("UART_MA1_REG: %08x\r\n",UART_MA1_REG(UART3));
-    pc.printf("UART_MA2_REG: %08x\r\n",UART_MA2_REG(UART3));
-    pc.printf("UART_C4_REG: %08x\r\n",UART_C4_REG(UART3));
-    pc.printf("UART_C5_REG: %08x\r\n",UART_C5_REG(UART3));
-    pc.printf("UART_ED_REG: %08x\r\n",UART_ED_REG(UART3));   
-    pc.printf("UART_MODEM_REG: %08x\r\n",UART_MODEM_REG(UART3));
-    pc.printf("UART_IR_REG: %08x\r\n",UART_IR_REG(UART3)); 
-    pc.printf("UART_PFIFO_REG: %08x\r\n",UART_PFIFO_REG(UART3));
-    pc.printf("UART_CFIFO_REG: %08x\r\n",UART_CFIFO_REG(UART3));
-    pc.printf("UART_SFIFO_REG: %08x\r\n",UART_SFIFO_REG(UART3)); 
-    pc.printf("UART_TWFIFO_REG: %08x\r\n",UART_TWFIFO_REG(UART3));
-    pc.printf("UART_TCFIFO_REG: %08x\r\n",UART_TCFIFO_REG(UART3)); 
-    pc.printf("UART_RWFIFO_REG: %08x\r\n",UART_RWFIFO_REG(UART3)); 
-    pc.printf("UART_RCFIFO_REG: %08x\r\n",UART_RCFIFO_REG(UART3));
-  
-}
\ No newline at end of file
--- a/GSMLibrary.h	Sat Mar 07 23:34:49 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,41 +0,0 @@
-#ifndef GSMLIBRARY_H
-#define GSMLIBRARY_H
-
-#include "mbed.h"
-
-#define MESSAGE_SIZE 1024
-
-enum gsm_states{GSM_INITIALIZE, 
-                    GSM_AT_OK,      //Make sure communication is stablished betheen fdm and gsm
-                    GSM_AT_CSQ,     //Check signal strength if strenght lower than 10 keep trying
-                    GSM_AT_CREG,    //Checking if phone is conected to network
-                    GSM_AT_CMGF,    //prepare phone to send message by CMGF=1
-                    GSM_AT_CMGS,    //input phone number by CMGS= "+phonenumber"
-                    GSM_AT_SENDSMS, //write message, finish with alt+z
-                    GSM_SUCCESS     //check if message was send correctly
-                };
-
-
-//GSM state machine
-void gsm_tick();
-bool gsm_timeOut();
-void gsm_nextStateLogic();
-void gsm_mealyOutputs();
-
-//Initialize DMA data transfer, and UART3.
-void gsm_initialize();
-
-//returns 1 for eady to send again, 0 for busy.
-bool gsm_ready();
-
-//begin message transmission
-void gsm_send_sms(char message[]);
-
-//Brings state machine back to initialize state (notice this is different than initialize function)
-//this will set flags to initial values and prepare gsm to work anew.
-void reset();
-
-//used for debuging purposes, it print all main registers from dma0 and uart3
-void print_registers(); 
-
-#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GSMLibrary/GSMLibrary.cpp	Sat Mar 07 23:57:06 2015 +0000
@@ -0,0 +1,309 @@
+#include "GSMLibrary.h"
+#include "gsmqueue.h"
+#include <string.h>
+
+#define TIME_CONST .3
+#define SECONDS_TIMEOUT 40
+#define TIMEOUTLIMIT SECONDS_TIMEOUT/TIME_CONST //$change check with main code this will set up condition fior timeout.
+
+//definition for AT comands
+#define AT_OK "AT"
+#define AT_CSQ "AT+CSQ" 
+#define AT_CREG "AT+CREG?"
+#define AT_CMGF "AT+CMGF=1"
+#define RECEIVER_PHONE_NUMBER "\"+18014722842\""
+#define AT_CMGS "AT+CMGS=" RECEIVER_PHONE_NUMBER 
+#define MESSAGE_BODY "stress test\32\32"
+#define MSG_SIZE 200
+
+//Definition for AT repsonses
+//Please notice that after ":" the gsm will usually send aditional information
+#define AT_OK_RESPONSE "OK" //Response after sending "AT" message
+#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 
+#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
+#define AT_CMGF_RESPONSE "OK"
+#define AT_CMGS_RESPONSE ">"  //Message is written aftersymbol
+#define AT_SENDSMS_RESPONSE ">" // +CMGS: <id> this will include the message id. CMGS ERROR for error and 
+#define AT_SUCCESS_RESPONSE "+CMGS:"
+
+//External variables
+extern Serial pc;
+extern Serial gsm;
+extern uint8_t buffer[BUFFER_LENGTH];//buffer storing char
+
+//Internal variables
+gsm_states gsm_current_state = GSM_INITIALIZE;
+char send = 0;
+int timeout_count = 0;
+char gsm_msg[MSG_SIZE + 1]; //1 extra for Ctrl+Z
+
+void gsm_tick()
+{
+    if (getGSMIdleBit() || gsm_timeOut() || (send && gsm_current_state == GSM_INITIALIZE))  //question with send...
+    {
+        resetGSMIdleBit();  //reset GSM idle bit
+        gsm_nextStateLogic();   //Next state
+        gsm_mealyOutputs(); //Mealy outputs
+        flushQueue();       //Flush the queue
+    }
+}
+
+
+
+//Advance timeout counter; if timeout, return true
+bool gsm_timeOut()
+{
+    if(++timeout_count >= TIMEOUTLIMIT){
+        timeout_count=0; 
+        gsm_current_state = GSM_INITIALIZE;
+        return true;
+    }
+    else
+        return false;     
+}
+
+//Have the GSM send a message
+void gsm_send_sms(char msg[])
+{
+    send = 1;
+    strcpy(gsm_msg,msg);  //If we need to optimize later we can do that, but this is more robust
+    strcat(gsm_msg,"\x1A");
+}
+
+//Return true if gsm is ready
+bool gsm_ready()
+{
+    return (send == 1) ? true : false;
+}
+
+//Reset the gsm
+void gsm_reset()
+{
+    gsm_current_state = GSM_INITIALIZE;
+}
+
+//Next state logic -----------------------------------------------------
+void gsm_nextStateLogic()
+{
+    printQueue(); //$debug
+
+    switch(gsm_current_state)
+    {
+        case GSM_INITIALIZE:
+            pc.printf("gsm_initialize state\r\n");//&debug
+            timeout_count = 0;
+            if (send)
+                gsm_current_state = GSM_AT_OK;   //unconditional (check it)
+            break;
+        case GSM_AT_OK:
+            pc.printf("inside AT_OK state\r\n");//&debug
+            if (findInQueue(AT_OK_RESPONSE))
+                gsm_current_state = GSM_AT_CSQ;
+            break;
+        case GSM_AT_CSQ:
+            pc.printf("inside AT_CSQ state \r\n");//&debug
+            if(findInQueue(AT_CSQ_RESPONSE))
+                gsm_current_state = GSM_AT_CREG;
+            break;
+        case GSM_AT_CREG:
+            pc.printf("gsm_creg state\r\n");//&debug
+            if(findInQueue(AT_CREG_RESPONSE))
+            {
+                pc.printf("creg parse Int1: %d\r\n",parseInt());//&debug
+                int q = parseInt();
+                pc.printf("creg parse Int2: %d\r\n",q);//&debug
+                if(q == 1)
+                    gsm_current_state = GSM_AT_CMGF;
+            } 
+            break;
+        case GSM_AT_CMGF:
+            pc.printf("gsm_cmgf state\r\n");//&debug
+            if(findInQueue(AT_CMGF_RESPONSE))
+                gsm_current_state = GSM_AT_CMGS;
+            break;
+        case GSM_AT_CMGS:
+            pc.printf("gsm_cmgs state\r\n");//&debug
+            if(findInQueue(AT_CMGS_RESPONSE))
+                gsm_current_state = GSM_AT_SENDSMS;
+            break;
+        case GSM_AT_SENDSMS:
+            pc.printf("gsm_send_sms state\r\n");//&debug
+            if(findInQueue(AT_SENDSMS_RESPONSE))    //>
+            {
+                //Check if the "successfully sent" has also already been received (most likely
+                //this won't be the case, but if it has been >500 ms there's a possibility
+                //we've received both the messages during the same call to our state machine.)
+                if(findInQueue(AT_SUCCESS_RESPONSE))
+                {
+                    pc.printf("Message SENT! msgID: %iY\r\n",parseInt());//&debug
+                    send = 0;
+                    gsm_current_state = GSM_INITIALIZE; //Skip success state (we've received both)
+                }
+                else
+                    gsm_current_state = GSM_SUCCESS;    //Go to success state
+            }
+            else
+                gsm_current_state = GSM_AT_CMGS;        //Try resending the message (until timeout)
+            break;
+        case GSM_SUCCESS:
+            pc.printf("gsm_success state\r\n");//&debug
+            if(findInQueue(AT_SUCCESS_RESPONSE))
+            {
+                pc.printf("Message SENT! msgID: %iY\r\n",parseInt());//&debug
+            }
+            send = 0;
+            gsm_current_state = GSM_INITIALIZE;     //We will restart regardless of whether it worked
+            break;
+        default:
+            pc.printf("This is a state error");
+    }
+}
+
+//Mealy output logic ------------------------------------------------------
+void gsm_mealyOutputs()
+{
+    switch(gsm_current_state)
+    {
+        case GSM_INITIALIZE:
+            pc.printf("No Mealy initialize state output\r\n");//&debug
+            break;
+        case GSM_AT_OK:
+            pc.printf("sending AT_OK\r\n");//&debug
+            gsm.puts(AT_OK);
+            gsm.puts("\r\n"); 
+            break;
+        case GSM_AT_CSQ:
+            pc.printf("sending AT_CSQ\r\n");//&debug
+            gsm.puts(AT_CSQ);
+            gsm.puts("\r\n"); 
+            break;
+        case GSM_AT_CREG:
+            pc.printf("sending AT_CREG\r\n");//&debug
+            gsm.puts(AT_CREG);
+            gsm.puts("\r\n"); 
+            break;
+        case GSM_AT_CMGF:   
+            pc.printf("sending AT_CMGF\r\n");//&debug
+            gsm.puts(AT_CMGF);
+            gsm.puts("\r\n"); 
+            break;
+        case GSM_AT_CMGS:   
+            pc.printf("sending AT_CMGS\r\n");//&debug
+            gsm.puts(AT_CMGS);
+            gsm.puts("\r\n"); 
+            break;
+        case GSM_AT_SENDSMS:
+            pc.printf("sending message\r\n");//&debug
+            gsm.puts(gsm_msg); //substitute char included
+            gsm.puts("\r\n"); 
+            break;
+        case GSM_SUCCESS:
+            pc.printf("No Mealy success state output\r\n");//&debug 
+        default:
+            pc.printf("This is a state error");
+    }
+}
+
+
+//Initialize the GSM
+void gsm_initialize(){  
+      SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK; //enabling dmamux clock
+      SIM_SCGC7 |= SIM_SCGC7_DMA_MASK;  // enebaling dma clock
+      pc.printf("initializing tregisters...!\r\n");
+     // control register mux, enabling uart3 receive        
+     DMAMUX_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK|DMAMUX_CHCFG_SOURCE(8); 
+     
+     // Enable request signal for channel 0 
+     DMA_ERQ = DMA_ERQ_ERQ0_MASK;
+     
+      // select round-robin arbitration priority
+     DMA_CR |= DMA_CR_ERCA_MASK;
+     
+     //enabled error interrupt for DMA0
+     //DMA_EEI = DMA_EEI_EEI0_MASK ;
+     //Addres for buffer
+     DMA_TCD0_SADDR = (uint32_t) &UART_D_REG(UART3_BASE_PTR);
+     DMA_TCD0_DADDR = (uint32_t) buffer;
+     // Set an offset for source and destination address
+     DMA_TCD0_SOFF = 0x00; 
+     DMA_TCD0_DOFF = 0x01; // Destination address offset of 1 byte per transaction
+     
+     // Set source and destination data transfer size
+     DMA_TCD0_ATTR = DMA_ATTR_SSIZE(0) | DMA_ATTR_DSIZE(0);
+     
+     // Number of bytes to be transfered in each service request of the channel
+     DMA_TCD0_NBYTES_MLNO = 0x01;
+     // Current major iteration count
+    DMA_TCD0_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(BUFFER_LENGTH);
+    DMA_TCD0_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(BUFFER_LENGTH);
+    // Adjustment value used to restore the source and destiny address to the initial value
+    // 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)
+    DMA_TCD0_SLAST = 0;   // Source address adjustment
+    DMA_TCD0_DLASTSGA = -BUFFER_LENGTH;  // Destination address adjustment   
+    // Setup control and status register
+    DMA_TCD0_CSR = 0;
+       
+    // enable interrupt call at end of major loop
+    DMA_TCD0_CSR |= DMA_CSR_INTMAJOR_MASK;
+    
+    //Activate dma trasnfer rx interrupt
+    UART_C2_REG(UART3) |= UART_C2_RIE_MASK;
+    UART_C5_REG(UART3) |= UART_C5_RDMAS_MASK | UART_C5_ILDMAS_MASK | UART_C5_LBKDDMAS_MASK;
+    //activate p fifo   
+    UART_PFIFO_REG(UART3) |= UART_PFIFO_RXFE_MASK; //RXFE and buffer size of 1 word
+    queueInit();
+    pc.printf("Initialization done...\n\r");
+}
+
+
+
+//initialization debuging purposes
+void print_registers() {
+    
+   
+    pc.printf("\n\rDMA REGISTERS\n\r");
+    pc.printf("DMA_MUX: 0x%08x\r\n",DMAMUX_CHCFG0);
+    pc.printf("SADDR0: 0x%08x\r\n",DMA_TCD0_SADDR);
+    pc.printf("DADDR0: 0x%08x\r\n",DMA_TCD0_DADDR);
+    pc.printf("CITER0: 0x%08x\r\n",DMA_TCD0_CITER_ELINKNO);
+    pc.printf("BITER0: 0x%08x\r\n",DMA_TCD0_BITER_ELINKNO);
+    pc.printf("DMA_CR: %08x\r\n", DMA_CR);
+    pc.printf("DMA_ES: %08x\r\n", DMA_ES);
+    pc.printf("DMA_ERQ: %08x\r\n", DMA_ERQ);
+    pc.printf("DMA_EEI: %08x\r\n", DMA_EEI);
+    pc.printf("DMA_CEEI: %02x\r\n", DMA_CEEI);
+    pc.printf("DMA_SEEI: %02x\r\n", DMA_SEEI);
+    pc.printf("DMA_CERQ: %02x\r\n", DMA_CERQ);
+    pc.printf("DMA_SERQ: %02x\r\n", DMA_SERQ);
+    pc.printf("DMA_CDNE: %02x\r\n", DMA_CDNE);
+    pc.printf("DMA_SSRT: %02x\r\n", DMA_SSRT);
+    pc.printf("DMA_CERR: %02x\r\n", DMA_CERR);
+    pc.printf("DMA_CINT: %02x\r\n", DMA_CINT);
+    pc.printf("DMA_INT: %08x\r\n", DMA_INT);
+    pc.printf("DMA_ERR: %08x\r\n", DMA_ERR);
+    pc.printf("DMA_HRS: %08x\r\n", DMA_HRS);
+    pc.printf("DMA_TCD0_DOFF: %08x\r\n",DMA_TCD0_DOFF);
+    pc.printf("\n\rUART REGISTERS\n\r");
+    pc.printf("UART_BDH_REG: %08x\r\n",UART_BDH_REG(UART3)); 
+    pc.printf("UART_C1_REG: %08x\r\n",UART_C1_REG(UART3));
+    pc.printf("UART_C2_REG: %08x\r\n",UART_C2_REG(UART3));
+    pc.printf("UART_S1_REG: %08x\r\n",UART_S1_REG(UART3));
+    pc.printf("UART_s2_REG: %08x\r\n",UART_S2_REG(UART3));  
+    pc.printf("UART_C3_REG: %08x\r\n",UART_C3_REG(UART3));
+    pc.printf("UART_D_REG: %08x\r\n",UART_D_REG(UART3));
+    pc.printf("UART_MA1_REG: %08x\r\n",UART_MA1_REG(UART3));
+    pc.printf("UART_MA2_REG: %08x\r\n",UART_MA2_REG(UART3));
+    pc.printf("UART_C4_REG: %08x\r\n",UART_C4_REG(UART3));
+    pc.printf("UART_C5_REG: %08x\r\n",UART_C5_REG(UART3));
+    pc.printf("UART_ED_REG: %08x\r\n",UART_ED_REG(UART3));   
+    pc.printf("UART_MODEM_REG: %08x\r\n",UART_MODEM_REG(UART3));
+    pc.printf("UART_IR_REG: %08x\r\n",UART_IR_REG(UART3)); 
+    pc.printf("UART_PFIFO_REG: %08x\r\n",UART_PFIFO_REG(UART3));
+    pc.printf("UART_CFIFO_REG: %08x\r\n",UART_CFIFO_REG(UART3));
+    pc.printf("UART_SFIFO_REG: %08x\r\n",UART_SFIFO_REG(UART3)); 
+    pc.printf("UART_TWFIFO_REG: %08x\r\n",UART_TWFIFO_REG(UART3));
+    pc.printf("UART_TCFIFO_REG: %08x\r\n",UART_TCFIFO_REG(UART3)); 
+    pc.printf("UART_RWFIFO_REG: %08x\r\n",UART_RWFIFO_REG(UART3)); 
+    pc.printf("UART_RCFIFO_REG: %08x\r\n",UART_RCFIFO_REG(UART3));
+  
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GSMLibrary/GSMLibrary.h	Sat Mar 07 23:57:06 2015 +0000
@@ -0,0 +1,39 @@
+#ifndef GSMLIBRARY_H
+#define GSMLIBRARY_H
+
+#include "mbed.h"
+
+enum gsm_states{GSM_INITIALIZE, 
+                    GSM_AT_OK,      //Make sure communication is stablished betheen fdm and gsm
+                    GSM_AT_CSQ,     //Check signal strength if strenght lower than 10 keep trying
+                    GSM_AT_CREG,    //Checking if phone is conected to network
+                    GSM_AT_CMGF,    //prepare phone to send message by CMGF=1
+                    GSM_AT_CMGS,    //input phone number by CMGS= "+phonenumber"
+                    GSM_AT_SENDSMS, //write message, finish with alt+z
+                    GSM_SUCCESS     //check if message was send correctly
+                };
+
+
+//GSM state machine
+void gsm_tick();
+bool gsm_timeOut();
+void gsm_nextStateLogic();
+void gsm_mealyOutputs();
+
+//Initialize DMA data transfer, and UART3.
+void gsm_initialize();
+
+//returns 1 for eady to send again, 0 for busy.
+bool gsm_ready();
+
+//begin message transmission
+void gsm_send_sms(char message[]);
+
+//Brings state machine back to initialize state (notice this is different than initialize function)
+//this will set flags to initial values and prepare gsm to work anew.
+void reset();
+
+//used for debuging purposes, it print all main registers from dma0 and uart3
+void print_registers(); 
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GSMLibrary/gsmqueue.cpp	Sat Mar 07 23:57:06 2015 +0000
@@ -0,0 +1,135 @@
+#include "gsmqueue.h"
+#include "mbed.h"
+/* queue.cpp
+ * Contains functions to read from the DMA buffer in a queue fashion
+ */
+extern Serial pc;
+
+char buffer[BUFFER_LENGTH];
+char* queueHead;
+
+
+//Initialize variables 
+void queueInit()
+{
+    //The buffer is initialized in init.cpp
+    queueHead = QUEUETAIL;
+}
+
+//Find an occurrence of the given string in the buffer.
+//Only advance queueHead until a matching string is found.
+//The given string terminates in NULL (\0)
+bool findInQueue(char* str)
+{
+    //Check that string to find is not empty
+    if (*str == NULL) return false;
+    
+    while (queueHead != QUEUETAIL)
+    {
+        //Does the character match the begin char?
+        if (*queueHead == *str){
+            //Check the remaining characters
+            char* sPos = str;
+            char* qPos = 0;
+            for (qPos = queueHead; qPos != QUEUETAIL; qPos = incrementIndex(qPos)){
+                //Compare the next char
+                if (*qPos == *sPos)
+                {
+                    ++sPos; //Increment index (prefix incrementation).
+                    if (*sPos == NULL)   //If finished, update queueHead, return true.
+                    {
+                        queueHead = incrementIndex(qPos);
+                        return true;
+                    }
+                }
+                else    //Not equal, so exit for loop and try again at a different location
+                    break;
+            }
+        }
+        //Increment queue index for next iteration
+        queueHead = incrementIndex(queueHead);
+    }
+    //We never finished, so return false
+    return false;
+}
+
+//Increment queue index by 1
+char* incrementIndex(char* pointerToIncrement)
+{
+    if((pointerToIncrement + 1) < (buffer + BUFFER_LENGTH))
+        return (pointerToIncrement + 1);
+    else
+        return buffer;
+}
+
+//clear queue
+void flushQueue()
+{
+    queueHead = QUEUETAIL;   
+}
+
+//$debug - print queue elements
+void printQueue()
+{
+    char* qPos = queueHead;
+    pc.printf("Queue:");
+    while (qPos != QUEUETAIL)
+    {
+        //Print the current character
+        if (*qPos == '\n')
+            pc.printf("\\n");
+        else if (*qPos == '\r')
+            pc.printf("\\r");
+        else    
+            pc.printf("%C",*qPos);
+        
+        
+        //Increment index
+        qPos = incrementIndex(qPos);
+    }
+    pc.printf("\n\r");
+}
+
+//Parse through characters until first integer is found
+//Advance qHead until you reach the next non-numeric character
+//Does not read negative integers; returns -1 if unsuccessful
+int parseInt()
+{
+    //Check if queue is empty first
+    if (queueHead == QUEUETAIL) return -1;
+    
+    //Advance to first numeric character
+    while (!isNumeric(queueHead))
+    {
+        queueHead = incrementIndex(queueHead);
+        if (queueHead == QUEUETAIL) return -1;
+    }
+    
+    //Continue until first non-numeric character
+    int val = 0;
+    while (queueHead != QUEUETAIL && isNumeric(queueHead))
+    {
+        val *= 10;
+        val += (int)(*queueHead - '0');
+        queueHead = incrementIndex(queueHead);
+    }
+    return val;
+}
+
+//Returns true if the character is numeric
+bool isNumeric(char* qPos)
+{
+    return ('0' <= *qPos && *qPos <= '9');
+}
+    
+//Reset the GSM DMA idle bit to 0
+void resetGSMIdleBit()
+{
+    UART_S1_REG(UART3) &= ~UART_S1_IDLE_MASK;
+}
+
+//Get the GSM DMA idle bit (if 1, indicates we already received a response)
+bool getGSMIdleBit()
+{
+    return (UART_S1_IDLE_MASK & UART_S1_REG(UART3)) >> UART_S1_IDLE_SHIFT;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GSMLibrary/gsmqueue.h	Sat Mar 07 23:57:06 2015 +0000
@@ -0,0 +1,43 @@
+#ifndef GSMQUEUE_H
+#define GSMQUEUE_H
+
+/* gsmqueue.cpp
+ * Contains functions to read from the DMA buffer in a queue fashion
+ */
+
+//Memory block of char size alocated for DMA
+#define BUFFER_LENGTH 255 //cannot exeede an int max value
+
+#define QUEUETAIL (char*)DMA_TCD0_DADDR
+
+
+//Initialize variables 
+void queueInit();
+
+//Find an occurrence of the given string in the buffer.
+//Only advance queueHead until a matching string is found.
+bool findInQueue(char* str);
+
+//Increment queue index by 1, auxiliar function
+char* incrementIndex(char* pointerToIncrement);
+
+//will eliminate all elements form queue.
+void flushQueue();
+
+//Print queue elements
+void printQueue();   //for debugging
+
+//Parse through characters until first integer is found
+int parseInt();
+
+//Returns true if the character is numeric
+bool isNumeric(char* qPos);
+
+//Reset the GSM DMA idle bit to 0
+void resetGSMIdleBit();
+
+//Get the GSM DMA idle bit (if 1, indicates we already received a response)
+bool getGSMIdleBit();
+
+
+#endif
\ No newline at end of file
--- a/Sensor.cpp	Sat Mar 07 23:34:49 2015 +0000
+++ b/Sensor.cpp	Sat Mar 07 23:57:06 2015 +0000
@@ -10,6 +10,7 @@
 DigitalOut led_red(LED_RED);
 Serial pc(USBTX, USBRX);
 Serial gsm(PTC17,PTC16);
+
 Timer t;
 
 /**************************************************
@@ -83,18 +84,19 @@
     
     pc.baud(115200);
     gsm.baud(115200);
+    
     pc.printf("hello\r\n");
     
     //GSM INITIALIZATION ///////////////////////////////
     gsm_initialize();
     // GPS INITIALIZATION //////////////////////////////
-    //gps_Serial = new Serial(D1,D0);
-    //Adafruit_GPS myGPS(gps_Serial);
-    //char c;
-    //myGPS.begin(9600);
-    //myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
-    //myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
-    //myGPS.sendCommand(PGCMD_ANTENNA);
+    Adafruit_GPS myGPS(gps_Serial);
+    gps_Serial = new Serial(PTD3,PTD2); 
+    myGPS.begin(9600);
+    volatile char c;
+    myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
+    myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
+    myGPS.sendCommand(PGCMD_ANTENNA);
     ////////////////////////////////////////////////////
     
     buildIQ();
@@ -153,7 +155,7 @@
             filteredShort = QAM(sSI, sSQ, &pc);
             filteredShortRef = QAM(sRefSI, sRefSQ, &pc);
         }  
-        
+       
         c = myGPS.read();
         if ( myGPS.newNMEAreceived() ) {
             if ( !myGPS.parse(myGPS.lastNMEA()) ) {
@@ -197,7 +199,7 @@
         
         
         
-        /*
+        
         if (sampleIndex+2 > SAMPLE_LENGTH) { // 0.25 seconds
             
             sampleIndex = 0;
@@ -221,6 +223,6 @@
                 fclose(fp);
             }    
         }
-        */
+        
     }
 }
\ No newline at end of file
--- a/gsmqueue.cpp	Sat Mar 07 23:34:49 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,136 +0,0 @@
-#include "gsmqueue.h"
-#include "mbed.h"
-/* queue.cpp
- * Contains functions to read from the DMA buffer in a queue fashion
- */
- 
-extern Serial pc;
-
-char buffer[BUFFER_LENGTH];
-char* queueHead;
-
-
-//Initialize variables 
-void queueInit()
-{
-    //The buffer is initialized in init.cpp
-    queueHead = QUEUETAIL;
-}
-
-//Find an occurrence of the given string in the buffer.
-//Only advance queueHead until a matching string is found.
-//The given string terminates in NULL (\0)
-bool findInQueue(char* str)
-{
-    //Check that string to find is not empty
-    if (*str == NULL) return false;
-    
-    while (queueHead != QUEUETAIL)
-    {
-        //Does the character match the begin char?
-        if (*queueHead == *str){
-            //Check the remaining characters
-            char* sPos = str;
-            char* qPos = 0;
-            for (qPos = queueHead; qPos != QUEUETAIL; qPos = incrementIndex(qPos)){
-                //Compare the next char
-                if (*qPos == *sPos)
-                {
-                    ++sPos; //Increment index (prefix incrementation).
-                    if (*sPos == NULL)   //If finished, update queueHead, return true.
-                    {
-                        queueHead = incrementIndex(qPos);
-                        return true;
-                    }
-                }
-                else    //Not equal, so exit for loop and try again at a different location
-                    break;
-            }
-        }
-        //Increment queue index for next iteration
-        queueHead = incrementIndex(queueHead);
-    }
-    //We never finished, so return false
-    return false;
-}
-
-//Increment queue index by 1
-char* incrementIndex(char* pointerToIncrement)
-{
-    if((pointerToIncrement + 1) < (buffer + BUFFER_LENGTH))
-        return (pointerToIncrement + 1);
-    else
-        return buffer;
-}
-
-//clear queue
-void flushQueue()
-{
-    queueHead = QUEUETAIL;   
-}
-
-//$debug - print queue elements
-void printQueue()
-{
-    char* qPos = queueHead;
-    pc.printf("Queue:");
-    while (qPos != QUEUETAIL)
-    {
-        //Print the current character
-        if (*qPos == '\n')
-            pc.printf("\\n");
-        else if (*qPos == '\r')
-            pc.printf("\\r");
-        else    
-            pc.printf("%C",*qPos);
-        
-        
-        //Increment index
-        qPos = incrementIndex(qPos);
-    }
-    pc.printf("\n\r");
-}
-
-//Parse through characters until first integer is found
-//Advance qHead until you reach the next non-numeric character
-//Does not read negative integers; returns -1 if unsuccessful
-int parseInt()
-{
-    //Check if queue is empty first
-    if (queueHead == QUEUETAIL) return -1;
-    
-    //Advance to first numeric character
-    while (!isNumeric(queueHead))
-    {
-        queueHead = incrementIndex(queueHead);
-        if (queueHead == QUEUETAIL) return -1;
-    }
-    
-    //Continue until first non-numeric character
-    int val = 0;
-    while (queueHead != QUEUETAIL && isNumeric(queueHead))
-    {
-        val *= 10;
-        val += (int)(*queueHead - '0');
-        queueHead = incrementIndex(queueHead);
-    }
-    return val;
-}
-
-//Returns true if the character is numeric
-bool isNumeric(char* qPos)
-{
-    return ('0' <= *qPos && *qPos <= '9');
-}
-    
-//Reset the GSM DMA idle bit to 0
-void resetGSMIdleBit()
-{
-    UART_S1_REG(UART3) &= ~UART_S1_IDLE_MASK;
-}
-
-//Get the GSM DMA idle bit (if 1, indicates we already received a response)
-bool getGSMIdleBit()
-{
-    return (UART_S1_IDLE_MASK & UART_S1_REG(UART3)) >> UART_S1_IDLE_SHIFT;
-}
\ No newline at end of file
--- a/gsmqueue.h	Sat Mar 07 23:34:49 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,43 +0,0 @@
-#ifndef GSMQUEUE_H
-#define GSMQUEUE_H
-
-/* gsmqueue.cpp
- * Contains functions to read from the DMA buffer in a queue fashion
- */
-
-//Memory block of char size alocated for DMA
-#define BUFFER_LENGTH 255 //cannot exeede an int max value
-
-#define QUEUETAIL (char*)DMA_TCD0_DADDR
-
-
-//Initialize variables 
-void queueInit();
-
-//Find an occurrence of the given string in the buffer.
-//Only advance queueHead until a matching string is found.
-bool findInQueue(char* str);
-
-//Increment queue index by 1, auxiliar function
-char* incrementIndex(char* pointerToIncrement);
-
-//will eliminate all elements form queue.
-void flushQueue();
-
-//Print queue elements
-void printQueue();   //for debugging
-
-//Parse through characters until first integer is found
-int parseInt();
-
-//Returns true if the character is numeric
-bool isNumeric(char* qPos);
-
-//Reset the GSM DMA idle bit to 0
-void resetGSMIdleBit();
-
-//Get the GSM DMA idle bit (if 1, indicates we already received a response)
-bool getGSMIdleBit();
-
-
-#endif
\ No newline at end of file