00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00044 #define COMMSCORE_C
00045 #include "inc/freeEMS.h"
00046 #include "inc/flashWrite.h"
00047 #include "inc/interrupts.h"
00048 #include "inc/utils.h"
00049 #include "inc/tableLookup.h"
00050 #include "inc/blockDetailsLookup.h"
00051 #include "inc/commsCore.h"
00052 #include <string.h>
00053
00054
00064 void populateBasicDatalog(){
00065
00066 unsigned char* position = TXBufferCurrentPositionHandler;
00067
00068 DerivedVars->sp5++;
00069
00070
00071 memcpy(TXBufferCurrentPositionHandler, CoreVars, sizeof(CoreVar));
00072 TXBufferCurrentPositionHandler += sizeof(CoreVar);
00073
00074 memcpy(TXBufferCurrentPositionHandler, DerivedVars, sizeof(DerivedVar));
00075 TXBufferCurrentPositionHandler += sizeof(DerivedVar);
00076
00077 memcpy(TXBufferCurrentPositionHandler, ADCArrays, sizeof(ADCArray));
00078 TXBufferCurrentPositionHandler += sizeof(ADCArray);
00079
00080
00081 TXBufferCurrentPositionHandler = position + configuredBasicDatalogLength;
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00149 void checksumAndSend(){
00150
00151 unsigned short TXPacketLengthToSend = (unsigned short)TXBufferCurrentPositionHandler - (unsigned short)&TXBuffer;
00152
00153
00154 *TXBufferCurrentPositionHandler = checksum((unsigned char*)&TXBuffer, TXPacketLengthToSend);
00155 TXPacketLengthToSend++;
00156
00157
00158
00159
00160 if(TXBufferInUseFlags & COM_SET_SCI0_INTERFACE_ID){
00161
00162 TXPacketLengthToSendSCI0 = TXPacketLengthToSend;
00163 TXPacketLengthToSendCAN0 = TXPacketLengthToSend;
00164
00165
00166
00167
00168
00169
00170
00171 SCI0DRL = START_BYTE;
00172 while(!(SCI0SR1 & 0x80)){}
00173 SCI0DRL = START_BYTE;
00174
00175
00176
00177 SCI0CR2 |= SCICR2_TX_ISR_ENABLE;
00178 }
00179
00180 if(TXBufferInUseFlags & COM_SET_CAN0_INTERFACE_ID){
00181
00182 TXBufferInUseFlags &= COM_CLEAR_CAN0_INTERFACE_ID;
00183 }
00184
00185 if(TXBufferInUseFlags & COM_SET_SPARE2_INTERFACE_ID){
00186
00187 TXBufferInUseFlags &= COM_CLEAR_SPARE2_INTERFACE_ID;
00188 }
00189
00190 if(TXBufferInUseFlags & COM_SET_SPARE3_INTERFACE_ID){
00191
00192 TXBufferInUseFlags &= COM_CLEAR_SPARE3_INTERFACE_ID;
00193 }
00194
00195 if(TXBufferInUseFlags & COM_SET_SPARE4_INTERFACE_ID){
00196
00197 TXBufferInUseFlags &= COM_CLEAR_SPARE4_INTERFACE_ID;
00198 }
00199
00200 if(TXBufferInUseFlags & COM_SET_SPARE5_INTERFACE_ID){
00201
00202 TXBufferInUseFlags &= COM_CLEAR_SPARE5_INTERFACE_ID;
00203 }
00204
00205 if(TXBufferInUseFlags & COM_SET_SPARE6_INTERFACE_ID){
00206
00207 TXBufferInUseFlags &= COM_CLEAR_SPARE6_INTERFACE_ID;
00208 }
00209
00210 if(TXBufferInUseFlags & COM_SET_SPARE7_INTERFACE_ID){
00211
00212 TXBufferInUseFlags &= COM_CLEAR_SPARE7_INTERFACE_ID;
00213 }
00214 }
00215
00216
00225 void decodePacketAndRespond(){
00226
00227 RXBufferCurrentPosition = (unsigned char*)&RXBuffer;
00228 TXBufferCurrentPositionHandler = (unsigned char*)&TXBuffer;
00229
00230
00231 TXBufferCurrentPositionSCI0 = (unsigned char*)&TXBuffer;
00232 TXBufferCurrentPositionCAN0 = (unsigned char*)&TXBuffer;
00233
00234
00235 RXCalculatedPayloadLength = RXPacketLengthReceived;
00236
00237
00238 RXHeaderFlags = *RXBufferCurrentPosition;
00239 RXBufferCurrentPosition++;
00240 RXCalculatedPayloadLength--;
00241
00242
00243 TXBufferInUseFlags |= COM_SET_SCI0_INTERFACE_ID;
00244
00245
00246
00247 unsigned char* TXHeaderFlags = TXBufferCurrentPositionHandler;
00248 *TXHeaderFlags = 0;
00249 TXBufferCurrentPositionHandler++;
00250
00251
00252 RXHeaderPayloadID = *((unsigned short*)RXBufferCurrentPosition);
00253 *((unsigned short*)TXBufferCurrentPositionHandler) = RXHeaderPayloadID + 1;
00254 RXBufferCurrentPosition += 2;
00255 TXBufferCurrentPositionHandler += 2;
00256 RXCalculatedPayloadLength -= 2;
00257
00258
00259 if(RXHeaderFlags & HEADER_HAS_ACK){
00260 *TXBufferCurrentPositionHandler = *RXBufferCurrentPosition;
00261 *TXHeaderFlags |= HEADER_HAS_ACK;
00262 RXBufferCurrentPosition++;
00263 TXBufferCurrentPositionHandler++;
00264 RXCalculatedPayloadLength--;
00265 }
00266
00267
00268 if(RXHeaderFlags & HEADER_HAS_ADDRS){
00269
00270 if(*RXBufferCurrentPosition != fixedConfigs1.serialSettings.networkAddress){
00271
00272 resetReceiveState(CLEAR_ALL_SOURCE_ID_FLAGS);
00273 TXBufferInUseFlags = 0;
00274 return;
00275 }
00276 RXBufferCurrentPosition++;
00277
00278
00279 RXHeaderSourceAddress = *RXBufferCurrentPosition;
00280 RXBufferCurrentPosition++;
00281 if(RXHeaderSourceAddress == 0){
00282 sendErrorInternal(sourceAddressIsBroadcast);
00283 resetReceiveState(CLEAR_ALL_SOURCE_ID_FLAGS);
00284 return;
00285 }
00286 if(RXHeaderSourceAddress == fixedConfigs1.serialSettings.networkAddress){
00287 sendErrorInternal(sourceAddressIsDuplicate);
00288 resetReceiveState(CLEAR_ALL_SOURCE_ID_FLAGS);
00289 return;
00290 }
00291
00292
00293 *TXHeaderFlags |= HEADER_HAS_ADDRS;
00294
00295 *TXBufferCurrentPositionHandler = RXHeaderSourceAddress;
00296 TXBufferCurrentPositionHandler++;
00297
00298 *TXBufferCurrentPositionHandler = fixedConfigs1.serialSettings.networkAddress;
00299 TXBufferCurrentPositionHandler++;
00300
00301 RXCalculatedPayloadLength -= 2;
00302 }
00303
00304
00305 RXCalculatedPayloadLength--;
00306
00307
00308 if(RXHeaderFlags & HEADER_HAS_LENGTH){
00309 RXHeaderPayloadLength = *((unsigned short*)RXBufferCurrentPosition);
00310 RXBufferCurrentPosition += 2;
00311 RXCalculatedPayloadLength -= 2;
00312
00313 if(RXHeaderPayloadLength != RXCalculatedPayloadLength){
00314 sendErrorInternal(payloadLengthHeaderMismatch);
00315 resetReceiveState(CLEAR_ALL_SOURCE_ID_FLAGS);
00316 return;
00317 }
00318 }
00319
00320
00321
00322
00323
00324
00325
00326 switch (RXHeaderPayloadID){
00327 case requestInterfaceVersion:
00328 {
00329 if(RXCalculatedPayloadLength != 0){
00330 sendErrorInternal(payloadLengthTypeMismatch);
00331 break;
00332 }
00333
00334
00335 *((unsigned short*)TXBufferCurrentPositionHandler) = sizeof(interfaceVersionAndType);
00336 *TXHeaderFlags |= HEADER_HAS_LENGTH;
00337 TXBufferCurrentPositionHandler += 2;
00338
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
00351 *((unsigned short*)TXBufferCurrentPositionHandler) = sizeof(firmwareVersion);
00352 *TXHeaderFlags |= HEADER_HAS_LENGTH;
00353 TXBufferCurrentPositionHandler += 2;
00354
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
00367 *((unsigned short*)TXBufferCurrentPositionHandler) = RX_BUFFER_SIZE;
00368 TXBufferCurrentPositionHandler += 2;
00369 checksumAndSend();
00370 break;
00371 }
00372 case requestEchoPacketReturn:
00373 {
00374
00375 *((unsigned short*)TXBufferCurrentPositionHandler) = RXPacketLengthReceived;
00376 *TXHeaderFlags |= HEADER_HAS_LENGTH;
00377 TXBufferCurrentPositionHandler += 2;
00378
00379 memcpy((void*)TXBufferCurrentPositionHandler, (void*)&RXBuffer, RXPacketLengthReceived);
00380
00381
00382 TXBufferCurrentPositionHandler += RXPacketLengthReceived;
00383 checksumAndSend();
00384 break;
00385 }
00386 case requestSoftSystemReset:
00387 {
00388 if(RXCalculatedPayloadLength != 0){
00389 sendErrorInternal(payloadLengthTypeMismatch);
00390 break;
00391 }
00392
00393 _start();
00394 }
00395 case requestHardSystemReset:
00396 {
00397 if(RXCalculatedPayloadLength != 0){
00398 sendErrorInternal(payloadLengthTypeMismatch);
00399 break;
00400 }
00401
00402
00403 COPCTL = 0x01;
00404 ARMCOP = 0xFF;
00405
00406
00407 }
00408 case replaceBlockInRAM:
00409 {
00410
00411 unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00412 RXBufferCurrentPosition += 2;
00413
00414
00415 blockDetails details;
00416 lookupBlockDetails(locationID, &details);
00417
00418
00419 if((RXCalculatedPayloadLength - 2) != details.size){
00420 sendErrorInternal(payloadLengthTypeMismatch);
00421 break;
00422 }
00423
00424 if((details.RAMPage == 0) || (details.RAMAddress == 0)){
00425 sendErrorInternal(invalidMemoryActionForID);
00426 break;
00427 }
00428
00429
00430 unsigned short errorID = 0;
00431 if(locationID < 16){
00432 errorID = validateMainTable((mainTable*)RXBufferCurrentPosition);
00433 }else if((locationID > 399) && (locationID < 900)){
00434 errorID = validateTwoDTable((twoDTableUS*)RXBufferCurrentPosition);
00435 }
00436
00437 if(errorID != 0){
00438 sendErrorInternal(errorID);
00439 break;
00440 }
00441
00442
00443 unsigned char oldRamPage = RPAGE;
00444
00445 RPAGE = details.RAMPage;
00446
00447 memcpy(details.RAMAddress, RXBufferCurrentPosition, details.size);
00448
00449 unsigned char index = compare(RXBufferCurrentPosition, details.RAMAddress, details.size);
00450
00451 RPAGE = oldRamPage;
00452
00453 if(index != 0){
00454 sendErrorInternal(MEMORY_WRITE_ERROR);
00455 }else{
00456 sendErrorInternal(NO_PROBLEMO);
00457 }
00458 break;
00459 }
00460 case replaceBlockInFlash:
00461 {
00462
00463 unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00464 RXBufferCurrentPosition += 2;
00465
00466
00467 blockDetails details;
00468 lookupBlockDetails(locationID, &details);
00469
00470
00471 if((RXCalculatedPayloadLength - 2) != details.size){
00472 sendErrorInternal(payloadLengthTypeMismatch);
00473 break;
00474 }
00475
00476 if((details.FlashPage == 0) || (details.FlashAddress == 0)){
00477 sendErrorInternal(invalidMemoryActionForID);
00478 break;
00479 }
00480
00481
00482 unsigned short errorID = 0;
00483 if(locationID < 16){
00484 errorID = validateMainTable((mainTable*)RXBufferCurrentPosition);
00485 }else if((locationID > 399) && (locationID < 900)){
00486 errorID = validateTwoDTable((twoDTableUS*)RXBufferCurrentPosition);
00487 }
00488
00489 if(errorID != 0){
00490 sendErrorInternal(errorID);
00491 break;
00492 }
00493
00494
00495 void* buffer = (void*)((unsigned short)&RXBuffer + RXPacketLengthReceived);
00496
00497
00498 unsigned char originalRAMPage = details.RAMPage;
00499 void* originalRAMAddress = details.RAMAddress;
00500 details.RAMPage = RPAGE;
00501 details.RAMAddress = RXBufferCurrentPosition;
00502
00503
00504 errorID = writeBlock(&details, buffer);
00505 if(errorID != 0){
00506 sendErrorInternal(errorID);
00507 break;
00508 }
00509
00510
00511 if((originalRAMPage != 0) && (originalRAMAddress != 0)){
00512
00513 unsigned char oldRamPage = RPAGE;
00514
00515 RPAGE = originalRAMPage;
00516
00517 memcpy(originalRAMAddress, RXBufferCurrentPosition, details.size);
00518
00519 unsigned char index = compare(RXBufferCurrentPosition, details.RAMAddress, details.size);
00520
00521 RPAGE = oldRamPage;
00522
00523 if(index != 0){
00524 sendErrorInternal(MEMORY_WRITE_ERROR);
00525 break;
00526 }
00527 }
00528
00529 sendErrorInternal(NO_PROBLEMO);
00530
00531 break;
00532 }
00533 case retrieveBlockFromRAM:
00534 {
00535 if(RXCalculatedPayloadLength != 2){
00536 sendErrorInternal(payloadLengthTypeMismatch);
00537 break;
00538 }
00539
00540
00541 unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00542
00543 *(unsigned short*)TXBufferCurrentPositionHandler = locationID;
00544 TXBufferCurrentPositionHandler += 2;
00545
00546
00547 if(locationID < 16){
00548
00549 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_RPM_LENGTH;
00550 TXBufferCurrentPositionHandler += 2;
00551
00552 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_LOAD_LENGTH;
00553 TXBufferCurrentPositionHandler += 2;
00554
00555 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_MAIN_LENGTH;
00556 TXBufferCurrentPositionHandler += 2;
00557 }
00558
00559
00560 blockDetails details;
00561 lookupBlockDetails(locationID, &details);
00562
00563 if((details.RAMPage == 0) || (details.RAMAddress == 0)){
00564 sendErrorInternal(invalidMemoryActionForID);
00565 break;
00566 }
00567
00568
00569 unsigned char oldRamPage = RPAGE;
00570 RPAGE = details.RAMPage;
00571
00572
00573 memcpy(TXBufferCurrentPositionHandler, details.RAMAddress, details.size);
00574 TXBufferCurrentPositionHandler += details.size;
00575
00576
00577 RPAGE = oldRamPage;
00578
00579 checksumAndSend();
00580 break;
00581 }
00582 case retrieveBlockFromFlash:
00583 {
00584 if(RXCalculatedPayloadLength != 2){
00585 sendErrorInternal(payloadLengthTypeMismatch);
00586 break;
00587 }
00588
00589
00590 unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00591
00592 *(unsigned short*)TXBufferCurrentPositionHandler = locationID;
00593 TXBufferCurrentPositionHandler += 2;
00594
00595
00596 if(locationID < 16){
00597
00598 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_RPM_LENGTH;
00599 TXBufferCurrentPositionHandler += 2;
00600
00601 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_LOAD_LENGTH;
00602 TXBufferCurrentPositionHandler += 2;
00603
00604 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_MAIN_LENGTH;
00605 TXBufferCurrentPositionHandler += 2;
00606 }
00607
00608
00609 blockDetails details;
00610 lookupBlockDetails(locationID, &details);
00611
00612 if((details.FlashPage == 0) || (details.FlashAddress == 0)){
00613 sendErrorInternal(invalidMemoryActionForID);
00614 break;
00615 }
00616
00617
00618 unsigned char oldFlashPage = PPAGE;
00619 PPAGE = details.FlashPage;
00620
00621
00622 memcpy(TXBufferCurrentPositionHandler, details.FlashAddress, details.size);
00623 TXBufferCurrentPositionHandler += details.size;
00624
00625
00626 PPAGE = oldFlashPage;
00627
00628 checksumAndSend();
00629 break;
00630 }
00631 case burnBlockFromRamToFlash:
00632 {
00633 if(RXCalculatedPayloadLength != 2){
00634 sendErrorInternal(payloadLengthTypeMismatch);
00635 break;
00636 }
00637
00638
00639 unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00640
00641
00642 blockDetails details;
00643 lookupBlockDetails(locationID, &details);
00644
00645
00646 if((details.RAMPage == 0) || (details.RAMAddress == 0) || (details.FlashPage == 0) || (details.FlashAddress == 0)){
00647 sendErrorInternal(invalidMemoryActionForID);
00648 break;
00649 }
00650
00651
00652 void* buffer = (void*)((unsigned short)&RXBuffer + RXPacketLengthReceived);
00653
00654
00655 unsigned short errorID = writeBlock(&details, buffer);
00656
00657 if(errorID != 0){
00658 sendErrorInternal(errorID);
00659 break;
00660 }
00661
00662 sendErrorInternal(NO_PROBLEMO);
00663 break;
00664 }
00665 case eraseAllBlocksFromFlash:
00666 {
00667 if(RXCalculatedPayloadLength != 0){
00668 sendErrorInternal(payloadLengthTypeMismatch);
00669 break;
00670 }
00671
00672
00673 unsigned char page = 0xE0;
00674 unsigned short start = 0x8000;
00675 unsigned short end = 0xC000;
00676 unsigned short inc = 0x0400;
00677 for(;page < 0xF8;page++){
00678 unsigned short addr;
00679 for(addr = start;addr < end; addr += inc){
00680
00681 eraseSector(page, (unsigned short*)addr);
00682 }
00683 }
00684 sendDebugInternal("Erased three 128k Flash blocks!");
00685 break;
00686 }
00687 case burnAllBlocksOfFlash:
00688 {
00689 if(RXCalculatedPayloadLength != 0){
00690 sendErrorInternal(payloadLengthTypeMismatch);
00691 break;
00692 }
00693
00694
00695 unsigned char page = 0xE0;
00696 unsigned short start = 0x8000;
00697 unsigned short end = 0xC000;
00698 unsigned short inc = 0x0400;
00699 for(;page < 0xF8;page++){
00700 unsigned short addr;
00701 for(addr = start;addr < end; addr += inc){
00702 writeSector(RPAGE, (unsigned short*)0xc000, page, (unsigned short*)addr);
00703 }
00704 }
00705 sendDebugInternal("Overwrote three 128k Flash blocks!");
00706 break;
00707 }
00708 case adjustMainTableCell:
00709 {
00710 if(RXCalculatedPayloadLength != 8){
00711 sendErrorInternal(payloadLengthTypeMismatch);
00712 break;
00713 }
00714
00715
00716 unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00717 RXBufferCurrentPosition += 2;
00718
00719
00720 if(15 < locationID){
00721 sendErrorInternal(invalidIDForMainTableAction);
00722 break;
00723 }
00724
00725
00726 unsigned short RPMIndex = *((unsigned short*)RXBufferCurrentPosition);
00727 RXBufferCurrentPosition += 2;
00728 unsigned short LoadIndex = *((unsigned short*)RXBufferCurrentPosition);
00729 RXBufferCurrentPosition += 2;
00730 unsigned short cellValue = *((unsigned short*)RXBufferCurrentPosition);
00731
00732
00733 blockDetails details;
00734 lookupBlockDetails(locationID, &details);
00735
00736
00737 unsigned short errorID = setPagedMainTableCellValue(details.RAMPage, details.RAMAddress, RPMIndex, LoadIndex, cellValue);
00738 if(errorID != 0){
00739 sendErrorInternal(errorID);
00740 }else{
00741 sendErrorInternal(NO_PROBLEMO);
00742 }
00743 break;
00744 }
00745 case adjustMainTableRPMAxis:
00746 {
00747 if(RXCalculatedPayloadLength != 6){
00748 sendErrorInternal(payloadLengthTypeMismatch);
00749 break;
00750 }
00751
00752
00753 unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00754 RXBufferCurrentPosition -= 2;
00755
00756
00757 if(15 < locationID){
00758 sendErrorInternal(invalidIDForMainTableAction);
00759 break;
00760 }
00761
00762
00763 unsigned short RPMIndex = *((unsigned short*)RXBufferCurrentPosition);
00764 RXBufferCurrentPosition -= 2;
00765 unsigned short RPMValue = *((unsigned short*)RXBufferCurrentPosition);
00766
00767
00768 blockDetails details;
00769 lookupBlockDetails(locationID, &details);
00770
00771
00772 unsigned short errorID = setPagedMainTableRPMValue(details.RAMPage, details.RAMAddress, RPMIndex, RPMValue);
00773 if(errorID != 0){
00774 sendErrorInternal(errorID);
00775 }else{
00776 sendErrorInternal(NO_PROBLEMO);
00777 }
00778 break;
00779 }
00780 case adjustMainTableLoadAxis:
00781 {
00782 if(RXCalculatedPayloadLength != 6){
00783 sendErrorInternal(payloadLengthTypeMismatch);
00784 break;
00785 }
00786
00787
00788 unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00789 RXBufferCurrentPosition -= 2;
00790
00791
00792 if(15 < locationID){
00793 sendErrorInternal(invalidIDForMainTableAction);
00794 break;
00795 }
00796
00797
00798 unsigned short LoadIndex = *((unsigned short*)RXBufferCurrentPosition);
00799 RXBufferCurrentPosition -= 2;
00800 unsigned short LoadValue = *((unsigned short*)RXBufferCurrentPosition);
00801
00802
00803 blockDetails details;
00804 lookupBlockDetails(locationID, &details);
00805
00806
00807 unsigned short errorID = setPagedMainTableLoadValue(details.RAMPage, details.RAMAddress, LoadIndex, LoadValue);
00808 if(errorID != 0){
00809 sendErrorInternal(errorID);
00810 }else{
00811 sendErrorInternal(NO_PROBLEMO);
00812 }
00813 break;
00814 }
00815 case adjust2dTableAxis:
00816 {
00817 if(RXCalculatedPayloadLength != 6){
00818 sendErrorInternal(payloadLengthTypeMismatch);
00819 break;
00820 }
00821
00822
00823 unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00824 RXBufferCurrentPosition -= 2;
00825
00826
00827 if((locationID > 899) || (locationID < 400)){
00828 sendErrorInternal(invalidIDForTwoDTableAction);
00829 break;
00830 }
00831
00832
00833 unsigned short axisIndex = *((unsigned short*)RXBufferCurrentPosition);
00834 RXBufferCurrentPosition -= 2;
00835 unsigned short axisValue = *((unsigned short*)RXBufferCurrentPosition);
00836
00837
00838 blockDetails details;
00839 lookupBlockDetails(locationID, &details);
00840
00841
00842 unsigned short errorID = setPagedTwoDTableAxisValue(details.RAMPage, details.RAMAddress, axisIndex, axisValue);
00843 if(errorID != 0){
00844 sendErrorInternal(errorID);
00845 }else{
00846 sendErrorInternal(NO_PROBLEMO);
00847 }
00848 break;
00849 }
00850 case adjust2dTableCell:
00851 {
00852 if(RXCalculatedPayloadLength != 6){
00853 sendErrorInternal(payloadLengthTypeMismatch);
00854 break;
00855 }
00856
00857
00858 unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00859 RXBufferCurrentPosition -= 2;
00860
00861
00862 if((locationID > 899) || (locationID < 400)){
00863 sendErrorInternal(invalidIDForTwoDTableAction);
00864 break;
00865 }
00866
00867
00868 unsigned short cellIndex = *((unsigned short*)RXBufferCurrentPosition);
00869 RXBufferCurrentPosition -= 2;
00870 unsigned short cellValue = *((unsigned short*)RXBufferCurrentPosition);
00871
00872
00873 blockDetails details;
00874 lookupBlockDetails(locationID, &details);
00875
00876
00877 unsigned short errorID = setPagedTwoDTableCellValue(details.RAMPage, details.RAMAddress, cellIndex, cellValue);
00878 if(errorID != 0){
00879 sendErrorInternal(errorID);
00880 }else{
00881 sendErrorInternal(NO_PROBLEMO);
00882 }
00883 break;
00884 }
00885 case requestBasicDatalog:
00886 {
00887 if((RXCalculatedPayloadLength > 2) || (RXCalculatedPayloadLength == 1)){
00888 sendErrorInternal(payloadLengthTypeMismatch);
00889 break;
00890 }else if(RXCalculatedPayloadLength == 2){
00891 unsigned short newConfiguredLength = *((unsigned short*)RXBufferCurrentPosition);
00892 if(newConfiguredLength > maxBasicDatalogLength){
00893 sendErrorInternal(datalogLengthExceedsMax);
00894 break;
00895 }else{
00896 configuredBasicDatalogLength = newConfiguredLength;
00897 }
00898 }
00899
00900
00901 *TXHeaderFlags |= HEADER_HAS_LENGTH;
00902 *(unsigned short*)TXBufferCurrentPositionHandler = configuredBasicDatalogLength;
00903
00904
00905 populateBasicDatalog();
00906 checksumAndSend();
00907 break;
00908 }
00909 case requestConfigurableDatalog:
00910 {
00911
00912 sendErrorInternal(unimplementedFunction);
00913 break;
00914 }
00915 case setAsyncDatalogType:
00916 {
00917 if(RXCalculatedPayloadLength != 1){
00918 sendErrorInternal(payloadLengthTypeMismatch);
00919 break;
00920 }
00921
00922 unsigned char newDatalogType = *((unsigned char*)RXBufferCurrentPosition);
00923 if(newDatalogType > 0x01){
00924 sendErrorInternal(noSuchAsyncDatalogType);
00925 break;
00926 }else{
00927 asyncDatalogType = newDatalogType;
00928 }
00929
00930 sendErrorInternal(NO_PROBLEMO);
00931 break;
00932 }
00933 case forwardPacketOverCAN:
00934 {
00935
00936 sendErrorInternal(unimplementedFunction);
00937 break;
00938 }
00939 case forwardPacketOverOtherUART:
00940 {
00941
00942 sendErrorInternal(unimplementedFunction);
00943 break;
00944 }
00945 case retrieveArbitraryMemory:
00946 {
00947 if(RXCalculatedPayloadLength != 6){
00948 sendErrorInternal(payloadLengthTypeMismatch);
00949 break;
00950 }
00951
00952 unsigned short length = *((unsigned short*)RXBufferCurrentPosition);
00953 RXBufferCurrentPosition += 2;
00954
00955 if(length > TX_MAX_PAYLOAD_SIZE){
00956 sendErrorInternal(requestedLengthTooLarge);
00957 break;
00958 }
00959
00960 void* address = (void*) *((unsigned short*)RXBufferCurrentPosition);
00961 RXBufferCurrentPosition += 2;
00962
00963 if(((unsigned short)address) <= ((0xFFFF - length) + 1)){
00964
00965 sendErrorInternal(requestedAddressDisallowed);
00966 break;
00967 }
00968
00969 unsigned char RAMPage = *((unsigned char*)RXBufferCurrentPosition);
00970 RXBufferCurrentPosition++;
00971
00972 if(RAMPage < RPAGE_MIN){
00973 sendErrorInternal(requestedRAMPageInvalid);
00974 }
00975
00976 unsigned char FlashPage = *((unsigned char*)RXBufferCurrentPosition);
00977 RXBufferCurrentPosition++;
00978
00979 if(FlashPage < PPAGE_MIN){
00980 sendErrorInternal(requestedFlashPageInvalid);
00981 }
00982
00983
00984 *((unsigned short*)TXBufferCurrentPositionHandler) = length + 6;
00985 *TXHeaderFlags |= HEADER_HAS_LENGTH;
00986 TXBufferCurrentPositionHandler += 2;
00987
00988
00989 *((unsigned short*)TXBufferCurrentPositionHandler) = (unsigned short) address;
00990 TXBufferCurrentPositionHandler += 2;
00991 *((unsigned short*)TXBufferCurrentPositionHandler) = length;
00992 TXBufferCurrentPositionHandler += 2;
00993 *((unsigned char*)TXBufferCurrentPositionHandler) = RAMPage;
00994 TXBufferCurrentPositionHandler++;
00995 *((unsigned char*)TXBufferCurrentPositionHandler) = FlashPage;
00996 TXBufferCurrentPositionHandler++;
00997
00998
00999 memcpy((void*)TXBufferCurrentPositionHandler, address, length);
01000 TXBufferCurrentPositionHandler += length;
01001
01002 checksumAndSend();
01003 break;
01004 }
01005 default:
01006 {
01007 if((RXHeaderPayloadID % 2) == 1){
01008 sendErrorInternal(invalidPayloadID);
01009 }else{
01010 sendErrorInternal(unrecognisedPayloadID);
01011 }
01012 }
01013 }
01014
01015 resetReceiveState(CLEAR_ALL_SOURCE_ID_FLAGS);
01016 PORTK |= BIT0;
01017 }
01018
01019
01032 void sendErrorIfClear(unsigned short errorID){
01033 if(!TXBufferInUseFlags){
01034 TXBufferInUseFlags = ONES;
01035 sendErrorInternal(errorID);
01036 }else{
01037 Counters.commsErrorMessagesNotSent++;
01038 }
01039 }
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01082 void sendErrorInternal(unsigned short errorCode){
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103 TXBufferCurrentPositionHandler = (unsigned char*)&TXBuffer;
01104
01105
01106
01107 *TXBufferCurrentPositionHandler = 0x01;
01108 TXBufferCurrentPositionHandler++;
01109
01110 *((unsigned short*)TXBufferCurrentPositionHandler) = asyncErrorCodePacket;
01111 TXBufferCurrentPositionHandler += 2;
01112
01113 *((unsigned short*)TXBufferCurrentPositionHandler) = errorCode;
01114 TXBufferCurrentPositionHandler += 2;
01115 checksumAndSend();
01116
01117 }
01118
01119
01130 void sendDebugIfClear(unsigned char* message){
01131 if(!TXBufferInUseFlags){
01132 TXBufferInUseFlags = ONES;
01133 sendDebugInternal(message);
01134 }else{
01135 Counters.commsDebugMessagesNotSent++;
01136 }
01137 }
01138
01139
01151
01152
01153
01154
01155
01156
01157
01175 void sendDebugInternal(unsigned char* message){
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202 TXBufferCurrentPositionHandler = (unsigned char*)&TXBuffer;
01203
01204
01205 *TXBufferCurrentPositionHandler = 0x11;
01206 TXBufferCurrentPositionHandler++;
01207
01208
01209 *((unsigned short*)TXBufferCurrentPositionHandler) = asyncDebugInfoPacket;
01210 TXBufferCurrentPositionHandler += 2;
01211
01212
01213 unsigned short* TXLength = (unsigned short*)TXBufferCurrentPositionHandler;
01214 TXBufferCurrentPositionHandler += 2;
01215
01216
01217 unsigned short messageLength = stringCopy(TXBufferCurrentPositionHandler, message);
01218 *TXLength = messageLength;
01219 TXBufferCurrentPositionHandler += messageLength;
01220
01221 checksumAndSend();
01222
01223 }
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01295
01296
01297
01298