mirror of
https://github.com/pine64/blisp.git
synced 2024-12-22 06:20:12 +00:00
Make proper error handling
This commit is contained in:
parent
da34daf69f
commit
8a00a63600
@ -6,6 +6,18 @@
|
||||
|
||||
#include "blisp_chip.h"
|
||||
|
||||
enum blisp_return {
|
||||
BLISP_OK = 0,
|
||||
BLISP_ERR_UNKNOWN = -1,
|
||||
BLISP_ERR_NO_RESPONSE = -2,
|
||||
BLISP_ERR_DEVICE_NOT_FOUND = -3,
|
||||
BLISP_ERR_CANT_OPEN_DEVICE = -4,
|
||||
// Can't auto-find device due it doesn't have native USB
|
||||
BLISP_ERR_NO_AUTO_FIND_AVAILABLE = -5,
|
||||
BLISP_ERR_PENDING = -6,
|
||||
BLISP_ERR_CHIP_ERR = -7
|
||||
};
|
||||
|
||||
struct blisp_segment_header {
|
||||
uint32_t dest_addr;
|
||||
uint32_t length;
|
||||
|
91
lib/blisp.c
91
lib/blisp.c
@ -27,16 +27,18 @@ int32_t blisp_device_open(struct blisp_device* device, const char* port_name) {
|
||||
if (port_name != NULL) {
|
||||
ret = sp_get_port_by_name(port_name, &serial_port);
|
||||
if (ret != SP_OK) {
|
||||
return -1; // TODO: Improve error codes
|
||||
blisp_dlog("Couldn't open device, err: %d", ret);
|
||||
return BLISP_ERR_CANT_OPEN_DEVICE;
|
||||
}
|
||||
} else {
|
||||
if (!device->chip->usb_isp_available) {
|
||||
return -2; // Can't auto-find device due it doesn't have native USB
|
||||
return BLISP_ERR_NO_AUTO_FIND_AVAILABLE;
|
||||
}
|
||||
struct sp_port** port_list;
|
||||
ret = sp_list_ports(&port_list);
|
||||
if (ret != SP_OK) {
|
||||
return -1; // TODO: Improve error codes
|
||||
blisp_dlog("Couldn't list ports, err: %d", ret);
|
||||
return BLISP_ERR_UNKNOWN;
|
||||
}
|
||||
for (int i = 0; port_list[i] != NULL; i++) {
|
||||
struct sp_port* port = port_list[i];
|
||||
@ -46,21 +48,26 @@ int32_t blisp_device_open(struct blisp_device* device, const char* port_name) {
|
||||
if (vid == 0xFFFF && pid == 0xFFFF) {
|
||||
ret = sp_get_port_by_name(sp_get_port_name(port), &serial_port);
|
||||
if (ret != SP_OK) {
|
||||
return -1; // TODO: Improve error codes
|
||||
blisp_dlog("Couldn't open device, err: %d", ret);
|
||||
return BLISP_ERR_CANT_OPEN_DEVICE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
sp_free_port_list(port_list);
|
||||
if (serial_port == NULL) {
|
||||
return -3; // Device not found
|
||||
return BLISP_ERR_DEVICE_NOT_FOUND;
|
||||
}
|
||||
}
|
||||
|
||||
ret = sp_open(serial_port, SP_MODE_READ_WRITE);
|
||||
if (ret != SP_OK) { // TODO: Handle not found
|
||||
return -1;
|
||||
if (ret != SP_OK) {
|
||||
blisp_dlog("SP open failed: %d", ret);
|
||||
return BLISP_ERR_UNKNOWN; // TODO: Maybe this should be that it can't open
|
||||
// device?
|
||||
}
|
||||
// TODO: Handle errors in following functions, although, none of them *should*
|
||||
// fail
|
||||
sp_set_bits(serial_port, 8);
|
||||
sp_set_parity(serial_port, SP_PARITY_NONE);
|
||||
sp_set_stopbits(serial_port, 1);
|
||||
@ -69,11 +76,11 @@ int32_t blisp_device_open(struct blisp_device* device, const char* port_name) {
|
||||
uint32_t vid, pid;
|
||||
sp_get_port_usb_vid_pid(serial_port, &vid, &pid);
|
||||
device->is_usb = pid == 0xFFFF;
|
||||
// if (device->is_usb) {
|
||||
// device->current_baud_rate = 2000000;
|
||||
// } else {
|
||||
// if (device->is_usb) {
|
||||
// device->current_baud_rate = 2000000;
|
||||
// } else {
|
||||
device->current_baud_rate = 500000;
|
||||
// }
|
||||
// }
|
||||
|
||||
#if 0
|
||||
int fd;
|
||||
@ -86,11 +93,12 @@ int32_t blisp_device_open(struct blisp_device* device, const char* port_name) {
|
||||
#endif
|
||||
ret = sp_set_baudrate(serial_port, device->current_baud_rate);
|
||||
if (ret != SP_OK) {
|
||||
return -1; // TODO: Handle this
|
||||
blisp_dlog("Set baud rate failed: %d... Also hello macOS user :)", ret);
|
||||
return BLISP_ERR_UNKNOWN;
|
||||
}
|
||||
device->serial_port = serial_port;
|
||||
|
||||
return 0;
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_send_command(struct blisp_device* device,
|
||||
@ -119,9 +127,10 @@ int32_t blisp_send_command(struct blisp_device* device,
|
||||
ret =
|
||||
sp_blocking_write(serial_port, device->tx_buffer, 4 + payload_size, 1000);
|
||||
if (ret != (4 + payload_size)) {
|
||||
return -1;
|
||||
blisp_dlog("Received error or not written all data: %d", ret);
|
||||
return BLISP_ERR_UNKNOWN;
|
||||
}
|
||||
return 0;
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_receive_response(struct blisp_device* device,
|
||||
@ -131,10 +140,8 @@ int32_t blisp_receive_response(struct blisp_device* device,
|
||||
struct sp_port* serial_port = device->serial_port;
|
||||
ret = sp_blocking_read(serial_port, &device->rx_buffer[0], 2, 1000);
|
||||
if (ret < 2) {
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Failed to receive response. (ret = %d)\n", ret);
|
||||
#endif
|
||||
return -1; // TODO: Terrible
|
||||
blisp_dlog("Failed to receive response, ret: %d", ret);
|
||||
return BLISP_ERR_UNKNOWN; // TODO: Terrible
|
||||
} else if (device->rx_buffer[0] == 'O' && device->rx_buffer[1] == 'K') {
|
||||
if (expect_payload) {
|
||||
sp_blocking_read(serial_port, &device->rx_buffer[2], 2,
|
||||
@ -146,17 +153,17 @@ int32_t blisp_receive_response(struct blisp_device* device,
|
||||
}
|
||||
return 0;
|
||||
} else if (device->rx_buffer[0] == 'P' && device->rx_buffer[1] == 'D') {
|
||||
return -3; // TODO: Terrible
|
||||
return BLISP_ERR_PENDING; // TODO: This might be rather positive return
|
||||
// number?
|
||||
} else if (device->rx_buffer[0] == 'F' && device->rx_buffer[1] == 'L') {
|
||||
sp_blocking_read(serial_port, &device->rx_buffer[2], 2, 100);
|
||||
device->error_code = (device->rx_buffer[3] << 8) | (device->rx_buffer[2]);
|
||||
return -4; // Failed
|
||||
blisp_dlog("Chip returned error: %d", device->error_code);
|
||||
return BLISP_ERR_CHIP_ERR;
|
||||
}
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Receive response failed... (err: %d, %d - %d)\n", ret,
|
||||
device->rx_buffer[0], device->rx_buffer[1]);
|
||||
#endif
|
||||
return -1;
|
||||
blisp_dlog("Failed to receive any response (err: %d, %d - %d)", ret,
|
||||
device->rx_buffer[0], device->rx_buffer[1]);
|
||||
return BLISP_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
int32_t blisp_device_handshake(struct blisp_device* device, bool in_ef_loader) {
|
||||
@ -188,7 +195,8 @@ int32_t blisp_device_handshake(struct blisp_device* device, bool in_ef_loader) {
|
||||
}
|
||||
ret = sp_blocking_write(serial_port, handshake_buffer, bytes_count, 500);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
blisp_dlog("Handshake write failed, ret %d", ret);
|
||||
return BLISP_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
if (!in_ef_loader && !device->is_usb) {
|
||||
@ -199,11 +207,12 @@ int32_t blisp_device_handshake(struct blisp_device* device, bool in_ef_loader) {
|
||||
ret = sp_blocking_read(serial_port, device->rx_buffer, 2, 50);
|
||||
if (ret >= 2) {
|
||||
if (device->rx_buffer[0] == 'O' && device->rx_buffer[1] == 'K') {
|
||||
return 0;
|
||||
return BLISP_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -4; // didn't received response
|
||||
blisp_dlog("Received no response from chip");
|
||||
return BLISP_ERR_NO_RESPONSE;
|
||||
}
|
||||
|
||||
int32_t blisp_device_get_boot_info(struct blisp_device* device,
|
||||
@ -224,7 +233,7 @@ int32_t blisp_device_get_boot_info(struct blisp_device* device,
|
||||
memcpy(boot_info->chip_id, &device->rx_buffer[16], 8);
|
||||
}
|
||||
// TODO: BL60X
|
||||
return 0;
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
// TODO: Use struct instead of uint8_t*
|
||||
@ -238,7 +247,7 @@ int32_t blisp_device_load_boot_header(struct blisp_device* device,
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
return 0;
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_device_load_segment_header(
|
||||
@ -252,7 +261,7 @@ int32_t blisp_device_load_segment_header(
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
return 0;
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_device_load_segment_data(struct blisp_device* device,
|
||||
@ -267,7 +276,7 @@ int32_t blisp_device_load_segment_data(struct blisp_device* device,
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
return 0;
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_device_check_image(struct blisp_device* device) {
|
||||
@ -279,7 +288,7 @@ int32_t blisp_device_check_image(struct blisp_device* device) {
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
return 0;
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_device_write_memory(struct blisp_device* device,
|
||||
@ -299,7 +308,7 @@ int32_t blisp_device_write_memory(struct blisp_device* device,
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_device_run_image(struct blisp_device* device) {
|
||||
@ -317,7 +326,7 @@ int32_t blisp_device_run_image(struct blisp_device* device) {
|
||||
ret = blisp_device_write_memory(device, 0x40000018, 0x00000002, false);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
return 0;
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
ret = blisp_send_command(device, 0x1A, NULL, 0, false);
|
||||
@ -327,7 +336,7 @@ int32_t blisp_device_run_image(struct blisp_device* device) {
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
return 0;
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_device_flash_erase(struct blisp_device* device,
|
||||
@ -342,7 +351,7 @@ int32_t blisp_device_flash_erase(struct blisp_device* device,
|
||||
return ret;
|
||||
do {
|
||||
ret = blisp_receive_response(device, false);
|
||||
} while (ret == -3);
|
||||
} while (ret == BLISP_ERR_PENDING);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -352,9 +361,9 @@ int32_t blisp_device_flash_write(struct blisp_device* device,
|
||||
uint8_t* payload,
|
||||
uint32_t payload_size) {
|
||||
// TODO: Add max payload size (8184?)
|
||||
// TODO: Don't use malloc + add check
|
||||
|
||||
uint8_t* buffer =
|
||||
malloc(4 + payload_size); // TODO: Don't use malloc + add check
|
||||
uint8_t* buffer = malloc(4 + payload_size);
|
||||
*((uint32_t*)(buffer)) = start_address;
|
||||
memcpy(buffer + 4, payload, payload_size);
|
||||
int ret = blisp_send_command(device, 0x31, buffer, payload_size + 4, true);
|
||||
@ -385,7 +394,7 @@ int32_t blisp_device_reset(struct blisp_device* device) {
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
return 0;
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
void blisp_device_close(struct blisp_device* device) {
|
||||
|
@ -71,7 +71,7 @@ ssize_t get_binary_folder(char* buffer, uint32_t buffer_size) {
|
||||
|
||||
void fill_up_boot_header(struct bfl_boot_header* boot_header) {
|
||||
memcpy(boot_header->magiccode, "BFNP", 4);
|
||||
;
|
||||
|
||||
boot_header->revison = 0x01;
|
||||
memcpy(boot_header->flashCfg.magiccode, "FCFG", 4);
|
||||
boot_header->flashCfg.cfg.ioMode = 0x11;
|
||||
@ -235,26 +235,26 @@ void blisp_flash_firmware() {
|
||||
struct blisp_device device;
|
||||
int32_t ret;
|
||||
ret = blisp_device_init(&device, chip);
|
||||
if (ret != 0) {
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "Failed to init device.\n");
|
||||
return;
|
||||
}
|
||||
ret = blisp_device_open(&device,
|
||||
port_name->count == 1 ? port_name->sval[0] : NULL);
|
||||
if (ret != 0) {
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "Failed to open device.\n");
|
||||
return;
|
||||
}
|
||||
printf("Sending a handshake...");
|
||||
ret = blisp_device_handshake(&device, false);
|
||||
if (ret != 0) {
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "\nFailed to handshake with device.\n");
|
||||
goto exit1;
|
||||
}
|
||||
printf(" OK\nGetting chip info...");
|
||||
struct blisp_boot_info boot_info;
|
||||
ret = blisp_device_get_boot_info(&device, &boot_info);
|
||||
if (ret != 0) {
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "\nFailed to get boot info.\n");
|
||||
goto exit1;
|
||||
}
|
||||
@ -303,7 +303,7 @@ void blisp_flash_firmware() {
|
||||
|
||||
printf("Loading eflash_loader...\n");
|
||||
ret = blisp_device_load_boot_header(&device, eflash_loader_header);
|
||||
if (ret != 0) {
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "Failed to load boot header.\n");
|
||||
goto exit1;
|
||||
}
|
||||
@ -335,7 +335,7 @@ void blisp_flash_firmware() {
|
||||
fread(buffer, buffer_size, 1, eflash_loader_file);
|
||||
ret = blisp_device_load_segment_data(
|
||||
&device, buffer, buffer_size); // TODO: Error handling
|
||||
if (ret < 0) {
|
||||
if (ret < BLISP_OK) {
|
||||
fprintf(stderr, "Failed to load segment data. (ret %d)\n", ret);
|
||||
goto exit1;
|
||||
}
|
||||
@ -348,20 +348,20 @@ void blisp_flash_firmware() {
|
||||
}
|
||||
|
||||
ret = blisp_device_check_image(&device);
|
||||
if (ret != 0) {
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "Failed to check image.\n");
|
||||
goto exit1;
|
||||
}
|
||||
|
||||
ret = blisp_device_run_image(&device);
|
||||
if (ret != 0) {
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "Failed to run image.\n");
|
||||
goto exit1;
|
||||
}
|
||||
|
||||
printf("Sending a handshake...");
|
||||
ret = blisp_device_handshake(&device, true);
|
||||
if (ret != 0) {
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "\nFailed to handshake with device.\n");
|
||||
goto exit1;
|
||||
}
|
||||
@ -386,13 +386,13 @@ eflash_loader:;
|
||||
ret =
|
||||
blisp_device_flash_erase(&device, firmware_base_address,
|
||||
firmware_base_address + firmware_file_size + 1);
|
||||
if (ret != 0) {
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "\nFailed to erase flash.\n");
|
||||
goto exit2;
|
||||
}
|
||||
ret =
|
||||
blisp_device_flash_erase(&device, 0x0000, sizeof(struct bfl_boot_header));
|
||||
if (ret != 0) {
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "\nFailed to erase flash.\n");
|
||||
goto exit2;
|
||||
}
|
||||
@ -400,7 +400,7 @@ eflash_loader:;
|
||||
printf(" OK!\nFlashing boot header...");
|
||||
ret = blisp_device_flash_write(&device, 0x0000, (uint8_t*)&boot_header,
|
||||
sizeof(struct bfl_boot_header));
|
||||
if (ret != 0) {
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "\nFailed to write boot header.\n");
|
||||
goto exit2;
|
||||
}
|
||||
@ -420,7 +420,7 @@ eflash_loader:;
|
||||
ret = blisp_device_flash_write(&device, firmware_base_address + sent_data,
|
||||
buffer,
|
||||
buffer_size); // TODO: Error handling
|
||||
if (ret < 0) {
|
||||
if (ret < BLISP_OK) {
|
||||
fprintf(stderr, "Failed to write firmware! (ret: %d)\n", ret);
|
||||
goto exit2;
|
||||
}
|
||||
@ -432,7 +432,7 @@ eflash_loader:;
|
||||
|
||||
printf("Checking program...");
|
||||
ret = blisp_device_program_check(&device);
|
||||
if (ret != 0) {
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "\nFailed to check program.\n");
|
||||
goto exit2;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user