From b0291b3cdbd88284b0f66d259769ae4607358e80 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Tue, 10 May 2022 14:32:55 -0400 Subject: [PATCH] led: Convert code to use a task There is no need for a custom check_blink_time() function as this can be done from a new led_blink_task(). Signed-off-by: Kevin O'Connor --- src/Makefile | 3 ++- src/canboot_main.c | 19 ++++++++++--------- src/canboot_main.h | 1 + src/led.c | 14 ++++++++------ src/led.h | 6 ------ 5 files changed, 21 insertions(+), 22 deletions(-) delete mode 100644 src/led.h diff --git a/src/Makefile b/src/Makefile index 4cfd21d..21d5eea 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,3 +1,4 @@ # Main code build rules -src-y += canboot_main.c sched.c command.c led.c +src-y += canboot_main.c sched.c command.c +src-$(CONFIG_ENABLE_LED) += led.c diff --git a/src/canboot_main.c b/src/canboot_main.c index f866e3c..4279df8 100644 --- a/src/canboot_main.c +++ b/src/canboot_main.c @@ -13,22 +13,24 @@ #include "canboot_main.h" // canboot_main #include "command.h" // command_respond_ack #include "ctr.h" // DECL_CTR -#include "led.h" // check_blink_time #include "byteorder.h" // cpu_to_le32 #include "sched.h" // sched_run_init -#define WAIT_BLINK_TIME 1000000 -#define XFER_BLINK_TIME 20000 - #define REQUEST_SIG 0x5984E3FA6CA1589B // Random request sig static uint8_t page_buffer[CONFIG_MAX_FLASH_PAGE_SIZE]; // Page Tracking static uint32_t last_page_address = 0; static uint8_t page_pending = 0; -static uint32_t blink_time = WAIT_BLINK_TIME; +static uint8_t is_in_transfer; static uint8_t complete = 0; +int +flashcmd_is_in_transfer(void) +{ + return is_in_transfer; +} + static void write_page(uint32_t page_address) { @@ -41,7 +43,7 @@ write_page(uint32_t page_address) void command_read_block(uint32_t *data) { - blink_time = XFER_BLINK_TIME; + is_in_transfer = 1; uint32_t block_address = le32_to_cpu(data[1]); uint32_t out[CONFIG_BLOCK_SIZE / 4 + 2 + 2]; out[2] = cpu_to_le32(block_address); @@ -52,7 +54,7 @@ command_read_block(uint32_t *data) void command_write_block(uint32_t *data) { - blink_time = XFER_BLINK_TIME; + is_in_transfer = 1; if (command_get_arg_count(data) != (CONFIG_BLOCK_SIZE / 4) + 1) { command_respond_command_error(); return; @@ -76,7 +78,7 @@ command_write_block(uint32_t *data) void command_eof(uint32_t *data) { - blink_time = WAIT_BLINK_TIME; + is_in_transfer = 0; uint32_t flash_page_size = flash_get_page_size(); if (page_pending) { write_page(last_page_address + flash_page_size); @@ -163,7 +165,6 @@ enter_bootloader(void) for (;;) { sched_run_tasks(); - check_blink_time(blink_time); if (complete && canbus_tx_clear()) // wait until we are complete and the ack has returned break; diff --git a/src/canboot_main.h b/src/canboot_main.h index 8d99bdf..91c6788 100644 --- a/src/canboot_main.h +++ b/src/canboot_main.h @@ -3,6 +3,7 @@ #include // uint32_t +int flashcmd_is_in_transfer(void); void canboot_main(void); #endif // canboot_main.h diff --git a/src/led.c b/src/led.c index a95a180..aa687bf 100644 --- a/src/led.c +++ b/src/led.c @@ -7,10 +7,13 @@ #include "autoconf.h" // CONFIG_ENABLE_LED #include "board/gpio.h" // gpio_out_setup #include "board/misc.h" // timer_read_time +#include "canboot_main.h" // flashcmd_is_in_transfer #include "ctr.h" // DECL_CTR -#include "led.h" // check_blink_time #include "sched.h" // DECL_INIT +#define WAIT_BLINK_TIME 1000000 +#define XFER_BLINK_TIME 20000 + DECL_CTR("DECL_LED_PIN " __stringify(CONFIG_STATUS_LED_PIN)); extern uint32_t led_gpio, led_gpio_high; // Generated by buildcommands.py @@ -20,8 +23,6 @@ static uint32_t last_blink_time; void led_init(void) { - if (!CONFIG_ENABLE_LED) - return; led = gpio_out_setup(led_gpio, led_gpio_high); // The short delay is simply to ensure that the Debug Timer is // enabled @@ -31,10 +32,10 @@ led_init(void) DECL_INIT(led_init); void -check_blink_time(uint32_t usec) +led_blink_task(void) { - if (!CONFIG_ENABLE_LED) - return; + int in_transfer = flashcmd_is_in_transfer(); + uint32_t usec = in_transfer ? XFER_BLINK_TIME : WAIT_BLINK_TIME; uint32_t curtime = timer_read_time(); uint32_t endtime = last_blink_time + timer_from_us(usec); if (timer_is_before(endtime, curtime)) { @@ -42,3 +43,4 @@ check_blink_time(uint32_t usec) last_blink_time = timer_read_time(); } } +DECL_TASK(led_blink_task); diff --git a/src/led.h b/src/led.h deleted file mode 100644 index 14b7403..0000000 --- a/src/led.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef __LED_H -#define __LED_H - -void check_blink_time(uint32_t usec); - -#endif // led.h