📄 xnpm.nc
字号:
// $Id: XnpM.nc,v 1.1.2.3 2003/08/21 00:21:16 idgay Exp $/***************************************************************************MODULE XNetProgFILE XnpM.ncPURPOSE In Network Programming/Boot ModulePLATFORM MICA2-128 mode only TOS NesCREVISION 02mar03 mm created 04apr03 mm restructure w/statemachine 08may03 mm datalength29 for TOS Packet size overrides application's packet size 09may03 mm add checksum test on incomming srecords==============================================================================DESCRIPTIONProvides In-Network Programming services to client/application.There are 2 main phases:1. Program/code download from network/host via radio.2. Local reprogram of uP and reboot.These operations are independent and coordinated with Mote Application (client)to avoid resource conflicts.TERMINOLOGY-ProgramID aka PID Identifies the Program being downloaded, reprogramming etc. Corresponds to srec checksum-CapsuleID aka CIDSREC record from Network - contained in a single TOS Message Packet-INP In-Network Programming. Reprogram process over the network-ISP In-System Programming. Typically refers to the Mote's internal reprogramming.-Client. The user's application executing on the Mote.RESOURCESNP uses -EEPROM (External Flash memory).-GENERIC_COMM / Active Message Handler#47-RANDOMLFSR / to randomize delays in response to Query Missing CIDNOTES-EEPROM resource is shared by NP and Client. TClient must release EEPROM resource before acknowledging NP_DOWNLOAD_REQ.-Only 1 Program image in EEPROM is currently supported.-Client must INIT and TUNE radio. I.e. establish wireless link.This module does NOT handle powerdown/sleep activities. I.e. radio/system linkmust be active/on for NP messages to be received and processed.-Application MUST be compiled for ATMega128 NATIVE mode. AT103 CompatiblityMode FUSE MUST be DISABLED/TURNED OFF.-BOOTLOADER INPISP must be installed at BOOTLOADER_START address (0xFC00 default)An absolute call/jump is made to this location to do in-sytem reprogramming of UP- system will crash/reboot if the NPXISP code is not present.-LOCAL/BROADCASTAccepts BROADCAST Reprogram/boot messages.-NETPROG Message StructuresNETWORK MEssage Format0 DestinationAddr12 ActiveMessageID3 GroupID4 TOS Message Length(NESC) 0x205 MessageID6 Extended Msg ID (reserved-PrivateModeID)0xFF:broadcast(donotrespond) 0xnn:specified mote acks7 ProgramID89 Message Specific: Eg CapsuleID1011 Message Specific: Eg CommandStatus###FLASH SREC Structurebyt name0 ProgID1 2 CID34 SType5 NofBytes6 S0:ProgID, S1:Address78 S1:Instruction0...--------------------------------------------------------------------------INTERFACES-Download PhaseNP_DOWNLOAD_REQSignal to Client that an in-network program download operation has beenreceived (over-air using AM#47).Passes to client planned EEPROM start pageand number of pages to be used to store download programNP_DOWNLOAD_ACKAcknowledge from Client that NP download operation can/cannot proceed withdownload.NP_DOWNLOAD_DONESignal to Client that in-network program download operation has completed.Supplies actual EEROM Start Page, NofPages used to store downloaded programClient is now responsible for maintaining integrity of code image downloadedinto EEPROM (i.e. must not overwrite/modify the section of EEPROM)-Reprogram PhaseNP_REPROG_MSGSignal to client that a in-network reprogram/reboot message has been receivedNP_REPROG_REQCommand from client to initiate a reprogram/reboot operation.Client passes ProgramID which is matched with ID found in EEPROM. If matchUP reprogramming/reboot operation is started. Client should release all resources/ before issuing request.MAJOR STATESsEEMMODE EEFLASH StatesEE_WRITEEE_READ===============================================================================REVISION HISTORY02mar03 mm created from NPTB04apr03 mm major re-write - add broadcast download and missing capsule support17apr03 mm random delay in request for missing CID 12jul03 Jaein Jeong, Sukun Kim Sends acknowledgement explicitly when a mote finishes receiving capsules.****************************************************************************/includes Xnp;includes avr_eeprom;module XnpM { provides { interface Xnp; interface XnpConfig; interface StdControl; } uses { interface StdControl as XnpmControl; interface StdControl as GenericCommCtl; //interface StdControl as CommControl; interface StdControl as EEPROMControl; interface SendMsg;; interface ReceiveMsg; interface Leds; interface EEPROMRead; interface EEPROMWrite; interface Random; interface TS; }}implementation { /*----------------------DIRECTIVES ------------------------------------------*/ //#define JTAG //#define JTAGHEX 1 //#define MICA2DOT //#define new /*--------------------- INCLUDES --------------------------------------------*/ /*--------------------- DEFINES ---------------------------------------------*/#define TRUE -1#define FALSE 0#define SUCCESS 1#define FAIL 0#ifdef JTAG#define TOS_LOCAL 5 //hack !!!! Must update to application/clients LA //#define LOCAL_GROUP 0x13#endif#define DATA_LENGTH29 29 //XNP uses a 29byte data packet //netprogramming states#define SYSM_IDLE 0#define SYSM_DOWNLOAD 1#define SYSM_UPDATE 2#define SYS_IDLE 0#define SYS_IDLE_RENTRY 1#define SYS_ACK 2#define SYS_DL_START 60#define SYS_DL_START0 61#define SYS_DL_START1 62#define SYS_DL_START2 63#define SYS_DL_SRECWRITE 64#define SYS_UP_SRECWRITE 65 //Ending states#define SYS_DL_ABORT 70 //not used?#define SYS_DL_END 73#define SYS_DL_END_SIGNAL 74#define SYS_DL_FAIL 77#define SYS_DL_FAIL_SIGNAL 78 //Write States#define SYS_EEFLASH_WRITE 67#define SYS_EEFLASH_WRITEDONE 68 //Read/check states#define SYS_REQ_CIDMISSING 50#define SYS_GET_CIDMISSING 51#define SYS_GETNEXTCID 54 #define SYS_GETDELAYED 55#define SYS_GETDONE 56 //#define SYS_ISP_REQ 40 //isp#define SYS_ISP_REQ1 41#define SYS_GET_EEPID 43 //read PID from EEFLASH#define SYS_BEACON 91 //ATMega INTERNAL eeprom locations for storing TOS ID info used after reboot#define AVREEPROM_GROUPID_ADDR 0xFF2#define AVREEPROM_LOCALID_ADDR 0xFF0#define AVREEPROM_PID_ADDR 0xFF4 #define EEPROM_NOFPAGES 1000 //Nof 256byte pages in EEPROM //TEMP DEBUG //#define EEPROM_NOFPAGES 20 //Nof 256byte pages in EEPROM#define EE_PAGE_START 1 //Start of EPROM - this MUST match LOGGER.c start!!#define NOFBYTESPERLINE 16 //16bytes per EPROM line#define NOFLINESPERPAGE 16#define EE_LINE_START EE_PAGE_START<<4 //based on 16 lines per page!!#define EEPROM_LAST_LINE (EEPROM_NOFPAGES*NOFLINESPERPAGE) -1 //last eprom write address 11/03 //Application linebuffer#define ELBUFF_NOFLINES 2#define ELBUFF_SIZE NOFBYTESPERLINE * ELBUFF_NOFLINES #define ELBUFF_PERPAGE NOFLINESPERPAGE / ELBUFF_NOFLINES //#define EPROM_READ_SIZE (EPROM_SIZE/NOFBYTESPERLINE) -1 //#define EPROM_READ_SIZE 32768/4 //ATmel 4Mbit EEFLASH Definitions#define EEBUF_PAGESIZE 256 //ignore extra 4 bytes#define SAMPLE_NOFBYTES 6 //6bytes per sample set#define LASTLINE_NOFSAMPLES 2 //lastline of each page has only 2 sample sets#define EEBUF_NOFSAMPLES EEBUF_PAGESIZE/SAMPLE_NOFBYTES //positions in FLASH Srex #define POS_PID 0 //new#define POS_CID POS_PID+2#define POS_STYPE POS_CID+2#define POS_SNOFB POS_STYPE+1#define POS_S0_PID POS_SNOFB+1#define POS_S0_CRC POS_S0_PID+2#define POS_S1_ADDR POS_SNOFB+1#define POS_S1_I0 POS_S1_ADDR+2 //1st instruction //SREC format#define SX_TYPES2 0x02 //S2 type#define SX_LEN14 0x14 //20bytes length#define SREC_S0 0 //type S0#define SREC_S1 1 //type S1#define SREC_S9 9 //type S9 //structure of TOSMessage w/Stype record //Data offset into data packet#define TS_CMD 0 //command ID#define TS_SUBCMD 1 #define TS_PID 2 //program id#define TS_CAPSULEID 4 //Capsule ID location#define TS_MSGDATA 6#define TS_TYPE 6#define TS_LEN 7#define TS1_ADDR_MSB 8#define TS1_ADDR_LSB 9#define TS_INSTR0_MSB 10 //first data/instruction byte (msb) //TOS Messaging defines //Commands from network (wireless)#define CMD_START_DOWNLOAD 1 // sampl and write to eprom#define CMD_DOWNLOADING 2 //#define CMD_QUERY_COMPLETE 3 // received all the capsules.#define CMD_DOWNLOAD_STATUS 4 // request/rsponse with download status#define CMD_DOWNLOAD_COMPLETE 8 // DACTEST#define CMD_ISP_EXEC 5#define CMD_GET_PIDSTATUS 7 //Get Program ID#define CMD_GET_CIDMISSING 6 //from network //#define CMD_GET_CIDMISSING2 7#define CMD_REQ_CIDMISSING 6 //to network //#define CMD_SET_SRECUPDATE 34 //scattered srec download#define CMD_DUMP 32 // start reading data from ERPOM#define CMD_START_READ 33#define CMD_STATUS 9 // xmit status#define CMD_RST 99#define MSG_COMMAND 1 //RF message response to a command#define MSG_LOGREAD 0 //RF message is part of Log dump#define MSG_IDLE 2 //Undefined //NPX TOSMessage reponses#define CMD_STATE_DONE 0 //command accepted and processed#define CMD_STATE_BUSY 1 //command accepted but system busy#define CMD_STATE_WRONGSTATE 2 //not in correct state to process command#define CMD_STATE_BADSEQUENCE 3 //command or iteration out of sequence,order#define CMD_STATE_BADCHECKSUM 4 //bad SREC CHecksum#define CMD_STATE_BADSTYPE 5 //bad SREC Type#define CMD_STATE_REQCID 10 //requesting a missing CID #define CMD_STATE_UKNOWN 0xff //unknown command /* EEPROM Data storage Each page in EEPROM is 264Bytes. We only use 256 bytes The page is divided into Lines of 16bytes each. Sixteen (16) Lines make a Page (useable 256 bytes) */ typedef struct { uint8_t Line[NOFBYTESPERLINE]; } EEBUF_line_struct; //EELine structure - 16bytes#define DELAY_PACKETTIME 0xFF<<5 //0xFF approx 0.5mSec in mica2dot /* Define the TOS Frame and variables it owns */ // #define TOS_FRAME_TYPE NPX_frame uint8_t sSYSState; //CPU state:normal,sleep uint8_t sSYSMState; //major state TOS_Msg NETTOSMsg; //double buffer rx TOS_Msg msg; //tx buffer TOS_MsgPtr ptrmsg; // message ptr uint8_t bTOSMsgBusy; uint8_t TOSMsgType; TOS_MsgPtr pNETTOSMsg; //global incomming message for statemachine uint8_t bNXPMsgBusy; //processing NPX message uint8_t bBCASTMsg; uint8_t bAckMessage; //acknowlege Newtwork messages uint16_t wTXReqDelay; //Netprogramming stuff uint8_t cCommand; //current command uint8_t cSubCommand; //extendedcurrent command uint8_t bCommandOK; //current command processed ok uint16_t wProgramID; uint16_t wPIDinFLASH;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -