From 3d0715ee875999c4b52bbc03383c96d94b432aa2 Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Thu, 18 Mar 2021 16:05:23 -0700 Subject: [PATCH] Cleaner module split in bl --- bl/bl.h | 26 +----------------- bl/blled.c | 51 +++++++++++++++++++++++++++++++++++ bl/blmain.c | 75 +++++----------------------------------------------- bl/blpwm.c | 2 +- bl/blutil.h | 30 +++++++++++++++++++++ bl/blutils.c | 2 +- 6 files changed, 90 insertions(+), 96 deletions(-) create mode 100644 bl/blled.c create mode 100644 bl/blutil.h diff --git a/bl/bl.h b/bl/bl.h index 830e7bd..07bbe83 100644 --- a/bl/bl.h +++ b/bl/bl.h @@ -1,24 +1,6 @@ #pragma once -#include "jdprofile.h" -#include "blproto.h" - -#if defined(STM32F0) -#include "stm32f0.h" -#elif defined(STM32G0) -#include "stm32g0.h" -#else -#error "invalid CPU" -#endif - -#include "jd_physical.h" -#include "jd_control.h" - -#ifndef QUICK_LOG -#define QUICK_LOG 0 -#endif - -#define CPU_MHZ HSI_MHZ +#include "blutil.h" typedef void (*cb_t)(void); @@ -91,10 +73,6 @@ void jd_prep_send(ctx_t *ctx); void tim_init(void); uint32_t tim_get_micros(void); - -void blled_init(uint32_t period); -void blled_set_duty(uint32_t duty); - void uart_init(ctx_t *ctx); int uart_tx(ctx_t *ctx, const void *data, uint32_t numbytes); #define RX_LINE_BUSY 1 @@ -103,8 +81,6 @@ int uart_tx(ctx_t *ctx, const void *data, uint32_t numbytes); int uart_rx(ctx_t *ctx, void *data, uint32_t maxbytes); void uart_post_rx(ctx_t *ctx); -uint16_t crc16(const void *data, uint32_t size); - uint32_t random(ctx_t *); uint16_t jd_crc16(const void *data, uint32_t size); void jd_compute_crc(jd_frame_t *frame); diff --git a/bl/blled.c b/bl/blled.c new file mode 100644 index 0000000..73fa144 --- /dev/null +++ b/bl/blled.c @@ -0,0 +1,51 @@ +#include "blutil.h" + +// The LED_BL will be run at 10/BL_LED_PERIOD and 30/BL_LED_PERIOD +#ifndef BL_LED_PERIOD +#define BL_LED_PERIOD 300 +#endif + +void led_init(void) { + led_set_value(0); +#ifdef PIN_BL_LED + blled_init(BL_LED_PERIOD); +#else + pin_setup_output(PIN_LED); + pin_setup_output(PIN_LED_GND); +#if QUICK_LOG == 1 + pin_setup_output(PIN_X0); + pin_setup_output(PIN_X1); +#endif + pin_set(PIN_LED_GND, 0); +#endif +#if QUICK_LOG == 1 + pin_setup_output(PIN_X0); + pin_setup_output(PIN_X1); +#endif +} + +void led_set_value(int v) { +#ifdef PIN_BL_LED +#ifdef LED_RGB_COMMON_CATHODE + blled_set_duty(v); +#else + blled_set_duty(BL_LED_PERIOD - (v)); +#endif +#else + pin_set(PIN_LED, v != 0); +#endif +} + +static void led_panic_blink(void) { + led_set_value(30); + target_wait_us(64 << 20); + led_set_value(0); + target_wait_us(64 << 20); +} + +void jd_panic(void) { + DMESG("PANIC!"); + while (1) { + led_panic_blink(); + } +} diff --git a/bl/blmain.c b/bl/blmain.c index 7b83f97..40b3736 100644 --- a/bl/blmain.c +++ b/bl/blmain.c @@ -10,42 +10,6 @@ static void start_app(void) { ctx_t ctx_; -// The LED_BL will be run at 10/BL_LED_PERIOD and 30/BL_LED_PERIOD -#ifndef BL_LED_PERIOD -#define BL_LED_PERIOD 300 -#endif - -#ifdef LED_RGB_COMMON_CATHODE -#define SET_LED(v) blled_set_duty(v) -#else -#define SET_LED(v) blled_set_duty(BL_LED_PERIOD - (v)) -#endif - -void led_init(void) { -#ifdef PIN_BL_LED - SET_LED(0); - blled_init(BL_LED_PERIOD); -#else - pin_setup_output(PIN_LED); - pin_setup_output(PIN_LED_GND); -#if QUICK_LOG == 1 - pin_setup_output(PIN_X0); - pin_setup_output(PIN_X1); -#endif - pin_set(PIN_LED_GND, 0); -#endif -#if QUICK_LOG == 1 - pin_setup_output(PIN_X0); - pin_setup_output(PIN_X1); -#endif -} - -#ifndef PIN_BL_LED -void led_set(int state) { - pin_set(PIN_LED, state); -} -#endif - void led_blink(int us) { ctx_.led_on_time = tim_get_micros() + us; } @@ -142,7 +106,7 @@ int main(void) { #else (void)app_valid; #endif - ctx->app_start_time = 0x80000000; + ctx->app_start_time = 0x80000000; // we delay the first LED light up randomly, so it's not very likely to get synchronized with // other bootloaders uint32_t led_cnt_down = (ctx->randomseed & 0xff) + 10; @@ -178,20 +142,20 @@ int main(void) { led_cnt_down = 10; if (ctx->led_on_time < now) { if (now & 0x80000) - SET_LED(30); + led_set_value(30); else - SET_LED(10); + led_set_value(10); } else { - SET_LED(0); + led_set_value(0); } } #else if (led_cnt_down == 1) { if (ctx->led_on_time < now) { - led_set(1); + led_set_value(30); } } else if (led_cnt_down == 0) { - led_set(0); + led_set_value(0); if (now & 0x80000) led_cnt_down = 5; else @@ -200,30 +164,3 @@ int main(void) { #endif } } - -static void busy_sleep(int ms) { - ms *= 1000; // check - while (ms--) - asm volatile("nop"); -} - -static void led_panic_blink(void) { -#ifdef PIN_BL_LED - SET_LED(40); - busy_sleep(70); - SET_LED(0); - busy_sleep(70); -#else - led_set(1); - busy_sleep(70); - led_set(0); - busy_sleep(70); -#endif -} - -void jd_panic(void) { - DMESG("PANIC!"); - while (1) { - led_panic_blink(); - } -} diff --git a/bl/blpwm.c b/bl/blpwm.c index 9625ff7..3220fea 100644 --- a/bl/blpwm.c +++ b/bl/blpwm.c @@ -1,4 +1,4 @@ -#include "bl.h" +#include "blutil.h" #ifdef PIN_BL_LED diff --git a/bl/blutil.h b/bl/blutil.h new file mode 100644 index 0000000..e2e8866 --- /dev/null +++ b/bl/blutil.h @@ -0,0 +1,30 @@ +#pragma once + +#include "jdprofile.h" +#include "blproto.h" + +#if defined(STM32F0) +#include "stm32f0.h" +#elif defined(STM32G0) +#include "stm32g0.h" +#else +#error "invalid CPU" +#endif + +#include "jd_physical.h" +#include "jd_control.h" + +#ifndef QUICK_LOG +#define QUICK_LOG 0 +#endif + +#define CPU_MHZ HSI_MHZ + +void blled_init(uint32_t period); +void blled_set_duty(uint32_t duty); + +void led_init(void); +void led_set_value(int v); +void jd_panic(void); + +uint16_t crc16(const void *data, uint32_t size); diff --git a/bl/blutils.c b/bl/blutils.c index 1726bc1..4fbaef8 100644 --- a/bl/blutils.c +++ b/bl/blutils.c @@ -1,4 +1,4 @@ -#include "bl.h" +#include "blutil.h" RAM_FUNC void target_wait_cycles(int n) {