Add blisp_easy, many improvements, adds iot single download feature

This commit is contained in:
Marek Kraus 2023-01-07 23:25:42 +01:00
parent ed2c9b80b8
commit 59cf5fb038
12 changed files with 430 additions and 140 deletions

View File

@ -8,7 +8,7 @@ option(BLISP_BUILD_CLI "Build CLI Tool" OFF)
add_library(libblisp_obj OBJECT add_library(libblisp_obj OBJECT
lib/blisp.c lib/blisp.c
lib/chip/blisp_chip_bl60x.c lib/chip/blisp_chip_bl60x.c
lib/chip/blisp_chip_bl70x.c) lib/chip/blisp_chip_bl70x.c lib/blisp_easy.c)
target_include_directories(libblisp_obj PRIVATE ${CMAKE_SOURCE_DIR}/include/) target_include_directories(libblisp_obj PRIVATE ${CMAKE_SOURCE_DIR}/include/)

47
include/blisp_easy.h Normal file
View File

@ -0,0 +1,47 @@
// SPDX-License-Identifier: MIT
#ifndef BLISP_BLISP_EASY_H
#define BLISP_BLISP_EASY_H
#include <stdio.h>
#include "blisp.h"
struct blisp_easy_transport {
uint8_t type; // 0 - memory, 1 - FILE file_handle
union {
FILE* file_handle;
struct {
void* data_location;
uint32_t data_size;
uint32_t current_position;
} memory;
} data;
};
enum blisp_easy_error {
BLISP_EASY_ERR_TRANSPORT_ERROR = -100,
BLISP_EASY_ERR_CHECK_IMAGE_FAILED = -101
};
typedef void (*blisp_easy_progress_callback)(uint32_t current_value,
uint32_t max_value);
struct blisp_easy_transport blisp_easy_transport_new_from_file(FILE* file);
int32_t blisp_easy_load_segment_data(
struct blisp_device* device,
uint32_t segment_size,
struct blisp_easy_transport* segment_transport,
blisp_easy_progress_callback progress_callback);
int32_t blisp_easy_load_ram_image(
struct blisp_device* device,
struct blisp_easy_transport* image_transport,
blisp_easy_progress_callback progress_callback);
int32_t blisp_easy_flash_write(struct blisp_device* device,
struct blisp_easy_transport* data_transport,
uint32_t flash_location,
uint32_t data_size,
blisp_easy_progress_callback progress_callback);
#endif // BLISP_BLISP_EASY_H

View File

@ -17,6 +17,7 @@ static void blisp_dlog(const char* format, ...)
va_start(args, format); va_start(args, format);
vfprintf(stderr, format, args); vfprintf(stderr, format, args);
va_end(args); va_end(args);
fputc('\n', stderr);
} }

View File

@ -211,7 +211,7 @@ int32_t blisp_device_handshake(struct blisp_device* device, bool in_ef_loader) {
} }
} }
} }
blisp_dlog("Received no response from chip"); blisp_dlog("Received no response from chip.");
return BLISP_ERR_NO_RESPONSE; return BLISP_ERR_NO_RESPONSE;
} }

137
lib/blisp_easy.c Normal file
View File

