示例#1
0
// 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;
}
示例#2
0
void WriteflashCode()  //дÃÜÂë
{
    ReadflashInterval();
    ReadflashBoatnum();
    ReadStartTask();

    hal_flash_page_erase(34);
    PCON &= ~0x10;//PMW

    hal_flash_bytes_write(0xFC30,&codeflag,1);

    hal_flash_bytes_write(0xFC00,&timinteval_1,1);
    hal_flash_bytes_write(0xFC01,&timinteval_2,1);
    hal_flash_bytes_write(0xFC02,boatnum,16);
//	hal_flash_bytes_write(0xFC31,&taskstart,1);
}
示例#3
0
void WriteStartTask()  //д¿ª»ú״̬
{
    ReadflashBoatnum();
    ReadflashInterval();
    ReadflashCode();

    hal_flash_page_erase(34);
    PCON &= ~0x10;//PMW

//	hal_flash_bytes_write(0xFC31,&taskstart,1);
    delay(1000);
//	hal_flash_bytes_read(0xFC31,&taskstart,1);

    hal_flash_bytes_write(0xFC00,&timinteval_1,1);
    hal_flash_bytes_write(0xFC01,&timinteval_2,1);
    hal_flash_bytes_write(0xFC02,boatnum,16);
    hal_flash_bytes_write(0xFC30,&codeflag,1);
}
示例#4
0
void WriteflashBoatnum(void)  //дÈë´¬Ãû
{
    ReadflashInterval();
    ReadflashCode();
    ReadStartTask();

    hal_flash_page_erase(34);
    PCON &= ~0x10;//PMW

    hal_flash_bytes_write(0xFC02,&(com_buf[2]),16);
    delay(1000);
    hal_flash_bytes_read (0xFC02,boatnum,16);

    hal_flash_bytes_write(0xFC00,&timinteval_1,1); //ÔÙдÈë·¢Éä¼ä¸ô
    hal_flash_bytes_write(0xFC01,&timinteval_2,1);
    hal_flash_bytes_write(0xFC30,&codeflag,1);
//	hal_flash_bytes_write(0xFC31,&taskstart,1);
}
示例#5
0
static bool gzp_set_host_id(const uint8_t* src)
{
  if(hal_flash_byte_read(GZP_PARAMS_STORAGE_ADR) == 0xff)
  {
    hal_flash_byte_write(GZP_PARAMS_STORAGE_ADR, 0x00);
    hal_flash_bytes_write(GZP_PARAMS_STORAGE_ADR + 1, src, GZP_HOST_ID_LENGTH);
    return true;
  }
  else
  {
    return false;
  }
}
示例#6
0
void WriteflashInterval(void)  //дÈë¼ä¸ô
{
    ReadflashBoatnum(); //ÏȶÁ³ö´¬Ãû£¬ÔÙ²Áдҳ
    ReadflashCode();
    ReadStartTask();

    hal_flash_page_erase(34);
    PCON &= ~0x10;//PMW

    timinteval_1 = com_buf[4];  //putch(timinteval_1);
    timinteval_2 = com_buf[5];  //putch(timinteval_2);
    timinteval = timinteval_1;
    timinteval = (timinteval << 8);
    timinteval = timinteval + timinteval_2;

    hal_flash_bytes_write(0xFC00,&timinteval_1,1);
    hal_flash_bytes_write(0xFC01,&timinteval_2,1);

    hal_flash_bytes_write(0xFC02,boatnum,16);  //ÔÙдÈë´¬Ãû
    hal_flash_bytes_write(0xFC30,&codeflag,1);
//	hal_flash_bytes_write(0xFC31,&taskstart,1);
}
示例#7
0
// Verifies that update-start command is valid.
// Will enter RECEIVING_FIRMWARE state if everything checks out.
void startFirmwareUpdate(state_t *state, uint16_t *bytes_total, 
                         uint16_t *bytes_received, uint8_t *firmware_number)
{
  uint8_t i, checksum = 0, reset_vector[3];
  uint16_t bytes = 0;
              
  // Calculate checksum
  for (i = 0; i < UPDATE_START_LENGTH; i++) {
    checksum += MSG_PAYLOAD(i);
  }
  // Checksum fail
  if (checksum != 0) {
    send_buf[1] = ERROR_CHECKSUM_FAIL;
    send(CMD_NACK);
    return;
  }

  // Get firmware size 
  bytes = MSG_ST_BYTES;
  // Check that firmware is within legal size range.
  if (bytes > FLASH_FW_MAX_SIZE) {
    // Send error report to host.
    send_buf[1] = ERROR_ILLEGAL_SIZE;
    send(CMD_NACK);
    return;
  }
  *bytes_total = bytes;

  // Get firmware's reset vector. 
  temp_data[0] = MSG_ST_RESET_OPCODE;
  temp_data[1] = MSG_ST_RESET_ADDR_H;
  temp_data[2] = MSG_ST_RESET_ADDR_L;
  // Write reset vector to non-volatile flash
  hal_flash_page_erase(FW_NV_DATA_PAGE);
  hal_flash_bytes_write(FW_RESET_VECTOR, temp_data, 3);
  // Get firmware serial number. Will be written to NV when update complete.
  *firmware_number = MSG_ST_NUMBER;
  *bytes_received = 0;

  // Read out old reset vector.
  PCON |= PMW;
  hal_flash_bytes_read(0x0000, reset_vector, 3);
  PCON &= ~PMW;
  // Erase first page, containing reset vector.
  hal_flash_page_erase(0);
  // Write back the old reset vector.
  PCON |= PMW;
  hal_flash_bytes_write(0x0000, reset_vector, 3);
  PCON &= ~PMW;
  // Erase the reset of pages available to firmware.
  for (i = 1; i < FLASH_FW_PAGES; i++) {
    hal_flash_page_erase(i);
  }

  send(CMD_ACK);
  if (send_success) {
    *state = RECEIVING_FIRMWARE;
  } else {
    *state = LISTENING;
  }

  return;
}
示例#8
0
static void gzp_params_db_add(const uint8_t* src_element, uint8_t index)
{
  hal_flash_bytes_write(GZP_PARAMS_DB_ADR + (index * GZP_PARAMS_DB_ELEMENT_SIZE), src_element, GZP_PARAMS_DB_ELEMENT_SIZE);
}