]> www.fi.muni.cz Git - openparking.git/commitdiff
RS485 xmit fixes
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 25 May 2015 18:20:54 +0000 (20:20 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 25 May 2015 18:20:54 +0000 (20:20 +0200)
firmware/modbus.c
firmware/rs485.c

index e45f5f0f7ba9064472ba9ff6510ac5e4fdc5df3b..b726280f9922f9de4abea11a702e0a69ba58af0f 100644 (file)
@@ -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 {
index 7aec92c8e54a4abf1f31691b411e44a5aac4a167..7c9032452930f221ab4c2f0fe0d9468dcd02f459 100644 (file)
@@ -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 {