SNIC UART Interface library: Serial to Wi-Fi library for Murata TypeYD Wi-Fi module. For more information about TypeYD: http://www.murata.co.jp/products/microwave/module/lbwb1zzydz/index.html

Dependents:   SNIC-xively-jumpstart-demo SNIC-FluentLogger-example TCPEchoServer murataDemo ... more

Fork of YDwifiInterface by Takao Kishino

Files at this revision

API Documentation at this revision

Comitter:
kishino
Date:
Tue Mar 11 10:38:36 2014 +0000
Parent:
1:c6e5f49dce5f
Child:
3:9f90024d7fb2
Commit message:
Created the basic composition of SNIC UART command API.; Created the UART command for getting firmware's version.

Changed in this revision

YDwifi/YDwifi.cpp Show annotated file Show diff for this revision Revisions of this file
YDwifi/YDwifi.h Show annotated file Show diff for this revision Revisions of this file
YDwifi/YDwifiUartCommand.cpp Show annotated file Show diff for this revision Revisions of this file
YDwifi/YDwifiUartCommand.h Show annotated file Show diff for this revision Revisions of this file
YDwifi/YDwifi_uartmsg.cpp Show annotated file Show diff for this revision Revisions of this file
YDwifi/YDwifi_uartmsg.h Show annotated file Show diff for this revision Revisions of this file
YDwifiInterface.cpp Show annotated file Show diff for this revision Revisions of this file
YDwifiInterface.h Show annotated file Show diff for this revision Revisions of this file
--- a/YDwifi/YDwifi.cpp	Fri Mar 07 02:13:09 2014 +0000
+++ b/YDwifi/YDwifi.cpp	Tue Mar 11 10:38:36 2014 +0000
@@ -4,6 +4,17 @@
 #include <string>
 //#include <algorithm>
 
+using namespace murata_wifi;
+
+#define UART_RECVBUF_SIZE   2048
+typedef struct
+{
+    unsigned char buf[UART_RECVBUF_SIZE];
+    unsigned int  size;
+    bool          is_receive;
+}tagUART_RECVBUF_T;
+tagUART_RECVBUF_T   gUART_RCVBUF;
+
 C_YDwifi *C_YDwifi::mInstance_p;
 
 C_YDwifi::C_YDwifi(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud):
@@ -13,8 +24,11 @@
     mInstance_p       = this;
     mUartRequestSeq   = 0;
 
-    printf("baud:%d\r\n", baud);
-    mUart.baud( baud );
+    // Initialize uart
+    gUART_RCVBUF.is_receive = false;
+    gUART_RCVBUF.size       = 0;
+    mUart.baud( 115200 );
+    mUart.format(8, SerialBase::None, 1);
 }
 
 int C_YDwifi::initUart()
@@ -26,26 +40,31 @@
         printf("[C_YDwifi::initUart] thread cread failed\r\n");
         return -1;
     }
-    
-    // set intr callback function
-    mUart.attach( this, &C_YDwifi::uartIntr_callback, Serial::RxIrq );
-    
+
     return 0;
 }
 
