Stephen Davis
/
BT_Hub
rs485.cpp@1:d6b18299a715, 2016-12-12 (annotated)
- Committer:
- Stephen_NewVistas
- Date:
- Mon Dec 12 23:19:30 2016 +0000
- Revision:
- 1:d6b18299a715
- Parent:
- 0:226550611f0d
- Child:
- 2:9ab591cf81b8
about to branch and make generic and not cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Stephen_NewVistas | 0:226550611f0d | 1 | #include <string.h> |
Stephen_NewVistas | 0:226550611f0d | 2 | #include <stdio.h> |
Stephen_NewVistas | 0:226550611f0d | 3 | |
Stephen_NewVistas | 0:226550611f0d | 4 | #include "rs485.h" |
Stephen_NewVistas | 0:226550611f0d | 5 | |
Stephen_NewVistas | 0:226550611f0d | 6 | |
Stephen_NewVistas | 0:226550611f0d | 7 | #define PRINTPACKETDEBUG |
Stephen_NewVistas | 0:226550611f0d | 8 | #define DEVADDRESS 0xF0 |
Stephen_NewVistas | 0:226550611f0d | 9 | #define DEBUG |
Stephen_NewVistas | 0:226550611f0d | 10 | |
Stephen_NewVistas | 0:226550611f0d | 11 | static list_t functionList = { .index = 0 }; |
Stephen_NewVistas | 0:226550611f0d | 12 | |
Stephen_NewVistas | 0:226550611f0d | 13 | |
Stephen_NewVistas | 0:226550611f0d | 14 | Packet buildPacket; |
Stephen_NewVistas | 0:226550611f0d | 15 | static unsigned int buffPtr; |
Stephen_NewVistas | 0:226550611f0d | 16 | static unsigned char buffer[128+1]; |
Stephen_NewVistas | 0:226550611f0d | 17 | int checksum; |
Stephen_NewVistas | 0:226550611f0d | 18 | int pState = START; |
Stephen_NewVistas | 0:226550611f0d | 19 | int returnState; |
Stephen_NewVistas | 0:226550611f0d | 20 | unsigned int plength; |
Stephen_NewVistas | 0:226550611f0d | 21 | unsigned char command; |
Stephen_NewVistas | 0:226550611f0d | 22 | int message; |
Stephen_NewVistas | 0:226550611f0d | 23 | // extern int message; |
Stephen_NewVistas | 0:226550611f0d | 24 | |
Stephen_NewVistas | 0:226550611f0d | 25 | // extern unsigned char packetBuffer[128]; |
Stephen_NewVistas | 0:226550611f0d | 26 | // extern unsigned int packetBufferSize; |
Stephen_NewVistas | 0:226550611f0d | 27 | static unsigned char packetBuffer[128]; |
Stephen_NewVistas | 0:226550611f0d | 28 | static unsigned int packetBufferSize; |
Stephen_NewVistas | 0:226550611f0d | 29 | |
Stephen_NewVistas | 0:226550611f0d | 30 | extern int Bluetooth_ReceivePacket( Packet *_packet ); |
Stephen_NewVistas | 0:226550611f0d | 31 | extern int Bluetooth_SendChar( unsigned char ); |
Stephen_NewVistas | 0:226550611f0d | 32 | |
Stephen_NewVistas | 0:226550611f0d | 33 | #ifdef __cplusplus |
Stephen_NewVistas | 0:226550611f0d | 34 | extern "C" { |
Stephen_NewVistas | 0:226550611f0d | 35 | #endif |
Stephen_NewVistas | 0:226550611f0d | 36 | |
Stephen_NewVistas | 0:226550611f0d | 37 | |
Stephen_NewVistas | 0:226550611f0d | 38 | unsigned char RegisterCommand( unsigned char _command , void (*_function)( unsigned char *) ){ |
Stephen_NewVistas | 0:226550611f0d | 39 | functionList.list[ functionList.index ].function = _function; |
Stephen_NewVistas | 0:226550611f0d | 40 | functionList.list[ functionList.index ].command = _command; |
Stephen_NewVistas | 0:226550611f0d | 41 | functionList.index++; |
Stephen_NewVistas | 0:226550611f0d | 42 | return 0; |
Stephen_NewVistas | 0:226550611f0d | 43 | } |
Stephen_NewVistas | 0:226550611f0d | 44 | |
Stephen_NewVistas | 0:226550611f0d | 45 | int CheckFunction( Packet *_packet ){ |
Stephen_NewVistas | 0:226550611f0d | 46 | int i = 0; |
Stephen_NewVistas | 0:226550611f0d | 47 | void (*Caller)(unsigned char *); |
Stephen_NewVistas | 0:226550611f0d | 48 | for( i = 0 ; i < functionList.index ; i++ ){ |
Stephen_NewVistas | 0:226550611f0d | 49 | if( _packet->command == functionList.list[i].command ){ |
Stephen_NewVistas | 0:226550611f0d | 50 | Caller = (void (*)(unsigned char *))functionList.list[i].function; |
Stephen_NewVistas | 0:226550611f0d | 51 | Caller( (unsigned char *)_packet->packetData ); |
Stephen_NewVistas | 0:226550611f0d | 52 | return 1; |
Stephen_NewVistas | 0:226550611f0d | 53 | } |
Stephen_NewVistas | 0:226550611f0d | 54 | } |
Stephen_NewVistas | 0:226550611f0d | 55 | return 0; |
Stephen_NewVistas | 0:226550611f0d | 56 | } |
Stephen_NewVistas | 0:226550611f0d | 57 | |
Stephen_NewVistas | 0:226550611f0d | 58 | void RS495_Init( void ){ |
Stephen_NewVistas | 0:226550611f0d | 59 | |
Stephen_NewVistas | 0:226550611f0d | 60 | } |
Stephen_NewVistas | 0:226550611f0d | 61 | |
Stephen_NewVistas | 0:226550611f0d | 62 | static void SendAck( void ){ |
Stephen_NewVistas | 0:226550611f0d | 63 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 64 | pc.printf( "\n\rAck Sent.\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 65 | #endif // DEBUG |
Stephen_NewVistas | 0:226550611f0d | 66 | unsigned char ack = ACKCHAR; |
Stephen_NewVistas | 0:226550611f0d | 67 | Bluetooth_SendChar( ack ); |
Stephen_NewVistas | 0:226550611f0d | 68 | } |
Stephen_NewVistas | 0:226550611f0d | 69 | |
Stephen_NewVistas | 0:226550611f0d | 70 | static void SendNack( void ){ |
Stephen_NewVistas | 0:226550611f0d | 71 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 72 | pc.printf( "\n\rNack Sent.\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 73 | #endif // DEBUG |
Stephen_NewVistas | 0:226550611f0d | 74 | unsigned char nack = NAKCHAR; |
Stephen_NewVistas | 0:226550611f0d | 75 | Bluetooth_SendChar( nack ); |
Stephen_NewVistas | 0:226550611f0d | 76 | } |
Stephen_NewVistas | 0:226550611f0d | 77 | |
Stephen_NewVistas | 0:226550611f0d | 78 | static int ProcessPacket( Packet *_packet ){ |
Stephen_NewVistas | 0:226550611f0d | 79 | return Bluetooth_ReceivePacket( _packet ); |
Stephen_NewVistas | 0:226550611f0d | 80 | } |
Stephen_NewVistas | 0:226550611f0d | 81 | |
Stephen_NewVistas | 0:226550611f0d | 82 | void SendMessage( void ){ |
Stephen_NewVistas | 0:226550611f0d | 83 | int j = 0; |
Stephen_NewVistas | 0:226550611f0d | 84 | Bluetooth_SendChar( packetBuffer[ j++ ] ); |
Stephen_NewVistas | 0:226550611f0d | 85 | while( j < packetBufferSize ){ |
Stephen_NewVistas | 0:226550611f0d | 86 | if( packetBuffer[ j ] == STARTPACK ){ |
Stephen_NewVistas | 0:226550611f0d | 87 | Bluetooth_SendChar( ESCAPE ); // #SCD Added this line, I think it should be here |
Stephen_NewVistas | 0:226550611f0d | 88 | Bluetooth_SendChar( 0x01 ); |
Stephen_NewVistas | 0:226550611f0d | 89 | j++; |
Stephen_NewVistas | 0:226550611f0d | 90 | } |
Stephen_NewVistas | 0:226550611f0d | 91 | else if( packetBuffer[ j ] == ESCAPE ){ |
Stephen_NewVistas | 0:226550611f0d | 92 | Bluetooth_SendChar( ESCAPE ); |
Stephen_NewVistas | 0:226550611f0d | 93 | Bluetooth_SendChar( ESCAPE ); |
Stephen_NewVistas | 0:226550611f0d | 94 | j++; |
Stephen_NewVistas | 0:226550611f0d | 95 | } |
Stephen_NewVistas | 0:226550611f0d | 96 | else if( packetBuffer[ j ] == STARTPOLL ){ |
Stephen_NewVistas | 0:226550611f0d | 97 | Bluetooth_SendChar( ESCAPE ); |
Stephen_NewVistas | 0:226550611f0d | 98 | Bluetooth_SendChar( 0x02 ); |
Stephen_NewVistas | 0:226550611f0d | 99 | j++; |
Stephen_NewVistas | 0:226550611f0d | 100 | } |
Stephen_NewVistas | 0:226550611f0d | 101 | else{ |
Stephen_NewVistas | 0:226550611f0d | 102 | Bluetooth_SendChar( packetBuffer[ j++ ] ); |
Stephen_NewVistas | 0:226550611f0d | 103 | } |
Stephen_NewVistas | 0:226550611f0d | 104 | } |
Stephen_NewVistas | 0:226550611f0d | 105 | } |
Stephen_NewVistas | 0:226550611f0d | 106 | |
Stephen_NewVistas | 0:226550611f0d | 107 | /****************************************************************************** |
Stephen_NewVistas | 0:226550611f0d | 108 | * Function void setResponse(int, int, int) |
Stephen_NewVistas | 0:226550611f0d | 109 | * |
Stephen_NewVistas | 0:226550611f0d | 110 | * This function sets the feedback information to be send in the master device. |
Stephen_NewVistas | 0:226550611f0d | 111 | * |
Stephen_NewVistas | 0:226550611f0d | 112 | * PreCondition: None |
Stephen_NewVistas | 0:226550611f0d | 113 | * |
Stephen_NewVistas | 0:226550611f0d | 114 | * Input: '_packet' - prebuilt packet to send |
Stephen_NewVistas | 0:226550611f0d | 115 | * |
Stephen_NewVistas | 0:226550611f0d | 116 | * Output: None |
Stephen_NewVistas | 0:226550611f0d | 117 | * |
Stephen_NewVistas | 0:226550611f0d | 118 | * Side Effects: None |
Stephen_NewVistas | 0:226550611f0d | 119 | * |
Stephen_NewVistas | 0:226550611f0d | 120 | *****************************************************************************/ |
Stephen_NewVistas | 0:226550611f0d | 121 | void SetResponse( Packet *_packet ){ |
Stephen_NewVistas | 1:d6b18299a715 | 122 | packetBufferSize = getFormattedPacket(_packet, packetBuffer); |
Stephen_NewVistas | 0:226550611f0d | 123 | message = 1; |
Stephen_NewVistas | 0:226550611f0d | 124 | } |
Stephen_NewVistas | 0:226550611f0d | 125 | |
Stephen_NewVistas | 0:226550611f0d | 126 | /****************************************************************************** |
Stephen_NewVistas | 0:226550611f0d | 127 | * Function void SerialHandler(unsigned char ) |
Stephen_NewVistas | 0:226550611f0d | 128 | * |
Stephen_NewVistas | 0:226550611f0d | 129 | * This function handles the received data from the Serial Communication. |
Stephen_NewVistas | 0:226550611f0d | 130 | * |
Stephen_NewVistas | 0:226550611f0d | 131 | * PreCondition: None |
Stephen_NewVistas | 0:226550611f0d | 132 | * |
Stephen_NewVistas | 0:226550611f0d | 133 | * Input: 'RXByte' - the received data from Serial Communication |
Stephen_NewVistas | 0:226550611f0d | 134 | * |
Stephen_NewVistas | 0:226550611f0d | 135 | * |
Stephen_NewVistas | 0:226550611f0d | 136 | * Output: None |
Stephen_NewVistas | 0:226550611f0d | 137 | * |
Stephen_NewVistas | 0:226550611f0d | 138 | * Side Effects: None |
Stephen_NewVistas | 0:226550611f0d | 139 | * |
Stephen_NewVistas | 0:226550611f0d | 140 | *****************************************************************************/ |
Stephen_NewVistas | 0:226550611f0d | 141 | void SerialHandler(unsigned char RXByte){ |
Stephen_NewVistas | 0:226550611f0d | 142 | |
Stephen_NewVistas | 0:226550611f0d | 143 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 144 | pc.printf( "RXByte: %x" , RXByte ); |
Stephen_NewVistas | 0:226550611f0d | 145 | #endif |
Stephen_NewVistas | 0:226550611f0d | 146 | |
Stephen_NewVistas | 0:226550611f0d | 147 | |
Stephen_NewVistas | 0:226550611f0d | 148 | // check the incoming byte for special characters |
Stephen_NewVistas | 0:226550611f0d | 149 | if( RXByte == STARTPACK ){ // start of packet byte |
Stephen_NewVistas | 0:226550611f0d | 150 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 151 | pc.printf( " - STARTBYTE\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 152 | #endif |
Stephen_NewVistas | 0:226550611f0d | 153 | pState = DEVICEADDRESS; // move on to check the destination address |
Stephen_NewVistas | 0:226550611f0d | 154 | buffPtr = 0; // reset buffer pointer |
Stephen_NewVistas | 0:226550611f0d | 155 | buffer[buffPtr++] = STARTPACK; // load RXByte into buffer |
Stephen_NewVistas | 0:226550611f0d | 156 | checksum = STARTPACK; // add to checksum |
Stephen_NewVistas | 0:226550611f0d | 157 | return; // exit function, will be called again in next state |
Stephen_NewVistas | 0:226550611f0d | 158 | } |
Stephen_NewVistas | 0:226550611f0d | 159 | else if( RXByte == STARTPOLL ){ // poll byte |
Stephen_NewVistas | 0:226550611f0d | 160 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 161 | pc.printf( " - STARTPOLL\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 162 | #endif |
Stephen_NewVistas | 0:226550611f0d | 163 | pState = POLL; // move on to check the distination address |
Stephen_NewVistas | 0:226550611f0d | 164 | return; // exit function, will be called again in next state |
Stephen_NewVistas | 0:226550611f0d | 165 | } |
Stephen_NewVistas | 0:226550611f0d | 166 | else if( RXByte == ESCAPE ){ // escape byte |
Stephen_NewVistas | 0:226550611f0d | 167 | if( pState == HANDLEESCAPE ){ // if this is the second escape byte in a row |
Stephen_NewVistas | 0:226550611f0d | 168 | } |
Stephen_NewVistas | 0:226550611f0d | 169 | else{ |
Stephen_NewVistas | 0:226550611f0d | 170 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 171 | pc.printf( " - ESCAPE TO " ); |
Stephen_NewVistas | 0:226550611f0d | 172 | #endif |
Stephen_NewVistas | 0:226550611f0d | 173 | returnState = pState; // if this is the first escape byte, record the current state |
Stephen_NewVistas | 0:226550611f0d | 174 | pState = HANDLEESCAPE; // change state |
Stephen_NewVistas | 0:226550611f0d | 175 | return; // exit function, will be called again in next state |
Stephen_NewVistas | 0:226550611f0d | 176 | } |
Stephen_NewVistas | 0:226550611f0d | 177 | } |
Stephen_NewVistas | 0:226550611f0d | 178 | |
Stephen_NewVistas | 0:226550611f0d | 179 | if( pState == HANDLEESCAPE ){ // if entering here RXByte needs to be unescaped |
Stephen_NewVistas | 0:226550611f0d | 180 | switch( RXByte ){ |
Stephen_NewVistas | 0:226550611f0d | 181 | case 1: // unescape a 0x7F/Start of packet byte |
Stephen_NewVistas | 0:226550611f0d | 182 | RXByte = 0x7F; |
Stephen_NewVistas | 0:226550611f0d | 183 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 184 | pc.printf( " -> %x" , RXByte ); |
Stephen_NewVistas | 0:226550611f0d | 185 | #endif |
Stephen_NewVistas | 0:226550611f0d | 186 | break; |
Stephen_NewVistas | 0:226550611f0d | 187 | |
Stephen_NewVistas | 0:226550611f0d | 188 | case 2: // unescape a 0x8F/Poll byte |
Stephen_NewVistas | 0:226550611f0d | 189 | RXByte = 0x8F; |
Stephen_NewVistas | 0:226550611f0d | 190 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 191 | pc.printf( " -> %x" , RXByte ); |
Stephen_NewVistas | 0:226550611f0d | 192 | #endif |
Stephen_NewVistas | 0:226550611f0d | 193 | break; |
Stephen_NewVistas | 0:226550611f0d | 194 | |
Stephen_NewVistas | 0:226550611f0d | 195 | case ESCAPE: // unescape a 0x8E/Escape byte |
Stephen_NewVistas | 0:226550611f0d | 196 | RXByte = 0x8E; |
Stephen_NewVistas | 0:226550611f0d | 197 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 198 | pc.printf( " -> %x" , RXByte ); |
Stephen_NewVistas | 0:226550611f0d | 199 | #endif |
Stephen_NewVistas | 0:226550611f0d | 200 | break; |
Stephen_NewVistas | 0:226550611f0d | 201 | } |
Stephen_NewVistas | 0:226550611f0d | 202 | pState = returnState; // change back to state before escape byte received |
Stephen_NewVistas | 0:226550611f0d | 203 | } |
Stephen_NewVistas | 0:226550611f0d | 204 | |
Stephen_NewVistas | 0:226550611f0d | 205 | switch( pState ){ |
Stephen_NewVistas | 0:226550611f0d | 206 | |
Stephen_NewVistas | 0:226550611f0d | 207 | case POLL: // of switch( pState ) // poll state checks RXByte to see if there is an address match |
Stephen_NewVistas | 0:226550611f0d | 208 | |
Stephen_NewVistas | 0:226550611f0d | 209 | |
Stephen_NewVistas | 0:226550611f0d | 210 | if( RXByte == DEVADDRESS ){ // if the device is being polled... |
Stephen_NewVistas | 0:226550611f0d | 211 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 212 | pc.printf( " - Address Match: %x" , DEVADDRESS ); |
Stephen_NewVistas | 0:226550611f0d | 213 | #endif |
Stephen_NewVistas | 0:226550611f0d | 214 | |
Stephen_NewVistas | 0:226550611f0d | 215 | if( message ){ // if a message is available send it |
Stephen_NewVistas | 0:226550611f0d | 216 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 217 | pc.printf( " - Message to Send\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 218 | #endif |
Stephen_NewVistas | 0:226550611f0d | 219 | SendMessage(); // #SCD haven't tested this |
Stephen_NewVistas | 0:226550611f0d | 220 | pState = RESPONSE; // change state to wait for ACK or NACK |
Stephen_NewVistas | 0:226550611f0d | 221 | } |
Stephen_NewVistas | 0:226550611f0d | 222 | else{ // device is pulled but no message to be sent |
Stephen_NewVistas | 0:226550611f0d | 223 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 224 | pc.printf( " - No message to Send\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 225 | #endif |
Stephen_NewVistas | 0:226550611f0d | 226 | SendAck(); // just send an ACK |
Stephen_NewVistas | 0:226550611f0d | 227 | pState = START; |
Stephen_NewVistas | 0:226550611f0d | 228 | } |
Stephen_NewVistas | 0:226550611f0d | 229 | } |
Stephen_NewVistas | 0:226550611f0d | 230 | else{ |
Stephen_NewVistas | 0:226550611f0d | 231 | pState = START; // device not addressed, do nothing |
Stephen_NewVistas | 0:226550611f0d | 232 | } |
Stephen_NewVistas | 0:226550611f0d | 233 | break; |
Stephen_NewVistas | 0:226550611f0d | 234 | |
Stephen_NewVistas | 0:226550611f0d | 235 | case START: // of switch( pState ) // this state really does nothing and just waits for |
Stephen_NewVistas | 0:226550611f0d | 236 | pState = START; // if the device is not addressed in a packet |
Stephen_NewVistas | 0:226550611f0d | 237 | break; |
Stephen_NewVistas | 0:226550611f0d | 238 | |
Stephen_NewVistas | 0:226550611f0d | 239 | case RESPONSE: // of switch( pState ) // a message was sent it last state and now this state is |
Stephen_NewVistas | 0:226550611f0d | 240 | |
Stephen_NewVistas | 0:226550611f0d | 241 | switch( RXByte ){ |
Stephen_NewVistas | 0:226550611f0d | 242 | |
Stephen_NewVistas | 0:226550611f0d | 243 | case ACKCHAR: // ACK is received - message successful, clear message to be sent |
Stephen_NewVistas | 0:226550611f0d | 244 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 245 | pc.printf( " - ACK RECEIVED\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 246 | #endif |
Stephen_NewVistas | 0:226550611f0d | 247 | message = 0; |
Stephen_NewVistas | 0:226550611f0d | 248 | pState = START; |
Stephen_NewVistas | 0:226550611f0d | 249 | break; |
Stephen_NewVistas | 0:226550611f0d | 250 | |
Stephen_NewVistas | 0:226550611f0d | 251 | case NAKCHAR: // NACK is received - message not successful |
Stephen_NewVistas | 0:226550611f0d | 252 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 253 | pc.printf( " - NACK RECEIVED\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 254 | #endif |
Stephen_NewVistas | 0:226550611f0d | 255 | if( message ){ // if a message still needs to be sent, send again |
Stephen_NewVistas | 0:226550611f0d | 256 | SendMessage(); |
Stephen_NewVistas | 0:226550611f0d | 257 | message = 0; // clear message after this seconda attempt to prevent too many tries |
Stephen_NewVistas | 0:226550611f0d | 258 | pState = RESPONSE; // set state to come by here next time around |
Stephen_NewVistas | 0:226550611f0d | 259 | } |
Stephen_NewVistas | 0:226550611f0d | 260 | else{ // if a NACK is received for the second time in a row |
Stephen_NewVistas | 0:226550611f0d | 261 | //#error #SCD // throw an error and do not resend |
Stephen_NewVistas | 0:226550611f0d | 262 | pc.printf( "Received two NACKs from master. Message failed.\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 263 | } |
Stephen_NewVistas | 0:226550611f0d | 264 | break; |
Stephen_NewVistas | 0:226550611f0d | 265 | |
Stephen_NewVistas | 0:226550611f0d | 266 | default: |
Stephen_NewVistas | 0:226550611f0d | 267 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 268 | pc.printf( " - EXPECTED ACK or NACK\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 269 | #endif |
Stephen_NewVistas | 0:226550611f0d | 270 | pState = START; // if neither an ACK nor NACK is received, reset state |
Stephen_NewVistas | 0:226550611f0d | 271 | break; |
Stephen_NewVistas | 0:226550611f0d | 272 | |
Stephen_NewVistas | 0:226550611f0d | 273 | } |
Stephen_NewVistas | 0:226550611f0d | 274 | break; |
Stephen_NewVistas | 0:226550611f0d | 275 | |
Stephen_NewVistas | 0:226550611f0d | 276 | case DEVICEADDRESS: // of switch( pState ) // checks to see if device is addressed in current packet |
Stephen_NewVistas | 0:226550611f0d | 277 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 278 | pc.printf( " - DEVICEADDRESS\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 279 | #endif |
Stephen_NewVistas | 0:226550611f0d | 280 | |
Stephen_NewVistas | 0:226550611f0d | 281 | if( RXByte == DEVADDRESS ){ |
Stephen_NewVistas | 0:226550611f0d | 282 | #ifdef PRINT_ADDRESS_MATCH |
Stephen_NewVistas | 0:226550611f0d | 283 | pc.printf( "Address Match: %d\n\r" , DEVADDRESS ); |
Stephen_NewVistas | 0:226550611f0d | 284 | #endif |
Stephen_NewVistas | 0:226550611f0d | 285 | |
Stephen_NewVistas | 0:226550611f0d | 286 | pState = SOURCEADD; // next state is grabbing the source device ID |
Stephen_NewVistas | 0:226550611f0d | 287 | buffer[buffPtr++] = RXByte; // add RXByte to buffer |
Stephen_NewVistas | 0:226550611f0d | 288 | buildPacket.deviceID = RXByte; // build packet |
Stephen_NewVistas | 0:226550611f0d | 289 | checksum += RXByte; // add RXByte to checksum |
Stephen_NewVistas | 0:226550611f0d | 290 | } |
Stephen_NewVistas | 0:226550611f0d | 291 | else{ |
Stephen_NewVistas | 0:226550611f0d | 292 | pState = START; // if device is not addressed reset state |
Stephen_NewVistas | 0:226550611f0d | 293 | } |
Stephen_NewVistas | 0:226550611f0d | 294 | |
Stephen_NewVistas | 0:226550611f0d | 295 | break; |
Stephen_NewVistas | 0:226550611f0d | 296 | |
Stephen_NewVistas | 0:226550611f0d | 297 | case SOURCEADD: // of switch( pState ) // records the source address of the packet |
Stephen_NewVistas | 0:226550611f0d | 298 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 299 | pc.printf( " - SOURCEADD\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 300 | #endif |
Stephen_NewVistas | 0:226550611f0d | 301 | |
Stephen_NewVistas | 0:226550611f0d | 302 | pState = COLLECTCOMMAND; // state advances to get the command byte |
Stephen_NewVistas | 0:226550611f0d | 303 | buffer[buffPtr++] = RXByte; // add RXByte to buffer |
Stephen_NewVistas | 0:226550611f0d | 304 | buildPacket.sourceID = RXByte; // build packet |
Stephen_NewVistas | 0:226550611f0d | 305 | checksum += RXByte; // add RXByte to checksum |
Stephen_NewVistas | 0:226550611f0d | 306 | break; |
Stephen_NewVistas | 0:226550611f0d | 307 | |
Stephen_NewVistas | 0:226550611f0d | 308 | case COLLECTCOMMAND: // of switch( pState )// records the command byte of the packet |
Stephen_NewVistas | 0:226550611f0d | 309 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 310 | pc.printf( " - COMMAND\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 311 | #endif |
Stephen_NewVistas | 0:226550611f0d | 312 | |
Stephen_NewVistas | 0:226550611f0d | 313 | command = RXByte; // record command byte |
Stephen_NewVistas | 0:226550611f0d | 314 | buildPacket.command = RXByte; // build packet |
Stephen_NewVistas | 0:226550611f0d | 315 | pState = COLLECTCOUNT1; // advance to get packetLength |
Stephen_NewVistas | 0:226550611f0d | 316 | buffer[buffPtr++] = RXByte; // add RXByte to buffer |
Stephen_NewVistas | 0:226550611f0d | 317 | checksum += RXByte; // add RXByte to checksum |
Stephen_NewVistas | 0:226550611f0d | 318 | break; |
Stephen_NewVistas | 0:226550611f0d | 319 | |
Stephen_NewVistas | 0:226550611f0d | 320 | case COLLECTCOUNT1: // of switch( pState ) // records first byte of packetLength |
Stephen_NewVistas | 0:226550611f0d | 321 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 322 | pc.printf( " - COUNT MSB \n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 323 | #endif |
Stephen_NewVistas | 0:226550611f0d | 324 | |
Stephen_NewVistas | 0:226550611f0d | 325 | pState = COLLECTCOUNT2; // advance state to get next byte of packetLength |
Stephen_NewVistas | 0:226550611f0d | 326 | plength = RXByte; |
Stephen_NewVistas | 0:226550611f0d | 327 | plength <<= 8; // store byte in packetLength #SCD is this storing correctly? |
Stephen_NewVistas | 0:226550611f0d | 328 | buffer[buffPtr++] = RXByte; // add RXByte to buffer |
Stephen_NewVistas | 0:226550611f0d | 329 | checksum += RXByte; // add RXByte to checksum |
Stephen_NewVistas | 0:226550611f0d | 330 | break; |
Stephen_NewVistas | 0:226550611f0d | 331 | |
Stephen_NewVistas | 0:226550611f0d | 332 | case COLLECTCOUNT2: // of switch( pState ) // records second byte of packetLength |
Stephen_NewVistas | 0:226550611f0d | 333 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 334 | pc.printf( " - COUNT LSB\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 335 | #endif |
Stephen_NewVistas | 0:226550611f0d | 336 | |
Stephen_NewVistas | 0:226550611f0d | 337 | plength += RXByte; // add RXByte to packetLength total |
Stephen_NewVistas | 0:226550611f0d | 338 | buildPacket.packetLength = plength; // build packet |
Stephen_NewVistas | 0:226550611f0d | 339 | buffer[buffPtr++] = RXByte; // add RXByte to buffer |
Stephen_NewVistas | 0:226550611f0d | 340 | checksum += RXByte; // add RXByte to checksum |
Stephen_NewVistas | 0:226550611f0d | 341 | if( plength == 0 ) // if packetLength is 0, advance to checksum state |
Stephen_NewVistas | 0:226550611f0d | 342 | pState = COLLECTCHECKSUM; |
Stephen_NewVistas | 0:226550611f0d | 343 | else // otherwise move on to collect packet payload |
Stephen_NewVistas | 0:226550611f0d | 344 | pState = COLLECTPACKET; |
Stephen_NewVistas | 0:226550611f0d | 345 | break; |
Stephen_NewVistas | 0:226550611f0d | 346 | |
Stephen_NewVistas | 0:226550611f0d | 347 | case COLLECTPACKET: // of switch( pState ) // collects packetData, enters this state multiple times |
Stephen_NewVistas | 0:226550611f0d | 348 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 349 | pc.printf( " - PACKETDATA \n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 350 | #endif |
Stephen_NewVistas | 0:226550611f0d | 351 | |
Stephen_NewVistas | 0:226550611f0d | 352 | plength--; // decrement length |
Stephen_NewVistas | 0:226550611f0d | 353 | buffer[buffPtr++] = RXByte; // add RXByte to buffer |
Stephen_NewVistas | 0:226550611f0d | 354 | checksum += RXByte; // add RXByte to checksum |
Stephen_NewVistas | 0:226550611f0d | 355 | |
Stephen_NewVistas | 0:226550611f0d | 356 | if( plength == 0 ){ // if collected all packetData advance state |
Stephen_NewVistas | 0:226550611f0d | 357 | pState = COLLECTCHECKSUM; |
Stephen_NewVistas | 0:226550611f0d | 358 | } |
Stephen_NewVistas | 0:226550611f0d | 359 | break; |
Stephen_NewVistas | 0:226550611f0d | 360 | |
Stephen_NewVistas | 0:226550611f0d | 361 | case COLLECTCHECKSUM:// of switch( pState ) // collects checksum of packet and checks for validity |
Stephen_NewVistas | 0:226550611f0d | 362 | #ifdef DEBUG |
Stephen_NewVistas | 0:226550611f0d | 363 | pc.printf( " - CHECKSUM\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 364 | #endif |
Stephen_NewVistas | 0:226550611f0d | 365 | memcpy( &buildPacket.packetData , &buffer[6] , buildPacket.packetLength ); |
Stephen_NewVistas | 0:226550611f0d | 366 | |
Stephen_NewVistas | 0:226550611f0d | 367 | if( RXByte == ( ( checksum* - 1 ) & 0xFF ) ){ // compares RXByte with LSB of checksum |
Stephen_NewVistas | 0:226550611f0d | 368 | message = 0; // if checksum is correct clear message #SCD could a message be waiting? |
Stephen_NewVistas | 0:226550611f0d | 369 | memset(packetBuffer, '\0', 128); // clear packetBuffer |
Stephen_NewVistas | 0:226550611f0d | 370 | |
Stephen_NewVistas | 0:226550611f0d | 371 | buffer[buffPtr++] = RXByte; // add RXByte to buffer |
Stephen_NewVistas | 0:226550611f0d | 372 | |
Stephen_NewVistas | 0:226550611f0d | 373 | #ifdef PRINTPACKETDEBUG |
Stephen_NewVistas | 0:226550611f0d | 374 | pc.printf( "Receive Buffer: " ); // debug print received packet |
Stephen_NewVistas | 0:226550611f0d | 375 | unsigned int i = 0; |
Stephen_NewVistas | 0:226550611f0d | 376 | for( i = 0 ; i < buffPtr ; i++ ){ |
Stephen_NewVistas | 0:226550611f0d | 377 | pc.printf( "%x " , buffer[ i ] ); |
Stephen_NewVistas | 0:226550611f0d | 378 | } |
Stephen_NewVistas | 0:226550611f0d | 379 | pc.printf( "\n\r" ); |
Stephen_NewVistas | 0:226550611f0d | 380 | #endif |
Stephen_NewVistas | 0:226550611f0d | 381 | |
Stephen_NewVistas | 0:226550611f0d | 382 | int errorResult = ProcessPacket( &buildPacket ); |
Stephen_NewVistas | 0:226550611f0d | 383 | |
Stephen_NewVistas | 0:226550611f0d | 384 | if( errorResult == -1 ){ // check to make sure function performed properly |
Stephen_NewVistas | 0:226550611f0d | 385 | //#SCD setResponse(RPIADDRESS, command, ERRORCOMMAND); |
Stephen_NewVistas | 0:226550611f0d | 386 | } |
Stephen_NewVistas | 0:226550611f0d | 387 | SendAck(); // send an ACK |
Stephen_NewVistas | 0:226550611f0d | 388 | } |
Stephen_NewVistas | 0:226550611f0d | 389 | else{ |
Stephen_NewVistas | 0:226550611f0d | 390 | SendNack(); // if checksum is not corret, send a NACK |
Stephen_NewVistas | 0:226550611f0d | 391 | } |
Stephen_NewVistas | 0:226550611f0d | 392 | |
Stephen_NewVistas | 0:226550611f0d | 393 | pState = START; |
Stephen_NewVistas | 0:226550611f0d | 394 | break; |
Stephen_NewVistas | 0:226550611f0d | 395 | |
Stephen_NewVistas | 0:226550611f0d | 396 | } // end switch( pState ) |
Stephen_NewVistas | 0:226550611f0d | 397 | |
Stephen_NewVistas | 0:226550611f0d | 398 | } // end serial2Handler function |
Stephen_NewVistas | 0:226550611f0d | 399 | |
Stephen_NewVistas | 0:226550611f0d | 400 | |
Stephen_NewVistas | 0:226550611f0d | 401 | |
Stephen_NewVistas | 0:226550611f0d | 402 | #ifdef __cplusplus |
Stephen_NewVistas | 0:226550611f0d | 403 | } |
Stephen_NewVistas | 0:226550611f0d | 404 | #endif |