Sending parts works
Added logic to detect bytes being send but never received
This commit is contained in:
parent
0cc3435cdc
commit
3fa59ddd0a
|
@ -1,2 +1,3 @@
|
||||||
-I/usr/avr/include
|
-I/usr/avr/include
|
||||||
-D__AVR_ATmega328__
|
-D__AVR_ATmega8__
|
||||||
|
-DF_CPU=16000000
|
||||||
|
|
|
@ -5,7 +5,7 @@ ISPPORT ?= /dev/ttyUSB0
|
||||||
VERSION = 0.1
|
VERSION = 0.1
|
||||||
|
|
||||||
HEADERS =
|
HEADERS =
|
||||||
SRC = main.c
|
SRC = main.c sss7.c uart.c
|
||||||
TARGET = sss7
|
TARGET = sss7
|
||||||
OBJDIR = bin
|
OBJDIR = bin
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,40 @@
|
||||||
|
#include <avr/io.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
#include <util/delay.h>
|
||||||
|
|
||||||
|
#include "uart.h"
|
||||||
#include "sss7.h"
|
#include "sss7.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(void) {
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,15 +8,17 @@ volatile enum sss7State sss7_state;
|
||||||
|
|
||||||
uint8_t sss7_rx_buffer[2][SSS7_PAYLOAD_SIZE];
|
uint8_t sss7_rx_buffer[2][SSS7_PAYLOAD_SIZE];
|
||||||
uint8_t sss7_rx_active_buffer;
|
uint8_t sss7_rx_active_buffer;
|
||||||
|
uint8_t sss7_rx_oldest_buffer;
|
||||||
uint8_t sss7_rx_pos;
|
uint8_t sss7_rx_pos;
|
||||||
|
|
||||||
uint8_t sss7_tx_buffer[SSS7_PAYLOAD_SIZE];
|
uint8_t sss7_tx_buffer[SSS7_PAYLOAD_SIZE];
|
||||||
uint8_t sss7_tx_pos;
|
uint8_t sss7_tx_pos;
|
||||||
uint8_t sss7_tx_crc;
|
uint8_t sss7_tx_crc;
|
||||||
uint8_t sss7_tx_failed;
|
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_state = SSS7_IDLE;
|
||||||
|
|
||||||
sss7_rx_pos = 0;
|
sss7_rx_pos = 0;
|
||||||
|
@ -26,9 +28,12 @@ void sss7_init() {
|
||||||
sss7_tx_pos = 0;
|
sss7_tx_pos = 0;
|
||||||
sss7_tx_crc = 0;
|
sss7_tx_crc = 0;
|
||||||
sss7_tx_failed = 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;
|
uint8_t crc = 0;
|
||||||
for(uint8_t i = 0; i < SSS7_PAYLOAD_SIZE; i++) {
|
for(uint8_t i = 0; i < SSS7_PAYLOAD_SIZE; i++) {
|
||||||
crc = _crc_ibutton_update(crc, buffer[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;
|
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]) {
|
void sss7_send(uint8_t msg[SSS7_PAYLOAD_SIZE]) {
|
||||||
// Check that we can send, because we will overwritte the buffer
|
// Check that we can send, because we will overwritte the buffer
|
||||||
if(sss7_state != SSS7_IDLE) {
|
if(sss7_state != SSS7_IDLE) {
|
||||||
|
@ -50,11 +61,13 @@ void sss7_send(uint8_t msg[SSS7_PAYLOAD_SIZE]) {
|
||||||
|
|
||||||
// Commit to send state
|
// Commit to send state
|
||||||
sss7_state = SSS7_TX_HEADER;
|
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;
|
uint8_t crc = 0;
|
||||||
|
|
||||||
switch(sss7_state) {
|
switch(sss7_state) {
|
||||||
|
@ -99,40 +112,46 @@ void sss7_receive_byte() {
|
||||||
sss7_state = SSS7_IDLE;
|
sss7_state = SSS7_IDLE;
|
||||||
sss7_tx_failed = 1;
|
sss7_tx_failed = 1;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
sss7_tx_last_ack = 1;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sss7_transmit_byte() {
|
ISR(USART_TXC_vect) {
|
||||||
uint8_t byte;
|
PORTB ^= (1 << PB2);
|
||||||
switch(sss7_state) {
|
|
||||||
case SSS7_TX_HEADER:
|
|
||||||
uart_sent_byte(SSS7_HEADER[1]);
|
|
||||||
sss7_state = SSS7_TX_PAYLOAD;
|
|
||||||
sss7_tx_pos = 0;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SSS7_TX_PAYLOAD:
|
if(sss7_tx_last_ack) {
|
||||||
byte = sss7_tx_buffer[sss7_tx_pos];
|
uint8_t byte;
|
||||||
uart_sent_byte(byte);
|
switch(sss7_state) {
|
||||||
sss7_tx_last_byte = byte;
|
case SSS7_TX_HEADER:
|
||||||
sss7_tx_pos++;
|
sss7_send_byte(SSS7_HEADER[1]);
|
||||||
|
sss7_state = SSS7_TX_PAYLOAD;
|
||||||
|
sss7_tx_pos = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
if(sss7_tx_pos >= SSS7_PAYLOAD_SIZE) {
|
case SSS7_TX_PAYLOAD:
|
||||||
sss7_state = SSS7_TX_CRC;
|
byte = sss7_tx_buffer[sss7_tx_pos];
|
||||||
}
|
sss7_send_byte(byte);
|
||||||
break;
|
sss7_tx_pos++;
|
||||||
|
|
||||||
case SSS7_TX_CRC:
|
if(sss7_tx_pos >= SSS7_PAYLOAD_SIZE) {
|
||||||
uart_sent_byte(sss7_tx_crc);
|
sss7_state = SSS7_TX_CRC;
|
||||||
sss7_tx_last_byte = sss7_tx_crc;
|
}
|
||||||
sss7_state = SSS7_IDLE;
|
break;
|
||||||
break;
|
|
||||||
|
|
||||||
//All the RX states
|
case SSS7_TX_CRC:
|
||||||
default:
|
sss7_send_byte(sss7_tx_crc);
|
||||||
break;
|
sss7_state = SSS7_IDLE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
sss7_tx_failed = 1;
|
||||||
|
sss7_state = SSS7_IDLE;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sss7_get_received(uint8_t msg[SSS7_PAYLOAD_SIZE]) {
|
void sss7_get_received(uint8_t msg[SSS7_PAYLOAD_SIZE]) {
|
||||||
|
|
|
@ -14,7 +14,7 @@ enum sss7State {
|
||||||
SSS7_RX_CRC
|
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_PAYLOAD_SIZE 16
|
||||||
#define SSS7_RX_BUFFER_COUNT 2
|
#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_active_buffer;
|
||||||
extern uint8_t sss7_rx_oldest_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;
|
return sss7_state == SSS7_IDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sss7_send(uint8_t msg[SSS7_PAYLOAD_SIZE]);
|
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;
|
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;
|
return sss7_rx_oldest_buffer < sss7_rx_active_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -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
|
Loading…
Reference in New Issue