Library set up as dummy module on mbed to mimic Nordic.

Dependencies:   mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
Stephen_NewVistas
Date:
Tue Dec 13 00:20:29 2016 +0000
Parent:
1:d6b18299a715
Commit message:
everything commented . now going to strip out mbed stuff.

Changed in this revision

BTpacket.cpp Show annotated file Show diff for this revision Revisions of this file
BTpacket.h Show annotated file Show diff for this revision Revisions of this file
list.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
rs485.cpp Show annotated file Show diff for this revision Revisions of this file
rs485.h Show annotated file Show diff for this revision Revisions of this file
--- a/BTpacket.cpp	Mon Dec 12 23:19:30 2016 +0000
+++ b/BTpacket.cpp	Tue Dec 13 00:20:29 2016 +0000
@@ -0,0 +1,3 @@
+#include "BTpacket.h"
+
+
--- a/BTpacket.h	Mon Dec 12 23:19:30 2016 +0000
+++ b/BTpacket.h	Tue Dec 13 00:20:29 2016 +0000
@@ -1,7 +1,12 @@
+#ifndef BTPACKET_H
+#define BTPACKET_H
+
 #pragma pack(1)
 typedef struct{
     unsigned char command;
     unsigned char packetLength;
     unsigned char packetData[18];
 } BTpacket_t;
-#pragma pack(0)
\ No newline at end of file
+#pragma pack(0)
+
+#endif
--- a/list.h	Mon Dec 12 23:19:30 2016 +0000
+++ b/list.h	Tue Dec 13 00:20:29 2016 +0000
@@ -1,7 +1,6 @@
 #ifndef LIST_H
 #define LIST_H
 
-
 typedef struct{
     unsigned char command;
     void *function;
--- a/main.cpp	Mon Dec 12 23:19:30 2016 +0000
+++ b/main.cpp	Tue Dec 13 00:20:29 2016 +0000
@@ -8,7 +8,14 @@
 #include "uart1.h"
 #include "BTpacket.h"
 
-#define FX 0x02
+extern const unsigned char DEVADDRESS = 0xF0;
+#define NORDIC_ADDRESS     DEVADDRESS
+#define BTMODULE_A_ADDRESS 0x20                     //  #CC not sure if this has been defined by CEPD
+#define BTMODULE_B_ADDRESS 0x22                     //  #CC not sure if this has been defined by CEPD
+
+enum Commands   {   FX1 = 0x10 , FX1_ACK = 0x90 ,   //  #CC list the commands needed for the NORDIC Module
+                    FX2 = 0x12 , FX2_ACK = 0x92
+                };
 
 /*===========================================    
 *   These functions need to be completed
@@ -16,9 +23,11 @@
 */
 int Bluetooth_ReceivePacket( Packet * );
 int Bluetooth_SendChar( unsigned char );
+int Bluetooth_SerialHandler( void );
 /*==========================================*/
 
-void Fx( unsigned char * );
+void Fx1( unsigned char * );
+void Fx2( unsigned char * );
 
 
 /*===========================================    
@@ -39,11 +48,13 @@
 }
 
 void Test( void );
+void PrintPacket( Packet *_packet );
 /*==========================================*/
 
 int main() {  
     
-    RegisterCommand( FX , &Fx );  
+    RegisterCommand( FX1 , &Fx1 );   
+    RegisterCommand( FX2 , &Fx2 );            
     
     initComms();
     pc.printf( "\n\rStarted...\n\r" );
@@ -60,10 +71,29 @@
 
 }
 
+//================ Custom Functions =============================================
+/*  These functions local functions to the Nordic chip that are exposed to the 
+*    system. They must follow this prototype (same return values and parameter)
+*     and must be registered with a command using RegisterCommand() from RS485
+*     library.
+*/
+void Fx1( unsigned char *_receivedData ){
+    
+    pc.printf( "In FX1!\n\r" );
+    
+    Packet packet;
+    packet.sourceID = 0xF0;
+    packet.deviceID = 0xFE;
+    packet.command = 0x90;
+    packet.packetData[0] = 0x22;
+    packet.packetLength = 0x01;
+    
+    SetResponsePacket( &packet );
+}
 
-void Fx( unsigned char *_receivedData ){
+void Fx2( unsigned char *_receivedData ){
     
-    pc.printf( "In FX!\n\r" );
+    pc.printf( "In FX2!\n\r" );
     
     Packet packet;
     packet.sourceID = 0xF0;
@@ -72,44 +102,82 @@
     packet.packetData[0] = 0x22;
     packet.packetLength = 0x01;
     
-    SetResponse( &packet );
+    SetResponsePacket( &packet );
 }
+//================ End Custom Functions =========================================
 
-int Bluetooth_SerialGetChar( unsigned char *_c ){
-    if( bus.readable() ){
-        char c = bus.getc();
-        pc.printf( "%x" , c );
-        _c = (unsigned char*)c;
-        return 1;
-    }   
+//#CC This needs to handle the uart port and pass received bytes on
+/******************************************************************************
+ * Bluetooth_SerialHandler
+ *
+ * The UART port needs to be handled and when a byte is received it is passed on
+ *  to SerialHandler() form the rs485 library.
+ *
+ * PreCondition:    None
+ *
+ * Input:           '_packet' - packet received from the bus.
+ *
+ *
+ * Output:          None
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/
+int Bluetooth_SerialHandler( void ){
+//    if( ByteAvailableOnSerial ){
+//        SerialHandler( GetByte() );    
+//    }
     return 0;
 }
 
+//#CC This needs to be modified as commented below
+/******************************************************************************
+ * Bluetooth_ReceivePacket
+ *
+ * This gets automatically called by the bus library when a complete packet is
+ *  received. It checks the address the packet is going to and redirects the
+ *  message appropriately (ie: if message is for the Nordic chip it calls a
+ *  function or it sends it off to a BT device).
+ *
+ * PreCondition:    None
+ *
+ * Input:           '_packet' - packet received from the bus.
+ *
+ *
+ * Output:          None
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/
 int Bluetooth_ReceivePacket( Packet *_packet ){
 
     #ifdef DEBUG
         PrintPacket( _packet );
     #endif // DEBUG   
     
-    switch ( _packet->command ){
+    switch ( _packet->command ){                //  makes decision based off where packet is addressed to go
         
-        case 0x01:        
-            CheckFunction( _packet );
+        case NORDIC_ADDRESS:                    //  if the message is for the Nordic chip...              
+            if( !CheckFunction( _packet ) ){    //  this will call the appropriate function if one is registered
+                //#CC                           //  code goes here if the function is not registered
+            }
             break;
             
-        case 0x02:
-        
+        case BTMODULE_A_ADDRESS:                //  if message is for Bluetooth Module A
+            //#CC
+            /*
+                Need to bring in packet and modify to send out over BT            
+            */                   
             break;
             
-        case 0x03:
+        case BTMODULE_B_ADDRESS:
         
             break;
             
         default:
             break;   
         
-    } 
-    
+    }   
     
     return 0;
 }
