commsCore.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define EXTERN   extern
#define COM_SET_SCI0_INTERFACE_ID   BIT0
#define COM_SET_CAN0_INTERFACE_ID   BIT1
#define COM_SET_SPARE2_INTERFACE_ID   BIT2
#define COM_SET_SPARE3_INTERFACE_ID   BIT3
#define COM_SET_SPARE4_INTERFACE_ID   BIT4
#define COM_SET_SPARE5_INTERFACE_ID   BIT5
#define COM_SET_SPARE6_INTERFACE_ID   BIT6
#define COM_SET_SPARE7_INTERFACE_ID   BIT7
#define COM_CLEAR_SCI0_INTERFACE_ID   NBIT0
#define COM_CLEAR_CAN0_INTERFACE_ID   NBIT1
#define COM_CLEAR_SPARE2_INTERFACE_ID   NBIT2
#define COM_CLEAR_SPARE3_INTERFACE_ID   NBIT3
#define COM_CLEAR_SPARE4_INTERFACE_ID   NBIT4
#define COM_CLEAR_SPARE5_INTERFACE_ID   NBIT5
#define COM_CLEAR_SPARE6_INTERFACE_ID   NBIT6
#define COM_CLEAR_SPARE7_INTERFACE_ID   NBIT7
#define CLEAR_ALL_SOURCE_ID_FLAGS   ZEROS
#define RX_READY_TO_PROCESS   BIT1
#define RX_SCI_ESCAPED_NEXT   BIT2
#define RX_CLEAR_READY_TO_PROCESS   NBIT1
#define RX_SCI_NOT_ESCAPED_NEXT   NBIT2
#define SCICR2_RX_ENABLE   BIT2
#define SCICR2_TX_ENABLE   BIT3
#define SCICR2_RX_ISR_ENABLE   BIT5
#define SCICR2_TX_ISR_ENABLE   BIT7
#define SCICR2_RX_DISABLE   NBIT2
#define SCICR2_TX_DISABLE   NBIT3
#define SCICR2_RX_ISR_DISABLE   NBIT5
#define SCICR2_TX_ISR_DISABLE   NBIT7
#define HEADER_IS_PROTO   BIT0
#define HEADER_HAS_ACK   BIT1
#define HEADER_IS_NACK   BIT2
#define HEADER_HAS_ADDRS   BIT3
#define HEADER_HAS_LENGTH   BIT4
#define HEADER_USER_BIT_A   BIT5
#define HEADER_USER_BIT_B   BIT6
#define HEADER_USER_BIT_C   BIT7
#define requestInterfaceVersion   0
#define requestFirmwareVersion   2
#define requestMaxPacketSize   4
#define requestEchoPacketReturn   6
#define requestSoftSystemReset   8
#define requestHardSystemReset   10
#define asyncErrorCodePacket   13
#define asyncDebugInfoPacket   15
#define replaceBlockInRAM   0
#define replaceBlockInFlash   2
#define retrieveBlockFromRAM   4
#define retrieveBlockFromFlash   6
#define burnBlockFromRamToFlash   8
#define eraseAllBlocksFromFlash   10
#define burnAllBlocksOfFlash   12
#define adjustMainTableCell   100
#define adjustMainTableRPMAxis   102
#define adjustMainTableLoadAxis   104
#define adjust2dTableAxis   200
#define adjust2dTableCell   202
#define requestBasicDatalog   300
#define responseBasicDatalog   301
#define requestConfigurableDatalog   302
#define responseConfigurableDatalog   303
#define setAsyncDatalogType   304
#define forwardPacketOverCAN   400
#define forwardPacketOverOtherUART   402
#define invalidRAMLocationID   404
#define invalidFlashLocationID   406
#define testPayloadIDPacketType0   65520
#define testPayloadIDPacketType1   65522
#define testPayloadIDPacketType2   65524
#define testPayloadIDPacketType3   65526
#define testPayloadIDPacketType4   65528
#define testPayloadIDPacketType5   65530
#define testPayloadIDPacketType6   65532
#define testPayloadIDPacketType7   65534

Functions

EXTERN void decodePacketAndRespond (void) TEXT
 Decode a packet and respond.
EXTERN void sendErrorIfClear (unsigned short) FPAGE_FE
 Send an error if buffer free.
EXTERN void sendDebugIfClear (unsigned char *) FPAGE_FE
 Send a debug message if buffer free.
EXTERN void sendErrorBusyWait (unsigned short) FPAGE_FE
 Send an error even if we must wait.
EXTERN void sendDebugBusyWait (unsigned char *) FPAGE_FE
EXTERN void resetReceiveState (unsigned char) FPAGE_FE
 Reset Receive State.
EXTERN void sendAckIfRequired (void) FPAGE_FE
 Send an ack if required.
EXTERN void checksumAndSend (void) FPAGE_FE
 Checksum a packet and send it.
EXTERN void populateBasicDatalog (void) FPAGE_FE
 Populate a basic datalog packet.

Variables

EXTERN unsigned short TXPacketLengthToSendCAN0
EXTERN unsigned short TXPacketLengthToSendSCI0
EXTERN unsigned char * TXBufferCurrentPositionHandler
EXTERN unsigned char * TXBufferCurrentPositionCAN0
EXTERN unsigned char * TXBufferCurrentPositionSCI0
EXTERN unsigned char TXBufferInUseFlags
EXTERN unsigned char RXBufferContentSourceID
EXTERN unsigned char RXStateFlags
EXTERN unsigned char * RXBufferCurrentPosition
EXTERN unsigned short RXPacketLengthReceived
EXTERN unsigned short RXCalculatedPayloadLength
EXTERN unsigned char RXHeaderFlags
EXTERN unsigned short RXHeaderPayloadID
EXTERN unsigned char RXHeaderSourceAddress
EXTERN unsigned short RXHeaderPayloadLength


Detailed Description

Definition in file commsCore.h.


Define Documentation

#define adjust2dTableAxis   200

Definition at line 207 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define adjust2dTableCell   202

Definition at line 208 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define adjustMainTableCell   100

Definition at line 202 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define adjustMainTableLoadAxis   104

Definition at line 204 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define adjustMainTableRPMAxis   102

Definition at line 203 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define asyncDebugInfoPacket   15

Definition at line 187 of file commsCore.h.

Referenced by sendDebugInternal().

#define asyncErrorCodePacket   13

Definition at line 185 of file commsCore.h.

Referenced by sendErrorInternal().

#define burnAllBlocksOfFlash   12

Definition at line 199 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define burnBlockFromRamToFlash   8

Definition at line 197 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define CLEAR_ALL_SOURCE_ID_FLAGS   ZEROS

Definition at line 97 of file commsCore.h.

Referenced by decodePacketAndRespond(), and SCI0ISR().

#define COM_CLEAR_CAN0_INTERFACE_ID   NBIT1

Definition at line 90 of file commsCore.h.

Referenced by checksumAndSend().

#define COM_CLEAR_SCI0_INTERFACE_ID   NBIT0

Definition at line 89 of file commsCore.h.

Referenced by SCI0ISR().

#define COM_CLEAR_SPARE2_INTERFACE_ID   NBIT2

Definition at line 91 of file commsCore.h.

Referenced by checksumAndSend().

#define COM_CLEAR_SPARE3_INTERFACE_ID   NBIT3

Definition at line 92 of file commsCore.h.

Referenced by checksumAndSend().

#define COM_CLEAR_SPARE4_INTERFACE_ID   NBIT4

Definition at line 93 of file commsCore.h.

Referenced by checksumAndSend().

#define COM_CLEAR_SPARE5_INTERFACE_ID   NBIT5

Definition at line 94 of file commsCore.h.

Referenced by checksumAndSend().

#define COM_CLEAR_SPARE6_INTERFACE_ID   NBIT6

Definition at line 95 of file commsCore.h.

Referenced by checksumAndSend().

#define COM_CLEAR_SPARE7_INTERFACE_ID   NBIT7

Definition at line 96 of file commsCore.h.

Referenced by checksumAndSend().

