From b21b8dbf2dfc0a292761d0649323f6003cae9950 Mon Sep 17 00:00:00 2001 From: yul Date: Mon, 30 May 2022 19:34:27 +0200 Subject: [PATCH] state machine, error handling, and twinkling led --- bootloader.c | 23 ++++++--- uart_commands.c | 122 +++++++----------------------------------------- 2 files changed, 33 insertions(+), 112 deletions(-) diff --git a/bootloader.c b/bootloader.c index adb910f..078405a 100644 --- a/bootloader.c +++ b/bootloader.c @@ -18,6 +18,7 @@ #include #include "uart_commands.h" +#include "timer.h" error_code status; @@ -66,7 +67,10 @@ int is_code_valid(){ if(is_error == ok){ return 1; } - else return 0; + else{ + set_colour(RED);//code invalid, red led + return 0; + } } int is_b_pressed(){ @@ -99,9 +103,12 @@ void launch_program(){ void manage_bootloader(){ cmd.argv = (char**)argvs; - while(1){ + int var = 1; + char digit; + while(var){ uart_receive_command(buff); uart_parse_command(buff, &cmd); + set_colour(GREEN); if ((state == S1) && (strncmp("GETID", cmd.argv[0], 5)==0)){ uart_commands_getid(&status); @@ -137,11 +144,16 @@ void manage_bootloader(){ uart_send_ok(); uart_send("bla\r\n", 5);//putting bla here so OK will show up //removing bla for the moment will make ok not show up + var = 0; + LPC_GPIO2->FIOCLR0 = 0xff; launch_program(); }else{ state = S2; uart_send_err(); - //flash LED red + set_colour(RED); + itoa(is_error, &digit, 10); + uart_send(&digit, 1); + //flash LED red, and stays in the bootloader mode } }else { uart_send_cmd_illeg(); @@ -150,11 +162,8 @@ void manage_bootloader(){ } int main(void) { - //verify b is pressed - //checksum is correct ( - //bootloader_init(); uart_init(115200); - LPC_GPIO0->FIODIR &=~(1<<19); //do I really need this line? + timer_0(); if(is_b_pressed() == 0){ if(is_code_valid()){ launch_program(); diff --git a/uart_commands.c b/uart_commands.c index 1f13438..438fceb 100644 --- a/uart_commands.c +++ b/uart_commands.c @@ -9,100 +9,6 @@ char CMD_ILLEGAL[13] = "CMD_ILLEGAL\r\n"; void digit_to_str(char* str, uint32_t number, int base){ itoa(number,str,16); } -/* -void digit_to_str(char* str, uint32_t number, int base){ - char tmp_char[32]; - uint32_t tmp = number; - int tmp_counter = 0; - for(int i = 0; i<32; i++); - switch (tmp & 15) - { - case 0: - tmp_char[tmp_counter] = '0'; - tmp_counter++; - tmp=tmp/base; - break; - case 1: - tmp_char[tmp_counter] = '1'; - tmp_counter++; - tmp=tmp/base; - break; - case 2: - tmp_char[tmp_counter] = '2'; - tmp_counter++; - tmp=tmp/base; - break; - case 3: - tmp_char[tmp_counter] = '3'; - tmp_counter++; - tmp=tmp/base; - break; - case 4: - tmp_char[tmp_counter] = '4'; - tmp_counter++; - tmp=tmp/base; - break; - case 5: - tmp_char[tmp_counter] = '5'; - tmp_counter++; - tmp=tmp/base; - break; - case 6: - tmp_char[tmp_counter] = '6'; - tmp_counter++; - tmp=tmp/base; - break; - case 7: - tmp_char[tmp_counter] = '7'; - tmp_counter++; - tmp=tmp/base; - break; - case 8: - tmp_char[tmp_counter] = '8'; - tmp_counter++; - tmp=tmp/base; - break; - case 9: - tmp_char[tmp_counter] = '9'; - tmp_counter++; - tmp=tmp/base; - break; - case 10: - tmp_char[tmp_counter] = 'a'; - tmp_counter++; - tmp=tmp/base; - break; - case 11: - tmp_char[tmp_counter] = 'b'; - tmp_counter++; - tmp=tmp/base; - break; - case 12: - tmp_char[tmp_counter] = 'c'; - tmp_counter++; - tmp=tmp/base; - break; - case 13: - tmp_char[tmp_counter] = 'd'; - tmp_counter++; - tmp=tmp/base; - break; - case 14: - tmp_char[tmp_counter] = 'e'; - tmp_counter++; - tmp=tmp/base; - break; - case 15: - tmp_char[tmp_counter] = 'f'; - tmp_counter++; - tmp=tmp/base; - break; - } - for(int i = 0; i