]> www.fi.muni.cz Git - openparking.git/commitdiff
Use TX end IRQ instead of busy wait
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 22 May 2015 22:13:07 +0000 (00:13 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 22 May 2015 22:13:07 +0000 (00:13 +0200)
firmware/modbus.c
firmware/rs485.c

index 0c238c2a9b00d655522bcafee847753a2b80ba2d..2b2a255bedc15d8523d30a9b5d8d518421de5331 100644 (file)
@@ -43,7 +43,6 @@ static uint8_t mb_unit_id;
 
 #define UART_BAUD      9600
 #define UBRR_VAL        ((F_CPU + 8UL * UART_BAUD) / (16UL*UART_BAUD) - 1)
-#define wait_one_byte()        _delay_us(10*1000000/UART_BAUD)
 
 /*
  * According to Wikipedia, it is indeed 28 bits = 3.5 bytes without
@@ -267,14 +266,19 @@ ISR(USART_RX_vect)
        }
 }
 
+ISR(USART_TX_vect)
+{
+       UCSR0B &= ~_BV(TXCIE0); // disable further IRQs
+       ctl_pin_off();
+}
+
 ISR(USART_UDRE_vect)
 {
        ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
                if (tx_head == tx_tail) {
+                       UCSR0B |= _BV(TXCIE0); // enable xmit complete irq
                        UCSR0B &= ~_BV(UDRIE0);
                        tx_tail = tx_head = 0;
-                       wait_one_byte(); // FIXME: too long busy-wait
-                       ctl_pin_off();
                } else {
                        UDR0 = txbuf[tx_tail];
                        tx_tail = bufptr_inc(tx_tail);
index 6d16a2af05840de11d0808bfe1d63c0ded2e5100..7aec92c8e54a4abf1f31691b411e44a5aac4a167 100644 (file)
@@ -34,7 +34,6 @@ static volatile char rxbuf[BUFSIZE], txbuf[BUFSIZE];
 
 #define UART_BAUD      9600
 #define UBRR_VAL        ((F_CPU + 8UL * UART_BAUD) / (16UL*UART_BAUD) - 1)
-#define wait_one_byte()        _delay_us(10*1000000/UART_BAUD)
 
 void rs485_init()
 {
@@ -99,13 +98,18 @@ ISR(USART_RX_vect)
        sei();
 }
 
+ISR(USART_TX_vect)
+{
+       UCSR0B &= ~_BV(TXCIE0); // disable further IRQs
+       ctl_pin_off();
+}
+
 ISR(USART_UDRE_vect)
 {
        cli();
        if (tx_head == tx_tail) {
+               UCSR0B |= _BV(TXCIE0); // enable xmit complete irq
                UCSR0B &= ~_BV(UDRIE0);
-               wait_one_byte();
-               ctl_pin_off();
        } else {
                UDR0 = txbuf[tx_tail];
                tx_tail = bufptr_inc(tx_tail);