#define COM_SET_CAN0_INTERFACE_ID   BIT1

Definition at line 82 of file commsCore.h.

Referenced by checksumAndSend(), and resetReceiveState().

#define COM_SET_SCI0_INTERFACE_ID   BIT0

Definition at line 81 of file commsCore.h.

Referenced by checksumAndSend(), decodePacketAndRespond(), main(), resetReceiveState(), and SCI0ISR().

#define COM_SET_SPARE2_INTERFACE_ID   BIT2

Definition at line 83 of file commsCore.h.

Referenced by checksumAndSend().

#define COM_SET_SPARE3_INTERFACE_ID   BIT3

Definition at line 84 of file commsCore.h.

Referenced by checksumAndSend().

#define COM_SET_SPARE4_INTERFACE_ID   BIT4

Definition at line 85 of file commsCore.h.

Referenced by checksumAndSend().

#define COM_SET_SPARE5_INTERFACE_ID   BIT5

Definition at line 86 of file commsCore.h.

Referenced by checksumAndSend().

#define COM_SET_SPARE6_INTERFACE_ID   BIT6

Definition at line 87 of file commsCore.h.

Referenced by checksumAndSend().

#define COM_SET_SPARE7_INTERFACE_ID   BIT7

Definition at line 88 of file commsCore.h.

Referenced by checksumAndSend().

#define eraseAllBlocksFromFlash   10

Definition at line 198 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define EXTERN   extern

Definition at line 49 of file commsCore.h.

#define forwardPacketOverCAN   400

Definition at line 218 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define forwardPacketOverOtherUART   402

Definition at line 219 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define HEADER_HAS_ACK   BIT1

Definition at line 153 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define HEADER_HAS_ADDRS   BIT3

Definition at line 155 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define HEADER_HAS_LENGTH   BIT4

Definition at line 156 of file commsCore.h.

Referenced by decodePacketAndRespond(), and main().

#define HEADER_IS_NACK   BIT2

Definition at line 154 of file commsCore.h.

#define HEADER_IS_PROTO   BIT0

Definition at line 152 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define HEADER_USER_BIT_A   BIT5

Definition at line 157 of file commsCore.h.

#define HEADER_USER_BIT_B   BIT6

Definition at line 158 of file commsCore.h.

#define HEADER_USER_BIT_C   BIT7

Definition at line 159 of file commsCore.h.

#define invalidFlashLocationID   406

Definition at line 221 of file commsCore.h.

#define invalidRAMLocationID   404

Definition at line 220 of file commsCore.h.

#define replaceBlockInFlash   2

Definition at line 194 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define replaceBlockInRAM   0

Definition at line 193 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define requestBasicDatalog   300

Definition at line 211 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define requestConfigurableDatalog   302

Definition at line 213 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define requestEchoPacketReturn   6

Definition at line 177 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define requestFirmwareVersion   2

Definition at line 175 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define requestHardSystemReset   10

Definition at line 180 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define requestInterfaceVersion   0

Definition at line 174 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define requestMaxPacketSize   4

Definition at line 176 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define requestSoftSystemReset   8

Definition at line 178 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define responseBasicDatalog   301

Definition at line 212 of file commsCore.h.

Referenced by main().

#define responseConfigurableDatalog   303

Definition at line 214 of file commsCore.h.

#define retrieveBlockFromFlash   6

Definition at line 196 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define retrieveBlockFromRAM   4

Definition at line 195 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define RX_CLEAR_READY_TO_PROCESS   NBIT1

Definition at line 111 of file commsCore.h.

Referenced by main().

#define RX_READY_TO_PROCESS   BIT1

Definition at line 107 of file commsCore.h.

Referenced by main(), and SCI0ISR().

#define RX_SCI_ESCAPED_NEXT   BIT2

Definition at line 108 of file commsCore.h.

Referenced by SCI0ISR().

#define RX_SCI_NOT_ESCAPED_NEXT   NBIT2

Definition at line 112 of file commsCore.h.

Referenced by SCI0ISR().

#define SCICR2_RX_DISABLE   NBIT2

Definition at line 125 of file commsCore.h.

Referenced by resetReceiveState(), and SCI0ISR().

#define SCICR2_RX_ENABLE   BIT2

Definition at line 121 of file commsCore.h.

Referenced by main(), and resetReceiveState().

#define SCICR2_RX_ISR_DISABLE   NBIT5

Definition at line 127 of file commsCore.h.

Referenced by resetReceiveState(), and SCI0ISR().

#define SCICR2_RX_ISR_ENABLE   BIT5

Definition at line 123 of file commsCore.h.

Referenced by main(), resetReceiveState(), and SCI0ISR().

#define SCICR2_TX_DISABLE   NBIT3

Definition at line 126 of file commsCore.h.

#define SCICR2_TX_ENABLE   BIT3

Definition at line 122 of file commsCore.h.

#define SCICR2_TX_ISR_DISABLE   NBIT7

Definition at line 128 of file commsCore.h.

Referenced by SCI0ISR().

#define SCICR2_TX_ISR_ENABLE   BIT7

Definition at line 124 of file commsCore.h.

Referenced by checksumAndSend(), and SCI0ISR().

#define setAsyncDatalogType   304

Definition at line 215 of file commsCore.h.

Referenced by decodePacketAndRespond().

#define testPayloadIDPacketType0   65520

Definition at line 224 of file commsCore.h.

#define testPayloadIDPacketType1   65522

Definition at line 225 of file commsCore.h.

#define testPayloadIDPacketType2   65524

Definition at line 226 of file commsCore.h.

#define testPayloadIDPacketType3   65526

Definition at line 227 of file commsCore.h.

#define testPayloadIDPacketType4   65528

Definition at line 228 of file commsCore.h.

#define testPayloadIDPacketType5   65530

Definition at line 229 of file commsCore.h.

#define testPayloadIDPacketType6   65532

Definition at line 230 of file commsCore.h.

#define testPayloadIDPacketType7   65534

Definition at line 231 of file commsCore.h.


Function Documentation

EXTERN void checksumAndSend ( void   ) 

Checksum a packet and send it.

This functions job is to finalise the main loop part of the packet sending process. It runs a checksum over the packet data and tags it to the end before configuring the various ISRs that need to send the data out.

Author:
Fred Cooke
Bug:
http://freeems.aaronb.info/tracker/view.php?id=81
Todo:
TODO fix the double/none start byte bug and remove the hack!

Definition at line 146 of file commsCore.c.

References checksum(), COM_CLEAR_CAN0_INTERFACE_ID, COM_CLEAR_SPARE2_INTERFACE_ID, COM_CLEAR_SPARE3_INTERFACE_ID, COM_CLEAR_SPARE4_INTERFACE_ID, COM_CLEAR_SPARE5_INTERFACE_ID, COM_CLEAR_SPARE6_INTERFACE_ID, COM_CLEAR_SPARE7_INTERFACE_ID, COM_SET_CAN0_INTERFACE_ID, COM_SET_SCI0_INTERFACE_ID, COM_SET_SPARE2_INTERFACE_ID, COM_SET_SPARE3_INTERFACE_ID, COM_SET_SPARE4_INTERFACE_ID, COM_SET_SPARE5_INTERFACE_ID, COM_SET_SPARE6_INTERFACE_ID, COM_SET_SPARE7_INTERFACE_ID, SCI0CR2, SCI0DRL, SCI0SR1, SCICR2_TX_ISR_ENABLE, START_BYTE, TXBufferCurrentPositionHandler, TXBufferInUseFlags, TXPacketLengthToSendCAN0, and TXPacketLengthToSendSCI0.

Referenced by decodePacketAndRespond(), main(), sendDebugInternal(), and sendErrorInternal().

