/* Write the data loaded using the Load Page Data command to flash. * Syntax: pp [pagenum] * Response: status code */ static void ProgramPage_CMD(const char* cmdbuf, uint8_t len) { uint16_t page; const char *endptr; cmdstatus_t status = eCmdOK; cmdbuf += 3; // two command chars and a space page = htou(cmdbuf, &endptr, 255); // the arg is missing if these are equal if(endptr != cmdbuf) { if(page < NUM_APP_PAGES) { uint16_t i = 0; for( ; i < SPM_PAGESIZE; i += 2) Flash_WriteLatch(i, *(uint16_t *)&m_pagebuf[i]); Flash_ProgramPage(page * SPM_PAGESIZE); if(!Flash_VerifyPage(page * SPM_PAGESIZE, (uint16_t *)m_pagebuf)) status = eCmdErrMismatch; } else status = eCmdErrRange; } else status = eCmdErrArgs; memset(m_pagebuf, 0xFF, sizeof(m_pagebuf)); PrintCmdStatus(status); }
/* Calculate and write the application's CRC to flash memory. This must be the last step in * programming a new application on the board. * Syntax: wc * Response: status code */ static void WriteCRC_CMD(const char* cmdbuf, uint8_t len) { cmdstatus_t status = eCmdOK; uint16_t appcrc = CalculateAppCRC(); // The CRC and validation key go into the last words of flash Flash_WriteLatch(SPM_PAGESIZE - 4, APP_CHECKSUM_VALID); Flash_WriteLatch(SPM_PAGESIZE - 2, appcrc); Flash_ProgramPage(FLASHEND); if(pgm_read_word(FLASHEND - 3) != APP_CHECKSUM_VALID || pgm_read_word(FLASHEND - 1) != appcrc) { status = eCmdErrMismatch; } PrintCmdStatus(status); }
uint8_t xboot_write_application_page(uint32_t address, uint8_t *data, uint8_t erase) { uint8_t saved_status = SREG; if (address > BOOT_SECTION_START) return XB_INVALID_ADDRESS; cli(); Flash_ProgramPage(address, data, erase); #ifdef __AVR_XMEGA__ NVM_CMD = NVM_CMD_NO_OPERATION_gc; #endif // __AVR_XMEGA__ SREG = saved_status; return XB_SUCCESS; }