X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=firmware%2Fmodbus.c;h=2b2a255bedc15d8523d30a9b5d8d518421de5331;hb=8b1a6253ffcd4365a8ee3d3531d2a3600732504d;hp=a49bd877e1c778357e5f3f39f3f1b5ece6defbec;hpb=21e8a8ac43a7de8539e40ef6dfe7e2b7c731176c;p=openparking.git diff --git a/firmware/modbus.c b/firmware/modbus.c old mode 100755 new mode 100644 index a49bd87..2b2a255 --- a/firmware/modbus.c +++ b/firmware/modbus.c @@ -11,6 +11,7 @@ #include #include +#include "clock.h" #include "modbus.h" #define BUFSIZE 128 // must be a power of two @@ -42,15 +43,12 @@ 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) -#define get_clock() (TCNT1) -#define CLOCK_SPEED (F_CPU/1024) /* * According to Wikipedia, it is indeed 28 bits = 3.5 bytes without * start- and stopbits. */ -#define TIMEOUT (28*CLOCK_SPEED/UART_BAUD) +#define REQ_TIMEOUT (28*CLOCK_HZ/UART_BAUD) uint16_t hold_regs[MB_N_HOLD_REGS]; @@ -196,7 +194,7 @@ void modbus_poll() if (rx_bytes == 0) // nothing received yet return; - if (get_clock() - last_rx < TIMEOUT) // still receiving + if (get_clock() - last_rx < REQ_TIMEOUT) // still receiving return; if (rx_bytes < 4) { // too short @@ -268,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);