📄 inpisp.c
字号:
pEEBuffW = &EEBuff[POS_S1_ADDR]; //lsbyte in 2, most in 3 //NOTE: wDestAddrW and wUAddress are BYTE addresses. wDestAddrW = (UINT16)*pEEBuffW; pEEBuffW = &EEBuff[POS_S1_I0]; //point to first data element for (i=0;i<cNofBytes-4;i+=2 ){ //go by words if( wUAddress != (wDestAddrW & 0xFF00) ) { //check crossing 128 word boundary CLR_RED_LED_PIN(); fUPageFlush(wUAddress); //flush the buffer into UP program memory wUAddress = wDestAddrW & 0xFF00; //make sure it is a page boundary SET_RED_LED_PIN(); } //new UP page //write instruction word (2bytes) into UP buffer at Word aligned addresses page_load(pEEBuffW, wDestAddrW); //note UAddr stays at page boundary pEEBuffW++; wDestAddrW +=2; //increment by 2 bytes }//for i break; //S1 rec case STYPE_9: //S9 Parse - boot address fUPageFlush(wUAddress); //flush the last buffer into UP program memory pEEBuffW = &EEBuff[POS_S1_ADDR]; //same struct as S1 header wDestAddrW = (UINT16)*pEEBuffW; //cheat - bigendian/littleendia pbm!! bOK = FINI; break; case STYPE_0: //verify correct ProgramID //S0 Parse - ProgID pEEBuffW = &EEBuff[POS_S0_PID]; wPID = (UINT16)*pEEBuffW; #ifdef old if( wPID != wProgID) bOK = ERROR; //exit-wrong PID =legacy code/ignore record#endif break; default: //ignore the record - move on to next... //bOK = ERROR; break; }//switch TYPE//advance to next position in EEFlash EEByteA += EE_SRECSIZE; //stepping thru flash page if( EEByteA > EE_BYTESPERPAGE-1 ){ //go to next EE Flash page fEEStopRead(); //deselect the eeprom EEPageA++; EEByteA = 0; fEEStartRead( EEPageA, EEByteA); //wait } }//while bOK// Here if done with programming or error//finished UP reprogramming fEEStopRead(); //deselect the flashif( bOK == ERROR ) { reset(); //reboot :)// sei(); //reenable interrupts // return(FALSE); //wrong PID,no UP reprogramming done - return to application }//Re Enable the Application section of uC Memory and wait until uC is ready// while (inp(SPMCR) & (1<<RWWSB)) { __SPM(wUAddress,APP_ENABLE);// } //while inp//UP Reprogrammed.#ifdef new1//update EEPROM with programid pEEBuff = &wProgID; //the prog id eeprom_write_byte (AVREEPROM_PID_ADDR, *pEEBuff++);//lsbyte eeprom_write_byte (AVREEPROM_PID_ADDR+1, *pEEBuff);//msbyte#endif SET_GREEN_LED_PIN(); reset(); //reboot :) return(TRUE); //never get here...}/*****************************************************************************fUPageFlushErase and flush Instruction buffer into UP Program Memory page*****************************************************************************/void fUPageFlush( UINT16 wUAddress ) { page_erase(wUAddress); //erase old program memory fSPMWait(); //wait page_erase is finished //Flush(write) UF page buffer into flash page_write(wUAddress); //write/flush uC buffer to FLASH - i.e. program it fSPMWait(); //wait page_write is finished return;}/*****************************************************************************resetpass address for restart*****************************************************************************/void reset() { int i; wdt_enable(1); while(1){ //we be waiting... CLR_RED_LED_PIN(); for (i=1; i != 0; i++); SET_RED_LED_PIN(); for (i=1; i != 0; i++); }}/*****************************************************************************fSPMWaitwait for SPM mode to completew*****************************************************************************/void fSPMWait(void) { while (inp(SPMCR) & (SPMEN_BIT)) return;}/*****************************************************************************waitwaste some time*****************************************************************************/void wait(void) { char i; for (i =0; i < 20; i++) { __asm__ __volatile__ ("nop" "\n\t"::); }}/*****************************************************************************page_eraseErase FLASH pageaddr is address of 1st byte in page. This is put in Z register.Does NOT wait for SPM operation to complete before returning*****************************************************************************/void page_erase(unsigned short addr) { __SPM(addr, PAGE_ERASE); while(inp(SPMCR) & 0x01);}/*****************************************************************************page_writeWrite uC FLASH pageaddr is address of 1st byte in page. This is put in Z register.Waits until SPM operation is complete before returning*****************************************************************************/void page_write(unsigned short addr) { __SPM(addr, PAGE_WRITE); while(inp(SPMCR) & 0x01); //wait for it to finish}/*****************************************************************************page_load (instr,addr)Store uC instruction (word) at addr in Page bufferaddr is a full 16bit byte address (includes Page address)Instruction (instr) is placed in r0:r1__SPM places addr in Z reg and executes PAGE_LOAD operationWaits for SPM operation to complete before returning*****************************************************************************/void page_load(unsigned short *pInstr, unsigned short addr) { unsigned short instr; instr = *pInstr; __asm__ __volatile__ ("movw r0, %0" "\n\t"::"r" (instr):"r0", "r1"); __SPM(addr, PAGE_LOAD); __asm__ __volatile__ ("clr r1" "\n\t"::); while(inp(SPMCR) & 0x01);}/*****************************************************************************fEEGetInstructionReturns an instruction read from EEFlashIncrements to next byte in EEFashIf at end of 256byte page (in TOS the pages are considered 256 bytes, not 264)releases CS - stopping readout - and issues fEEStartRead at next page.*****************************************************************************/unsigned short fEEGetInstruction(void) { unsigned short retval; UINT16 i; retval = fSPIByte(0)& 0x00ff; //lsbyte i = (UINT16) (fSPIByte(0)<<8); retval = i | retval; //make 16bits return retval;}/*****************************************************************************fEERead32Read 32 bytes from flash into buffer*****************************************************************************/UINT8 fEERead32( UINT8 *pEEBuff ) { char i; for (i=0;i<32;i++ ) pEEBuff[i] = fSPIByte(0); return(1);}/*****************************************************************************fEEStopReadReleases EEFlash CS thereby terminating continuous readout operation*****************************************************************************/void fEEStopRead(void) { //select the flash MAKE_FLASH_SELECT_OUTPUT(); SET_FLASH_SELECT_PIN(); return;}/*****************************************************************************fEEStartRead(PageAddress,ByteAddress)Setup EEFlash for continuous readoutEE Opcode = 0x68Instruction Format<opcode:8><address:24><fill:32><address:24> = <0x0:4, PA[10-0]:11, BA[8-0]:9>Assert CSXmit InstructionAt end of instruction, SPI bus turned around to read in data bit on each SCLK.CS is remains assertedSucceeding SCLKS clock in bitwise data starting at <address> until CS released fEEStartReadExecute an command+address to EEFlash*****************************************************************************/char fEEStartRead( UINT16 PA, UINT16 BA) {// void fEEExecCommand(UINT8 opcode, UINT16 PA, UINT16 BA) { // byte address, there are 8bytes per page that cannot be accessed. Will // use for CRC, etc. // command buffer is filled in reverse char cmdBuf[4]; char i; cmdBuf[0] = EE_AUTOREAD; //opcode; //EE Flash opcode cmdBuf[1] = (PA>>7)& 0x0F; //PA[10:7] in lower nibble cmdBuf[2] = (PA<<1) + (BA>>8); //PA[6:0]+ BA[8] cmdBuf[3] = (UINT8) BA; //BA[7:0] //select the flash MAKE_FLASH_SELECT_OUTPUT(); CLR_FLASH_SELECT_PIN(); for( i=0;i<4;i++) fSPIByte(cmdBuf[i]); //writeout the command for( i=0;i<4;i++) fSPIByte(0xAA); //write out 4 fill bytes //EEFlash requires 1 additional (65th) clock to setup data on SOut pin fSPIClockCycle(); return(0); }/*****************************************************************************fEEReadyGet EEFLash status (busy/idle)issue 8bit opcode, read back status byte. MSB is READY/Busy*****************************************************************************/char fEEReady(void) { fSPIByte(EE_STATUS); return(fSPIByte(0) & 0x80);}//fEEStatus /*****************************************************************************fSPIInit*****************************************************************************/char fSPIInit(void) { CLR_FLASH_CLK_PIN(); MAKE_FLASH_CLK_OUTPUT(); SET_FLASH_OUT_PIN(); MAKE_FLASH_OUT_OUTPUT(); CLR_FLASH_IN_PIN(); MAKE_FLASH_IN_INPUT(); return 1;}/*****************************************************************************char fSPIByteWrite byte out spi portRead byte in spi in port*****************************************************************************/UINT8 fSPIByte( UINT8 cOut) { UINT8 i; UINT8 spi_out = cOut; UINT8 spi_in = 0; for (i=0; i < 8; i++) { SPIOUTPUT; SET_FLASH_CLK_PIN(); SPIINPUT; CLR_FLASH_CLK_PIN(); } return(spi_in);}/*****************************************************************************void fSPIClock(void) Toggle the SPIclk line*****************************************************************************/void fSPIClockCycle(void) { SET_FLASH_CLK_PIN(); wait1(); CLR_FLASH_CLK_PIN(); return;}/**********************************************************************************************************************************************************//*****************************************************************************//*ENDOFFILE******************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -