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-04-06 11:17:13 -04:00
|
|
|
#include <cpu/irq.h> // Interrupt functionality from BertOS
|
2014-04-06 19:15:52 -04:00
|
|
|
#include "cfg/debug.h" // Debug configuration from BertOS
|
2014-04-06 11:17:13 -04:00
|
|
|
|
|
|
|
#include <drv/ser.h> // Serial driver from BertOS
|
|
|
|
#include <drv/timer.h> // Timer driver from BertOS
|
|
|
|
|
|
|
|
#include <stdio.h> // Standard input/output
|
|
|
|
#include <string.h> // String operations
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-04 05:17:47 -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-14 05:44:39 -04:00
|
|
|
|
2014-04-06 14:26:24 -04:00
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// A few definitions //
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
2014-04-06 11:17:13 -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-04-06 11:17:13 -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-06 11:17:13 -04:00
|
|
|
#define TEST_TX false // Whether we should send test packets
|
|
|
|
// periodically, plus what to send:
|
2014-04-14 15:03:22 -04:00
|
|
|
#define TEST_PACKET "Packet received. This is an ACK."
|
2014-04-09 17:33:15 -04:00
|
|
|
#define TEST_TX_INTERVAL 10000L
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-04 05:17:47 -04:00
|
|
|
|
2014-04-06 11:17:13 -04:00
|
|
|
static uint8_t serialBuffer[MP1_MAX_FRAME_LENGTH]; // This is a buffer for incoming serial data
|
|
|
|
static int sbyte; // For holding byte read from serial port
|
|
|
|
static int 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-03 16:21:37 -04:00
|
|
|
|
2014-04-03 18:39:51 -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-04-09 17:33:15 -04:00
|
|
|
kfile_printf(&ser.fd, "%.*s\n", packet->dataLength, packet->data);
|
2014-04-15 14:18:22 -04:00
|
|
|
|
|
|
|
if (packet->data[0]-128 == 'R' && packet->data[1]-128 == 'Q') {
|
|
|
|
timer_delay(1000);
|
|
|
|
mp1Send(&mp1, TEST_PACKET, sizeof(TEST_PACKET));
|
|
|
|
}
|
|
|
|
|
2014-04-09 17:33:15 -04:00
|
|
|
//kprintf("%.*s\n", packet->dataLength, packet->data);
|
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-04-06 11:17:13 -04:00
|
|
|
// Enable interrupts
|
2014-04-03 16:21:37 -04:00
|
|
|
IRQ_ENABLE;
|
2014-04-06 11:17:13 -04:00
|
|
|
// Initialize BertOS debug bridge
|
2014-04-09 17:33:15 -04:00
|
|
|
// kdbg_init();
|
2014-04-04 11:05:53 -04:00
|
|
|
|
2014-04-06 11:17:13 -04:00
|
|
|
// Initialize hardware timers
|
2014-04-03 16:21:37 -04:00
|
|
|
timer_init();
|
|
|
|
|
2014-04-06 11:17:13 -04:00
|
|
|
// Initialize serial comms on UART0,
|
|
|
|
// which is the hardware serial on arduino
|
|
|
|
ser_init(&ser, SER_UART0);
|
2014-04-07 07:28:14 -04:00
|
|
|
ser_setbaudrate(&ser, 115200);
|
2014-04-06 11:17:13 -04:00
|
|
|
|
|
|
|
// Create a modem context
|
2014-04-06 14:29:39 -04:00
|
|
|
afsk_init(&afsk, ADC_CH);
|
2014-04-06 11:17:13 -04:00
|
|
|
// ... and a protocol context with the modem
|
2014-04-04 05:17:47 -04:00
|
|
|
mp1Init(&mp1, &afsk.fd, mp1Callback);
|
|
|
|
|
2014-04-06 11:17:13 -04:00
|
|
|
// That's all!
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2014-04-06 11:17:13 -04:00
|
|
|
// Start by running the main initialization
|
2014-04-03 16:21:37 -04:00
|
|
|
init();
|
2014-04-06 11:17:13 -04:00
|
|
|
// Record the current tick count for time-keeping
|
2014-04-03 18:39:51 -04:00
|
|
|
ticks_t start = timer_clock();
|
2014-04-04 11:05:53 -04:00
|
|
|
|
2014-04-06 11:17:13 -04:00
|
|
|
// Go into ye good ol' infinite loop
|
2014-04-03 16:21:37 -04:00
|
|
|
while (1)
|
2014-04-10 06:52:00 -04:00
|
|
|
{
|
2014-04-06 11:17:13 -04:00
|
|
|
// First we instruct the protocol to check for
|
|
|
|
// incoming data
|
2014-04-04 05:17:47 -04:00
|
|
|
mp1Poll(&mp1);
|
2014-04-03 18:39:51 -04:00
|
|
|
|
2014-04-07 16:06:33 -04:00
|
|
|
|
2014-04-06 11:17:13 -04:00
|
|
|
// 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.
|
2014-04-04 11:05:53 -04:00
|
|
|
sbyte = ser_getchar_nowait(&ser);
|
2014-04-06 11:17:13 -04:00
|
|
|
// If there was actually some data waiting for us
|
|
|
|
// there, let's se what it tastes like :)
|
2014-04-04 11:05:53 -04:00
|
|
|
if (sbyte != EOF) {
|
2014-04-06 11:17:13 -04:00
|
|
|
// 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_FRAME_LENGTH) && (sbyte != 138)) {
|
|
|
|
// Put the read byte into the buffer;
|
2014-04-04 11:05:53 -04:00
|
|
|
serialBuffer[serialLen] = sbyte;
|
2014-04-06 11:17:13 -04:00
|
|
|
// Increment the read length counter
|
2014-04-04 11:05:53 -04:00
|
|
|
serialLen++;
|
|
|
|
} else {
|
2014-04-06 11:17:13 -04:00
|
|
|
// If one of the above conditions were actually the
|
|
|
|
// case, it means we have to transmit, se we set
|
|
|
|
// transmission flag to true.
|
2014-04-15 14:18:22 -04:00
|
|
|
serialBuffer[serialLen] = sbyte;
|
|
|
|
serialLen++;
|
2014-04-04 11:05:53 -04:00
|
|
|
sertx = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-06 11:17:13 -04:00
|
|
|
// Check whether we should send data in our serial buffer
|
2014-04-04 11:05:53 -04:00
|
|
|
if (sertx) {
|
2014-04-06 11:17:13 -04:00
|
|
|
// If we should, pass the buffer to the protocol's
|
|
|
|
// send function.
|
2014-04-14 11:38:35 -04:00
|
|
|
|
2014-04-14 15:03:22 -04:00
|
|
|
mp1Send(&mp1, serialBuffer, serialLen);
|
|
|
|
|
2014-04-06 11:17:13 -04:00
|
|
|
// Reset the transmission flag and length counter
|
2014-04-04 11:05:53 -04:00
|
|
|
sertx = false;
|
|
|
|
serialLen = 0;
|
|
|
|
}
|
|
|
|
|
2014-04-06 11:17:13 -04:00
|
|
|
// Periodically send test data if we should do so
|
2014-04-09 17:33:15 -04:00
|
|
|
if (TEST_TX && timer_clock() - start > ms_to_ticks(TEST_TX_INTERVAL)) {
|
2014-04-06 11:17:13 -04:00
|
|
|
// Reset the timer counter;
|
2014-04-03 18:39:51 -04:00
|
|
|
start = timer_clock();
|
2014-04-06 11:17:13 -04:00
|
|
|
// And send a test packet!
|
2014-04-04 05:40:43 -04:00
|
|
|
mp1Send(&mp1, TEST_PACKET, sizeof(TEST_PACKET));
|
2014-04-03 18:39:51 -04:00
|
|
|
}
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
return 0;
|
2014-04-03 16:41:49 -04:00
|
|
|
}
|