From e53fbf06a69762685b82dd74dd70b3cb1d4347d8 Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Sun, 30 Oct 2016 02:04:35 +0200 Subject: [PATCH] Fixed rx buffer behaviour Fixed bytes being swapped in error message --- README.md | 6 ++++-- software/AVR8/Makefile | 2 +- software/AVR8/main.c | 7 +++---- software/AVR8/sss7.c | 13 +++++++++---- software/AVR8/sss7.h | 4 ++-- software/testutils/send.py | 2 +- 6 files changed, 20 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 877b79e..1733c1c 100644 --- a/README.md +++ b/README.md @@ -81,9 +81,11 @@ Initial value: 0x0 the frame is considered timed out and all received data can be dropped. - Incoming messages are stored in a fifo until the application retrieves them. - The receive fifo has a size of at least 2 messages. -- If the fifo is full new messages will be dropped, until the application retrieves a message. +- If the fifo is full new messages will override the older ones. + It is up to the application retrieve message in time. - Even if sending was successful, there is still a chance that the receiver could not - receive the frame due to missing buffer space or not enough processing time to react. **Important messages should utilize a ack-mechanism.** + receive the frame due to missing buffer space or not enough processing time to react. + **Important messages should utilize a ack-mechanism.** - It is up to the application to resend messages in case of a collision/missing ack. ### Planned API diff --git a/software/AVR8/Makefile b/software/AVR8/Makefile index 6e8afdf..09b4c36 100644 --- a/software/AVR8/Makefile +++ b/software/AVR8/Makefile @@ -1,6 +1,6 @@ AVRMCU ?= atmega8 F_CPU ?= 16000000 -ISPPORT ?= /dev/ttyUSB0 +ISPPORT ?= /dev/kaboard VERSION = 0.1 diff --git a/software/AVR8/main.c b/software/AVR8/main.c index df74832..2ef2ec3 100644 --- a/software/AVR8/main.c +++ b/software/AVR8/main.c @@ -33,15 +33,14 @@ sei(); while(1) { - if(sss7_can_send()) { - sss7_send(msg); - } + while(!sss7_can_send()); + sss7_send(msg); while(!sss7_can_send()); if(sss7_send_failed()) { PORTB ^= (1 << PB2); } - _delay_ms(250); + _delay_ms(100); } diff --git a/software/AVR8/sss7.c b/software/AVR8/sss7.c index 9aaf671..33de007 100644 --- a/software/AVR8/sss7.c +++ b/software/AVR8/sss7.c @@ -6,7 +6,7 @@ volatile enum sss7State sss7_state; -uint8_t sss7_rx_buffer[2][SSS7_PAYLOAD_SIZE]; +uint8_t sss7_rx_buffer[SSS7_RX_BUFFER_COUNT][SSS7_PAYLOAD_SIZE]; uint8_t sss7_rx_active_buffer; uint8_t sss7_rx_oldest_buffer; uint8_t sss7_rx_pos; @@ -14,7 +14,7 @@ uint8_t sss7_rx_pos; uint8_t sss7_tx_buffer[SSS7_PAYLOAD_SIZE]; uint8_t sss7_tx_pos; uint8_t sss7_tx_crc; -uint8_t sss7_tx_failed; +volatile uint8_t sss7_tx_failed; uint8_t sss7_tx_last_byte; uint8_t sss7_tx_last_ack; @@ -74,6 +74,7 @@ ISR(USART_RXC_vect) { case SSS7_IDLE: if(byte == SSS7_HEADER[0]) { sss7_state = SSS7_RX_HEADER; + PORTB |= (1 << PB3); } else { sss7_state = SSS7_IDLE; @@ -101,8 +102,9 @@ ISR(USART_RXC_vect) { case SSS7_RX_CRC: crc = sss7_payload_crc(sss7_rx_buffer[sss7_rx_active_buffer]); if(byte == crc) { - sss7_rx_active_buffer = (sss7_rx_active_buffer + 1); + sss7_rx_active_buffer = (sss7_rx_active_buffer + 1) % SSS7_RX_BUFFER_COUNT; } + PORTB &= ~(1 << PB3); sss7_state = SSS7_IDLE; break; @@ -144,6 +146,9 @@ ISR(USART_TXC_vect) { sss7_send_byte(sss7_tx_crc); sss7_state = SSS7_IDLE; break; + + default: + break; } } else { @@ -156,6 +161,6 @@ ISR(USART_TXC_vect) { void sss7_get_received(uint8_t msg[SSS7_PAYLOAD_SIZE]) { if(sss7_has_received()) { memcpy(msg, sss7_rx_buffer[sss7_rx_oldest_buffer], SSS7_PAYLOAD_SIZE); - sss7_rx_oldest_buffer++; + sss7_rx_oldest_buffer = (sss7_rx_oldest_buffer + 1) % SSS7_RX_BUFFER_COUNT; }; } diff --git a/software/AVR8/sss7.h b/software/AVR8/sss7.h index 3347100..793207e 100644 --- a/software/AVR8/sss7.h +++ b/software/AVR8/sss7.h @@ -20,7 +20,7 @@ const static uint8_t SSS7_HEADER[] = {0xAA, 0xFE}; #define SSS7_RX_BUFFER_COUNT 2 extern volatile enum sss7State sss7_state; -extern uint8_t sss7_tx_failed; +extern volatile uint8_t sss7_tx_failed; extern uint8_t sss7_rx_active_buffer; extern uint8_t sss7_rx_oldest_buffer; @@ -33,7 +33,7 @@ static inline uint8_t sss7_can_send(void) { void sss7_send(uint8_t msg[SSS7_PAYLOAD_SIZE]); static inline uint8_t sss7_send_failed(void) { - return sss7_state == SSS7_IDLE && sss7_tx_failed; + return sss7_tx_failed; } static inline uint8_t sss7_has_received(void) { diff --git a/software/testutils/send.py b/software/testutils/send.py index 1ca282d..c4d1dab 100644 --- a/software/testutils/send.py +++ b/software/testutils/send.py @@ -10,7 +10,7 @@ def send_byte(ser, byte): ser.write(byte) read_byte = ser.read() if read_byte != byte: - print "Written %s read %s" % (hex(ord(read_byte)), hex(ord(byte))) + print "Written %s read %s" % (hex(ord(byte)), hex(ord(read_byte))) sys.exit(-1)