commsCore.c

Go to the documentation of this file.
00001 /*      commsCore.c
00002 
00003         Copyright 2008 Fred Cooke
00004 
00005         This file is part of the FreeEMS project.
00006 
00007         FreeEMS software is free software: you can redistribute it and/or modify
00008         it under the terms of the GNU General Public License as published by
00009         the Free Software Foundation, either version 3 of the License, or
00010         (at your option) any later version.
00011 
00012         FreeEMS software is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU General Public License for more details.
00016 
00017         You should have received a copy of the GNU General Public License
00018         along with any FreeEMS software.  If not, see <http://www.gnu.org/licenses/>.
00019 
00020         We ask that if you make any changes to this file you send them upstream to us at admin@diyefi.org
00021 
00022         Thank you for choosing FreeEMS to run your engine! */
00023 
00024 #define COMMSCORE_C
00025 #include "inc/freeEMS.h"
00026 #include "inc/flashWrite.h"
00027 #include "inc/interrupts.h"
00028 #include "inc/utils.h"
00029 #include "inc/tableLookup.h"
00030 #include "inc/commsCore.h"
00031 #include <string.h>
00032 
00033 void sendError(unsigned short);
00034 void sendDebug(unsigned char*);
00035 
00036 
00037 /* Checksum and initiate send process */
00038 void checksumAndSend(){
00039         /* Get the length from the pointer */
00040         unsigned short TXPacketLengthToSend = (unsigned short)TXBufferCurrentPositionHandler - (unsigned short)&TXBuffer;
00041 
00042         /* Tag the checksum on the end */
00043         *TXBufferCurrentPositionHandler = checksum((unsigned char*)&TXBuffer, TXPacketLengthToSend);
00044         TXPacketLengthToSend++;
00045 
00046         /* Send it out on all the channels required. */
00047 
00048         /* SCI0 - Main serial interface */
00049         if(TXBufferInUseFlags & COM_SET_SCI0_INTERFACE_ID){
00050                 /* Copy numbers to interface specific vars */
00051                 TXPacketLengthToSendSCI0 = TXPacketLengthToSend;
00052                 TXPacketLengthToSendCAN0 = TXPacketLengthToSend;
00053 
00054                 /* Initiate transmission */
00055                 SCI0DRL = START_BYTE;
00056                 /* Note : Order Is Important! */
00057                 /* TX empty flag is already set, so we must clear it by writing out before enabling the interrupt */
00058                 SCI0CR2 |= SCICR2_TX_ISR_ENABLE;
00059         }
00060         /* CAN0 - Main CAN interface */
00061         if(TXBufferInUseFlags & COM_SET_CAN0_INTERFACE_ID){
00062                 // just clear up front for now
00063                 TXBufferInUseFlags &= COM_CLEAR_CAN0_INTERFACE_ID;
00064         }
00065         /* spare2 */
00066         if(TXBufferInUseFlags & COM_SET_SPARE2_INTERFACE_ID){
00067                 // just clear up front for now
00068                 TXBufferInUseFlags &= COM_CLEAR_SPARE2_INTERFACE_ID;
00069         }
00070         /* spare3 */
00071         if(TXBufferInUseFlags & COM_SET_SPARE3_INTERFACE_ID){
00072                 // just clear up front for now
00073                 TXBufferInUseFlags &= COM_CLEAR_SPARE3_INTERFACE_ID;
00074         }
00075         /* spare4 */
00076         if(TXBufferInUseFlags & COM_SET_SPARE4_INTERFACE_ID){
00077                 // just clear up front for now
00078                 TXBufferInUseFlags &= COM_CLEAR_SPARE4_INTERFACE_ID;
00079         }
00080         /* spare5 */
00081         if(TXBufferInUseFlags & COM_SET_SPARE5_INTERFACE_ID){
00082                 // just clear up front for now
00083                 TXBufferInUseFlags &= COM_CLEAR_SPARE5_INTERFACE_ID;
00084         }
00085         /* spare6 */
00086         if(TXBufferInUseFlags & COM_SET_SPARE6_INTERFACE_ID){
00087                 // just clear up front for now
00088                 TXBufferInUseFlags &= COM_CLEAR_SPARE6_INTERFACE_ID;
00089         }
00090         /* spare7 */
00091         if(TXBufferInUseFlags & COM_SET_SPARE7_INTERFACE_ID){
00092                 // just clear up front for now
00093                 TXBufferInUseFlags &= COM_CLEAR_SPARE7_INTERFACE_ID;
00094         }
00095 }
00096 
00097 
00098 void decodePacketAndBuildResponse(){
00099         /* Extract and build up the header fields */
00100         RXBufferCurrentPosition = (unsigned char*)&RXBuffer;
00101         TXBufferCurrentPositionHandler = (unsigned char*)&TXBuffer;
00102 
00103         /* Initialised here such that override is possible */
00104         TXBufferCurrentPositionSCI0 = (unsigned char*)&TXBuffer;
00105         TXBufferCurrentPositionCAN0 = (unsigned char*)&TXBuffer;
00106 
00107         /* Start this off as full packet length and build down to the actual length */
00108         RXCalculatedPayloadLength = RXPacketLengthReceived;
00109 
00110         /* Grab the RX header flags out of the RX buffer */
00111         RXHeaderFlags = *RXBufferCurrentPosition;
00112         RXBufferCurrentPosition++;
00113         RXCalculatedPayloadLength--;
00114 
00115         /* Flag that we are transmitting! */
00116         TXBufferInUseFlags |= COM_SET_SCI0_INTERFACE_ID;
00117         // SCI0 only for now...
00118 
00119         /* Load a blank header into the TX buffer ready for masking */
00120         unsigned char* TXHeaderFlags = TXBufferCurrentPositionHandler;
00121         *TXHeaderFlags = 0;
00122         TXBufferCurrentPositionHandler++;
00123 
00124         /* Grab the payload ID for processing and load the return ID */
00125         RXHeaderPayloadID = *((unsigned short*)RXBufferCurrentPosition);
00126         *((unsigned short*)TXBufferCurrentPositionHandler) = RXHeaderPayloadID + 1;
00127         RXBufferCurrentPosition += 2;
00128         TXBufferCurrentPositionHandler += 2;
00129         RXCalculatedPayloadLength -= 2;
00130 
00131         /* If there is an ack, copy it to the return packet */
00132         if(RXHeaderFlags & HEADER_HAS_ACK){
00133                 *TXBufferCurrentPositionHandler = *RXBufferCurrentPosition;
00134                 *TXHeaderFlags |= HEADER_HAS_ACK;
00135                 RXBufferCurrentPosition++;
00136                 TXBufferCurrentPositionHandler++;
00137                 RXCalculatedPayloadLength--;
00138         }
00139 
00140         /* If the header has addresses, check them and if OK copy them */
00141         if(RXHeaderFlags & HEADER_HAS_ADDRS){
00142                 /* Check the destination address against our address */
00143                 if(!(*RXBufferCurrentPosition == fixedConfigs.networkAddress)){
00144                         /* Addresses do not match, discard packet without error */
00145                         resetReceiveState(CLEAR_ALL_SOURCE_ID_FLAGS);
00146                         TXBufferInUseFlags = 0;
00147                         return;
00148                 }
00149                 RXBufferCurrentPosition++;
00150 
00151                 /* Save and check the source address */
00152                 RXHeaderSourceAddress = *RXBufferCurrentPosition;
00153                 RXBufferCurrentPosition++;
00154                 if(RXHeaderSourceAddress == 0){
00155                         sendError(invalidSourceAddress);
00156                         resetReceiveState(CLEAR_ALL_SOURCE_ID_FLAGS);
00157                         return;
00158                 }
00159 
00160                 /* All is well, setup reply addresses */
00161                 *TXHeaderFlags |= HEADER_HAS_ADDRS;
00162                 /* TX destination = RX source */
00163                 *TXBufferCurrentPositionHandler = RXHeaderSourceAddress;
00164                 TXBufferCurrentPositionHandler++;
00165                 /* TX source = our address */
00166                 *TXBufferCurrentPositionHandler = fixedConfigs.networkAddress;
00167                 TXBufferCurrentPositionHandler++;
00168                 /* Decrement for both at once to save a cycle */
00169                 RXCalculatedPayloadLength -= 2;
00170         }
00171 
00172         /* Grab the length if available */
00173         if(RXHeaderFlags & HEADER_HAS_LENGTH){
00174                 RXHeaderPayloadLength = *((unsigned short*)RXBufferCurrentPosition);
00175                 RXBufferCurrentPosition += 2;
00176                 RXCalculatedPayloadLength -= 2;
00177                 /* Minus one for the checksum on the end ;-) */
00178                 if(RXHeaderPayloadLength != (RXCalculatedPayloadLength - 1)){
00179                         sendError(payloadLengthHeaderMismatch);
00180                         resetReceiveState(CLEAR_ALL_SOURCE_ID_FLAGS);
00181                         return;
00182                 }
00183         }
00184 
00185         /* Please Note :                                                                                                                        */
00186         /* length (and it's flag) should be set by each return packet type handler if required or desired.      */
00187         /* If an ack has been requested, ensure the negative ack flag is set if the opration failed.            */
00188 
00189         /* Perform the requested action based on payload ID */
00190         if (RXHeaderFlags & HEADER_IS_PROTO){ /* Protocol payload types */
00191                 /* Set the return type to be protocol too */
00192                 *TXHeaderFlags |= HEADER_IS_PROTO;
00193 
00194                 switch (RXHeaderPayloadID){
00195                 case requestInterfaceVersion:
00196                         /* Subtract one to exclude checksum */
00197                         if((RXCalculatedPayloadLength - 1) != 0){
00198                                 sendError(payloadLengthTypeMismatch);
00199                                 break;
00200                         }
00201                         /* This type must have a length field, set that up */
00202                         *((unsigned short*)TXBufferCurrentPositionHandler) = sizeof(interfaceVersionAndType);
00203                         *TXHeaderFlags |= HEADER_HAS_LENGTH;
00204                         TXBufferCurrentPositionHandler += 2;
00205                         /* Load the body into place */
00206                         memcpy((void*)TXBufferCurrentPositionHandler, (void*)&interfaceVersionAndType, sizeof(interfaceVersionAndType));
00207                         TXBufferCurrentPositionHandler += sizeof(interfaceVersionAndType);
00208                         checksumAndSend();
00209                         break;
00210                 case requestFirmwareVersion:
00211                         /* Subtract one to exclude checksum */
00212                         if((RXCalculatedPayloadLength - 1) != 0){
00213                                 sendError(payloadLengthTypeMismatch);
00214                                 break;
00215                         }
00216                         /* This type must have a length field, set that up */
00217                         *((unsigned short*)TXBufferCurrentPositionHandler) = sizeof(firmwareVersion);
00218                         *TXHeaderFlags |= HEADER_HAS_LENGTH;
00219                         TXBufferCurrentPositionHandler += 2;
00220                         /* Load the body into place */
00221                         memcpy((void*)TXBufferCurrentPositionHandler, (void*)&firmwareVersion, sizeof(firmwareVersion));
00222                         TXBufferCurrentPositionHandler += sizeof(firmwareVersion);
00223                         checksumAndSend();
00224                         break;
00225                 case requestMaxPacketSize:
00226                         /* Subtract one to exclude checksum */
00227                         if((RXCalculatedPayloadLength - 1) != 0){
00228                                 sendError(payloadLengthTypeMismatch);
00229                                 break;
00230                         }
00231                         /* Load the size into place */
00232                         *((unsigned short*)TXBufferCurrentPositionHandler) = RX_BUFFER_SIZE;
00233                         TXBufferCurrentPositionHandler += 2;
00234                         checksumAndSend();
00235                         break;
00236                 case requestEchoPacketReturn:
00237                         /* This type must have a length field, set that up */
00238                         *((unsigned short*)TXBufferCurrentPositionHandler) = RXPacketLengthReceived;
00239                         *TXHeaderFlags |= HEADER_HAS_LENGTH;
00240                         TXBufferCurrentPositionHandler += 2;
00241                         /* Load the body into place */
00242                         memcpy((void*)TXBufferCurrentPositionHandler, (void*)&RXBuffer, RXPacketLengthReceived);
00243                         /* Note, there is no overflow check here because the TX buffer is slightly       */
00244                         /* bigger than the RX buffer and there is overflow checking for receives anyway. */
00245                         TXBufferCurrentPositionHandler += RXPacketLengthReceived;
00246                         checksumAndSend();
00247                         break;
00248                 case requestSoftSystemReset:
00249                         /* Subtract one to exclude checksum */
00250                         if((RXCalculatedPayloadLength - 1) != 0){
00251                                 sendError(payloadLengthTypeMismatch);
00252                                 break;
00253                         }
00254                         /* Perform soft system reset */
00255                         _start();
00256                 case requestHardSystemReset:
00257                         /* Subtract one to exclude checksum */
00258                         if((RXCalculatedPayloadLength - 1) != 0){
00259                                 sendError(payloadLengthTypeMismatch);
00260                                 break;
00261                         }
00262                         /* Perform full hard system reset */
00263                         hardReset();
00264                         // Currently, corrupted memory instead of reset as of paged flash setup being introduced TODO FIXME this is broken...
00265 
00266                         /* 0xFFFE is the master reset vector. Using _start() */
00267                         /* only resets the app ignoring the monitor switch.  */
00268                         /* It does not work properly because the location of */
00269                         /* _start is not the master reset vector location.   */
00270                 default:
00271                         if((RXHeaderPayloadID % 2) == 1){
00272                                 sendError(invalidProtocolPayloadID);
00273                         }else{
00274                                 sendError(unrecognisedProtocolPayloadID);
00275                         }
00276                 }
00277         }else{ /* Otherwise firmware payload types */
00278                 switch (RXHeaderPayloadID) {
00279                 case replaceBlockInRAM:
00280                 {
00281                         int id = PORTB;
00282                         if(id < 16){
00283                                 mainTable aTable;
00284                                 validateMainTable(&aTable);
00285                         }else if(id > 27 && id < 50){
00286                                 twoDTableUS aTable;
00287                                 validateTwoDTable(&aTable);
00288                         }
00289 
00290 
00291 //                      /* Save page values for restore */
00292 //                      unsigned char oldRamPage = RPAGE;
00293 //                      unsigned char oldFlashPage = PPAGE;
00294 //
00295 //
00296 //              if((details->RAMPage == 0) || (details->RAMAddress == 0)){
00297 //                      return bugInMemoryBlockCode;
00298 //              }
00299 //              /* Set the viewable ram page */
00300 //              RPAGE = details->RAMPage;
00301 //              /* Copy from the RX buffer to the block of ram */
00302 //              memcpy(details->RAMAddress, RXBufferCurrentPosition, details->size);
00303 //
00304 //
00305 //      /* Restore the original ram and flash pages */
00306 //      RPAGE = oldRamPage;
00307 //      PPAGE = oldFlashPage;
00308 //
00309 
00310 
00311                         // perform function TODO
00312                         sendError(unimplementedFunction);
00313                         break;
00314                 }
00315                 case replaceBlockInFlash:
00316                 {
00317                         // if 0 - 15 validate table
00318                         mainTable aTable;
00319                         validateMainTable(&aTable);
00320                         // otherwise hope for the best
00321 
00322 //              check size > 0 and % 1024 != 0 same for ram to flash TODO
00323 
00324 //      /* Save page values for restore */
00325 //      unsigned char oldRamPage = RPAGE;
00326 //      unsigned char oldFlashPage = PPAGE;
00327 //
00328 //
00329 //              if((details->FlashPage == 0) || (details->FlashAddress == 0)){
00330 //                      return bugInMemoryBlockCode;
00331 //              }
00332 //              /* Set the viewable flash page */
00333 //              PPAGE = details->FlashPage;
00334 //              /* Copy from the RX buffer to the block of flash */
00335 //              error = writeAlignedBlock((unsigned short*)details->FlashAddress, (unsigned short*)RXBufferCurrentPosition);
00336 //              /* If present in ram, update that too */
00337 //              if((details->RAMPage != 0) && (details->RAMAddress != 0)){
00338 //                      /* Set the viewable ram page */
00339 //                      RPAGE = details->RAMPage;
00340 //                      /* Copy from the RX buffer to the block of ram */
00341 //                      memcpy(details->RAMAddress, RXBufferCurrentPosition, details->size);
00342 //              }
00343 //
00344 //
00345 //      /* Restore the original ram and flash pages */
00346 //      RPAGE = oldRamPage;
00347 //      PPAGE = oldFlashPage;
00348 //
00349 
00350 
00351                         // perform function TODO
00352                         sendError(unimplementedFunction);
00353                         break;
00354                 }
00355                 case retrieveBlockFromRAM:
00356                 {
00357                         /* Subtract one to exclude checksum */
00358                         if((RXCalculatedPayloadLength - 1) != 2){
00359                                 sendError(payloadLengthTypeMismatch);
00360                                 break;
00361                         }
00362                         /* Extract the ram location ID from the received data */
00363                         unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00364                         /* Store it back into the output data */
00365                         *(unsigned short*)TXBufferCurrentPositionHandler = locationID;
00366                         TXBufferCurrentPositionHandler += 2;
00367 
00368                         /* If it's a main table we are returning, specify the limits explicitly */
00369                         if(locationID < 16){
00370                                 /* Store it back into the output data */
00371                                 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_RPM_LENGTH;
00372                                 TXBufferCurrentPositionHandler += 2;
00373                                 /* Store it back into the output data */
00374                                 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_LOAD_LENGTH;
00375                                 TXBufferCurrentPositionHandler += 2;
00376                                 /* Store it back into the output data */
00377                                 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_MAIN_LENGTH;
00378                                 TXBufferCurrentPositionHandler += 2;
00379                         }
00380 
00381                         /* Look up the memory location details */
00382                         blockDetails details;
00383                         lookupBlockDetails(locationID, &details);
00384 
00385                         if((details.RAMPage == 0) || (details.RAMAddress == 0)){
00386                                 sendError(invalidMemoryActionForID);
00387                                 break;
00388                         }
00389 
00390                         /* Save page value for restore and set the visible page */
00391                         unsigned char oldRamPage = RPAGE;
00392                         RPAGE = details.RAMPage;
00393 
00394                         /* Copy the block of ram to the TX buffer */
00395                         memcpy(TXBufferCurrentPositionHandler, details.RAMAddress, details.size);
00396                         TXBufferCurrentPositionHandler += details.size;
00397 
00398                         /* Restore the original ram and flash pages */
00399                         RPAGE = oldRamPage;
00400 
00401                         checksumAndSend();
00402                         break;
00403                 }
00404                 case retrieveBlockFromFlash:
00405                 {
00406                         /* Subtract one to exclude checksum */
00407                         if((RXCalculatedPayloadLength - 1) != 2){
00408                                 sendError(payloadLengthTypeMismatch);
00409                                 break;
00410                         }
00411 
00412                         /* Extract the flash location ID from the received data */
00413                         unsigned short locationID = *((unsigned short*)RXBufferCurrentPosition);
00414                         /* Store it back into the output data */
00415                         *(unsigned short*)TXBufferCurrentPositionHandler = locationID;
00416                         TXBufferCurrentPositionHandler += 2;
00417 
00418                         /* If it's a main table we are returning, specify the limits explicitly */
00419                         if(locationID < 16){
00420                                 /* Store it back into the output data */
00421                                 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_RPM_LENGTH;
00422                                 TXBufferCurrentPositionHandler += 2;
00423                                 /* Store it back into the output data */
00424                                 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_LOAD_LENGTH;
00425                                 TXBufferCurrentPositionHandler += 2;
00426                                 /* Store it back into the output data */
00427                                 *(unsigned short*)TXBufferCurrentPositionHandler = MAINTABLE_MAX_MAIN_LENGTH;
00428                                 TXBufferCurrentPositionHandler += 2;
00429                         }
00430 
00431                         /* Look up the memory location details */
00432                         blockDetails details;
00433                         lookupBlockDetails(locationID, &details);
00434 
00435                         if((details.FlashPage == 0) || (details.FlashAddress == 0)){
00436                                 sendError(invalidMemoryActionForID);
00437                                 break;
00438                         }
00439 
00440                         /* Save page value for restore and set the visible page */
00441                         unsigned char oldFlashPage = PPAGE;
00442                         PPAGE = details.FlashPage;
00443 
00444                         /* Copy the block of flash to the TX buffer */
00445                         memcpy(TXBufferCurrentPositionHandler, details.FlashAddress, details.size);
00446                         TXBufferCurrentPositionHandler += details.size;
00447 
00448                         /* Restore the original ram and flash pages */
00449                         PPAGE = oldFlashPage;
00450 
00451                         checksumAndSend();
00452                         break;
00453                 }
00454                 case burnBlockFromRamToFlash:
00455                 {
00456                         // perform function TODO
00457                         writeAlignedBlock((unsigned short*)0x7c00, (unsigned short*)0xc000);
00458 
00459 //                      unsigned char old = PPAGE;
00460 //                      PPAGE = BIGTABLES_PPAGE;
00461 //                      writeAlignedBlock((unsigned short*)&VETableMainFlash, (unsigned short*)0xC000);
00462 //                      PPAGE = old;
00463                         //sendError(unimplementedFunction);
00464 //                      PORTB = ONES;
00465 
00466 //              check size > 0 and % 1024 != 0 same for ram to flash TODO
00467 
00468 //                              /* Save page values for restore */
00469 //      unsigned char oldRamPage = RPAGE;
00470 //      unsigned char oldFlashPage = PPAGE;
00471 //
00472 //
00473 //              if((details->RAMPage == 0) || (details->RAMAddress == 0) || (details->FlashPage == 0) || (details->FlashAddress == 0)){
00474 //                      return bugInMemoryBlockCode;
00475 //              }
00476 //              /* Set the viewable ram and flash pages */
00477 //              RPAGE = details->RAMPage;
00478 //              PPAGE = details->FlashPage;
00479 //              error = writeAlignedBlock((unsigned short*)details->FlashAddress, (unsigned short*)details->RAMAddress);
00480 //
00481 //
00482 //      /* Restore the original ram and flash pages */
00483 //      RPAGE = oldRamPage;
00484 //      PPAGE = oldFlashPage;
00485 
00486 
00487 
00488                         sendDebug("Burn Ran!");
00489                         break;
00490                 }
00491                 case adjustMainTableCell:
00492                 {
00493                         // extract id
00494                         // extract value
00495                         // extract indices x 2
00496                         // lookup addresses
00497                         mainTable aTable;
00498                         validateMainTable(&aTable);
00499                         /* Test */
00500                         setPagedMainTableCellValue(&TablesA.VETableMain, 0, 0, 32768, currentFuelRPage);
00501                         // perform function TODO
00502                         sendError(unimplementedFunction);
00503                         break;
00504                 }
00505                 case adjustMainTableRPMAxis:
00506                 {
00507                         // extract id
00508                         // extract value
00509                         // extract index
00510                         // lookup addresses
00511                         mainTable aTable;
00512                         validateMainTable(&aTable);
00513                         /* Test */
00514                         setPagedMainTableRPMValue(&TablesA.VETableMain, 0, 0, currentFuelRPage);
00515                         // perform function TODO
00516                         sendError(unimplementedFunction);
00517                         break;
00518                 }
00519                 case adjustMainTableLoadAxis:
00520                 {
00521                         // extract id
00522                         // extract value
00523                         // extract index
00524                         // lookup addresses
00525                         mainTable aTable;
00526                         validateMainTable(&aTable);
00527                         /* Test */
00528                         setPagedMainTableLoadValue(&TablesA.VETableMain, 0, 0, currentFuelRPage);
00529                         // perform function TODO
00530                         sendError(unimplementedFunction);
00531                         break;
00532                 }
00533                 case adjust2dTableAxis:
00534                         // extract id
00535                         // extract value
00536                         // extract index
00537                         // lookup addresses
00538 
00539                         // perform function TODO
00540                         sendError(unimplementedFunction);
00541                         break;
00542                 case adjust2dTableCell:
00543                         // extract id
00544                         // extract value
00545                         // extract index
00546                         // lookup addresses
00547 
00548                         // perform function TODO
00549                         sendError(unimplementedFunction);
00550                         break;
00551                 case replace2dTableInRAM:
00552                 {
00553                         // extract id
00554                         // lookup addresses
00555                         twoDTableUS aTable;
00556                         validateTwoDTable(&aTable);
00557                         // perform function TODO
00558                         sendError(unimplementedFunction);
00559                         break;
00560                 }
00561                 case requestBasicDatalog:
00562                         // perform function TODO
00563                         sendError(unimplementedFunction);
00564                         break;
00565                 case requestAdjustableDatalog:
00566                         // perform function TODO
00567                         sendError(unimplementedFunction);
00568                         break;
00569                 case forwardPacketOverCAN:
00570                         // perform function TODO
00571                         sendError(unimplementedFunction);
00572                         break;
00573                 case forwardPacketOverUART:
00574                         // perform function TODO
00575                         sendError(unimplementedFunction);
00576                         break;
00577                 default:
00578                         if((RXHeaderPayloadID % 2) == 1){
00579                                 sendError(invalidFirmwarePayloadID);
00580                         }else{
00581                                 sendError(unrecognisedFirmwarePayloadID);
00582                         }
00583                 }
00584         }
00585         /* Switch reception back on now that we are done with the received data */
00586         resetReceiveState(CLEAR_ALL_SOURCE_ID_FLAGS);
00587         PORTK |= BIT0;
00588 }
00589 
00590 
00591 /* Flash only blocks leave the ram address and page values       */
00592 /* set to zero. ID's that don't exist leave all set to zero. */
00593 /* Error handling is to be done externally based on that.        */
00594 void lookupBlockDetails(unsigned short locationID, blockDetails* details){
00595         /* Initialise the four values needed for operations on memory at 0 for error checking */
00596         details->RAMPage = 0;
00597         details->FlashPage = 0;
00598         details->RAMAddress = 0;
00599         details->FlashAddress = 0;
00600 
00601         /* Initialise the block size to 1024 to save code space and increase readability */
00602         details->size = MAINTABLE_SIZE;
00603 
00604         /* Look up the locations and set non default sizes */
00605         switch (locationID) {
00606         case FixedConfigLocationID:
00607                 details->FlashPage = PPAGE;
00608                 details->FlashAddress = (void*)&fixedConfigs;
00609                 break;
00610         case FixedConfig2LocationID:
00611                 details->FlashPage = PPAGE;
00612                 details->FlashAddress = (void*)&fixedConfigs2;
00613                 break;
00614         case IATTransferTableLocationID:
00615                 details->size = TransferTableSize;
00616                 details->FlashPage = PPAGE;
00617                 details->FlashAddress = (void*)&IATTransferTable;
00618                 break;
00619         case CHTTransferTableLocationID:
00620                 details->size = TransferTableSize;
00621                 details->FlashPage = PPAGE;
00622                 details->FlashAddress = (void*)&CHTTransferTable;
00623                 break;
00624         case MAFTransferTableLocationID:
00625                 details->size = TransferTableSize;
00626                 details->FlashPage = PPAGE;
00627                 details->FlashAddress = (void*)&MAFTransferTable;
00628                 break;
00629         case VETableMainFlashLocationID:
00630                 details->RAMPage = RPAGE_FUEL_ONE;
00631                 details->FlashPage = BIGTABLES_PPAGE;
00632                 details->RAMAddress = (void*)&TablesA;
00633                 details->FlashAddress = (void*)&VETableMainFlash;
00634                 break;
00635         case VETableMainFlash2LocationID:
00636                 details->RAMPage = RPAGE_FUEL_TWO;
00637                 details->FlashPage = BIGTABLES_PPAGE;
00638                 details->RAMAddress = (void*)&TablesA;
00639                 details->FlashAddress = (void*)&VETableMainFlash2;
00640                 break;
00641         case VETableSecondaryFlashLocationID:
00642                 details->RAMPage = RPAGE_FUEL_ONE;
00643                 details->FlashPage = BIGTABLES_PPAGE;
00644                 details->RAMAddress = (void*)&TablesB;
00645                 details->FlashAddress = (void*)&VETableSecondaryFlash;
00646                 break;
00647         case VETableSecondaryFlash2LocationID:
00648                 details->RAMPage = RPAGE_FUEL_TWO;
00649                 details->FlashPage = BIGTABLES_PPAGE;
00650                 details->RAMAddress = (void*)&TablesB;
00651                 details->FlashAddress = (void*)&VETableSecondaryFlash2;
00652                 break;
00653         case VETableTertiaryFlashLocationID:
00654                 details->RAMPage = RPAGE_FUEL_ONE;
00655                 details->FlashPage = BIGTABLES_PPAGE;
00656                 details->RAMAddress = (void*)&TablesC;
00657                 details->FlashAddress = (void*)&VETableTertiaryFlash;
00658                 break;
00659         case VETableTertiaryFlash2LocationID:
00660                 details->RAMPage = RPAGE_FUEL_TWO;
00661                 details->FlashPage = BIGTABLES_PPAGE;
00662                 details->RAMAddress = (void*)&TablesC;
00663                 details->FlashAddress = (void*)&VETableTertiaryFlash2;
00664                 break;
00665         case LambdaTableFlashLocationID:
00666                 details->RAMPage = RPAGE_FUEL_ONE;
00667                 details->FlashPage = BIGTABLES_PPAGE;
00668                 details->RAMAddress = (void*)&TablesD;
00669                 details->FlashAddress = (void*)&LambdaTableFlash;
00670                 break;
00671         case LambdaTableFlash2LocationID:
00672                 details->RAMPage = RPAGE_FUEL_TWO;
00673                 details->FlashPage = BIGTABLES_PPAGE;
00674                 details->RAMAddress = (void*)&TablesD;
00675                 details->FlashAddress = (void*)&LambdaTableFlash2;
00676                 break;
00677         case IgnitionAdvanceTableMainFlashLocationID:
00678                 details->RAMPage = RPAGE_TIME_ONE;
00679                 details->FlashPage = BIGTABLES_PPAGE;
00680                 details->RAMAddress = (void*)&TablesA;
00681                 details->FlashAddress = (void*)&IgnitionAdvanceTableMainFlash;
00682                 break;
00683         case IgnitionAdvanceTableMainFlash2LocationID:
00684                 details->RAMPage = RPAGE_TIME_TWO;
00685                 details->FlashPage = BIGTABLES_PPAGE;
00686                 details->RAMAddress = (void*)&TablesA;
00687                 details->FlashAddress = (void*)&IgnitionAdvanceTableMainFlash2;
00688                 break;
00689         case IgnitionAdvanceTableSecondaryFlashLocationID:
00690                 details->RAMPage = RPAGE_TIME_ONE;
00691                 details->FlashPage = BIGTABLES_PPAGE;
00692                 details->RAMAddress = (void*)&TablesB;
00693                 details->FlashAddress = (void*)&IgnitionAdvanceTableSecondaryFlash;
00694                 break;
00695         case IgnitionAdvanceTableSecondaryFlash2LocationID:
00696                 details->RAMPage = RPAGE_TIME_TWO;
00697                 details->FlashPage = BIGTABLES_PPAGE;
00698                 details->RAMAddress = (void*)&TablesB;
00699                 details->FlashAddress = (void*)&IgnitionAdvanceTableSecondaryFlash2;
00700                 break;
00701         case InjectionAdvanceTableMainFlashLocationID:
00702                 details->RAMPage = RPAGE_TIME_ONE;
00703                 details->FlashPage = BIGTABLES_PPAGE;
00704                 details->RAMAddress = (void*)&TablesC;
00705                 details->FlashAddress = (void*)&InjectionAdvanceTableMainFlash;
00706                 break;
00707         case InjectionAdvanceTableMainFlash2LocationID:
00708                 details->RAMPage = RPAGE_TIME_TWO;
00709                 details->FlashPage = BIGTABLES_PPAGE;
00710                 details->RAMAddress = (void*)&TablesC;
00711                 details->FlashAddress = (void*)&InjectionAdvanceTableMainFlash2;
00712                 break;
00713         case InjectionAdvanceTableSecondaryFlashLocationID:
00714                 details->RAMPage = RPAGE_TIME_ONE;
00715                 details->FlashPage = BIGTABLES_PPAGE;
00716                 details->RAMAddress = (void*)&TablesD;
00717                 details->FlashAddress = (void*)&InjectionAdvanceTableSecondaryFlash;
00718                 break;
00719         case InjectionAdvanceTableSecondaryFlash2LocationID:
00720                 details->RAMPage = RPAGE_TIME_TWO;
00721                 details->FlashPage = BIGTABLES_PPAGE;
00722                 details->RAMAddress = (void*)&TablesD;
00723                 details->FlashAddress = (void*)&InjectionAdvanceTableSecondaryFlash2;
00724                 break;
00725         case SmallTablesAFlashLocationID:
00726                 details->RAMPage = RPAGE_TUNE_ONE;
00727                 details->FlashPage = PPAGE;
00728                 details->RAMAddress = (void*)&TablesA;
00729                 details->FlashAddress = (void*)&SmallTablesAFlash;
00730                 break;
00731         case SmallTablesAFlash2LocationID:
00732                 details->RAMPage = RPAGE_TUNE_TWO;
00733                 details->FlashPage = PPAGE;
00734                 details->RAMAddress = (void*)&TablesA;
00735                 details->FlashAddress = (void*)&SmallTablesAFlash2;
00736                 break;
00737         case SmallTablesBFlashLocationID:
00738                 details->RAMPage = RPAGE_TUNE_ONE;
00739                 details->FlashPage = PPAGE;
00740                 details->RAMAddress = (void*)&TablesB;
00741                 details->FlashAddress = (void*)&SmallTablesBFlash;
00742                 break;
00743         case SmallTablesBFlash2LocationID:
00744                 details->RAMPage = RPAGE_TUNE_TWO;
00745                 details->FlashPage = PPAGE;
00746                 details->RAMAddress = (void*)&TablesB;
00747                 details->FlashAddress = (void*)&SmallTablesBFlash2;
00748                 break;
00749         case SmallTablesCFlashLocationID:
00750                 details->RAMPage = RPAGE_TUNE_ONE;
00751                 details->FlashPage = PPAGE;
00752                 details->RAMAddress = (void*)&TablesC;
00753                 details->FlashAddress = (void*)&SmallTablesCFlash;
00754                 break;
00755         case SmallTablesCFlash2LocationID:
00756                 details->RAMPage = RPAGE_TUNE_TWO;
00757                 details->FlashPage = PPAGE;
00758                 details->RAMAddress = (void*)&TablesC;
00759                 details->FlashAddress = (void*)&SmallTablesCFlash2;
00760                 break;
00761         case SmallTablesDFlashLocationID:
00762                 details->RAMPage = RPAGE_TUNE_ONE;
00763                 details->FlashPage = PPAGE;
00764                 details->RAMAddress = (void*)&TablesD;
00765                 details->FlashAddress = (void*)&SmallTablesDFlash;
00766                 break;
00767         case SmallTablesDFlash2LocationID:
00768                 details->RAMPage = RPAGE_TUNE_TWO;
00769                 details->FlashPage = PPAGE;
00770                 details->RAMAddress = (void*)&TablesD;
00771                 details->FlashAddress = (void*)&SmallTablesDFlash2;
00772                 break;
00773         }
00774 }
00775 
00776 
00777 /* Send an error code out in a frequency limited way */
00778 void sendError(unsigned short errorCode){
00779 //      TXBufferInUseFlags = 0;
00780         /* No need for atomic block here as one of two conditions will always be */
00781         /* true when calling this. Either we have been flagged to receive and    */
00782         /* decode a packet, or we are in an ISR. In either case it is safe to    */
00783         /* check the flags and initiate the sequence if they are clear.          */
00784 //      if(RXTXSerialStateFlags & TX_IN_PROGRESS){
00785                 /* It's OK to return without resetting as it will be done by */
00786                 /* either of those processes if they are underway. The other */
00787                 /* processes are not overridden because they have priority.  */
00788 //              return;
00789 //      }else{ /* Turn off reception */
00790                 /* It's OK to turn this off if nothing was currently being received */
00791 //              SCI0CR2 &= SCICR2_RX_ISR_DISABLE;
00792 //              SCI0CR2 &= SCICR2_RX_DISABLE;
00793 
00794                 /* Build up the packet */
00795                 /* Set the pointer to the start */
00796                 TXBufferCurrentPositionHandler = (unsigned char*)&TXBuffer;
00797                 /* Set the length */
00798 //              TXPacketLengthToSend = 5; /* Flags + Payload ID + Error Code */
00799                 /* Flags = protocol with no extra fields */
00800                 *TXBufferCurrentPositionHandler = 0x01;
00801                 TXBufferCurrentPositionHandler++;
00802                 /* Set the payload ID */
00803                 *((unsigned short*)TXBufferCurrentPositionHandler) = asyncErrorCodePacket;
00804                 TXBufferCurrentPositionHandler += 2;
00805                 /* Set the error code */
00806                 *((unsigned short*)TXBufferCurrentPositionHandler) = errorCode;
00807                 TXBufferCurrentPositionHandler += 2;
00808                 checksumAndSend();
00809 //      }
00810 }
00811 
00812 
00813 /* Send a null terminated message out on the broadcast address */
00814 void sendDebug(unsigned char* message){
00815 //      if(TRUE){
00816 //              Counters.serialDebugUnsentCounter++;
00817 //              return;
00818 //      }
00819         // wrong :
00820         /* No need for atomic block here as one of two conditions will always be */
00821         /* true when calling this. Either we have been flagged to receive and    */
00822         /* decode a packet, or we are in an ISR. In either case it is safe to    */
00823         /* check the flags and initiate the sequence if they are clear.          */
00824         //if(RXTXSerialStateFlags & TX_IN_PROGRESS){
00825                 // wrong :
00826                 /* It's OK to return without resetting as it will be done by */
00827                 /* either of those processes if they are underway. The other */
00828                 /* processes are not overridden because they have priority.  */
00829                 //TXBufferInUseFlags = 0;
00830                 //return;
00831 //      }else{ /* Turn off reception */
00832                 /* It's OK to turn this off if nothing was currently being received */
00833         //      SCI0CR2 &= SCICR2_RX_ISR_DISABLE;
00834         //      SCI0CR2 &= SCICR2_RX_DISABLE;
00835 
00836                 /* Build up the packet */
00837                 /* Set the pointer to the start and init the length */
00838                 TXBufferCurrentPositionHandler = (unsigned char*)&TXBuffer;
00839 
00840                 /* Load a protocol with length header into the TX buffer ready for masking */
00841                 *TXBufferCurrentPositionHandler = 0x11;
00842                 TXBufferCurrentPositionHandler++;
00843 
00844                 /* Set the payload ID */
00845                 *((unsigned short*)TXBufferCurrentPositionHandler) = asyncDebugInfoPacket;
00846                 TXBufferCurrentPositionHandler += 2;
00847 
00848                 /* Store the length location */
00849                 unsigned short* TXLength = (unsigned short*)TXBufferCurrentPositionHandler;
00850                 TXBufferCurrentPositionHandler += 2;
00851 
00852                 /* Copy the string into place and record the length copied */
00853                 unsigned short messageLength = stringCopy(TXBufferCurrentPositionHandler, message);
00854                 *TXLength = messageLength;
00855                 TXBufferCurrentPositionHandler += messageLength;
00856 
00857                 checksumAndSend();
00858         //}
00859 }
00860 
00861 
00862 /* This function should be period limited to about 10 seconds internally (or by scheduler) */
00863 //void checkCountersAndSendErrors(){
00864         // compare time stamps  with current time stamps and execute if old enough. (if no scheduler)
00865 
00866         // compare counters with counters cache (from last time) sending an error packet when they differ
00867 
00868         // copy counters to counters cache for next time
00869 
00870         // send errors with busy wait on the basis that all errors should be taken care of and not be sent in fairly short order?
00871 
00872         // or send with isr but just busy wait for it to finish before sending the next?
00873 
00874         // debug messages, busy wait or isr or both, perhaps busy wait till able to send, lock sending (need semaphore for this as well as sending one?) and initiate send, then carry on? investigate timeframes for sends of smallish 100byte packets.
00875 
00876         // need to figure out how to queue received packets for processing when we are currently sending stuff out.
00877 
00878         // above notes don't belong here really.
00879 //}
00880 
00881 
00882 //void prepareDatalog(){
00883 //      // send data log by default otherwise
00884 //      unsigned char chunksExpected = 8; // based on configuration, yet to determine how to calculate this number
00885 //      unsigned char chunksLoaded = 0;
00886 //      if ((!receiving) && (datalogMask & rawVarsMask)) {
00887 //              //
00888 //              chunksLoaded++;
00889 //      }
00890 //      if ((!receiving) && (datalogMask & Mask)) {
00891 //              //
00892 //              chunksLoaded++;
00893 //      }
00894 //      if ((!receiving) && (datalogMask & Mask)) {
00895 //              //
00896 //              chunksLoaded++;
00897 //      }
00898 //      if ((!receiving) && (datalogMask & Mask)) {
00899 //              //
00900 //              chunksLoaded++;
00901 //      }
00902 //      if ((!receiving) && (datalogMask & Mask)) {
00903 //              //
00904 //              chunksLoaded++;
00905 //      }
00906 //      if ((!receiving) && (datalogMask & Mask)) {
00907 //              //
00908 //              chunksLoaded++;
00909 //      }
00910 //      if ((!receiving) && (datalogMask & Mask)) {
00911 //              //
00912 //              chunksLoaded++;
00913 //      }
00914 //      if ((!receiving) && (datalogMask & Mask)) {
00915 //              //
00916 //              chunksLoaded++;
00917 //      }
00918 //      //      set the length
00919 //      //      the pointer should be correct already
00920 //}

Generated on Mon Nov 10 21:18:50 2008 for freeems by  doxygen 1.5.2