Sending parts works

Added logic to detect bytes being send but never received
This commit is contained in:
Sebastian 2016-10-27 23:35:24 +02:00
parent 0cc3435cdc
commit 3fa59ddd0a
7 changed files with 135 additions and 39 deletions

View File

@ -1,2 +1,3 @@
-I/usr/avr/include
-D__AVR_ATmega328__
-D__AVR_ATmega8__
-DF_CPU=16000000

View File

@ -5,7 +5,7 @@ ISPPORT ?= /dev/ttyUSB0
VERSION = 0.1
HEADERS =
SRC = main.c
SRC = main.c sss7.c uart.c
TARGET = sss7
OBJDIR = bin

View File

@ -1,9 +1,40 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "uart.h"
#include "sss7.h"
int main(void) {
uint8_t msg[SSS7_PAYLOAD_SIZE];
msg[0] = 'H';
msg[1] = 'e';
msg[2] = 'l';
msg[3] = 'l';
msg[4] = 'o';
msg[5] = ' ';
msg[6] = 'W';
msg[7] = 'o';
msg[8] = 'r';
msg[9] = 'l';
msg[10] = 'd';
uart_init();
sss7_init();
sei();
while(1) {
if(sss7_can_send()) {
sss7_send(msg);
}
_delay_ms(250);
}
}

View File

@ -8,15 +8,17 @@ volatile enum sss7State sss7_state;
uint8_t sss7_rx_buffer[2][SSS7_PAYLOAD_SIZE];
uint8_t sss7_rx_active_buffer;
uint8_t sss7_rx_oldest_buffer;
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;
uint8_t sss7_tx_last_byte = 0;
uint8_t sss7_tx_last_byte;
uint8_t sss7_tx_last_ack;
void sss7_init() {
void sss7_init(void) {
sss7_state = SSS7_IDLE;
sss7_rx_pos = 0;
@ -26,9 +28,12 @@ void sss7_init() {
sss7_tx_pos = 0;
sss7_tx_crc = 0;
sss7_tx_failed = 0;
DDRB |= (1 << PB2)| (1 << PB3);
PORTB |= (1 << PB2) | (1 << PB3);
}
inline uint8_t sss7_payload_crc(uint8_t buffer[SSS7_PAYLOAD_SIZE]) {
static inline uint8_t sss7_payload_crc(uint8_t buffer[SSS7_PAYLOAD_SIZE]) {
uint8_t crc = 0;
for(uint8_t i = 0; i < SSS7_PAYLOAD_SIZE; i++) {
crc = _crc_ibutton_update(crc, buffer[i]);
@ -36,6 +41,12 @@ inline uint8_t sss7_payload_crc(uint8_t buffer[SSS7_PAYLOAD_SIZE]) {
return crc;
}
static inline void sss7_send_byte(uint8_t byte) {
sss7_tx_last_ack = 0;
sss7_tx_last_byte = byte;
UDR = byte;
}
void sss7_send(uint8_t msg[SSS7_PAYLOAD_SIZE]) {
// Check that we can send, because we will overwritte the buffer
if(sss7_state != SSS7_IDLE) {
@ -50,11 +61,13 @@ void sss7_send(uint8_t msg[SSS7_PAYLOAD_SIZE]) {
// Commit to send state
sss7_state = SSS7_TX_HEADER;
uart_sent_byte(SSS7_HEADER[0]);
sss7_send_byte(SSS7_HEADER[0]);
}
void sss7_receive_byte() {
uint8_t byte = uart_get_byte();
ISR(USART_RXC_vect) {
PORTB ^= (1 << PB3);
uint8_t byte = UDR;
uint8_t crc = 0;
switch(sss7_state) {
@ -99,40 +112,46 @@ void sss7_receive_byte() {
sss7_state = SSS7_IDLE;
sss7_tx_failed = 1;
}
else {
sss7_tx_last_ack = 1;
}
break;
}
}
void sss7_transmit_byte() {
uint8_t byte;
switch(sss7_state) {
case SSS7_TX_HEADER:
uart_sent_byte(SSS7_HEADER[1]);
sss7_state = SSS7_TX_PAYLOAD;
sss7_tx_pos = 0;
break;
ISR(USART_TXC_vect) {
PORTB ^= (1 << PB2);
case SSS7_TX_PAYLOAD:
byte = sss7_tx_buffer[sss7_tx_pos];
uart_sent_byte(byte);
sss7_tx_last_byte = byte;
sss7_tx_pos++;
if(sss7_tx_last_ack) {
uint8_t byte;
switch(sss7_state) {
case SSS7_TX_HEADER:
sss7_send_byte(SSS7_HEADER[1]);
sss7_state = SSS7_TX_PAYLOAD;
sss7_tx_pos = 0;
break;
if(sss7_tx_pos >= SSS7_PAYLOAD_SIZE) {
sss7_state = SSS7_TX_CRC;
}
break;
case SSS7_TX_PAYLOAD:
byte = sss7_tx_buffer[sss7_tx_pos];
sss7_send_byte(byte);
sss7_tx_pos++;
case SSS7_TX_CRC:
uart_sent_byte(sss7_tx_crc);
sss7_tx_last_byte = sss7_tx_crc;
sss7_state = SSS7_IDLE;
break;
if(sss7_tx_pos >= SSS7_PAYLOAD_SIZE) {
sss7_state = SSS7_TX_CRC;
}
break;
//All the RX states
default:
break;
case SSS7_TX_CRC:
sss7_send_byte(sss7_tx_crc);
sss7_state = SSS7_IDLE;
break;
}
}
else {
sss7_tx_failed = 1;
sss7_state = SSS7_IDLE;
}
}
void sss7_get_received(uint8_t msg[SSS7_PAYLOAD_SIZE]) {

View File

@ -14,7 +14,7 @@ enum sss7State {
SSS7_RX_CRC
};
const uint8_t SSS7_HEADER[] = {0xAA, 0xFE};
const static uint8_t SSS7_HEADER[] = {0xAA, 0xFE};
#define SSS7_PAYLOAD_SIZE 16
#define SSS7_RX_BUFFER_COUNT 2
@ -24,19 +24,19 @@ extern uint8_t sss7_tx_failed;
extern uint8_t sss7_rx_active_buffer;
extern uint8_t sss7_rx_oldest_buffer;
void sss7_init();
void sss7_init(void);
static inline uint8_t sss7_can_send() {
static inline uint8_t sss7_can_send(void) {
return sss7_state == SSS7_IDLE;
}
void sss7_send(uint8_t msg[SSS7_PAYLOAD_SIZE]);
static inline uint8_t sss7_send_failed() {
static inline uint8_t sss7_send_failed(void) {
return sss7_state != SSS7_IDLE && sss7_tx_failed;
}
static inline uint8_t sss7_has_received() {
static inline uint8_t sss7_has_received(void) {
return sss7_rx_oldest_buffer < sss7_rx_active_buffer;
}

25
software/AVR8/uart.c Normal file
View File

@ -0,0 +1,25 @@
#include "uart.h"
#include <avr/io.h>
#include "sss7.h"
void uart_init(void) {
UBRRH = UBRR_VAL >> 8; // Setting baudrate
UBRRL = UBRR_VAL & 0xFF;
UCSRB = (1 << TXEN) | (1 << RXEN); // Enable TX and RX
UCSRC = (1 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0); // Asynchronous 8N1
// flush UDR
do
{
UDR;
}
while (UCSRA & (1 << RXC));
// reset tx and rx complete flags
UCSRA = (1 << RXEN) | (1 << TXC);
UCSRB |= (1 << TXCIE) | (1 << RXCIE); // enable tx and rx interrupts
}

20
software/AVR8/uart.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef _UART_H_
#define _UART_H_
#define BAUD 9600UL
// Some calculations ...
#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1) // Rounding magic
#define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1))) // Real baudrate
#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // Error in 0.1%
#if ((BAUD_ERROR<950) || (BAUD_ERROR>1050)) // Make sure our UBRR_VAL will work
#error Baudrate error is bigger then 1% !
#endif
void uart_init(void);
#endif