00146                       {
00147     /* Get the length from the pointer */
00148     unsigned short TXPacketLengthToSend = (unsigned short)TXBufferCurrentPositionHandler - (unsigned short)&TXBuffer;
00149 
00150     /* Tag the checksum on the end */
00151     *TXBufferCurrentPositionHandler = checksum((unsigned char*)&TXBuffer, TXPacketLengthToSend);
00152     TXPacketLengthToSend++;
00153 
00154     /* Send it out on all the channels required. */
00155 
00156     /* SCI0 - Main serial interface */
00157     if(TXBufferInUseFlags & COM_SET_SCI0_INTERFACE_ID){
00158         /* Copy numbers to interface specific vars */
00159         TXPacketLengthToSendSCI0 = TXPacketLengthToSend;
00160         TXPacketLengthToSendCAN0 = TXPacketLengthToSend;
00161 
00162         /* Queue preamble by clearing and then setting transmit enable  */
00163         /* See section 11.4.5.2 of the xdp512 specification document    */
00164         //SCI0CR2 &= SCICR2_TX_DISABLE;
00165         //SCI0CR2 |= SCICR2_TX_ENABLE;
00166 
00167         /* Initiate transmission */
00168         SCI0DRL = START_BYTE;
00169         while(!(SCI0SR1 & 0x80)){/* Wait for ever until able to send then move on */}
00170         SCI0DRL = START_BYTE; // nasty hack that works... means at least one and most 2 starts are sent so stuff works, but is messy... there must be a better way.
00171 
00172         /* Note : Order Is Important! */
00173         /* TX empty flag is already set, so we must clear it by writing out before enabling the interrupt */
00174         SCI0CR2 |= SCICR2_TX_ISR_ENABLE;
00175     }
00176     /* CAN0 - Main CAN interface */
00177     if(TXBufferInUseFlags & COM_SET_CAN0_INTERFACE_ID){
00178         // just clear up front for now
00179         TXBufferInUseFlags &= COM_CLEAR_CAN0_INTERFACE_ID;
00180     }
00181     /* spare2 */
00182     if(TXBufferInUseFlags & COM_SET_SPARE2_INTERFACE_ID){
00183         // just clear up front for now
00184         TXBufferInUseFlags &= COM_CLEAR_SPARE2_INTERFACE_ID;
00185     }
00186     /* spare3 */
00187     if(TXBufferInUseFlags & COM_SET_SPARE3_INTERFACE_ID){
00188         // just clear up front for now
00189         TXBufferInUseFlags &= COM_CLEAR_SPARE3_INTERFACE_ID;
00190     }
00191     /* spare4 */
00192     if(TXBufferInUseFlags & COM_SET_SPARE4_INTERFACE_ID){
00193         // just clear up front for now
00194         TXBufferInUseFlags &= COM_CLEAR_SPARE4_INTERFACE_ID;
00195     }
00196     /* spare5 */
00197     if(TXBufferInUseFlags & COM_SET_SPARE5_INTERFACE_ID){
00198         // just clear up front for now
00199         TXBufferInUseFlags &= COM_CLEAR_SPARE5_INTERFACE_ID;
00200     }
00201     /* spare6 */
00202     if(TXBufferInUseFlags & COM_SET_SPARE6_INTERFACE_ID){
00203         // just clear up front for now
00204         TXBufferInUseFlags &= COM_CLEAR_SPARE6_INTERFACE_ID;
00205     }
00206     /* spare7 */
00207     if(TXBufferInUseFlags & COM_SET_SPARE7_INTERFACE_ID){
00208         // just clear up front for now
00209         TXBufferInUseFlags &= COM_CLEAR_SPARE7_INTERFACE_ID;
00210     }
00211 }

Here is the call graph for this function:

EXTERN void decodePacketAndRespond ( void   ) 

Decode a packet and respond.

This is the core function that controls what functionality is run when a packet is received in full by the ISR code and control is passed back to the main loop code. The vast majority of communications action happens here.

Author:
Fred Cooke

Definition at line 222 of file commsCore.c.

References adjust2dTableAxis, adjust2dTableCell, adjustMainTableCell, adjustMainTableLoadAxis, adjustMainTableRPMAxis, ARMCOP, asyncDatalogBasic, asyncDatalogOff, asyncDatalogType, BIT0, burnAllBlocksOfFlash, burnBlockFromRamToFlash, checksumAndSend(), CLEAR_ALL_SOURCE_ID_FLAGS, COM_SET_SCI0_INTERFACE_ID, configuredBasicDatalogLength, COPCTL, datalogLengthExceedsMax, eraseAllBlocksFromFlash, eraseSector(), firmwareVersion, fixedConfigs1, blockDetails::FlashAddress, blockDetails::FlashPage, forwardPacketOverCAN, forwardPacketOverOtherUART, HEADER_HAS_ACK, HEADER_HAS_ADDRS, HEADER_HAS_LENGTH, HEADER_IS_PROTO, interfaceVersionAndType, invalidFirmwarePayloadID, invalidIDForMainTableAction, invalidIDForTwoDTableAction, invalidMemoryActionForID, invalidProtocolPayloadID, lookupBlockDetails(), MAINTABLE_MAX_LOAD_LENGTH, MAINTABLE_MAX_MAIN_LENGTH, MAINTABLE_MAX_RPM_LENGTH, maxBasicDatalogLength, serialSetting::networkAddress, noSuchAsyncDatalogType, payloadLengthHeaderMismatch, payloadLengthTypeMismatch, populateBasicDatalog(), PORTK, PPAGE, blockDetails::RAMAddress, blockDetails::RAMPage, replaceBlockInFlash, replaceBlockInRAM, requestBasicDatalog, requestConfigurableDatalog, requestEchoPacketReturn, requestFirmwareVersion, requestHardSystemReset, requestInterfaceVersion, requestMaxPacketSize, requestSoftSystemReset, resetReceiveState(), retrieveBlockFromFlash, retrieveBlockFromRAM, RPAGE, RX_BUFFER_SIZE, RXBufferCurrentPosition, RXCalculatedPayloadLength, RXHeaderFlags, RXHeaderPayloadID, RXHeaderPayloadLength, RXHeaderSourceAddress, RXPacketLengthReceived, sendAckIfRequired(), sendDebugInternal(), sendErrorInternal(), fixedConfig1::serialSettings, setAsyncDatalogType, setPagedMainTableCellValue(), setPagedMainTableLoadValue(), setPagedMainTableRPMValue(), setPagedTwoDTableAxisValue(), setPagedTwoDTableCellValue(), blockDetails::size, sourceAddressIsBroadcast, sourceAddressIsDuplicate, TXBufferCurrentPositionCAN0, TXBufferCurrentPositionHandler, TXBufferCurrentPositionSCI0, TXBufferInUseFlags, unimplementedFunction, unrecognisedFirmwarePayloadID, unrecognisedProtocolPayloadID, validateMainTable(), validateTwoDTable(), writeBlock(), and writeSector().

Referenced by main().

