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 <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2022-05-10 14:32:55 -04:00 committed by Eric Callahan
parent 38ef4860da
commit b0291b3cdb
5 changed files with 21 additions and 22 deletions

View File

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

View File

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

View File

@ -3,6 +3,7 @@
#include <stdint.h> // uint32_t
int flashcmd_is_in_transfer(void);
void canboot_main(void);
#endif // canboot_main.h

View File

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

View File

@ -1,6 +0,0 @@
#ifndef __LED_H
#define __LED_H
void check_blink_time(uint32_t usec);
#endif // led.h