@ -0,0 +1,137 @@
// SPDX-License-Identifier: MIT
#include "blisp_easy.h"
#include "blisp_struct.h"
#include <inttypes.h>
static int64_t blisp_easy_transport_read(struct blisp_easy_transport* transport,
void* buffer,
uint32_t size) {
if (transport->type == 0) {
} else {
return fread(buffer, size, 1, transport->data.file_handle);
}
}
static void blisp_easy_report_progress(blisp_easy_progress_callback callback,
uint32_t current_value,
uint32_t max_value) {
if (callback != NULL) {
callback(current_value, max_value);
}
}
struct blisp_easy_transport blisp_easy_transport_new_from_file(FILE* file) {
struct blisp_easy_transport transport = {.type = 1, .data.file_handle = file};
return transport;
}
int32_t blisp_easy_load_segment_data(
struct blisp_device* device,
uint32_t segment_size,
struct blisp_easy_transport* segment_transport,
blisp_easy_progress_callback progress_callback) {
int32_t ret;
uint32_t sent_data = 0;
uint32_t buffer_size = 0;
uint8_t buffer[4092];
blisp_easy_report_progress(progress_callback, 0, segment_size);
while (sent_data < segment_size) {
buffer_size = segment_size - sent_data;
if (buffer_size > 4092) {
buffer_size = 4092;
}
blisp_easy_transport_read(segment_transport, buffer, buffer_size); // TODO: Error Handling
ret = blisp_device_load_segment_data(device, buffer,
buffer_size);
if (ret < BLISP_OK) {
// TODO: Error logging fprintf(stderr, "Failed to load segment data. (ret
// %d)\n", ret);
return BLISP_EASY_ERR_TRANSPORT_ERROR;
}
sent_data += buffer_size;
blisp_easy_report_progress(progress_callback, sent_data, segment_size);
}
return 0;
}
int32_t blisp_easy_load_ram_image(
struct blisp_device* device,
struct blisp_easy_transport* image_transport,
blisp_easy_progress_callback progress_callback) {
int32_t ret;
struct bfl_boot_header image_boot_header;
// TODO: Error handling
blisp_easy_transport_read(image_transport, &image_boot_header, 176);
ret = blisp_device_load_boot_header(device, (uint8_t*)&image_boot_header);
if (ret != BLISP_OK) {
// TODO: Error printing: fprintf(stderr, "Failed to load boot header.\n");
return BLISP_EASY_ERR_TRANSPORT_ERROR;
}
{
for (uint8_t seg_index = 0;
seg_index < image_boot_header.segment_info.segment_cnt; seg_index++) {
struct blisp_segment_header segment_header = {0};
blisp_easy_transport_read(image_transport, &segment_header,
16); // TODO: Error handling
ret = blisp_device_load_segment_header(device, &segment_header);
if (ret != 0) {
// TODO: Error printing: fprintf(stderr, "Failed to load segment
// header.");
return BLISP_EASY_ERR_TRANSPORT_ERROR;
}
// TODO: Info printing: printf("Flashing %d. segment\n", seg_index + 1);
ret = blisp_easy_load_segment_data(device, segment_header.length,
image_transport, progress_callback);
if (ret != 0) {
return BLISP_EASY_ERR_TRANSPORT_ERROR;
}
}
}
ret = blisp_device_check_image(device);
if (ret != BLISP_OK) {
// TODO: Error printing: fprintf(stderr, "Failed to check image.\n");
return BLISP_EASY_ERR_CHECK_IMAGE_FAILED;
}
return BLISP_OK;
}
int32_t blisp_easy_flash_write(struct blisp_device* device,
struct blisp_easy_transport* data_transport,
uint32_t flash_location,
uint32_t data_size,
blisp_easy_progress_callback progress_callback) {
int32_t ret;
uint32_t sent_data = 0;
uint32_t buffer_size = 0;
uint8_t buffer[8184];
blisp_easy_report_progress(progress_callback, 0, data_size);
while (sent_data < data_size) {
buffer_size = data_size - sent_data;
if (buffer_size > 2052) {
buffer_size = 2052;
}
blisp_easy_transport_read(data_transport, buffer, buffer_size); // TODO: Error Handling
ret = blisp_device_flash_write(device, flash_location + sent_data, buffer,
buffer_size);
if (ret < BLISP_OK) {
// TODO: Error logigng: fprintf(stderr, "Failed to write firmware! (ret:
// %d)\n", ret);
return ret;
}
sent_data += buffer_size;
blisp_easy_report_progress(progress_callback, sent_data, data_size);
}
return BLISP_OK;
}

View File

