2014-12-02 19:10:06 -05:00
|
|
|
#ifndef AFSK_H
|
|
|
|
#define AFSK_H
|
|
|
|
|
|
|
|
#include "device.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
#include "util/FIFO.h"
|
|
|
|
#include "protocol/HDLC.h"
|
|
|
|
|
|
|
|
#define SIN_LEN 512
|
2018-12-31 07:24:28 -05:00
|
|
|
static const uint8_t sine_table[] =
|
2014-12-02 19:10:06 -05:00
|
|
|
{
|
|
|
|
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 static uint8_t sinSample(uint16_t i) {
|
|
|
|
uint16_t newI = i % (SIN_LEN/2);
|
|
|
|
newI = (newI >= (SIN_LEN/4)) ? (SIN_LEN/2 - newI -1) : newI;
|
2018-12-31 07:24:28 -05:00
|
|
|
uint8_t sine = sine_table[newI];
|
2014-12-02 19:10:06 -05:00
|
|
|
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)
|
2018-12-29 10:06:48 -05:00
|
|
|
#define TRANSITION_FOUND(bits) BITS_DIFFER((bits), (bits) >> 1)
|
2014-12-02 19:10:06 -05:00
|
|
|
#define DUAL_XOR(bits1, bits2) ((((bits1)^(bits2)) & 0x03) == 0x03)
|
2018-12-29 18:32:19 -05:00
|
|
|
#define QUAD_XOR(bits1, bits2) ((((bits1)^(bits2)) & 0x0F) == 0x0F)
|
2014-12-02 19:10:06 -05:00
|
|
|
|
|
|
|
#define CPU_FREQ F_CPU
|
|
|
|
|
2019-01-05 07:47:46 -05:00
|
|
|
#define CONFIG_AFSK_RX_BUFLEN CONFIG_ADC_SAMPLERATE/150
|
|
|
|
#define CONFIG_AFSK_TX_BUFLEN CONFIG_ADC_SAMPLERATE/150
|
2014-12-02 19:10:06 -05:00
|
|
|
#define CONFIG_AFSK_RXTIMEOUT 0
|
2018-12-01 05:48:29 -05:00
|
|
|
#define CONFIG_AFSK_TXWAIT 0UL
|
2019-01-01 14:34:02 -05:00
|
|
|
#define CONFIG_AFSK_PREAMBLE_LEN 150UL
|
|
|
|
#define CONFIG_AFSK_TRAILER_LEN 25UL
|
2015-04-26 12:15:05 -04:00
|
|
|
#define BIT_STUFF_LEN 5
|
|
|
|
|
2019-01-08 05:10:21 -05:00
|
|
|
#define BITRATE 1200
|
2019-01-01 14:34:02 -05:00
|
|
|
|
2019-01-07 07:41:49 -05:00
|
|
|
#if BITRATE == 300
|
2019-01-08 05:10:21 -05:00
|
|
|
#define CONFIG_ADC_SAMPLERATE 9600UL
|
2019-01-07 07:41:49 -05:00
|
|
|
#define CONFIG_DAC_SAMPLERATE 19200UL
|
|
|
|
#elif BITRATE == 1200
|
2019-01-05 07:47:46 -05:00
|
|
|
#define CONFIG_ADC_SAMPLERATE 9600UL
|
|
|
|
#define CONFIG_DAC_SAMPLERATE 19200UL
|
2019-01-01 14:34:02 -05:00
|
|
|
#elif BITRATE == 2400
|
2019-01-05 07:47:46 -05:00
|
|
|
#define CONFIG_ADC_SAMPLERATE 19200UL
|
|
|
|
#define CONFIG_DAC_SAMPLERATE 38400UL
|
2019-01-01 14:34:02 -05:00
|
|
|
#endif
|
|
|
|
|
2019-01-05 07:47:46 -05:00
|
|
|
#define ADC_SAMPLESPERBIT (CONFIG_ADC_SAMPLERATE / BITRATE)
|
|
|
|
#define ADC_TICKS_BETWEEN_SAMPLES ((((CPU_FREQ+FREQUENCY_CORRECTION)) / CONFIG_ADC_SAMPLERATE) - 1)
|
2018-04-24 09:34:50 -04:00
|
|
|
|
2019-01-05 07:47:46 -05:00
|
|
|
#define DAC_SAMPLESPERBIT (CONFIG_DAC_SAMPLERATE / BITRATE)
|
|
|
|
#define DAC_TICKS_BETWEEN_SAMPLES ((((CPU_FREQ+FREQUENCY_CORRECTION)) / CONFIG_DAC_SAMPLERATE) - 1)
|
2018-12-31 07:24:28 -05:00
|
|
|
|
2019-01-05 07:47:46 -05:00
|
|
|
// TODO: Maybe revert to only looking at two samples
|
2019-01-07 07:41:49 -05:00
|
|
|
#if BITRATE == 300
|
|
|
|
#define SIGNAL_TRANSITIONED(bits) DUAL_XOR((bits), (bits) >> 2)
|
|
|
|
#elif BITRATE == 1200
|
2019-01-05 07:47:46 -05:00
|
|
|
#if CONFIG_ADC_SAMPLERATE == 19200
|
2019-01-01 14:34:02 -05:00
|
|
|
#define SIGNAL_TRANSITIONED(bits) QUAD_XOR((bits), (bits) >> 4)
|
2019-01-05 07:47:46 -05:00
|
|
|
#elif CONFIG_ADC_SAMPLERATE == 9600
|
2019-01-01 14:34:02 -05:00
|
|
|
#define SIGNAL_TRANSITIONED(bits) DUAL_XOR((bits), (bits) >> 2)
|
|
|
|
#endif
|
2018-12-31 07:24:28 -05:00
|
|
|
#elif BITRATE == 2400
|
|
|
|
#define SIGNAL_TRANSITIONED(bits) DUAL_XOR((bits), (bits) >> 2)
|
|
|
|
#endif
|
|
|
|
|
2019-01-08 05:10:21 -05:00
|
|
|
#if BITRATE == 300
|
|
|
|
// TODO: Real-world tests on which resolution is best
|
|
|
|
//#define PHASE_BITS 8
|
|
|
|
#define PHASE_BITS 4
|
|
|
|
#else
|
|
|
|
#define PHASE_BITS 8 // Sub-sample phase counter resolution
|
|
|
|
#endif
|
|
|
|
|
2018-12-31 07:24:28 -05:00
|
|
|
#define PHASE_INC 1 // Nudge by above resolution for each adjustment
|
|
|
|
|
2019-01-05 07:47:46 -05:00
|
|
|
#define PHASE_MAX (ADC_SAMPLESPERBIT * PHASE_BITS) // Size of our phase counter
|
2018-12-29 09:57:49 -05:00
|
|
|
|
2018-12-29 18:32:19 -05:00
|
|
|
// TODO: Test which target is best in real world
|
2018-12-31 07:24:28 -05:00
|
|
|
// For 1200, this seems a little better
|
2019-01-07 07:41:49 -05:00
|
|
|
#if BITRATE == 300
|
|
|
|
#define PHASE_THRESHOLD (PHASE_MAX / 2)
|
|
|
|
#elif BITRATE == 1200
|
2019-01-05 07:47:46 -05:00
|
|
|
#if CONFIG_ADC_SAMPLERATE == 19200
|
2019-01-01 14:34:02 -05:00
|
|
|
#define PHASE_THRESHOLD (PHASE_MAX / 2)+3*PHASE_BITS // Target transition point of our phase window
|
2019-01-05 07:47:46 -05:00
|
|
|
#elif CONFIG_ADC_SAMPLERATE == 9600
|
2019-01-01 14:34:02 -05:00
|
|
|
#define PHASE_THRESHOLD (PHASE_MAX / 2) // 64 // Target transition point of our phase window
|
|
|
|
#endif
|
2018-12-31 07:24:28 -05:00
|
|
|
#elif BITRATE == 2400
|
2019-01-01 14:34:02 -05:00
|
|
|
#define PHASE_THRESHOLD (PHASE_MAX / 2) // Target transition point of our phase window
|
2018-12-31 07:24:28 -05:00
|
|
|
#endif
|
|
|
|
|
2019-01-08 05:10:21 -05:00
|
|
|
#if BITRATE == 300
|
|
|
|
#define DCD_TIMEOUT_SAMPLES 520
|
|
|
|
#define DCD_MIN_COUNT 2
|
|
|
|
#elif BITRATE == 1200
|
|
|
|
#define DCD_TIMEOUT_SAMPLES CONFIG_ADC_SAMPLERATE/100
|
|
|
|
#define DCD_MIN_COUNT CONFIG_ADC_SAMPLERATE/1600
|
|
|
|
#elif BITRATE == 2400
|
|
|
|
#define DCD_TIMEOUT_SAMPLES CONFIG_ADC_SAMPLERATE/100
|
|
|
|
#define DCD_MIN_COUNT CONFIG_ADC_SAMPLERATE/1600
|
|
|
|
#endif
|
2018-12-29 18:32:19 -05:00
|
|
|
|
|
|
|
// TODO: Revamp filtering
|
2018-12-29 09:57:49 -05:00
|
|
|
#if BITRATE == 1200
|
2019-01-01 14:34:02 -05:00
|
|
|
#define FILTER_CUTOFF 500
|
2015-04-24 04:47:50 -04:00
|
|
|
#define MARK_FREQ 1200
|
|
|
|
#define SPACE_FREQ 2200
|
2018-12-31 07:24:28 -05:00
|
|
|
#elif BITRATE == 2400
|
2019-01-01 14:34:02 -05:00
|
|
|
// TODO: Determine best filter cutoff
|
|
|
|
// #define FILTER_CUTOFF 772
|
|
|
|
#define FILTER_CUTOFF 1000
|
|
|
|
#define MARK_FREQ 2165
|
|
|
|
#define SPACE_FREQ 3970
|
|
|
|
//#define MARK_FREQ 2200
|
|
|
|
//#define SPACE_FREQ 4000
|
2018-12-31 07:24:28 -05:00
|
|
|
#elif BITRATE == 300
|
2019-01-08 05:10:21 -05:00
|
|
|
#define FILTER_CUTOFF 500
|
2018-12-31 07:24:28 -05:00
|
|
|
#define MARK_FREQ 1600
|
|
|
|
#define SPACE_FREQ 1800
|
2015-04-24 04:47:50 -04:00
|
|
|
#else
|
|
|
|
#error Unsupported bitrate!
|
|
|
|
#endif
|
|
|
|
|
2014-12-02 19:10:06 -05:00
|
|
|
typedef struct Hdlc
|
|
|
|
{
|
|
|
|
uint8_t demodulatedBits;
|
|
|
|
uint8_t bitIndex;
|
|
|
|
uint8_t currentByte;
|
|
|
|
bool receiving;
|
2018-04-24 09:34:50 -04:00
|
|
|
bool dcd;
|
|
|
|
uint8_t dcd_count;
|
2014-12-02 19:10:06 -05:00
|
|
|
} Hdlc;
|
|
|
|
|
|
|
|
typedef struct Afsk
|
|
|
|
{
|
|
|
|
// Stream access to modem
|
|
|
|
FILE fd;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2019-01-08 05:10:21 -05:00
|
|
|
uint16_t silentSamples; // How many samples were completely silent
|
2018-04-24 11:34:59 -04:00
|
|
|
|
2014-12-02 19:10:06 -05:00
|
|
|
FIFOBuffer txFifo; // FIFO for transmit data
|
2018-04-24 08:34:59 -04:00
|
|
|
uint8_t txBuf[CONFIG_AFSK_TX_BUFLEN]; // Actual data storage for said FIFO
|
2014-12-02 19:10:06 -05:00
|
|
|
|
|
|
|
volatile bool sending; // Set when modem is sending
|
2018-04-24 08:34:59 -04:00
|
|
|
volatile bool sending_data; // Set when modem is sending data
|
2014-12-02 19:10:06 -05:00
|
|
|
|
|
|
|
// Demodulation values
|
|
|
|
FIFOBuffer delayFifo; // Delayed FIFO for frequency discrimination
|
2018-12-31 07:24:28 -05:00
|
|
|
#if BITRATE == 1200
|
2019-01-07 07:41:49 -05:00
|
|
|
int8_t delayBuf[ADC_SAMPLESPERBIT / 2 + 1]; // Actual data storage for said FIFO
|
2018-12-31 07:24:28 -05:00
|
|
|
#elif BITRATE == 2400
|
2019-01-07 07:41:49 -05:00
|
|
|
int8_t delayBuf[7 + 1];
|
|
|
|
#elif BITRATE == 300
|
2019-01-08 05:10:21 -05:00
|
|
|
int8_t delayBuf[16 + 1];
|
2018-12-31 07:24:28 -05:00
|
|
|
#endif
|
2014-12-02 19:10:06 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-01-05 07:47:46 -05:00
|
|
|
#if ADC_SAMPLESPERBIT < 17
|
2018-12-29 18:32:19 -05:00
|
|
|
uint16_t sampledBits; // Bits sampled by the demodulator (at ADC speed)
|
2019-01-08 05:10:21 -05:00
|
|
|
#elif ADC_SAMPLESPERBIT < 33
|
|
|
|
uint32_t sampledBits;
|
2018-12-29 18:32:19 -05:00
|
|
|
#else
|
2019-01-07 07:41:49 -05:00
|
|
|
#error Not enough space in sampledBits variable!
|
2018-12-29 18:32:19 -05:00
|
|
|
#endif
|
|
|
|
int16_t currentPhase; // Current phase of the demodulator
|
2014-12-02 19:10:06 -05:00
|
|
|
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))
|
2019-01-05 07:47:46 -05:00
|
|
|
#define MARK_INC (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)MARK_FREQ, CONFIG_DAC_SAMPLERATE))
|
|
|
|
#define SPACE_INC (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)SPACE_FREQ, CONFIG_DAC_SAMPLERATE))
|
2014-12-02 19:10:06 -05:00
|
|
|
|
|
|
|
#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)
|
2018-12-27 14:24:21 -05:00
|
|
|
|
2014-12-02 19:10:06 -05:00
|
|
|
// Here's some macros for controlling the RX/TX LEDs
|
2018-12-29 09:57:49 -05:00
|
|
|
// THE _INIT() functions writes to the DDR registers
|
2014-12-02 19:10:06 -05:00
|
|
|
// to configure the pins as output pins, and the _ON()
|
|
|
|
// and _OFF() functions writes to the PORT registers
|
|
|
|
// to turn the pins on or off.
|
2015-11-17 02:09:39 -05:00
|
|
|
#define LED_TX_INIT() do { LED_DDR |= _BV(1); } while (0)
|
|
|
|
#define LED_TX_ON() do { LED_PORT |= _BV(1); } while (0)
|
|
|
|
#define LED_TX_OFF() do { LED_PORT &= ~_BV(1); } while (0)
|
2014-12-02 19:10:06 -05:00
|
|
|
|
2015-11-17 02:09:39 -05:00
|
|
|
#define LED_RX_INIT() do { LED_DDR |= _BV(2); } while (0)
|
|
|
|
#define LED_RX_ON() do { LED_PORT |= _BV(2); } while (0)
|
|
|
|
#define LED_RX_OFF() do { LED_PORT &= ~_BV(2); } while (0)
|
2014-12-02 19:10:06 -05:00
|
|
|
|
|
|
|
void AFSK_init(Afsk *afsk);
|
2019-01-05 07:47:46 -05:00
|
|
|
void AFSK_adc_init(void);
|
|
|
|
void AFSK_dac_init(void);
|
2014-12-02 19:10:06 -05:00
|
|
|
void AFSK_transmit(char *buffer, size_t size);
|
|
|
|
void AFSK_poll(Afsk *afsk);
|
|
|
|
|
2015-11-17 02:09:39 -05:00
|
|
|
#endif
|