-void C_YDwifi::uartIntr_callback( void )
+int C_YDwifi::sendUart( unsigned int len, unsigned char *data )
 {
-#if 0
-    C_YDwifi *instance = C_YDwifi::getInstance();
-    instance->mUart.putc('G');
-    instance->mUartRecvThread_p->signal_set(1);
-#else
-    mUart.putc('G');
-    mUartRecvThread_p->signal_set(1);
-#endif
+    int ret = 0;
+    
+    mUartMutex.lock();
+    for( int i = 0; i < len; i++ )
+    {
+        // Write to UART
+        ret = mUart.putc( data[i] );
+        if( ret == -1 )
+        {
+            ret = -1;
+            break;
+        }
+    }
+    mUartMutex.unlock();
+    return ret;
 }
 
 void C_YDwifi::uartRecvThread (void const *args_p) {
+
     C_YDwifi *instance_p = C_YDwifi::getInstance();
     if ( instance_p == NULL )
     {
@@ -53,15 +72,75 @@
     }
 
     int recvdata = 0;
-    printf("uartRecvThread\r\n");
-
+    int i;
+    
     /* UART recv thread main loop */
     for (;;) 
     {
-        Thread::signal_wait(1);
-//        wait(0.1);      
-        recvdata = instance_p->mUart.getc();
-        printf( "[thread]%02x\r\n", recvdata );
+        while( instance_p->mUart.readable() )
+        {
+            // Receive data from UART.
+            instance_p->mUartMutex.lock();
+            recvdata = instance_p->mUart.getc();
+            instance_p->mUartMutex.unlock();
+            
+            // Check UART receiving flg
+            if( gUART_RCVBUF.is_receive )
+            {
+                gUART_RCVBUF.buf[ gUART_RCVBUF.size ] = (unsigned char)recvdata;
+                gUART_RCVBUF.size++;
+                // Check  received data is EOM.
+                if( recvdata == UART_CMD_EOM )
+                {
+                    printf("[recv]\r\n");
+                    for( i = 0; i < gUART_RCVBUF.size; i++ )
+                    {
+                        printf("%02x ", gUART_RCVBUF.buf[i]);
+                    }
+                    printf("\r\n");
+                    
+                    // Get buffer for payload data
+                    unsigned char *payload_buf_p = instance_p->mUartCommand.getResponseBuf();
+                    unsigned char command_id;
+                    
+                    if( payload_buf_p != NULL )
+                    {
+                        // Get payload from received data from UART.
+                        int payload_len = C_YD_UartMsg::getResponsePayload( gUART_RCVBUF.size, gUART_RCVBUF.buf
+                                                                , &command_id, payload_buf_p );
+                        printf("[payload]\r\n");
+                        for( i = 0; i < payload_len; i++ )
+                        {
+                            printf("%02x ", payload_buf_p[i]);
+                        }
+                        printf("\r\n");
+                        
+                        // Checks in the command which is waiting.
+                        if( instance_p->mUartCommand.isWaitingCommand(command_id, payload_buf_p) )
+                        {
+                            // Set status
+                            instance_p->mUartCommand.setCommandStatus( payload_buf_p[2] );
+                            // Set signal for command response wait.
+                            instance_p->mUartCommand.signal();
+                        }
+                    }
+                    gUART_RCVBUF.size = 0;
+                    gUART_RCVBUF.is_receive = false;
+                }
+            }
+            else
+            {
+                // Check  received data is SOM.
+                if( recvdata == UART_CMD_SOM )
+                {
+                    gUART_RCVBUF.size = 0;
+                    gUART_RCVBUF.buf[ gUART_RCVBUF.size ] = (unsigned char)recvdata;
+                    gUART_RCVBUF.size++;
+                    gUART_RCVBUF.is_receive = true;
+                }
+            }
+        }
+        Thread::yield();
     }
 }
 
--- a/YDwifi/YDwifi.h	Fri Mar 07 02:13:09 2014 +0000
+++ b/YDwifi/YDwifi.h	Tue Mar 11 10:38:36 2014 +0000
@@ -4,8 +4,13 @@
 #include "mbed.h"
 #include "rtos.h"
 #include "RawSerial.h"
+
+#include "YDwifiUartCommand.h"
 //#include "CBuffer.h"
 
+namespace murata_wifi
+{
+
 /** C_YDwifi class
  */
 class C_YDwifi
@@ -42,17 +47,32 @@
     };
     Thread          *mUartRecvThread_p;
 
+    /** Send data to UART
+        @param len  Length of send data
+        @param data Pointer of send data
+        @return 0:success/other:fail
+    */
+    int sendUart( unsigned int len, unsigned char *data );
+
+    
 protected:
+    /** GEN_FW_VER_GET_REQ Command */
+    typedef struct 
+    {
+        unsigned char cmd_sid;
+        unsigned char seq;
+    }tagGEN_FW_VER_GET_REQ_T;
+
     static C_YDwifi *mInstance_p;
     