--- a/rs485.cpp	Mon Dec 12 23:19:30 2016 +0000
+++ b/rs485.cpp	Tue Dec 13 00:20:29 2016 +0000
@@ -3,14 +3,10 @@
 
 #include "rs485.h"
 
-
-#define PRINTPACKETDEBUG
-#define DEVADDRESS 0xF0
 #define DEBUG
 
 static list_t functionList = { .index = 0 };
 
-
 Packet buildPacket;
 static unsigned int buffPtr;
 static unsigned char buffer[128+1];
@@ -20,21 +16,35 @@
 unsigned int plength;
 unsigned char command;
 int message;
-//  extern int message;
-
-//  extern unsigned char packetBuffer[128];
-//  extern unsigned int packetBufferSize;
 static unsigned char packetBuffer[128];
 static unsigned int packetBufferSize;
 
+/************************** External Functions********************************
+*   These functions need to be externall defined to be able to process packets
+*    and to send out chars over the bus.
+/************************** External Functions********************************/
 extern int Bluetooth_ReceivePacket( Packet *_packet );
 extern int Bluetooth_SendChar( unsigned char );
+extern const unsigned char DEVADDRESS;
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-
+/******************************************************************************
+ * Function void RegisterCommand( command , function )
+ *
+ * When a command is registered with this function it will be added to the function
+ *  list. Once on the list, when a packet is received on the bus containing that
+ *  command the associated function will get called and it will be passed the data
+ *  within the packet as a parameter.
+ *
+ * PreCondition:    None
+ *
+ * Input:           'command'  - command byte
+ *                  'function' - pointer to function to be called
+ *                  
+ * Output:          None
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/
 unsigned char RegisterCommand( unsigned char _command , void (*_function)( unsigned char *) ){
     functionList.list[ functionList.index ].function = _function;
     functionList.list[ functionList.index ].command = _command;
@@ -42,6 +52,23 @@
     return 0;
 }
 
+/******************************************************************************
+ * Function void CheckFunction( Packet * )
+ *
+ * This passes in a packet and compares the command byte with a list of registered
+ *  commands. If it finds a match it will call that function along with the data
+ *  in the packet.
+ *
+ * PreCondition:    None
+ *
+ * Input:           '_packet' - packet containing command byte and data
+ *                  
+ * Output:          '1' - command matched and function called
+ *                  '2' - no command found
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/
 int CheckFunction( Packet *_packet ){
     int i = 0;
     void (*Caller)(unsigned char *);
@@ -59,6 +86,21 @@
        
 }
 
+/******************************************************************************
+ * Function void SendAck( void )
+ *
+ * Sends an acknowledge byte over the bus. It calls an external function to
+ *  handle the actual hardware communication.
+ *
+ * PreCondition:    None
+ *
+ * Input:           '_packet' - prebuilt packet to send
+ *                  
+ * Output:          None
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/ 
 static void SendAck( void ){
     #ifdef DEBUG
         pc.printf( "\n\rAck Sent.\n\r" );
@@ -67,6 +109,21 @@
     Bluetooth_SendChar( ack );
 }
 
+/******************************************************************************
+ * Function void SendNack( void )
+ *
+ * Sends a non acknowledge byte over the bus. It calls an external function to
+ *  handle the actual hardware communication.
+ *
+ * PreCondition:    None
+ *
+ * Input:           '_packet' - prebuilt packet to send
+ *                  
+ * Output:          None
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/ 
 static void SendNack( void ){
     #ifdef DEBUG
         pc.printf( "\n\rNack Sent.\n\r" );
@@ -75,10 +132,40 @@
     Bluetooth_SendChar( nack );
 }
 
+/******************************************************************************
+ * Function void ProcessPacket( Packet *)
+ *
+ * Gets automatically called once an entire packet is successfully received.
+ *  May modify what is done, but it should just pass the packet over to 
+ *  an external function where it can be correctly handled.
+ *
+ * PreCondition:    None
+ *
+ * Input:           '_packet' - packet received on bus
+ *                  
+ * Output:          None
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/ 
 static int ProcessPacket( Packet *_packet ){
     return Bluetooth_ReceivePacket( _packet );
 }
-  
+
+/******************************************************************************
+ * Function void setResponse(int, int, int)
+ *
+ * This function sets the feedback information to be send in the master device.
+ *
+ * PreCondition:    None
+ *
+ * Input:           '_packet' - prebuilt packet to send
+ *                  
+ * Output:          None
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/  
 void SendMessage( void ){    
     int j = 0;
     Bluetooth_SendChar( packetBuffer[ j++ ] );
@@ -105,7 +192,7 @@
 }
 
 /******************************************************************************
- * Function void setResponse(int, int, int)
+ * Function void SetResponse( Packet *_packet )
  *
  * This function sets the feedback information to be send in the master device.
  *
@@ -118,12 +205,54 @@
  * Side Effects:    None
  *
  *****************************************************************************/ 