@ -1,6 +1,6 @@
add_subdirectory(${CMAKE_SOURCE_DIR}/vendor/argtable3 ${CMAKE_CURRENT_BINARY_DIR}/argtable3) add_subdirectory(${CMAKE_SOURCE_DIR}/vendor/argtable3 ${CMAKE_CURRENT_BINARY_DIR}/argtable3)
add_executable(blisp src/main.c src/cmd/write.c src/util.c src/common.c src/common.h) add_executable(blisp src/main.c src/cmd/write.c src/util.c src/common.c src/cmd/iot.c)
target_include_directories(blisp PRIVATE target_include_directories(blisp PRIVATE
"${CMAKE_SOURCE_DIR}/include" "${CMAKE_SOURCE_DIR}/include"

View File

@ -13,5 +13,6 @@ struct cmd {
}; };
extern struct cmd cmd_write; extern struct cmd cmd_write;
extern struct cmd cmd_iot;
#endif // BLISP_CMD_H #endif // BLISP_CMD_H

140
tools/blisp/src/cmd/iot.c Normal file
View File

@ -0,0 +1,140 @@
#include <blisp_easy.h>
#include "../cmd.h"
#include "../common.h"
#include <argtable3.h>
#define REG_EXTENDED 1
#define REG_ICASE (REG_EXTENDED << 1)
static struct arg_rex* cmd;
static struct arg_file* single_download;
static struct arg_int* single_download_location;
static struct arg_str *port_name, *chip_type; // TODO: Make this common
static struct arg_lit* reset;
static struct arg_end* end;
static void* cmd_iot_argtable[7];
void blisp_single_download()
{
struct blisp_device device;
int32_t ret;
if (blisp_common_init_device(&device, port_name, chip_type) != 0) {
return;
}
if (blisp_common_prepare_flash(&device) != 0) {
// TODO: Error handling
goto exit1;
}
FILE* data_file = fopen(single_download->filename[0], "rb");
if (data_file == NULL) {
fprintf(stderr, "Failed to open data file \"%s\".\n",
single_download->filename[0]);
goto exit1;
}
fseek(data_file, 0, SEEK_END);
int64_t data_file_size = ftell(data_file);
rewind(data_file);
printf("Erasing the area, this might take a while...\n");
ret =
blisp_device_flash_erase(&device, *single_download_location->ival,
*single_download_location->ival + data_file_size + 1);
if (ret != BLISP_OK) {
fprintf(stderr, "Failed to erase.\n");
goto exit2;
}
printf("Writing the data...\n");
struct blisp_easy_transport data_transport =
blisp_easy_transport_new_from_file(data_file);
ret = blisp_easy_flash_write(&device, &data_transport, *single_download_location->ival,
data_file_size,
blisp_common_progress_callback);
if (ret < BLISP_OK) {
fprintf(stderr, "Failed to write data to flash.\n");
goto exit2;
}
printf("Checking program...\n");
ret = blisp_device_program_check(&device);
if (ret != BLISP_OK) {
fprintf(stderr, "Failed to check program.\n");
goto exit2;
}
printf("Program OK!\n");
if (reset->count > 0) { // TODO: could be common
blisp_device_reset(&device);
printf("Resetting the chip.\n");
}
printf("Download complete!\n");
exit2:
if (data_file != NULL)
fclose(data_file);
exit1:
blisp_device_close(&device);
}
int8_t cmd_iot_args_init() {
cmd_iot_argtable[0] = cmd =
arg_rex1(NULL, NULL, "iot", NULL, REG_ICASE, NULL);
cmd_iot_argtable[1] = chip_type =
arg_str1("c", "chip", "<chip_type>", "Chip Type");
cmd_iot_argtable[2] = port_name =
arg_str0("p", "port", "<port_name>",
"Name/Path to the Serial Port (empty for search)");
cmd_iot_argtable[3] = reset =
arg_lit0(NULL, "reset", "Reset chip after write");
cmd_iot_argtable[4] = single_download =
arg_file0("s", "single-down", "<file>", "Single download file");
cmd_iot_argtable[5] = single_download_location =
arg_int0("l", "single-down-loc", NULL, "Single download offset");
cmd_iot_argtable[6] = end = arg_end(10);
if (arg_nullcheck(cmd_iot_argtable) != 0) {
fprintf(stderr, "insufficient memory\n");
return -1;
}
return 0;
}
void cmd_iot_args_print_glossary() {
fputs("Usage: blisp", stdout);
arg_print_syntax(stdout, cmd_iot_argtable, "\n");
puts("Flashes firmware as Bouffalo's DevCube");
arg_print_glossary(stdout, cmd_iot_argtable, " %-25s %s\n");
}
uint8_t cmd_iot_parse_exec(int argc, char** argv) {
int errors = arg_parse(argc, argv, cmd_iot_argtable);
if (errors == 0) {
if (single_download->count == 1 && single_download_location->count == 1) {
blisp_single_download();
return 1;
} else {
return 0;
}
} else if (cmd->count == 1) {
cmd_iot_args_print_glossary();
return 1;
}
return 0;
}
void cmd_iot_args_print_syntax() {
arg_print_syntax(stdout, cmd_iot_argtable, "\n");
}
void cmd_iot_free() {
arg_freetable(cmd_iot_argtable,
sizeof(cmd_iot_argtable) / sizeof(cmd_iot_argtable[0]));
}
struct cmd cmd_iot = {"iot", cmd_iot_args_init, cmd_iot_parse_exec,
cmd_iot_args_print_syntax, cmd_iot_free};

View File

@ -6,8 +6,9 @@
#include "../cmd.h" #include "../cmd.h"
#include "../common.h" #include "../common.h"
#include "../util.h" #include "../util.h"
#include "argtable3.h" #include <argtable3.h>
#include "blisp_struct.h" #include <blisp_easy.h>
#include <blisp_struct.h>
#define REG_EXTENDED 1 #define REG_EXTENDED 1
#define REG_ICASE (REG_EXTENDED << 1) #define REG_ICASE (REG_EXTENDED << 1)
@ -164,37 +165,14 @@ void fill_up_boot_header(struct bfl_boot_header* boot_header) {
} }
void blisp_flash_firmware() { void blisp_flash_firmware() {
if (chip_type->count == 0) {
fprintf(stderr, "Chip type is invalid.\n");
return;
}
struct blisp_chip* chip = NULL;
if (strcmp(chip_type->sval[0], "bl70x") == 0) {
chip = &blisp_chip_bl70x;
} else if (strcmp(chip_type->sval[0], "bl60x") == 0) {
chip = &blisp_chip_bl60x;
} else {
fprintf(stderr, "Chip type is invalid.\n");
return;
}
struct blisp_device device; struct blisp_device device;
int32_t ret; int32_t ret;
ret = blisp_device_init(&device, chip);
if (ret != BLISP_OK) { if (blisp_common_init_device(&device, port_name, chip_type) != 0) {
fprintf(stderr, "Failed to init device.\n");
return;
}
ret = blisp_device_open(&device,
port_name->count == 1 ? port_name->sval[0] : NULL);
if (ret != BLISP_OK) {
fprintf(stderr, "Failed to open device.\n");
return; return;
} }
if (blisp_prepare_flash(&device) != 0) { if (blisp_common_prepare_flash(&device) != 0) {
// TODO: Error handling // TODO: Error handling
goto exit1; goto exit1;
} }
@ -213,61 +191,47 @@ void blisp_flash_firmware() {
fill_up_boot_header(&boot_header); fill_up_boot_header(&boot_header);
const uint32_t firmware_base_address = 0x2000; const uint32_t firmware_base_address = 0x2000;
printf("Erasing flash, this might take a while..."); printf("Erasing flash, this might take a while...\n");
ret = ret =
blisp_device_flash_erase(&device, firmware_base_address, blisp_device_flash_erase(&device, firmware_base_address,
firmware_base_address + firmware_file_size + 1); firmware_base_address + firmware_file_size + 1);
if (ret != BLISP_OK) { if (ret != BLISP_OK) {
fprintf(stderr, "\nFailed to erase flash.\n"); fprintf(stderr, "Failed to erase flash.\n");
goto exit2; goto exit2;
} }
ret = ret =
blisp_device_flash_erase(&device, 0x0000, sizeof(struct bfl_boot_header)); blisp_device_flash_erase(&device, 0x0000, sizeof(struct bfl_boot_header));
if (ret != BLISP_OK) { if (ret != BLISP_OK) {
fprintf(stderr, "\nFailed to erase flash.\n"); fprintf(stderr, "Failed to erase flash.\n");
goto exit2; goto exit2;
} }
printf(" OK!\nFlashing boot header..."); printf("Flashing boot header...\n");
ret = blisp_device_flash_write(&device, 0x0000, (uint8_t*)&boot_header, ret = blisp_device_flash_write(&device, 0x0000, (uint8_t*)&boot_header,
sizeof(struct bfl_boot_header)); sizeof(struct bfl_boot_header));
if (ret != BLISP_OK) { if (ret != BLISP_OK) {
fprintf(stderr, "\nFailed to write boot header.\n"); fprintf(stderr, "Failed to write boot header.\n");
goto exit2; goto exit2;
} }
printf(" OK!\nFlashing the firmware...\n"); printf("Flashing the firmware...\n");
{ struct blisp_easy_transport data_transport =
uint32_t sent_data = 0; blisp_easy_transport_new_from_file(firmware_file);
uint32_t buffer_size = 0;
uint8_t buffer[8184];
printf("0b / %ldb (0.00%%)\n", firmware_file_size);
while (sent_data < firmware_file_size) { ret = blisp_easy_flash_write(&device, &data_transport, firmware_base_address,
buffer_size = firmware_file_size - sent_data; firmware_file_size,
if (buffer_size > 2052) { blisp_common_progress_callback);
buffer_size = 2052;
}
fread(buffer, buffer_size, 1, firmware_file);
ret = blisp_device_flash_write(&device, firmware_base_address + sent_data,
buffer,
buffer_size); // TODO: Error handling
if (ret < BLISP_OK) { if (ret < BLISP_OK) {
fprintf(stderr, "Failed to write firmware! (ret: %d)\n", ret); fprintf(stderr, "Failed to write app to flash.\n");
goto exit2; goto exit2;
} }
sent_data += buffer_size;
printf("%" PRIu32 "b / %ldb (%.2f%%)\n", sent_data, firmware_file_size,
(((float)sent_data / (float)firmware_file_size) * 100.0f));
}
}
printf("Checking program..."); printf("Checking program...\n");
ret = blisp_device_program_check(&device); ret = blisp_device_program_check(&device);
if (ret != BLISP_OK) { if (ret != BLISP_OK) {
fprintf(stderr, "\nFailed to check program.\n"); fprintf(stderr, "Failed to check program.\n");
goto exit2; goto exit2;
} }
printf("OK\n"); printf("Program OK!\n");
if (reset->count > 0) { if (reset->count > 0) {
blisp_device_reset(&device); blisp_device_reset(&device);
@ -288,7 +252,7 @@ int8_t cmd_write_args_init() {
cmd_write_argtable[0] = cmd = cmd_write_argtable[0] = cmd =
arg_rex1(NULL, NULL, "write", NULL, REG_ICASE, NULL); arg_rex1(NULL, NULL, "write", NULL, REG_ICASE, NULL);
cmd_write_argtable[1] = chip_type = cmd_write_argtable[1] = chip_type =
arg_str1("c", "chip", "<chip_type>", "Chip Type (bl70x)"); arg_str1("c", "chip", "<chip_type>", "Chip Type");
cmd_write_argtable[2] = port_name = cmd_write_argtable[2] = port_name =
arg_str0("p", "port", "<port_name>", arg_str0("p", "port", "<port_name>",
"Name/Path to the Serial Port (empty for search)"); "Name/Path to the Serial Port (empty for search)");

View File

@ -1,41 +1,75 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
#include "common.h" #include "common.h"
#include <blisp.h> #include <blisp.h>
#include <argtable3.h>
#include <string.h>
#include <inttypes.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <inttypes.h> #include "blisp_easy.h"
#include "util.h" #include "util.h"
void blisp_common_progress_callback(uint32_t current_value, uint32_t max_value) {
printf("%" PRIu32 "b / %ldb (%.2f%%)\n", current_value, max_value,
(((float)current_value / (float)max_value) * 100.0f));
}
int32_t blisp_common_init_device(struct blisp_device* device, struct arg_str* port_name, struct arg_str* chip_type)
{
if (chip_type->count == 0) {
fprintf(stderr, "Chip type is invalid.\n");
return -1;
}
struct blisp_chip* chip = NULL;
if (strcmp(chip_type->sval[0], "bl70x") == 0) {
chip = &blisp_chip_bl70x;
} else if (strcmp(chip_type->sval[0], "bl60x") == 0) {
chip = &blisp_chip_bl60x;
} else {
fprintf(stderr, "Chip type is invalid.\n");
return -1;
}
int32_t ret;
ret = blisp_device_init(device, chip);
if (ret != BLISP_OK) {
fprintf(stderr, "Failed to init device.\n");
return -1;
}
ret = blisp_device_open(device,
port_name->count == 1 ? port_name->sval[0] : NULL);
if (ret != BLISP_OK) {
fprintf(stderr, ret == BLISP_ERR_DEVICE_NOT_FOUND ? "Device not found\n" : "Failed to open device.\n");
return -1;
}
return 0;
}
/** /**
* Prepares chip to access flash * Prepares chip to access flash
* this means performing handshake, and loading eflash_loader if needed. * this means performing handshake, and loading eflash_loader if needed.
*/ */
int32_t blisp_prepare_flash(struct blisp_device* device) { int32_t blisp_common_prepare_flash(struct blisp_device* device) {
int32_t ret = 0; int32_t ret = 0;
FILE* eflash_loader_file = NULL; FILE* eflash_loader_file = NULL;
printf("Sending a handshake..."); printf("Sending a handshake...\n");
ret = blisp_device_handshake(device, false); ret = blisp_device_handshake(device, false);
if (ret != BLISP_OK) { if (ret != BLISP_OK) {
fprintf(stderr, "\nFailed to handshake with device.\n"); fprintf(stderr, "Failed to handshake with device.\n");
return -1; return -1;
} }
printf(" OK\nGetting chip info..."); printf("Handshake successful!\nGetting chip info...\n");
struct blisp_boot_info boot_info; struct blisp_boot_info boot_info;
ret = blisp_device_get_boot_info(device, &boot_info); ret = blisp_device_get_boot_info(device, &boot_info);
if (ret != BLISP_OK) { if (ret != BLISP_OK) {
fprintf(stderr, "\nFailed to get boot info.\n"); fprintf(stderr, "Failed to get boot info.\n");
return -1; return -1;
} }
if (boot_info.boot_rom_version[0] == 255 &&
boot_info.boot_rom_version[1] == 255 &&
boot_info.boot_rom_version[2] == 255 &&
boot_info.boot_rom_version[3] == 255) {
printf(" OK\nDevice already in eflash_loader.\n");
return 0;
}
printf( printf(
"BootROM version %d.%d.%d.%d, ChipID: " "BootROM version %d.%d.%d.%d, ChipID: "
"%02X%02X%02X%02X%02X%02X%02X%02X\n", "%02X%02X%02X%02X%02X%02X%02X%02X\n",
@ -45,6 +79,18 @@ int32_t blisp_prepare_flash(struct blisp_device* device) {
boot_info.chip_id[3], boot_info.chip_id[4], boot_info.chip_id[5], boot_info.chip_id[3], boot_info.chip_id[4], boot_info.chip_id[5],
boot_info.chip_id[6], boot_info.chip_id[7]); boot_info.chip_id[6], boot_info.chip_id[7]);
if (!device->chip->needs_eflash_loader) {
return 0;
}
if (boot_info.boot_rom_version[0] == 255 &&
boot_info.boot_rom_version[1] == 255 &&
boot_info.boot_rom_version[2] == 255 &&
boot_info.boot_rom_version[3] == 255) {
printf("Device already in eflash_loader.\n");
return 0;
}
char exe_path[PATH_MAX]; char exe_path[PATH_MAX];
char eflash_loader_path[PATH_MAX]; char eflash_loader_path[PATH_MAX];
if (util_get_binary_folder(exe_path, PATH_MAX) <= 0) { if (util_get_binary_folder(exe_path, PATH_MAX) <= 0) {
@ -54,9 +100,10 @@ int32_t blisp_prepare_flash(struct blisp_device* device) {
return -1; return -1;
} }
snprintf(eflash_loader_path, PATH_MAX, "%s/data/%s/eflash_loader_%s.bin", snprintf(eflash_loader_path, PATH_MAX, "%s/data/%s/eflash_loader_%s.bin",
exe_path, device->chip->type_str, device->chip->default_xtal); exe_path, device->chip->type_str,
device->chip->default_xtal); // TODO: Let user pick
printf("Loading the eflash loader file from disk\n"); printf("Loading the eflash loader file from disk\n");
eflash_loader_file = fopen(eflash_loader_path, "rb"); // TODO: Error handling eflash_loader_file = fopen(eflash_loader_path, "rb");
if (eflash_loader_file == NULL) { if (eflash_loader_file == NULL) {
fprintf(stderr, fprintf(stderr,
"Could not open the eflash loader file from disk.\n" "Could not open the eflash loader file from disk.\n"
@ -65,63 +112,13 @@ int32_t blisp_prepare_flash(struct blisp_device* device) {
ret = -1; ret = -1;
goto exit1; goto exit1;
} }
uint8_t
eflash_loader_header[176]; // TODO: Remap it to the boot header struct
fread(eflash_loader_header, 176, 1,
eflash_loader_file); // TODO: Error handling
printf("Loading eflash_loader...\n"); struct blisp_easy_transport eflash_loader_transport =
ret = blisp_device_load_boot_header(device, eflash_loader_header); blisp_easy_transport_new_from_file(eflash_loader_file);
ret = blisp_easy_load_ram_image(device, &eflash_loader_transport,
blisp_common_progress_callback);
if (ret != BLISP_OK) { if (ret != BLISP_OK) {
fprintf(stderr, "Failed to load boot header.\n"); fprintf(stderr, "Failed to load eflash_loader, ret: %d\n", ret);
ret = -1;
goto exit1;
}
{
uint32_t sent_data = 0;
uint32_t buffer_size = 0;
uint8_t buffer[4092];
// TODO: Real checking of segments count
for (uint8_t seg_index = 0; seg_index < 1; seg_index++) {
struct blisp_segment_header segment_header = {0};
fread(&segment_header, 16, 1,
eflash_loader_file); // TODO: Error handling
ret = blisp_device_load_segment_header(device, &segment_header);
if (ret != 0) {
fprintf(stderr, "Failed to load segment header.\n");
ret = -1;
goto exit1;
}
printf("Flashing %d. segment\n", seg_index + 1);
printf("0b / %" PRIu32 "b (0.00%%)\n", segment_header.length);
while (sent_data < segment_header.length) {
buffer_size = segment_header.length - sent_data;
if (buffer_size > 4092) {
buffer_size = 4092;
}
fread(buffer, buffer_size, 1, eflash_loader_file);
ret = blisp_device_load_segment_data(
device, buffer, buffer_size); // TODO: Error handling
if (ret < BLISP_OK) {
fprintf(stderr, "Failed to load segment data. (ret %d)\n", ret);
ret = -1;
goto exit1;
}
sent_data += buffer_size;
printf("%" PRIu32 "b / %" PRIu32 "b (%.2f%%)\n", sent_data,
segment_header.length,
(((float)sent_data / (float)segment_header.length) * 100.0f));
}
}
}
ret = blisp_device_check_image(device);
if (ret != BLISP_OK) {
fprintf(stderr, "Failed to check image.\n");
ret = -1; ret = -1;
goto exit1; goto exit1;
} }
@ -133,14 +130,14 @@ int32_t blisp_prepare_flash(struct blisp_device* device) {
goto exit1; goto exit1;
} }
printf("Sending a handshake..."); printf("Sending a handshake...\n");
ret = blisp_device_handshake(device, true); ret = blisp_device_handshake(device, true);
if (ret != BLISP_OK) { if (ret != BLISP_OK) {
fprintf(stderr, "\nFailed to handshake with device.\n"); fprintf(stderr, "Failed to handshake with device.\n");
ret = -1; ret = -1;
goto exit1; goto exit1;
} }
printf(" OK\n"); printf("Handshake with eflash_loader successful.\n");
exit1: exit1:
if (eflash_loader_file != NULL) if (eflash_loader_file != NULL)
fclose(eflash_loader_file); fclose(eflash_loader_file);

View File

@ -4,7 +4,10 @@
#include <stdint.h> #include <stdint.h>
#include <blisp.h> #include <blisp.h>
#include <argtable3.h>
int32_t blisp_prepare_flash(struct blisp_device* device); int32_t blisp_common_prepare_flash(struct blisp_device* device);
void blisp_common_progress_callback(uint32_t current_value, uint32_t max_value);
int32_t blisp_common_init_device(struct blisp_device* device, struct arg_str* port_name, struct arg_str* chip_type);
#endif // BLISP_COMMON_H #endif // BLISP_COMMON_H

View File

@ -5,7 +5,7 @@
#include "argtable3.h" #include "argtable3.h"
#include "cmd.h" #include "cmd.h"
struct cmd* cmds[] = {&cmd_write}; struct cmd* cmds[] = {&cmd_write, &cmd_iot};
static uint8_t cmds_count = sizeof(cmds) / sizeof(cmds[0]); static uint8_t cmds_count = sizeof(cmds) / sizeof(cmds[0]);