parent
84d3258a5d
commit
3788ad540f
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (C) 2016,2017,2018 by Jonathan Naylor G4KLX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#if !defined(NXDNDEFINES_H)
|
||||
#define NXDNDEFINES_H
|
||||
|
||||
const unsigned int NXDN_RADIO_SYMBOL_LENGTH = 10U; // At 24 kHz sample rate
|
||||
|
||||
const unsigned int NXDN_FRAME_LENGTH_BITS = 384U;
|
||||
const unsigned int NXDN_FRAME_LENGTH_BYTES = NXDN_FRAME_LENGTH_BITS / 8U;
|
||||
const unsigned int NXDN_FRAME_LENGTH_SYMBOLS = NXDN_FRAME_LENGTH_BITS / 2U;
|
||||
const unsigned int NXDN_FRAME_LENGTH_SAMPLES = NXDN_FRAME_LENGTH_SYMBOLS * NXDN_RADIO_SYMBOL_LENGTH;
|
||||
|
||||
const unsigned int NXDN_FSW_LENGTH_BITS = 20U;
|
||||
const unsigned int NXDN_FSW_LENGTH_SYMBOLS = NXDN_FSW_LENGTH_BITS / 2U;
|
||||
const unsigned int NXDN_FSW_LENGTH_SAMPLES = NXDN_FSW_LENGTH_SYMBOLS * NXDN_RADIO_SYMBOL_LENGTH;
|
||||
|
||||
const uint8_t NXDN_FSW_BYTES[] = {0xCDU, 0xF5U, 0x90U};
|
||||
const uint8_t NXDN_FSW_BYTES_MASK[] = {0xFFU, 0xFFU, 0xF0U};
|
||||
const uint8_t NXDN_FSW_BYTES_LENGTH = 3U;
|
||||
|
||||
const uint32_t NXDN_FSW_BITS = 0x000CDF59U;
|
||||
const uint32_t NXDN_FSW_BITS_MASK = 0x000FFFFFU;
|
||||
|
||||
// C D F 5 9
|
||||
// 11 00 11 01 11 11 01 01 10 01
|
||||
// -3 +1 -3 +3 -3 -3 +3 +3 -1 +3
|
||||
|
||||
const int8_t NXDN_FSW_SYMBOLS_VALUES[] = {-3, +1, -3, +3, -3, -3, +3, +3, -1, +3};
|
||||
|
||||
const uint16_t NXDN_FSW_SYMBOLS = 0x014DU;
|
||||
const uint16_t NXDN_FSW_SYMBOLS_MASK = 0x03FFU;
|
||||
|
||||
#endif
|
||||
|
||||
@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright (C) 2009-2017,2018 by Jonathan Naylor G4KLX
|
||||
* Copyright (C) 2016,2017,2018 by Andy Uribe CA6JAU
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include "Config.h"
|
||||
#include "Globals.h"
|
||||
#include "NXDNRX.h"
|
||||
#include "Utils.h"
|
||||
|
||||
const uint8_t MAX_FSW_BIT_START_ERRS = 1U;
|
||||
const uint8_t MAX_FSW_BIT_RUN_ERRS = 3U;
|
||||
|
||||
const unsigned int MAX_FSW_FRAMES = 5U + 1U;
|
||||
|
||||
const uint8_t BIT_MASK_TABLE[] = {0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U};
|
||||
|
||||
#define WRITE_BIT1(p,i,b) p[(i)>>3] = (b) ? (p[(i)>>3] | BIT_MASK_TABLE[(i)&7]) : (p[(i)>>3] & ~BIT_MASK_TABLE[(i)&7])
|
||||
|
||||
CNXDNRX::CNXDNRX() :
|
||||
m_prev(false),
|
||||
m_state(NXDNRXS_NONE),
|
||||
m_bitBuffer(0x00U),
|
||||
m_outBuffer(),
|
||||
m_buffer(NULL),
|
||||
m_bufferPtr(0U),
|
||||
m_lostCount(0U)
|
||||
{
|
||||
m_buffer = m_outBuffer + 1U;
|
||||
}
|
||||
|
||||
void CNXDNRX::reset()
|
||||
{
|
||||
m_prev = false;
|
||||
m_state = NXDNRXS_NONE;
|
||||
m_bitBuffer = 0x00U;
|
||||
m_bufferPtr = 0U;
|
||||
m_lostCount = 0U;
|
||||
}
|
||||
|
||||
void CNXDNRX::databit(bool bit)
|
||||
{
|
||||
if (m_state == NXDNRXS_NONE)
|
||||
processNone(bit);
|
||||
else
|
||||
processData(bit);
|
||||
}
|
||||
|
||||
void CNXDNRX::processNone(bool bit)
|
||||
{
|
||||
m_bitBuffer <<= 1;
|
||||
if (bit)
|
||||
m_bitBuffer |= 0x01U;
|
||||
|
||||
// Fuzzy matching of the data sync bit sequence
|
||||
if (countBits64((m_bitBuffer & NXDN_FSW_BITS_MASK) ^ NXDN_FSW_BITS) <= MAX_FSW_BIT_START_ERRS) {
|
||||
DEBUG1("NXDNRX: sync found in None");
|
||||
for (uint8_t i = 0U; i < NXDN_FSW_BYTES_LENGTH; i++)
|
||||
m_buffer[i] = NXDN_FSW_BYTES[i];
|
||||
|
||||
m_lostCount = MAX_FSW_FRAMES;
|
||||
m_bufferPtr = NXDN_FSW_LENGTH_BITS;
|
||||
m_state = NXDNRXS_DATA;
|
||||
|
||||
io.setDecode(true);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void CNXDNRX::processData(bool bit)
|
||||
{
|
||||
m_bitBuffer <<= 1;
|
||||
if (bit)
|
||||
m_bitBuffer |= 0x01U;
|
||||
|
||||
WRITE_BIT1(m_buffer, m_bufferPtr, bit);
|
||||
m_bufferPtr++;
|
||||
|
||||
// Only search for a sync in the right place +-2 symbols
|
||||
if (m_bufferPtr >= (NXDN_FSW_LENGTH_BITS - 2U) && m_bufferPtr <= (NXDN_FSW_LENGTH_BITS + 2U)) {
|
||||
// Fuzzy matching of the data sync bit sequence
|
||||
if (countBits64((m_bitBuffer & NXDN_FSW_BITS_MASK) ^ NXDN_FSW_BITS) <= MAX_FSW_BIT_RUN_ERRS) {
|
||||
DEBUG2("NXDNRX: found sync in Data, pos", m_bufferPtr - NXDN_FSW_LENGTH_BITS);
|
||||
m_lostCount = MAX_FSW_FRAMES;
|
||||
m_bufferPtr = NXDN_FSW_LENGTH_BITS;
|
||||
}
|
||||
}
|
||||
|
||||
// Send a data frame to the host if the required number of bits have been received
|
||||
if (m_bufferPtr == NXDN_FRAME_LENGTH_BITS) {
|
||||
// We've not seen a data sync for too long, signal RXLOST and change to RX_NONE
|
||||
m_lostCount--;
|
||||
if (m_lostCount == 0U) {
|
||||
DEBUG1("NXDNRX: sync timed out, lost lock");
|
||||
io.setDecode(false);
|
||||
|
||||
serial.writeNXDNLost();
|
||||
|
||||
m_state = NXDNRXS_NONE;
|
||||
} else {
|
||||
m_outBuffer[0U] = m_lostCount == (MAX_FSW_FRAMES - 1U) ? 0x01U : 0x00U;
|
||||
|
||||
writeRSSIData(m_outBuffer);
|
||||
|
||||
// Start the next frame
|
||||
::memset(m_outBuffer, 0x00U, NXDN_FRAME_LENGTH_BYTES + 3U);
|
||||
m_bufferPtr = 0U;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CNXDNRX::writeRSSIData(uint8_t* data)
|
||||
{
|
||||
#if defined(SEND_RSSI_DATA)
|
||||
uint16_t rssi = io.readRSSI();
|
||||
|
||||
data[49U] = (rssi >> 8) & 0xFFU;
|
||||
data[50U] = (rssi >> 0) & 0xFFU;
|
||||
|
||||
serial.writeNXDNData(data, NXDN_FRAME_LENGTH_BYTES + 3U);
|
||||
#else
|
||||
serial.writeNXDNData(data, NXDN_FRAME_LENGTH_BYTES + 1U);
|
||||
#endif
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2015,2016,2017,2018 by Jonathan Naylor G4KLX
|
||||
* Copyright (C) 2016,2017,2018 by Andy Uribe CA6JAU
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#if !defined(NXDNRX_H)
|
||||
#define NXDNRX_H
|
||||
|
||||
#include "NXDNDefines.h"
|
||||
|
||||
enum NXDNRX_STATE {
|
||||
NXDNRXS_NONE,
|
||||
NXDNRXS_DATA
|
||||
};
|
||||
|
||||
class CNXDNRX {
|
||||
public:
|
||||
CNXDNRX();
|
||||
|
||||
void databit(bool bit);
|
||||
|
||||
void reset();
|
||||
|
||||
private:
|
||||
bool m_prev;
|
||||
NXDNRX_STATE m_state;
|
||||
uint64_t m_bitBuffer;
|
||||
uint8_t m_outBuffer[NXDN_FRAME_LENGTH_BYTES + 3U];
|
||||
uint8_t* m_buffer;
|
||||
uint16_t m_bufferPtr;
|
||||
uint16_t m_lostCount;
|
||||
|
||||
void processNone(bool bit);
|
||||
void processData(bool bit);
|
||||
void writeRSSIData(uint8_t* data);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Copyright (C) 2009-2016,2018 by Jonathan Naylor G4KLX
|
||||
* Copyright (C) 2016,2017,2018 by Andy Uribe CA6JAU
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include "Config.h"
|
||||
#include "Globals.h"
|
||||
#include "NXDNTX.h"
|
||||
|
||||
#include "NXDNDefines.h"
|
||||
|
||||
const uint8_t NXDN_PREAMBLE[] = {0x57U, 0x75U, 0xFDU};
|
||||
const uint8_t NXDN_SYNC = 0x5FU;
|
||||
|
||||
CNXDNTX::CNXDNTX() :
|
||||
m_buffer(1500U),
|
||||
m_poBuffer(),
|
||||
m_poLen(0U),
|
||||
m_poPtr(0U),
|
||||
m_txDelay(240U), // 200ms
|
||||
m_count(0U)
|
||||
{
|
||||
}
|
||||
|
||||
void CNXDNTX::process()
|
||||
{
|
||||
if (m_buffer.getData() == 0U && m_poLen == 0U)
|
||||
return;
|
||||
|
||||
if (m_poLen == 0U) {
|
||||
if (!m_tx) {
|
||||
m_delay = true;
|
||||
m_count = 0U;
|
||||
m_poLen = m_txDelay;
|
||||
} else {
|
||||
m_delay = false;
|
||||
for (uint8_t i = 0U; i < NXDN_FRAME_LENGTH_BYTES; i++)
|
||||
m_poBuffer[m_poLen++] = m_buffer.get();
|
||||
}
|
||||
|
||||
m_poPtr = 0U;
|
||||
}
|
||||
|
||||
if (m_poLen > 0U) {
|
||||
uint16_t space = io.getSpace();
|
||||
|
||||
while (space > 8U) {
|
||||
if (m_delay) {
|
||||
m_poPtr++;
|
||||
writeByte(NXDN_SYNC);
|
||||
}
|
||||
else
|
||||
writeByte(m_poBuffer[m_poPtr++]);
|
||||
|
||||
space -= 8U;
|
||||
|
||||
if (m_poPtr >= m_poLen) {
|
||||
m_poPtr = 0U;
|
||||
m_poLen = 0U;
|
||||
m_delay = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t CNXDNTX::writeData(const uint8_t* data, uint8_t length)
|
||||
{
|
||||
if (length != (NXDN_FRAME_LENGTH_BYTES + 1U))
|
||||
return 4U;
|
||||
|
||||
uint16_t space = m_buffer.getSpace();
|
||||
if (space < NXDN_FRAME_LENGTH_BYTES)
|
||||
return 5U;
|
||||
|
||||
for (uint8_t i = 0U; i < NXDN_FRAME_LENGTH_BYTES; i++)
|
||||
m_buffer.put(data[i + 1U]);
|
||||
|
||||
return 0U;
|
||||
}
|
||||
|
||||
void CNXDNTX::writeByte(uint8_t c)
|
||||
{
|
||||
uint8_t bit;
|
||||
uint8_t mask = 0x80U;
|
||||
|
||||
for (uint8_t i = 0U; i < 8U; i++, c <<= 1) {
|
||||
if ((c & mask) == mask)
|
||||
bit = 1U;
|
||||
else
|
||||
bit = 0U;
|
||||
|
||||
io.write(&bit, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void CNXDNTX::setTXDelay(uint8_t delay)
|
||||
{
|
||||
m_txDelay = 300U + uint16_t(delay) * 6U; // 500ms + tx delay
|
||||
}
|
||||
|
||||
uint16_t CNXDNTX::getSpace() const
|
||||
{
|
||||
return m_buffer.getSpace() / NXDN_FRAME_LENGTH_BYTES;
|
||||
}
|
||||
|
||||
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (C) 2015,2016,2017,2018 by Jonathan Naylor G4KLX
|
||||
* Copyright (C) 2016,2017,2018 by Andy Uribe CA6JAU
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#if !defined(NXDNTX_H)
|
||||
#define NXDNTX_H
|
||||
|
||||
#include "SerialRB.h"
|
||||
|
||||
class CNXDNTX {
|
||||
public:
|
||||
CNXDNTX();
|
||||
|
||||
uint8_t writeData(const uint8_t* data, uint8_t length);
|
||||
|
||||
void process();
|
||||
|
||||
void setTXDelay(uint8_t delay);
|
||||
|
||||
uint16_t getSpace() const;
|
||||
|
||||
private:
|
||||
CSerialRB m_buffer;
|
||||
uint8_t m_poBuffer[60U];
|
||||
uint16_t m_poLen;
|
||||
uint16_t m_poPtr;
|
||||
uint16_t m_txDelay;
|
||||
uint32_t m_count;
|
||||
bool m_delay;
|
||||
|
||||
void writeByte(uint8_t c);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Loading…
Reference in new issue