// Writes hex-record's data field to flash memory. // Will update bytes_received and send reply to host. void writeHexRecord(state_t *state, uint16_t *bytes_received) { uint8_t i, checksum = 0, bytes = MSG_WR_BYTE_COUNT; uint16_t addr = MSG_WR_ADDR; // Disable RF receiving while writing. CE_LOW(); // Calculate checksum for message. for (i = 0; i < bytes+HEX_BYTES; i++) { checksum += MSG_PAYLOAD(i); } if (checksum != 0) { // Checksum fail send_buf[1] = ERROR_CHECKSUM_FAIL; send(CMD_NACK); return; } // Copy data portion of payload to idata temp memory. for (i = 0; i < bytes; i++) { temp_data[i] = MSG_WR_DATA(i); } // This will prevent the reset vector from being overwritten. if (addr == 0x0000) { PCON |= PMW; // Offset write with the 3 bytes of the reset vector hal_flash_bytes_write((addr+3), (temp_data+3), (bytes-3)); PCON &= ~PMW; // Make sure that bytes to be written is within legal pages. } else if (addr+bytes < FLASH_FW_MAX_SIZE) { // Write line to flash. PCON |= PMW; hal_flash_bytes_write(addr, temp_data, bytes); PCON &= ~PMW; // Address is outside pages available to new firmware. } else { // Invalid address send_buf[1] = ERROR_ILLEGAL_ADDRESS; send(CMD_NACK); return; } // Add bytes to total received. *bytes_received += bytes; // Acknowledge message send(CMD_ACK); if (!send_success) { *state = ERROR; } return; }
// Writes hex-record's data field to flash memory. // Will update bytes_received and send reply to host. static void writeHexRecord() { uint8_t i, checksum = 0, bytes = MSG_WR_BYTE_COUNT; uint16_t addr = MSG_WR_ADDR; // Calculate checksum for message. for (i = 0; i < bytes+HEX_BYTES; i++) { checksum += MSG_PAYLOAD(i); } if (checksum != 0) { // Checksum fail send_buf[1] = ERROR_CHECKSUM_FAIL; send(CMD_NACK,2); return; } // Copy data portion of payload to idata temp memory. for (i = 0; i < bytes; i++) { temp_data[i] = MSG_WR_DATA(i); } /* // This will prevent the reset vector from being overwritten. if (addr == 0x0000) { movx_access_code();; // Offset write with the 3 bytes of the reset vector flash_write_bytes((addr+3), (temp_data+3), (bytes-3)); movx_access_data(); // Make sure that bytes to be written is within legal pages. } else */ if (addr+bytes < FLASH_FW_END && addr >= FLASH_FW_BEGIN) { // Write line to flash. movx_access_code(); flash_write_bytes(addr, temp_data, bytes); movx_access_data(); // Address is outside pages available to new firmware. } else { send_buf[1] = ERROR_ILLEGAL_ADDRESS; send(CMD_NACK,2); return; } // Add bytes to total received. bytes_received += bytes; // Acknowledge message send(CMD_ACK,1); if (!send_success) { state = ERROR; } return; }