2014-04-03 16:21:37 -04:00
|
|
|
#include "afsk.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "hardware.h"
|
|
|
|
|
|
|
|
#include <drv/timer.h>
|
|
|
|
#include <cfg/module.h>
|
|
|
|
|
|
|
|
#define HDLC_FLAG 0x7E
|
|
|
|
#define HDLC_RESET 0x7F
|
|
|
|
#define AX25_ESC 0x1B
|
|
|
|
|
|
|
|
#include <cfg/log.h>
|
|
|
|
|
|
|
|
#include <cpu/power.h>
|
|
|
|
#include <cpu/pgm.h>
|
|
|
|
#include <struct/fifobuf.h>
|
|
|
|
|
|
|
|
#include <string.h> /* memset */
|
|
|
|
|
2014-04-03 17:02:57 -04:00
|
|
|
// Sine table for DAC DDS
|
|
|
|
#define SIN_LEN 512 // Length of a full wave. Table is 1/4 wave.
|
2014-04-03 16:21:37 -04:00
|
|
|
static const uint8_t PROGMEM sin_table[] =
|
|
|
|
{
|
|
|
|
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,
|
2014-04-03 17:02:57 -04:00
|
|
|
}; STATIC_ASSERT(sizeof(sin_table) == SIN_LEN / 4);
|
2014-04-03 16:21:37 -04:00
|
|
|
|
|
|
|
|
2014-04-03 17:02:57 -04:00
|
|
|
// Calculate Sine value from quarter sine table
|
|
|
|
INLINE uint8_t sinSample(uint16_t i) {
|
|
|
|
ASSERT(i < SIN_LEN);
|
|
|
|
uint16_t newI = i % (SIN_LEN/2);
|
|
|
|
newI = (newI >= (SIN_LEN/4)) ? (SIN_LEN/2 - newI -1) : newI;
|
|
|
|
uint8_t sine = pgm_read8(&sin_table[newI]);
|
|
|
|
return (i >= (SIN_LEN/2)) ? (255 - sine) : sine;
|
|
|
|
}
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-03 17:02:57 -04:00
|
|
|
// Look for signal transition. Used for phase sync.
|
|
|
|
#define BITS_DIFFER(bits1, bits2) (((bits1)^(bits2)) & 0x01)
|
|
|
|
#define EDGE_FOUND(bits) BITS_DIFFER((bits), (bits) >> 1)
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-03 17:02:57 -04:00
|
|
|
// Phase sync constants
|
|
|
|
#define PHASE_BIT 8
|
|
|
|
#define PHASE_INC 1
|
|
|
|
#define PHASE_MAX (SAMPLESPERBIT * PHASE_BIT)
|
|
|
|
#define PHASE_THRES (PHASE_MAX / 2)
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-03 17:02:57 -04:00
|
|
|
// Modulation constants
|
|
|
|
#define MARK_FREQ 1200
|
|
|
|
#define MARK_INC (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)MARK_FREQ, CONFIG_AFSK_DAC_SAMPLERATE))
|
|
|
|
#define SPACE_FREQ 2200
|
|
|
|
#define SPACE_INC (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)SPACE_FREQ, CONFIG_AFSK_DAC_SAMPLERATE))
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-03 17:02:57 -04:00
|
|
|
// Check that sample rate is divisible by bitrate
|
|
|
|
STATIC_ASSERT(!(CONFIG_AFSK_DAC_SAMPLERATE % BITRATE));
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-03 17:02:57 -04:00
|
|
|
#define DAC_SAMPLESPERBIT (CONFIG_AFSK_DAC_SAMPLERATE / BITRATE)
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-03 17:02:57 -04:00
|
|
|
static bool hdlcParse(Hdlc *hdlc, bool bit, FIFOBuffer *fifo)
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
|
2014-04-03 17:25:22 -04:00
|
|
|
hdlc->demodulatedBits <<= 1;
|
|
|
|
hdlc->demodulatedBits |= bit ? 1 : 0;
|
2014-04-03 16:21:37 -04:00
|
|
|
|
|
|
|
/* HDLC Flag */
|
2014-04-03 17:25:22 -04:00
|
|
|
if (hdlc->demodulatedBits == HDLC_FLAG)
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
|
|
|
if (!fifo_isfull(fifo))
|
|
|
|
{
|
|
|
|
fifo_push(fifo, HDLC_FLAG);
|
2014-04-03 17:25:22 -04:00
|
|
|
hdlc->receiving = true;
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = false;
|
2014-04-03 17:25:22 -04:00
|
|
|
hdlc->receiving = false;
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
|
2014-04-03 17:25:22 -04:00
|
|
|
hdlc->currentByte = 0;
|
|
|
|
hdlc->bitIndex = 0;
|
2014-04-03 16:21:37 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset */
|
2014-04-03 17:25:22 -04:00
|
|
|
if ((hdlc->demodulatedBits & HDLC_RESET) == HDLC_RESET)
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
2014-04-03 17:25:22 -04:00
|
|
|
hdlc->receiving = false;
|
2014-04-03 16:21:37 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-03 17:25:22 -04:00
|
|
|
if (!hdlc->receiving)
|
2014-04-03 16:21:37 -04:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Stuffed bit */
|
2014-04-03 17:25:22 -04:00
|
|
|
if ((hdlc->demodulatedBits & 0x3f) == 0x3e)
|
2014-04-03 16:21:37 -04:00
|
|
|
return ret;
|
|
|
|
|
2014-04-03 17:25:22 -04:00
|
|
|
if (hdlc->demodulatedBits & 0x01)
|
|
|
|
hdlc->currentByte |= 0x80;
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-03 17:25:22 -04:00
|
|
|
if (++hdlc->bitIndex >= 8)
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
2014-04-03 17:25:22 -04:00
|
|
|
if ((hdlc->currentByte == HDLC_FLAG
|
|
|
|
|| hdlc->currentByte == HDLC_RESET
|
|
|
|
|| hdlc->currentByte == AX25_ESC))
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
|
|
|
if (!fifo_isfull(fifo))
|
|
|
|
fifo_push(fifo, AX25_ESC);
|
|
|
|
else
|
|
|
|
{
|
2014-04-03 17:25:22 -04:00
|
|
|
hdlc->receiving = false;
|
2014-04-03 16:21:37 -04:00
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fifo_isfull(fifo))
|
2014-04-03 17:25:22 -04:00
|
|
|
fifo_push(fifo, hdlc->currentByte);
|
2014-04-03 16:21:37 -04:00
|
|
|
else
|
|
|
|
{
|
2014-04-03 17:25:22 -04:00
|
|
|
hdlc->receiving = false;
|
2014-04-03 16:21:37 -04:00
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
|
2014-04-03 17:25:22 -04:00
|
|
|
hdlc->currentByte = 0;
|
|
|
|
hdlc->bitIndex = 0;
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
else
|
2014-04-03 17:25:22 -04:00
|
|
|
hdlc->currentByte >>= 1;
|
2014-04-03 16:21:37 -04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-03 17:22:15 -04:00
|
|
|
|
2014-04-03 16:21:37 -04:00
|
|
|
void afsk_adc_isr(Afsk *af, int8_t curr_sample)
|
|
|
|
{
|
|
|
|
AFSK_STROBE_ON();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Frequency discriminator and LP IIR filter.
|
|
|
|
* This filter is designed to work
|
|
|
|
* at the given sample rate and bit rate.
|
|
|
|
*/
|
|
|
|
STATIC_ASSERT(SAMPLERATE == 9600);
|
|
|
|
STATIC_ASSERT(BITRATE == 1200);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Frequency discrimination is achieved by simply multiplying
|
|
|
|
* the sample with a delayed sample of (samples per bit) / 2.
|
|
|
|
* Then the signal is lowpass filtered with a first order,
|
|
|
|
* 600 Hz filter. The filter implementation is selectable
|
|
|
|
* through the CONFIG_AFSK_FILTER config variable.
|
|
|
|
*/
|
|
|
|
|
2014-04-03 17:22:15 -04:00
|
|
|
af->iirX[0] = af->iirX[1];
|
2014-04-03 16:21:37 -04:00
|
|
|
|
|
|
|
#if (CONFIG_AFSK_FILTER == AFSK_BUTTERWORTH)
|
2014-04-03 17:22:15 -04:00
|
|
|
af->iirX[1] = ((int8_t)fifo_pop(&af->delayFifo) * curr_sample) >> 2;
|
|
|
|
//af->iirX[1] = ((int8_t)fifo_pop(&af->delayFifo) * curr_sample) / 6.027339492;
|
2014-04-03 16:21:37 -04:00
|
|
|
#elif (CONFIG_AFSK_FILTER == AFSK_CHEBYSHEV)
|
2014-04-03 17:22:15 -04:00
|
|
|
af->iirX[1] = ((int8_t)fifo_pop(&af->delayFifo) * curr_sample) >> 2;
|
|
|
|
//af->iirX[1] = ((int8_t)fifo_pop(&af->delayFifo) * curr_sample) / 3.558147322;
|
2014-04-03 16:21:37 -04:00
|
|
|
#else
|
|
|
|
#error Filter type not found!
|
|
|
|
#endif
|
|
|
|
|
2014-04-03 17:22:15 -04:00
|
|
|
af->iirY[0] = af->iirY[1];
|
2014-04-03 16:21:37 -04:00
|
|
|
|
|
|
|
#if CONFIG_AFSK_FILTER == AFSK_BUTTERWORTH
|
|
|
|
/*
|
2014-04-03 17:22:15 -04:00
|
|
|
* This strange sum + shift is an optimization for af->iirY[0] * 0.668.
|
2014-04-03 16:21:37 -04:00
|
|
|
* iir * 0.668 ~= (iir * 21) / 32 =
|
|
|
|
* = (iir * 16) / 32 + (iir * 4) / 32 + iir / 32 =
|
|
|
|
* = iir / 2 + iir / 8 + iir / 32 =
|
|
|
|
* = iir >> 1 + iir >> 3 + iir >> 5
|
|
|
|
*/
|
2014-04-03 17:22:15 -04:00
|
|
|
af->iirY[1] = af->iirX[0] + af->iirX[1] + (af->iirY[0] >> 1) + (af->iirY[0] >> 3) + (af->iirY[0] >> 5);
|
|
|
|
//af->iirY[1] = af->iirX[0] + af->iirX[1] + af->iirY[0] * 0.6681786379;
|
2014-04-03 16:21:37 -04:00
|
|
|
#elif CONFIG_AFSK_FILTER == AFSK_CHEBYSHEV
|
|
|
|
/*
|
2014-04-03 17:22:15 -04:00
|
|
|
* This should be (af->iirY[0] * 0.438) but
|
|
|
|
* (af->iirY[0] >> 1) is a faster approximation :-)
|
2014-04-03 16:21:37 -04:00
|
|
|
*/
|
2014-04-03 17:22:15 -04:00
|
|
|
af->iirY[1] = af->iirX[0] + af->iirX[1] + (af->iirY[0] >> 1);
|
|
|
|
//af->iirY[1] = af->iirX[0] + af->iirX[1] + af->iirY[0] * 0.4379097269;
|
2014-04-03 16:21:37 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Save this sampled bit in a delay line */
|
2014-04-03 17:22:15 -04:00
|
|
|
af->sampledBits <<= 1;
|
|
|
|
af->sampledBits |= (af->iirY[1] > 0) ? 1 : 0;
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-03 17:22:15 -04:00
|
|
|
/* Store current ADC sample in the af->delayFifo */
|
|
|
|
fifo_push(&af->delayFifo, curr_sample);
|
2014-04-03 16:21:37 -04:00
|
|
|
|
|
|
|
/* If there is an edge, adjust phase sampling */
|
2014-04-03 17:22:15 -04:00
|
|
|
if (EDGE_FOUND(af->sampledBits))
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
2014-04-03 17:22:15 -04:00
|
|
|
if (af->currentPhase < PHASE_THRES)
|
|
|
|
af->currentPhase += PHASE_INC;
|
2014-04-03 16:21:37 -04:00
|
|
|
else
|
2014-04-03 17:22:15 -04:00
|
|
|
af->currentPhase -= PHASE_INC;
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
2014-04-03 17:22:15 -04:00
|
|
|
af->currentPhase += PHASE_BIT;
|
2014-04-03 16:21:37 -04:00
|
|
|
|
|
|
|
/* sample the bit */
|
2014-04-03 17:22:15 -04:00
|
|
|
if (af->currentPhase >= PHASE_MAX)
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
2014-04-03 17:22:15 -04:00
|
|
|
af->currentPhase %= PHASE_MAX;
|
2014-04-03 16:21:37 -04:00
|
|
|
|
|
|
|
/* Shift 1 position in the shift register of the found bits */
|
2014-04-03 17:22:15 -04:00
|
|
|
af->actualBits <<= 1;
|
2014-04-03 16:21:37 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine bit value by reading the last 3 sampled bits.
|
|
|
|
* If the number of ones is two or greater, the bit value is a 1,
|
|
|
|
* otherwise is a 0.
|
|
|
|
* This algorithm presumes that there are 8 samples per bit.
|
|
|
|
*/
|
2014-04-03 16:54:34 -04:00
|
|
|
STATIC_ASSERT(SAMPLESPERBIT == 8);
|
2014-04-03 17:22:15 -04:00
|
|
|
uint8_t bits = af->sampledBits & 0x07;
|
2014-04-03 16:21:37 -04:00
|
|
|
if (bits == 0x07 // 111, 3 bits set to 1
|
|
|
|
|| bits == 0x06 // 110, 2 bits
|
|
|
|
|| bits == 0x05 // 101, 2 bits
|
|
|
|
|| bits == 0x03 // 011, 2 bits
|
|
|
|
)
|
2014-04-03 17:22:15 -04:00
|
|
|
af->actualBits |= 1;
|
2014-04-03 16:21:37 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NRZI coding: if 2 consecutive bits have the same value
|
|
|
|
* a 1 is received, otherwise it's a 0.
|
|
|
|
*/
|
2014-04-03 17:22:15 -04:00
|
|
|
if (!hdlcParse(&af->hdlc, !EDGE_FOUND(af->actualBits), &af->rxFifo))
|
2014-04-03 17:45:41 -04:00
|
|
|
af->status |= RX_OVERRUN;
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AFSK_STROBE_OFF();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void afsk_txStart(Afsk *af)
|
|
|
|
{
|
|
|
|
if (!af->sending)
|
|
|
|
{
|
2014-04-03 17:22:15 -04:00
|
|
|
af->phaseInc = MARK_INC;
|
|
|
|
af->phaseAcc = 0;
|
|
|
|
af->bitstuffCount = 0;
|
2014-04-03 16:21:37 -04:00
|
|
|
af->sending = true;
|
2014-04-03 17:22:15 -04:00
|
|
|
af->preambleLength = DIV_ROUND(CONFIG_AFSK_PREAMBLE_LEN * BITRATE, 8000);
|
|
|
|
AFSK_DAC_IRQ_START(af->dacPin);
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
2014-04-03 17:22:15 -04:00
|
|
|
ATOMIC(af->tailLength = DIV_ROUND(CONFIG_AFSK_TRAILER_LEN * BITRATE, 8000));
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define BIT_STUFF_LEN 5
|
|
|
|
|
|
|
|
#define SWITCH_TONE(inc) (((inc) == MARK_INC) ? SPACE_INC : MARK_INC)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DAC ISR callback.
|
|
|
|
* This function has to be called by the DAC ISR when a sample of the configured
|
|
|
|
* channel has been converted out.
|
|
|
|
*
|
|
|
|
* \param af Afsk context to operate on.
|
|
|
|
*
|
|
|
|
* \return The next DAC output sample.
|
|
|
|
*/
|
|
|
|
uint8_t afsk_dac_isr(Afsk *af)
|
|
|
|
{
|
|
|
|
AFSK_STROBE_ON();
|
|
|
|
|
|
|
|
/* Check if we are at a start of a sample cycle */
|
2014-04-03 17:22:15 -04:00
|
|
|
if (af->sampleIndex == 0)
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
2014-04-03 17:22:15 -04:00
|
|
|
if (af->txBit == 0)
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
|
|
|
/* We have just finished transimitting a char, get a new one. */
|
2014-04-03 17:22:15 -04:00
|
|
|
if (fifo_isempty(&af->txFifo) && af->tailLength == 0)
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
2014-04-03 17:22:15 -04:00
|
|
|
AFSK_DAC_IRQ_STOP(af->dacPin);
|
2014-04-03 16:21:37 -04:00
|
|
|
af->sending = false;
|
|
|
|
AFSK_STROBE_OFF();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If we have just finished sending an unstuffed byte,
|
|
|
|
* reset bitstuff counter.
|
|
|
|
*/
|
2014-04-03 17:22:15 -04:00
|
|
|
if (!af->bitStuff)
|
|
|
|
af->bitstuffCount = 0;
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-03 17:22:15 -04:00
|
|
|
af->bitStuff = true;
|
2014-04-03 16:21:37 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle preamble and trailer
|
|
|
|
*/
|
2014-04-03 17:22:15 -04:00
|
|
|
if (af->preambleLength == 0)
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
2014-04-03 17:22:15 -04:00
|
|
|
if (fifo_isempty(&af->txFifo))
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
2014-04-03 17:22:15 -04:00
|
|
|
af->tailLength--;
|
|
|
|
af->currentOutputByte = HDLC_FLAG;
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
else
|
2014-04-03 17:22:15 -04:00
|
|
|
af->currentOutputByte = fifo_pop(&af->txFifo);
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-03 17:22:15 -04:00
|
|
|
af->preambleLength--;
|
|
|
|
af->currentOutputByte = HDLC_FLAG;
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle char escape */
|
2014-04-03 17:22:15 -04:00
|
|
|
if (af->currentOutputByte == AX25_ESC)
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
2014-04-03 17:22:15 -04:00
|
|
|
if (fifo_isempty(&af->txFifo))
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
2014-04-03 17:22:15 -04:00
|
|
|
AFSK_DAC_IRQ_STOP(af->dacPin);
|
2014-04-03 16:21:37 -04:00
|
|
|
af->sending = false;
|
|
|
|
AFSK_STROBE_OFF();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
2014-04-03 17:22:15 -04:00
|
|
|
af->currentOutputByte = fifo_pop(&af->txFifo);
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
2014-04-03 17:22:15 -04:00
|
|
|
else if (af->currentOutputByte == HDLC_FLAG || af->currentOutputByte == HDLC_RESET)
|
2014-04-03 16:21:37 -04:00
|
|
|
/* If these chars are not escaped disable bit stuffing */
|
2014-04-03 17:22:15 -04:00
|
|
|
af->bitStuff = false;
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
/* Start with LSB mask */
|
2014-04-03 17:22:15 -04:00
|
|
|
af->txBit = 0x01;
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check for bit stuffing */
|
2014-04-03 17:22:15 -04:00
|
|
|
if (af->bitStuff && af->bitstuffCount >= BIT_STUFF_LEN)
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
|
|
|
/* If there are more than 5 ones in a row insert a 0 */
|
2014-04-03 17:22:15 -04:00
|
|
|
af->bitstuffCount = 0;
|
2014-04-03 16:21:37 -04:00
|
|
|
/* switch tone */
|
2014-04-03 17:22:15 -04:00
|
|
|
af->phaseInc = SWITCH_TONE(af->phaseInc);
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* NRZI: if we want to transmit a 1 the modulated frequency will stay
|
|
|
|
* unchanged; with a 0, there will be a change in the tone.
|
|
|
|
*/
|
2014-04-03 17:22:15 -04:00
|
|
|
if (af->currentOutputByte & af->txBit)
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Transmit a 1:
|
|
|
|
* - Stay on the previous tone
|
|
|
|
* - Increase bit stuff counter
|
|
|
|
*/
|
2014-04-03 17:22:15 -04:00
|
|
|
af->bitstuffCount++;
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Transmit a 0:
|
|
|
|
* - Reset bit stuff counter
|
|
|
|
* - Switch tone
|
|
|
|
*/
|
2014-04-03 17:22:15 -04:00
|
|
|
af->bitstuffCount = 0;
|
|
|
|
af->phaseInc = SWITCH_TONE(af->phaseInc);
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Go to the next bit */
|
2014-04-03 17:22:15 -04:00
|
|
|
af->txBit <<= 1;
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
2014-04-03 17:22:15 -04:00
|
|
|
af->sampleIndex = DAC_SAMPLESPERBIT;
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get new sample and put it out on the DAC */
|
2014-04-03 17:22:15 -04:00
|
|
|
af->phaseAcc += af->phaseInc;
|
|
|
|
af->phaseAcc %= SIN_LEN;
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-03 17:22:15 -04:00
|
|
|
af->sampleIndex--;
|
2014-04-03 16:21:37 -04:00
|
|
|
AFSK_STROBE_OFF();
|
2014-04-03 17:22:15 -04:00
|
|
|
return sinSample(af->phaseAcc);
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static size_t afsk_read(KFile *fd, void *_buf, size_t size)
|
|
|
|
{
|
|
|
|
Afsk *af = AFSK_CAST(fd);
|
|
|
|
uint8_t *buf = (uint8_t *)_buf;
|
|
|
|
|
|
|
|
#if CONFIG_AFSK_RXTIMEOUT == 0
|
2014-04-03 17:22:15 -04:00
|
|
|
while (size-- && !fifo_isempty_locked(&af->rxFifo))
|
2014-04-03 16:21:37 -04:00
|
|
|
#else
|
|
|
|
while (size--)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#if CONFIG_AFSK_RXTIMEOUT != -1
|
|
|
|
ticks_t start = timer_clock();
|
|
|
|
#endif
|
|
|
|
|
2014-04-03 17:22:15 -04:00
|
|
|
while (fifo_isempty_locked(&af->rxFifo))
|
2014-04-03 16:21:37 -04:00
|
|
|
{
|
|
|
|
cpu_relax();
|
|
|
|
#if CONFIG_AFSK_RXTIMEOUT != -1
|
|
|
|
if (timer_clock() - start > ms_to_ticks(CONFIG_AFSK_RXTIMEOUT))
|
|
|
|
return buf - (uint8_t *)_buf;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-04-03 17:22:15 -04:00
|
|
|
*buf++ = fifo_pop_locked(&af->rxFifo);
|
2014-04-03 16:21:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf - (uint8_t *)_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t afsk_write(KFile *fd, const void *_buf, size_t size)
|
|
|
|
{
|
|
|
|
Afsk *af = AFSK_CAST(fd);
|
|
|
|
const uint8_t *buf = (const uint8_t *)_buf;
|
|
|
|
|
|
|
|
while (size--)
|
|
|
|
{
|
2014-04-03 17:22:15 -04:00
|
|
|
while (fifo_isfull_locked(&af->txFifo))
|
2014-04-03 16:21:37 -04:00
|
|
|
cpu_relax();
|
|
|
|
|
2014-04-03 17:22:15 -04:00
|
|
|
fifo_push_locked(&af->txFifo, *buf++);
|
2014-04-03 16:21:37 -04:00
|
|
|
afsk_txStart(af);
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf - (const uint8_t *)_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int afsk_flush(KFile *fd)
|
|
|
|
{
|
|
|
|
Afsk *af = AFSK_CAST(fd);
|
|
|
|
while (af->sending)
|
|
|
|
cpu_relax();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int afsk_error(KFile *fd)
|
|
|
|
{
|
|
|
|
Afsk *af = AFSK_CAST(fd);
|
|
|
|
int err;
|
|
|
|
|
|
|
|
ATOMIC(err = af->status);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void afsk_clearerr(KFile *fd)
|
|
|
|
{
|
|
|
|
Afsk *af = AFSK_CAST(fd);
|
|
|
|
ATOMIC(af->status = 0);
|
|
|
|
}
|
|
|
|
|
2014-04-03 17:45:41 -04:00
|
|
|
void afsk_init(Afsk *afsk, int _adcPin, int _dacPin) {
|
|
|
|
// Allocate memory for struct
|
|
|
|
memset(afsk, 0, sizeof(*afsk));
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-03 17:45:41 -04:00
|
|
|
// Configure pins
|
|
|
|
afsk->adcPin = _adcPin;
|
|
|
|
afsk->dacPin = _dacPin;
|
|
|
|
afsk->phaseInc = MARK_INC;
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-03 17:45:41 -04:00
|
|
|
// Init FIFO buffers
|
|
|
|
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));
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-03 17:45:41 -04:00
|
|
|
// Fill delay FIFO with zeroes
|
|
|
|
for (int i = 0; i<SAMPLESPERBIT / 2; i++) {
|
|
|
|
fifo_push(&afsk->delayFifo, 0);
|
|
|
|
}
|
2014-04-03 16:21:37 -04:00
|
|
|
|
2014-04-03 17:45:41 -04:00
|
|
|
// Init DAC & ADC
|
|
|
|
AFSK_ADC_INIT(_adcPin, afsk);
|
|
|
|
AFSK_DAC_INIT(_dacPin, afsk);
|
2014-04-03 16:21:37 -04:00
|
|
|
AFSK_STROBE_INIT();
|
2014-04-03 17:45:41 -04:00
|
|
|
|
|
|
|
DB(afsk->fd._type = KFT_AFSK);
|
|
|
|
afsk->fd.write = afsk_write;
|
|
|
|
afsk->fd.read = afsk_read;
|
|
|
|
afsk->fd.flush = afsk_flush;
|
|
|
|
afsk->fd.error = afsk_error;
|
|
|
|
afsk->fd.clearerr = afsk_clearerr;
|
|
|
|
}
|