From 8421080c367083e01e07569d82ea4fe6f63cb86c Mon Sep 17 00:00:00 2001 From: Michael Buesch Date: Thu, 23 Oct 2008 21:43:33 +0200 Subject: pressure_control: Implement RX timeout Signed-off-by: Michael Buesch --- pressure_control/firmware/main.c | 1 + pressure_control/firmware/remote.c | 21 +++++++++++++++++++++ pressure_control/firmware/remote.h | 1 + 3 files changed, 23 insertions(+) (limited to 'pressure_control/firmware') diff --git a/pressure_control/firmware/main.c b/pressure_control/firmware/main.c index ef4c81e..fc16dad 100644 --- a/pressure_control/firmware/main.c +++ b/pressure_control/firmware/main.c @@ -108,6 +108,7 @@ ISR(TIMER1_COMPA_vect) { if (state.sensor_trigger_cnt > 0) state.sensor_trigger_cnt--; + remote_1khz_work(); } void system_timer_init(void) diff --git a/pressure_control/firmware/remote.c b/pressure_control/firmware/remote.c index dafca5d..29b15b4 100644 --- a/pressure_control/firmware/remote.c +++ b/pressure_control/firmware/remote.c @@ -36,6 +36,7 @@ static struct remote_message rx_msg; static uint8_t rx_msg_count; static bool rx_msg_valid; static bool rx_softirq; +static uint16_t rx_timeout; static inline void usart_tx(uint8_t data) @@ -236,6 +237,7 @@ static void usart_handle_rx_irq(void) rxbuf[rx_msg_count++] = data; if (rx_msg_count == sizeof(struct remote_message)) { rx_msg_count = 0; + rx_timeout = 0; mb(); rx_msg_valid = 1; break; @@ -254,6 +256,19 @@ ISR(USART_RXC_vect) usart_handle_rx_irq(); } +/* Called with IRQs disabled. */ +static void usart_rx_timeout_check() +{ + if (rx_msg_count > 0) + rx_timeout++; + if (rx_timeout > 100 /* milliseconds */) { + /* Timeout! Reset the RX buffer. */ + rx_msg_count = 0; + rx_timeout = 0; + } + mb(); +} + void print_pgm(const prog_char *str) { struct remote_message msg; @@ -291,6 +306,12 @@ void remote_work(void) sei(); } +/* Maintanance work. Called at a frequency of 1KHz with IRQs disabled. */ +void remote_1khz_work(void) +{ + usart_rx_timeout_check(); +} + void remote_pressure_change_notification(uint16_t mbar, uint16_t hysteresis) { diff --git a/pressure_control/firmware/remote.h b/pressure_control/firmware/remote.h index ad67540..9aabfe9 100644 --- a/pressure_control/firmware/remote.h +++ b/pressure_control/firmware/remote.h @@ -78,6 +78,7 @@ void remote_pressure_change_notification(uint16_t mbar, uint16_t hysteresis); void remote_work(void); +void remote_1khz_work(void); void remote_init(void); #endif /* REMOTE_H_ */ -- cgit v1.2.3