From 2819ee99d6635c26834701bbae00647bf4b9ea94 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Fri, 29 Sep 2023 22:09:12 +0200 Subject: [PATCH] Improvements to main loop + build fixes - add -lm linker flag - add fan power management --- Makefile | 2 +- src/main.c | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index dfbbf07..be32f54 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ CFLAGS+=-Ifxplib/include -DPOINTPOS=16 LDFLAGS+=-Lfxplib/lib/$(BUILD) -lfxp_stm32l0 # generic linking -LDFLAGS+=-Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group +LDFLAGS+=-Wl,--start-group -lc -lm -lgcc -lnosys -Wl,--end-group # build type specific flags CFLAGS_debug=-O0 -ggdb -DDEBUG diff --git a/src/main.c b/src/main.c index ebe55d9..4915387 100644 --- a/src/main.c +++ b/src/main.c @@ -17,10 +17,11 @@ static void clock_setup(void) { /* We are running on MSI (2.1 MHz) after boot. */ rcc_periph_clock_enable(RCC_GPIOA); // for basically everything - rcc_periph_clock_enable(RCC_GPIOC); // for UART only - /* Enable clocks for USART2. */ + /* Enable clocks for peripherals. */ rcc_periph_clock_enable(RCC_USART2); + rcc_periph_clock_enable(RCC_ADC1); + rcc_periph_clock_enable(RCC_TIM21); } /* Set up systick to fire freq times per second */ @@ -59,7 +60,8 @@ int main(void) fan_ctrl_pwm_enable(); // TODO: make DC/PWM configurable - // triggered every 1 ms + uint8_t last_duty = 0; + while (1) { if(systick_triggered) { systick_triggered = false; @@ -77,7 +79,17 @@ int main(void) uint8_t duty = fan_controller_update(temperature); + // handle fan power-on and power-off + if(last_duty == 0 && duty != 0) { + fan_ctrl_pwm_enable(); + } else if(last_duty != 0 && duty == 0) { + fan_ctrl_pwm_disable(); + } + + // set the updated duty cycle fan_ctrl_pwm_set_duty(duty); + + last_duty = duty; } __WFI();