From 5da22fff03e55183c5f37e393348b7be83ae1a4d Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Sun, 20 Nov 2016 02:24:07 +0100 Subject: [PATCH] Started to seperate AVR specific code from SSS7 core --- software/AVR8/sss7.c | 33 ++++++++++++++++++++++++--------- software/AVR8/sss7.h | 6 ++++-- software/AVR8/uart.c | 12 +++++++++++- software/AVR8/uart.h | 11 ++++++++++- 4 files changed, 49 insertions(+), 13 deletions(-) diff --git a/software/AVR8/sss7.c b/software/AVR8/sss7.c index 33de007..5f36dac 100644 --- a/software/AVR8/sss7.c +++ b/software/AVR8/sss7.c @@ -1,9 +1,9 @@ #include "sss7.h" -#include -#include #include +#include "uart.h" + volatile enum sss7State sss7_state; uint8_t sss7_rx_buffer[SSS7_RX_BUFFER_COUNT][SSS7_PAYLOAD_SIZE]; @@ -18,6 +18,7 @@ volatile uint8_t sss7_tx_failed; uint8_t sss7_tx_last_byte; uint8_t sss7_tx_last_ack; + void sss7_init(void) { sss7_state = SSS7_IDLE; @@ -33,20 +34,34 @@ void sss7_init(void) { PORTB |= (1 << PB2) | (1 << PB3); } + 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]); + + for(uint8_t byte = 0; byte < SSS7_PAYLOAD_SIZE; byte++) { + crc = crc ^ buffer[byte]; + for (uint8_t bit = 0; bit < 8; bit++) + { + if (crc & 0x01) { + crc = (crc >> 1) ^ 0x8C; + } + else { + crc >>= 1; + } + } } + return crc; } + static inline void sss7_send_byte(uint8_t byte) { sss7_tx_last_ack = 0; sss7_tx_last_byte = byte; - UDR = byte; + uart_put_byte(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) { @@ -66,8 +81,8 @@ void sss7_send(uint8_t msg[SSS7_PAYLOAD_SIZE]) { } -ISR(USART_RXC_vect) { - uint8_t byte = UDR; +void sss7_process_rx(void) { + uint8_t byte = uart_get_byte(); uint8_t crc = 0; switch(sss7_state) { @@ -121,8 +136,8 @@ ISR(USART_RXC_vect) { } } -ISR(USART_TXC_vect) { +void sss7_process_tx(void) { if(sss7_tx_last_ack) { uint8_t byte; switch(sss7_state) { @@ -155,9 +170,9 @@ ISR(USART_TXC_vect) { sss7_tx_failed = 1; sss7_state = SSS7_IDLE; } - } + 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); diff --git a/software/AVR8/sss7.h b/software/AVR8/sss7.h index 793207e..fbcf317 100644 --- a/software/AVR8/sss7.h +++ b/software/AVR8/sss7.h @@ -24,6 +24,10 @@ extern volatile uint8_t sss7_tx_failed; extern uint8_t sss7_rx_active_buffer; extern uint8_t sss7_rx_oldest_buffer; +void sss7_process_rx(void); +void sss7_process_tx(void); + + void sss7_init(void); static inline uint8_t sss7_can_send(void) { @@ -40,8 +44,6 @@ static inline uint8_t sss7_has_received(void) { return sss7_rx_oldest_buffer < sss7_rx_active_buffer; } - - void sss7_get_received(uint8_t msg[SSS7_PAYLOAD_SIZE]); #endif diff --git a/software/AVR8/uart.c b/software/AVR8/uart.c index f7c09fc..3b1b427 100644 --- a/software/AVR8/uart.c +++ b/software/AVR8/uart.c @@ -1,6 +1,7 @@ #include "uart.h" -#include +#include +#include #include "sss7.h" @@ -23,3 +24,12 @@ void uart_init(void) { UCSRB |= (1 << TXCIE) | (1 << RXCIE); // enable tx and rx interrupts } + + +ISR(USART_RXC_vect) { + sss7_process_rx(); +} + +ISR(USART_TXC_vect) { + sss7_process_tx(); +} diff --git a/software/AVR8/uart.h b/software/AVR8/uart.h index 5813d24..1f6c59d 100644 --- a/software/AVR8/uart.h +++ b/software/AVR8/uart.h @@ -1,7 +1,7 @@ #ifndef _UART_H_ #define _UART_H_ - +#include #define BAUD 9600UL @@ -17,4 +17,13 @@ void uart_init(void); +static inline uint8_t uart_get_byte(void) { + uint8_t byte = UDR; + return byte; +} + +static inline void uart_put_byte(uint8_t byte) { + UDR = byte; +} + #endif