From 84c3f5421626ac3e86064ef801273e2b69c692d7 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Thu, 18 Jul 2019 12:19:23 +0200 Subject: [PATCH] Aircon: Better main loop --- aircon/firmware/config.h | 2 +- aircon/firmware/main.c | 31 ++++++++++++++++++++++--------- lib/util.h | 7 +++++++ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/aircon/firmware/config.h b/aircon/firmware/config.h index 2c40c0b..af1ea4c 100644 --- a/aircon/firmware/config.h +++ b/aircon/firmware/config.h @@ -30,7 +30,7 @@ #define MODBUS_BAUD_RATE 19200 -#define MODBUS_DEBUG +#undef MODBUS_DEBUG // DS18B20 library parameters diff --git a/aircon/firmware/main.c b/aircon/firmware/main.c index b4650bf..737aafb 100644 --- a/aircon/firmware/main.c +++ b/aircon/firmware/main.c @@ -19,6 +19,7 @@ #include static void rc_init(void); +static bool rc_send(char key); static void clock_init(void) { @@ -106,12 +107,14 @@ static void tick_init(void) systick_interrupt_enable(); } +#if 0 static void delay_ms(uint ms) { u32 start_ticks = ms_ticks; while (ms_ticks - start_ticks < ms) ; } +#endif static void usart_init(void) { @@ -131,7 +134,6 @@ static void usart_init(void) static byte bypass_active; static byte fan_pwm; -// FIXME static void show_temperature(void) { debug_putc('#'); @@ -157,7 +159,6 @@ static void pwm_init(void) // 50% PWM for the IR LED timer_set_oc_mode(TIM4, TIM_OC2, TIM_OCM_FORCE_HIGH); // will be TIM_OCM_PWM1 when transmitting - // timer_set_oc_mode(TIM4, TIM_OC2, TIM_OCM_PWM1); // FIXME timer_set_oc_value(TIM4, TIM_OC2, T4_CYCLE / 2); timer_set_oc_polarity_high(TIM4, TIM_OC2); timer_enable_oc_output(TIM4, TIM_OC2); @@ -180,17 +181,23 @@ int main(void) pwm_init(); rc_init(); - debug_puts("Hello, world!\n"); + debug_puts("Hello, world! This is Aircon Controller speaking.\n"); ds_init(); modbus_init(); - byte cycles = 0; + u32 last_show_temp = 0; + u32 last_ds_step = 0; + for (;;) { - debug_led_toggle(); - delay_ms(100); - ds_step(); + if (ms_ticks - last_ds_step >= 100) { + debug_led_toggle(); + ds_step(); + last_ds_step = ms_ticks; + } + modbus_loop(); + if (usart_get_flag(USART1, USART_SR_RXNE)) { uint ch = usart_recv(USART1); if (ch == 'B') { @@ -219,12 +226,17 @@ int main(void) * % = pwm*4.389 - 12.723 */ timer_set_oc_value(TIM4, TIM_OC1, T4_CYCLE * fan_pwm / 256); + } else { + rc_send(ch); } } - if (cycles++ >= 50) { - cycles = 0; + + if (ms_ticks - last_show_temp >= 5000) { show_temperature(); + last_show_temp = ms_ticks; } + + wait_for_interrupt(); } return 0; @@ -341,6 +353,7 @@ static bool rc_send(char key) return false; rc_pending = key; rc_pattern_pos = rc_patterns[s - rc_keys]; + debug_printf("RC sending: %c", key); gpio_clear(GPIOC, GPIO13); diff --git a/lib/util.h b/lib/util.h index b37c527..e31f7fd 100644 --- a/lib/util.h +++ b/lib/util.h @@ -77,6 +77,13 @@ static inline void put_u32_le(byte *p, u32 x) p[0] = x & 0xff; } +// CPU instructions not covered by libopencm3 + +static inline void wait_for_interrupt(void) +{ + asm volatile ("wfi"); +} + // util-debug.c void debug_printf(const char *fmt, ...); -- 2.39.5