Library set up as dummy module on mbed to mimic Nordic.
Revision 0:226550611f0d, committed 2016-12-12
- Comitter:
- Stephen_NewVistas
- Date:
- Mon Dec 12 23:06:58 2016 +0000
- Child:
- 1:d6b18299a715
- Commit message:
- Got system working on bus, calling registered functions with ACK/NACK/RESPONSE. Need to clean up.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/charactercode.h Mon Dec 12 23:06:58 2016 +0000 @@ -0,0 +1,14 @@ +#ifndef CHARACTERCODE_H +#define CHARACTERCODE_H + +#define STARTPACKET 0x7F +#define STARTPOLL 0x8F +#define FLAGBYTE 0x8E +#define NAKCHAR 0x8C +#define ACKCHAR 0x8D + +#define STX 0x55 +#define ETX 0x04 +#define ENQ 0x05 + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/comms.cpp Mon Dec 12 23:06:58 2016 +0000
@@ -0,0 +1,29 @@
+#include "mbed.h"
+#include "comms.h"
+
+#define SERIAL1_TX PA_9
+#define SERIAL1_RX PA_10
+
+I2C i2c( D4 , D5 );
+Serial pc( USBTX , USBRX );
+Serial bus( SERIAL1_TX , SERIAL1_RX );
+
+
+void initComms() {
+ pc.baud( 115200 );
+ i2c.frequency( 400000 );
+ bus.baud( 115200 );
+}
+
+void debugLog( char* fmt, ...) {
+ char buf[100]; // this should really be sized appropriately
+ // possibly in response to a call to vsnprintf()
+ va_list vl;
+ va_start(vl, fmt);
+
+ vsnprintf( buf, sizeof( buf), fmt, vl);
+
+ va_end( vl);
+
+ pc.printf(buf);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/comms.h Mon Dec 12 23:06:58 2016 +0000 @@ -0,0 +1,17 @@ +#ifndef COMMS_H +#define COMMS_H + +#include "mbed.h" + +//global hardware +extern I2C i2c; +extern Serial pc; +extern Serial bus; + + +void initComms(); + + +void debugLog( char* fmt, ...); + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/list.h Mon Dec 12 23:06:58 2016 +0000
@@ -0,0 +1,15 @@
+#ifndef LIST_H
+#define LIST_H
+
+
+typedef struct{
+ unsigned char command;
+ void *function;
+} listContents_t;
+
+typedef struct{
+ listContents_t list[64];
+ unsigned char index;
+} list_t;
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Mon Dec 12 23:06:58 2016 +0000
@@ -0,0 +1,174 @@
+//#define TEST
+#define DEBUG
+
+#include "mbed.h"
+#include "rtos.h"
+#include "comms.h"
+#include "rs485.h"
+#include "uart1.h"
+
+#define FX 0x02
+
+/*===========================================
+* These functions need to be completed
+*
+*/
+int Bluetooth_ReceivePacket( Packet * );
+int Bluetooth_SendChar( unsigned char );
+/*==========================================*/
+
+void Fx( unsigned char * );
+
+
+/*===========================================
+*
+*
+*/
+void receivedSerialData( void ){
+ while( uart1_is_char() ){
+ SerialHandler( uart1_get_char() );
+ }
+}
+
+void bus_thread(void const *argument){
+ while (true){
+ receivedSerialData();
+ wait_ms( 100 );
+ }
+}
+
+void Test( void );
+/*==========================================*/
+
+int main() {
+
+ RegisterCommand( FX , &Fx );
+
+ initComms();
+ pc.printf( "\n\rStarted...\n\r" );
+ init_uart1();
+
+ Thread bus( bus_thread , NULL , osPriorityRealtime , DEFAULT_STACK_SIZE );
+
+ while( true ){ };
+
+ #ifdef TEST
+ Test();
+ #endif // DEBUG
+
+
+}
+
+
+void Fx( unsigned char *_receivedData ){
+
+ pc.printf( "In FX!\n\r" );
+
+ Packet packet;
+ packet.sourceID = 0xF0;
+ packet.deviceID = 0xFE;
+ packet.command = 0x90;
+ packet.packetData[0] = 0x22;
+ packet.packetLength = 0x01;
+
+ SetResponse( &packet );
+}
+
+int Bluetooth_SerialGetChar( unsigned char *_c ){
+ if( bus.readable() ){
+ char c = bus.getc();
+ pc.printf( "%x" , c );
+ _c = (unsigned char*)c;
+ return 1;
+ }
+ return 0;
+}
+
+int Bluetooth_ReceivePacket( Packet *_packet ){
+
+ #ifdef DEBUG
+ pc.printf( "Process Packet\n\r" );
+
+ pc.printf( "\t%x - Device ID\n\r" , _packet->deviceID );
+ pc.printf( "\t%x - Source ID\n\r" , _packet->sourceID );
+ pc.printf( "\t%x - Command\n\r" , _packet->command );
+ pc.printf( "\t%x - Length\n\r" , _packet->packetLength );
+ unsigned char *ptr = (unsigned char *)&_packet->packetData;
+ for( int i = 0 ; i < _packet->packetLength ; i++ ){
+ pc.printf( "\t%x - Data[%d]\n\r" , *ptr++ , i );
+ }
+
+ #endif // DEBUG
+
+ CheckFunction( _packet );
+
+ return 0;
+}
+
+int Bluetooth_SendChar( unsigned char _c ){
+ bus.putc( _c );
+ return 0;
+}
+
+void Test( void ){
+
+ Packet commandPacket;
+ commandPacket.sourceID = 0xFE;
+ commandPacket.deviceID = 0xF0;
+ commandPacket.command = 0x10;
+ commandPacket.packetLength = 0x04;
+ commandPacket.packetData[0] = 0x8E;
+ commandPacket.packetData[1] = 0x7F;
+ commandPacket.packetData[2] = 0x8F;
+ commandPacket.packetData[3] = 0xB0;
+
+ unsigned char buf1[1024];
+ unsigned char buf2[1024];
+ unsigned char *ptr = buf1;
+
+ // buf for commandPacket
+ int size1 = getFormattedPacket( &commandPacket , buf1 );
+ int size2;
+
+ // buf for POLL
+ buf1[0] = 0x8F;
+ buf1[1] = 0xF0;
+ size1 = 2;
+
+ buf2[0] = 0x8D;
+ size2 = 1;
+
+ pc.printf( "size: %i\t" , size1 );
+
+ pc.printf( "buf: " );
+ for( int i = 0 ; i < size1 ; i++ ){
+ pc.printf( "%x " , *(ptr++) );
+ }
+ pc.printf( " \n\r" );
+
+ while(1) {
+ // poll with no message
+ ptr = buf1;
+ for( int i = 0 ; i < size1 ; i++ ){
+ SerialHandler( *(ptr++) );
+ wait( 1 );
+ }
+
+ // create message
+ SetResponse( 0xF0 , 0x10 , 0x01 );
+
+ // poll with message
+ ptr = buf1;
+ for( int i = 0 ; i < size1 ; i++ ){
+ SerialHandler( *(ptr++) );
+ wait( 1 );
+ }
+
+ // send ACK
+ ptr = buf2;
+ for( int i = 0 ; i < size2 ; i++ ){
+ SerialHandler( *(ptr++) );
+ wait( 1 );
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Mon Dec 12 23:06:58 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Dec 12 23:06:58 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/d75b3fe1f5cb \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/packetformatter.cpp Mon Dec 12 23:06:58 2016 +0000
@@ -0,0 +1,316 @@
+#include <stdio.h>
+#include <string.h>
+#include "packetformatter.h"
+#include "charactercode.h"
+//#include "command.h"
+
+//#define DEBUG
+
+static unsigned int checkSum;
+static unsigned int packetSize;
+static unsigned char buffer[1024];
+
+/******************************************************************************
+ * Function ConvertToPacket( Packet *_packet , unsigned char *_buffer )
+ *
+ * This function is passed an empty packet a buffer with a message. The message
+ * is broken down and placed into the packet.
+ *
+ *
+ * PreCondition: None
+ *
+ * Input: '_packet' - empty packet , '_buffer' - message
+ *
+ * Output:
+ *
+ * Side Effects: None
+ *
+ *****************************************************************************/
+int ConvertToPacket( Packet *_packet , unsigned char *_buffer ){
+ memset( _packet , '\0' , sizeof( Packet ) );
+
+ _packet->deviceID = (int)_buffer[1] ;
+ _packet->sourceID = (int)_buffer[2] ;
+ _packet->command = _buffer[3];
+ _packet->packetLength = _buffer[4];
+ _packet->packetLength = _packet->packetLength << 8;
+ _packet->packetLength |= _buffer[5];
+
+ int i;
+ for( i = 0 ; i < _packet->packetLength ; i++ ){
+ _packet->packetData[i] = *( _buffer + i + 6 );
+ }
+ return 0;
+}
+
+/******************************************************************************
+ * Function unsigned short escapedToSpecialChar(unsigned char)
+ *
+ * This function is called to convert a data if it is a special character (0x7F, 0x8F, 0x8E)
+ * to equivalent escaped character.
+ *
+ * e.g. Special Character Escaped Character
+ * 0x7F = 0x8E 0x01
+ * 0x8F = 0x8E 0x02
+ * 0x8E = 0x8E 0x8E
+ *
+ * PreCondition: None
+ *
+ * Input: 'c' - character to convert if it is an special character.
+ *
+ * Output: Equivalent escaped character
+ *
+ * Side Effects: None
+ *
+ *****************************************************************************/
+unsigned short
+specialToEscapedChar(unsigned char c)
+{
+
+ unsigned short escapedChar = 0;
+ char isSpecialChar = ((STARTPOLL == c) || (STARTPACKET == c) || (FLAGBYTE == c));
+
+ if(isSpecialChar)
+ {
+ escapedChar = (FLAGBYTE << 8);
+ switch(c)
+ {
+ case STARTPACKET:
+ escapedChar |= 0x01;
+ break;
+ case STARTPOLL:
+ escapedChar |= 0x02;
+ break;
+ case FLAGBYTE:
+ escapedChar |= FLAGBYTE;
+ break;
+ }
+ }
+ else
+ {
+ escapedChar = (unsigned char) c;
+ }
+
+ return escapedChar;
+}
+
+/******************************************************************************
+ * Function static void insertValueToBuffer(unsigned char)
+ *
+ * This function is called to insert the value to buffer.
+ *
+ * PreCondition: None
+ *
+ * Input: 'value' - value to insert in the buffer.
+ *
+ * Output: None
+ *
+ * Side Effects: Increment the packetSize variable
+ *
+ *****************************************************************************/
+static void
+insertValueToBuffer(unsigned char value)
+{
+ buffer[packetSize] = value;
+ packetSize++;
+}
+
+/******************************************************************************
+ * Function static void processPacketData(unsigned char)
+ *
+ * This function is called to process the packet if it is a special character it will
+ * convert to escaped character e.g. 0x7F = 0x8E 0x01, 0x8E = 0x8E 0x8E, 0x8F = 0x8E 0x02.
+ * The size of return value of specialToEscapedChar function is 2 bytes so it must be inserted
+ * in the buffer twice.
+ *
+ * PreCondition: None
+ *
+ * Input: 'packetData' - value to convert if it is a special character.
+ *
+ * Output: None
+ *
+ * Side Effects: None
+ *
+ *****************************************************************************/
+static void
+processPacketData(unsigned char packetData)
+{
+
+ unsigned short packetDataResult = specialToEscapedChar(packetData);
+
+ if( (packetDataResult >> 8) != 0)
+ {
+ insertValueToBuffer((unsigned char)(packetDataResult>> 8));
+ }
+
+ insertValueToBuffer((unsigned char) packetDataResult);
+
+}
+
+/******************************************************************************
+ * Function void getFormattedPacket(Packet *, unsigned char *)
+ *
+ * This function is called to process the packet in to a series of bytes that
+ * conforms into the protocol of the communication.
+ * Protocol Format Example:
+ * 0x7F - Start Packet
+ * 0xFE - Device ID
+ * 0x01 - Source ID
+ * 0x05 - Command type
+ * 0x00 - Payload length 1st byte
+ * 0x01 - Payload length 2nd byte
+ * 0x04 - Payload or data
+ * 0x76 - checksum
+ *
+ *
+ * PreCondition: None
+ *
+ * Input: 'packet' - instance of Packet data structure that contains the
+ * following information:
+ * + device id
+ * + source id
+ * + command
+ * + packet or payload length
+ * + packet or payload
+ *
+ * 'packetBuffer' - empty buffer that will be the holder of the process data or packet.
+ *
+ * Output: Total size of data inserted in the buffer.
+ *
+ * Side Effects: None
+ *
+ *****************************************************************************/
+int getFormattedPacket(Packet * packet, unsigned char * packetBuffer)
+{
+
+ checkSum = 0;
+ packetSize = 0;
+
+ insertValueToBuffer(STARTPACKET);
+ checkSum += STARTPACKET;
+
+ #ifdef DEBUG
+ printf("Start Packet: %x\n", STARTPACKET);
+ #endif
+
+ processPacketData(packet->deviceID);
+ checkSum += packet->deviceID;
+
+ #ifdef DEBUG
+ printf("Device ID: %x\n", packet->deviceID);
+ #endif
+
+ processPacketData(packet->sourceID);
+ checkSum += packet->sourceID;
+
+ #ifdef DEBUG
+ printf("Source ID: %x\n", packet->sourceID);
+ #endif
+
+ processPacketData(packet->command);
+ checkSum += packet->command;
+
+ #ifdef DEBUG
+ printf("Command: %x\n", packet->command);
+ #endif
+
+ unsigned char mostSignificantBytePacketLength = packet->packetLength >> 8;
+ unsigned char leastSignificantBytePacketLength = (unsigned char) packet->packetLength;
+ processPacketData(mostSignificantBytePacketLength);
+ checkSum += mostSignificantBytePacketLength;
+ processPacketData(leastSignificantBytePacketLength);
+ checkSum += leastSignificantBytePacketLength;
+
+ #ifdef DEBUG
+ printf("Packet length MSB: %x\n", mostSignificantBytePacketLength);
+ printf("Packet length LSB: %x\n", leastSignificantBytePacketLength);
+ #endif
+
+ int i;
+ for(i = 0; i < packet->packetLength; i++)
+ {
+ processPacketData(packet->packetData[i]);
+ checkSum += packet->packetData[i];
+
+ #ifdef DEBUG
+ printf("Data %d: %x\n", i, packet->packetData[i]);
+ #endif
+ }
+
+ unsigned char checksumLSB = (unsigned char)(checkSum & 0xFF);
+ unsigned char twosComplementCheckSum = ~checksumLSB + 1;
+ processPacketData(twosComplementCheckSum);
+
+ #ifdef DEBUG
+ printf("Checksum: %x\n", twosComplementCheckSum);
+ #endif
+
+ memcpy(packetBuffer, buffer, packetSize);
+
+ return packetSize;
+}
+
+/******************************************************************************
+ * Function void getFormattedBootloaderPacket(unsigned char *, unsigned char *, int)
+ *
+ * This function is called to process the data in to a series of bytes that
+ * conforms into the bootloader protocol.
+ * Protocol Format Example:
+ * 0x55 - STX or Start of TeXt
+ * 0x55 - STX or Start of TeXt
+ * 0x01 - Data
+ * 0x03 - Data
+ * 0x0C - Checksum
+ * 0x04 - ETX or End of TeXt
+ *
+ *
+ * PreCondition: None
+ *
+ * Input:
+ * 'buffer' - empty buffer that will be the holder of the process.
+ * 'data' - values to be send in bootloader.
+ * 'size' - size of data to be send.
+ *
+ * Output: Total size of data inserted in the buffer.
+ *
+ * Side Effects: None
+ *
+ *****************************************************************************/
+int
+getFormattedBootloaderPacket(unsigned char *buffer, unsigned char *data, int size)
+{
+ int i;
+ int j;
+ int sum;
+
+ i = 0;
+ buffer[i++] = STX;
+ buffer[i++] = STX;
+ sum = 0;
+
+ for(j = 0; j < size; j++)
+ {
+ if(data[j] == ETX || data[j] == STX || data[j] == ENQ)
+ {
+ buffer[j+i++] = ENQ;
+ }
+
+ buffer[j+i] = data[j];
+ sum += data[j];
+ }
+
+ sum = ((sum * -1) & 0xFF);
+ if (sum == ETX || sum == STX || sum == ENQ)
+ buffer[j+i++] = ENQ;
+
+ buffer[j+i] = sum;
+ j++;
+ buffer[j+i] = ETX;
+ j++;
+
+ //printf("getFormattedBootloaderPacket return = %d\n",j+i);
+
+ return (j+i);
+}
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/packetformatter.h Mon Dec 12 23:06:58 2016 +0000
@@ -0,0 +1,19 @@
+#ifndef PACKETFORMATTER
+#define PACKETFORMATTER
+
+#pragma pack(1)
+typedef struct
+{
+ unsigned char deviceID;
+ unsigned char sourceID;
+ unsigned char command;
+ unsigned short packetLength;
+ unsigned char packetData[1024];
+
+} Packet;
+#pragma pack(0)
+
+extern int getFormattedPacket(Packet *packet, unsigned char * packetBuffer);
+extern int ConvertToPacket( Packet *_packet , unsigned char *_buffer );
+extern int getArraySize(unsigned char* sampleData);
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/protocol.h Mon Dec 12 23:06:58 2016 +0000 @@ -0,0 +1,22 @@ +#ifndef PROTOCOL_H +#define PROTOCOL_H + + +#define START 0x00 +#define DEVICEADDRESS 0x01 +#define SOURCEADD 0x02 +#define POLL 0x03 +#define COLLECTCOMMAND 0x04 +#define COLLECTCOUNT1 0x05 +#define COLLECTCOUNT2 0x06 +#define COLLECTPACKET 0x07 +#define COLLECTCHECKSUM 0x08 +#define HANDLEESCAPE 0x20 +#define STARTPACK 0x7F +#define STARTPOLL 0x8F +#define ESCAPE 0x8E +#define ACKCHAR 0x8D +#define NAKCHAR 0x8C +#define RESPONSE 0x8E //#SCD #RESPONSE + +#endif // PROTOCOL_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/rs485.cpp Mon Dec 12 23:06:58 2016 +0000
@@ -0,0 +1,404 @@
+#include <string.h>
+#include <stdio.h>
+
+#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];
+int checksum;
+int pState = START;
+int returnState;
+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;
+
+extern int Bluetooth_ReceivePacket( Packet *_packet );
+extern int Bluetooth_SendChar( unsigned char );
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+unsigned char RegisterCommand( unsigned char _command , void (*_function)( unsigned char *) ){
+ functionList.list[ functionList.index ].function = _function;
+ functionList.list[ functionList.index ].command = _command;
+ functionList.index++;
+ return 0;
+}
+
+int CheckFunction( Packet *_packet ){
+ int i = 0;
+ void (*Caller)(unsigned char *);
+ for( i = 0 ; i < functionList.index ; i++ ){
+ if( _packet->command == functionList.list[i].command ){
+ Caller = (void (*)(unsigned char *))functionList.list[i].function;
+ Caller( (unsigned char *)_packet->packetData );
+ return 1;
+ }
+ }
+ return 0;
+}
+
+void RS495_Init( void ){
+
+}
+
+static void SendAck( void ){
+ #ifdef DEBUG
+ pc.printf( "\n\rAck Sent.\n\r" );
+ #endif // DEBUG
+ unsigned char ack = ACKCHAR;
+ Bluetooth_SendChar( ack );
+}
+
+static void SendNack( void ){
+ #ifdef DEBUG
+ pc.printf( "\n\rNack Sent.\n\r" );
+ #endif // DEBUG
+ unsigned char nack = NAKCHAR;
+ Bluetooth_SendChar( nack );
+}
+
+static int ProcessPacket( Packet *_packet ){
+ return Bluetooth_ReceivePacket( _packet );
+}
+
+void SendMessage( void ){
+ int j = 0;
+ Bluetooth_SendChar( packetBuffer[ j++ ] );
+ while( j < packetBufferSize ){
+ if( packetBuffer[ j ] == STARTPACK ){
+ Bluetooth_SendChar( ESCAPE ); // #SCD Added this line, I think it should be here
+ Bluetooth_SendChar( 0x01 );
+ j++;
+ }
+ else if( packetBuffer[ j ] == ESCAPE ){
+ Bluetooth_SendChar( ESCAPE );
+ Bluetooth_SendChar( ESCAPE );
+ j++;
+ }
+ else if( packetBuffer[ j ] == STARTPOLL ){
+ Bluetooth_SendChar( ESCAPE );
+ Bluetooth_SendChar( 0x02 );
+ j++;
+ }
+ else{
+ Bluetooth_SendChar( packetBuffer[ j++ ] );
+ }
+ }
+}
+
+/******************************************************************************
+ * 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 SetResponse( Packet *_packet ){
+ packetBufferSize = getFormattedPacket(&_packet, packetBuffer);
+ message = 1;
+}
+
+/******************************************************************************
+ * Function void SerialHandler(unsigned char )
+ *
+ * This function handles the received data from the Serial Communication.
+ *
+ * PreCondition: None
+ *
+ * Input: 'RXByte' - the received data from Serial Communication
+ *
+ *
+ * Output: None
+ *
+ * Side Effects: None
+ *
+ *****************************************************************************/
+void SerialHandler(unsigned char RXByte){
+
+ #ifdef DEBUG
+ pc.printf( "RXByte: %x" , RXByte );
+ #endif
+
+
+ // check the incoming byte for special characters
+ if( RXByte == STARTPACK ){ // start of packet byte
+ #ifdef DEBUG
+ pc.printf( " - STARTBYTE\n\r" );
+ #endif
+ pState = DEVICEADDRESS; // move on to check the destination address
+ buffPtr = 0; // reset buffer pointer
+ buffer[buffPtr++] = STARTPACK; // load RXByte into buffer
+ checksum = STARTPACK; // add to checksum
+ return; // exit function, will be called again in next state
+ }
+ else if( RXByte == STARTPOLL ){ // poll byte
+ #ifdef DEBUG
+ pc.printf( " - STARTPOLL\n\r" );
+ #endif
+ pState = POLL; // move on to check the distination address
+ return; // exit function, will be called again in next state
+ }
+ else if( RXByte == ESCAPE ){ // escape byte
+ if( pState == HANDLEESCAPE ){ // if this is the second escape byte in a row
+ }
+ else{
+ #ifdef DEBUG
+ pc.printf( " - ESCAPE TO " );
+ #endif
+ returnState = pState; // if this is the first escape byte, record the current state
+ pState = HANDLEESCAPE; // change state
+ return; // exit function, will be called again in next state
+ }
+ }
+
+ if( pState == HANDLEESCAPE ){ // if entering here RXByte needs to be unescaped
+ switch( RXByte ){
+ case 1: // unescape a 0x7F/Start of packet byte
+ RXByte = 0x7F;
+ #ifdef DEBUG
+ pc.printf( " -> %x" , RXByte );
+ #endif
+ break;
+
+ case 2: // unescape a 0x8F/Poll byte
+ RXByte = 0x8F;
+ #ifdef DEBUG
+ pc.printf( " -> %x" , RXByte );
+ #endif
+ break;
+
+ case ESCAPE: // unescape a 0x8E/Escape byte
+ RXByte = 0x8E;
+ #ifdef DEBUG
+ pc.printf( " -> %x" , RXByte );
+ #endif
+ break;
+ }
+ pState = returnState; // change back to state before escape byte received
+ }
+
+ switch( pState ){
+
+ case POLL: // of switch( pState ) // poll state checks RXByte to see if there is an address match
+
+
+ if( RXByte == DEVADDRESS ){ // if the device is being polled...
+ #ifdef DEBUG
+ pc.printf( " - Address Match: %x" , DEVADDRESS );
+ #endif
+
+ if( message ){ // if a message is available send it
+ #ifdef DEBUG
+ pc.printf( " - Message to Send\n\r" );
+ #endif
+ SendMessage(); // #SCD haven't tested this
+ pState = RESPONSE; // change state to wait for ACK or NACK
+ }
+ else{ // device is pulled but no message to be sent
+ #ifdef DEBUG
+ pc.printf( " - No message to Send\n\r" );
+ #endif
+ SendAck(); // just send an ACK
+ pState = START;
+ }
+ }
+ else{
+ pState = START; // device not addressed, do nothing
+ }
+ break;
+
+ case START: // of switch( pState ) // this state really does nothing and just waits for
+ pState = START; // if the device is not addressed in a packet
+ break;
+
+ case RESPONSE: // of switch( pState ) // a message was sent it last state and now this state is
+
+ switch( RXByte ){
+
+ case ACKCHAR: // ACK is received - message successful, clear message to be sent
+ #ifdef DEBUG
+ pc.printf( " - ACK RECEIVED\n\r" );
+ #endif
+ message = 0;
+ pState = START;
+ break;
+
+ case NAKCHAR: // NACK is received - message not successful
+ #ifdef DEBUG
+ pc.printf( " - NACK RECEIVED\n\r" );
+ #endif
+ if( message ){ // if a message still needs to be sent, send again
+ SendMessage();
+ message = 0; // clear message after this seconda attempt to prevent too many tries
+ pState = RESPONSE; // set state to come by here next time around
+ }
+ else{ // if a NACK is received for the second time in a row
+ //#error #SCD // throw an error and do not resend
+ pc.printf( "Received two NACKs from master. Message failed.\n\r" );
+ }
+ break;
+
+ default:
+ #ifdef DEBUG
+ pc.printf( " - EXPECTED ACK or NACK\n\r" );
+ #endif
+ pState = START; // if neither an ACK nor NACK is received, reset state
+ break;
+
+ }
+ break;
+
+ case DEVICEADDRESS: // of switch( pState ) // checks to see if device is addressed in current packet
+ #ifdef DEBUG
+ pc.printf( " - DEVICEADDRESS\n\r" );
+ #endif
+
+ if( RXByte == DEVADDRESS ){
+ #ifdef PRINT_ADDRESS_MATCH
+ pc.printf( "Address Match: %d\n\r" , DEVADDRESS );
+ #endif
+
+ pState = SOURCEADD; // next state is grabbing the source device ID
+ buffer[buffPtr++] = RXByte; // add RXByte to buffer
+ buildPacket.deviceID = RXByte; // build packet
+ checksum += RXByte; // add RXByte to checksum
+ }
+ else{
+ pState = START; // if device is not addressed reset state
+ }
+
+ break;
+
+ case SOURCEADD: // of switch( pState ) // records the source address of the packet
+ #ifdef DEBUG
+ pc.printf( " - SOURCEADD\n\r" );
+ #endif
+
+ pState = COLLECTCOMMAND; // state advances to get the command byte
+ buffer[buffPtr++] = RXByte; // add RXByte to buffer
+ buildPacket.sourceID = RXByte; // build packet
+ checksum += RXByte; // add RXByte to checksum
+ break;
+
+ case COLLECTCOMMAND: // of switch( pState )// records the command byte of the packet
+ #ifdef DEBUG
+ pc.printf( " - COMMAND\n\r" );
+ #endif
+
+ command = RXByte; // record command byte
+ buildPacket.command = RXByte; // build packet
+ pState = COLLECTCOUNT1; // advance to get packetLength
+ buffer[buffPtr++] = RXByte; // add RXByte to buffer
+ checksum += RXByte; // add RXByte to checksum
+ break;
+
+ case COLLECTCOUNT1: // of switch( pState ) // records first byte of packetLength
+ #ifdef DEBUG
+ pc.printf( " - COUNT MSB \n\r" );
+ #endif
+
+ pState = COLLECTCOUNT2; // advance state to get next byte of packetLength
+ plength = RXByte;
+ plength <<= 8; // store byte in packetLength #SCD is this storing correctly?
+ buffer[buffPtr++] = RXByte; // add RXByte to buffer
+ checksum += RXByte; // add RXByte to checksum
+ break;
+
+ case COLLECTCOUNT2: // of switch( pState ) // records second byte of packetLength
+ #ifdef DEBUG
+ pc.printf( " - COUNT LSB\n\r" );
+ #endif
+
+ plength += RXByte; // add RXByte to packetLength total
+ buildPacket.packetLength = plength; // build packet
+ buffer[buffPtr++] = RXByte; // add RXByte to buffer
+ checksum += RXByte; // add RXByte to checksum
+ if( plength == 0 ) // if packetLength is 0, advance to checksum state
+ pState = COLLECTCHECKSUM;
+ else // otherwise move on to collect packet payload
+ pState = COLLECTPACKET;
+ break;
+
+ case COLLECTPACKET: // of switch( pState ) // collects packetData, enters this state multiple times
+ #ifdef DEBUG
+ pc.printf( " - PACKETDATA \n\r" );
+ #endif
+
+ plength--; // decrement length
+ buffer[buffPtr++] = RXByte; // add RXByte to buffer
+ checksum += RXByte; // add RXByte to checksum
+
+ if( plength == 0 ){ // if collected all packetData advance state
+ pState = COLLECTCHECKSUM;
+ }
+ break;
+
+ case COLLECTCHECKSUM:// of switch( pState ) // collects checksum of packet and checks for validity
+ #ifdef DEBUG
+ pc.printf( " - CHECKSUM\n\r" );
+ #endif
+ memcpy( &buildPacket.packetData , &buffer[6] , buildPacket.packetLength );
+
+ if( RXByte == ( ( checksum* - 1 ) & 0xFF ) ){ // compares RXByte with LSB of checksum
+ message = 0; // if checksum is correct clear message #SCD could a message be waiting?
+ memset(packetBuffer, '\0', 128); // clear packetBuffer
+
+ buffer[buffPtr++] = RXByte; // add RXByte to buffer
+
+ #ifdef PRINTPACKETDEBUG
+ pc.printf( "Receive Buffer: " ); // debug print received packet
+ unsigned int i = 0;
+ for( i = 0 ; i < buffPtr ; i++ ){
+ pc.printf( "%x " , buffer[ i ] );
+ }
+ pc.printf( "\n\r" );
+ #endif
+
+ int errorResult = ProcessPacket( &buildPacket );
+
+ if( errorResult == -1 ){ // check to make sure function performed properly
+ //#SCD setResponse(RPIADDRESS, command, ERRORCOMMAND);
+ }
+ SendAck(); // send an ACK
+ }
+ else{
+ SendNack(); // if checksum is not corret, send a NACK
+ }
+
+ pState = START;
+ break;
+
+ } // end switch( pState )
+
+} // end serial2Handler function
+
+
+
+#ifdef __cplusplus
+}
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/rs485.h Mon Dec 12 23:06:58 2016 +0000
@@ -0,0 +1,29 @@
+#ifndef RS485
+#define RS485
+
+#include "packetformatter.h"
+#include "protocol.h"
+#include "list.h"
+#include "comms.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void SerialHandler(unsigned char RXByte);
+int ChangeState( int );
+
+
+
+//extern char processCommand(int command,unsigned char *receivedData);
+
+void RS485_Init();
+unsigned char RegisterCommand( unsigned char , void (*fx)(unsigned char *) );
+int CheckFunction( Packet *_packet );
+void SetResponse( Packet *);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/uart1.cpp Mon Dec 12 23:06:58 2016 +0000
@@ -0,0 +1,69 @@
+#include "uart1.h"
+#include "mbed.h"
+
+#define BUFFERSIZE 128
+extern Serial bus;
+DigitalOut led_2(LED2);
+
+static unsigned char U1buf[BUFFERSIZE];
+static unsigned int U1ptr;
+static unsigned int U1gptr;
+static unsigned int U1cnt;
+
+
+static void
+serial_event()
+{
+ led_2 = !led_2;
+
+ if(bus.readable())
+ {
+ U1buf[U1ptr++] = (unsigned char) USART1->RDR & 0xFF;//(unsigned char)bus.getc();
+ U1ptr &= BUFFERSIZE-1;
+ if(U1ptr == U1gptr)
+ {
+ U1gptr++;
+ U1gptr &= BUFFERSIZE-1;
+ }
+ else
+ {
+ U1cnt++;
+ }
+ }
+}
+
+ void
+init_uart1(void)
+{
+ bus.baud(115200);
+ bus.attach(&serial_event,Serial::RxIrq);
+
+ U1ptr = U1gptr = U1cnt = 0;
+}
+
+int
+uart1_is_char(void)
+{
+ return U1cnt;
+}
+
+int
+uart1_get_char(void)
+{
+ int result;
+ result = 0;
+ if(U1cnt)
+ {
+ result = U1buf[U1gptr++];
+ U1gptr &= BUFFERSIZE-1;
+ U1cnt--;
+ }
+
+ return result;
+}
+
+void
+send_uart1_char(unsigned char c)
+{
+ bus.putc(c);
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/uart1.h Mon Dec 12 23:06:58 2016 +0000
@@ -0,0 +1,19 @@
+#ifndef UART1_
+#define UART1_
+
+#include "comms.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void init_uart1(void);
+int uart1_is_char(void);
+int uart1_get_char(void);
+void send_uart1_char(unsigned char c);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
\ No newline at end of file