- void SetResponse( Packet *_packet ){
+ void SetResponsePacket( Packet *_packet ){
     packetBufferSize = getFormattedPacket(_packet, packetBuffer);    
     message = 1;
 }
 
 /******************************************************************************
+ * Function void SetResponseWithData( unsigned char deviceID, unsigned char 
+    command, unsigned char * data, unsigned short dataLength)
+ *
+ * This function sets the feedback information to be send in the master device.
+ *
+ * PreCondition:    None
+ *
+ * Input:           'deviceID'   - address where the packet is going
+                    'command'    - command to be sent
+                    '*data'      - point to unsiged char array packet with data
+                    'dataLength' - length of 'data'
+ *                  
+ * Output:          None
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/ 
+void SetResponseWithData( unsigned char deviceID, unsigned char command, unsigned char * data, unsigned short dataLength){
+        
+    //  build packet to be sent    
+    Packet packet;    
+    packet.deviceID = deviceID;
+    packet.sourceID = DEVADDRESS;
+    packet.command = command;
+    packet.packetLength = dataLength;
+    memcpy( packet.packetData , data , dataLength );
+    packetBufferSize = getFormattedPacket(&packet, packetBuffer);
+    
+    //  print message to be sent
+    #ifdef DEBUG
+        pc.printf( "Message to send: ");    
+        unsigned short length = 0;
+        for( length = 1 ; length < ( dataLength + sizeof( packet.deviceID ) + sizeof( packet.sourceID ) + sizeof( packet.command ) + sizeof( packet.packetLength ) + 1 ) ; length++ ){
+            pc.printf( "%x " , packetBuffer[ length ] );   
+        }
+        pc.printf( "\n\r" );
+    #endif
+    
+    message = 1;
+}
+
+/******************************************************************************
  * Function void SerialHandler(unsigned char )
  *
  * This function handles the received data from the Serial Communication.
@@ -135,7 +264,8 @@
  *
  * Output:          None
  *
- * Side Effects:    None
+ * Side Effects:    Will call functions to send ACK, NACK and process a packet
+ *                   once an entire packet is received.
  *
  *****************************************************************************/
 void SerialHandler(unsigned char RXByte){
@@ -370,7 +500,7 @@
                
                 buffer[buffPtr++] = RXByte;             //  add RXByte to buffer
                 
-                #ifdef PRINTPACKETDEBUG
+                #ifdef DEBUG
                    pc.printf( "Receive Buffer: " );       //  debug print received packet
                     unsigned int i = 0;
                     for( i = 0 ; i < buffPtr ; i++ ){
@@ -398,7 +528,3 @@
 }   //  end serial2Handler function
 
 
-
-#ifdef __cplusplus
-}
-#endif
--- a/rs485.h	Mon Dec 12 23:19:30 2016 +0000
+++ b/rs485.h	Tue Dec 13 00:20:29 2016 +0000
@@ -20,7 +20,8 @@
 void RS485_Init();
 unsigned char RegisterCommand( unsigned char , void (*fx)(unsigned char *) );
 int CheckFunction( Packet *_packet );
-void SetResponse( Packet *);
+void SetResponsePacket( Packet *);
+void setResponseWithData(unsigned char deviceID, unsigned char command, unsigned char * data, unsigned short dataLength);
 
 #ifdef __cplusplus
 }