Treehouse Mbed Team / Mbed 2 deprecated APS_DCM1SL2

Dependencies:   mbed

Revision:
0:44a3005d4f20
Child:
1:9f8583ba2431
diff -r 000000000000 -r 44a3005d4f20 src/serial.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/serial.cpp	Sat Nov 24 18:22:31 2018 +0000
@@ -0,0 +1,139 @@
+#include "mbed.h"
+#include "serial.h"
+#include "stdio.h"
+#include "string.h"
+#include "globals.h"
+#include "dio.h"
+
+/************* GLOBAL VARIABLES *****************************/
+struct serialStatusBits serialStatus;
+char strbuf[100] = {0};
+char rxbuf[MAXRXBUF+1] = {0};
+volatile unsigned int bufloc = 0;
+
+/************* FORWARD DECLARATIONS *************************/
+void Rx_IRQHandler(void);
+
+
+// set up serial port for 115200 baud
+Serial pc(USBTX, USBRX, 115200);
+
+
+// initialize the serial port variables
+void initSerial(void)
+{
+   // create the RxIRQ callback
+   pc.attach(&Rx_IRQHandler, Serial::RxIrq);
+   
+}
+   
+
+// this is the interrupt request handler (IRQ) for ALL Recoev interrupts
+void Rx_IRQHandler(void)
+{
+   // reinit the repeat flag
+   serialStatus.repeat = FALSE;
+
+   // check if the USART1 receive interrupt flag was set
+   while (pc.readable())
+   {
+      led_usb = 0;
+      rxbuf[bufloc] = pc.getc();
+
+      if (rxbuf[bufloc] == CR)        // check for end of command
+      {
+         serialStatus.command = TRUE;
+         rxbuf[bufloc] = 0;
+         bufloc = 0;
+         return;
+      }
+      else if (rxbuf[bufloc] == '$')  // check for computer command
+      {
+         serialStatus.computer = TRUE;
+         bufloc = 0; // reset the buffer if computer command
+      }
+      else if (rxbuf[bufloc] == ')')  // check for repeat command
+      {
+         serialStatus.repeat = TRUE;
+         rxbuf[bufloc] = 0;
+      }
+
+      // Echo character back out the serial port
+      if (!serialStatus.computer)
+      {
+         pc.putc((unsigned char)rxbuf[bufloc]);
+      }
+
+      // handle backspace
+      if (rxbuf[bufloc] != 0x08)
+      {
+         bufloc++; // increment the location
+      }
+      else
+      {
+         if (bufloc > 0)
+         {
+            rxbuf[--bufloc] = 0;  // set to null
+         }
+         else
+         {
+            rxbuf[bufloc] = 0;  // set to null
+         }
+      }
+      
+      // check for overflow
+      if (bufloc >= MAXRXBUF)
+      {
+         bufloc = 0;
+         rxbuf[0] = 0;
+      }
+      
+      led_usb = 1;
+   }
+}
+
+
+// sends carriage return and linefeed and prompt character
+void sendCRLF(void)
+{
+ char strMenu[30] ={0};
+/*
+    switch(menuType)
+    {
+      case  MENU_DIFFERENTIAL: strcpy(strMenu,"DIFF"); break;
+      case  MENU_SINGLE: strcpy(strMenu,"SINGLE"); break;
+      case  MENU_DUAL: strcpy(strMenu,"DUAL"); break;
+      case  MENU_INVERT: strcpy(strMenu,"INVERT"); break;
+      case  MENU_SETTINGS: strcpy(strMenu,"SET"); break;
+      case  MENU_SETTINGS_CHAN1: strcpy(strMenu,"SET CHAN1"); break;
+      case  MENU_SETTINGS_CHAN2: strcpy(strMenu,"SET CHAN2"); break;
+      case  MENU_SETTINGS_CLOCK: strcpy(strMenu,"SET CLOCK"); break;
+      case  MENU_CALIBRATE: strcpy(strMenu,"CAL"); break;
+      case  MENU_TEST: strcpy(strMenu,"TEST"); break;
+      case  MENU_MAIN: strcpy(strMenu,"MAIN"); break;
+      default: strcpy(strMenu,"UNDF"); break;
+    }
+*/
+    // append the CR,LF and the ready character
+     if(serialStatus.computer)
+     {
+         sendSerial("\r");
+     }
+     else
+     {
+        sprintf(strbuf,"\n\r%s>",strMenu);
+        sendSerial(strbuf);
+        // adds a carrot so you know you are talking in terminal
+        // sendSerial("\n\r>");
+     }
+}
+
+// send buffter to the serial port
+void sendSerial(char *buf)
+{
+    led_usb = 0;
+    pc.printf(buf);
+    led_usb = 1;
+}
+
+ 
\ No newline at end of file