00222                              {
00223     /* Extract and build up the header fields */
00224     RXBufferCurrentPosition = (unsigned char*)&RXBuffer;
00225     TXBufferCurrentPositionHandler = (unsigned char*)&TXBuffer;
00226 
00227     /* Initialised here such that override is possible */
00228     TXBufferCurrentPositionSCI0 = (unsigned char*)&TXBuffer;
00229     TXBufferCurrentPositionCAN0 = (unsigned char*)&TXBuffer;
00230 
00231     /* Start this off as full packet length and build down to the actual length */
00232     RXCalculatedPayloadLength = RXPacketLengthReceived;
00233 
00234     /* Grab the RX header flags out of the RX buffer */
00235     RXHeaderFlags = *RXBufferCurrentPosition;
00236     RXBufferCurrentPosition++;
00237     RXCalculatedPayloadLength--;
00238 
00239     /* Flag that we are transmitting! */
00240     TXBufferInUseFlags |= COM_SET_SCI0_INTERFACE_ID;
00241     // SCI0 only for now...
00242 
00243     /* Load a blank header into the TX buffer ready for masking */
00244     unsigned char* TXHeaderFlags = TXBufferCurrentPositionHandler;
00245     *TXHeaderFlags = 0;
00246     TXBufferCurrentPositionHandler++;
00247 
00248     /* Grab the payload ID for processing and load the return ID */
00249     RXHeaderPayloadID = *((unsigned short*)RXBufferCurrentPosition);
00250     *((unsigned short*)TXBufferCurrentPositionHandler) = RXHeaderPayloadID + 1;
00251     RXBufferCurrentPosition += 2;
00252     TXBufferCurrentPositionHandler += 2;
00253     RXCalculatedPayloadLength -= 2;
00254 
00255     /* If there is an ack, copy it to the return packet */
00256     if(RXHeaderFlags & HEADER_HAS_ACK){
00257         *TXBufferCurrentPositionHandler = *RXBufferCurrentPosition;
00258         *TXHeaderFlags |= HEADER_HAS_ACK;
00259         RXBufferCurrentPosition++;
00260         TXBufferCurrentPositionHandler++;
00261         RXCalculatedPayloadLength--;
00262     }
00263 
00264     /* If the header has addresses, check them and if OK copy them */
00265     if(RXHeaderFlags & HEADER_HAS_ADDRS){
00266         /* Check the destination address against our address */
00267         if(*RXBufferCurrentPosition != fixedConfigs1.serialSettings.networkAddress){
00268             /* Addresses do not match, discard packet without error */
00269             resetReceiveState(CLEAR_ALL_SOURCE_ID_FLAGS);
00270             TXBufferInUseFlags = 0;
00271             return;
00272         }
00273         RXBufferCurrentPosition++;
00274 
00275         /* Save and check the source address */
00276         RXHeaderSourceAddress = *RXBufferCurrentPosition;
00277         RXBufferCurrentPosition++;
00278         if(RXHeaderSourceAddress == 0){
00279             sendErrorInternal(sourceAddressIsBroadcast);
00280             resetReceiveState(CLEAR_ALL_SOURCE_ID_FLAGS);
00281             return;
00282         }
00283         if(RXHeaderSourceAddress == fixedConfigs1.serialSettings.networkAddress){
00284             sendErrorInternal(sourceAddressIsDuplicate);
00285             resetReceiveState(CLEAR_ALL_SOURCE_ID_FLAGS);
00286             return;
00287         }
00288 
00289         /* All is well, setup reply addresses */
00290         *TXHeaderFlags |= HEADER_HAS_ADDRS;
00291         /* TX destination = RX source */
00292         *TXBufferCurrentPositionHandler = RXHeaderSourceAddress;
00293         TXBufferCurrentPositionHandler++;
00294         /* TX source = our address */
00295         *TXBufferCurrentPositionHandler = fixedConfigs1.serialSettings.networkAddress;
00296         TXBufferCurrentPositionHandler++;
00297         /* Decrement for both at once to save a cycle */
00298         RXCalculatedPayloadLength -= 2;
00299     }
00300 
00301     /* Subtract checksum to get final length */
00302     RXCalculatedPayloadLength--;
00303 
00304     /* Grab the length if available */
00305     if(RXHeaderFlags & HEADER_HAS_LENGTH){
00306         RXHeaderPayloadLength = *((unsigned short*)RXBufferCurrentPosition);
00307         RXBufferCurrentPosition += 2;
00308         RXCalculatedPayloadLength -= 2;
00309         /* Already subtracted one for checksum */
00310         if(RXHeaderPayloadLength != RXCalculatedPayloadLength){
00311             sendErrorInternal(payloadLengthHeaderMismatch);
00312             resetReceiveState(CLEAR_ALL_SOURCE_ID_FLAGS);
00313             return;
00314         }
00315     }
00316 
00317     /* Please Note :                                                            */
00318     /* length (and it's flag) should be set by each return packet type handler if required or desired.  */
00319     /* If an ack has been requested, ensure the negative ack flag is set if the opration failed.        */
00320 
00321     /* Perform the requested action based on payload ID */
00322     if (RXHeaderFlags & HEADER_IS_PROTO){ /* Protocol payload types */
00323         /* Set the return type to be protocol too */
00324         *TXHeaderFlags |= HEADER_IS_PROTO;
00325 
00326         switch (RXHeaderPayloadID){
00327         case requestInterfaceVersion:
00328         {
00329             if(RXCalculatedPayloadLength != 0){
00330                 sendErrorInternal(payloadLengthTypeMismatch);
00331                 break;
00332             }
00333 
00334             /* This type must have a length field, set that up */
00335             *((unsigned short*)TXBufferCurrentPositionHandler) = sizeof(interfaceVersionAndType);
00336             *TXHeaderFlags |= HEADER_HAS_LENGTH;
00337             TXBufferCurrentPositionHandler += 2;
00338             /* Load the body into place */
00339             memcpy((void*)TXBufferCurrentPositionHandler, (void*)&interfaceVersionAndType, sizeof(interfaceVersionAndType));
00340             TXBufferCurrentPositionHandler += sizeof(interfaceVersionAndType);
00341             checksumAndSend();
00342             break;
00343         }
00344         case requestFirmwareVersion:
00345         {
00346             if(RXCalculatedPayloadLength != 0){
00347                 sendErrorInternal(payloadLengthTypeMismatch);
00348                 break;
00349             }
00350             /* This type must have a length field, set that up */
00351             *((unsigned short*)TXBufferCurrentPositionHandler) = sizeof(firmwareVersion);
00352             *TXHeaderFlags |= HEADER_HAS_LENGTH;
00353             TXBufferCurrentPositionHandler += 2;
00354             /* Load the body into place */
00355             memcpy((void*)TXBufferCurrentPositionHandler, (void*)&firmwareVersion, sizeof(firmwareVersion));
00356             TXBufferCurrentPositionHandler += sizeof(firmwareVersion);
00357             checksumAndSend();
00358             break;
00359         }
00360         case requestMaxPacketSize:
00361         {
00362             if(RXCalculatedPayloadLength != 0){
00363                 sendErrorInternal(payloadLengthTypeMismatch);
00364                 break;
00365             }
00366             /* Load the size into place */
00367             *((unsigned short*)TXBufferCurrentPositionHandler) = RX_BUFFER_SIZE;
00368             TXBufferCurrentPositionHandler += 2;
00369             checksumAndSend();
00370             break;
00371         }
00372         case requestEchoPacketReturn:
00373         {
00374             /* This type must have a length field, set that up */
00375             *((unsigned short*)TXBufferCurrentPositionHandler) = RXPacketLengthReceived;
00376             *TXHeaderFlags |= HEADER_HAS_LENGTH;
00377             TXBufferCurrentPositionHandler += 2;
00378             /* Load the body into place */
00379             memcpy((void*)TXBufferCurrentPositionHandler, (void*)&RXBuffer, RXPacketLengthReceived);
00380             /* Note, there is no overflow check here because the TX buffer is slightly       */
00381             /* bigger than the RX buffer and there is overflow checking for receives anyway. */
00382             TXBufferCurrentPositionHandler += RXPacketLengthReceived;
00383             checksumAndSend();
00384             break;
00385         }
00386         case requestSoftSystemReset:
00387         {
00388             // hack to allow datalog on/off from the orange button (thank christ I asked for that button when I did)
00389             if(asyncDatalogType){
00390                 asyncDatalogType = asyncDatalogOff;
00391             }else{
00392                 asyncDatalogType = asyncDatalogBasic;
00393             }
00394             sendAckIfRequired(); // TODO implement
00395             break;
00396 
00397 //          // hack to use soft reset to request registers
00398 //          /* This type must have a length field, set that up */
00399 //          *((unsigned short*)TXBufferCurrentPositionHandler) = memdumplength;
00400 //          *TXHeaderFlags |= HEADER_HAS_LENGTH;
00401 //          TXBufferCurrentPositionHandler += 2;
00402 //          /* Load the body into place */
00403 //          memcpy((void*)TXBufferCurrentPositionHandler, memdumpaddr, memdumplength);
00404 //          TXBufferCurrentPositionHandler += memdumplength;
00405 //          memdumpaddr += memdumplength;
00406 //          checksumAndSend();
00407 //          break;
00408 
00409 //          if(RXCalculatedPayloadLength != 0){
00410 //              sendErrorInternal(payloadLengthTypeMismatch);
00411 //              break;
00412 //          }
00413 //          /* Perform soft system reset */
00414 //          _start();
00415         }
00416         case requestHardSystemReset:
00417         {
00418             if(RXCalculatedPayloadLength != 0){
00419                 sendErrorInternal(payloadLengthTypeMismatch);
00420                 break;
00421             }
00422 
00423             /* This is how the serial monitor does it. */
00424             COPCTL = 0x01; /* Arm with shortest time */
00425             ARMCOP = 0xFF; /* Write bad value, should cause immediate reset */
00426             /* Using _start() only resets the app ignoring the monitor switch. It does not work */
00427             /* properly because the location of _start is not the master reset vector location. */
00428         }
00429         default:
00430         {
00431             if((RXHeaderPayloadID % 2) == 1){
00432                 sendErrorInternal(invalidProtocolPayloadID);
00433             }else{
00434                 sendErrorInternal(unrecognisedProtocolPayloadID);
00435             }
00436         }
00437         }
00438     }else{ /* Otherwise firmware payload types */
00439         switch (RXHeaderPayloadID) {
00440         case replaceBlockInRAM:
00441         {
00442             /* Extract the ram location ID from the received data */
00443             unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00444             RXBufferCurrentPosition += 2;
00445 
00446             /* Look up the memory location details */
00447             blockDetails details;
00448             lookupBlockDetails(locationID, &details);
00449 
00450             /* Subtract two to allow for the locationID */
00451             if((RXCalculatedPayloadLength - 2) != details.size){
00452                 sendErrorInternal(payloadLengthTypeMismatch);
00453                 break;
00454             }
00455 
00456             if((details.RAMPage == 0) || (details.RAMAddress == 0)){
00457                 sendErrorInternal(invalidMemoryActionForID);
00458                 break;
00459             }
00460 
00461             // TODO factor this out into validation delegation function once the number of types increases somewhat
00462             unsigned short errorID = 0;
00463             if(locationID < 16){
00464 //              mainTable aTable;
00465                 errorID = validateMainTable((mainTable*)RXBufferCurrentPosition);
00466             }else if((locationID > 399) && (locationID < 900)){
00467 //              twoDTableUS aTable;
00468                 errorID = validateTwoDTable((twoDTableUS*)RXBufferCurrentPosition);
00469             }// TODO add other table types here
00470             /* If the validation failed, report it */
00471             if(errorID != 0){
00472                 sendErrorInternal(errorID);
00473                 break;
00474             }
00475 
00476             /* Save page values for restore */
00477             unsigned char oldRamPage = RPAGE;
00478             /* Set the viewable ram page */
00479             RPAGE = details.RAMPage;
00480             /* Copy from the RX buffer to the block of ram */
00481             memcpy(details.RAMAddress, RXBufferCurrentPosition, details.size);
00482             /* Restore the original ram and flash pages */
00483             RPAGE = oldRamPage;
00484 
00485             sendAckIfRequired(); // TODO implement
00486             break;
00487         }
00488         case replaceBlockInFlash:
00489         {
00490             /* Extract the ram location ID from the received data */
00491             unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00492             RXBufferCurrentPosition += 2;
00493 
00494             /* Look up the memory location details */
00495             blockDetails details;
00496             lookupBlockDetails(locationID, &details);
00497 
00498             /* Subtract two to allow for the locationID */
00499             if((RXCalculatedPayloadLength - 2) != details.size){
00500                 sendErrorInternal(payloadLengthTypeMismatch);
00501                 break;
00502             }
00503 
00504             if((details.FlashPage == 0) || (details.FlashAddress == 0)){
00505                 sendErrorInternal(invalidMemoryActionForID);
00506                 break;
00507             }
00508 
00509             // TODO factor this out into validation delegation function once the number of types increases somewhat
00510             unsigned short errorID = 0;
00511             if(locationID < 16){
00512 //              mainTable aTable;
00513                 errorID = validateMainTable((mainTable*)RXBufferCurrentPosition);
00514             }else if((locationID > 399) && (locationID < 900)){
00515 //              twoDTableUS aTable;
00516                 errorID = validateTwoDTable((twoDTableUS*)RXBufferCurrentPosition);
00517             }// TODO add other table types here
00518             /* If the validation failed, report it */
00519             if(errorID != 0){
00520                 sendErrorInternal(errorID);
00521                 break;
00522             }
00523 
00524             /* Calculate the position of the end of the stored packet for use as a buffer */
00525             void* buffer = (void*)((unsigned short)&RXBuffer + RXPacketLengthReceived);
00526 
00527             /* Swap the RAM details such that the block gets pulled down from the buffer */
00528             unsigned char originalRAMPage = details.RAMPage;
00529             void* originalRAMAddress = details.RAMAddress;
00530             details.RAMPage = RPAGE;
00531             details.RAMAddress = RXBufferCurrentPosition;
00532 
00533             /* Copy from the RX buffer to the block of flash */
00534             errorID = writeBlock(&details, buffer);
00535             if(errorID != 0){
00536                 sendErrorInternal(errorID);
00537                 break;
00538             }
00539 
00540             /* If present in RAM, update that too */
00541             if((originalRAMPage != 0) && (originalRAMAddress != 0)){
00542                 /* Save page values for restore */
00543                 unsigned char oldRamPage = RPAGE;
00544                 /* Set the viewable ram page */
00545                 RPAGE = originalRAMPage;
00546                 /* Copy from the RX buffer to the block of ram */
00547                 memcpy(originalRAMAddress, RXBufferCurrentPosition, details.size);
00548                 /* Restore the original ram and flash pages */
00549                 RPAGE = oldRamPage;
00550             }
00551 
00552             sendAckIfRequired();
00553             // TODO document errors can always be returned and add error check in to send as response for ack and async otherwise
00554             break;
00555         }
00556         case retrieveBlockFromRAM:
00557         {
00558             if(RXCalculatedPayloadLength != 2){
00559                 sendErrorInternal(payloadLengthTypeMismatch);
00560                 break;
00561             }
00562 
00563             /* Extract the ram location ID from the received data */
00564             unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00565             /* Store it back into the output data */
00566             *(unsigned short*)TXBufferCurrentPositionHandler = locationID;
00567             TXBufferCurrentPositionHandler += 2;
00568 
00569             /* If it's a main table we are returning, specify the limits explicitly */
00570             if(locationID < 16){
00571                 /* Store it back into the output data */
00572                 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_RPM_LENGTH;
00573                 TXBufferCurrentPositionHandler += 2;
00574                 /* Store it back into the output data */
00575                 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_LOAD_LENGTH;
00576                 TXBufferCurrentPositionHandler += 2;
00577                 /* Store it back into the output data */
00578                 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_MAIN_LENGTH;
00579                 TXBufferCurrentPositionHandler += 2;
00580             }
00581 
00582             /* Look up the memory location details */
00583             blockDetails details;
00584             lookupBlockDetails(locationID, &details);
00585 
00586             if((details.RAMPage == 0) || (details.RAMAddress == 0)){
00587                 sendErrorInternal(invalidMemoryActionForID);
00588                 break;
00589             }
00590 
00591             /* Save page value for restore and set the visible page */
00592             unsigned char oldRamPage = RPAGE;
00593             RPAGE = details.RAMPage;
00594 
00595             /* Copy the block of ram to the TX buffer */
00596             memcpy(TXBufferCurrentPositionHandler, details.RAMAddress, details.size);
00597             TXBufferCurrentPositionHandler += details.size;
00598 
00599             /* Restore the original ram and flash pages */
00600             RPAGE = oldRamPage;
00601 
00602             checksumAndSend();
00603             break;
00604         }
00605         case retrieveBlockFromFlash:
00606         {
00607             if(RXCalculatedPayloadLength != 2){
00608                 sendErrorInternal(payloadLengthTypeMismatch);
00609                 break;
00610             }
00611 
00612             /* Extract the flash location ID from the received data */
00613             unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00614             /* Store it back into the output data */
00615             *(unsigned short*)TXBufferCurrentPositionHandler = locationID;
00616             TXBufferCurrentPositionHandler += 2;
00617 
00618             /* If it's a main table we are returning, specify the limits explicitly */
00619             if(locationID < 16){
00620                 /* Store it back into the output data */
00621                 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_RPM_LENGTH;
00622                 TXBufferCurrentPositionHandler += 2;
00623                 /* Store it back into the output data */
00624                 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_LOAD_LENGTH;
00625                 TXBufferCurrentPositionHandler += 2;
00626                 /* Store it back into the output data */
00627                 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_MAIN_LENGTH;
00628                 TXBufferCurrentPositionHandler += 2;
00629             }
00630 
00631             /* Look up the memory location details */
00632             blockDetails details;
00633             lookupBlockDetails(locationID, &details);
00634 
00635             if((details.FlashPage == 0) || (details.FlashAddress == 0)){
00636                 sendErrorInternal(invalidMemoryActionForID);
00637                 break;
00638             }
00639 
00640             /* Save page value for restore and set the visible page */
00641             unsigned char oldFlashPage = PPAGE;
00642             PPAGE = details.FlashPage;
00643 
00644             /* Copy the block of flash to the TX buffer */
00645             memcpy(TXBufferCurrentPositionHandler, details.FlashAddress, details.size);
00646             TXBufferCurrentPositionHandler += details.size;
00647 
00648             /* Restore the original ram and flash pages */
00649             PPAGE = oldFlashPage;
00650 
00651             checksumAndSend();
00652             break;
00653         }
00654         case burnBlockFromRamToFlash:
00655         {
00656             if(RXCalculatedPayloadLength != 2){
00657                 sendErrorInternal(payloadLengthTypeMismatch);
00658                 break;
00659             }
00660 
00661             /* Extract the flash location ID from the received data */
00662             unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00663 
00664             /* Look up the memory location details */
00665             blockDetails details;
00666             lookupBlockDetails(locationID, &details);
00667 
00668             /* Check that all data we need is present */
00669             if((details.RAMPage == 0) || (details.RAMAddress == 0) || (details.FlashPage == 0) || (details.FlashAddress == 0)){
00670                 sendErrorInternal(invalidMemoryActionForID);
00671                 break;
00672             }
00673 
00674             /* Calculate the position of the end of the stored packet for use as a buffer */
00675             void* buffer = (void*)((unsigned short)&RXBuffer + RXPacketLengthReceived);
00676 
00677             /* Write the block down from RAM to Flash */
00678             unsigned short errorID = writeBlock(&details, buffer);
00679 
00680             if(errorID != 0){
00681                 sendErrorInternal(errorID);
00682                 break;
00683             }
00684 
00685             sendAckIfRequired();
00686             break;
00687         }
00688         case eraseAllBlocksFromFlash:
00689         {
00690             if(RXCalculatedPayloadLength != 0){
00691                 sendErrorInternal(payloadLengthTypeMismatch);
00692                 break;
00693             }
00694 
00695             // perform function TODO
00696             unsigned char page = 0xE0;
00697             unsigned short start = 0x8000;
00698             unsigned short end = 0xC000;
00699             unsigned short inc = 0x0400;
00700             for(;page < 0xF8;page++){
00701                 unsigned short addr;
00702                 for(addr = start;addr < end; addr += inc){
00703                     // TODO create selfDestruct() function for loading larger code to the device using all flash pages.
00704                     eraseSector(page, (unsigned short*)addr);
00705                 }
00706             }
00707             sendDebugInternal("Erased three 128k Flash blocks!");
00708             break;
00709         }
00710         case burnAllBlocksOfFlash:
00711         {
00712             if(RXCalculatedPayloadLength != 0){
00713                 sendErrorInternal(payloadLengthTypeMismatch);
00714                 break;
00715             }
00716 
00717             // perform function TODO
00718             unsigned char page = 0xE0;
00719             unsigned short start = 0x8000;
00720             unsigned short end = 0xC000;
00721             unsigned short inc = 0x0400;
00722             for(;page < 0xF8;page++){
00723                 unsigned short addr;
00724                 for(addr = start;addr < end; addr += inc){
00725                     writeSector(RPAGE, (unsigned short*)0xc000, page, (unsigned short*)addr);
00726                 }
00727             }
00728             sendDebugInternal("Overwrote three 128k Flash blocks!");
00729             break;
00730         }
00731         case adjustMainTableCell:
00732         {
00733             if(RXCalculatedPayloadLength != 8){
00734                 sendErrorInternal(payloadLengthTypeMismatch);
00735                 break;
00736             }
00737 
00738             /* Extract the flash location ID from the received data */
00739             unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00740             RXBufferCurrentPosition += 2;
00741 
00742             /* Check the ID to ensure it is a main table */
00743             if(15 < locationID){
00744                 sendErrorInternal(invalidIDForMainTableAction);
00745                 break;
00746             }
00747 
00748             /* Extract the cell value and coordinates */
00749             unsigned short RPMIndex = *((unsigned short*)RXBufferCurrentPosition);
00750             RXBufferCurrentPosition += 2;
00751             unsigned short LoadIndex = *((unsigned short*)RXBufferCurrentPosition);
00752             RXBufferCurrentPosition += 2;
00753             unsigned short cellValue = *((unsigned short*)RXBufferCurrentPosition);
00754 
00755             /* Look up the memory location details */
00756             blockDetails details;
00757             lookupBlockDetails(locationID, &details);
00758 
00759             /* Attempt to set the value */
00760             unsigned short errorID = setPagedMainTableCellValue(details.RAMPage, details.RAMAddress, RPMIndex, LoadIndex, cellValue);
00761             if(errorID != 0){
00762                 sendErrorInternal(errorID);
00763             }else{
00764                 sendAckIfRequired();
00765             }
00766             break;
00767         }
00768         case adjustMainTableRPMAxis:
00769         {
00770             if(RXCalculatedPayloadLength != 6){
00771                 sendErrorInternal(payloadLengthTypeMismatch);
00772                 break;
00773             }
00774 
00775             /* Extract the flash location ID from the received data */
00776             unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00777             RXBufferCurrentPosition -= 2;
00778 
00779             /* Check the ID to ensure it is a main table */
00780             if(15 < locationID){
00781                 sendErrorInternal(invalidIDForMainTableAction);
00782                 break;
00783             }
00784 
00785             /* Extract the cell value and coordinates */
00786             unsigned short RPMIndex = *((unsigned short*)RXBufferCurrentPosition);
00787             RXBufferCurrentPosition -= 2;
00788             unsigned short RPMValue = *((unsigned short*)RXBufferCurrentPosition);
00789 
00790             /* Look up the memory location details */
00791             blockDetails details;
00792             lookupBlockDetails(locationID, &details);
00793 
00794             /* Attempt to set the value */
00795             unsigned short errorID = setPagedMainTableRPMValue(details.RAMPage, details.RAMAddress, RPMIndex, RPMValue);
00796             if(errorID != 0){
00797                 sendErrorInternal(errorID);
00798             }else{
00799                 sendAckIfRequired();
00800             }
00801             break;
00802         }
00803         case adjustMainTableLoadAxis:
00804         {
00805             if(RXCalculatedPayloadLength != 6){
00806                 sendErrorInternal(payloadLengthTypeMismatch);
00807                 break;
00808             }
00809 
00810             /* Extract the flash location ID from the received data */
00811             unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00812             RXBufferCurrentPosition -= 2;
00813 
00814             /* Check the ID to ensure it is a main table */
00815             if(15 < locationID){
00816                 sendErrorInternal(invalidIDForMainTableAction);
00817                 break;
00818             }
00819 
00820             /* Extract the cell value and coordinates */
00821             unsigned short LoadIndex = *((unsigned short*)RXBufferCurrentPosition);
00822             RXBufferCurrentPosition -= 2;
00823             unsigned short LoadValue = *((unsigned short*)RXBufferCurrentPosition);
00824 
00825             /* Look up the memory location details */
00826             blockDetails details;
00827             lookupBlockDetails(locationID, &details);
00828 
00829             /* Attempt to set the value */
00830             unsigned short errorID = setPagedMainTableLoadValue(details.RAMPage, details.RAMAddress, LoadIndex, LoadValue);
00831             if(errorID != 0){
00832                 sendErrorInternal(errorID);
00833             }else{
00834                 sendAckIfRequired();
00835             }
00836             break;
00837         }
00838         case adjust2dTableAxis:
00839         {
00840             if(RXCalculatedPayloadLength != 6){
00841                 sendErrorInternal(payloadLengthTypeMismatch);
00842                 break;
00843             }
00844 
00845             /* Extract the flash location ID from the received data */
00846             unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00847             RXBufferCurrentPosition -= 2;
00848 
00849             /* Check the ID to ensure it is a 2d table */
00850             if((locationID > 899) || (locationID < 400)){
00851                 sendErrorInternal(invalidIDForTwoDTableAction);
00852                 break;
00853             }
00854 
00855             /* Extract the cell value and coordinates */
00856             unsigned short axisIndex = *((unsigned short*)RXBufferCurrentPosition);
00857             RXBufferCurrentPosition -= 2;
00858             unsigned short axisValue = *((unsigned short*)RXBufferCurrentPosition);
00859 
00860             /* Look up the memory location details */
00861             blockDetails details;
00862             lookupBlockDetails(locationID, &details);
00863 
00864             /* Attempt to set the value */
00865             unsigned short errorID = setPagedTwoDTableAxisValue(details.RAMPage, details.RAMAddress, axisIndex, axisValue);
00866             if(errorID != 0){
00867                 sendErrorInternal(errorID);
00868             }else{
00869                 sendAckIfRequired();
00870             }
00871             break;
00872         }
00873         case adjust2dTableCell:
00874         {
00875             if(RXCalculatedPayloadLength != 6){
00876                 sendErrorInternal(payloadLengthTypeMismatch);
00877                 break;
00878             }
00879 
00880             /* Extract the flash location ID from the received data */
00881             unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00882             RXBufferCurrentPosition -= 2;
00883 
00884             /* Check the ID to ensure it is a 2d table */
00885             if((locationID > 899) || (locationID < 400)){
00886                 sendErrorInternal(invalidIDForTwoDTableAction);
00887                 break;
00888             }
00889 
00890             /* Extract the cell value and coordinates */
00891             unsigned short cellIndex = *((unsigned short*)RXBufferCurrentPosition);
00892             RXBufferCurrentPosition -= 2;
00893             unsigned short cellValue = *((unsigned short*)RXBufferCurrentPosition);
00894 
00895             /* Look up the memory location details */
00896             blockDetails details;
00897             lookupBlockDetails(locationID, &details);
00898 
00899             /* Attempt to set the value */
00900             unsigned short errorID = setPagedTwoDTableCellValue(details.RAMPage, details.RAMAddress, cellIndex, cellValue);
00901             if(errorID != 0){
00902                 sendErrorInternal(errorID);
00903             }else{
00904                 sendAckIfRequired();
00905             }
00906             break;
00907         }
00908         case requestBasicDatalog:
00909         {
00910             if((RXCalculatedPayloadLength > 2) || (RXCalculatedPayloadLength == 1)){
00911                 sendErrorInternal(payloadLengthTypeMismatch);
00912                 break;
00913             }else if(RXCalculatedPayloadLength == 2){
00914                 unsigned short newConfiguredLength = *((unsigned short*)RXBufferCurrentPosition);
00915                 if(newConfiguredLength > maxBasicDatalogLength){
00916                     sendErrorInternal(datalogLengthExceedsMax);
00917                     break;
00918                 }else{
00919                     configuredBasicDatalogLength = newConfiguredLength;
00920                 }
00921             }
00922 
00923             /* Set the length field up */
00924             *TXHeaderFlags |= HEADER_HAS_LENGTH;
00925             *(unsigned short*)TXBufferCurrentPositionHandler = configuredBasicDatalogLength;
00926 
00927             /* Fill out the log and send */
00928             populateBasicDatalog();
00929             checksumAndSend();
00930             break;
00931         }
00932         case requestConfigurableDatalog:
00933         {
00934             // perform function TODO
00935             sendErrorInternal(unimplementedFunction);
00936             break;
00937         }
00938         case forwardPacketOverCAN:
00939         {
00940             // perform function TODO
00941             sendErrorInternal(unimplementedFunction);
00942             break;
00943         }
00944         case forwardPacketOverOtherUART:
00945         {
00946             // perform function TODO
00947             sendErrorInternal(unimplementedFunction);
00948             break;
00949         }
00950         case setAsyncDatalogType:
00951         {
00952             if(RXCalculatedPayloadLength != 1){
00953                 sendErrorInternal(payloadLengthTypeMismatch);
00954                 break;
00955             }
00956 
00957             unsigned char newDatalogType = *((unsigned char*)RXBufferCurrentPosition);
00958             if(newDatalogType > 0x03){
00959                 sendErrorInternal(noSuchAsyncDatalogType);
00960                 break;
00961             }else{
00962                 asyncDatalogType = newDatalogType;
00963             }
00964 
00965             sendAckIfRequired();
00966             break;
00967         }
00968         default:
00969             if((RXHeaderPayloadID % 2) == 1){
00970                 sendErrorInternal(invalidFirmwarePayloadID);
00971             }else{
00972                 sendErrorInternal(unrecognisedFirmwarePayloadID);
00973             }
00974         }
00975     }
00976     /* Switch reception back on now that we are done with the received data */
00977     resetReceiveState(CLEAR_ALL_SOURCE_ID_FLAGS);
00978     PORTK |= BIT0;
00979 }

