Treehouse Mbed Team / Mbed 2 deprecated APS_1U5x

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers serial.cpp Source File

serial.cpp

00001 #include "mbed.h"
00002 #include "serial.h"
00003 #include "stdio.h"
00004 #include "string.h"
00005 #include "globals.h"
00006 #include "all_io.h"
00007 
00008 /************* GLOBAL VARIABLES *****************************/
00009 struct serialStatusBits serialStatus;
00010 char strbuf[150] = {0};
00011 char rxbuf[MAXRXBUF+1] = {0};
00012 volatile unsigned int bufloc = 0;
00013 
00014 /************* FORWARD DECLARATIONS *************************/
00015 void Rx_IRQHandler(void);
00016 
00017 
00018 // set up serial port for 115200 baud
00019 //Serial dcm1(USBTX, USBRX, 115200);
00020 Serial dcm1(USBTX, USBRX, 921600);
00021 
00022 
00023 // initialize the serial port variables
00024 void initSerial(void)
00025 {
00026    // create the RxIRQ callback
00027    dcm1.attach(&Rx_IRQHandler, Serial::RxIrq);
00028    
00029 }
00030    
00031 
00032 // this is the interrupt request handler (IRQ) for ALL Recoev interrupts
00033 void Rx_IRQHandler(void)
00034 {
00035    // reinit the repeat flag
00036    serialStatus.repeat = FALSE;
00037 
00038    // check if the USART1 receive interrupt flag was set
00039    while (dcm1.readable())
00040    {
00041       //led_usb = 0;
00042       rxbuf[bufloc] = dcm1.getc();
00043 
00044       if (rxbuf[bufloc] == CR)        // check for end of command
00045       {
00046          serialStatus.command = TRUE;
00047          rxbuf[bufloc] = 0;
00048          bufloc = 0;
00049          return;
00050       }
00051       else if (rxbuf[bufloc] == '$')  // check for computer command
00052       {
00053          serialStatus.computer = TRUE;
00054          bufloc = 0; // reset the buffer if computer command
00055       }
00056       else if (rxbuf[bufloc] == ')')  // check for repeat command
00057       {
00058          serialStatus.repeat = TRUE;
00059          rxbuf[bufloc] = 0;
00060       }
00061 
00062       // Echo character back out the serial port
00063       if (!serialStatus.computer)
00064       {
00065          dcm1.putc((unsigned char)rxbuf[bufloc]);
00066       }
00067 
00068       // handle backspace
00069       if (rxbuf[bufloc] != 0x08)
00070       {
00071          bufloc++; // increment the location
00072       }
00073       else
00074       {
00075          if (bufloc > 0)
00076          {
00077             rxbuf[--bufloc] = 0;  // set to null
00078          }
00079          else
00080          {
00081             rxbuf[bufloc] = 0;  // set to null
00082          }
00083       }
00084       
00085       // check for overflow
00086       if (bufloc >= MAXRXBUF)
00087       {
00088          bufloc = 0;
00089          rxbuf[0] = 0;
00090       }
00091       
00092       //led_usb = 1;
00093    }
00094 }
00095 
00096 
00097 // sends carriage return and linefeed and prompt character
00098 /*
00099 void sendCRLF(void)
00100 {
00101  char strMenu[30] ={0};
00102 
00103     menu_type = MENU_DCM1;
00104 
00105     switch(menuType)
00106     {
00107       case  MENU_DCM1: strcpy(strMenu,"DCM1"); break;
00108       case  MENU_DIFFERENTIAL: strcpy(strMenu,"DIFF"); break;
00109       case  MENU_SINGLE: strcpy(strMenu,"SINGLE"); break;
00110       case  MENU_DUAL: strcpy(strMenu,"DUAL"); break;
00111       case  MENU_INVERT: strcpy(strMenu,"INVERT"); break;
00112       case  MENU_SETTINGS: strcpy(strMenu,"SET"); break;
00113       case  MENU_SETTINGS_CHAN1: strcpy(strMenu,"SET CHAN1"); break;
00114       case  MENU_SETTINGS_CHAN2: strcpy(strMenu,"SET CHAN2"); break;
00115       case  MENU_SETTINGS_CLOCK: strcpy(strMenu,"SET CLOCK"); break;
00116       case  MENU_CALIBRATE: strcpy(strMenu,"CAL"); break;
00117       case  MENU_TEST: strcpy(strMenu,"TEST"); break;
00118       case  MENU_MAIN: strcpy(strMenu,"MAIN"); break;
00119       default: strcpy(strMenu,"UNDF"); break;
00120     }
00121 
00122     // append the CR,LF and the ready character
00123      if(serialStatus.computer)
00124      {
00125          sendSerial("\r");
00126      }
00127      else
00128      {
00129         // adds a carrot so you know you are talking in terminal
00130         sprintf(strbuf,"\n\r%s>",strMenu);
00131         sendSerial(strbuf);
00132         // sendSerial("\n\r>");
00133      }
00134 }*/
00135 
00136 // send buffer to the serial port
00137 void sendSerial(char *buf)
00138 {
00139     //led_usb = 0;
00140     dcm1.printf(buf);
00141     //led_usb = 1;
00142 }
00143 
00144 /************************************************************
00145 * Routine: showRangeError
00146 * Input:   error -- 0 = in range, 1 = out of range
00147 * Output:  none
00148 * Description:
00149 * Sends a floating point number (value) over the serial port
00150 * if it is being retrieved (GET)
00151 *
00152 **************************************************************/
00153 void showRangeError(int intValue, int iBadValue, float fBadValue)
00154 {
00155    if (intValue){ 
00156       sprintf(strbuf, " Out of Range: %d",    iBadValue);
00157    }else{
00158       sprintf(strbuf, " Out of Range: %0.3f", fBadValue);
00159    }
00160 
00161    sendSerial(strbuf);
00162 }
00163