From 376852285011045c00c51961ec15ac989404ba69 Mon Sep 17 00:00:00 2001 From: yul Date: Tue, 24 May 2022 12:48:02 +0200 Subject: [PATCH] oh well --- bootloader.c | 5 ++++- iap.c | 2 +- uart_commands.c | 52 +++++++++++++++++++++++++++++-------------------- uart_commands.h | 6 +++++- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/bootloader.c b/bootloader.c index 9addc80..cc018cf 100644 --- a/bootloader.c +++ b/bootloader.c @@ -26,9 +26,11 @@ char errr[5] = "ERR\r\n"; +char* argvs[3][10]; cmd_t cmd; + uint32_t offset, total_size, offset_pointer; crc_t crc_prog, crc_data; @@ -41,6 +43,7 @@ crc_t crc_prog, crc_data; char buff[256]; int main(void) { + cmd.argv = argvs; uart_init(115200); while(1){ uart_receive_command(buff); @@ -55,7 +58,7 @@ int main(void) { crc_prog = uart_string_to_int(cmd.argv[3]); total_size = uart_string_to_int(cmd.argv[2]); offset = uart_string_to_int(cmd.argv[1]); - crc_data = uart_commands_prog(); + crc_data = uart_commands_prog(&status, total_size); offset_pointer = offset; }else if(strcmp("DATA", cmd.argv[0])==0){ diff --git a/iap.c b/iap.c index 8b17925..613a38e 100644 --- a/iap.c +++ b/iap.c @@ -94,7 +94,7 @@ uint32_t iap_read_part_id(error_code* status){ void iap_copy_to_flash(error_code* status, int dst_flash, uint32_t* src_ram, int num_bytes){ //iap_prepare_sectors(status, dst_flash, dst_flash+num_bytes); //iap_erase_sectors(status, dst_flash, dst_flash+num_bytes); - //iap_prepare_sectors(status, dst_flash, dst_flash+num_bytes); + iap_prepare_sectors(status, dst_flash, dst_flash+num_bytes); command[0] = 51; command[1] = dst_flash; command[2] = src_ram; diff --git a/uart_commands.c b/uart_commands.c index 7128988..1e3645e 100644 --- a/uart_commands.c +++ b/uart_commands.c @@ -22,13 +22,16 @@ void uart_commands_getserial(error_code* status){ iap_read_serial(status, res); sprintf(hex, "0x%x%x%x%x\r\n", res[0], res[1], res[2], res[3]); if(*status == 0){ - uart_send(OK, 4); + uart_send_ok(); uart_send(hex, 35); } } -crc_t uart_commands_prog(void){ - uart_send(OK, 5); +crc_t uart_commands_prog(error_code* status, int size){ + iap_prepare_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+5); + iap_erase_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+5); + iap_prepare_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+5); + uart_send_ok(); return crc_init(); } @@ -37,49 +40,50 @@ error_code uart_commands_data(error_code* status, int size, crc_t* checksum_glob crc_t checksum_tmp = crc_init(); checksum_tmp = crc_update(checksum_tmp, data, size); checksum_tmp = crc_finalize(checksum_tmp); + int offset_counter = offset; if(checksum_loc == checksum_tmp){ int tmp = size; - iap_prepare_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+1); - iap_erase_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+1); - iap_prepare_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+1); + //iap_prepare_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+1); + //iap_erase_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+1); + iap_prepare_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+5); *checksum_global = crc_update(*checksum_global, data, size); while(tmp >= 4096){ - iap_copy_to_flash(status, offset/0x1000, checksum_global, 4096); + iap_copy_to_flash(status, offset, checksum_global, 4096); if(*status != ok){ - uart_send(ERR, 6); + uart_send_err(); return *status; } tmp = tmp - 4096; } while(tmp >= 1024){ - //iap_copy_to_flash(status, offset/0x1000, checksum_global, 1024); iap_copy_to_flash(status, offset, checksum_global, 1024); if(*status != ok){ - uart_send(ERR, 6); + uart_send_err(); return *status; } tmp = tmp - 1024; + offset_counter += 1024; } while(tmp >= 512){ - iap_copy_to_flash(status, offset/0x1000, checksum_global, 512); + iap_copy_to_flash(status, offset, checksum_global, 512); if(*status != ok){ - uart_send(ERR, 6); + uart_send_err(); return *status; } tmp = tmp - 512; } while(tmp >= 256){ - iap_copy_to_flash(status, offset/0x1000, checksum_global, 256); + iap_copy_to_flash(status, offset, checksum_global, 256); if(*status != ok){ - uart_send(ERR, 6); + uart_send_err(); return *status; } tmp = tmp - 256; } - uart_send(OK, 4); + uart_send_ok(); return ok; }else{ - uart_send(ERR, 6); + uart_send_err(); return local_checksum; } } @@ -94,7 +98,9 @@ error_code uart_commands_check(crc_t crc_received, crc_t crc_calculated){ int uart_parse_command(char *user_input, cmd_t *cmd) { //Initialize a simple command (empty, simple, foreground) - cmd->argv = NULL; + cmd->argv[0] = NULL; + cmd->argv[1] = NULL; + cmd->argv[2] = NULL; cmd->argc = -1; //Separate string in different token (i.e. command name + params + &) @@ -102,10 +108,6 @@ int uart_parse_command(char *user_input, cmd_t *cmd) { //A new element will be added cmd->argc += 1; - //Allocate a new pointer on char for next argv element - if((cmd->argv = realloc(cmd->argv, (cmd->argc+1)*sizeof(char*))) == NULL) - perror("uart_parse_command::realloc"); - //Get the adress of the next token (could be NULL to indicate end of argv) cmd->argv[cmd->argc] = strtok(user_input, DELIMIERS); user_input = NULL; //Useless to execute it each time but easier than having two different strtok calls @@ -121,3 +123,11 @@ uint32_t uart_string_to_int(const char *str) { unsigned long long int hex = strtoull(str, &tmp, 16); return hex; } + +void uart_send_ok(){ + uart_send(OK, 4); +} + +void uart_send_err(){ + uart_send(ERR, 6); +} \ No newline at end of file diff --git a/uart_commands.h b/uart_commands.h index 6cead02..1a71eb2 100644 --- a/uart_commands.h +++ b/uart_commands.h @@ -22,7 +22,7 @@ void uart_commands_getid(error_code* status); void uart_commands_getserial(error_code* status); -crc_t uart_commands_prog(void); +crc_t uart_commands_prog(error_code* status, int size); error_code uart_commands_data(error_code* status, int size, crc_t* checksum_global, crc_t checksum_loc, int offset); @@ -30,4 +30,8 @@ error_code uart_commands_check(crc_t crc_received, crc_t crc_calculated); int uart_parse_command(char *user_input, cmd_t *cmd); +void uart_send_ok(); + +void uart_send_err(); + uint32_t uart_string_to_int(const char* str);