Here is the call graph for this function:

EXTERN void populateBasicDatalog ( void   ) 

Populate a basic datalog packet.

Copies various chunks of data to the transmission buffer and truncates to the configured length. If changing this, update the maxBasicDatalogLength.

Author:
Fred Cooke
Warning:
This function is only a skeleton at this time.

Definition at line 63 of file commsCore.c.

References ADCArrays, configuredBasicDatalogLength, CoreVars, DerivedVars, and TXBufferCurrentPositionHandler.

Referenced by decodePacketAndRespond(), and main().

00063                            {
00064     /* Save the current position */
00065     unsigned char* position = TXBufferCurrentPositionHandler;
00066 
00067     /* Get core vars */
00068     memcpy(TXBufferCurrentPositionHandler, CoreVars, sizeof(CoreVar));
00069     TXBufferCurrentPositionHandler += sizeof(CoreVar);
00070     /* Get derived vars */
00071     memcpy(TXBufferCurrentPositionHandler, DerivedVars, sizeof(DerivedVar));
00072     TXBufferCurrentPositionHandler += sizeof(DerivedVar);
00073     /* Get raw adc counts */
00074     memcpy(TXBufferCurrentPositionHandler, ADCArrays, sizeof(ADCArray));
00075     TXBufferCurrentPositionHandler += sizeof(ADCArray);
00076 
00077     /* Set/Truncate the log to the specified length */
00078     TXBufferCurrentPositionHandler = position + configuredBasicDatalogLength;
00079 }

