From 31b17dfd8d2b2df28ed8816d4d2593d55df812ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sat, 10 Apr 2021 14:36:54 +0200 Subject: [PATCH] Updater: Clear shell at startup and print size of update --- stm-firmware/updater/ram-code/itoa.c | 53 ++++++++++++++++++++++++++++ stm-firmware/updater/ram-code/itoa.h | 8 +++++ stm-firmware/updater/ram-code/main.c | 26 ++++++++++++-- 3 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 stm-firmware/updater/ram-code/itoa.c create mode 100644 stm-firmware/updater/ram-code/itoa.h diff --git a/stm-firmware/updater/ram-code/itoa.c b/stm-firmware/updater/ram-code/itoa.c new file mode 100644 index 0000000..d74fed1 --- /dev/null +++ b/stm-firmware/updater/ram-code/itoa.c @@ -0,0 +1,53 @@ +#include "itoa.h" + +/* + * The heapless_itoa function is copied from the shellmatta project. + */ + +/** + * @brief itoa like function to convert int to an ascii string + * @warning you have to provide a large enough buffer + * @param[in] value + * @param[in,out] buffer + * @param[in] base + * @return number of bytes in string + */ +uint32_t heapless_itoa(int32_t value, char *buffer, uint32_t base) +{ + char tempBuffer[34u]; + uint32_t i; + uint32_t bufferIdx = 0u; + int8_t digitValue; + + /** -# check the base for plausibility */ + if((base >= 2) && (base <= 16)) + { + /** -# check for sign */ + if(value < 0) + { + value = value * (-1); + buffer[0u] = '-'; + bufferIdx += 1u; + } + + /** -# loop through all digits in reverse order */ + i = 0u; + do + { + digitValue = (int8_t) (value % base); + tempBuffer[i] = (digitValue < 10) ? ('0' + digitValue) : ('A' + (digitValue - 10)); + value /= base; + i ++; + }while(value > 0); + + /** -# store the string in the correct order onto the buffer */ + while(i > 0u) + { + buffer[bufferIdx] = tempBuffer[i - 1u]; + i --; + bufferIdx ++; + } + } + return bufferIdx; +} + diff --git a/stm-firmware/updater/ram-code/itoa.h b/stm-firmware/updater/ram-code/itoa.h new file mode 100644 index 0000000..5397908 --- /dev/null +++ b/stm-firmware/updater/ram-code/itoa.h @@ -0,0 +1,8 @@ +#ifndef _ITOA_H_ +#define _ITOA_H_ + +#include + +uint32_t heapless_itoa(int32_t value, char *buffer, uint32_t base); + +#endif /* _ITOA_H_ */ diff --git a/stm-firmware/updater/ram-code/main.c b/stm-firmware/updater/ram-code/main.c index 551d5e6..e0495dc 100644 --- a/stm-firmware/updater/ram-code/main.c +++ b/stm-firmware/updater/ram-code/main.c @@ -15,6 +15,7 @@ #include #include #include "uart.h" +#include "itoa.h" static volatile unsigned int wait_tick; @@ -55,7 +56,7 @@ static void __attribute__((noreturn)) ram_code_exit(bool updated) while(1); } -static int check_hex_file(const char *fname) +static int check_hex_file(const char *fname, uint32_t *update_size) { enum hex_parser_ret hex_ret; struct hex_parser parser; @@ -65,6 +66,7 @@ static int check_hex_file(const char *fname) int retval = -1; uint32_t flash_base; uint32_t flash_top; + uint32_t total_size = 0UL; flash_base = flash_writer_get_base_address(); flash_top = flash_base + flash_writer_get_flash_size(); @@ -82,11 +84,14 @@ static int check_hex_file(const char *fname) retval = -2; goto ret_close_parser; } + total_size += dlen; } } while (hex_ret == HEX_PARSER_DATA_OK || hex_ret == HEX_PARSER_OK); if (hex_ret == HEX_PARSER_EOF_RECORD) { retval = 0; + if (update_size) + *update_size = total_size; } ret_close_parser: @@ -198,8 +203,12 @@ int ram_code_main(void) int res; enum safety_memory_state safety_mem_state; static char filename[256]; + static char tmp_buff[256]; + uint32_t count; + uint32_t update_size; int retries = 3; + SysTick_Config(168000UL); external_watchdog_disable(); __enable_irq(); @@ -208,6 +217,9 @@ int ram_code_main(void) * Pins don't need configuration. They're already setup by the main program */ uart_init(); + + /* Clear display and set cursor to home position */ + uart_send_string("\e[2J\e[H"); uart_send_string("Updater started.\r\n"); res = safety_memory_init(&safety_mem_state); @@ -225,10 +237,10 @@ int ram_code_main(void) if (res) ram_code_exit(false); - uart_send_string("Checking hex file\r\n"); + uart_send_string("Checking hex file "); uart_send_string(filename); uart_send_string("\r\n"); - if (check_hex_file(filename)) { + if (check_hex_file(filename, &update_size)) { uart_send_string("Error in hex file\r\n"); ram_code_exit(false); } @@ -236,6 +248,14 @@ int ram_code_main(void) uart_send_string("File "); uart_send_string(filename); uart_send_string(" checked successfully.\r\n"); + count = heapless_itoa(update_size, tmp_buff, 10); + if (count > 0) { + tmp_buff[count] = 0; + uart_send_string("Update size: "); + uart_send_string(tmp_buff); + uart_send_string(" bytes"); + } + uart_send_string("Starting updater...\r\n"); /* disable the ART caches */