samuel belete / Mbed 2 deprecated mcr20_wireless_uart

Dependencies:   fsl_phy_mcr20a fsl_smac mbed-rtos mbed

Fork of mcr20_wireless_uart by Freescale

Files at this revision

API Documentation at this revision

Comitter:
andreikovacs
Date:
Mon Jun 29 05:50:47 2015 +0000
Parent:
26:56ca40dcfae1
Child:
28:2555c5ae3ccd
Commit message:
Changed app to behave like the initial Wireless UART demo

Changed in this revision

circular_buffer.cpp Show annotated file Show diff for this revision Revisions of this file
circular_buffer.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/circular_buffer.cpp	Mon Jun 29 05:50:47 2015 +0000
@@ -0,0 +1,77 @@
+#include "circular_buffer.h"
+
+CircularBuffer::CircularBuffer()
+{
+    size = gCircularBufferSize_c;
+    readIndex = 0;
+    writeIndex = 0;
+    count = 0;
+    MEM_Init();
+    buffer = (uint8_t *) MEM_BufferAlloc(size * sizeof(uint8_t));
+    if ( NULL == buffer )
+    {
+        /*if buffer alloc fails stop the program execution*/
+        while(1);
+    }
+}
+ 
+CircularBuffer::CircularBuffer(uint32_t sz)
+{
+    size = sz;
+    readIndex = 0;
+    writeIndex = 0;
+    count = 0;
+    MEM_Init();
+    buffer = (uint8_t *) MEM_BufferAlloc(size * sizeof(uint8_t));
+    if ( NULL == buffer )
+    {
+        /*if buffer alloc fails stop the program execution*/
+        while(1);
+    }
+}
+
+CircularBuffer::~CircularBuffer()
+{
+    size = 0;
+    readIndex = 0;
+    writeIndex = 0;
+    count = 0;
+    MEM_BufferFree(buffer);  
+}
+
+bufferStatus_t CircularBuffer :: addToBuffer (uint8_t c)
+{
+    buffer[writeIndex] = c;
+    writeIndex++;
+    if (writeIndex >= size)
+    {
+        writeIndex = 0;
+    }
+    count++;
+    if (count >= size)
+    {
+        return buffer_Full_c;
+    }
+    return buffer_Ok_c;
+}
+
+bufferStatus_t CircularBuffer :: getFromBuffer (uint8_t *c)
+{
+    if ( 0 == count )
+    {
+        return buffer_Empty_c;
+    }
+    (*c) = buffer[readIndex];
+    readIndex++;
+    if (readIndex >= size)
+    {
+        readIndex = 0;
+    }
+    count--;
+    return buffer_Ok_c;
+}
+
+uint32_t CircularBuffer :: getCount()
+{
+    return count;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/circular_buffer.h	Mon Jun 29 05:50:47 2015 +0000
@@ -0,0 +1,34 @@
+#ifndef __CIRCULAR_BUFFER_H__
+#define __CIRCULAR_BUFFER_H__
+
+#include "EmbeddedTypes.h"
+#include "MemManager.h"
+
+#ifndef gCircularBufferSize_c
+#define gCircularBufferSize_c 32
+#endif
+
+typedef enum bufferStatus_tag
+{
+    buffer_Ok_c = 0,
+    buffer_Empty_c,
+    buffer_Full_c
+}bufferStatus_t;
+
+class CircularBuffer {
+  public:
+    CircularBuffer();
+    CircularBuffer(uint32_t sz);
+    ~CircularBuffer();
+    bufferStatus_t addToBuffer (uint8_t c);
+    bufferStatus_t getFromBuffer (uint8_t *c);
+    uint32_t getCount();
+  private:
+    uint8_t *buffer;
+    uint32_t size;
+    uint32_t readIndex;
+    uint32_t writeIndex; 
+    uint32_t count;
+};
+
+#endif
\ No newline at end of file
--- a/main.cpp	Tue Jun 23 11:49:16 2015 +0000
+++ b/main.cpp	Mon Jun 29 05:50:47 2015 +0000
@@ -1,11 +1,35 @@
 #include "mbed.h"
 #include "rtos.h"
 
-#include "MemManager.h"
 #include "Phy.h"
 #include "SMAC_Interface.h"
 #include "SMAC_Config.h"
+#include "MemManager.h"
+#include "circular_buffer.h"
 
+char * const cu8FreescaleLogo[]={  
+  "\f\r\n",
+  "\n\r\n\r\n\r      #\n",
+  "\r     ###\n",
+  "\r    ###  *\n",
+  "\r     #  ***\n",
+  "\r       ***  #\n",
+  "\r        *  ###\n",
+  "\r          ###\n",
+  "\r        *  #\n",
+  "\r       ***\n",
+  "\r      ***  #\n",
+  "\r    #  *  ###\n",
+  "\r   ###   ###\n",
+  "\r  ###  *  #         F R E E S C A L E\n",
+  "\r   #  ***\n",
+  "\r     ***            S E M I C O N D U C T O R\n",
+  "\r   #  *\n",
+  "\r  ###               2 0 1 5\n",
+  "\r ###\n",
+  "\r  #           Wireless Uart Demo\r\n\n",
+  NULL
+};
 
 #define gMcps_Cnf_EVENT_c        (1<<1)
 #define gMcps_Ind_EVENT_c        (1<<2)
@@ -14,6 +38,11 @@
 #define gMlme_TimeoutInd_EVENT_c (1<<5)
 #define gWUSelf_EVENT_c          (1<<6)
 
+#define gDefaultBaudRate_UART_c 115200UL
+
+Serial uart(USBTX, USBRX);
+CircularBuffer uartBuf;
+
 #ifdef VERBOSE
 static bool_t bCCAFailed;
 static bool_t bACKFailed;
@@ -24,18 +53,34 @@
 rxPacket_t *gAppRxPacket;
 static txContextConfig_t txConfigContext;
 
+
+void PrintMenu(char * const pu8Menu[])
+{
+  uint8_t u8Index = 0;
+  while(pu8Menu[u8Index]){
+    uart.printf(pu8Menu[u8Index]);
+    u8Index++;
+  }
+}
+
+
 void InitProject(void);
 void InitApp(void);
 
 extern smacErrors_t smacToAppMlmeSap(smacToAppMlmeMessage_t* pMsg, instanceId_t instance);
 extern smacErrors_t smacToAppMcpsSap(smacToAppDataMessage_t* pMsg, instanceId_t instance);
 
-DigitalOut led1(LED1);
+DigitalOut led1(LED_GREEN);
 InterruptIn sw2(SW2);
 uint32_t button_pressed;
 Thread *thread2;
 Thread *eventsThread;
 
+void uartSetBaudRate(uint32_t b)
+{
+    uart.baud(b);
+}
+
 void sw2_press(void)
 {
     thread2->signal_set(0x1);
@@ -45,7 +90,7 @@
 {
     while (true) {
         led1 = !led1;
-        Thread::wait(1000);
+        Thread::wait(200);
     }
 }
 
@@ -59,19 +104,17 @@
 
 void events_thread(void const *argument)
 {
-    uint8_t rcvd = 0;
+    uint8_t rcvd = 0, c = 0; 
 
     while (true)
     {
         Thread::signal_wait(0x1);
-
         if(gMcps_Cnf_EVENT_c == (gTaskEventFlags & gMcps_Cnf_EVENT_c))
         {
             //get back in RX
             MLMERXEnableRequest(gAppRxPacket, 0); 
+            //uart.printf("McpsDataCnf: Packet sent\r\n");
 
-            //printf("McpsDataCnf: Packet sent\r\n");
-            //fflush(stdout);
         }
         
         if(gMcps_Ind_EVENT_c == (gTaskEventFlags & gMcps_Ind_EVENT_c))
@@ -81,35 +124,38 @@
             //get back in RX
             //gAppRxPacket = (rxPacket_t*)MEM_BufferAlloc(gMaxSmacSDULength_c + sizeof(rxPacket_t));
             //gAppRxPacket->u8MaxDataLength = gMaxSmacSDULength_c;
+            uart.printf("%c", rcvd);
             MLMERXEnableRequest(gAppRxPacket, 0);
 
-            printf("McpsDataInd: Received %d\r\n", rcvd);
-            fflush(stdout);
+            
         }
         
         if(gMlme_TimeoutInd_EVENT_c == (gTaskEventFlags & gMlme_TimeoutInd_EVENT_c))
         {
-            printf("MlmeTimeoutInd: \r\n");
-            fflush(stdout);
+            uart.printf("MlmeTimeoutInd: \r\n");
         }
         
         if(gMlme_EdCnf_EVENT_c == (gTaskEventFlags & gMlme_EdCnf_EVENT_c))
         {
-            printf("EdCnf: \r\n");
-            fflush(stdout);
+            uart.printf("EdCnf: \r\n");
         }
         
         if(gMlme_CcaCnf_EVENT_c == (gTaskEventFlags & gMlme_CcaCnf_EVENT_c))
         {
-            printf("CcaCnf: \r\n");
-            fflush(stdout);
+            uart.printf("CcaCnf: \r\n");
         }
         
         if(gWUSelf_EVENT_c == (gTaskEventFlags & gWUSelf_EVENT_c))
         {
-
+            if (buffer_Ok_c == uartBuf.getFromBuffer(&c))
+            {
+                gAppTxPacket->smacPdu.smacPdu[0] = c;
+                gAppTxPacket->u8DataLength = 1;
+                (void)MLMERXDisableRequest();
+                (void)MCPSDataRequest(gAppTxPacket);
+            }
         }
-
+       
         gTaskEventFlags = 0;
     }
 }
@@ -120,28 +166,32 @@
     Thread thread(led_thread);
     thread2 = new Thread(button_thread);
     eventsThread = new Thread(events_thread);
-
     Phy_Init();
     InitSmac();
-
+    
+    uartSetBaudRate(gDefaultBaudRate_UART_c);
+    
     //Tell SMAC who to call when it needs to pass a message to the application thread.
     Smac_RegisterSapHandlers((SMAC_APP_MCPS_SapHandler_t)smacToAppMcpsSap,(SMAC_APP_MLME_SapHandler_t)smacToAppMlmeSap,0);
 
     InitApp();
+    
+    PrintMenu(cu8FreescaleLogo);
 	    
     button_pressed = 0;
     sw2.fall(&sw2_press);
-    while (true) {
-        Thread::wait(5000);
-        
-        gAppTxPacket->smacPdu.smacPdu[0] = (uint8_t)button_pressed;
-        gAppTxPacket->u8DataLength = 1;
-        (void)MLMERXDisableRequest();
-        (void)MCPSDataRequest(gAppTxPacket);
-        
-        printf("SW2 was pressed (last 5 seconds): %d \r\n", button_pressed);
-        fflush(stdout);
-        button_pressed = 0;
+    while (true) 
+    {
+        if(uart.readable())
+        { 
+            (void)uartBuf.addToBuffer(uart.getc());
+        }
+        if ( uartBuf.getCount() )
+        {
+            gTaskEventFlags |= gWUSelf_EVENT_c;
+            eventsThread->signal_set(0x1);
+        }   
+        Thread::yield();
     }
 }
 
@@ -217,7 +267,6 @@
         default:
             break;
     }
-    
     eventsThread->signal_set(0x1);
     MEM_BufferFree(pMsg);