EXTERN void resetReceiveState ( unsigned char  sourceIDState  ) 

Reset Receive State.

Reset communications reception to the state provided.

Author:
Fred Cooke
Todo:
TODO this is in the wrong file!! Either move the header declaration or move the function!

TODO CAN0CTL1 &= CANCTL1_RX_ISR_DISABLE;

TODO CAN0CTL1 |= CANCTL1_RX_ISR_ENABLE;

Parameters:
sourceIDState is the state to apply to the RX buffer state variable.

Todo:
TODO CAN0CTL1 &= CANCTL1_RX_DISABLE;

Todo:
TODO CAN0CTL1 |= CANCTL1_RX_ENABLE;

Definition at line 99 of file commsISRs.c.

References COM_SET_CAN0_INTERFACE_ID, COM_SET_SCI0_INTERFACE_ID, RXBufferContentSourceID, RXBufferCurrentPosition, RXCalculatedChecksum, RXPacketLengthReceived, RXStateFlags, SCI0CR2, SCICR2_RX_DISABLE, SCICR2_RX_ENABLE, SCICR2_RX_ISR_DISABLE, and SCICR2_RX_ISR_ENABLE.

Referenced by decodePacketAndRespond(), and SCI0ISR().

00099                                                    {
00100     /* Set the receive buffer pointer to the beginning */
00101     RXBufferCurrentPosition = (unsigned char*)&RXBuffer;
00102 
00103     /* Zero the flags, buffer length and checksum */
00104     RXPacketLengthReceived = 0;
00105     RXCalculatedChecksum = 0;
00106     RXStateFlags = 0;
00107 
00108     /* Set the source ID state (clear all or all but one flag(s)) */
00109     RXBufferContentSourceID = sourceIDState;
00110 
00111     /* Which ever interface we are setting is the one we came from. By definition   */
00112     /* it must be on and we want it to stay on, so just turn off all the others.    */
00113     if(sourceIDState & COM_SET_SCI0_INTERFACE_ID){
00114         /* Turn off all others here */
00117         /* SPI ? I2C ? SCI1 ? */
00118     }else if(sourceIDState & COM_SET_CAN0_INTERFACE_ID){
00119         /* Turn off all others here */
00120         /* Only SCI for now */
00121         SCI0CR2 &= SCICR2_RX_DISABLE;
00122         SCI0CR2 &= SCICR2_RX_ISR_DISABLE;
00123         /* SPI ? I2C ? SCI1 ? */
00124     }else{ /* If clearing all flags then enable RX on all interfaces */
00125         /* Only SCI for now */
00126         SCI0CR2 |= SCICR2_RX_ENABLE;
00127         SCI0CR2 |= SCICR2_RX_ISR_ENABLE;
00130         /* SPI ? I2C ? SCI1 ? */
00131     }
00132 }

