From: Jan "Yenya" Kasprzak Date: Mon, 25 May 2015 18:20:54 +0000 (+0200) Subject: RS485 xmit fixes X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=openparking.git;a=commitdiff_plain;h=d8f201d671212ce6ea71de49e852776e5f03f4e7 RS485 xmit fixes --- diff --git a/firmware/modbus.c b/firmware/modbus.c index e45f5f0..b726280 100644 --- a/firmware/modbus.c +++ b/firmware/modbus.c @@ -28,7 +28,7 @@ typedef uint8_t bufptr_t; #endif static volatile bufptr_t buf_len, tx_ptr; -static volatile uint8_t buffer[BUFSIZE], transmitting;; +static volatile uint8_t buffer[BUFSIZE], transmitting; static volatile uint16_t last_rx; #ifndef mb_unit_id static uint8_t mb_unit_id; @@ -199,14 +199,14 @@ uint8_t modbus_poll() switch (buffer[1]) { // function case 3: - if (packet_len == 5) + if (packet_len == 6) rv = read_holding_regs( get_word(buffer, 2), get_word(buffer, 4) ); break; case 6: - if (packet_len == 5) + if (packet_len == 6) rv = write_single_reg( get_word(buffer, 2), get_word(buffer, 4) @@ -267,7 +267,8 @@ ISR(USART_TX_vect) ISR(USART_UDRE_vect) { - if (tx_ptr+1 >= buf_len) { + if (tx_ptr >= buf_len) { + UCSR0A |= _BV(TXC0); // clear the pending TXC flag UCSR0B |= _BV(TXCIE0); // enable xmit complete irq UCSR0B &= ~_BV(UDRIE0); // disable ourselves } else { diff --git a/firmware/rs485.c b/firmware/rs485.c index 7aec92c..7c90324 100644 --- a/firmware/rs485.c +++ b/firmware/rs485.c @@ -108,6 +108,7 @@ ISR(USART_UDRE_vect) { cli(); if (tx_head == tx_tail) { + UCSR0A |= _BV(TXC0); // clear the pending TXC flag UCSR0B |= _BV(TXCIE0); // enable xmit complete irq UCSR0B &= ~_BV(UDRIE0); } else {