sss7modem/software/Arduino/ardusss7.cpp

89 lines
1.5 KiB
C++
Raw Normal View History

2016-11-21 12:48:29 +01:00
#include "ardusss7.h"
#include <avr/io.h>
#include <avr/interrupt.h>
SSS7Wrapper SSS7;
void SSS7Wrapper::init() {
2016-11-21 12:48:29 +01:00
sss7_init();
this->setupUart();
this->setupTimer();
}
uint8_t SSS7Wrapper::canSend() {
return sss7_can_send();
}
void SSS7Wrapper::send(uint8_t msg[SSS7_PAYLOAD_SIZE]) {
return sss7_send(msg);
}
uint8_t SSS7Wrapper::sendFailed() {
return sss7_send_failed();
}
uint8_t SSS7Wrapper::hasReceived() {
return sss7_has_received();
}
void SSS7Wrapper::getReceived(uint8_t msg[SSS7_PAYLOAD_SIZE]) {
sss7_get_received(msg);
}
void SSS7Wrapper::setupUart() {
UBRR2H = UBRR_VAL >> 8; // Setting baudrate
UBRR2L = UBRR_VAL & 0xFF;
2016-11-21 12:48:29 +01:00
UCSR2B = (1 << TXEN2) | (1 << RXEN2); // Enable TX and RX
UCSR2C = (1 << UCSZ21) | (1 << UCSZ20); // Asynchronous 8N1
2016-11-21 12:48:29 +01:00
// flush UDR
do
{
UDR2;
2016-11-21 12:48:29 +01:00
}
while (UCSR2A & (1 << RXC2));
2016-11-21 12:48:29 +01:00
// reset tx and rx complete flags
UCSR2A = (1 << RXC2) | (1 << TXC2);
2016-11-21 12:48:29 +01:00
UCSR2B |= (1 << TXCIE2) | (1 << RXCIE2); // enable tx and rx interrupts
2016-11-21 12:48:29 +01:00
}
void uart_put_byte(uint8_t byte) {
UDR2 = byte;
2016-11-21 12:48:29 +01:00
}
uint8_t uart_get_byte() {
return UDR2;
2016-11-21 12:48:29 +01:00
}
ISR(USART2_RX_vect) {
2016-11-21 12:48:29 +01:00
sss7_process_rx();
}
ISR(USART2_TX_vect) {
2016-11-21 12:48:29 +01:00
sss7_process_tx();
}
2016-11-21 13:10:14 +01:00
void SSS7Wrapper::setupTimer() {
2016-11-26 22:44:56 +01:00
TCCR4B = 0;
2016-11-21 13:10:14 +01:00
TCNT4 = 65535 - 16000; //Preload for 16000 ticks to overflow
2016-11-26 22:44:56 +01:00
// Take the Timer by force ...
TCCR4A = 0;
2016-11-21 13:10:14 +01:00
TCCR4B = (1 << CS40); // Prescaler 1
2016-11-26 22:44:56 +01:00
TCCR4C = 0;
TIMSK4 = (1 << TOIE4);
2016-11-21 13:10:14 +01:00
}
ISR(TIMER4_OVF_vect) {
TCNT4 = 65535 - 16000; //Preload for 16000 ticks to overflow
sss7_process_ticks(sss7_timeout_increment);
}