EXTERN void sendAckIfRequired ( void   ) 

Send an ack if required.

Currently only used to clear the TX buffer flags if we no longer need it.

Author:
Fred Cooke
Todo:
TODO when implementing, check that ppage is OK!!!

Definition at line 1256 of file commsCore.c.

References TXBufferInUseFlags.

Referenced by decodePacketAndRespond().

01256                         {
01257     TXBufferInUseFlags = 0;
01258     // check PPAGE while implementing TODO
01259 }

EXTERN void sendDebugBusyWait ( unsigned char *  message  ) 

Send a debug message even if we must wait

This is a wrapper for use outside the communication handler function. This function will block until the debug message is able to be sent.

Author:
Fred Cooke
Note:
This function exists as a convenience to developers, do not publish code that calls this function.
Parameters:
message is a pointer to the null terminated debug message string.

Definition at line 1112 of file commsCore.c.

References ONES, sendDebugInternal(), and TXBufferInUseFlags.

01112                                               {
01113     while(TXBufferInUseFlags){} /* Wait till clear to send */
01114     TXBufferInUseFlags = ONES;
01115     sendDebugInternal(message);
01116 }

Here is the call graph for this function:

EXTERN void sendDebugIfClear ( unsigned char *  message  ) 

Send a debug message if buffer free.

