From 3028595801878f962bd362421ed3ec5af635ff45 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 21 Nov 2021 18:48:00 +0000 Subject: add blinken lights --- stm32/app/Makefile | 2 +- stm32/app/board.h | 4 +-- stm32/app/leds.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ stm32/app/main.c | 3 +- stm32/app/prototypes.h | 4 ++- stm32/app/rtc.c | 2 +- stm32/app/ticker.c | 10 +----- 7 files changed, 107 insertions(+), 16 deletions(-) create mode 100644 stm32/app/leds.c diff --git a/stm32/app/Makefile b/stm32/app/Makefile index 997011f..62a13cc 100644 --- a/stm32/app/Makefile +++ b/stm32/app/Makefile @@ -25,7 +25,7 @@ LDLIBS+=-lm V=1 default: ${PROG}.elf -CSRCS= main.c cdcacm.c dfu.c ring.c usart.c ticker.c dummy_kb.c usb.c rtc.c time_fn.c events.c hands.c motor.c +CSRCS= main.c cdcacm.c dfu.c ring.c usart.c ticker.c dummy_kb.c usb.c rtc.c time_fn.c events.c hands.c motor.c leds.c HSRCS = project.h diff --git a/stm32/app/board.h b/stm32/app/board.h index 41eeafc..29d465d 100644 --- a/stm32/app/board.h +++ b/stm32/app/board.h @@ -1,8 +1,8 @@ -#define LED1 GPIO8 +#define LED1 GPIO9 #define LED1_PORT GPIOB -#define LED2 GPIO9 +#define LED2 GPIO8 #define LED2_PORT GPIOB diff --git a/stm32/app/leds.c b/stm32/app/leds.c new file mode 100644 index 0000000..bcac9b6 --- /dev/null +++ b/stm32/app/leds.c @@ -0,0 +1,98 @@ +#include "project.h" + + +#define FLASH 100 + +static unsigned led1 = FLASH, led2 = FLASH; + + +static void metric_pll (void) +{ + static uint32_t last; + static int which; + static unsigned ttg; + EPOCH e; + uint64_t v; + + if ((ticks - last) < ttg) return; + + e = rtc_get(); + + if (which) { + led2 = FLASH; + CLEAR (LED2); + } else { + led1 = FLASH; + CLEAR (LED1); + } + + last = ticks; + + which = !which; + + + v = e.s; + v %= 86400; + v *= 1000; + v += e.ns / 1000000; + + v %= 864; + + // printf("M: %5u %9u\r\n",(unsigned) v, (unsigned ) (e.ns/1000000)); + + ttg = 864 - (unsigned)v; +} + +static void customary_pll (void) +{ + static uint32_t last; + static unsigned ttg; + EPOCH e; + uint64_t v; + + if ((ticks - last) < ttg) return; + + e = rtc_get(); + + led2 = FLASH; + CLEAR (LED2); + + last = ticks; + + v = e.ns / 1000000; + v %= 864; + + ttg = 1000 - (unsigned)v; + + // printf("C: %9u\r\n",(unsigned ) (e.ns/1000000)); +} + + + +void led_tick (void) +{ + + if (led1) { + CLEAR (LED1); + led1--; + } else SET (LED1); + + + if (led2) { + CLEAR (LED2); + led2--; + } else SET (LED2); + + + if (!rtc_ready) return; + + metric_pll(); + // customary_pll(); +} + + +void leds_init (void) +{ + MAP_OUTPUT_OD (LED1); + MAP_OUTPUT_OD (LED2); +} diff --git a/stm32/app/main.c b/stm32/app/main.c index 4722479..344a106 100644 --- a/stm32/app/main.c +++ b/stm32/app/main.c @@ -27,13 +27,12 @@ int main (void) nvic_set_priority (NVIC_USB_LP_CAN_RX0_IRQ, 0x40); nvic_set_priority (NVIC_RTC_IRQ, 0x10); - MAP_OUTPUT_OD (LED1); - MAP_OUTPUT_OD (LED2); usart_init(); cdcacm_rings_init(); rtc_init(); + leds_init(); printf ("Startup:\r\n"); diff --git a/stm32/app/prototypes.h b/stm32/app/prototypes.h index 751ba25..18106bc 100644 --- a/stm32/app/prototypes.h +++ b/stm32/app/prototypes.h @@ -31,7 +31,6 @@ extern int _write(int file, char *ptr, int len); extern void usart_init(void); /* ticker.c */ extern volatile uint32_t ticks; -extern unsigned led2; extern void delay_us(uint32_t d); extern void sys_tick_handler(void); extern void ticker_init(void); @@ -79,3 +78,6 @@ extern void hands_tick(void); /* motor.c */ extern void motor_tick(void); extern void motor_init(void); +/* leds.c */ +extern void led_tick(void); +extern void leds_init(void); diff --git a/stm32/app/rtc.c b/stm32/app/rtc.c index 0030cfd..9c1bc47 100644 --- a/stm32/app/rtc.c +++ b/stm32/app/rtc.c @@ -77,7 +77,7 @@ void rtc_isr (void) v = rtc_get_counter_val(); - TOGGLE (LED1); + // TOGGLE (LED1); cycle_ref = now; diff --git a/stm32/app/ticker.c b/stm32/app/ticker.c index f124307..f89d855 100644 --- a/stm32/app/ticker.c +++ b/stm32/app/ticker.c @@ -4,7 +4,6 @@ volatile uint32_t ticks; static uint32_t scale = 7; -unsigned led2 = 1000; @@ -25,14 +24,7 @@ sys_tick_handler (void) hands_tick(); motor_tick(); - if (led2) - led2--; - - if (led2) - CLEAR (LED2); - - else - SET (LED2); + led_tick(); } void -- cgit v1.2.3