OpenModem/Modem/main.c

195 lines
6.9 KiB
C
Raw Normal View History

2014-04-03 16:21:37 -04:00
2014-04-06 14:26:24 -04:00
//////////////////////////////////////////////////////
// First things first, all the includes we need //
//////////////////////////////////////////////////////
2014-05-20 05:20:19 -04:00
#include <cpu/irq.h> // Interrupt functionality from BertOS
2014-04-06 11:17:13 -04:00
2014-05-20 05:20:19 -04:00
#include <drv/ser.h> // Serial driver from BertOS
#include <drv/timer.h> // Timer driver from BertOS
2014-04-06 11:17:13 -04:00
2014-05-20 05:20:19 -04:00
#include <stdio.h> // Standard input/output
#include <string.h> // String operations
2014-04-03 16:21:37 -04:00
2014-05-29 05:03:25 -04:00
#include <net/ax25.h>
2014-05-20 05:20:19 -04:00
#include "afsk.h" // Header for AFSK modem
#include "protocol/mp1.h" // Header for MP.1 protocol
2014-04-03 16:21:37 -04:00
2014-04-28 11:47:32 -04:00
#if SERIAL_DEBUG
2014-05-20 05:20:19 -04:00
#include "cfg/debug.h" // Debug configuration from BertOS
2014-04-28 11:47:32 -04:00
#endif
2014-04-14 05:44:39 -04:00
2014-04-06 14:26:24 -04:00
//////////////////////////////////////////////////////
2014-05-20 05:20:19 -04:00
// A few definitions //
2014-04-06 14:26:24 -04:00
//////////////////////////////////////////////////////
2014-05-20 05:20:19 -04:00
static Afsk afsk; // Declare a AFSK modem struct
static MP1 mp1; // Declare a protocol struct
static Serial ser; // Declare a serial interface struct
2014-04-03 16:21:37 -04:00
2014-05-20 05:20:19 -04:00
#define ADC_CH 0 // Define which channel (pin) we want
// for the ADC (this is A0 on arduino)
2014-04-03 16:21:37 -04:00
2014-04-04 05:17:47 -04:00
2014-05-20 05:20:19 -04:00
static uint8_t serialBuffer[MP1_MAX_DATA_SIZE]; // This is a buffer for incoming serial data
2014-05-20 05:20:19 -04:00
static int sbyte; // For holding byte read from serial port
static size_t serialLen = 0; // Counter for counting length of data from serial
static bool sertx = false; // Flag signifying whether it's time to send data
// received on the serial port.
2014-04-28 11:47:32 -04:00
#define SER_BUFFER_FULL (serialLen < MP1_MAX_DATA_SIZE-1)
2014-04-03 16:21:37 -04:00
2014-04-06 14:26:24 -04:00
//////////////////////////////////////////////////////
// And here comes the actual program :) //
//////////////////////////////////////////////////////
2014-04-06 11:17:13 -04:00
// This is a callback we register with the protocol,
// so we can process each packet as they are decoded.
// Right now it just prints the packet to the serial port.
2014-04-04 05:17:47 -04:00
static void mp1Callback(struct MP1Packet *packet) {
2014-05-20 05:20:19 -04:00
if (SERIAL_DEBUG) {
kfile_printf(&ser.fd, "%.*s\n", packet->dataLength, packet->data);
} else {
for (unsigned long i = 0; i < packet->dataLength; i++) {
kfile_putc(packet->data[i], &ser.fd);
}
}
2014-04-04 05:17:47 -04:00
}
2014-04-03 18:39:51 -04:00
2014-04-06 11:17:13 -04:00
// Simple initialization function.
2014-04-03 16:21:37 -04:00
static void init(void)
{
2014-05-20 05:20:19 -04:00
// Enable interrupts
IRQ_ENABLE;
2014-04-04 11:05:53 -04:00
2014-04-06 11:17:13 -04:00
// Initialize hardware timers
2014-05-20 05:20:19 -04:00
timer_init();
2014-04-03 16:21:37 -04:00
2014-05-20 05:20:19 -04:00
// Initialize serial comms on UART0,
// which is the hardware serial on arduino
ser_init(&ser, SER_UART0);
ser_setbaudrate(&ser, 9600);
2014-04-06 11:17:13 -04:00
2014-05-20 05:20:19 -04:00
// For some reason BertOS sets the serial
// to 7 bit characters by default. We set
// it to 8 instead.
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
2014-04-23 16:49:04 -04:00
2014-05-20 05:20:19 -04:00
// Create a modem context
afsk_init(&afsk, ADC_CH);
// ... and a protocol context with the modem
mp1Init(&mp1, &afsk.fd, mp1Callback);
2014-04-04 05:17:47 -04:00
2014-05-20 05:20:19 -04:00
// That's all!
2014-04-03 16:21:37 -04:00
}
int main(void)
{
2014-05-20 05:20:19 -04:00
// Start by running the main initialization
init();
// Record the current tick count for time-keeping
ticks_t start = timer_clock();
#if MP1_USE_TX_QUEUE
ticks_t frameQueued = 0;
#endif
// Go into ye good ol' infinite loop
while (1)
{
// First we instruct the protocol to check for
// incoming data
mp1Poll(&mp1);
// If there was actually some data waiting for us
// there, let's se what it tastes like :)
if (!sertx && ser_available(&ser)) {
// We then read a byte from the serial port.
// Notice that we use "_nowait" since we can't
// have this blocking execution until a byte
// comes in.
sbyte = ser_getchar_nowait(&ser);
// If SERIAL_DEBUG is specified we'll handle
// serial data as direct human input and only
// transmit when we get a LF character
#if SERIAL_DEBUG
// If we have not yet surpassed the maximum frame length
// and the byte is not a "transmit" (newline) character,
// we should store it for transmission.
if ((serialLen < MP1_MAX_DATA_SIZE) && (sbyte != 10)) {
// Put the read byte into the buffer;
serialBuffer[serialLen] = sbyte;
// Increment the read length counter
serialLen++;
} else {
// If one of the above conditions were actually the
// case, it means we have to transmit, se we set
// transmission flag to true.
sertx = true;
}
#else
// Otherwise we assume the modem is running
// in automated mode, and we push out data
// as it becomes available. We either transmit
// immediately when the max frame length has
// been reached, or when we get no input for
// a certain amount of time.
if (serialLen < MP1_MAX_DATA_SIZE-1) {
// Put the read byte into the buffer;
serialBuffer[serialLen] = sbyte;
// Increment the read length counter
serialLen++;
} else {
// If max frame length has been reached
// we need to transmit.
serialBuffer[serialLen] = sbyte;
serialLen++;
sertx = true;
}
start = timer_clock();
#endif
} else {
if (!SERIAL_DEBUG && serialLen > 0 && timer_clock() - start > ms_to_ticks(TX_MAXWAIT)) {
sertx = true;
}
}
// Check whether we should send data in our serial buffer
if (sertx) {
#if MP1_USE_TX_QUEUE
mp1QueueFrame(&mp1, serialBuffer, serialLen);
frameQueued = timer_clock();
sertx = false;
serialLen = 0;
#else
// Wait until incoming packets are done
if (!mp1CarrierSense(&mp1)) {
// And then send the data
mp1Send(&mp1, serialBuffer, serialLen);
// Reset the transmission flag and length counter
sertx = false;
serialLen = 0;
}
#endif
}
#if MP1_USE_TX_QUEUE
// We first wait a little to see if more
// frames are coming in.
if (timer_clock() - frameQueued > ms_to_ticks(MP1_QUEUE_TX_WAIT)) {
if (!ser_available(&ser) && !mp1CarrierSense(&mp1)) {
// And if not, we send process the frame
// queue if possible.
mp1ProcessQueue(&mp1);
}
}
#endif
}
return 0;
2014-04-03 16:41:49 -04:00
}