-    Mutex           mUartMutex;
-//    RawSerial       mUart;
-    Serial          mUart;
-    DigitalInOut    mModuleReset;
-
-    /* Function */
-    void uartIntr_callback( void );
+    Mutex               mUartMutex;
+    DigitalInOut        mModuleReset;
+    C_YDwifiUartCommand mUartCommand;
+    RawSerial           mUart;
+    
     static void uartRecvThread( void const *args_p );
+    
 };
+}
 
 #endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/YDwifi/YDwifiUartCommand.cpp	Tue Mar 11 10:38:36 2014 +0000
@@ -0,0 +1,78 @@
+#include "YDwifiUartCommand.h"
+
+using namespace murata_wifi;
+
+void C_YDwifiUartCommand::setCommandID( unsigned char cmd_id )
+{
+    mCommandID = cmd_id;
+}
+
+unsigned char C_YDwifiUartCommand::getCommandID()
+{
+    return mCommandID;
+}
+
+void C_YDwifiUartCommand::setCommandSID( unsigned char cmd_sid )
+{
+    mCommandSID = cmd_sid;
+}
+
+unsigned char C_YDwifiUartCommand::getCommandSID()
+{
+    return mCommandSID;
+}
+
+void C_YDwifiUartCommand::setCommandStatus( unsigned char status )
+{
+    mCommandStatus = status;
+}
+
+unsigned char C_YDwifiUartCommand::getCommandStatus()
+{
+    return mCommandStatus;
+}
+
+void C_YDwifiUartCommand::setResponseBuf( unsigned char *buf_p )
+{
+    mResponseBuf_p = buf_p;
+}
+
+unsigned char *C_YDwifiUartCommand::getResponseBuf()
+{
+    return mResponseBuf_p;
+}
+
+
+int C_YDwifiUartCommand::wait()
+{
+    int ret = 0;
+    // Get thread ID
+    mCommandThreadID = osThreadGetId();
+    
+    // Signal flags that are reported as event are automatically cleared.
+    osEvent event_ret = osSignalWait( UART_COMMAND_SIGNAL, UART_COMMAND_WAIT_TIMEOUT);
+    if( event_ret.status != osEventSignal )
+    {
+        ret = -1;
+    }
+    return ret;
+}
+
+int C_YDwifiUartCommand::signal()
+{
+    // set signal
+    return osSignalSet(mCommandThreadID, UART_COMMAND_SIGNAL);;
+}
+
+bool C_YDwifiUartCommand::isWaitingCommand( unsigned int command_id, unsigned char *payload_p )
+{
+    bool ret = false;
+
+    if( (command_id == getCommandID())
+        && (payload_p[0] == (getCommandSID() | 0x80) ) )
+    {
+        ret = true;
+    }
+    return ret;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/YDwifi/YDwifiUartCommand.h	Tue Mar 11 10:38:36 2014 +0000
@@ -0,0 +1,83 @@
+#ifndef _YD_WIFI_UART_COMMAND_H_
+#define _YD_WIFI_UART_COMMAND_H_
+#include "mbed.h"
+#include "rtos.h"
+
+namespace murata_wifi
+{
+    
+/** Wait signal ID of UART command */
+#define UART_COMMAND_SIGNAL        0x00000001
+/** Timeout of UART command wait(ms)*/
+#define UART_COMMAND_WAIT_TIMEOUT  10000
+
+class C_YDwifiUartCommand
+{
+public:
+
+    /** Set Command ID
+        @param cmd_id Command ID
+    */
+    void setCommandID( unsigned char cmd_id );
+
+    /** Get Command ID
+        @return Command ID
+    */
+    unsigned char getCommandID();
+
+    /** Set Command SubID
+        @param cmd_sid Command Sub ID
+    */
+    void setCommandSID( unsigned char cmd_sid );
+
+    /** Get Command SubID
+        @return Command Sub ID
+    */
+    unsigned char getCommandSID();
+    
+    /** Set Command status
+        @param status Command status
+    */
+    void setCommandStatus( unsigned char status );
+
+    /** Get Command status
+        @return Command status
+    */
+    unsigned char getCommandStatus();
+
+    /** Set Response buffer
+        @param buf_p Pointer of response buffer
+    */
+    void setResponseBuf( unsigned char *buf_p );
+
+    /** Get Response buffer
+        @return Pointer of response buffer
+    */
+    unsigned char *getResponseBuf();
+    
+    /** Checks in the command which is waiting from Command ID and Sub ID.
+        @param  command_id  Command ID
+        @param  payload_p   Command payload
+        @return true: Waiting command / false: Not waiting command
+    */
+    bool isWaitingCommand( unsigned int command_id, unsigned char *payload_p );
+
+    int wait();
+    
+    int signal();
+
+protected:
+    /** Command request thread ID */
+    osThreadId    mCommandThreadID;
+    /** Command ID */
+    unsigned char mCommandID;
+    /** Command SubID */
+    unsigned char mCommandSID;
+    /** Status of command response */
+    unsigned char mCommandStatus;
+    /** ResponseData of command response */
+    unsigned char *mResponseBuf_p;
+};
+    
+}
+#endif
--- a/YDwifi/YDwifi_uartmsg.cpp	Fri Mar 07 02:13:09 2014 +0000
+++ b/YDwifi/YDwifi_uartmsg.cpp	Tue Mar 11 10:38:36 2014 +0000
@@ -79,7 +79,7 @@
     return uart_cmd_len;
 }
 
