From 3fa59ddd0ac6d03675ae73c1d52d9a501fa8600e Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Thu, 27 Oct 2016 23:35:24 +0200 Subject: [PATCH] Sending parts works Added logic to detect bytes being send but never received --- software/AVR8/.clang_complete | 3 +- software/AVR8/Makefile | 2 +- software/AVR8/main.c | 33 +++++++++++++- software/AVR8/sss7.c | 81 +++++++++++++++++++++-------------- software/AVR8/sss7.h | 10 ++--- software/AVR8/uart.c | 25 +++++++++++ software/AVR8/uart.h | 20 +++++++++ 7 files changed, 135 insertions(+), 39 deletions(-) create mode 100644 software/AVR8/uart.c create mode 100644 software/AVR8/uart.h diff --git a/software/AVR8/.clang_complete b/software/AVR8/.clang_complete index a5f3d5c..7a4710a 100644 --- a/software/AVR8/.clang_complete +++ b/software/AVR8/.clang_complete @@ -1,2 +1,3 @@ -I/usr/avr/include --D__AVR_ATmega328__ +-D__AVR_ATmega8__ +-DF_CPU=16000000 diff --git a/software/AVR8/Makefile b/software/AVR8/Makefile index bb6f1e7..405ebcf 100644 --- a/software/AVR8/Makefile +++ b/software/AVR8/Makefile @@ -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 diff --git a/software/AVR8/main.c b/software/AVR8/main.c index b21ec5d..18fb381 100644 --- a/software/AVR8/main.c +++ b/software/AVR8/main.c @@ -1,9 +1,40 @@ +#include +#include +#include - +#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); +} + } diff --git a/software/AVR8/sss7.c b/software/AVR8/sss7.c index 2d61fa7..e309b05 100644 --- a/software/AVR8/sss7.c +++ b/software/AVR8/sss7.c @@ -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]) { diff --git a/software/AVR8/sss7.h b/software/AVR8/sss7.h index e7b808c..f4ab3db 100644 --- a/software/AVR8/sss7.h +++ b/software/AVR8/sss7.h @@ -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; } diff --git a/software/AVR8/uart.c b/software/AVR8/uart.c new file mode 100644 index 0000000..f7c09fc --- /dev/null +++ b/software/AVR8/uart.c @@ -0,0 +1,25 @@ +#include "uart.h" + +#include + +#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 +} diff --git a/software/AVR8/uart.h b/software/AVR8/uart.h new file mode 100644 index 0000000..5813d24 --- /dev/null +++ b/software/AVR8/uart.h @@ -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