From 6966eb287f160c68c597105e60b191806a63b9ba Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Sat, 22 Sep 2012 23:02:15 +0200 Subject: can: handle interrupt asynchronously --- can.c | 22 ++++++++++++++-------- door.c | 4 ---- lightctrl.c | 11 ++++------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/can.c b/can.c index 6deff37..255cf14 100644 --- a/can.c +++ b/can.c @@ -24,13 +24,6 @@ #ifndef R0KET #define spi_ss(x) PORTB = ((x) << B_SS) | 0x3; -static volatile bool canint = false; - -ISR(INT0_vect) -{ - canint = true; -} - static uint8_t spi_wrrd(uint8_t out) { SPDR = out; @@ -152,6 +145,9 @@ union { } can_rx_addr; uint8_t can_rx_dlc, can_rx_data[8]; +#define can_rx_avail() (can_rx_addr.u) +#define can_rx_pop() do { can_rx_addr.u = 0; } while (0) + static void can_rxh(uint8_t buffer) { uint8_t c, byte; @@ -162,6 +158,14 @@ static void can_rxh(uint8_t buffer) uart_puts("can: RX0IF\n"); spi_ss(0); spi_wrrd(0x90 + 0x04 * buffer); + + if (can_rx_addr.u) { + spi_wrrd(0xff); + spi_ss(1); + uart_puts("-- CAN RX overrun\n"); + return; + }; + c = 0; #define rdaddr() byte = spi_wrrd(0xff); uart_puthex(byte); can_rx_addr.b[c++] = byte rdaddr(); @@ -180,7 +184,7 @@ static void can_rxh(uint8_t buffer) spi_ss(1); } -static void can_int(void) +ISR(INT0_vect) { uint8_t canintf, eflg, canstat; @@ -230,6 +234,8 @@ static void can_int(void) static void can_preinit(void) { + can_rx_addr.u = 0; + spi_ss(1); #ifndef R0KET DDRB |= (1 << B_SCK) | (1 << B_MOSI) | (1 << B_SS); diff --git a/door.c b/door.c index 8780e81..561289f 100644 --- a/door.c +++ b/door.c @@ -107,10 +107,6 @@ int main(void) TCCR1B = (1 << WGM12) | (1 << CS11); while (1) { - if (canint) { - canint = false; - can_int(); - } _delay_ms(1); } } diff --git a/lightctrl.c b/lightctrl.c index 2a82ad3..e901714 100644 --- a/lightctrl.c +++ b/lightctrl.c @@ -223,15 +223,12 @@ int main(void) while (1) { wdt_reset(); - if (canint) { - canint = false; - can_int(); - if (can_rx_addr.b[0]) { - can_rx_exec(); - can_rx_addr.u = 0; - } + if (can_rx_avail()) { + can_rx_exec(); + can_rx_pop(); } + _delay_ms(5); do_tick(); -- cgit v1.2.1