-unsigned int C_YD_UartMsg::getResponsePayload( unsigned int cmd_len, unsigned char *recvdata_p
+unsigned int C_YD_UartMsg::getResponsePayload( unsigned int recvdata_len, unsigned char *recvdata_p
                                             , unsigned char *command_id_p,  unsigned char *payload_p )
 {
     unsigned short payload_len  = 0;
--- a/YDwifi/YDwifi_uartmsg.h	Fri Mar 07 02:13:09 2014 +0000
+++ b/YDwifi/YDwifi_uartmsg.h	Tue Mar 11 10:38:36 2014 +0000
@@ -108,8 +108,13 @@
 
 
     /** Get uart command from receive data.
+            @param recvdata_len   Receive data length
+            @param recvdata_p     Pointer of received data from UART
+            @param command_id_p   Pointer of command ID[output]
+            @param payload_p      Pointer of payload[output]
+            @return Payload length    
     */
-    static unsigned int getResponsePayload( unsigned int cmd_len, unsigned char *recvdata_p
+    static unsigned int getResponsePayload( unsigned int recvdata_len, unsigned char *recvdata_p
                                     , unsigned char *command_id_p,  unsigned char *payload_p );
 protected:
 
--- a/YDwifiInterface.cpp	Fri Mar 07 02:13:09 2014 +0000
+++ b/YDwifiInterface.cpp	Tue Mar 11 10:38:36 2014 +0000
@@ -1,5 +1,18 @@
 #include "YDwifiInterface.h"
+#include "YDwifi_uartmsg.h"
 
+using namespace murata_wifi;
+
+#define MEMPOOL_BLOCK_SIZE  2048
+typedef struct
+{
+    unsigned char buf[MEMPOOL_BLOCK_SIZE];
+}tagMEMPOOL_BLOCK_T;
+#define MEMPOOL_PAYLOAD_NUM 1
+/** MemoryPool for payload of UART response */
+MemoryPool<tagMEMPOOL_BLOCK_T, MEMPOOL_PAYLOAD_NUM>     gMEMPOOL_PAYLOAD;
+
+#define UART_REQUEST_PAYLOAD_MAX    512
 
 C_YDwifiInterface::C_YDwifiInterface( PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud):
     C_YDwifi(tx, rx, cts, rts, reset, alarm, baud)
@@ -11,4 +24,58 @@
     /* Initialize UART */
     initUart();
     return 0;
-}
\ No newline at end of file
+}
+
+int C_YDwifiInterface::getFWVersion( unsigned char *version_p )
+{
+    // Get buffer for response payloadfrom MemoryPool
+    tagMEMPOOL_BLOCK_T *payload_buf = gMEMPOOL_PAYLOAD.alloc();
+    if( payload_buf == NULL )
+    {
+        printf("getFWVersion payload_buf NULL\r\n");
+        return -1;
+    }
+
+    tagGEN_FW_VER_GET_REQ_T req;
+    unsigned char payload_array[UART_REQUEST_PAYLOAD_MAX];
+    unsigned char command_array[UART_REQUEST_PAYLOAD_MAX];
+    unsigned short payload_len;
+    unsigned int   command_len;
+    int ret;
+    
+    // Make request
+    req.cmd_sid = UART_CMD_SID_GEN_FW_VER_GET_REQ;
+    req.seq = mUartRequestSeq++;
+    
+    // Make command payload
+    payload_len = C_YD_UartMsg::makePayload( sizeof(tagGEN_FW_VER_GET_REQ_T), (unsigned char *)&req, payload_array );
+    // Make all command request
+    command_len = C_YD_UartMsg::makeRequest( UART_CMD_ID_GEN, payload_array, payload_len, command_array );
+
+    // Set data for response
+    mUartCommand.setCommandID( UART_CMD_ID_GEN );
+    mUartCommand.setCommandSID( UART_CMD_SID_GEN_FW_VER_GET_REQ );
+    mUartCommand.setResponseBuf( payload_buf->buf );
+    
+    // Send uart command request
+    sendUart( command_len, command_array );
+    
+    // Wait UART response
+    ret = mUartCommand.wait();
+    printf( "getFWversion wait:%d\r\n", ret );
+    if( ret != 0 )
+    {
+        printf( "getFWversion failed\r\n" );
+        gMEMPOOL_PAYLOAD.free( payload_buf );
+        return -1;
+    }
+    
+    printf("getFWversion status:%02x\r\n", mUartCommand.getCommandStatus());
+    if( mUartCommand.getCommandStatus() == 0 )
+    {
+        unsigned char version_len = payload_buf->buf[3];
+        memcpy( version_p, &payload_buf->buf[4], version_len );
+    }
+    gMEMPOOL_PAYLOAD.free( payload_buf );
+    return 0;
+}
--- a/YDwifiInterface.h	Fri Mar 07 02:13:09 2014 +0000
+++ b/YDwifiInterface.h	Tue Mar 11 10:38:36 2014 +0000
@@ -3,27 +3,35 @@
 
 #include "YDwifi.h"
 
+namespace murata_wifi
+{
+
 class C_YDwifiInterface: public C_YDwifi {
 public:
 
-    /**
-    * Constructor
-    *
-    * \param tx mbed pin to use for tx line of Serial interface
-    * \param rx mbed pin to use for rx line of Serial interface
-    * \param cts mbed pin to use for cts line of Serial interface
-    * \param rts mbed pin to use for rts line of Serial interface
-    * \param reset reset pin of the wifi module
-    * \param alarm alarm pin of the wifi module (default: NC)
-    * \param baud baud rate of Serial interface (default: 9600)
+    /** Constructor
+        @param tx mbed pin to use for tx line of Serial interface
+        @param rx mbed pin to use for rx line of Serial interface
+        @param cts mbed pin to use for cts line of Serial interface
+        @param rts mbed pin to use for rts line of Serial interface
+        @param reset reset pin of the wifi module
+        @param alarm alarm pin of the wifi module (default: NC)
+        @param baud baud rate of Serial interface (default: 9600)
     */
-  C_YDwifiInterface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 9600);
+    C_YDwifiInterface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 9600);
+
+    /** Initialize the interface.
+        @return 0 on success, a negative number on failure
+    */
+    int init();
 
-  /** Initialize the interface with DHCP.
-  * Initialize the interface and configure it to use DHCP (no connection at this point).
-  * \return 0 on success, a negative number on failure
-  */
-  int init();
+    /** Get Firmware version string.
+        @param  version_p Pointer of FW version string.(null terminated)[output]
+        @return 0:success/other:fail
+        @note   This function is blocked until a returns.
+                When you use it by UI thread, be careful. 
+    */
+    int getFWVersion( unsigned char *version_p );
 
 #if 0
   /** Connect
@@ -63,5 +71,6 @@
   char* getNetworkMask();
 #endif
 };
+}
 
 #endif  /* _YD_WIFIINTERFACE_H_ */
\ No newline at end of file