MicroAPRS/Modem/main.c

112 lines
3.4 KiB
C
Raw Normal View History

2014-04-03 14:21:37 -06:00
2014-04-06 12:26:24 -06:00
//////////////////////////////////////////////////////
// First things first, all the includes we need //
//////////////////////////////////////////////////////
2014-05-20 03:20:19 -06:00
#include <cpu/irq.h> // Interrupt functionality from BertOS
2014-04-06 09:17:13 -06:00
2014-05-20 03:20:19 -06:00
#include <drv/ser.h> // Serial driver from BertOS
#include <drv/timer.h> // Timer driver from BertOS
2014-04-06 09:17:13 -06:00
2014-05-20 03:20:19 -06:00
#include <stdio.h> // Standard input/output
#include <string.h> // String operations
2014-04-03 14:21:37 -06:00
2014-05-29 03:03:25 -06:00
#include <net/ax25.h>
2014-05-20 03:20:19 -06:00
#include "afsk.h" // Header for AFSK modem
2014-04-03 14:21:37 -06:00
2014-04-28 09:47:32 -06:00
#if SERIAL_DEBUG
2014-05-20 03:20:19 -06:00
#include "cfg/debug.h" // Debug configuration from BertOS
2014-04-28 09:47:32 -06:00
#endif
2014-04-14 03:44:39 -06:00
2014-04-06 12:26:24 -06:00
//////////////////////////////////////////////////////
2014-05-20 03:20:19 -06:00
// A few definitions //
2014-04-06 12:26:24 -06:00
//////////////////////////////////////////////////////
2014-05-20 03:20:19 -06:00
static Afsk afsk; // Declare a AFSK modem struct
2014-05-29 03:47:49 -06:00
static AX25Ctx ax25; // Declare a protocol struct
2014-05-20 03:20:19 -06:00
static Serial ser; // Declare a serial interface struct
2014-04-03 14:21:37 -06:00
2014-05-20 03:20:19 -06:00
#define ADC_CH 0 // Define which channel (pin) we want
// for the ADC (this is A0 on arduino)
2014-04-03 14:21:37 -06:00
2014-05-29 03:47:49 -06:00
#define YOUR_CALLSIGN "nocall"
#define TO_CALL "apzmdm"
static AX25Call path[] = AX25_PATH(AX25_CALL(TO_CALL, 0), AX25_CALL(YOUR_CALLSIGN, 0), AX25_CALL("wide1", 1), AX25_CALL("wide2", 2));
#define SEND_TEST_PACKETS true
#define TEST_INTERVAL 15000L
#define APRS_MSG "Test APRS packet"
2014-04-04 03:17:47 -06:00
2014-04-28 09:47:32 -06:00
#define SER_BUFFER_FULL (serialLen < MP1_MAX_DATA_SIZE-1)
2014-04-03 14:21:37 -06:00
2014-04-06 12:26:24 -06:00
//////////////////////////////////////////////////////
// And here comes the actual program :) //
//////////////////////////////////////////////////////
2014-04-06 09:17:13 -06: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-05-29 03:47:49 -06:00
static void message_callback(struct AX25Msg *msg)
{
kfile_printf(&ser.fd, "\n\nSRC[%.6s-%d], DST[%.6s-%d]\r\n", msg->src.call, msg->src.ssid, msg->dst.call, msg->dst.ssid);
for (int i = 0; i < msg->rpt_cnt; i++)
kfile_printf(&ser.fd, "via: [%.6s-%d]\r\n", msg->rpt_lst[i].call, msg->rpt_lst[i].ssid);
kfile_printf(&ser.fd, "DATA: %.*s\r\n", msg->len, msg->info);
2014-04-04 03:17:47 -06:00
}
2014-04-03 16:39:51 -06:00
2014-04-06 09:17:13 -06:00
// Simple initialization function.
2014-04-03 14:21:37 -06:00
static void init(void)
{
2014-05-20 03:20:19 -06:00
// Enable interrupts
IRQ_ENABLE;
2014-04-04 09:05:53 -06:00
2014-04-06 09:17:13 -06:00
// Initialize hardware timers
2014-05-20 03:20:19 -06:00
timer_init();
2014-04-03 14:21:37 -06:00
2014-05-20 03:20:19 -06: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 09:17:13 -06:00
2014-05-20 03:20:19 -06: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 14:49:04 -06:00
2014-05-20 03:20:19 -06:00
// Create a modem context
afsk_init(&afsk, ADC_CH);
// ... and a protocol context with the modem
2014-05-29 03:47:49 -06:00
ax25_init(&ax25, &afsk.fd, message_callback);
2014-04-04 03:17:47 -06:00
2014-05-20 03:20:19 -06:00
// That's all!
2014-04-03 14:21:37 -06:00
}
int main(void)
{
2014-05-20 03:20:19 -06:00
// Start by running the main initialization
init();
// Record the current tick count for time-keeping
ticks_t start = timer_clock();
// Go into ye good ol' infinite loop
while (1)
{
// First we instruct the protocol to check for
// incoming data
2014-05-29 03:47:49 -06:00
ax25_poll(&ax25);
2014-05-20 03:20:19 -06:00
2014-05-29 03:47:49 -06:00
// Use AX.25 to send test data
if (SEND_TEST_PACKETS && timer_clock() - start > ms_to_ticks(TEST_INTERVAL))
{
start = timer_clock();
ax25_sendVia(&ax25, path, countof(path), APRS_MSG, sizeof(APRS_MSG));
2014-05-20 03:20:19 -06:00
}
}
return 0;
2014-04-03 14:41:49 -06:00
}