📄 xnpm.nc
字号:
bEEWriteEnabled = 1; // eeprom writing enabled flag bEEWriteContinuous = FALSE; //MUST release EEFLASH WriteEnable to do future reads on EEFLASH sSYSState = SYS_EEFLASH_WRITE; post NPX_STATEMACHINE();//store srec to eepromnreturn;} //fnpx_srecupdate/*************************************************************************fNPXSRecDownloadDoneEnd of broadcast srec download.Network will interrogate motes for missing packetscleanup regardless of state - this function can also be used to abort*************************************************************************/uint8_t fNPXSRecDownloadDone() { sSYSState = SYS_DL_END;//we are finished....flush eeprom buffer sSYSMState = SYSM_IDLE; post NPX_STATEMACHINE();//fetch srec from eeprom flashreturn(1);}//fNPXSrecDownloadDone/*************************************************************************fNPXGetEEProgIDRead the ProgramID currently in the External EEFLASH*************************************************************************/void fNPXGetEEProgID(uint16_t wEEStart) { wEE_LineR = wEEStart; sSYSState = SYS_ISP_REQ; bEELineReadPending = TRUE; //main state machine handles posting task and busy retries post NPX_STATEMACHINE();//fetch srec from eeprom flash return;}/*************************************************************************fNPXGetNextEmptySRECStarte/Resume Scan thru EEPROM from specified point and starts search for next missing srec.NOTES1.Need terminator (S9 record) indicating end of program2. Actual work is done in READ_DONE 3. Must have released FLASH from writing*************************************************************************/uint8_t fNPXGetNextEmptySREC(uint16_t wCurrentCID) {// VAR(wEEProgStart2) = wEEProgStart+2; call Leds.greenOff(); //diagnostic wCapsuleNo = wCurrentCID; //where we are... wEE_LineR = wEEProgStart+ (wCurrentCID<<1); //2 lines per srec -1st line is S0 sSYSState = SYS_GETNEXTCID; bEELineReadPending = TRUE; //main state machine handles posting task and busy retries post NPX_STATEMACHINE();//fetch srec from eeprom flash return(TRUE);}/*************************************************************************EEPROM_READ_DONEEEPROM read record completed.Handle per current state*************************************************************************/event result_t EEPROMRead.readDone(uint8_t *pEEB, result_t result){uint16_t* pW;uint16_t wCID,wPID;bEELineReadPending = FALSE; //main state machine handles posting task and busy retriescall Leds.redOff(); //diagnostic=LED on during read now off switch (sSYSState){#ifdef new1case SYS_GET_EEPID: //read EEFLASH program ID and store pW = &pEEB[POS_PID]; wPIDinFLASH = pW; break;#endifcase SYS_ISP_REQ1: //check the EEFLASH programid matches the ISP requested PID pW = &pEEB[POS_PID]; if( *pW == wProgramID ) { //this matches so invoke ISP bCommandOK = CMD_STATE_DONE; post NPX_ISP(); }else { //programids do not match - donot invoke ISP bCommandOK = CMD_STATE_BADSEQUENCE; } sSYSMState = SYSM_IDLE; //reset state because SYS_ACK does not sSYSState = SYS_ACK; //we are finished-turnoff led post NPX_STATEMACHINE();//fetch srec from eeprom flash break;case SYS_GETNEXTCID: //searching for bad/missing srecs//check correct ProgramID and expected Capsule ID pW = &pEEB[POS_CID]; wCID = *pW; //type cast pW = &pEEB[POS_PID]; wPID = *pW; if( (wPID != wProgramID ) | (wCID != wCapsuleNo) ) { //something is wrong fProcessMissingCapsule(wPID, wCapsuleNo); } else //ProgramID and CapsuleID are correct { //if at end - clear bMissingCapsules flag and exit if( pEEB[POS_STYPE] == SREC_S9 ) { bCIDMissing = FALSE; wCIDMissing = 0; wCIDMissingPrevious = 1; //wrap around so next REQ will check again bCommandOK = CMD_STATE_DONE;// if(bAckMessage) //no response if at end...// fNPXSendStatus(); sSYSState = SYS_GETDONE; //we are finished #ifdef PLATFORM_MICA2DOT call Leds.redOn(); //turn on => Program is loaded and OK #endif call Leds.greenOn(); //diagnostic post NPX_STATEMACHINE();//fetch srec from eeprom flash break; } wCapsuleNo = wCID+1; //advance to next CID wEE_LineR = wEEProgStart+ (wCapsuleNo<<1); //2 lines per srec // wEE_LineR = wEE_LineR+2; //advance to next srec locaction sSYSState = SYS_GETNEXTCID; bEELineReadPending = TRUE; //main state machine handles posting task and busy retries post NPX_STATEMACHINE();//store srec to eeprom or other operation } break;} //switchreturn( 1 );}/*************************************************************************fProcessMissingCapsuleLocation of a missing capsuleSet flags for future request to network*************************************************************************/void fProcessMissingCapsule(uint16_t wlPID, uint16_t wlCIDMissing){ uint16_t wPacketTime; bCIDMissing = TRUE; wCIDMissing = wlCIDMissing; wCIDMissingPrevious = wCIDMissing; //forces next scan to verify it has been fixed if( bAckMessage ) { //xmit request if network allows it //introduce a random delay before TXing the request - in multiples of Packet time //8MHz CPU clock =>256 instructions per packet time at 38.4KBaud // call Leds.redOn(); //diagnostic wCIDMissing = call Random.rand(); //should be multiples of Packet Time wTXReqDelay = (wCIDMissing & 0x000f) +1; wPacketTime = DELAY_PACKETTIME; while (wTXReqDelay ) { while(wPacketTime) wPacketTime--; wPacketTime = DELAY_PACKETTIME; wTXReqDelay--; } //call Leds.redOff(); //diagnostic fReqMissingCapsule(wlPID,wlCIDMissing); //tx request } sSYSMState = SYSM_UPDATE; //allow random srec updates in this mote sSYSState = SYS_GETDONE; //we are finished for this scan post NPX_STATEMACHINE();//return;}/*************************************************************************fDownLoadAbortAbort and return to idle state*************************************************************************/void fDownLoadAbort(){ sSYSState = SYS_DL_ABORT; sSYSMState = SYS_IDLE; post NPX_STATEMACHINE();return;}/*************************************************************************fSendStatusBuild a Status TOS message and Send it to Generic Com if RF not busy*************************************************************************/void fNPXSendStatus(){ uint8_t i; //ptrmsg->data[0] = 32; //NESC's TOS MEssage Length ptrmsg->data[TS_CMD] = CMD_DOWNLOAD_STATUS; ptrmsg->data[TS_SUBCMD] = TOS_LOCAL_ADDRESS; // lsbye of moteid ptrmsg->data[TS_PID] = wProgramID; //who I am ptrmsg->data[TS_PID+1] = wProgramID>>8; //msg id ptrmsg->data[TS_CAPSULEID] = wCapsuleNo; //reserved- extended msg type ptrmsg->data[TS_CAPSULEID+1] = wCapsuleNo>>8; //reserved- extended msg type ptrmsg->data[TS_MSGDATA] = cCommand; //reserved- extended msg type ptrmsg->data[TS_MSGDATA+1] = bCommandOK; //Last command recevied/processed for (i=8;i<20;i++ ) { ptrmsg->data[i] = i; //fill } TOSMsgType = MSG_COMMAND; //command response message is xmitting if( bTOSMsgBusy == FALSE ){ bTOSMsgBusy = call SendMsg.send(TOS_LOCAL_ADDRESS, DATA_LENGTH29,ptrmsg); } return; } //fStatus/*************************************************************************fSendPIDStatusBuild a Status TOS message and Send it to Generic Com if RF not busy*************************************************************************/void fNPXSendPIDStatus(uint16_t wlPID){ uint8_t i; //ptrmsg->data[0] = 32; //NESC's TOS MEssage Length ptrmsg->data[TS_CMD] = CMD_GET_PIDSTATUS; ptrmsg->data[TS_SUBCMD] = TOS_LOCAL_ADDRESS; // lsbye of moteid ptrmsg->data[TS_PID] = wlPID; //who I am ptrmsg->data[TS_PID+1] = wlPID>>8; //msg id ptrmsg->data[TS_CAPSULEID] = 0; //reserved- extended msg type ptrmsg->data[TS_CAPSULEID+1] = 0; //reserved- extended msg type ptrmsg->data[TS_MSGDATA] = cCommand; //reserved- extended msg type ptrmsg->data[TS_MSGDATA+1] = bCommandOK; //Last command recevied/processed for (i=8;i<20;i++ ) { ptrmsg->data[i] = i; //fill } TOSMsgType = MSG_COMMAND; //command response message is xmitting if( bTOSMsgBusy == FALSE ){ bTOSMsgBusy = call SendMsg.send(TOS_LOCAL_ADDRESS, DATA_LENGTH29,ptrmsg); } return; } //fPIDStatus/*************************************************************************fNPXInSystemProgramExecExecute ISP using specfied ProgID*************************************************************************/void fNPXInSystemProgramExec(TOS_MsgPtr msgptr){ uint16_t *wptr; wptr = (uint16_t *)&(msgptr->data[TS_PID]); wProgramID = *wptr++; EE_PageW = EE_PAGE_START; wEEProgStart = EE_PageW<<4;//the starting point in EEFlash //check EEFLASH to verify this programID is present fNPXGetEEProgID(wEEProgStart); //check valid progid and invoke ISP //EEProgID invokes ISP if all okreturn;}//fNPXInSystemProgramExec /*************************************************************************fReqMissingCapsuleRequest a missing capsule*************************************************************************/void fReqMissingCapsule(uint16_t wlPID, uint16_t wlCIDMissing){ uint8_t i; if( bCIDMissing ) bCommandOK = CMD_STATE_REQCID; else bCommandOK = CMD_STATE_DONE; //no CID requested if (bTOSMsgBusy) return; wlCIDMissing--; //Network wants CID 0 based on return but 1 on xmit ptrmsg->data[TS_CMD] = CMD_REQ_CIDMISSING; ptrmsg->data[TS_SUBCMD] = TOS_LOCAL_ADDRESS; // lsbye of moteid ptrmsg->data[TS_PID] = wlPID; //who I am... or want to be.... ptrmsg->data[TS_PID+1] = wlPID>>8; //msg id ptrmsg->data[TS_CAPSULEID] = wlCIDMissing; //e ptrmsg->data[TS_CAPSULEID+1] = wlCIDMissing>>8; //pe ptrmsg->data[TS_MSGDATA] = cCommand; ptrmsg->data[TS_MSGDATA+1] = bCommandOK; for (i=8;i<20;i++ ) { ptrmsg->data[i] = i; //fill } wlCIDMissing++; //restore to 1 based TOSMsgType = MSG_COMMAND; //command response message is xmitting if( bTOSMsgBusy == FALSE ){ bTOSMsgBusy = call SendMsg.send(TOS_LOCAL_ADDRESS, DATA_LENGTH29,ptrmsg); } return; } //fmissingcid/*************************************************************************fNPXSRECParseGot a code capsule containing 1 srec from radio[0]:commandid[1.2]:program id[3:4]:nofcapsules (srecords)Computes destination EEFlash address based on SREC address and stores inEEFlash. Does NOT respond to Host - this used during Broadcast downloads.Accepts non-contiguous srecs.If bEEPROMBusy we abort because EEBuf is not availableNOTE1. Changes sSYSState if last/S9 recordCopies into a local buffer for passing to EEFLASH driver. The source buffer(TOSMsg) is not available after function returns because TOS reuses it.*************************************************************************/uint8_t fNPXSRECParse(TOS_MsgPtr msgptr) { uint8_t i,j; uint16_t wTemp; uint8_t cTemp,cLen; uint8_t cSRecType; uint8_t bSRecValid; uint8_t bFini = FALSE; uint16_t *wptr; uint8_t cCheckSum; bSRecValid = TRUE; //assume we can parse the sREC //Verify Checksum cLen = msgptr->data[TS_LEN]; //get nof bytes in srec cCheckSum = 0; for (j=TS_LEN;j<TS_LEN+cLen;j++ ) cCheckSum = cCheckSum + msgptr->data[j]; cCheckSum = ~cCheckSum; if( cCheckSum != (uint8_t)msgptr->data[j]) //j is sitting at srec checksum return(FALSE); //bad so ignore if( bEEPROMBusy ) return(FALSE); //busy so ignore cSRecType = msgptr->data[TS_TYPE]; switch ( cSRecType ) { case SREC_S1:/*---------------------------------------------------------------------------Build S1 in binary format-2byte address fieldS11300700C9463000C9463000C9463000C94630070S11300800C9463000C9463000C94630011241FBE51S1130090CFEFD0E1DEBFCDBF11E0A0E0B1E0EAEEEA----------------------------------------------------------------------------*/// TOS_CALL_COMMAND(RED_LED_TOGGLE)(); //diag ELBuff[POS_PID] = wProgramID; ELBuff[POS_PID+1] = wProgramID>>8; ELBuff[POS_CID] = msgptr->data[TS_CAPSULEID]; ELBuff[POS_CID+1] = msgptr->data[TS_CAPSULEID+1]; //Build an SREC Format ELBuff[POS_STYPE] = SREC_S1; ELBuff[POS_SNOFB] = cLen = msgptr->data[TS_LEN]; //get nof bytes in srec ELBuff[POS_S1_ADDR] = msgptr->data[TS1_ADDR_LSB]; //lsbyte of address ELBuff[POS_S1_ADDR+1] = msgptr->data[TS1_ADDR_MSB]; //mostbyte of address //fill the buffer with data section of the SRec w/ lsbyte first cLen = (cLen - 3); //nof Instructions (bytes) for (i=POS_S1_I0,j=TS_INSTR0_MSB;i<POS_S1_I0+cLen;i++,j++ ) { ELBuff[i] = msgptr->data[j]; //get lsbyte// ELBuff[i+1] = msgptr->data[j]; //get msbyte } //place holder for binary computed checksum - i is sitting at correct position ELBuff[i] = 0x77; //diag-indicates stored via SRECStore //fill rest of buffer for (j=i+1;j<32;j++) ELBuff[j] = j; //fillbytes break; //SRec S1 case SREC_S9: //s9 contains boot address - and is last record of sfile// S9030000FC ELBuff[POS_PID] = wProgramID; ELBuff[POS_PID+1] = wProgramID>>8; //Store CapsuleID ELBuff[POS_CID] = msgptr->data[TS_CAPSULEID]; ELBuff[POS_CID+1] = msgptr->data[TS_CAPSULEID+1]; //Build an SREC Format ELBuff[POS_STYPE] = SREC_S9; //srec9 only has 2byte address ELBuff[POS_SNOFB] = cLen = msgptr->data[TS_LEN]; //get nof bytes in srec ELBuff[POS_S1_ADDR] = msgptr->data[TS1_ADDR_LSB]; //lsbyte of address ELBuff[POS_S1_ADDR+1] = msgptr->data[TS1_ADDR_MSB]; //midsbyte of address // bFini = TRUE; / break; default: bSRecValid = FALSE; //unrecognized sREC break; //unknown SRec type }//switch srectypereturn(bSRecValid);}//fNPXSRECParse/*************************************************************************fSysVarInitInit all VARS to defaults*************************************************************************/void fNPXSysVarInit() { #ifdef JTAG TOS_LOCAL_ADDRESS = TOS_LOCAL; //local address set in client #endif sSYSState = SYS_IDLE;// sSYSState = SYS_BEACON;// post NPX_STATEMACHINE();//store srec to eepromn //Netprogramming stuff cCommand = 0; //current command bCommandOK = FALSE; //current command processed ok wCapsuleNo = 0; //code capsule number wNofCapsules = 0; //expected number of capsules wEEProgStart = 0; bEEWriteEnabled = 0; //no EEFLASH after init EE_PageW = EE_PAGE_START; wEEProgStart = EE_PageW<<4; wLastEEPage = EEPROM_NOFPAGES-1; //maxnof lines in EEPROM bEEPROMBusy = 0; //EPROM available to write bEELineWritePending = FALSE; bELBuffAWE = TRUE; //BufferA or B selected for write enable bELBuffFull = FALSE; //EEPROM buffer is full and should be flushed cLineinPage = 0; //reset the line count TOSMsgType = MSG_IDLE; itest = 240; btest = 0; //0:no test pattern //Init message parameters pNETTOSMsg = &NETTOSMsg; //rx buffer ptrmsg = &msg; //init pointer to tx buffer bTOSMsgBusy = 0; //no message pending bNXPMsgBusy = FALSE; itest = TOS_LOCAL_ADDRESS; return;}//fSysVarInit } //end implementation/*****************************************************************************//*ENDOFFILE******************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -