Initial commit
commit
9b988a51a3
|
@ -0,0 +1,80 @@
|
|||
#include <stddef.h>
|
||||
#include <util/atomic.h>
|
||||
|
||||
typedef struct FIFOBuffer
|
||||
{
|
||||
unsigned char *begin;
|
||||
unsigned char *end;
|
||||
unsigned char * volatile head;
|
||||
unsigned char * volatile tail;
|
||||
} FIFOBuffer;
|
||||
|
||||
inline bool fifo_isempty(const FIFOBuffer *f) {
|
||||
return f->head == f->tail;
|
||||
}
|
||||
|
||||
inline bool fifo_isfull(const FIFOBuffer *f) {
|
||||
return ((f->head == f->begin) && (f->tail == f->end)) || (f->tail == f->head - 1);
|
||||
}
|
||||
|
||||
inline void fifo_push(FIFOBuffer *f, unsigned char c) {
|
||||
*(f->tail) = c;
|
||||
|
||||
if (f->tail == f->end) {
|
||||
f->tail = f->begin;
|
||||
} else {
|
||||
f->tail++;
|
||||
}
|
||||
}
|
||||
|
||||
inline unsigned char fifo_pop(FIFOBuffer *f) {
|
||||
if(f->head == f->end) {
|
||||
f->head = f->begin;
|
||||
return *(f->end);
|
||||
} else {
|
||||
return *(f->head++);
|
||||
}
|
||||
}
|
||||
|
||||
inline void fifo_flush(FIFOBuffer *f) {
|
||||
f->head = f->tail;
|
||||
}
|
||||
|
||||
inline bool fifo_isempty_locked(const FIFOBuffer *f) {
|
||||
bool result;
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
result = fifo_isempty(f);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
inline bool fifo_isfull_locked(const FIFOBuffer *f) {
|
||||
bool result;
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
result = fifo_isfull(f);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
inline void fifo_push_locked(FIFOBuffer *f, unsigned char c) {
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
fifo_push(f, c);
|
||||
}
|
||||
}
|
||||
|
||||
inline unsigned char fifo_pop_locked(FIFOBuffer *f) {
|
||||
unsigned char c;
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
c = fifo_pop(f);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
inline void fifo_init(FIFOBuffer *f, unsigned char *buffer, size_t size) {
|
||||
f->head = f->tail = f->begin = buffer;
|
||||
f->end = buffer + size -1;
|
||||
}
|
||||
|
||||
inline size_t fifo_len(FIFOBuffer *f) {
|
||||
return f->end - f->begin;
|
||||
}
|
|
@ -0,0 +1,476 @@
|
|||
#include "Arduino.h"
|
||||
#include "TelemetryKit.h"
|
||||
|
||||
Afsk modem;
|
||||
bool hw_afsk_dac_isr = false;
|
||||
|
||||
void tk_hwInit() {
|
||||
|
||||
}
|
||||
|
||||
void tk_afsk_init(Afsk *afsk) {
|
||||
memset(afsk, 0, sizeof(*afsk));
|
||||
afsk->phaseInc = MARK_INC;
|
||||
fifo_init(&afsk->delayFifo, (uint8_t *)afsk->delayBuf, sizeof(afsk->delayBuf));
|
||||
fifo_init(&afsk->rxFifo, afsk->rxBuf, sizeof(afsk->rxBuf));
|
||||
fifo_init(&afsk->txFifo, afsk->txBuf, sizeof(afsk->txBuf));
|
||||
}
|
||||
|
||||
void TelemetryKitInitTxOnly() {
|
||||
tk_afsk_init(&modem);
|
||||
cli();
|
||||
|
||||
// Set up Timer1 to 9600Hz interrupt
|
||||
TCCR1A = 0;
|
||||
TCCR1B = _BV(CS10) | _BV(WGM12);
|
||||
OCR1A = 1666;
|
||||
TIMSK1 |= _BV(OCIE1A);
|
||||
|
||||
sei();
|
||||
AFSK_DAC_INIT();
|
||||
|
||||
}
|
||||
|
||||
void TelemetryKitInit() {
|
||||
tk_afsk_init(&modem);
|
||||
cli();
|
||||
|
||||
TCCR1A = 0;
|
||||
TCCR1B = _BV(CS10) | _BV(WGM13) | _BV(WGM12);
|
||||
ICR1 = (((16000000+FREQUENCY_CORRECTION)) / 9600) - 1;
|
||||
ADMUX = _BV(REFS0) | 0;
|
||||
DDRC &= ~_BV(0);
|
||||
PORTC &= ~_BV(0);
|
||||
DIDR0 |= _BV(0);
|
||||
ADCSRB = _BV(ADTS2) |
|
||||
_BV(ADTS1) |
|
||||
_BV(ADTS0);
|
||||
ADCSRA = _BV(ADEN) |
|
||||
_BV(ADSC) |
|
||||
_BV(ADATE)|
|
||||
_BV(ADIE) |
|
||||
_BV(ADPS2);
|
||||
sei();
|
||||
AFSK_DAC_INIT();
|
||||
}
|
||||
|
||||
#define MIN_FRAME_LEN 2
|
||||
#define FRAME_LEN 256
|
||||
size_t readlength = 0;
|
||||
char frameBuffer[FRAME_LEN];
|
||||
bool tk_escape = false;
|
||||
bool tk_sync = false;
|
||||
void TelemetryKitPoll() {
|
||||
extern void telemetryKitCallback(char *packet, size_t length);
|
||||
int byte;
|
||||
while (!fifo_isempty_locked(&modem.rxFifo) && (byte = fifo_pop_locked(&modem.rxFifo))) {
|
||||
if (!tk_escape && byte == HDLC_FLAG) {
|
||||
if (readlength >= MIN_FRAME_LEN) {
|
||||
|
||||
// Frame received, no validation for now,
|
||||
// just forward to handler
|
||||
telemetryKitCallback(frameBuffer, readlength);
|
||||
}
|
||||
tk_sync = true;
|
||||
readlength = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!tk_escape && byte == HDLC_RESET) {
|
||||
tk_sync = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!tk_escape && byte == AX25_ESC) {
|
||||
tk_escape = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tk_sync) {
|
||||
if (readlength < FRAME_LEN) {
|
||||
frameBuffer[readlength++] = byte;
|
||||
} else {
|
||||
Serial.println("ERROR: RX Buffer overrun!");
|
||||
tk_sync = false;
|
||||
}
|
||||
}
|
||||
tk_escape = false;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static void tk_afsk_txStart(Afsk *afsk) {
|
||||
if (!afsk->sending) {
|
||||
afsk->phaseInc = MARK_INC;
|
||||
afsk->phaseAcc = 0;
|
||||
afsk->bitstuffCount = 0;
|
||||
afsk->sending = true;
|
||||
afsk->preambleLength = DIV_ROUND(CONFIG_AFSK_PREAMBLE_LEN * BITRATE, 8000);
|
||||
AFSK_DAC_IRQ_START();
|
||||
}
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
afsk->tailLength = DIV_ROUND(CONFIG_AFSK_TRAILER_LEN * BITRATE, 8000);
|
||||
}
|
||||
}
|
||||
|
||||
void TelemetryKitTransmit(char *buffer, size_t size) {
|
||||
fifo_flush(&modem.txFifo);
|
||||
tk_afsk_txStart(&modem);
|
||||
for (int i = 0; i < size; i++) {
|
||||
while (fifo_isfull_locked(&modem.txFifo)) { /* Wait */ }
|
||||
fifo_push(&modem.txFifo, buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t tk_afsk_dac_isr(Afsk *afsk) {
|
||||
if (afsk->sampleIndex == 0) {
|
||||
if (afsk->txBit == 0) {
|
||||
if (fifo_isempty(&afsk->txFifo) && afsk->tailLength == 0) {
|
||||
AFSK_DAC_IRQ_STOP();
|
||||
afsk->sending = false;
|
||||
return 0;
|
||||
} else {
|
||||
if (!afsk->bitStuff) afsk->bitstuffCount = 0;
|
||||
afsk->bitStuff = true;
|
||||
if (afsk->preambleLength == 0) {
|
||||
if (fifo_isempty(&afsk->txFifo)) {
|
||||
afsk->tailLength--;
|
||||
afsk->currentOutputByte = HDLC_FLAG;
|
||||
} else {
|
||||
afsk->currentOutputByte = fifo_pop(&afsk->txFifo);
|
||||
}
|
||||
} else {
|
||||
afsk->preambleLength--;
|
||||
afsk->currentOutputByte = HDLC_FLAG;
|
||||
}
|
||||
if (afsk->currentOutputByte == AX25_ESC) {
|
||||
if (fifo_isempty(&afsk->txFifo)) {
|
||||
AFSK_DAC_IRQ_STOP();
|
||||
afsk->sending = false;
|
||||
return 0;
|
||||
} else {
|
||||
afsk->currentOutputByte = fifo_pop(&afsk->txFifo);
|
||||
}
|
||||
} else if (afsk->currentOutputByte == HDLC_FLAG || afsk->currentOutputByte == HDLC_RESET) {
|
||||
afsk->bitStuff = false;
|
||||
}
|
||||
}
|
||||
afsk->txBit = 0x01;
|
||||
}
|
||||
|
||||
if (afsk->bitStuff && afsk->bitstuffCount >= BIT_STUFF_LEN) {
|
||||
afsk->bitstuffCount = 0;
|
||||
afsk->phaseInc = SWITCH_TONE(afsk->phaseInc);
|
||||
} else {
|
||||
if (afsk->currentOutputByte & afsk->txBit) {
|
||||
afsk->bitstuffCount++;
|
||||
} else {
|
||||
afsk->bitstuffCount = 0;
|
||||
afsk->phaseInc = SWITCH_TONE(afsk->phaseInc);
|
||||
}
|
||||
afsk->txBit <<= 1;
|
||||
}
|
||||
|
||||
afsk->sampleIndex = SAMPLESPERBIT;
|
||||
}
|
||||
|
||||
afsk->phaseAcc += afsk->phaseInc;
|
||||
afsk->phaseAcc %= SIN_LEN;
|
||||
afsk->sampleIndex--;
|
||||
|
||||
return sinSample(afsk->phaseAcc);
|
||||
}
|
||||
|
||||
static bool hdlcParse(Hdlc *hdlc, bool bit, FIFOBuffer *fifo) {
|
||||
// Initialise a return value. We start with the
|
||||
// assumption that all is going to end well :)
|
||||
bool ret = true;
|
||||
|
||||
// Bitshift our byte of demodulated bits to
|
||||
// the left by one bit, to make room for the
|
||||
// next incoming bit
|
||||
hdlc->demodulatedBits <<= 1;
|
||||
// And then put the newest bit from the
|
||||
// demodulator into the byte.
|
||||
hdlc->demodulatedBits |= bit ? 1 : 0;
|
||||
|
||||
// Now we'll look at the last 8 received bits, and
|
||||
// check if we have received a HDLC flag (01111110)
|
||||
if (hdlc->demodulatedBits == HDLC_FLAG) {
|
||||
// If we have, check that our output buffer is
|
||||
// not full.
|
||||
if (!fifo_isfull(fifo)) {
|
||||
// If it isn't, we'll push the HDLC_FLAG into
|
||||
// the buffer and indicate that we are now
|
||||
// receiving data. For bling we also turn
|
||||
// on the RX LED.
|
||||
fifo_push(fifo, HDLC_FLAG);
|
||||
hdlc->receiving = true;
|
||||
} else {
|
||||
// If the buffer is full, we have a problem
|
||||
// and abort by setting the return value to
|
||||
// false and stopping the here.
|
||||
ret = false;
|
||||
hdlc->receiving = false;
|
||||
digitalWrite(13, HIGH);
|
||||
}
|
||||
|
||||
// Everytime we receive a HDLC_FLAG, we reset the
|
||||
// storage for our current incoming byte and bit
|
||||
// position in that byte. This effectively
|
||||
// synchronises our parsing to the start and end
|
||||
// of the received bytes.
|
||||
hdlc->currentByte = 0;
|
||||
hdlc->bitIndex = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Check if we have received a RESET flag (01111111)
|
||||
// In this comparison we also detect when no transmission
|
||||
// (or silence) is taking place, and the demodulator
|
||||
// returns an endless stream of zeroes. Due to the NRZ
|
||||
// coding, the actual bits send to this function will
|
||||
// be an endless stream of ones, which this AND operation
|
||||
// will also detect.
|
||||
if ((hdlc->demodulatedBits & HDLC_RESET) == HDLC_RESET) {
|
||||
// If we have, something probably went wrong at the
|
||||
// transmitting end, and we abort the reception.
|
||||
hdlc->receiving = false;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// If we have not yet seen a HDLC_FLAG indicating that
|
||||
// a transmission is actually taking place, don't bother
|
||||
// with anything.
|
||||
if (!hdlc->receiving)
|
||||
return ret;
|
||||
|
||||
// First check if what we are seeing is a stuffed bit.
|
||||
// Since the different HDLC control characters like
|
||||
// HDLC_FLAG, HDLC_RESET and such could also occur in
|
||||
// a normal data stream, we employ a method known as
|
||||
// "bit stuffing". All control characters have more than
|
||||
// 5 ones in a row, so if the transmitting party detects
|
||||
// this sequence in the _data_ to be transmitted, it inserts
|
||||
// a zero to avoid the receiving party interpreting it as
|
||||
// a control character. Therefore, if we detect such a
|
||||
// "stuffed bit", we simply ignore it and wait for the
|
||||
// next bit to come in.
|
||||
//
|
||||
// We do the detection by applying an AND bit-mask to the
|
||||
// stream of demodulated bits. This mask is 00111111 (0x3f)
|
||||
// if the result of the operation is 00111110 (0x3e), we
|
||||
// have detected a stuffed bit.
|
||||
if ((hdlc->demodulatedBits & 0x3f) == 0x3e)
|
||||
return ret;
|
||||
|
||||
// If we have an actual 1 bit, push this to the current byte
|
||||
// If it's a zero, we don't need to do anything, since the
|
||||
// bit is initialized to zero when we bitshifted earlier.
|
||||
if (hdlc->demodulatedBits & 0x01)
|
||||
hdlc->currentByte |= 0x80;
|
||||
|
||||
// Increment the bitIndex and check if we have a complete byte
|
||||
if (++hdlc->bitIndex >= 8) {
|
||||
// If we have a HDLC control character, put a AX.25 escape
|
||||
// in the received data. We know we need to do this,
|
||||
// because at this point we must have already seen a HDLC
|
||||
// flag, meaning that this control character is the result
|
||||
// of a bitstuffed byte that is equal to said control
|
||||
// character, but is actually part of the data stream.
|
||||
// By inserting the escape character, we tell the protocol
|
||||
// layer that this is not an actual control character, but
|
||||
// data.
|
||||
if ((hdlc->currentByte == HDLC_FLAG ||
|
||||
hdlc->currentByte == HDLC_RESET ||
|
||||
hdlc->currentByte == AX25_ESC)) {
|
||||
// We also need to check that our received data buffer
|
||||
// is not full before putting more data in
|
||||
if (!fifo_isfull(fifo)) {
|
||||
fifo_push(fifo, AX25_ESC);
|
||||
} else {
|
||||
// If it is, abort and return false
|
||||
hdlc->receiving = false;
|
||||
digitalWrite(13, HIGH);
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Push the actual byte to the received data FIFO,
|
||||
// if it isn't full.
|
||||
if (!fifo_isfull(fifo)) {
|
||||
fifo_push(fifo, hdlc->currentByte);
|
||||
} else {
|
||||
// If it is, well, you know by now!
|
||||
hdlc->receiving = false;
|
||||
digitalWrite(13, HIGH);
|
||||
ret = false;
|
||||
}
|
||||
|
||||
// Wipe received byte and reset bit index to 0
|
||||
hdlc->currentByte = 0;
|
||||
hdlc->bitIndex = 0;
|
||||
|
||||
} else {
|
||||
// We don't have a full byte yet, bitshift the byte
|
||||
// to make room for the next bit
|
||||
hdlc->currentByte >>= 1;
|
||||
}
|
||||
|
||||
digitalWrite(13, LOW);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void tk_afsk_adc_isr(Afsk *afsk, int8_t currentSample) {
|
||||
// To determine the received frequency, and thereby
|
||||
// the bit of the sample, we multiply the sample by
|
||||
// a sample delayed by (samples per bit / 2).
|
||||
// We then lowpass-filter the samples with a
|
||||
// Chebyshev filter. The lowpass filtering serves
|
||||
// to "smooth out" the variations in the samples.
|
||||
|
||||
afsk->iirX[0] = afsk->iirX[1];
|
||||
afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) >> 2;
|
||||
|
||||
afsk->iirY[0] = afsk->iirY[1];
|
||||
|
||||
afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] >> 1); // Chebyshev filter
|
||||
|
||||
|
||||
// We put the sampled bit in a delay-line:
|
||||
// First we bitshift everything 1 left
|
||||
afsk->sampledBits <<= 1;
|
||||
// And then add the sampled bit to our delay line
|
||||
afsk->sampledBits |= (afsk->iirY[1] > 0) ? 1 : 0;
|
||||
|
||||
// Put the current raw sample in the delay FIFO
|
||||
fifo_push(&afsk->delayFifo, currentSample);
|
||||
|
||||
// We need to check whether there is a signal transition.
|
||||
// If there is, we can recalibrate the phase of our
|
||||
// sampler to stay in sync with the transmitter. A bit of
|
||||
// explanation is required to understand how this works.
|
||||
// Since we have PHASE_MAX/PHASE_BITS = 8 samples per bit,
|
||||
// we employ a phase counter (currentPhase), that increments
|
||||
// by PHASE_BITS everytime a sample is captured. When this
|
||||
// counter reaches PHASE_MAX, it wraps around by modulus
|
||||
// PHASE_MAX. We then look at the last three samples we
|
||||
// captured and determine if the bit was a one or a zero.
|
||||
//
|
||||
// This gives us a "window" looking into the stream of
|
||||
// samples coming from the ADC. Sort of like this:
|
||||
//
|
||||
// Past Future
|
||||
// 0000000011111111000000001111111100000000
|
||||
// |________|
|
||||
// ||
|
||||
// Window
|
||||
//
|
||||
// Every time we detect a signal transition, we adjust
|
||||
// where this window is positioned little. How much we
|
||||
// adjust it is defined by PHASE_INC. If our current phase
|
||||
// phase counter value is less than half of PHASE_MAX (ie,
|
||||
// the window size) when a signal transition is detected,
|
||||
// add PHASE_INC to our phase counter, effectively moving
|
||||
// the window a little bit backward (to the left in the
|
||||
// illustration), inversely, if the phase counter is greater
|
||||
// than half of PHASE_MAX, we move it forward a little.
|
||||
// This way, our "window" is constantly seeking to position
|
||||
// it's center at the bit transitions. Thus, we synchronise
|
||||
// our timing to the transmitter, even if it's timing is
|
||||
// a little off compared to our own.
|
||||
if (SIGNAL_TRANSITIONED(afsk->sampledBits)) {
|
||||
if (afsk->currentPhase < PHASE_THRESHOLD) {
|
||||
afsk->currentPhase += PHASE_INC;
|
||||
} else {
|
||||
afsk->currentPhase -= PHASE_INC;
|
||||
}
|
||||
}
|
||||
|
||||
// We increment our phase counter
|
||||
afsk->currentPhase += PHASE_BITS;
|
||||
|
||||
// Check if we have reached the end of
|
||||
// our sampling window.
|
||||
if (afsk->currentPhase >= PHASE_MAX) {
|
||||
// If we have, wrap around our phase
|
||||
// counter by modulus
|
||||
afsk->currentPhase %= PHASE_MAX;
|
||||
|
||||
// Bitshift to make room for the next
|
||||
// bit in our stream of demodulated bits
|
||||
afsk->actualBits <<= 1;
|
||||
|
||||
// We determine the actual bit value by reading
|
||||
// the last 3 sampled bits. If there is three or
|
||||
// more 1's, we will assume that the transmitter
|
||||
// sent us a one, otherwise we assume a zero
|
||||
uint8_t bits = afsk->sampledBits & 0x07;
|
||||
if (bits == 0x07 || // 111
|
||||
bits == 0x06 || // 110
|
||||
bits == 0x05 || // 101
|
||||
bits == 0x03 // 011
|
||||
) {
|
||||
afsk->actualBits |= 1;
|
||||
}
|
||||
|
||||
//// Alternative using five bits ////////////////
|
||||
// uint8_t bits = afsk->sampledBits & 0x0f;
|
||||
// uint8_t c = 0;
|
||||
// c += bits & BV(1);
|
||||
// c += bits & BV(2);
|
||||
// c += bits & BV(3);
|
||||
// c += bits & BV(4);
|
||||
// c += bits & BV(5);
|
||||
// if (c >= 3) afsk->actualBits |= 1;
|
||||
/////////////////////////////////////////////////
|
||||
|
||||
// Now we can pass the actual bit to the HDLC parser.
|
||||
// We are using NRZ coding, so if 2 consecutive bits
|
||||
// have the same value, we have a 1, otherwise a 0.
|
||||
// We use the TRANSITION_FOUND function to determine this.
|
||||
//
|
||||
// This is smart in combination with bit stuffing,
|
||||
// since it ensures a transmitter will never send more
|
||||
// than five consecutive 1's. When sending consecutive
|
||||
// ones, the signal stays at the same level, and if
|
||||
// this happens for longer periods of time, we would
|
||||
// not be able to synchronize our phase to the transmitter
|
||||
// and would start experiencing "bit slip".
|
||||
//
|
||||
// By combining bit-stuffing with NRZ coding, we ensure
|
||||
// that the signal will regularly make transitions
|
||||
// that we can use to synchronize our phase.
|
||||
//
|
||||
// We also check the return of the Link Control parser
|
||||
// to check if an error occured.
|
||||
|
||||
if (!hdlcParse(&afsk->hdlc, !TRANSITION_FOUND(afsk->actualBits), &afsk->rxFifo)) {
|
||||
afsk->status |= 1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ISR(TIMER1_COMPA_vect) {
|
||||
if (hw_afsk_dac_isr) {
|
||||
PORTD = (tk_afsk_dac_isr(&modem) & 0xF0);
|
||||
digitalWrite(13, HIGH);
|
||||
digitalWrite(13, LOW);
|
||||
} else {
|
||||
PORTD = 128;
|
||||
}
|
||||
}
|
||||
|
||||
ISR(ADC_vect) {
|
||||
TIFR1 = _BV(ICF1);
|
||||
tk_afsk_adc_isr(&modem, ((int16_t)((ADC) >> 2) - 128));
|
||||
if (hw_afsk_dac_isr) {
|
||||
PORTD = (tk_afsk_dac_isr(&modem) & 0xF0);
|
||||
} else {
|
||||
PORTD = 128;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,122 @@
|
|||
#ifndef TelemetryKit_h
|
||||
#define TelemetryKit_h
|
||||
#include "Arduino.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <avr/pgmspace.h>
|
||||
#include "FIFO.h"
|
||||
|
||||
#define HDLC_FLAG 0x7E
|
||||
#define HDLC_RESET 0x7F
|
||||
#define AX25_ESC 0x1B
|
||||
|
||||
#define SIN_LEN 512
|
||||
static const uint8_t sin_table[] PROGMEM =
|
||||
{
|
||||
128, 129, 131, 132, 134, 135, 137, 138, 140, 142, 143, 145, 146, 148, 149, 151,
|
||||
152, 154, 155, 157, 158, 160, 162, 163, 165, 166, 167, 169, 170, 172, 173, 175,
|
||||
176, 178, 179, 181, 182, 183, 185, 186, 188, 189, 190, 192, 193, 194, 196, 197,
|
||||
198, 200, 201, 202, 203, 205, 206, 207, 208, 210, 211, 212, 213, 214, 215, 217,
|
||||
218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
|
||||
234, 234, 235, 236, 237, 238, 238, 239, 240, 241, 241, 242, 243, 243, 244, 245,
|
||||
245, 246, 246, 247, 248, 248, 249, 249, 250, 250, 250, 251, 251, 252, 252, 252,
|
||||
253, 253, 253, 253, 254, 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255,
|
||||
};
|
||||
|
||||
inline uint8_t sinSample(uint16_t i) {
|
||||
uint16_t newI = i % (SIN_LEN/2);
|
||||
newI = (newI >= (SIN_LEN/4)) ? (SIN_LEN/2 - newI -1) : newI;
|
||||
uint8_t sine = pgm_read_byte(&sin_table[newI]);
|
||||
return (i >= (SIN_LEN/2)) ? (255 - sine) : sine;
|
||||
}
|
||||
|
||||
|
||||
#define SWITCH_TONE(inc) (((inc) == MARK_INC) ? SPACE_INC : MARK_INC)
|
||||
#define BITS_DIFFER(bits1, bits2) (((bits1)^(bits2)) & 0x01)
|
||||
#define DUAL_XOR(bits1, bits2) ((((bits1)^(bits2)) & 0x03) == 0x03)
|
||||
#define SIGNAL_TRANSITIONED(bits) DUAL_XOR((bits), (bits) >> 2)
|
||||
#define TRANSITION_FOUND(bits) BITS_DIFFER((bits), (bits) >> 1)
|
||||
|
||||
#define FREQUENCY_CORRECTION 0
|
||||
#define CONFIG_AFSK_RX_BUFLEN 64
|
||||
#define CONFIG_AFSK_TX_BUFLEN 64
|
||||
#define CONFIG_AFSK_DAC_SAMPLERATE 9600
|
||||
#define CONFIG_AFSK_RXTIMEOUT 0
|
||||
#define CONFIG_AFSK_PREAMBLE_LEN 150UL
|
||||
#define CONFIG_AFSK_TRAILER_LEN 5UL
|
||||
#define SAMPLERATE 9600
|
||||
#define BITRATE 1200
|
||||
#define SAMPLESPERBIT (SAMPLERATE / BITRATE)
|
||||
#define BIT_STUFF_LEN 5
|
||||
#define MARK_FREQ 1200
|
||||
#define SPACE_FREQ 2200
|
||||
#define PHASE_BITS 8 // How much to increment phase counter each sample
|
||||
#define PHASE_INC 1 // Nudge by an eigth of a sample each adjustment
|
||||
#define PHASE_MAX (SAMPLESPERBIT * PHASE_BITS) // Resolution of our phase counter = 64
|
||||
#define PHASE_THRESHOLD (PHASE_MAX / 2) // Target transition point of our phase window
|
||||
|
||||
|
||||
typedef struct Hdlc
|
||||
{
|
||||
uint8_t demodulatedBits;
|
||||
uint8_t bitIndex;
|
||||
uint8_t currentByte;
|
||||
bool receiving;
|
||||
} Hdlc;
|
||||
|
||||
typedef struct Afsk
|
||||
{
|
||||
// General values
|
||||
Hdlc hdlc; // We need a link control structure
|
||||
uint16_t preambleLength; // Length of sync preamble
|
||||
uint16_t tailLength; // Length of transmission tail
|
||||
|
||||
// Modulation values
|
||||
uint8_t sampleIndex; // Current sample index for outgoing bit
|
||||
uint8_t currentOutputByte; // Current byte to be modulated
|
||||
uint8_t txBit; // Mask of current modulated bit
|
||||
bool bitStuff; // Whether bitstuffing is allowed
|
||||
|
||||
uint8_t bitstuffCount; // Counter for bit-stuffing
|
||||
|
||||
uint16_t phaseAcc; // Phase accumulator
|
||||
uint16_t phaseInc; // Phase increment per sample
|
||||
|
||||
FIFOBuffer txFifo; // FIFO for transmit data
|
||||
uint8_t txBuf[CONFIG_AFSK_TX_BUFLEN]; // Actial data storage for said FIFO
|
||||
|
||||
volatile bool sending; // Set when modem is sending
|
||||
|
||||
// Demodulation values
|
||||
FIFOBuffer delayFifo; // Delayed FIFO for frequency discrimination
|
||||
int8_t delayBuf[SAMPLESPERBIT / 2 + 1]; // Actual data storage for said FIFO
|
||||
|
||||
FIFOBuffer rxFifo; // FIFO for received data
|
||||
uint8_t rxBuf[CONFIG_AFSK_RX_BUFLEN]; // Actual data storage for said FIFO
|
||||
|
||||
int16_t iirX[2]; // IIR Filter X cells
|
||||
int16_t iirY[2]; // IIR Filter Y cells
|
||||
|
||||
uint8_t sampledBits; // Bits sampled by the demodulator (at ADC speed)
|
||||
int8_t currentPhase; // Current phase of the demodulator
|
||||
uint8_t actualBits; // Actual found bits at correct bitrate
|
||||
|
||||
volatile int status; // Status of the modem, 0 means OK
|
||||
|
||||
} Afsk;
|
||||
|
||||
#define DIV_ROUND(dividend, divisor) (((dividend) + (divisor) / 2) / (divisor))
|
||||
#define MARK_INC (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)MARK_FREQ, CONFIG_AFSK_DAC_SAMPLERATE))
|
||||
#define SPACE_INC (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)SPACE_FREQ, CONFIG_AFSK_DAC_SAMPLERATE))
|
||||
|
||||
#define AFSK_DAC_IRQ_START() do { extern bool hw_afsk_dac_isr; hw_afsk_dac_isr = true; } while (0)
|
||||
#define AFSK_DAC_IRQ_STOP() do { extern bool hw_afsk_dac_isr; hw_afsk_dac_isr = false; } while (0)
|
||||
#define AFSK_DAC_INIT() do { DDRD |= 0xF0; } while (0)
|
||||
|
||||
void TelemetryKitInitTxOnly();
|
||||
void TelemetryKitInit();
|
||||
void TelemetryKitTransmit(char *buffer, size_t size);
|
||||
void TelemetryKitPoll();
|
||||
|
||||
#endif
|
|
@ -0,0 +1,19 @@
|
|||
#include <TelemetryKit.h>
|
||||
|
||||
void telemetryKitCallback(char *packet, size_t length) {
|
||||
Serial.print("Received packet: ");
|
||||
Serial.println(packet);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
extern Afsk modem;
|
||||
Serial.begin(9600);
|
||||
|
||||
TelemetryKitInit();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
while (true) {
|
||||
TelemetryKitPoll();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
#include <TelemetryKit.h>
|
||||
|
||||
void telemetryKitCallback(char *packet, size_t length) {
|
||||
Serial.print("Received packet: ");
|
||||
Serial.println(packet);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
extern Afsk modem;
|
||||
Serial.begin(9600);
|
||||
|
||||
TelemetryKitInit();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
char msg[] = "Hello world! This is a string to be transmitted.\n";
|
||||
TelemetryKitTransmit(msg, sizeof(msg));
|
||||
|
||||
while (true) {
|
||||
TelemetryKitPoll();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
#include <TelemetryKit.h>
|
||||
|
||||
void telemetryKitCallback(char *packet, size_t length) {
|
||||
Serial.print("Received packet: ");
|
||||
Serial.println(packet);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
extern Afsk modem;
|
||||
Serial.begin(9600);
|
||||
|
||||
TelemetryKitInit();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
char msg[] = "Hello world! This is a longer string...\n";
|
||||
TelemetryKitTransmit(msg, sizeof(msg));
|
||||
|
||||
while (true) {
|
||||
TelemetryKitPoll();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
#include <TelemetryKit.h>
|
||||
|
||||
void telemetryKitCallback(char *packet, size_t length) {
|
||||
Serial.print("Received packet: ");
|
||||
Serial.println(packet);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
extern Afsk modem;
|
||||
Serial.begin(9600);
|
||||
|
||||
TelemetryKitInit();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
while (true) {
|
||||
char msg[] = "Hello world! This is a string to be transmitted.\n";
|
||||
TelemetryKitTransmit(msg, sizeof(msg));
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue