Fixed rx buffer behaviour

Fixed bytes being swapped in error message
This commit is contained in:
Sebastian 2016-10-30 02:04:35 +02:00
parent 090cb3a201
commit e53fbf06a6
6 changed files with 20 additions and 14 deletions

View File

@ -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

View File

@ -1,6 +1,6 @@
AVRMCU ?= atmega8
F_CPU ?= 16000000
ISPPORT ?= /dev/ttyUSB0
ISPPORT ?= /dev/kaboard
VERSION = 0.1

View File

@ -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);
}

View File

@ -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;
};
}

View File

@ -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) {

View File

@ -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)