Updater: Clear shell at startup and print size of update

This commit is contained in:
Mario Hüttel 2021-04-10 14:36:54 +02:00
parent 9f1a791be2
commit 31b17dfd8d
3 changed files with 84 additions and 3 deletions

View File

@ -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;
}

View File

@ -0,0 +1,8 @@
#ifndef _ITOA_H_
#define _ITOA_H_
#include <stdint.h>
uint32_t heapless_itoa(int32_t value, char *buffer, uint32_t base);
#endif /* _ITOA_H_ */

View File

@ -15,6 +15,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include "uart.h" #include "uart.h"
#include "itoa.h"
static volatile unsigned int wait_tick; static volatile unsigned int wait_tick;
@ -55,7 +56,7 @@ static void __attribute__((noreturn)) ram_code_exit(bool updated)
while(1); 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; enum hex_parser_ret hex_ret;
struct hex_parser parser; struct hex_parser parser;
@ -65,6 +66,7 @@ static int check_hex_file(const char *fname)
int retval = -1; int retval = -1;
uint32_t flash_base; uint32_t flash_base;
uint32_t flash_top; uint32_t flash_top;
uint32_t total_size = 0UL;
flash_base = flash_writer_get_base_address(); flash_base = flash_writer_get_base_address();
flash_top = flash_base + flash_writer_get_flash_size(); flash_top = flash_base + flash_writer_get_flash_size();
@ -82,11 +84,14 @@ static int check_hex_file(const char *fname)
retval = -2; retval = -2;
goto ret_close_parser; goto ret_close_parser;
} }
total_size += dlen;
} }
} while (hex_ret == HEX_PARSER_DATA_OK || hex_ret == HEX_PARSER_OK); } while (hex_ret == HEX_PARSER_DATA_OK || hex_ret == HEX_PARSER_OK);
if (hex_ret == HEX_PARSER_EOF_RECORD) { if (hex_ret == HEX_PARSER_EOF_RECORD) {
retval = 0; retval = 0;
if (update_size)
*update_size = total_size;
} }
ret_close_parser: ret_close_parser:
@ -198,8 +203,12 @@ int ram_code_main(void)
int res; int res;
enum safety_memory_state safety_mem_state; enum safety_memory_state safety_mem_state;
static char filename[256]; static char filename[256];
static char tmp_buff[256];
uint32_t count;
uint32_t update_size;
int retries = 3; int retries = 3;
SysTick_Config(168000UL); SysTick_Config(168000UL);
external_watchdog_disable(); external_watchdog_disable();
__enable_irq(); __enable_irq();
@ -208,6 +217,9 @@ int ram_code_main(void)
* Pins don't need configuration. They're already setup by the main program * Pins don't need configuration. They're already setup by the main program
*/ */
uart_init(); uart_init();
/* Clear display and set cursor to home position */
uart_send_string("\e[2J\e[H");
uart_send_string("Updater started.\r\n"); uart_send_string("Updater started.\r\n");
res = safety_memory_init(&safety_mem_state); res = safety_memory_init(&safety_mem_state);
@ -225,10 +237,10 @@ int ram_code_main(void)
if (res) if (res)
ram_code_exit(false); 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(filename);
uart_send_string("\r\n"); 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"); uart_send_string("Error in hex file\r\n");
ram_code_exit(false); ram_code_exit(false);
} }
@ -236,6 +248,14 @@ int ram_code_main(void)
uart_send_string("File "); uart_send_string("File ");
uart_send_string(filename); uart_send_string(filename);
uart_send_string(" checked successfully.\r\n"); 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"); uart_send_string("Starting updater...\r\n");
/* disable the ART caches */ /* disable the ART caches */