-bool hdl_da_readback(const hdl_readback_arg_t *readback_arg)
-{
- bool success = FALSE;
- uint32_t error_status = 0;
- const uint32_t readback_flash_addr = readback_arg->readback_flash_addr;
- const uint32_t readback_total_len = readback_arg->readback_total_len;
- HDL_COM_PutByte(DA_READ_CMD);
- HDL_COM_PutData32(readback_flash_addr);
- HDL_COM_PutData32(readback_total_len);
- HDL_COM_PutData32(DA_RECV_PACKET_LEN);
-
- // DA Check Readback Arg
- bool da_check_success = hdl_da_check_cmd_arg(&error_status);
- HDL_Require_Noerr_Action(da_check_success, exit, "DA Readback CMD, Check Range");
-
- // Readback init callback
- if (readback_arg->readback_init_cb != NULL) {
- readback_arg->readback_init_cb(readback_arg->readback_init_cb_arg);
- }
-
- // Readback Packet(4K) & Callback
- const uint32_t packet_num = ((readback_total_len - 1) / DA_RECV_PACKET_LEN) + 1;
- uint32_t packet_read_num = 0;
- uint32_t read_total_bytes = 0;
- while (packet_read_num < packet_num) {
- const bool is_last_packet = (packet_read_num == (packet_num - 1));
- const uint32_t start_offset = packet_read_num * DA_RECV_PACKET_LEN;
- const uint32_t cur_packet_len = is_last_packet ? (readback_total_len - start_offset) : DA_RECV_PACKET_LEN;
- HDL_LOGI("hdl_da_readback packet %d/%d %d", packet_read_num + 1, packet_num, cur_packet_len);
-
- uint32_t packet_read_bytes = 0;
- uint8_t *packet_read_buf = g_hdl_data_buf;
- while (packet_read_bytes < cur_packet_len) {
- // Even if last_packet, should also read DA_RECV_PACKET_LEN
- packet_read_bytes += HDL_COM_GetByte_Buffer(packet_read_buf + packet_read_bytes,
- DA_RECV_PACKET_LEN - packet_read_bytes);
- }
- read_total_bytes += cur_packet_len;
- #if defined (HDL_VIA_I2C)
- // ToDo Note: Wait DA to compute checksum
- // hdl_delay(50);
- #endif
- uint32_t packet_checksum_from_local = hdl_compute_simple_checksum(packet_read_buf, cur_packet_len);
- uint32_t packet_checksum_from_device = HDL_COM_GetData32();
- HDL_Require_Noerr_Action(packet_checksum_from_device == packet_checksum_from_local, exit,
- "hdl_readback packet checksum");
- // Readback Callback
- if (readback_arg->readback_cb != NULL) {
- readback_arg->readback_cb(readback_arg->readback_cb_arg, readback_flash_addr + start_offset,
- packet_read_buf, cur_packet_len,
- read_total_bytes, readback_total_len);
- }
-
- // Next
- HDL_COM_PutByte(DA_ACK);
-
- packet_read_num++;
- }
- if (read_total_bytes == readback_total_len) {
- success = TRUE;
- }
-
-exit:
- if (error_status != 0) {
- HDL_LOGE("hdl_da_readback error_status=%d", error_status);
- }
- return success;
-}