Started to seperate AVR specific code from SSS7 core

This commit is contained in:
Sebastian 2016-11-20 02:24:07 +01:00
parent 27a6338585
commit 5da22fff03
4 changed files with 49 additions and 13 deletions

View File

@ -1,9 +1,9 @@
#include "sss7.h"
#include <avr/interrupt.h>
#include <util/crc16.h>
#include <string.h>
#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);

View File

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

View File

@ -1,6 +1,7 @@
#include "uart.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/crc16.h>
#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();
}

View File

@ -1,7 +1,7 @@
#ifndef _UART_H_
#define _UART_H_
#include <avr/io.h>
#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