This is a wrapper for use outside the communication handler function. The debug message will only be sent if the buffer is empty and available, if not, it will be discarded.

Author:
Fred Cooke
Note:
This function exists as a convenience to developers, do not publish code that calls this function.
Parameters:
message is a pointer to the null terminated debug message string.

Definition at line 1091 of file commsCore.c.

References Counter::commsDebugMessagesNotSent, Counters, ONES, sendDebugInternal(), and TXBufferInUseFlags.

01091                                              {
01092     if(!TXBufferInUseFlags){
01093         TXBufferInUseFlags = ONES;
01094         sendDebugInternal(message);
01095     }else{
01096         Counters.commsDebugMessagesNotSent++;
01097     }
01098 }

Here is the call graph for this function:

EXTERN void sendErrorBusyWait ( unsigned short  errorID  ) 

Send an error even if we must wait.

This is a wrapper for use outside the communication handler function. This function will block until the error is able to be sent. This behaviour is not recommended as it will interfere with engine operation somewhat.

Author:
Fred Cooke
Warning:
Use of this function signifies that the error you are trying to propagate is extremely urgent and can not be forgotten.
Note:
Using this function blocks other main loop code from execution. Consider handling the error in another way if it seems appropriate to use this.
Parameters:
errorID is the error ID to be passed out to listening devices.

Definition at line 1018 of file commsCore.c.

References ONES, sendErrorInternal(), and TXBufferInUseFlags.

01018                                               {
01019     while(TXBufferInUseFlags){} /* Wait till clear to send */
01020     TXBufferInUseFlags = ONES;
01021     sendErrorInternal(errorID);
01022 }

Here is the call graph for this function:

EXTERN void sendErrorIfClear ( unsigned short  errorID  ) 

Send an error if buffer free.

This is a wrapper for use outside the communication handler function. The error will only be sent if the buffer is empty and available, if not, it will be discarded.

Author:
Fred Cooke
Warning:
Use of this function signifies that the error you are trying to propagate is not urgent and can be forgotten.
Note:
Consider not throwing an error if it seems appropriate to use this.
Parameters:
errorID is the error ID to be passed out to listening devices.

Definition at line 994 of file commsCore.c.

References Counter::commsErrorMessagesNotSent, Counters, ONES, sendErrorInternal(), and TXBufferInUseFlags.

Referenced by generateCoreVars(), and generateDerivedVars().

00994                                              {
00995     if(!TXBufferInUseFlags){
00996         TXBufferInUseFlags = ONES;
00997         sendErrorInternal(errorID);
00998     }else{
00999         Counters.commsErrorMessagesNotSent++;
01000     }
01001 }

Here is the call graph for this function:


Variable Documentation

EXTERN unsigned char RXBufferContentSourceID

Definition at line 79 of file commsCore.h.

Referenced by resetReceiveState(), and SCI0ISR().

EXTERN unsigned char* RXBufferCurrentPosition

EXTERN unsigned short RXCalculatedPayloadLength

Definition at line 104 of file commsCore.h.

Referenced by decodePacketAndRespond().

EXTERN unsigned char RXHeaderFlags

Definition at line 142 of file commsCore.h.

Referenced by decodePacketAndRespond().

EXTERN unsigned short RXHeaderPayloadID

Definition at line 143 of file commsCore.h.

Referenced by decodePacketAndRespond().

EXTERN unsigned short RXHeaderPayloadLength

Definition at line 145 of file commsCore.h.

Referenced by decodePacketAndRespond().

EXTERN unsigned char RXHeaderSourceAddress

Definition at line 144 of file commsCore.h.

Referenced by decodePacketAndRespond().

EXTERN unsigned short RXPacketLengthReceived

EXTERN unsigned char RXStateFlags

Definition at line 101 of file commsCore.h.

Referenced by main(), resetReceiveState(), and SCI0ISR().

EXTERN unsigned char* TXBufferCurrentPositionCAN0

Definition at line 73 of file commsCore.h.

Referenced by decodePacketAndRespond(), and main().

EXTERN unsigned char* TXBufferCurrentPositionHandler

EXTERN unsigned char* TXBufferCurrentPositionSCI0

Definition at line 74 of file commsCore.h.

Referenced by decodePacketAndRespond(), main(), SCI0ISR(), and sendAndIncrement().

EXTERN unsigned char TXBufferInUseFlags

EXTERN unsigned short TXPacketLengthToSendCAN0

Definition at line 70 of file commsCore.h.

Referenced by checksumAndSend().

EXTERN unsigned short TXPacketLengthToSendSCI0

Definition at line 71 of file commsCore.h.

Referenced by checksumAndSend(), SCI0ISR(), and sendAndIncrement().


Generated on Mon Jan 26 00:16:22 2009 for FreeEMS by  doxygen 1.5.8