From dd17a47972192a0ff448fb6842b5bef1c783bd34 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Thu, 14 Jan 2016 18:57:21 +0000 Subject: [PATCH] Initial commit --- BUILD.txt | 88 +++++ CalRX.cpp | 128 ++++++++ CalRX.h | 42 +++ CalTX.cpp | 183 +++++++++++ CalTX.h | 39 +++ Config.h | 34 ++ DMRDefines.h | 82 +++++ DMRIdleRX.cpp | 199 +++++++++++ DMRIdleRX.h | 51 +++ DMRRX.cpp | 78 +++++ DMRRX.h | 44 +++ DMRSlotRX.cpp | 279 ++++++++++++++++ DMRSlotRX.h | 60 ++++ DMRSlotType.cpp | 287 ++++++++++++++++ DMRSlotType.h | 37 +++ DMRTX.cpp | 306 +++++++++++++++++ DMRTX.h | 72 ++++ DStarDefines.h | 48 +++ DStarRX.cpp | 696 +++++++++++++++++++++++++++++++++++++++ DStarRX.h | 68 ++++ DStarTX.cpp | 435 ++++++++++++++++++++++++ DStarTX.h | 54 +++ Debug.h | 51 +++ Globals.h | 90 +++++ IO.cpp | 403 +++++++++++++++++++++++ IO.h | 82 +++++ LICENCE | 340 +++++++++++++++++++ MMDVM.ino | 71 ++++ README.md | 4 + SampleRB.cpp | 83 +++++ SampleRB.h | 52 +++ SerialPort.cpp | 858 ++++++++++++++++++++++++++++++++++++++++++++++++ SerialPort.h | 77 +++++ SerialRB.cpp | 93 ++++++ SerialRB.h | 57 ++++ Utils.cpp | 59 ++++ Utils.h | 35 ++ YSFDefines.h | 48 +++ YSFRX.cpp | 696 +++++++++++++++++++++++++++++++++++++++ YSFRX.h | 70 ++++ YSFTX.cpp | 147 +++++++++ YSFTX.h | 51 +++ 42 files changed, 6677 insertions(+) create mode 100644 BUILD.txt create mode 100644 CalRX.cpp create mode 100644 CalRX.h create mode 100644 CalTX.cpp create mode 100644 CalTX.h create mode 100644 Config.h create mode 100644 DMRDefines.h create mode 100644 DMRIdleRX.cpp create mode 100644 DMRIdleRX.h create mode 100644 DMRRX.cpp create mode 100644 DMRRX.h create mode 100644 DMRSlotRX.cpp create mode 100644 DMRSlotRX.h create mode 100644 DMRSlotType.cpp create mode 100644 DMRSlotType.h create mode 100644 DMRTX.cpp create mode 100644 DMRTX.h create mode 100644 DStarDefines.h create mode 100644 DStarRX.cpp create mode 100644 DStarRX.h create mode 100644 DStarTX.cpp create mode 100644 DStarTX.h create mode 100644 Debug.h create mode 100644 Globals.h create mode 100644 IO.cpp create mode 100644 IO.h create mode 100644 LICENCE create mode 100644 MMDVM.ino create mode 100644 README.md create mode 100644 SampleRB.cpp create mode 100644 SampleRB.h create mode 100644 SerialPort.cpp create mode 100644 SerialPort.h create mode 100644 SerialRB.cpp create mode 100644 SerialRB.h create mode 100644 Utils.cpp create mode 100644 Utils.h create mode 100644 YSFDefines.h create mode 100644 YSFRX.cpp create mode 100644 YSFRX.h create mode 100644 YSFTX.cpp create mode 100644 YSFTX.h diff --git a/BUILD.txt b/BUILD.txt new file mode 100644 index 0000000..9ff3f6f --- /dev/null +++ b/BUILD.txt @@ -0,0 +1,88 @@ +In order to build MMDVM, you need to tell the Arduino GUI to link with the +CMSIS DSP library, which is doesn't do by default. + +My development is on Windows and so I can vouch for the following instructions. + +For Arduino 1.6.3 with SAM 1.6.4 +-------------------------------- + +1. Go to the where the platform.txt file is located. On my Windows machine it's +in: + +C:\Users\Jonathan\AppData\Roaming\Arduino15\packages\arduino\hardware\sam\1.6.4 + +On Mac OS X it's located in: + +/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam + +2. You'll need to open the file in a text editor and find the line: + +## Combine gc-sections, archives, and objects +recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} -mcpu={build.mcu} "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" {compiler.c.elf.extra_flags} -o "{build.path}/{build.project_name}.elf" "-L{build.path}" -mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--entry=Reset_Handler -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -Wl,--warn-unresolved-symbols -Wl,--start-group "{build.path}/syscalls_sam3.c.o" {object_files} "{build.variant.path}/{build.variant_system_lib}" "{build.path}/{archive_file}" -Wl,--end-group -lm -gcc + +In my version it's line 73. + +3. Modify it to read as follows: + +## Combine gc-sections, archives, and objects +recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} -mcpu={build.mcu} "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" {compiler.c.elf.extra_flags} -o "{build.path}/{build.project_name}.elf" "-L{build.path}" -mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--entry=Reset_Handler -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -Wl,--warn-unresolved-symbols -Wl,--start-group "{build.path}/syscalls_sam3.c.o" {object_files} "{build.variant.path}/{build.variant_system_lib}" "{build.system.path}/CMSIS/CMSIS/Lib/ARM/arm_cortexM3l_math.lib" "{build.path}/{archive_file}" -Wl,--end-group -lm -gcc + +The change is near the end, and is the addition of: + +"{build.system.path}/CMSIS/CMSIS/Lib/ARM/arm_cortexM3l_math.lib" + +Which is the CMSIS AMR3 DSP library for little-endian operation. + +4. Save the file and start up the Arduino GUI and build MMDVM. + +I would like to get instructions for doing the same on a Linux platform. As a +starter find the relevent platform.txt and try adding: + +"{build.system.path}/CMSIS/CMSIS/Lib/ARM/libarm_cortexM3l_math.a" + +or maybe: + +"{build.variant.path}/libarm_cortexM3l_math.a" + +For Arduino 1.6.7 with SAM 1.6.6 +-------------------------------- + +1. Go to the where the platform.txt file is located. On my Windows machine it's +in: + +C:\Users\Jonathan\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.6 + +On Mac OS X it's located in: + +/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam + +2. You'll need to open the file in a text editor and find the line: + + +## Combine gc-sections, archives, and objects +recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" -mcpu={build.mcu} -mthumb {compiler.c.elf.flags} "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" {compiler.c.elf.extra_flags} -o "{build.path}/{build.project_name}.elf" "-L{build.path}" -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--entry=Reset_Handler -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -Wl,--start-group "{build.path}/core/syscalls_sam3.c.o" {object_files} "{build.variant.path}/{build.variant_system_lib}" "{build.path}/{archive_file}" -Wl,--end-group -lm -gcc + +In my version it's line 73. + +3. Modify it to read as follows: + + +## Combine gc-sections, archives, and objects +recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" -mcpu={build.mcu} -mthumb {compiler.c.elf.flags} "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" {compiler.c.elf.extra_flags} -o "{build.path}/{build.project_name}.elf" "-L{build.path}" -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--entry=Reset_Handler -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -Wl,--start-group "{build.path}/core/syscalls_sam3.c.o" {object_files} "{build.variant.path}/{build.variant_system_lib}" "{build.system.path}/CMSIS/CMSIS/Lib/ARM/arm_cortexM3l_math.lib" "{build.path}/{archive_file}" -Wl,--end-group -lm -gcc + +The change is near the end, and is the addition of: + +"{build.system.path}/CMSIS/CMSIS/Lib/ARM/arm_cortexM3l_math.lib" + +Which is the CMSIS AMR3 DSP library for little-endian operation. + +4. Save the file and start up the Arduino GUI and build MMDVM. + +I would like to get instructions for doing the same on a Linux platform. As a +starter find the relevent platform.txt and try adding: + +"{build.system.path}/CMSIS/CMSIS/Lib/ARM/libarm_cortexM3l_math.a" + +or maybe: + +"{build.variant.path}/libarm_cortexM3l_math.a" diff --git a/CalRX.cpp b/CalRX.cpp new file mode 100644 index 0000000..c37f191 --- /dev/null +++ b/CalRX.cpp @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2009-2015 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. + */ + +#include "Config.h" +#include "Globals.h" +#include "CalRX.h" +#include "Utils.h" + +const unsigned int BUFFER_LENGTH = 200U; + +const uint32_t PLLMAX = 0x10000U; +const uint32_t PLLINC = PLLMAX / DSTAR_RADIO_BIT_LENGTH; +const uint32_t INC = PLLINC / 32U; + +// D-Star bit order version of 0x55 0x2D 0x16 +const uint32_t DATA_SYNC_DATA1 = 0x00AAB468U; +const uint32_t DATA_SYNC_DATA2 = 0x00554B97U; +const uint32_t DATA_SYNC_MASK = 0x00FFFFFFU; +const uint8_t DATA_SYNC_ERRS = 2U; + +CCalRX::CCalRX() : +m_pll(0U), +m_prev(false), +m_patternBuffer(0x00U), +m_rxBuffer(), +m_ptr(0U) +{ +} + +void CCalRX::samples(const q15_t* samples, uint8_t length) +{ + for (uint16_t i = 0U; i < length; i++) { + bool bit = samples[i] < 0; + + if (bit != m_prev) { + if (m_pll < (PLLMAX / 2U)) + m_pll += INC; + else + m_pll -= INC; + } + + m_prev = bit; + + m_pll += PLLINC; + + if (m_pll >= PLLMAX) { + m_pll -= PLLMAX; + process(samples[i]); + } + } +} + +void CCalRX::process(q15_t value) +{ + m_patternBuffer <<= 1; + if (value < 0) + m_patternBuffer |= 0x01U; + + m_rxBuffer[m_ptr++] = value; + if (m_ptr >= 24U) + m_ptr = 0U; + + if (countBits32((m_patternBuffer & DATA_SYNC_MASK) ^ DATA_SYNC_DATA1) <= DATA_SYNC_ERRS) { + q15_t max = -16000; + q15_t min = 16000; + + for (uint8_t i = 0U; i < 24U; i++) { + q15_t value = m_rxBuffer[i]; + + if (value > max) + max = value; + if (value < min) + min = value; + } + + if ((max - min) > 5) { + uint8_t buffer[5U]; + buffer[0U] = 0x00U; + buffer[1U] = (max >> 8) & 0xFFU; + buffer[2U] = (max >> 0) & 0xFFU; + buffer[3U] = (min >> 8) & 0xFFU; + buffer[4U] = (min >> 0) & 0xFFU; + + serial.writeCalData(buffer, 5U); + } + } + + if (countBits32((m_patternBuffer & DATA_SYNC_MASK) ^ DATA_SYNC_DATA2) <= DATA_SYNC_ERRS) { + q15_t max = -16000; + q15_t min = 16000; + + for (uint8_t i = 0U; i < 24U; i++) { + q15_t value = m_rxBuffer[i]; + + if (value > max) + max = value; + if (value < min) + min = value; + } + + if ((max - min) > 5) { + uint8_t buffer[5U]; + buffer[0U] = 0x80U; + buffer[1U] = (max >> 8) & 0xFFU; + buffer[2U] = (max >> 0) & 0xFFU; + buffer[3U] = (min >> 8) & 0xFFU; + buffer[4U] = (min >> 0) & 0xFFU; + + serial.writeCalData(buffer, 5U); + } + } +} + diff --git a/CalRX.h b/CalRX.h new file mode 100644 index 0000000..cc1ba67 --- /dev/null +++ b/CalRX.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2015 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(CALRX_H) +#define CALRX_H + +#include "Config.h" +#include "DStarDefines.h" + +class CCalRX { +public: + CCalRX(); + + void samples(const q15_t* samples, uint8_t length); + +private: + uint32_t m_pll; + bool m_prev; + uint32_t m_patternBuffer; + q15_t m_rxBuffer[3U * 8U]; + uint8_t m_ptr; + + void process(q15_t value); +}; + +#endif + diff --git a/CalTX.cpp b/CalTX.cpp new file mode 100644 index 0000000..4534d1e --- /dev/null +++ b/CalTX.cpp @@ -0,0 +1,183 @@ +/* + * Copyright (C) 2009-2015 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. + */ + +#include "Config.h" +#include "Globals.h" +#include "CalTX.h" + +const uint8_t HEADER[] = {0x00U, 0x00U, 0x00U, 'D', 'I', 'R', 'E', 'C', 'T', ' ', ' ', + 'D', 'I', 'R', 'E', 'C', 'T', ' ', ' ', + 'C', 'Q', 'C', 'Q', 'C', 'Q', ' ', ' ', + 'M', 'M', 'D', 'V', 'M', ' ', ' ', ' ', + 'T', 'E', 'S', 'T', 0xA9U, 0x6AU}; + +const uint8_t SLOW_DATA_TEXT[] = {'M', 'M', 'D', 'V', 'M', ' ', 'M', 'o', 'd', 'e', 'm', ' ', 'T', 'e', 's', 't', ' ', ' ', ' ', ' '}; + +CCalTX::CCalTX() : +m_transmit(false), +m_count(0U) +{ +} + +void CCalTX::process() +{ + dstarTX.process(); + + if (!m_transmit) + return; + + uint16_t space = dstarTX.getSpace(); + if (space < 5U) + return; + + uint8_t buffer[DSTAR_DATA_LENGTH_BYTES]; + for (uint8_t i = 0U; i < DSTAR_DATA_LENGTH_BYTES; i++) + buffer[i] = DSTAR_DATA_SYNC_BYTES[i]; + + if ((m_count % 21U) == 0U) { + // Sync is already included + } else if (m_count == 1U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ (DSTAR_SLOW_DATA_TYPE_TEXT | 0U); + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ SLOW_DATA_TEXT[0U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ SLOW_DATA_TEXT[1U]; + } else if (m_count == 2U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ SLOW_DATA_TEXT[2U]; + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ SLOW_DATA_TEXT[3U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ SLOW_DATA_TEXT[4U]; + } else if (m_count == 3U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ (DSTAR_SLOW_DATA_TYPE_TEXT | 1U); + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ SLOW_DATA_TEXT[5U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ SLOW_DATA_TEXT[6U]; + } else if (m_count == 4U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ SLOW_DATA_TEXT[7U]; + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ SLOW_DATA_TEXT[8U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ SLOW_DATA_TEXT[9U]; + } else if (m_count == 5U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ (DSTAR_SLOW_DATA_TYPE_TEXT | 2U); + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ SLOW_DATA_TEXT[10U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ SLOW_DATA_TEXT[11U]; + } else if (m_count == 6U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ SLOW_DATA_TEXT[12U]; + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ SLOW_DATA_TEXT[13U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ SLOW_DATA_TEXT[14U]; + } else if (m_count == 7U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ (DSTAR_SLOW_DATA_TYPE_TEXT | 3U); + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ SLOW_DATA_TEXT[15U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ SLOW_DATA_TEXT[16U]; + } else if (m_count == 8U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ SLOW_DATA_TEXT[17U]; + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ SLOW_DATA_TEXT[18U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ SLOW_DATA_TEXT[19U]; + } else if (m_count != 1U && (m_count % 21U) == 1U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ (DSTAR_SLOW_DATA_TYPE_HEADER | 5U); + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[0U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[1U]; + } else if (m_count != 2U && (m_count % 21U) == 2U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ HEADER[2U]; + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[3U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[4U]; + } else if (m_count != 3U && (m_count % 21U) == 3U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ (DSTAR_SLOW_DATA_TYPE_HEADER | 5U); + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[5U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[6U]; + } else if (m_count != 4U && (m_count % 21U) == 4U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ HEADER[7U]; + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[8U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[9U]; + } else if (m_count != 5U && (m_count % 21U) == 5U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ (DSTAR_SLOW_DATA_TYPE_HEADER | 5U); + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[10U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[11U]; + } else if (m_count != 6U && (m_count % 21U) == 6U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ HEADER[12U]; + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[13U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[14U]; + } else if (m_count != 7U && (m_count % 21U) == 7U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ (DSTAR_SLOW_DATA_TYPE_HEADER | 5U); + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[15U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[16U]; + } else if (m_count != 8U && (m_count % 21U) == 8U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ HEADER[17U]; + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[18U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[19U]; + } else if (m_count != 9U && (m_count % 21U) == 9U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ (DSTAR_SLOW_DATA_TYPE_HEADER | 5U); + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[20U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[21U]; + } else if (m_count != 10U && (m_count % 21U) == 10U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ HEADER[22U]; + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[23U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[24U]; + } else if (m_count != 11U && (m_count % 21U) == 11U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ (DSTAR_SLOW_DATA_TYPE_HEADER | 5U); + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[25U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[26U]; + } else if (m_count != 12U && (m_count % 21U) == 12U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ HEADER[27U]; + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[28U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[29U]; + } else if (m_count != 13U && (m_count % 21U) == 13U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ (DSTAR_SLOW_DATA_TYPE_HEADER | 5U); + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[30U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[31U]; + } else if (m_count != 14U && (m_count % 21U) == 14U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ HEADER[32U]; + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[33U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[34U]; + } else if (m_count != 15U && (m_count % 21U) == 15U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ (DSTAR_SLOW_DATA_TYPE_HEADER | 5U); + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[35U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[36U]; + } else if (m_count != 16U && (m_count % 21U) == 16U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ HEADER[37U]; + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[38U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ HEADER[39U]; + } else if (m_count != 17U && (m_count % 21U) == 17U) { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ (DSTAR_SLOW_DATA_TYPE_HEADER | 1U); + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ HEADER[40U]; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ 'f'; + } else { + buffer[9U] = DSTAR_SCRAMBLER_BYTES[0U] ^ 'f'; + buffer[10U] = DSTAR_SCRAMBLER_BYTES[1U] ^ 'f'; + buffer[11U] = DSTAR_SCRAMBLER_BYTES[2U] ^ 'f'; + } + + dstarTX.writeData(buffer, DSTAR_DATA_LENGTH_BYTES); + + m_count = (m_count + 1U) % (30U * 21U); +} + +uint8_t CCalTX::write(const uint8_t* data, uint8_t length) +{ + if (length != 1U) + return 4U; + + bool transmit = data[0U] == 1U; + + if (transmit && !m_transmit) { + m_count = 0U; + dstarTX.writeHeader(HEADER, DSTAR_HEADER_LENGTH_BYTES); + } else if (!transmit && m_transmit) { + dstarTX.writeEOT(); + } + + m_transmit = transmit; + + return 0U; +} + diff --git a/CalTX.h b/CalTX.h new file mode 100644 index 0000000..cd6a374 --- /dev/null +++ b/CalTX.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2015 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(CALTX_H) +#define CALTX_H + +#include "Config.h" +#include "DStarDefines.h" + +class CCalTX { +public: + CCalTX(); + + uint8_t write(const uint8_t* data, uint8_t length); + + void process(); + +private: + bool m_transmit; + uint16_t m_count; +}; + +#endif + diff --git a/Config.h b/Config.h new file mode 100644 index 0000000..7b71272 --- /dev/null +++ b/Config.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2015 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(CONFIG_H) +#define CONFIG_H + +#define WANT_DEBUG + +// For the original Arduino Due pin layout +#define ARDUINO_DUE_PAPA + +// For the new Arduino Due pin layout +// #define ARDUINO_DUE_ZUM + +// For the SP8NTH board +// #define ARDUINO_DUE_NTH + +#endif + diff --git a/DMRDefines.h b/DMRDefines.h new file mode 100644 index 0000000..c839bd5 --- /dev/null +++ b/DMRDefines.h @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2009-2015 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(DMRDEFINES_H) +#define DMRDEFINES_H + +const unsigned int DMR_RADIO_SYMBOL_LENGTH = 5U; // At 24 kHz sample rate + +const unsigned int DMR_FRAME_LENGTH_BYTES = 33U; +const unsigned int DMR_FRAME_LENGTH_BITS = DMR_FRAME_LENGTH_BYTES * 8U; +const unsigned int DMR_FRAME_LENGTH_SYMBOLS = DMR_FRAME_LENGTH_BYTES * 4U; +const unsigned int DMR_FRAME_LENGTH_SAMPLES = DMR_FRAME_LENGTH_SYMBOLS * DMR_RADIO_SYMBOL_LENGTH; + +const unsigned int DMR_SYNC_LENGTH_BYTES = 6U; +const unsigned int DMR_SYNC_LENGTH_BITS = DMR_SYNC_LENGTH_BYTES * 8U; +const unsigned int DMR_SYNC_LENGTH_SYMBOLS = DMR_SYNC_LENGTH_BYTES * 4U; +const unsigned int DMR_SYNC_LENGTH_SAMPLES = DMR_SYNC_LENGTH_SYMBOLS * DMR_RADIO_SYMBOL_LENGTH; + +const unsigned int DMR_EMB_LENGTH_BITS = 16U; +const unsigned int DMR_EMB_LENGTH_SYMBOLS = 8U; +const unsigned int DMR_EMB_LENGTH_SAMPLES = DMR_EMB_LENGTH_SYMBOLS * DMR_RADIO_SYMBOL_LENGTH; + +const unsigned int DMR_SLOT_TYPE_LENGTH_BITS = 20U; +const unsigned int DMR_SLOT_TYPE_LENGTH_SYMBOLS = 10U; +const unsigned int DMR_SLOT_TYPE_LENGTH_SAMPLES = DMR_SLOT_TYPE_LENGTH_SYMBOLS * DMR_RADIO_SYMBOL_LENGTH; + +const unsigned int DMR_INFO_LENGTH_BITS = 196U; +const unsigned int DMR_INFO_LENGTH_SYMBOLS = 98U; +const unsigned int DMR_INFO_LENGTH_SAMPLES = DMR_INFO_LENGTH_SYMBOLS * DMR_RADIO_SYMBOL_LENGTH; + +const unsigned int DMR_AUDIO_LENGTH_BITS = 216U; +const unsigned int DMR_AUDIO_LENGTH_SYMBOLS = 108U; +const unsigned int DMR_AUDIO_LENGTH_SAMPLES = DMR_AUDIO_LENGTH_SYMBOLS * DMR_RADIO_SYMBOL_LENGTH; + +const unsigned int DMR_CACH_LENGTH_BYTES = 3U; +const unsigned int DMR_CACH_LENGTH_BITS = DMR_CACH_LENGTH_BYTES * 8U; +const unsigned int DMR_CACH_LENGTH_SYMBOLS = DMR_CACH_LENGTH_BYTES * 4U; +const unsigned int DMR_CACH_LENGTH_SAMPLES = DMR_CACH_LENGTH_SYMBOLS * DMR_RADIO_SYMBOL_LENGTH; + +const uint8_t DMR_SYNC_BYTES_LENGTH = 7U; +const uint8_t DMR_MS_DATA_SYNC_BYTES[] = {0x0DU, 0x5DU, 0x7FU, 0x77U, 0xFDU, 0x75U, 0x70U}; +const uint8_t DMR_MS_VOICE_SYNC_BYTES[] = {0x07U, 0xF7U, 0xD5U, 0xDDU, 0x57U, 0xDFU, 0xD0U}; +const uint8_t DMR_BS_DATA_SYNC_BYTES[] = {0x0DU, 0xFFU, 0x57U, 0xD7U, 0x5DU, 0xF5U, 0xD0U}; +const uint8_t DMR_BS_VOICE_SYNC_BYTES[] = {0x07U, 0x55U, 0xFDU, 0x7DU, 0xF7U, 0x5FU, 0x70U}; +const uint8_t DMR_SYNC_BYTES_MASK[] = {0x0FU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xF0U}; + +const uint64_t DMR_MS_DATA_SYNC_BITS = 0x0000D5D7F77FD757U; +const uint64_t DMR_MS_VOICE_SYNC_BITS = 0x00007F7D5DD57DFDU; +const uint64_t DMR_BS_DATA_SYNC_BITS = 0x0000DFF57D75DF5DU; +const uint64_t DMR_BS_VOICE_SYNC_BITS = 0x0000755FD7DF75F7U; +const uint64_t DMR_SYNC_BITS_MASK = 0x0000FFFFFFFFFFFFU; + +const uint32_t DMR_MS_DATA_SYNC_SYMBOLS = 0x0076286EU; +const uint32_t DMR_MS_VOICE_SYNC_SYMBOLS = 0x0089D791U; +const uint32_t DMR_BS_DATA_SYNC_SYMBOLS = 0x00439B4DU; +const uint32_t DMR_BS_VOICE_SYNC_SYMBOLS = 0x00BC64B2U; +const uint32_t DMR_SYNC_SYMBOLS_MASK = 0x00FFFFFFU; + +const uint8_t DT_VOICE_PI_HEADER = 0U; +const uint8_t DT_VOICE_LC_HEADER = 1U; +const uint8_t DT_TERMINATOR_WITH_LC = 2U; +const uint8_t DT_CSBK = 3U; +const uint8_t DT_IDLE = 9U; +// All others are for data transfer + +#endif + diff --git a/DMRIdleRX.cpp b/DMRIdleRX.cpp new file mode 100644 index 0000000..039d720 --- /dev/null +++ b/DMRIdleRX.cpp @@ -0,0 +1,199 @@ +/* + * Copyright (C) 2009-2016 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. + */ + +#include "Config.h" +#include "Globals.h" +#include "DMRIdleRX.h" +#include "DMRSlotType.h" +#include "Utils.h" + +const q15_t SCALING_FACTOR = 19505; // Q15(0.60) + +const uint8_t MAX_SYNC_SYMBOLS_ERRS = 2U; +const uint8_t MAX_SYNC_BYTES_ERRS = 3U; + +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]) + +CDMRIdleRX::CDMRIdleRX() : +m_bitBuffer(), +m_buffer(), +m_bitPtr(0U), +m_dataPtr(0U), +m_endPtr(999U), +m_maxCorr(0), +m_centre(0), +m_threshold(0), +m_colorCode(0U) +{ +} + +void CDMRIdleRX::reset() +{ + m_dataPtr = 0U; + m_bitPtr = 0U; + m_maxCorr = 0; + m_threshold = 0; + m_centre = 0; + m_endPtr = 999U; +} + +void CDMRIdleRX::samples(const q15_t* samples, uint8_t length) +{ + for (uint8_t i = 0U; i < length; i++) + processSample(samples[i]); +} + +void CDMRIdleRX::processSample(q15_t sample) +{ + m_bitBuffer[m_bitPtr] <<= 1; + if (sample < 0) + m_bitBuffer[m_bitPtr] |= 0x01U; + + m_buffer[m_dataPtr] = sample; + + if (countBits32((m_bitBuffer[m_bitPtr] & DMR_SYNC_SYMBOLS_MASK) ^ DMR_MS_DATA_SYNC_SYMBOLS) <= MAX_SYNC_SYMBOLS_ERRS) { + uint16_t ptr = m_dataPtr + DMR_FRAME_LENGTH_SAMPLES - DMR_SYNC_LENGTH_SAMPLES + DMR_RADIO_SYMBOL_LENGTH; + if (ptr >= DMR_FRAME_LENGTH_SAMPLES) + ptr -= DMR_FRAME_LENGTH_SAMPLES; + + q31_t corr = 0; + q15_t max = -16000; + q15_t min = 16000; + + uint32_t mask = 0x00800000U; + for (uint8_t i = 0U; i < DMR_SYNC_LENGTH_SYMBOLS; i++, mask >>= 1) { + bool b = (DMR_MS_DATA_SYNC_SYMBOLS & mask) == mask; + + if (m_buffer[ptr] > max) + max = m_buffer[ptr]; + if (m_buffer[ptr] < min) + min = m_buffer[ptr]; + + corr += b ? -m_buffer[ptr] : m_buffer[ptr]; + + ptr += DMR_RADIO_SYMBOL_LENGTH; + if (ptr >= DMR_FRAME_LENGTH_SAMPLES) + ptr -= DMR_FRAME_LENGTH_SAMPLES; + } + + if (corr > m_maxCorr) { + q15_t centre = (max + min) >> 1; + + q31_t v1 = (max - centre) * SCALING_FACTOR; + q15_t threshold = q15_t(v1 >> 15); + + uint8_t sync[DMR_SYNC_BYTES_LENGTH]; + + uint16_t ptr = m_dataPtr + DMR_FRAME_LENGTH_SAMPLES - DMR_SYNC_LENGTH_SAMPLES + DMR_RADIO_SYMBOL_LENGTH; + if (ptr >= DMR_FRAME_LENGTH_SAMPLES) + ptr -= DMR_FRAME_LENGTH_SAMPLES; + + samplesToBits(ptr, DMR_SYNC_LENGTH_SYMBOLS, sync, 4U, centre, threshold); + + uint8_t errs = 0U; + for (uint8_t i = 0U; i < DMR_SYNC_BYTES_LENGTH; i++) + errs += countBits8((sync[i] & DMR_SYNC_BYTES_MASK[i]) ^ DMR_MS_DATA_SYNC_BYTES[i]); + + if (errs <= MAX_SYNC_BYTES_ERRS) { + DEBUG4("DMRIdleRX: data sync found pos/centre/threshold", m_dataPtr, centre, threshold); + m_maxCorr = corr; + m_centre = centre; + m_threshold = threshold; + + m_endPtr = m_dataPtr + DMR_SLOT_TYPE_LENGTH_SAMPLES / 2U + DMR_INFO_LENGTH_SAMPLES / 2U - 1U; + if (m_endPtr >= DMR_FRAME_LENGTH_SAMPLES) + m_endPtr -= DMR_FRAME_LENGTH_SAMPLES; + } + } + } + + if (m_dataPtr == m_endPtr) { + uint16_t ptr = m_endPtr + DMR_RADIO_SYMBOL_LENGTH + 1U; + if (ptr >= DMR_FRAME_LENGTH_SAMPLES) + ptr -= DMR_FRAME_LENGTH_SAMPLES; + + uint8_t frame[DMR_FRAME_LENGTH_BYTES + 1U]; + samplesToBits(ptr, DMR_FRAME_LENGTH_SYMBOLS, frame, 8U, m_centre, m_threshold); + + uint8_t colorCode; + uint8_t dataType; + CDMRSlotType slotType; + slotType.decode(frame + 1U, colorCode, dataType); + + if (colorCode == m_colorCode && dataType == DT_CSBK) { + frame[0U] = 0x80U | 0x40U | DT_CSBK; // Idle RX, Data Sync, CSBK + serial.writeDMRData(false, frame, DMR_FRAME_LENGTH_BYTES + 1U); +#if defined(WANT_DEBUG) + } else { + DEBUG5("DMRIdleRX: invalid color code or data type", colorCode, m_colorCode, dataType, DT_CSBK); +#endif + } + + m_endPtr = 999U; + m_maxCorr = 0U; + } + + m_dataPtr++; + if (m_dataPtr >= DMR_FRAME_LENGTH_SAMPLES) + m_dataPtr = 0U; + + m_bitPtr++; + if (m_bitPtr >= DMR_RADIO_SYMBOL_LENGTH) + m_bitPtr = 0U; +} + +void CDMRIdleRX::samplesToBits(uint16_t start, uint8_t count, uint8_t* buffer, uint16_t offset, q15_t centre, q15_t threshold) +{ + for (uint8_t i = 0U; i < count; i++) { + q15_t sample = m_buffer[start] - centre; + + if (sample < -threshold) { + WRITE_BIT1(buffer, offset, false); + offset++; + WRITE_BIT1(buffer, offset, true); + offset++; + } else if (sample < 0) { + WRITE_BIT1(buffer, offset, false); + offset++; + WRITE_BIT1(buffer, offset, false); + offset++; + } else if (sample < threshold) { + WRITE_BIT1(buffer, offset, true); + offset++; + WRITE_BIT1(buffer, offset, false); + offset++; + } else { + WRITE_BIT1(buffer, offset, true); + offset++; + WRITE_BIT1(buffer, offset, true); + offset++; + } + + start += DMR_RADIO_SYMBOL_LENGTH; + if (start >= DMR_FRAME_LENGTH_SAMPLES) + start -= DMR_FRAME_LENGTH_SAMPLES; + } +} + +void CDMRIdleRX::setColorCode(uint8_t colorCode) +{ + m_colorCode = colorCode; +} + diff --git a/DMRIdleRX.h b/DMRIdleRX.h new file mode 100644 index 0000000..fbb82fa --- /dev/null +++ b/DMRIdleRX.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2015 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(DMRIDLERX_H) +#define DMRIDLERX_H + +#include "Config.h" +#include "DMRDefines.h" + +class CDMRIdleRX { +public: + CDMRIdleRX(); + + void samples(const q15_t* samples, uint8_t length); + + void setColorCode(uint8_t colorCode); + + void reset(); + +private: + uint32_t m_bitBuffer[DMR_RADIO_SYMBOL_LENGTH]; + q15_t m_buffer[DMR_FRAME_LENGTH_SAMPLES]; + uint16_t m_bitPtr; + uint16_t m_dataPtr; + uint16_t m_endPtr; + q31_t m_maxCorr; + q15_t m_centre; + q15_t m_threshold; + uint8_t m_colorCode; + + void processSample(q15_t sample); + void samplesToBits(uint16_t start, uint8_t count, uint8_t* buffer, uint16_t offset, q15_t centre, q15_t threshold); +}; + +#endif + diff --git a/DMRRX.cpp b/DMRRX.cpp new file mode 100644 index 0000000..ba3ea0c --- /dev/null +++ b/DMRRX.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2009-2015 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. + */ + +#include "Config.h" +#include "Globals.h" +#include "DMRRX.h" + +CDMRRX::CDMRRX() : +m_slot1RX(false), +m_slot2RX(true), +m_slotNo(0U), +m_dcd1(false), +m_dcd2(false) +{ +} + +void CDMRRX::samples(const q15_t* samples, const uint8_t* control, uint8_t length) +{ + for (uint16_t i = 0U; i < length; i++) { + if (control[i] == MARK_SLOT1 && m_slotNo != 1U) { + m_slot2RX.stop(); + m_slot1RX.start(); + m_slotNo = 1U; + } else if (control[i] == MARK_SLOT2 && m_slotNo != 2U) { + m_slot1RX.stop(); + m_slot2RX.start(); + m_slotNo = 2U; + } else if (control[i] == MARK_END && m_slotNo != 0U) { + if (m_slotNo == 1U) + m_slot1RX.stop(); + if (m_slotNo == 2U) + m_slot2RX.stop(); + m_slotNo = 0U; + } + + switch (m_slotNo) { + case 1U: + m_dcd1 = m_slot1RX.processSample(samples[i]); + break; + case 2U: + m_dcd2 = m_slot2RX.processSample(samples[i]); + break; + default: + break; + } + } + + io.setDecode(m_dcd1 || m_dcd2); +} + +void CDMRRX::setColorCode(uint8_t colorCode) +{ + m_slot1RX.setColorCode(colorCode); + m_slot2RX.setColorCode(colorCode); +} + +void CDMRRX::reset() +{ + m_slot1RX.reset(); + m_slot2RX.reset(); + m_slotNo = 0U; +} + diff --git a/DMRRX.h b/DMRRX.h new file mode 100644 index 0000000..04a6cd8 --- /dev/null +++ b/DMRRX.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2015 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(DMRRX_H) +#define DMRRX_H + +#include "Config.h" +#include "DMRSlotRX.h" + +class CDMRRX { +public: + CDMRRX(); + + void samples(const q15_t* samples, const uint8_t* control, uint8_t length); + + void setColorCode(uint8_t colorCode); + + void reset(); + +private: + CDMRSlotRX m_slot1RX; + CDMRSlotRX m_slot2RX; + uint8_t m_slotNo; + bool m_dcd1; + bool m_dcd2; +}; + +#endif + diff --git a/DMRSlotRX.cpp b/DMRSlotRX.cpp new file mode 100644 index 0000000..577e12e --- /dev/null +++ b/DMRSlotRX.cpp @@ -0,0 +1,279 @@ +/* + * Copyright (C) 2009-2016 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. + */ + +#include "Config.h" +#include "Globals.h" +#include "DMRSlotRX.h" +#include "DMRSlotType.h" +#include "Utils.h" + +const q15_t SCALING_FACTOR = 19505; // Q15(0.60) + +const uint8_t MAX_SYNC_SYMBOLS_ERRS = 2U; +const uint8_t MAX_SYNC_BYTES_ERRS = 3U; + +const uint8_t MAX_SYNC_LOST_FRAMES = 13U; + +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]) + +CDMRSlotRX::CDMRSlotRX(bool slot) : +m_slot(slot), +m_bitBuffer(), +m_buffer(), +m_bitPtr(0U), +m_dataPtr(0U), +m_endPtr(999U), +m_maxCorr(0), +m_centre(0), +m_threshold(0), +m_control(0x00U), +m_syncCount(0U), +m_colorCode(0U), +m_receiving(false), +m_n(0U) +{ +} + +void CDMRSlotRX::start() +{ + m_dataPtr = 0U; + m_bitPtr = 0U; + m_maxCorr = 0; + m_control = 0x00U; + + if (m_receiving) + m_syncCount++; +} + +void CDMRSlotRX::stop() +{ + if (m_receiving && m_syncCount >= MAX_SYNC_LOST_FRAMES) { + DEBUG2("DMRSlotRX: lost for slot", m_slot ? 2U : 1U); + serial.writeDMRLost(m_slot); + m_receiving = false; + m_syncCount = 0U; + m_threshold = 0; + m_centre = 0; + m_endPtr = 999U; + } +} + +void CDMRSlotRX::reset() +{ + m_receiving = false; + m_dataPtr = 0U; + m_bitPtr = 0U; + m_maxCorr = 0; + m_control = 0x00U; + m_syncCount = 0U; + m_threshold = 0; + m_centre = 0; + m_endPtr = 999U; +} + +bool CDMRSlotRX::processSample(q15_t sample) +{ + // Ensure that the buffer doesn't overflow + if (m_dataPtr > m_endPtr || m_dataPtr >= 900U) + return m_receiving; + + m_buffer[m_dataPtr] = sample; + + // The approximate position of the sync samples, XXX to be updated later + // XXX change range when m_endPtr is set, make it tighter. + if (m_dataPtr >= 160U && m_dataPtr <= 530U) + correlateSync(sample); + + if (m_dataPtr == m_endPtr) { + uint8_t frame[DMR_FRAME_LENGTH_BYTES + 1U]; + frame[0U] = m_control; + + uint16_t ptr = m_endPtr - DMR_FRAME_LENGTH_SAMPLES + DMR_RADIO_SYMBOL_LENGTH + 1U; + samplesToBits(ptr, DMR_FRAME_LENGTH_SYMBOLS, frame, 8U, m_centre, m_threshold); + + if (m_control == 0x40U) { + // Data sync + uint8_t colorCode; + uint8_t dataType; + CDMRSlotType slotType; + slotType.decode(frame + 1U, colorCode, dataType); + + frame[0U] |= dataType; + + switch (dataType) { + case DT_VOICE_LC_HEADER: + DEBUG3("DMRSlotRX: LC header for slot with color code", m_slot ? 2U : 1U, colorCode); + m_receiving = true; + m_syncCount = 0U; + m_n = 0U; + serial.writeDMRData(m_slot, frame, DMR_FRAME_LENGTH_BYTES + 1U); + break; + case DT_VOICE_PI_HEADER: + DEBUG3("DMRSlotRX: PI header for slot with color code", m_slot ? 2U : 1U, colorCode); + m_receiving = true; + m_syncCount = 0U; + m_n = 0U; + serial.writeDMRData(m_slot, frame, DMR_FRAME_LENGTH_BYTES + 1U); + break; + case DT_TERMINATOR_WITH_LC: + DEBUG3("DMRSlotRX: terminator for slot with color code", m_slot ? 2U : 1U, colorCode); + m_receiving = false; + m_endPtr = 999U; + serial.writeDMRData(m_slot, frame, DMR_FRAME_LENGTH_BYTES + 1U); + break; + default: + DEBUG4("DMRSlotRX: data sync for slot, type and color code", m_slot ? 2U : 1U, dataType, colorCode); + m_receiving = false; + serial.writeDMRData(m_slot, frame, DMR_FRAME_LENGTH_BYTES + 1U); + break; + } + } else if (m_control == 0x20U) { + // Voice sync + serial.writeDMRData(m_slot, frame, DMR_FRAME_LENGTH_BYTES + 1U); + m_receiving = true; + m_syncCount = 0U; + m_n = 0U; + } else { + // Voice data + frame[0U] |= ++m_n; + serial.writeDMRData(m_slot, frame, DMR_FRAME_LENGTH_BYTES + 1U); + } + } + + m_dataPtr++; + + return m_receiving; +} + +void CDMRSlotRX::correlateSync(q15_t sample) +{ + m_bitBuffer[m_bitPtr] <<= 1; + if (sample < 0) + m_bitBuffer[m_bitPtr] |= 0x01U; + + uint8_t errs = countBits32((m_bitBuffer[m_bitPtr] & DMR_SYNC_SYMBOLS_MASK) ^ DMR_MS_DATA_SYNC_SYMBOLS); + + // The voice sync is the complement of the data sync + bool data = (errs <= MAX_SYNC_SYMBOLS_ERRS); + bool voice = (errs >= (DMR_SYNC_LENGTH_SYMBOLS - MAX_SYNC_SYMBOLS_ERRS)); + + if (data || voice) { + uint16_t ptr = m_dataPtr - DMR_SYNC_LENGTH_SAMPLES + DMR_RADIO_SYMBOL_LENGTH; + + q31_t corr = 0; + q15_t min = 16000; + q15_t max = -16000; + + uint32_t mask = 0x00800000U; + for (uint8_t i = 0U; i < DMR_SYNC_LENGTH_SYMBOLS; i++, mask >>= 1) { + bool b = (DMR_MS_DATA_SYNC_SYMBOLS & mask) == mask; + + if (m_buffer[ptr] > max) + max = m_buffer[ptr]; + if (m_buffer[ptr] < min) + min = m_buffer[ptr]; + + if (data) + corr += b ? -m_buffer[ptr] : m_buffer[ptr]; + else // if (voice) + corr += b ? m_buffer[ptr] : -m_buffer[ptr]; + + ptr += DMR_RADIO_SYMBOL_LENGTH; + } + + if (corr > m_maxCorr) { + q15_t centre = (max + min) >> 1; + + q31_t v1 = (max - centre) * SCALING_FACTOR; + q15_t threshold = q15_t(v1 >> 15); + + uint8_t sync[DMR_SYNC_BYTES_LENGTH]; + uint16_t ptr = m_dataPtr - DMR_SYNC_LENGTH_SAMPLES + DMR_RADIO_SYMBOL_LENGTH; + samplesToBits(ptr, DMR_SYNC_LENGTH_SYMBOLS, sync, 4U, centre, threshold); + + if (data) { + uint8_t errs = 0U; + for (uint8_t i = 0U; i < DMR_SYNC_BYTES_LENGTH; i++) + errs += countBits8((sync[i] & DMR_SYNC_BYTES_MASK[i]) ^ DMR_MS_DATA_SYNC_BYTES[i]); + + if (errs <= MAX_SYNC_BYTES_ERRS) { + DEBUG5("DMRSlotRX: data sync found slot/pos/centre/threshold", m_slot ? 2U : 1U, m_dataPtr, centre, threshold); + m_maxCorr = corr; + m_centre = centre; + m_threshold = threshold; + m_control = 0x40U; + m_endPtr = m_dataPtr + DMR_SLOT_TYPE_LENGTH_SAMPLES / 2U + DMR_INFO_LENGTH_SAMPLES / 2U - 1U; + } + } else { // if (voice) + uint8_t errs = 0U; + for (uint8_t i = 0U; i < DMR_SYNC_BYTES_LENGTH; i++) + errs += countBits8((sync[i] & DMR_SYNC_BYTES_MASK[i]) ^ DMR_MS_VOICE_SYNC_BYTES[i]); + + if (errs <= MAX_SYNC_BYTES_ERRS) { + DEBUG5("DMRSlotRX: voice sync found slot/pos/centre/threshold", m_slot ? 2U : 1U, m_dataPtr, centre, threshold); + m_maxCorr = corr; + m_centre = centre; + m_threshold = threshold; + m_control = 0x20U; + m_endPtr = m_dataPtr + DMR_SLOT_TYPE_LENGTH_SAMPLES / 2U + DMR_INFO_LENGTH_SAMPLES / 2U - 1U; + } + } + } + } + + m_bitPtr++; + if (m_bitPtr >= DMR_RADIO_SYMBOL_LENGTH) + m_bitPtr = 0U; +} + +void CDMRSlotRX::samplesToBits(uint16_t start, uint8_t count, uint8_t* buffer, uint16_t offset, q15_t centre, q15_t threshold) +{ + for (uint8_t i = 0U; i < count; i++, start += DMR_RADIO_SYMBOL_LENGTH) { + q15_t sample = m_buffer[start] - centre; + + if (sample < -threshold) { + WRITE_BIT1(buffer, offset, false); + offset++; + WRITE_BIT1(buffer, offset, true); + offset++; + } else if (sample < 0) { + WRITE_BIT1(buffer, offset, false); + offset++; + WRITE_BIT1(buffer, offset, false); + offset++; + } else if (sample < threshold) { + WRITE_BIT1(buffer, offset, true); + offset++; + WRITE_BIT1(buffer, offset, false); + offset++; + } else { + WRITE_BIT1(buffer, offset, true); + offset++; + WRITE_BIT1(buffer, offset, true); + offset++; + } + } +} + +void CDMRSlotRX::setColorCode(uint8_t colorCode) +{ + m_colorCode = colorCode; +} + diff --git a/DMRSlotRX.h b/DMRSlotRX.h new file mode 100644 index 0000000..83635a3 --- /dev/null +++ b/DMRSlotRX.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2015 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(DMRSLOTRX_H) +#define DMRSLOTRX_H + +#include "Config.h" +#include "DMRDefines.h" + +class CDMRSlotRX { +public: + CDMRSlotRX(bool slot); + + void start(); + + bool processSample(q15_t sample); + + void stop(); + + void setColorCode(uint8_t colorCode); + + void reset(); + +private: + bool m_slot; + uint32_t m_bitBuffer[DMR_RADIO_SYMBOL_LENGTH]; + q15_t m_buffer[900U]; + uint16_t m_bitPtr; + uint16_t m_dataPtr; + uint16_t m_endPtr; + q31_t m_maxCorr; + q15_t m_centre; + q15_t m_threshold; + uint8_t m_control; + uint8_t m_syncCount; + uint8_t m_colorCode; + bool m_receiving; + uint8_t m_n; + + void correlateSync(q15_t sample); + void samplesToBits(uint16_t start, uint8_t count, uint8_t* buffer, uint16_t offset, q15_t centre, q15_t threshold); +}; + +#endif + diff --git a/DMRSlotType.cpp b/DMRSlotType.cpp new file mode 100644 index 0000000..2d89a74 --- /dev/null +++ b/DMRSlotType.cpp @@ -0,0 +1,287 @@ +/* + * Copyright (C) 2015 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. + */ + +#include "Config.h" +#include "Globals.h" +#include "DMRSlotType.h" + +const uint16_t ENCODING_TABLE_2087[] = + {0x0000U, 0xB08EU, 0xE093U, 0x501DU, 0x70A9U, 0xC027U, 0x903AU, 0x20B4U, 0x60DCU, 0xD052U, 0x804FU, 0x30C1U, + 0x1075U, 0xA0FBU, 0xF0E6U, 0x4068U, 0x7036U, 0xC0B8U, 0x90A5U, 0x202BU, 0x009FU, 0xB011U, 0xE00CU, 0x5082U, + 0x10EAU, 0xA064U, 0xF079U, 0x40F7U, 0x6043U, 0xD0CDU, 0x80D0U, 0x305EU, 0xD06CU, 0x60E2U, 0x30FFU, 0x8071U, + 0xA0C5U, 0x104BU, 0x4056U, 0xF0D8U, 0xB0B0U, 0x003EU, 0x5023U, 0xE0ADU, 0xC019U, 0x7097U, 0x208AU, 0x9004U, + 0xA05AU, 0x10D4U, 0x40C9U, 0xF047U, 0xD0F3U, 0x607DU, 0x3060U, 0x80EEU, 0xC086U, 0x7008U, 0x2015U, 0x909BU, + 0xB02FU, 0x00A1U, 0x50BCU, 0xE032U, 0x90D9U, 0x2057U, 0x704AU, 0xC0C4U, 0xE070U, 0x50FEU, 0x00E3U, 0xB06DU, + 0xF005U, 0x408BU, 0x1096U, 0xA018U, 0x80ACU, 0x3022U, 0x603FU, 0xD0B1U, 0xE0EFU, 0x5061U, 0x007CU, 0xB0F2U, + 0x9046U, 0x20C8U, 0x70D5U, 0xC05BU, 0x8033U, 0x30BDU, 0x60A0U, 0xD02EU, 0xF09AU, 0x4014U, 0x1009U, 0xA087U, + 0x40B5U, 0xF03BU, 0xA026U, 0x10A8U, 0x301CU, 0x8092U, 0xD08FU, 0x6001U, 0x2069U, 0x90E7U, 0xC0FAU, 0x7074U, + 0x50C0U, 0xE04EU, 0xB053U, 0x00DDU, 0x3083U, 0x800DU, 0xD010U, 0x609EU, 0x402AU, 0xF0A4U, 0xA0B9U, 0x1037U, + 0x505FU, 0xE0D1U, 0xB0CCU, 0x0042U, 0x20F6U, 0x9078U, 0xC065U, 0x70EBU, 0xA03DU, 0x10B3U, 0x40AEU, 0xF020U, + 0xD094U, 0x601AU, 0x3007U, 0x8089U, 0xC0E1U, 0x706FU, 0x2072U, 0x90FCU, 0xB048U, 0x00C6U, 0x50DBU, 0xE055U, + 0xD00BU, 0x6085U, 0x3098U, 0x8016U, 0xA0A2U, 0x102CU, 0x4031U, 0xF0BFU, 0xB0D7U, 0x0059U, 0x5044U, 0xE0CAU, + 0xC07EU, 0x70F0U, 0x20EDU, 0x9063U, 0x7051U, 0xC0DFU, 0x90C2U, 0x204CU, 0x00F8U, 0xB076U, 0xE06BU, 0x50E5U, + 0x108DU, 0xA003U, 0xF01EU, 0x4090U, 0x6024U, 0xD0AAU, 0x80B7U, 0x3039U, 0x0067U, 0xB0E9U, 0xE0F4U, 0x507AU, + 0x70CEU, 0xC040U, 0x905DU, 0x20D3U, 0x60BBU, 0xD035U, 0x8028U, 0x30A6U, 0x1012U, 0xA09CU, 0xF081U, 0x400FU, + 0x30E4U, 0x806AU, 0xD077U, 0x60F9U, 0x404DU, 0xF0C3U, 0xA0DEU, 0x1050U, 0x5038U, 0xE0B6U, 0xB0ABU, 0x0025U, + 0x2091U, 0x901FU, 0xC002U, 0x708CU, 0x40D2U, 0xF05CU, 0xA041U, 0x10CFU, 0x307BU, 0x80F5U, 0xD0E8U, 0x6066U, + 0x200EU, 0x9080U, 0xC09DU, 0x7013U, 0x50A7U, 0xE029U, 0xB034U, 0x00BAU, 0xE088U, 0x5006U, 0x001BU, 0xB095U, + 0x9021U, 0x20AFU, 0x70B2U, 0xC03CU, 0x8054U, 0x30DAU, 0x60C7U, 0xD049U, 0xF0FDU, 0x4073U, 0x106EU, 0xA0E0U, + 0x90BEU, 0x2030U, 0x702DU, 0xC0A3U, 0xE017U, 0x5099U, 0x0084U, 0xB00AU, 0xF062U, 0x40ECU, 0x10F1U, 0xA07FU, + 0x80CBU, 0x3045U, 0x6058U, 0xD0D6U}; + +const uint32_t DECODING_TABLE_1987[] = + {0x00000U, 0x00001U, 0x00002U, 0x00003U, 0x00004U, 0x00005U, 0x00006U, 0x00007U, 0x00008U, 0x00009U, 0x0000AU, 0x0000BU, 0x0000CU, + 0x0000DU, 0x0000EU, 0x24020U, 0x00010U, 0x00011U, 0x00012U, 0x00013U, 0x00014U, 0x00015U, 0x00016U, 0x00017U, 0x00018U, 0x00019U, + 0x0001AU, 0x0001BU, 0x0001CU, 0x0001DU, 0x48040U, 0x01480U, 0x00020U, 0x00021U, 0x00022U, 0x00023U, 0x00024U, 0x00025U, 0x00026U, + 0x24008U, 0x00028U, 0x00029U, 0x0002AU, 0x24004U, 0x0002CU, 0x24002U, 0x24001U, 0x24000U, 0x00030U, 0x00031U, 0x00032U, 0x08180U, + 0x00034U, 0x00C40U, 0x00036U, 0x00C42U, 0x00038U, 0x43000U, 0x0003AU, 0x43002U, 0x02902U, 0x24012U, 0x02900U, 0x24010U, 0x00040U, + 0x00041U, 0x00042U, 0x00043U, 0x00044U, 0x00045U, 0x00046U, 0x00047U, 0x00048U, 0x00049U, 0x0004AU, 0x02500U, 0x0004CU, 0x0004DU, + 0x48010U, 0x48011U, 0x00050U, 0x00051U, 0x00052U, 0x21200U, 0x00054U, 0x00C20U, 0x48008U, 0x48009U, 0x00058U, 0x00059U, 0x48004U, + 0x48005U, 0x48002U, 0x48003U, 0x48000U, 0x48001U, 0x00060U, 0x00061U, 0x00062U, 0x00063U, 0x00064U, 0x00C10U, 0x10300U, 0x0B000U, + 0x00068U, 0x00069U, 0x01880U, 0x01881U, 0x40181U, 0x40180U, 0x24041U, 0x24040U, 0x00070U, 0x00C04U, 0x00072U, 0x00C06U, 0x00C01U, + 0x00C00U, 0x00C03U, 0x00C02U, 0x05204U, 0x00C0CU, 0x48024U, 0x48025U, 0x05200U, 0x00C08U, 0x48020U, 0x48021U, 0x00080U, 0x00081U, + 0x00082U, 0x00083U, 0x00084U, 0x00085U, 0x00086U, 0x00087U, 0x00088U, 0x00089U, 0x0008AU, 0x50200U, 0x0008CU, 0x0A800U, 0x01411U, + 0x01410U, 0x00090U, 0x00091U, 0x00092U, 0x08120U, 0x00094U, 0x00095U, 0x04A00U, 0x01408U, 0x00098U, 0x00099U, 0x01405U, 0x01404U, + 0x01403U, 0x01402U, 0x01401U, 0x01400U, 0x000A0U, 0x000A1U, 0x000A2U, 0x08110U, 0x000A4U, 0x000A5U, 0x42400U, 0x42401U, 0x000A8U, + 0x000A9U, 0x01840U, 0x01841U, 0x40141U, 0x40140U, 0x24081U, 0x24080U, 0x000B0U, 0x08102U, 0x08101U, 0x08100U, 0x000B4U, 0x08106U, + 0x08105U, 0x08104U, 0x20A01U, 0x20A00U, 0x08109U, 0x08108U, 0x01423U, 0x01422U, 0x01421U, 0x01420U, 0x000C0U, 0x000C1U, 0x000C2U, + 0x000C3U, 0x000C4U, 0x000C5U, 0x000C6U, 0x000C7U, 0x000C8U, 0x000C9U, 0x01820U, 0x01821U, 0x20600U, 0x40120U, 0x16000U, 0x16001U, + 0x000D0U, 0x000D1U, 0x42801U, 0x42800U, 0x03100U, 0x18200U, 0x03102U, 0x18202U, 0x000D8U, 0x000D9U, 0x48084U, 0x01444U, 0x48082U, + 0x01442U, 0x48080U, 0x01440U, 0x000E0U, 0x32000U, 0x01808U, 0x04600U, 0x40109U, 0x40108U, 0x0180CU, 0x4010AU, 0x01802U, 0x40104U, + 0x01800U, 0x01801U, 0x40101U, 0x40100U, 0x01804U, 0x40102U, 0x0A408U, 0x08142U, 0x08141U, 0x08140U, 0x00C81U, 0x00C80U, 0x00C83U, + 0x00C82U, 0x0A400U, 0x0A401U, 0x01810U, 0x01811U, 0x40111U, 0x40110U, 0x01814U, 0x40112U, 0x00100U, 0x00101U, 0x00102U, 0x00103U, + 0x00104U, 0x00105U, 0x00106U, 0x41800U, 0x00108U, 0x00109U, 0x0010AU, 0x02440U, 0x0010CU, 0x0010DU, 0x0010EU, 0x02444U, 0x00110U, + 0x00111U, 0x00112U, 0x080A0U, 0x00114U, 0x00115U, 0x00116U, 0x080A4U, 0x00118U, 0x00119U, 0x15000U, 0x15001U, 0x02822U, 0x02823U, + 0x02820U, 0x02821U, 0x00120U, 0x00121U, 0x00122U, 0x08090U, 0x00124U, 0x00125U, 0x10240U, 0x10241U, 0x00128U, 0x00129U, 0x0012AU, + 0x24104U, 0x09400U, 0x400C0U, 0x02810U, 0x24100U, 0x00130U, 0x08082U, 0x08081U, 0x08080U, 0x31001U, 0x31000U, 0x02808U, 0x08084U, + 0x02806U, 0x0808AU, 0x02804U, 0x08088U, 0x02802U, 0x02803U, 0x02800U, 0x02801U, 0x00140U, 0x00141U, 0x00142U, 0x02408U, 0x00144U, + 0x00145U, 0x10220U, 0x10221U, 0x00148U, 0x02402U, 0x02401U, 0x02400U, 0x400A1U, 0x400A0U, 0x02405U, 0x02404U, 0x00150U, 0x00151U, + 0x00152U, 0x02418U, 0x03080U, 0x03081U, 0x03082U, 0x03083U, 0x09801U, 0x09800U, 0x02411U, 0x02410U, 0x48102U, 0x09804U, 0x48100U, + 0x48101U, 0x00160U, 0x00161U, 0x10204U, 0x10205U, 0x10202U, 0x40088U, 0x10200U, 0x10201U, 0x40085U, 0x40084U, 0x02421U, 0x02420U, + 0x40081U, 0x40080U, 0x10208U, 0x40082U, 0x41402U, 0x080C2U, 0x41400U, 0x080C0U, 0x00D01U, 0x00D00U, 0x10210U, 0x10211U, 0x40095U, + 0x40094U, 0x02844U, 0x080C8U, 0x40091U, 0x40090U, 0x02840U, 0x02841U, 0x00180U, 0x00181U, 0x00182U, 0x08030U, 0x00184U, 0x14400U, + 0x22201U, 0x22200U, 0x00188U, 0x00189U, 0x0018AU, 0x08038U, 0x40061U, 0x40060U, 0x40063U, 0x40062U, 0x00190U, 0x08022U, 0x08021U, + 0x08020U, 0x03040U, 0x03041U, 0x08025U, 0x08024U, 0x40C00U, 0x40C01U, 0x08029U, 0x08028U, 0x2C000U, 0x2C001U, 0x01501U, 0x01500U, + 0x001A0U, 0x08012U, 0x08011U, 0x08010U, 0x40049U, 0x40048U, 0x08015U, 0x08014U, 0x06200U, 0x40044U, 0x30400U, 0x08018U, 0x40041U, + 0x40040U, 0x40043U, 0x40042U, 0x08003U, 0x08002U, 0x08001U, 0x08000U, 0x08007U, 0x08006U, 0x08005U, 0x08004U, 0x0800BU, 0x0800AU, + 0x08009U, 0x08008U, 0x40051U, 0x40050U, 0x02880U, 0x0800CU, 0x001C0U, 0x001C1U, 0x64000U, 0x64001U, 0x03010U, 0x40028U, 0x08C00U, + 0x08C01U, 0x40025U, 0x40024U, 0x02481U, 0x02480U, 0x40021U, 0x40020U, 0x40023U, 0x40022U, 0x03004U, 0x03005U, 0x08061U, 0x08060U, + 0x03000U, 0x03001U, 0x03002U, 0x03003U, 0x0300CU, 0x40034U, 0x30805U, 0x30804U, 0x03008U, 0x40030U, 0x30801U, 0x30800U, 0x4000DU, + 0x4000CU, 0x08051U, 0x08050U, 0x40009U, 0x40008U, 0x10280U, 0x4000AU, 0x40005U, 0x40004U, 0x01900U, 0x40006U, 0x40001U, 0x40000U, + 0x40003U, 0x40002U, 0x14800U, 0x08042U, 0x08041U, 0x08040U, 0x03020U, 0x40018U, 0x08045U, 0x08044U, 0x40015U, 0x40014U, 0x08049U, + 0x08048U, 0x40011U, 0x40010U, 0x40013U, 0x40012U, 0x00200U, 0x00201U, 0x00202U, 0x00203U, 0x00204U, 0x00205U, 0x00206U, 0x00207U, + 0x00208U, 0x00209U, 0x0020AU, 0x50080U, 0x0020CU, 0x0020DU, 0x0020EU, 0x50084U, 0x00210U, 0x00211U, 0x00212U, 0x21040U, 0x00214U, + 0x00215U, 0x04880U, 0x04881U, 0x00218U, 0x00219U, 0x0E001U, 0x0E000U, 0x0021CU, 0x0021DU, 0x04888U, 0x0E004U, 0x00220U, 0x00221U, + 0x00222U, 0x00223U, 0x00224U, 0x00225U, 0x10140U, 0x10141U, 0x00228U, 0x00229U, 0x0022AU, 0x24204U, 0x12401U, 0x12400U, 0x24201U, + 0x24200U, 0x00230U, 0x00231U, 0x00232U, 0x21060U, 0x2A000U, 0x2A001U, 0x2A002U, 0x2A003U, 0x20881U, 0x20880U, 0x20883U, 0x20882U, + 0x05040U, 0x05041U, 0x05042U, 0x24210U, 0x00240U, 0x00241U, 0x00242U, 0x21010U, 0x00244U, 0x46000U, 0x10120U, 0x10121U, 0x00248U, + 0x00249U, 0x0024AU, 0x21018U, 0x20480U, 0x20481U, 0x20482U, 0x20483U, 0x00250U, 0x21002U, 0x21001U, 0x21000U, 0x18081U, 0x18080U, + 0x21005U, 0x21004U, 0x12800U, 0x12801U, 0x21009U, 0x21008U, 0x05020U, 0x05021U, 0x48200U, 0x48201U, 0x00260U, 0x00261U, 0x10104U, + 0x04480U, 0x10102U, 0x10103U, 0x10100U, 0x10101U, 0x62002U, 0x62003U, 0x62000U, 0x62001U, 0x05010U, 0x05011U, 0x10108U, 0x10109U, + 0x0500CU, 0x21022U, 0x21021U, 0x21020U, 0x05008U, 0x00E00U, 0x10110U, 0x10111U, 0x05004U, 0x05005U, 0x05006U, 0x21028U, 0x05000U, + 0x05001U, 0x05002U, 0x05003U, 0x00280U, 0x00281U, 0x00282U, 0x50008U, 0x00284U, 0x00285U, 0x04810U, 0x22100U, 0x00288U, 0x50002U, + 0x50001U, 0x50000U, 0x20440U, 0x20441U, 0x50005U, 0x50004U, 0x00290U, 0x00291U, 0x04804U, 0x04805U, 0x04802U, 0x18040U, 0x04800U, + 0x04801U, 0x20821U, 0x20820U, 0x50011U, 0x50010U, 0x0480AU, 0x01602U, 0x04808U, 0x01600U, 0x002A0U, 0x002A1U, 0x04441U, 0x04440U, + 0x002A4U, 0x002A5U, 0x04830U, 0x04444U, 0x06100U, 0x20810U, 0x50021U, 0x50020U, 0x06104U, 0x20814U, 0x50025U, 0x50024U, 0x20809U, + 0x20808U, 0x13000U, 0x08300U, 0x04822U, 0x2080CU, 0x04820U, 0x04821U, 0x20801U, 0x20800U, 0x20803U, 0x20802U, 0x20805U, 0x20804U, + 0x04828U, 0x20806U, 0x002C0U, 0x002C1U, 0x04421U, 0x04420U, 0x20408U, 0x18010U, 0x2040AU, 0x18012U, 0x20404U, 0x20405U, 0x50041U, + 0x50040U, 0x20400U, 0x20401U, 0x20402U, 0x20403U, 0x18005U, 0x18004U, 0x21081U, 0x21080U, 0x18001U, 0x18000U, 0x04840U, 0x18002U, + 0x20414U, 0x1800CU, 0x21089U, 0x21088U, 0x20410U, 0x18008U, 0x20412U, 0x1800AU, 0x04403U, 0x04402U, 0x04401U, 0x04400U, 0x10182U, + 0x04406U, 0x10180U, 0x04404U, 0x01A02U, 0x0440AU, 0x01A00U, 0x04408U, 0x20420U, 0x40300U, 0x20422U, 0x40302U, 0x04413U, 0x04412U, + 0x04411U, 0x04410U, 0x18021U, 0x18020U, 0x10190U, 0x18022U, 0x20841U, 0x20840U, 0x01A10U, 0x20842U, 0x05080U, 0x05081U, 0x05082U, + 0x05083U, 0x00300U, 0x00301U, 0x00302U, 0x00303U, 0x00304U, 0x00305U, 0x10060U, 0x22080U, 0x00308U, 0x00309U, 0x28800U, 0x28801U, + 0x44402U, 0x44403U, 0x44400U, 0x44401U, 0x00310U, 0x00311U, 0x10C01U, 0x10C00U, 0x00314U, 0x00315U, 0x10070U, 0x10C04U, 0x00318U, + 0x00319U, 0x28810U, 0x10C08U, 0x44412U, 0x00000U, 0x44410U, 0x44411U, 0x00320U, 0x60400U, 0x10044U, 0x10045U, 0x10042U, 0x0C800U, + 0x10040U, 0x10041U, 0x06080U, 0x06081U, 0x06082U, 0x06083U, 0x1004AU, 0x0C808U, 0x10048U, 0x10049U, 0x58008U, 0x08282U, 0x08281U, + 0x08280U, 0x10052U, 0x0C810U, 0x10050U, 0x10051U, 0x58000U, 0x58001U, 0x58002U, 0x08288U, 0x02A02U, 0x02A03U, 0x02A00U, 0x02A01U, + 0x00340U, 0x00341U, 0x10024U, 0x10025U, 0x10022U, 0x10023U, 0x10020U, 0x10021U, 0x34001U, 0x34000U, 0x02601U, 0x02600U, 0x1002AU, + 0x34004U, 0x10028U, 0x10029U, 0x0C400U, 0x0C401U, 0x21101U, 0x21100U, 0x60800U, 0x60801U, 0x10030U, 0x10031U, 0x0C408U, 0x34010U, + 0x21109U, 0x21108U, 0x60808U, 0x60809U, 0x10038U, 0x28420U, 0x10006U, 0x10007U, 0x10004U, 0x10005U, 0x10002U, 0x10003U, 0x10000U, + 0x10001U, 0x1000EU, 0x40284U, 0x1000CU, 0x1000DU, 0x1000AU, 0x40280U, 0x10008U, 0x10009U, 0x10016U, 0x10017U, 0x10014U, 0x10015U, + 0x10012U, 0x10013U, 0x10010U, 0x10011U, 0x05104U, 0x44802U, 0x44801U, 0x44800U, 0x05100U, 0x05101U, 0x10018U, 0x28400U, 0x00380U, + 0x00381U, 0x22005U, 0x22004U, 0x22003U, 0x22002U, 0x22001U, 0x22000U, 0x06020U, 0x06021U, 0x50101U, 0x50100U, 0x11800U, 0x11801U, + 0x22009U, 0x22008U, 0x45001U, 0x45000U, 0x08221U, 0x08220U, 0x04902U, 0x22012U, 0x04900U, 0x22010U, 0x06030U, 0x45008U, 0x08229U, + 0x08228U, 0x11810U, 0x11811U, 0x04908U, 0x22018U, 0x06008U, 0x06009U, 0x08211U, 0x08210U, 0x100C2U, 0x22022U, 0x100C0U, 0x22020U, + 0x06000U, 0x06001U, 0x06002U, 0x06003U, 0x06004U, 0x40240U, 0x06006U, 0x40242U, 0x08203U, 0x08202U, 0x08201U, 0x08200U, 0x08207U, + 0x08206U, 0x08205U, 0x08204U, 0x06010U, 0x20900U, 0x08209U, 0x08208U, 0x61002U, 0x20904U, 0x61000U, 0x61001U, 0x29020U, 0x29021U, + 0x100A4U, 0x22044U, 0x100A2U, 0x22042U, 0x100A0U, 0x22040U, 0x20504U, 0x40224U, 0x0D005U, 0x0D004U, 0x20500U, 0x40220U, 0x0D001U, + 0x0D000U, 0x03204U, 0x18104U, 0x08261U, 0x08260U, 0x03200U, 0x18100U, 0x03202U, 0x18102U, 0x11421U, 0x11420U, 0x00000U, 0x11422U, + 0x03208U, 0x18108U, 0x0D011U, 0x0D010U, 0x29000U, 0x29001U, 0x10084U, 0x04500U, 0x10082U, 0x40208U, 0x10080U, 0x10081U, 0x06040U, + 0x40204U, 0x06042U, 0x40206U, 0x40201U, 0x40200U, 0x10088U, 0x40202U, 0x29010U, 0x08242U, 0x08241U, 0x08240U, 0x10092U, 0x40218U, + 0x10090U, 0x10091U, 0x11401U, 0x11400U, 0x11403U, 0x11402U, 0x40211U, 0x40210U, 0x10098U, 0x40212U, 0x00400U, 0x00401U, 0x00402U, + 0x00403U, 0x00404U, 0x00405U, 0x00406U, 0x00407U, 0x00408U, 0x00409U, 0x0040AU, 0x02140U, 0x0040CU, 0x0040DU, 0x01091U, 0x01090U, + 0x00410U, 0x00411U, 0x00412U, 0x00413U, 0x00414U, 0x00860U, 0x01089U, 0x01088U, 0x00418U, 0x38000U, 0x01085U, 0x01084U, 0x01083U, + 0x01082U, 0x01081U, 0x01080U, 0x00420U, 0x00421U, 0x00422U, 0x00423U, 0x00424U, 0x00850U, 0x42080U, 0x42081U, 0x00428U, 0x00429U, + 0x48801U, 0x48800U, 0x09100U, 0x12200U, 0x24401U, 0x24400U, 0x00430U, 0x00844U, 0x00432U, 0x00846U, 0x00841U, 0x00840U, 0x1C000U, + 0x00842U, 0x00438U, 0x0084CU, 0x010A5U, 0x010A4U, 0x00849U, 0x00848U, 0x010A1U, 0x010A0U, 0x00440U, 0x00441U, 0x00442U, 0x02108U, + 0x00444U, 0x00830U, 0x70001U, 0x70000U, 0x00448U, 0x02102U, 0x02101U, 0x02100U, 0x20280U, 0x20281U, 0x02105U, 0x02104U, 0x00450U, + 0x00824U, 0x00452U, 0x00826U, 0x00821U, 0x00820U, 0x00823U, 0x00822U, 0x24802U, 0x02112U, 0x24800U, 0x02110U, 0x00829U, 0x00828U, + 0x48400U, 0x010C0U, 0x00460U, 0x00814U, 0x04281U, 0x04280U, 0x00811U, 0x00810U, 0x00813U, 0x00812U, 0x54000U, 0x54001U, 0x02121U, + 0x02120U, 0x00819U, 0x00818U, 0x0081BU, 0x0081AU, 0x00805U, 0x00804U, 0x41100U, 0x00806U, 0x00801U, 0x00800U, 0x00803U, 0x00802U, + 0x0A080U, 0x0080CU, 0x0A082U, 0x0080EU, 0x00809U, 0x00808U, 0x0080BU, 0x0080AU, 0x00480U, 0x00481U, 0x00482U, 0x00483U, 0x00484U, + 0x14100U, 0x42020U, 0x01018U, 0x00488U, 0x00489U, 0x01015U, 0x01014U, 0x20240U, 0x01012U, 0x01011U, 0x01010U, 0x00490U, 0x00491U, + 0x0100DU, 0x0100CU, 0x0100BU, 0x0100AU, 0x01009U, 0x01008U, 0x40900U, 0x01006U, 0x01005U, 0x01004U, 0x01003U, 0x01002U, 0x01001U, + 0x01000U, 0x004A0U, 0x004A1U, 0x42004U, 0x04240U, 0x42002U, 0x42003U, 0x42000U, 0x42001U, 0x30102U, 0x30103U, 0x30100U, 0x30101U, + 0x4200AU, 0x01032U, 0x42008U, 0x01030U, 0x25000U, 0x25001U, 0x08501U, 0x08500U, 0x008C1U, 0x008C0U, 0x42010U, 0x01028U, 0x0A040U, + 0x0A041U, 0x01025U, 0x01024U, 0x01023U, 0x01022U, 0x01021U, 0x01020U, 0x004C0U, 0x49000U, 0x04221U, 0x04220U, 0x20208U, 0x20209U, + 0x08900U, 0x08901U, 0x20204U, 0x20205U, 0x02181U, 0x02180U, 0x20200U, 0x20201U, 0x20202U, 0x01050U, 0x0A028U, 0x008A4U, 0x0104DU, + 0x0104CU, 0x008A1U, 0x008A0U, 0x01049U, 0x01048U, 0x0A020U, 0x0A021U, 0x01045U, 0x01044U, 0x20210U, 0x01042U, 0x01041U, 0x01040U, + 0x04203U, 0x04202U, 0x04201U, 0x04200U, 0x00891U, 0x00890U, 0x42040U, 0x04204U, 0x0A010U, 0x0A011U, 0x01C00U, 0x04208U, 0x20220U, + 0x40500U, 0x20222U, 0x40502U, 0x0A008U, 0x00884U, 0x04211U, 0x04210U, 0x00881U, 0x00880U, 0x00883U, 0x00882U, 0x0A000U, 0x0A001U, + 0x0A002U, 0x0A003U, 0x0A004U, 0x00888U, 0x01061U, 0x01060U, 0x00500U, 0x00501U, 0x00502U, 0x02048U, 0x00504U, 0x14080U, 0x00506U, + 0x14082U, 0x00508U, 0x02042U, 0x02041U, 0x02040U, 0x09020U, 0x09021U, 0x44200U, 0x02044U, 0x00510U, 0x00511U, 0x10A01U, 0x10A00U, + 0x4A001U, 0x4A000U, 0x4A003U, 0x4A002U, 0x40880U, 0x40881U, 0x02051U, 0x02050U, 0x40884U, 0x01182U, 0x01181U, 0x01180U, 0x00520U, + 0x60200U, 0x00522U, 0x60202U, 0x09008U, 0x09009U, 0x0900AU, 0x0900BU, 0x09004U, 0x09005U, 0x30080U, 0x02060U, 0x09000U, 0x09001U, + 0x09002U, 0x09003U, 0x41042U, 0x08482U, 0x41040U, 0x08480U, 0x00941U, 0x00940U, 0x41044U, 0x00942U, 0x09014U, 0x09015U, 0x02C04U, + 0x08488U, 0x09010U, 0x09011U, 0x02C00U, 0x02C01U, 0x00540U, 0x0200AU, 0x02009U, 0x02008U, 0x08882U, 0x0200EU, 0x08880U, 0x0200CU, + 0x02003U, 0x02002U, 0x02001U, 0x02000U, 0x02007U, 0x02006U, 0x02005U, 0x02004U, 0x0C200U, 0x0C201U, 0x41020U, 0x02018U, 0x00921U, + 0x00920U, 0x41024U, 0x00922U, 0x02013U, 0x02012U, 0x02011U, 0x02010U, 0x02017U, 0x02016U, 0x02015U, 0x02014U, 0x41012U, 0x0202AU, + 0x41010U, 0x02028U, 0x26000U, 0x00910U, 0x10600U, 0x10601U, 0x02023U, 0x02022U, 0x02021U, 0x02020U, 0x09040U, 0x40480U, 0x02025U, + 0x02024U, 0x41002U, 0x00904U, 0x41000U, 0x41001U, 0x00901U, 0x00900U, 0x41004U, 0x00902U, 0x4100AU, 0x02032U, 0x41008U, 0x02030U, + 0x00909U, 0x00908U, 0x28201U, 0x28200U, 0x00580U, 0x14004U, 0x00582U, 0x14006U, 0x14001U, 0x14000U, 0x08840U, 0x14002U, 0x40810U, + 0x40811U, 0x30020U, 0x020C0U, 0x14009U, 0x14008U, 0x01111U, 0x01110U, 0x40808U, 0x40809U, 0x08421U, 0x08420U, 0x14011U, 0x14010U, + 0x01109U, 0x01108U, 0x40800U, 0x40801U, 0x40802U, 0x01104U, 0x40804U, 0x01102U, 0x01101U, 0x01100U, 0x03801U, 0x03800U, 0x30008U, + 0x08410U, 0x14021U, 0x14020U, 0x42100U, 0x42101U, 0x30002U, 0x30003U, 0x30000U, 0x30001U, 0x09080U, 0x40440U, 0x30004U, 0x30005U, + 0x08403U, 0x08402U, 0x08401U, 0x08400U, 0x08407U, 0x08406U, 0x08405U, 0x08404U, 0x40820U, 0x40821U, 0x30010U, 0x08408U, 0x40824U, + 0x01122U, 0x01121U, 0x01120U, 0x08806U, 0x0208AU, 0x08804U, 0x02088U, 0x08802U, 0x14040U, 0x08800U, 0x08801U, 0x02083U, 0x02082U, + 0x02081U, 0x02080U, 0x20300U, 0x40420U, 0x08808U, 0x02084U, 0x03404U, 0x03405U, 0x08814U, 0x02098U, 0x03400U, 0x03401U, 0x08810U, + 0x08811U, 0x40840U, 0x40841U, 0x02091U, 0x02090U, 0x40844U, 0x01142U, 0x01141U, 0x01140U, 0x04303U, 0x04302U, 0x04301U, 0x04300U, + 0x40409U, 0x40408U, 0x08820U, 0x08821U, 0x40405U, 0x40404U, 0x30040U, 0x020A0U, 0x40401U, 0x40400U, 0x40403U, 0x40402U, 0x41082U, + 0x08442U, 0x41080U, 0x08440U, 0x00981U, 0x00980U, 0x41084U, 0x00982U, 0x0A100U, 0x11200U, 0x0A102U, 0x11202U, 0x40411U, 0x40410U, + 0x40413U, 0x40412U, 0x00600U, 0x00601U, 0x00602U, 0x00603U, 0x00604U, 0x00605U, 0x00606U, 0x00607U, 0x00608U, 0x05800U, 0x0060AU, + 0x05802U, 0x200C0U, 0x12020U, 0x44100U, 0x44101U, 0x00610U, 0x00611U, 0x10901U, 0x10900U, 0x51000U, 0x51001U, 0x51002U, 0x10904U, + 0x00618U, 0x05810U, 0x01285U, 0x01284U, 0x51008U, 0x01282U, 0x01281U, 0x01280U, 0x00620U, 0x60100U, 0x040C1U, 0x040C0U, 0x12009U, + 0x12008U, 0x21800U, 0x21801U, 0x12005U, 0x12004U, 0x12007U, 0x12006U, 0x12001U, 0x12000U, 0x12003U, 0x12002U, 0x00630U, 0x00A44U, + 0x040D1U, 0x040D0U, 0x00A41U, 0x00A40U, 0x21810U, 0x00A42U, 0x12015U, 0x12014U, 0x00000U, 0x12016U, 0x12011U, 0x12010U, 0x12013U, + 0x12012U, 0x00640U, 0x00641U, 0x040A1U, 0x040A0U, 0x20088U, 0x20089U, 0x2008AU, 0x040A4U, 0x20084U, 0x20085U, 0x19000U, 0x02300U, + 0x20080U, 0x20081U, 0x20082U, 0x20083U, 0x0C100U, 0x0C101U, 0x21401U, 0x21400U, 0x00A21U, 0x00A20U, 0x00A23U, 0x00A22U, 0x20094U, + 0x20095U, 0x19010U, 0x21408U, 0x20090U, 0x20091U, 0x20092U, 0x28120U, 0x04083U, 0x04082U, 0x04081U, 0x04080U, 0x00A11U, 0x00A10U, + 0x10500U, 0x04084U, 0x200A4U, 0x0408AU, 0x04089U, 0x04088U, 0x200A0U, 0x12040U, 0x200A2U, 0x12042U, 0x00A05U, 0x00A04U, 0x04091U, + 0x04090U, 0x00A01U, 0x00A00U, 0x00A03U, 0x00A02U, 0x05404U, 0x00A0CU, 0x28105U, 0x28104U, 0x05400U, 0x00A08U, 0x28101U, 0x28100U, + 0x00680U, 0x00681U, 0x04061U, 0x04060U, 0x20048U, 0x20049U, 0x2004AU, 0x04064U, 0x20044U, 0x20045U, 0x50401U, 0x50400U, 0x20040U, + 0x20041U, 0x20042U, 0x01210U, 0x68002U, 0x68003U, 0x68000U, 0x68001U, 0x04C02U, 0x0120AU, 0x04C00U, 0x01208U, 0x20054U, 0x01206U, + 0x01205U, 0x01204U, 0x20050U, 0x01202U, 0x01201U, 0x01200U, 0x18800U, 0x04042U, 0x04041U, 0x04040U, 0x42202U, 0x04046U, 0x42200U, + 0x04044U, 0x20064U, 0x0404AU, 0x04049U, 0x04048U, 0x20060U, 0x12080U, 0x20062U, 0x12082U, 0x18810U, 0x04052U, 0x04051U, 0x04050U, + 0x4C009U, 0x4C008U, 0x42210U, 0x04054U, 0x20C01U, 0x20C00U, 0x20C03U, 0x20C02U, 0x4C001U, 0x4C000U, 0x01221U, 0x01220U, 0x2000CU, + 0x04022U, 0x04021U, 0x04020U, 0x20008U, 0x20009U, 0x2000AU, 0x04024U, 0x20004U, 0x20005U, 0x20006U, 0x04028U, 0x20000U, 0x20001U, + 0x20002U, 0x20003U, 0x2001CU, 0x04032U, 0x04031U, 0x04030U, 0x20018U, 0x18400U, 0x2001AU, 0x18402U, 0x20014U, 0x20015U, 0x20016U, + 0x01244U, 0x20010U, 0x20011U, 0x20012U, 0x01240U, 0x04003U, 0x04002U, 0x04001U, 0x04000U, 0x20028U, 0x04006U, 0x04005U, 0x04004U, + 0x20024U, 0x0400AU, 0x04009U, 0x04008U, 0x20020U, 0x20021U, 0x20022U, 0x0400CU, 0x04013U, 0x04012U, 0x04011U, 0x04010U, 0x00A81U, + 0x00A80U, 0x04015U, 0x04014U, 0x0A200U, 0x11100U, 0x04019U, 0x04018U, 0x20030U, 0x20031U, 0x50800U, 0x50801U, 0x00700U, 0x60020U, + 0x10811U, 0x10810U, 0x4400AU, 0x60024U, 0x44008U, 0x44009U, 0x44006U, 0x02242U, 0x44004U, 0x02240U, 0x44002U, 0x44003U, 0x44000U, + 0x44001U, 0x0C040U, 0x10802U, 0x10801U, 0x10800U, 0x0C044U, 0x10806U, 0x10805U, 0x10804U, 0x23000U, 0x23001U, 0x10809U, 0x10808U, + 0x44012U, 0x44013U, 0x44010U, 0x44011U, 0x60001U, 0x60000U, 0x60003U, 0x60002U, 0x60005U, 0x60004U, 0x10440U, 0x10441U, 0x60009U, + 0x60008U, 0x44024U, 0x6000AU, 0x09200U, 0x12100U, 0x44020U, 0x44021U, 0x60011U, 0x60010U, 0x10821U, 0x10820U, 0x07003U, 0x07002U, + 0x07001U, 0x07000U, 0x23020U, 0x60018U, 0x28045U, 0x28044U, 0x09210U, 0x28042U, 0x28041U, 0x28040U, 0x0C010U, 0x0C011U, 0x02209U, + 0x02208U, 0x10422U, 0x10423U, 0x10420U, 0x10421U, 0x02203U, 0x02202U, 0x02201U, 0x02200U, 0x20180U, 0x20181U, 0x44040U, 0x02204U, + 0x0C000U, 0x0C001U, 0x0C002U, 0x10840U, 0x0C004U, 0x0C005U, 0x0C006U, 0x10844U, 0x0C008U, 0x0C009U, 0x02211U, 0x02210U, 0x0C00CU, + 0x28022U, 0x28021U, 0x28020U, 0x60041U, 0x60040U, 0x10404U, 0x04180U, 0x10402U, 0x10403U, 0x10400U, 0x10401U, 0x02223U, 0x02222U, + 0x02221U, 0x02220U, 0x1040AU, 0x28012U, 0x10408U, 0x28010U, 0x0C020U, 0x0C021U, 0x41200U, 0x41201U, 0x00B01U, 0x00B00U, 0x10410U, + 0x28008U, 0x11081U, 0x11080U, 0x28005U, 0x28004U, 0x28003U, 0x28002U, 0x28001U, 0x28000U, 0x52040U, 0x14204U, 0x22405U, 0x22404U, + 0x14201U, 0x14200U, 0x22401U, 0x22400U, 0x20144U, 0x20145U, 0x44084U, 0x022C0U, 0x20140U, 0x20141U, 0x44080U, 0x44081U, 0x40A08U, + 0x10882U, 0x10881U, 0x10880U, 0x14211U, 0x14210U, 0x1A008U, 0x10884U, 0x40A00U, 0x40A01U, 0x40A02U, 0x01304U, 0x1A002U, 0x01302U, + 0x1A000U, 0x01300U, 0x60081U, 0x60080U, 0x04141U, 0x04140U, 0x60085U, 0x60084U, 0x104C0U, 0x04144U, 0x06400U, 0x06401U, 0x30200U, + 0x30201U, 0x06404U, 0x40640U, 0x30204U, 0x30205U, 0x08603U, 0x08602U, 0x08601U, 0x08600U, 0x00000U, 0x08606U, 0x08605U, 0x08604U, + 0x11041U, 0x11040U, 0x30210U, 0x11042U, 0x11045U, 0x11044U, 0x1A020U, 0x01320U, 0x52000U, 0x52001U, 0x04121U, 0x04120U, 0x20108U, + 0x20109U, 0x08A00U, 0x08A01U, 0x20104U, 0x20105U, 0x02281U, 0x02280U, 0x20100U, 0x20101U, 0x20102U, 0x20103U, 0x0C080U, 0x0C081U, + 0x0C082U, 0x04130U, 0x0C084U, 0x06808U, 0x08A10U, 0x08A11U, 0x11021U, 0x11020U, 0x11023U, 0x11022U, 0x20110U, 0x06800U, 0x20112U, + 0x06802U, 0x04103U, 0x04102U, 0x04101U, 0x04100U, 0x10482U, 0x04106U, 0x10480U, 0x04104U, 0x11011U, 0x11010U, 0x04109U, 0x04108U, + 0x20120U, 0x40600U, 0x20122U, 0x40602U, 0x11009U, 0x11008U, 0x22800U, 0x04110U, 0x1100DU, 0x1100CU, 0x22804U, 0x04114U, 0x11001U, + 0x11000U, 0x11003U, 0x11002U, 0x11005U, 0x11004U, 0x28081U, 0x28080U}; + +#define X18 0x00040000 /* vector representation of X^{18} */ +#define X11 0x00000800 /* vector representation of X^{11} */ +#define MASK8 0xfffff800 /* auxiliary vector for testing */ +#define GENPOL 0x00000c75 /* generator polinomial, g(x) */ + +CDMRSlotType::CDMRSlotType() +{ +} + +uint32_t CDMRSlotType::getSyndrome1987(uint32_t pattern) const +/* + * Compute the syndrome corresponding to the given pattern, i.e., the + * remainder after dividing the pattern (when considering it as the vector + * representation of a polynomial) by the generator polynomial, GENPOL. + * In the program this pattern has several meanings: (1) pattern = infomation + * bits, when constructing the encoding table; (2) pattern = error pattern, + * when constructing the decoding table; and (3) pattern = received vector, to + * obtain its syndrome in decoding. + */ +{ + unsigned int aux = X18; + + if (pattern >= X11) { + while (pattern & MASK8) { + while (!(aux & pattern)) + aux = aux >> 1; + + pattern ^= (aux / X11) * GENPOL; + } + } + + return pattern; +} + +uint8_t CDMRSlotType::decode2087(const uint8_t* data) const +{ + uint32_t code = (data[0U] << 11) + (data[1U] << 3) + (data[2U] >> 5); + uint32_t syndrome = getSyndrome1987(code); + uint32_t error_pattern = DECODING_TABLE_1987[syndrome]; + + if (error_pattern != 0x00U) + code ^= error_pattern; + + return code >> 11; +} + +void CDMRSlotType::decode(const uint8_t* frame, uint8_t& colorCode, uint8_t& dataType) const +{ + uint8_t slotType[3U]; + slotType[0U] = (frame[12U] << 2) & 0xFCU; + slotType[0U] |= (frame[13U] >> 6) & 0x03U; + + slotType[1U] = (frame[13U] << 2) & 0xC0U; + slotType[1U] |= (frame[19U] << 2) & 0x3CU; + slotType[1U] |= (frame[20U] >> 6) & 0x03U; + + slotType[2U] = (frame[20U] << 2) & 0xF0U; + + uint8_t code = decode2087(slotType); + + colorCode = (code >> 4) & 0x0FU; + dataType = (code >> 0) & 0x0FU; +} + +void CDMRSlotType::encode(uint8_t colorCode, uint8_t dataType, uint8_t* frame) const +{ + uint8_t slotType[3U]; + slotType[0U] = (colorCode << 4) & 0xF0U; + slotType[0U] |= (dataType << 0) & 0x0FU; + + uint16_t cksum = ENCODING_TABLE_2087[slotType[0U]]; + + slotType[1U] = (cksum >> 0) & 0xFFU; + slotType[2U] = (cksum >> 8) & 0xFFU; + + frame[12U] = (frame[12U] & 0xC0U) | ((slotType[0U] >> 2) & 0x3FU); + frame[13U] = (frame[13U] & 0x0FU) | ((slotType[0U] << 6) & 0xC0U) | ((slotType[1U] >> 2) & 0x30U); + frame[19U] = (frame[19U] & 0xF0U) | ((slotType[1U] >> 2) & 0x0FU); + frame[20U] = (frame[20U] & 0x03U) | ((slotType[1U] << 6) & 0xC0U) | ((slotType[2U] >> 2) & 0x3CU); +} + diff --git a/DMRSlotType.h b/DMRSlotType.h new file mode 100644 index 0000000..0052467 --- /dev/null +++ b/DMRSlotType.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2015 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(DMRSLOTTYPE_H) +#define DMRSLOTTYPE_H + +class CDMRSlotType { +public: + CDMRSlotType(); + + void decode(const uint8_t* frame, uint8_t& colorCode, uint8_t& dataType) const; + + void encode(uint8_t colorCode, uint8_t dataType, uint8_t* frame) const; + +private: + + uint8_t decode2087(const uint8_t* data) const; + uint32_t getSyndrome1987(uint32_t pattern) const; +}; + +#endif + diff --git a/DMRTX.cpp b/DMRTX.cpp new file mode 100644 index 0000000..7a7bf06 --- /dev/null +++ b/DMRTX.cpp @@ -0,0 +1,306 @@ +/* + * Copyright (C) 2009-2016 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. + */ + +#include "Config.h" +#include "Globals.h" +#include "DMRSlotType.h" + +// Generated using rcosdesign(0.2, 8, 5, 'sqrt') in MATLAB +static q15_t DMR_C4FSK_FILTER[] = {401, 104, -340, -731, -847, -553, 112, 909, 1472, 1450, 683, -675, -2144, -3040, -2706, -770, 2667, 6995, + 11237, 14331, 15464, 14331, 11237, 6995, 2667, -770, -2706, -3040, -2144, -675, 683, 1450, 1472, 909, 112, + -553, -847, -731, -340, 104, 401, 0}; +const uint16_t DMR_C4FSK_FILTER_LEN = 42U; + +q15_t DMR_A[] = { 1280, 1280, 1280, 1280, 1280}; +q15_t DMR_B[] = { 427, 427, 427, 427, 427}; +q15_t DMR_C[] = { -427, -427, -427, -427, -427}; +q15_t DMR_D[] = {-1280, -1280, -1280, -1280, -1280}; + +// The PR FILL and Data Sync pattern. +const uint8_t IDLE_DATA[] = + {0x53U, 0xC2U, 0x5EU, 0xABU, 0xA8U, 0x67U, 0x1DU, 0xC7U, 0x38U, 0x3BU, 0xD9U, + 0x36U, 0x00U, 0x0DU, 0xFFU, 0x57U, 0xD7U, 0x5DU, 0xF5U, 0xD0U, 0x03U, 0xF6U, + 0xE4U, 0x65U, 0x17U, 0x1BU, 0x48U, 0xCAU, 0x6DU, 0x4FU, 0xC6U, 0x10U, 0xB4U}; + +const uint8_t CACH_INTERLEAVE[] = + { 1U, 2U, 3U, 5U, 6U, 7U, 9U, 10U, 11U, 13U, 15U, 16U, 17U, 19U, 20U, 21U, 23U, + 25U, 26U, 27U, 29U, 30U, 31U, 33U, 34U, 35U, 37U, 39U, 40U, 41U, 43U, 44U, 45U, 47U, + 49U, 50U, 51U, 53U, 54U, 55U, 57U, 58U, 59U, 61U, 63U, 64U, 65U, 67U, 68U, 69U, 71U, + 73U, 74U, 75U, 77U, 78U, 79U, 81U, 82U, 83U, 85U, 87U, 88U, 89U, 91U, 92U, 93U, 95U}; + +const uint8_t EMPTY_SHORT_LC[] = + {0x00U, 0x08U, 0x44U, 0x00U, 0x88U, 0x18U, 0x63U, 0x89U, 0x18U, 0x64U, 0x80U, 0x00U}; + +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]) +#define READ_BIT1(p,i) (p[(i)>>3] & BIT_MASK_TABLE[(i)&7]) + +CDMRTX::CDMRTX() : +m_fifo(), +m_modFilter(), +m_modState(), +m_state(DMRTXSTATE_IDLE), +m_idle(), +m_cachPtr(0U), +m_shortLC(), +m_newShortLC(), +m_markBuffer(), +m_poBuffer(), +m_poLen(0U), +m_poPtr(0U) +{ + ::memset(m_modState, 0x00U, 70U * sizeof(q15_t)); + + m_modFilter.numTaps = DMR_C4FSK_FILTER_LEN; + m_modFilter.pState = m_modState; + m_modFilter.pCoeffs = DMR_C4FSK_FILTER; + + ::memcpy(m_newShortLC, EMPTY_SHORT_LC, 12U); + ::memcpy(m_shortLC, EMPTY_SHORT_LC, 12U); +} + +void CDMRTX::process() +{ + if (m_state == DMRTXSTATE_IDLE) + return; + + if (m_poLen == 0U) { + switch (m_state) { + case DMRTXSTATE_SLOT1: + createData(0U); + m_state = DMRTXSTATE_CACH2; + break; + + case DMRTXSTATE_CACH2: + createCACH(1U, 0U); + m_state = DMRTXSTATE_SLOT2; + break; + + case DMRTXSTATE_SLOT2: + createData(1U); + m_state = DMRTXSTATE_CACH1; + break; + + default: + createCACH(0U, 1U); + m_state = DMRTXSTATE_SLOT1; + break; + } + } + + if (m_poLen > 0U) { + uint16_t space = io.getSpace(); + + while (space > (4U * DMR_RADIO_SYMBOL_LENGTH)) { + uint8_t c = m_poBuffer[m_poPtr]; + uint8_t m = m_markBuffer[m_poPtr]; + m_poPtr++; + + writeByte(c, m); + + space -= 4U * DMR_RADIO_SYMBOL_LENGTH; + + if (m_poPtr >= m_poLen) { + m_poPtr = 0U; + m_poLen = 0U; + return; + } + } + } +} + +uint8_t CDMRTX::writeData1(const uint8_t* data, uint8_t length) +{ + if (length != (DMR_FRAME_LENGTH_BYTES + 1U)) + return 4U; + + uint16_t space = m_fifo[0U].getSpace(); + if (space < DMR_FRAME_LENGTH_BYTES) + return 5U; + + for (uint8_t i = 0U; i < DMR_FRAME_LENGTH_BYTES; i++) + m_fifo[0U].put(data[i + 1U]); + + // Start the TX if it isn't already on + if (!m_tx) + m_state = DMRTXSTATE_SLOT1; + + return 0U; +} + +uint8_t CDMRTX::writeData2(const uint8_t* data, uint8_t length) +{ + if (length != (DMR_FRAME_LENGTH_BYTES + 1U)) + return 4U; + + uint16_t space = m_fifo[1U].getSpace(); + if (space < DMR_FRAME_LENGTH_BYTES) + return 5U; + + for (uint8_t i = 0U; i < DMR_FRAME_LENGTH_BYTES; i++) + m_fifo[1U].put(data[i + 1U]); + + // Start the TX if it isn't already on + if (!m_tx) + m_state = DMRTXSTATE_SLOT1; + + return 0U; +} + +uint8_t CDMRTX::writeShortLC(const uint8_t* data, uint8_t length) +{ + if (length != 9U) + return 4U; + + ::memset(m_newShortLC, 0x00U, 12U); + + // Set the LCSS bits + m_newShortLC[1U] = 0x08U; + m_newShortLC[4U] = 0x88U; + m_newShortLC[7U] = 0x88U; + m_newShortLC[11U] = 0x80U; + + for (uint8_t i = 0U; i < 68U; i++) { + bool b = READ_BIT1(data, i); + uint8_t n = CACH_INTERLEAVE[i]; + WRITE_BIT1(m_newShortLC, n, b); + } + + return 0U; +} + +void CDMRTX::setStart(bool start) +{ + m_state = start ? DMRTXSTATE_SLOT1 : DMRTXSTATE_IDLE; +} + +void CDMRTX::writeByte(uint8_t c, uint8_t control) +{ + q15_t inBuffer[DMR_RADIO_SYMBOL_LENGTH * 4U]; + q15_t outBuffer[DMR_RADIO_SYMBOL_LENGTH * 4U]; + + const uint8_t MASK = 0xC0U; + + q15_t* p = inBuffer; + for (uint8_t i = 0U; i < 4U; i++, c <<= 2, p += DMR_RADIO_SYMBOL_LENGTH) { + switch (c & MASK) { + case 0xC0U: + ::memcpy(p, DMR_A, DMR_RADIO_SYMBOL_LENGTH * sizeof(q15_t)); + break; + case 0x80U: + ::memcpy(p, DMR_B, DMR_RADIO_SYMBOL_LENGTH * sizeof(q15_t)); + break; + case 0x00U: + ::memcpy(p, DMR_C, DMR_RADIO_SYMBOL_LENGTH * sizeof(q15_t)); + break; + default: + ::memcpy(p, DMR_D, DMR_RADIO_SYMBOL_LENGTH * sizeof(q15_t)); + break; + } + } + + uint8_t controlBuffer[DMR_RADIO_SYMBOL_LENGTH * 4U]; + ::memset(controlBuffer, MARK_NONE, DMR_RADIO_SYMBOL_LENGTH * 4U * sizeof(uint8_t)); + controlBuffer[DMR_RADIO_SYMBOL_LENGTH * 2U] = control; + + ::arm_fir_fast_q15(&m_modFilter, inBuffer, outBuffer, DMR_RADIO_SYMBOL_LENGTH * 4U); + + io.write(outBuffer, DMR_RADIO_SYMBOL_LENGTH * 4U, controlBuffer); +} + +uint16_t CDMRTX::getSpace1() const +{ + return m_fifo[0U].getSpace() / (DMR_FRAME_LENGTH_BYTES + 2U); +} + +uint16_t CDMRTX::getSpace2() const +{ + return m_fifo[1U].getSpace() / (DMR_FRAME_LENGTH_BYTES + 2U); +} + +void CDMRTX::createData(uint8_t slotIndex) +{ + if (m_fifo[slotIndex].getData()> 0U) { + for (unsigned int i = 0U; i < DMR_FRAME_LENGTH_BYTES; i++) { + m_poBuffer[i] = m_fifo[slotIndex].get(); + m_markBuffer[i] = MARK_NONE; + } + } else { + // Transmit an idle message + for (unsigned int i = 0U; i < DMR_FRAME_LENGTH_BYTES; i++) { + m_poBuffer[i] = m_idle[i]; + m_markBuffer[i] = MARK_NONE; + } + } + + m_poLen = DMR_FRAME_LENGTH_BYTES; + m_poPtr = 0U; +} + +void CDMRTX::createCACH(uint8_t txSlotIndex, uint8_t rxSlotIndex) +{ + if (m_cachPtr >= 12U) + m_cachPtr = 0U; + + if (m_cachPtr == 0U) { + if (m_fifo[0U].getData() == 0U && m_fifo[1U].getData() == 0U) + ::memcpy(m_shortLC, EMPTY_SHORT_LC, 12U); + else + ::memcpy(m_shortLC, m_newShortLC, 12U); + } + + ::memcpy(m_poBuffer, m_shortLC + m_cachPtr, 3U); + m_markBuffer[0U] = MARK_END; + m_markBuffer[1U] = MARK_NONE; + m_markBuffer[2U] = rxSlotIndex == 1U ? MARK_SLOT2 : MARK_SLOT1; + + bool at = m_fifo[rxSlotIndex].getData() > 0U; + bool tc = txSlotIndex == 1U; + bool lcss0 = true; // For 1 and 2 + bool lcss1 = true; + + if (m_cachPtr == 0U) // For 0 + lcss0 = false; + else if (m_cachPtr == 9U) // For 3 + lcss1 = false; + + bool h0 = at ^ tc ^ lcss0; + bool h1 = tc ^ lcss0 ^ lcss1; + bool h2 = at ^ tc ^ lcss1; + + m_poBuffer[0U] |= at ? 0x80U : 0x00U; + m_poBuffer[0U] |= tc ? 0x08U : 0x00U; + m_poBuffer[1U] |= h0 ? 0x02U : 0x00U; + m_poBuffer[2U] |= h1 ? 0x20U : 0x00U; + m_poBuffer[2U] |= h2 ? 0x02U : 0x00U; + + m_poLen = DMR_CACH_LENGTH_BYTES; + m_poPtr = 0U; + + m_cachPtr += 3U; +} + +void CDMRTX::setColorCode(uint8_t colorCode) +{ + ::memcpy(m_idle, IDLE_DATA, DMR_FRAME_LENGTH_BYTES); + + CDMRSlotType slotType; + slotType.encode(colorCode, DT_IDLE, m_idle); +} + diff --git a/DMRTX.h b/DMRTX.h new file mode 100644 index 0000000..3cae45e --- /dev/null +++ b/DMRTX.h @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2015 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(DMRTX_H) +#define DMRTX_H + +#include "Config.h" +#include "DMRDefines.h" + +#include "SerialRB.h" + +enum DMRTXSTATE { + DMRTXSTATE_IDLE, + DMRTXSTATE_SLOT1, + DMRTXSTATE_CACH1, + DMRTXSTATE_SLOT2, + DMRTXSTATE_CACH2 +}; + +class CDMRTX { +public: + CDMRTX(); + + uint8_t writeData1(const uint8_t* data, uint8_t length); + uint8_t writeData2(const uint8_t* data, uint8_t length); + uint8_t writeShortLC(const uint8_t* data, uint8_t length); + + void setStart(bool start); + + void process(); + + uint16_t getSpace1() const; + uint16_t getSpace2() const; + + void setColorCode(uint8_t colorCode); + +private: + CSerialRB m_fifo[2U]; + arm_fir_instance_q15 m_modFilter; + q15_t m_modState[70U]; // NoTaps + BlockSize - 1, 42 + 20 - 1 plus some spare + DMRTXSTATE m_state; + uint8_t m_idle[DMR_FRAME_LENGTH_BYTES]; + uint8_t m_cachPtr; + uint8_t m_shortLC[12U]; + uint8_t m_newShortLC[12U]; + uint8_t m_markBuffer[40U]; + uint8_t m_poBuffer[40U]; + uint16_t m_poLen; + uint16_t m_poPtr; + + void createData(uint8_t slotIndex); + void createCACH(uint8_t txSlotIndex, uint8_t rxSlotIndex); + void writeByte(uint8_t c, uint8_t control); +}; + +#endif + diff --git a/DStarDefines.h b/DStarDefines.h new file mode 100644 index 0000000..6255028 --- /dev/null +++ b/DStarDefines.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2009-2015 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(DSTARDEFINES_H) +#define DSTARDEFINES_H + +const unsigned int DSTAR_RADIO_BIT_LENGTH = 5U; // At 24 kHz sample rate + +const unsigned int DSTAR_HEADER_LENGTH_BYTES = 41U; +const unsigned int DSTAR_HEADER_LENGTH_BITS = DSTAR_HEADER_LENGTH_BYTES * 8U; + +const unsigned int DSTAR_FEC_SECTION_LENGTH_BYTES = 83U; +const unsigned int DSTAR_FEC_SECTION_LENGTH_BITS = 660U; + +const unsigned int DSTAR_DATA_LENGTH_BYTES = 12U; +const unsigned int DSTAR_DATA_LENGTH_BITS = DSTAR_DATA_LENGTH_BYTES * 8U; + +const uint8_t DSTAR_EOT_BYTES[] = {0x55, 0x55, 0x55, 0x55, 0xC8, 0x7A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; +const unsigned int DSTAR_EOT_LENGTH_BYTES = 6U; +const unsigned int DSTAR_EOT_LENGTH_BITS = DSTAR_EOT_LENGTH_BYTES * 8U; + +const uint8_t DSTAR_DATA_SYNC_LENGTH_BYTES = 3U; +const uint8_t DSTAR_DATA_SYNC_LENGTH_BITS = DSTAR_DATA_SYNC_LENGTH_BYTES * 8U; + +const uint8_t DSTAR_DATA_SYNC_BYTES[] = {0x9E, 0x8D, 0x32, 0x88, 0x26, 0x1A, 0x3F, 0x61, 0xE8, 0x55, 0x2D, 0x16}; + +const uint8_t DSTAR_SLOW_DATA_TYPE_TEXT = 0x40U; +const uint8_t DSTAR_SLOW_DATA_TYPE_HEADER = 0x50U; + +const uint8_t DSTAR_SCRAMBLER_BYTES[] = {0x70U, 0x4FU, 0x93U}; + +#endif + diff --git a/DStarRX.cpp b/DStarRX.cpp new file mode 100644 index 0000000..47629ef --- /dev/null +++ b/DStarRX.cpp @@ -0,0 +1,696 @@ +/* + * Copyright (C) 2009-2015 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. + */ + +#include "Config.h" +#include "Globals.h" +#include "DStarRX.h" +#include "Utils.h" + +const unsigned int BUFFER_LENGTH = 200U; + +const uint32_t PLLMAX = 0x10000U; +const uint32_t PLLINC = PLLMAX / DSTAR_RADIO_BIT_LENGTH; +const uint32_t INC = PLLINC / 32U; + +const unsigned int MAX_SYNC_BITS = 50U * DSTAR_DATA_LENGTH_BITS; + +const q15_t THRESHOLD = 0; + +// D-Star bit order version of 0x55 0x55 0x6E 0x0A +const uint32_t FRAME_SYNC_DATA = 0x00557650U; +const uint32_t FRAME_SYNC_MASK = 0x00FFFFFFU; +const uint8_t FRAME_SYNC_ERRS = 2U; + +// D-Star bit order version of 0x55 0x2D 0x16 +const uint32_t DATA_SYNC_DATA = 0x00AAB468U; +const uint32_t DATA_SYNC_MASK = 0x00FFFFFFU; +const uint8_t DATA_SYNC_ERRS = 2U; + +// D-Star bit order version of 0x55 0x55 0xC8 0x7A +const uint32_t END_SYNC_DATA = 0xAAAA135EU; +const uint32_t END_SYNC_MASK = 0xFFFFFFFFU; +const uint8_t END_SYNC_ERRS = 3U; + +const uint8_t BIT_MASK_TABLE0[] = {0x7FU, 0xBFU, 0xDFU, 0xEFU, 0xF7U, 0xFBU, 0xFDU, 0xFEU}; +const uint8_t BIT_MASK_TABLE1[] = {0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U}; +const uint8_t BIT_MASK_TABLE2[] = {0xFEU, 0xFDU, 0xFBU, 0xF7U, 0xEFU, 0xDFU, 0xBFU, 0x7FU}; +const uint8_t BIT_MASK_TABLE3[] = {0x01U, 0x02U, 0x04U, 0x08U, 0x10U, 0x20U, 0x40U, 0x80U}; + +#define WRITE_BIT1(p,i,b) p[(i)>>3] = (b) ? (p[(i)>>3] | BIT_MASK_TABLE1[(i)&7]) : (p[(i)>>3] & BIT_MASK_TABLE0[(i)&7]) +#define READ_BIT1(p,i) (p[(i)>>3] & BIT_MASK_TABLE1[(i)&7]) + +#define WRITE_BIT2(p,i,b) p[(i)>>3] = (b) ? (p[(i)>>3] | BIT_MASK_TABLE3[(i)&7]) : (p[(i)>>3] & BIT_MASK_TABLE2[(i)&7]) +#define READ_BIT2(p,i) (p[(i)>>3] & BIT_MASK_TABLE3[(i)&7]) + +const uint8_t INTERLEAVE_TABLE_RX[] = { + 0x00U, 0x00U, 0x03U, 0x00U, 0x06U, 0x00U, 0x09U, 0x00U, 0x0CU, 0x00U, + 0x0FU, 0x00U, 0x12U, 0x00U, 0x15U, 0x00U, 0x18U, 0x00U, 0x1BU, 0x00U, + 0x1EU, 0x00U, 0x21U, 0x00U, 0x24U, 0x00U, 0x27U, 0x00U, 0x2AU, 0x00U, + 0x2DU, 0x00U, 0x30U, 0x00U, 0x33U, 0x00U, 0x36U, 0x00U, 0x39U, 0x00U, + 0x3CU, 0x00U, 0x3FU, 0x00U, 0x42U, 0x00U, 0x45U, 0x00U, 0x48U, 0x00U, + 0x4BU, 0x00U, 0x4EU, 0x00U, 0x51U, 0x00U, 0x00U, 0x01U, 0x03U, 0x01U, + 0x06U, 0x01U, 0x09U, 0x01U, 0x0CU, 0x01U, 0x0FU, 0x01U, 0x12U, 0x01U, + 0x15U, 0x01U, 0x18U, 0x01U, 0x1BU, 0x01U, 0x1EU, 0x01U, 0x21U, 0x01U, + 0x24U, 0x01U, 0x27U, 0x01U, 0x2AU, 0x01U, 0x2DU, 0x01U, 0x30U, 0x01U, + 0x33U, 0x01U, 0x36U, 0x01U, 0x39U, 0x01U, 0x3CU, 0x01U, 0x3FU, 0x01U, + 0x42U, 0x01U, 0x45U, 0x01U, 0x48U, 0x01U, 0x4BU, 0x01U, 0x4EU, 0x01U, + 0x51U, 0x01U, 0x00U, 0x02U, 0x03U, 0x02U, 0x06U, 0x02U, 0x09U, 0x02U, + 0x0CU, 0x02U, 0x0FU, 0x02U, 0x12U, 0x02U, 0x15U, 0x02U, 0x18U, 0x02U, + 0x1BU, 0x02U, 0x1EU, 0x02U, 0x21U, 0x02U, 0x24U, 0x02U, 0x27U, 0x02U, + 0x2AU, 0x02U, 0x2DU, 0x02U, 0x30U, 0x02U, 0x33U, 0x02U, 0x36U, 0x02U, + 0x39U, 0x02U, 0x3CU, 0x02U, 0x3FU, 0x02U, 0x42U, 0x02U, 0x45U, 0x02U, + 0x48U, 0x02U, 0x4BU, 0x02U, 0x4EU, 0x02U, 0x51U, 0x02U, 0x00U, 0x03U, + 0x03U, 0x03U, 0x06U, 0x03U, 0x09U, 0x03U, 0x0CU, 0x03U, 0x0FU, 0x03U, + 0x12U, 0x03U, 0x15U, 0x03U, 0x18U, 0x03U, 0x1BU, 0x03U, 0x1EU, 0x03U, + 0x21U, 0x03U, 0x24U, 0x03U, 0x27U, 0x03U, 0x2AU, 0x03U, 0x2DU, 0x03U, + 0x30U, 0x03U, 0x33U, 0x03U, 0x36U, 0x03U, 0x39U, 0x03U, 0x3CU, 0x03U, + 0x3FU, 0x03U, 0x42U, 0x03U, 0x45U, 0x03U, 0x48U, 0x03U, 0x4BU, 0x03U, + 0x4EU, 0x03U, 0x51U, 0x03U, 0x00U, 0x04U, 0x03U, 0x04U, 0x06U, 0x04U, + 0x09U, 0x04U, 0x0CU, 0x04U, 0x0FU, 0x04U, 0x12U, 0x04U, 0x15U, 0x04U, + 0x18U, 0x04U, 0x1BU, 0x04U, 0x1EU, 0x04U, 0x21U, 0x04U, 0x24U, 0x04U, + 0x27U, 0x04U, 0x2AU, 0x04U, 0x2DU, 0x04U, 0x30U, 0x04U, 0x33U, 0x04U, + 0x36U, 0x04U, 0x39U, 0x04U, 0x3CU, 0x04U, 0x3FU, 0x04U, 0x42U, 0x04U, + 0x45U, 0x04U, 0x48U, 0x04U, 0x4BU, 0x04U, 0x4EU, 0x04U, 0x51U, 0x04U, + 0x00U, 0x05U, 0x03U, 0x05U, 0x06U, 0x05U, 0x09U, 0x05U, 0x0CU, 0x05U, + 0x0FU, 0x05U, 0x12U, 0x05U, 0x15U, 0x05U, 0x18U, 0x05U, 0x1BU, 0x05U, + 0x1EU, 0x05U, 0x21U, 0x05U, 0x24U, 0x05U, 0x27U, 0x05U, 0x2AU, 0x05U, + 0x2DU, 0x05U, 0x30U, 0x05U, 0x33U, 0x05U, 0x36U, 0x05U, 0x39U, 0x05U, + 0x3CU, 0x05U, 0x3FU, 0x05U, 0x42U, 0x05U, 0x45U, 0x05U, 0x48U, 0x05U, + 0x4BU, 0x05U, 0x4EU, 0x05U, 0x51U, 0x05U, 0x00U, 0x06U, 0x03U, 0x06U, + 0x06U, 0x06U, 0x09U, 0x06U, 0x0CU, 0x06U, 0x0FU, 0x06U, 0x12U, 0x06U, + 0x15U, 0x06U, 0x18U, 0x06U, 0x1BU, 0x06U, 0x1EU, 0x06U, 0x21U, 0x06U, + 0x24U, 0x06U, 0x27U, 0x06U, 0x2AU, 0x06U, 0x2DU, 0x06U, 0x30U, 0x06U, + 0x33U, 0x06U, 0x36U, 0x06U, 0x39U, 0x06U, 0x3CU, 0x06U, 0x3FU, 0x06U, + 0x42U, 0x06U, 0x45U, 0x06U, 0x48U, 0x06U, 0x4BU, 0x06U, 0x4EU, 0x06U, + 0x51U, 0x06U, 0x00U, 0x07U, 0x03U, 0x07U, 0x06U, 0x07U, 0x09U, 0x07U, + 0x0CU, 0x07U, 0x0FU, 0x07U, 0x12U, 0x07U, 0x15U, 0x07U, 0x18U, 0x07U, + 0x1BU, 0x07U, 0x1EU, 0x07U, 0x21U, 0x07U, 0x24U, 0x07U, 0x27U, 0x07U, + 0x2AU, 0x07U, 0x2DU, 0x07U, 0x30U, 0x07U, 0x33U, 0x07U, 0x36U, 0x07U, + 0x39U, 0x07U, 0x3CU, 0x07U, 0x3FU, 0x07U, 0x42U, 0x07U, 0x45U, 0x07U, + 0x48U, 0x07U, 0x4BU, 0x07U, 0x4EU, 0x07U, 0x51U, 0x07U, 0x01U, 0x00U, + 0x04U, 0x00U, 0x07U, 0x00U, 0x0AU, 0x00U, 0x0DU, 0x00U, 0x10U, 0x00U, + 0x13U, 0x00U, 0x16U, 0x00U, 0x19U, 0x00U, 0x1CU, 0x00U, 0x1FU, 0x00U, + 0x22U, 0x00U, 0x25U, 0x00U, 0x28U, 0x00U, 0x2BU, 0x00U, 0x2EU, 0x00U, + 0x31U, 0x00U, 0x34U, 0x00U, 0x37U, 0x00U, 0x3AU, 0x00U, 0x3DU, 0x00U, + 0x40U, 0x00U, 0x43U, 0x00U, 0x46U, 0x00U, 0x49U, 0x00U, 0x4CU, 0x00U, + 0x4FU, 0x00U, 0x52U, 0x00U, 0x01U, 0x01U, 0x04U, 0x01U, 0x07U, 0x01U, + 0x0AU, 0x01U, 0x0DU, 0x01U, 0x10U, 0x01U, 0x13U, 0x01U, 0x16U, 0x01U, + 0x19U, 0x01U, 0x1CU, 0x01U, 0x1FU, 0x01U, 0x22U, 0x01U, 0x25U, 0x01U, + 0x28U, 0x01U, 0x2BU, 0x01U, 0x2EU, 0x01U, 0x31U, 0x01U, 0x34U, 0x01U, + 0x37U, 0x01U, 0x3AU, 0x01U, 0x3DU, 0x01U, 0x40U, 0x01U, 0x43U, 0x01U, + 0x46U, 0x01U, 0x49U, 0x01U, 0x4CU, 0x01U, 0x4FU, 0x01U, 0x52U, 0x01U, + 0x01U, 0x02U, 0x04U, 0x02U, 0x07U, 0x02U, 0x0AU, 0x02U, 0x0DU, 0x02U, + 0x10U, 0x02U, 0x13U, 0x02U, 0x16U, 0x02U, 0x19U, 0x02U, 0x1CU, 0x02U, + 0x1FU, 0x02U, 0x22U, 0x02U, 0x25U, 0x02U, 0x28U, 0x02U, 0x2BU, 0x02U, + 0x2EU, 0x02U, 0x31U, 0x02U, 0x34U, 0x02U, 0x37U, 0x02U, 0x3AU, 0x02U, + 0x3DU, 0x02U, 0x40U, 0x02U, 0x43U, 0x02U, 0x46U, 0x02U, 0x49U, 0x02U, + 0x4CU, 0x02U, 0x4FU, 0x02U, 0x52U, 0x02U, 0x01U, 0x03U, 0x04U, 0x03U, + 0x07U, 0x03U, 0x0AU, 0x03U, 0x0DU, 0x03U, 0x10U, 0x03U, 0x13U, 0x03U, + 0x16U, 0x03U, 0x19U, 0x03U, 0x1CU, 0x03U, 0x1FU, 0x03U, 0x22U, 0x03U, + 0x25U, 0x03U, 0x28U, 0x03U, 0x2BU, 0x03U, 0x2EU, 0x03U, 0x31U, 0x03U, + 0x34U, 0x03U, 0x37U, 0x03U, 0x3AU, 0x03U, 0x3DU, 0x03U, 0x40U, 0x03U, + 0x43U, 0x03U, 0x46U, 0x03U, 0x49U, 0x03U, 0x4CU, 0x03U, 0x4FU, 0x03U, + 0x52U, 0x03U, 0x01U, 0x04U, 0x04U, 0x04U, 0x07U, 0x04U, 0x0AU, 0x04U, + 0x0DU, 0x04U, 0x10U, 0x04U, 0x13U, 0x04U, 0x16U, 0x04U, 0x19U, 0x04U, + 0x1CU, 0x04U, 0x1FU, 0x04U, 0x22U, 0x04U, 0x25U, 0x04U, 0x28U, 0x04U, + 0x2BU, 0x04U, 0x2EU, 0x04U, 0x31U, 0x04U, 0x34U, 0x04U, 0x37U, 0x04U, + 0x3AU, 0x04U, 0x3DU, 0x04U, 0x40U, 0x04U, 0x43U, 0x04U, 0x46U, 0x04U, + 0x49U, 0x04U, 0x4CU, 0x04U, 0x4FU, 0x04U, 0x01U, 0x05U, 0x04U, 0x05U, + 0x07U, 0x05U, 0x0AU, 0x05U, 0x0DU, 0x05U, 0x10U, 0x05U, 0x13U, 0x05U, + 0x16U, 0x05U, 0x19U, 0x05U, 0x1CU, 0x05U, 0x1FU, 0x05U, 0x22U, 0x05U, + 0x25U, 0x05U, 0x28U, 0x05U, 0x2BU, 0x05U, 0x2EU, 0x05U, 0x31U, 0x05U, + 0x34U, 0x05U, 0x37U, 0x05U, 0x3AU, 0x05U, 0x3DU, 0x05U, 0x40U, 0x05U, + 0x43U, 0x05U, 0x46U, 0x05U, 0x49U, 0x05U, 0x4CU, 0x05U, 0x4FU, 0x05U, + 0x01U, 0x06U, 0x04U, 0x06U, 0x07U, 0x06U, 0x0AU, 0x06U, 0x0DU, 0x06U, + 0x10U, 0x06U, 0x13U, 0x06U, 0x16U, 0x06U, 0x19U, 0x06U, 0x1CU, 0x06U, + 0x1FU, 0x06U, 0x22U, 0x06U, 0x25U, 0x06U, 0x28U, 0x06U, 0x2BU, 0x06U, + 0x2EU, 0x06U, 0x31U, 0x06U, 0x34U, 0x06U, 0x37U, 0x06U, 0x3AU, 0x06U, + 0x3DU, 0x06U, 0x40U, 0x06U, 0x43U, 0x06U, 0x46U, 0x06U, 0x49U, 0x06U, + 0x4CU, 0x06U, 0x4FU, 0x06U, 0x01U, 0x07U, 0x04U, 0x07U, 0x07U, 0x07U, + 0x0AU, 0x07U, 0x0DU, 0x07U, 0x10U, 0x07U, 0x13U, 0x07U, 0x16U, 0x07U, + 0x19U, 0x07U, 0x1CU, 0x07U, 0x1FU, 0x07U, 0x22U, 0x07U, 0x25U, 0x07U, + 0x28U, 0x07U, 0x2BU, 0x07U, 0x2EU, 0x07U, 0x31U, 0x07U, 0x34U, 0x07U, + 0x37U, 0x07U, 0x3AU, 0x07U, 0x3DU, 0x07U, 0x40U, 0x07U, 0x43U, 0x07U, + 0x46U, 0x07U, 0x49U, 0x07U, 0x4CU, 0x07U, 0x4FU, 0x07U, 0x02U, 0x00U, + 0x05U, 0x00U, 0x08U, 0x00U, 0x0BU, 0x00U, 0x0EU, 0x00U, 0x11U, 0x00U, + 0x14U, 0x00U, 0x17U, 0x00U, 0x1AU, 0x00U, 0x1DU, 0x00U, 0x20U, 0x00U, + 0x23U, 0x00U, 0x26U, 0x00U, 0x29U, 0x00U, 0x2CU, 0x00U, 0x2FU, 0x00U, + 0x32U, 0x00U, 0x35U, 0x00U, 0x38U, 0x00U, 0x3BU, 0x00U, 0x3EU, 0x00U, + 0x41U, 0x00U, 0x44U, 0x00U, 0x47U, 0x00U, 0x4AU, 0x00U, 0x4DU, 0x00U, + 0x50U, 0x00U, 0x02U, 0x01U, 0x05U, 0x01U, 0x08U, 0x01U, 0x0BU, 0x01U, + 0x0EU, 0x01U, 0x11U, 0x01U, 0x14U, 0x01U, 0x17U, 0x01U, 0x1AU, 0x01U, + 0x1DU, 0x01U, 0x20U, 0x01U, 0x23U, 0x01U, 0x26U, 0x01U, 0x29U, 0x01U, + 0x2CU, 0x01U, 0x2FU, 0x01U, 0x32U, 0x01U, 0x35U, 0x01U, 0x38U, 0x01U, + 0x3BU, 0x01U, 0x3EU, 0x01U, 0x41U, 0x01U, 0x44U, 0x01U, 0x47U, 0x01U, + 0x4AU, 0x01U, 0x4DU, 0x01U, 0x50U, 0x01U, 0x02U, 0x02U, 0x05U, 0x02U, + 0x08U, 0x02U, 0x0BU, 0x02U, 0x0EU, 0x02U, 0x11U, 0x02U, 0x14U, 0x02U, + 0x17U, 0x02U, 0x1AU, 0x02U, 0x1DU, 0x02U, 0x20U, 0x02U, 0x23U, 0x02U, + 0x26U, 0x02U, 0x29U, 0x02U, 0x2CU, 0x02U, 0x2FU, 0x02U, 0x32U, 0x02U, + 0x35U, 0x02U, 0x38U, 0x02U, 0x3BU, 0x02U, 0x3EU, 0x02U, 0x41U, 0x02U, + 0x44U, 0x02U, 0x47U, 0x02U, 0x4AU, 0x02U, 0x4DU, 0x02U, 0x50U, 0x02U, + 0x02U, 0x03U, 0x05U, 0x03U, 0x08U, 0x03U, 0x0BU, 0x03U, 0x0EU, 0x03U, + 0x11U, 0x03U, 0x14U, 0x03U, 0x17U, 0x03U, 0x1AU, 0x03U, 0x1DU, 0x03U, + 0x20U, 0x03U, 0x23U, 0x03U, 0x26U, 0x03U, 0x29U, 0x03U, 0x2CU, 0x03U, + 0x2FU, 0x03U, 0x32U, 0x03U, 0x35U, 0x03U, 0x38U, 0x03U, 0x3BU, 0x03U, + 0x3EU, 0x03U, 0x41U, 0x03U, 0x44U, 0x03U, 0x47U, 0x03U, 0x4AU, 0x03U, + 0x4DU, 0x03U, 0x50U, 0x03U, 0x02U, 0x04U, 0x05U, 0x04U, 0x08U, 0x04U, + 0x0BU, 0x04U, 0x0EU, 0x04U, 0x11U, 0x04U, 0x14U, 0x04U, 0x17U, 0x04U, + 0x1AU, 0x04U, 0x1DU, 0x04U, 0x20U, 0x04U, 0x23U, 0x04U, 0x26U, 0x04U, + 0x29U, 0x04U, 0x2CU, 0x04U, 0x2FU, 0x04U, 0x32U, 0x04U, 0x35U, 0x04U, + 0x38U, 0x04U, 0x3BU, 0x04U, 0x3EU, 0x04U, 0x41U, 0x04U, 0x44U, 0x04U, + 0x47U, 0x04U, 0x4AU, 0x04U, 0x4DU, 0x04U, 0x50U, 0x04U, 0x02U, 0x05U, + 0x05U, 0x05U, 0x08U, 0x05U, 0x0BU, 0x05U, 0x0EU, 0x05U, 0x11U, 0x05U, + 0x14U, 0x05U, 0x17U, 0x05U, 0x1AU, 0x05U, 0x1DU, 0x05U, 0x20U, 0x05U, + 0x23U, 0x05U, 0x26U, 0x05U, 0x29U, 0x05U, 0x2CU, 0x05U, 0x2FU, 0x05U, + 0x32U, 0x05U, 0x35U, 0x05U, 0x38U, 0x05U, 0x3BU, 0x05U, 0x3EU, 0x05U, + 0x41U, 0x05U, 0x44U, 0x05U, 0x47U, 0x05U, 0x4AU, 0x05U, 0x4DU, 0x05U, + 0x50U, 0x05U, 0x02U, 0x06U, 0x05U, 0x06U, 0x08U, 0x06U, 0x0BU, 0x06U, + 0x0EU, 0x06U, 0x11U, 0x06U, 0x14U, 0x06U, 0x17U, 0x06U, 0x1AU, 0x06U, + 0x1DU, 0x06U, 0x20U, 0x06U, 0x23U, 0x06U, 0x26U, 0x06U, 0x29U, 0x06U, + 0x2CU, 0x06U, 0x2FU, 0x06U, 0x32U, 0x06U, 0x35U, 0x06U, 0x38U, 0x06U, + 0x3BU, 0x06U, 0x3EU, 0x06U, 0x41U, 0x06U, 0x44U, 0x06U, 0x47U, 0x06U, + 0x4AU, 0x06U, 0x4DU, 0x06U, 0x50U, 0x06U, 0x02U, 0x07U, 0x05U, 0x07U, + 0x08U, 0x07U, 0x0BU, 0x07U, 0x0EU, 0x07U, 0x11U, 0x07U, 0x14U, 0x07U, + 0x17U, 0x07U, 0x1AU, 0x07U, 0x1DU, 0x07U, 0x20U, 0x07U, 0x23U, 0x07U, + 0x26U, 0x07U, 0x29U, 0x07U, 0x2CU, 0x07U, 0x2FU, 0x07U, 0x32U, 0x07U, + 0x35U, 0x07U, 0x38U, 0x07U, 0x3BU, 0x07U, 0x3EU, 0x07U, 0x41U, 0x07U, + 0x44U, 0x07U, 0x47U, 0x07U, 0x4AU, 0x07U, 0x4DU, 0x07U, 0x50U, 0x07U, +}; + +const uint8_t SCRAMBLE_TABLE_RX[] = { + 0x70U, 0x4FU, 0x93U, 0x40U, 0x64U, 0x74U, 0x6DU, 0x30U, 0x2BU, 0xE7U, + 0x2DU, 0x54U, 0x5FU, 0x8AU, 0x1DU, 0x7FU, 0xB8U, 0xA7U, 0x49U, 0x20U, + 0x32U, 0xBAU, 0x36U, 0x98U, 0x95U, 0xF3U, 0x16U, 0xAAU, 0x2FU, 0xC5U, + 0x8EU, 0x3FU, 0xDCU, 0xD3U, 0x24U, 0x10U, 0x19U, 0x5DU, 0x1BU, 0xCCU, + 0xCAU, 0x79U, 0x0BU, 0xD5U, 0x97U, 0x62U, 0xC7U, 0x1FU, 0xEEU, 0x69U, + 0x12U, 0x88U, 0x8CU, 0xAEU, 0x0DU, 0x66U, 0xE5U, 0xBCU, 0x85U, 0xEAU, + 0x4BU, 0xB1U, 0xE3U, 0x0FU, 0xF7U, 0x34U, 0x09U, 0x44U, 0x46U, 0xD7U, + 0x06U, 0xB3U, 0x72U, 0xDEU, 0x42U, 0xF5U, 0xA5U, 0xD8U, 0xF1U, 0x87U, + 0x7BU, 0x9AU, 0x04U, 0x22U, 0xA3U, 0x6BU, 0x83U, 0x59U, 0x39U, 0x6FU, + 0x00U}; + +const uint16_t CCITT_TABLE[] = { + 0x0000U, 0x1189U, 0x2312U, 0x329bU, 0x4624U, 0x57adU, 0x6536U, 0x74bfU, + 0x8c48U, 0x9dc1U, 0xaf5aU, 0xbed3U, 0xca6cU, 0xdbe5U, 0xe97eU, 0xf8f7U, + 0x1081U, 0x0108U, 0x3393U, 0x221aU, 0x56a5U, 0x472cU, 0x75b7U, 0x643eU, + 0x9cc9U, 0x8d40U, 0xbfdbU, 0xae52U, 0xdaedU, 0xcb64U, 0xf9ffU, 0xe876U, + 0x2102U, 0x308bU, 0x0210U, 0x1399U, 0x6726U, 0x76afU, 0x4434U, 0x55bdU, + 0xad4aU, 0xbcc3U, 0x8e58U, 0x9fd1U, 0xeb6eU, 0xfae7U, 0xc87cU, 0xd9f5U, + 0x3183U, 0x200aU, 0x1291U, 0x0318U, 0x77a7U, 0x662eU, 0x54b5U, 0x453cU, + 0xbdcbU, 0xac42U, 0x9ed9U, 0x8f50U, 0xfbefU, 0xea66U, 0xd8fdU, 0xc974U, + 0x4204U, 0x538dU, 0x6116U, 0x709fU, 0x0420U, 0x15a9U, 0x2732U, 0x36bbU, + 0xce4cU, 0xdfc5U, 0xed5eU, 0xfcd7U, 0x8868U, 0x99e1U, 0xab7aU, 0xbaf3U, + 0x5285U, 0x430cU, 0x7197U, 0x601eU, 0x14a1U, 0x0528U, 0x37b3U, 0x263aU, + 0xdecdU, 0xcf44U, 0xfddfU, 0xec56U, 0x98e9U, 0x8960U, 0xbbfbU, 0xaa72U, + 0x6306U, 0x728fU, 0x4014U, 0x519dU, 0x2522U, 0x34abU, 0x0630U, 0x17b9U, + 0xef4eU, 0xfec7U, 0xcc5cU, 0xddd5U, 0xa96aU, 0xb8e3U, 0x8a78U, 0x9bf1U, + 0x7387U, 0x620eU, 0x5095U, 0x411cU, 0x35a3U, 0x242aU, 0x16b1U, 0x0738U, + 0xffcfU, 0xee46U, 0xdcddU, 0xcd54U, 0xb9ebU, 0xa862U, 0x9af9U, 0x8b70U, + 0x8408U, 0x9581U, 0xa71aU, 0xb693U, 0xc22cU, 0xd3a5U, 0xe13eU, 0xf0b7U, + 0x0840U, 0x19c9U, 0x2b52U, 0x3adbU, 0x4e64U, 0x5fedU, 0x6d76U, 0x7cffU, + 0x9489U, 0x8500U, 0xb79bU, 0xa612U, 0xd2adU, 0xc324U, 0xf1bfU, 0xe036U, + 0x18c1U, 0x0948U, 0x3bd3U, 0x2a5aU, 0x5ee5U, 0x4f6cU, 0x7df7U, 0x6c7eU, + 0xa50aU, 0xb483U, 0x8618U, 0x9791U, 0xe32eU, 0xf2a7U, 0xc03cU, 0xd1b5U, + 0x2942U, 0x38cbU, 0x0a50U, 0x1bd9U, 0x6f66U, 0x7eefU, 0x4c74U, 0x5dfdU, + 0xb58bU, 0xa402U, 0x9699U, 0x8710U, 0xf3afU, 0xe226U, 0xd0bdU, 0xc134U, + 0x39c3U, 0x284aU, 0x1ad1U, 0x0b58U, 0x7fe7U, 0x6e6eU, 0x5cf5U, 0x4d7cU, + 0xc60cU, 0xd785U, 0xe51eU, 0xf497U, 0x8028U, 0x91a1U, 0xa33aU, 0xb2b3U, + 0x4a44U, 0x5bcdU, 0x6956U, 0x78dfU, 0x0c60U, 0x1de9U, 0x2f72U, 0x3efbU, + 0xd68dU, 0xc704U, 0xf59fU, 0xe416U, 0x90a9U, 0x8120U, 0xb3bbU, 0xa232U, + 0x5ac5U, 0x4b4cU, 0x79d7U, 0x685eU, 0x1ce1U, 0x0d68U, 0x3ff3U, 0x2e7aU, + 0xe70eU, 0xf687U, 0xc41cU, 0xd595U, 0xa12aU, 0xb0a3U, 0x8238U, 0x93b1U, + 0x6b46U, 0x7acfU, 0x4854U, 0x59ddU, 0x2d62U, 0x3cebU, 0x0e70U, 0x1ff9U, + 0xf78fU, 0xe606U, 0xd49dU, 0xc514U, 0xb1abU, 0xa022U, 0x92b9U, 0x8330U, + 0x7bc7U, 0x6a4eU, 0x58d5U, 0x495cU, 0x3de3U, 0x2c6aU, 0x1ef1U, 0x0f78U}; + +CDStarRX::CDStarRX() : +m_pll(0U), +m_prev(false), +m_rxState(DSRXS_NONE), +m_patternBuffer(0x00U), +m_rxBuffer(), +m_rxBufferBits(0U), +m_dataBits(0U), +m_mar(0U), +m_pathMetric(), +m_pathMemory0(), +m_pathMemory1(), +m_pathMemory2(), +m_pathMemory3(), +m_fecOutput(), +m_samples(), +m_samplesPtr(0U) +{ +} + +void CDStarRX::reset() +{ + m_pll = 0U; + m_prev = false; + m_rxState = DSRXS_NONE; + m_patternBuffer = 0x00U; + m_rxBufferBits = 0U; + m_dataBits = 0U; + m_samplesPtr = 0U; +} + +void CDStarRX::samples(const q15_t* samples, uint8_t length) +{ + for (uint16_t i = 0U; i < length; i++) { + m_samples[m_samplesPtr] = samples[i]; + + bool bit = samples[i] < THRESHOLD; + + if (bit != m_prev) { + if (m_pll < (PLLMAX / 2U)) + m_pll += INC; + else + m_pll -= INC; + } + + m_prev = bit; + + m_pll += PLLINC; + + if (m_pll >= PLLMAX) { + m_pll -= PLLMAX; + + switch (m_rxState) { + case DSRXS_NONE: + processNone(bit); + break; + case DSRXS_HEADER: + processHeader(bit); + break; + case DSRXS_DATA: + processData(bit); + break; + default: + break; + } + } + + m_samplesPtr++; + if (m_samplesPtr >= DSTAR_DATA_SYNC_LENGTH_BITS) + m_samplesPtr = 0U; + } +} + +void CDStarRX::processNone(bool bit) +{ + m_patternBuffer <<= 1; + if (bit) + m_patternBuffer |= 0x01U; + + // Exact matching of the frame sync sequence + if (countBits32((m_patternBuffer & FRAME_SYNC_MASK) ^ FRAME_SYNC_DATA) == 0U) { + DEBUG1("DStarRX: found frame sync in None"); + + ::memset(m_rxBuffer, 0x00U, DSTAR_FEC_SECTION_LENGTH_BYTES); + m_rxBufferBits = 0U; + + m_rxState = DSRXS_HEADER; + return; + } + + // Exact matching of the data sync bit sequence + if (countBits32((m_patternBuffer & DATA_SYNC_MASK) ^ DATA_SYNC_DATA) == 0U) { + DEBUG1("DStarRX: found data sync in None"); + io.setDecode(true); + +#if defined(WANT_DEBUG) + q15_t min = 16000; + q15_t max = -16000; + for (uint8_t i = 0U; i < DSTAR_DATA_SYNC_LENGTH_BITS; i++) { + if (m_samples[i] > max) + max = m_samples[i]; + if (m_samples[i] < min) + min = m_samples[i]; + } + DEBUG3("DStarRX: data sync found min/max", min, max); +#endif + + serial.writeDStarData(DSTAR_DATA_SYNC_BYTES, DSTAR_DATA_LENGTH_BYTES); + + ::memset(m_rxBuffer, 0x00U, DSTAR_DATA_LENGTH_BYTES); + m_rxBufferBits = 0U; + + m_dataBits = MAX_SYNC_BITS; + m_rxState = DSRXS_DATA; + return; + } +} + +void CDStarRX::processHeader(bool bit) +{ + m_patternBuffer <<= 1; + if (bit) + m_patternBuffer |= 0x01U; + + WRITE_BIT2(m_rxBuffer, m_rxBufferBits, bit); + m_rxBufferBits++; + + // A full FEC header + if (m_rxBufferBits == DSTAR_FEC_SECTION_LENGTH_BITS) { + // Process the scrambling, interleaving and FEC, then return if the chcksum was correct + unsigned char header[DSTAR_HEADER_LENGTH_BYTES]; + bool ok = rxHeader(m_rxBuffer, header); + if (ok) { + DEBUG1("DStarRX: header checksum ok"); + io.setDecode(true); + + serial.writeDStarHeader(header, DSTAR_HEADER_LENGTH_BYTES); + + ::memset(m_rxBuffer, 0x00U, DSTAR_DATA_LENGTH_BYTES); + m_rxBufferBits = 0U; + + m_rxState = DSRXS_DATA; + m_dataBits = MAX_SYNC_BITS; + } else { + DEBUG1("DStarRX: header checksum failed"); + + // The checksum failed, return to looking for syncs + m_rxState = DSRXS_NONE; + } + } +} + +void CDStarRX::processData(bool bit) +{ + m_patternBuffer <<= 1; + if (bit) + m_patternBuffer |= 0x01U; + + WRITE_BIT2(m_rxBuffer, m_rxBufferBits, bit); + m_rxBufferBits++; + + // Fuzzy matching of the end frame sequences + if (countBits32((m_patternBuffer & END_SYNC_MASK) ^ END_SYNC_DATA) <= END_SYNC_ERRS) { + DEBUG1("DStarRX: Found end sync in Data"); + io.setDecode(false); + + serial.writeDStarEOT(); + + m_rxState = DSRXS_NONE; + return; + } + + // Fuzzy matching of the data sync bit sequence + bool syncSeen = false; + if (m_rxBufferBits >= (DSTAR_DATA_LENGTH_BITS - 3U)) { + if (countBits32((m_patternBuffer & DATA_SYNC_MASK) ^ DATA_SYNC_DATA) <= DATA_SYNC_ERRS) { +#if defined(WANT_DEBUG) + if (m_rxBufferBits < DSTAR_DATA_LENGTH_BITS) + DEBUG2("DStarRX: found data sync in Data, early", DSTAR_DATA_LENGTH_BITS - m_rxBufferBits); + else + DEBUG1("DStarRX: found data sync in Data"); + + q15_t min = 16000; + q15_t max = -16000; + for (uint8_t i = 0U; i < DSTAR_DATA_SYNC_LENGTH_BITS; i++) { + if (m_samples[i] > max) + max = m_samples[i]; + if (m_samples[i] < min) + min = m_samples[i]; + } + + DEBUG3("DStarRX: data sync found min/max", min, max); +#endif + m_rxBufferBits = DSTAR_DATA_LENGTH_BITS; + m_dataBits = MAX_SYNC_BITS; + syncSeen = true; + } + } + + // We've not seen a data sync for too long, signal RXLOST and change to RX_NONE + m_dataBits--; + if (m_dataBits == 0U) { + DEBUG1("DStarRX: data sync timed out, lost lock"); + io.setDecode(false); + + serial.writeDStarLost(); + + m_rxState = DSRXS_NONE; + return; + } + + // Check to see if the sync is arriving late + if (m_rxBufferBits == DSTAR_DATA_LENGTH_BITS && !syncSeen) { + for (uint8_t i = 1U; i <= 3U; i++) { + uint32_t syncMask = DATA_SYNC_MASK >> i; + uint32_t syncData = DATA_SYNC_DATA >> i; + if (countBits32((m_patternBuffer & syncMask) ^ syncData) <= DATA_SYNC_ERRS) { + DEBUG2("DStarRX: found data sync in Data, late", i); + m_rxBufferBits -= i; + break; + } + } + } + + // Send a data frame to the host if the required number of bits have been received, or if a data sync has been seen + if (m_rxBufferBits == DSTAR_DATA_LENGTH_BITS) { + if (syncSeen) { + m_rxBuffer[9U] = DSTAR_DATA_SYNC_BYTES[9U]; + m_rxBuffer[10U] = DSTAR_DATA_SYNC_BYTES[10U]; + m_rxBuffer[11U] = DSTAR_DATA_SYNC_BYTES[11U]; + } + + serial.writeDStarData(m_rxBuffer, DSTAR_DATA_LENGTH_BYTES); + + // Start the next frame + ::memset(m_rxBuffer, 0x00U, DSTAR_DATA_LENGTH_BYTES); + m_rxBufferBits = 0U; + } +} + +bool CDStarRX::rxHeader(uint8_t* in, uint8_t* out) +{ + int i; + + // Descramble the header + for (i = 0; i < int(DSTAR_FEC_SECTION_LENGTH_BYTES); i++) + in[i] ^= SCRAMBLE_TABLE_RX[i]; + + unsigned char intermediate[84U]; + for (i = 0; i < 84; i++) + intermediate[i] = 0x00U; + + // Deinterleave the header + i = 0; + while (i < 660) { + unsigned char d = in[i / 8]; + + if (d & 0x01U) + intermediate[INTERLEAVE_TABLE_RX[i * 2U]] |= (0x80U >> INTERLEAVE_TABLE_RX[i * 2U + 1U]); + i++; + + if (d & 0x02U) + intermediate[INTERLEAVE_TABLE_RX[i * 2U]] |= (0x80U >> INTERLEAVE_TABLE_RX[i * 2U + 1U]); + i++; + + if (d & 0x04U) + intermediate[INTERLEAVE_TABLE_RX[i * 2U]] |= (0x80U >> INTERLEAVE_TABLE_RX[i * 2U + 1U]); + i++; + + if (d & 0x08U) + intermediate[INTERLEAVE_TABLE_RX[i * 2U]] |= (0x80U >> INTERLEAVE_TABLE_RX[i * 2U + 1U]); + i++; + + if (i < 660) { + if (d & 0x10U) + intermediate[INTERLEAVE_TABLE_RX[i * 2U]] |= (0x80U >> INTERLEAVE_TABLE_RX[i * 2U + 1U]); + i++; + + if (d & 0x20U) + intermediate[INTERLEAVE_TABLE_RX[i * 2U]] |= (0x80U >> INTERLEAVE_TABLE_RX[i * 2U + 1U]); + i++; + + if (d & 0x40U) + intermediate[INTERLEAVE_TABLE_RX[i * 2U]] |= (0x80U >> INTERLEAVE_TABLE_RX[i * 2U + 1U]); + i++; + + if (d & 0x80U) + intermediate[INTERLEAVE_TABLE_RX[i * 2U]] |= (0x80U >> INTERLEAVE_TABLE_RX[i * 2U + 1U]); + i++; + } + } + + for (i = 0; i < 4; i++) + m_pathMetric[i] = 0; + + int decodeData[2U]; + + m_mar = 0U; + for (i = 0; i < 660; i += 2) { + if (intermediate[i >> 3] & (0x80U >> (i & 7))) + decodeData[1U] = 1U; + else + decodeData[1U] = 0U; + + if (intermediate[i >> 3] & (0x40U >> (i & 7))) + decodeData[0U] = 1U; + else + decodeData[0U] = 0U; + + viterbiDecode(decodeData); + } + + traceBack(); + + for (i = 0; i < int(DSTAR_HEADER_LENGTH_BYTES); i++) + out[i] = 0x00U; + + unsigned int j = 0; + for (i = 329; i >= 0; i--) { + if (READ_BIT1(m_fecOutput, i)) + out[j >> 3] |= (0x01U << (j & 7)); + + j++; + } + + return checksum(out); +} + +void CDStarRX::acs(int* metric) +{ + int tempMetric[4U]; + + unsigned int j = m_mar >> 3; + unsigned int k = m_mar & 7; + + // Pres. state = S0, Prev. state = S0 & S2 + int m1 = metric[0U] + m_pathMetric[0U]; + int m2 = metric[4U] + m_pathMetric[2U]; + tempMetric[0U] = m1 < m2 ? m1 : m2; + if (m1 < m2) + m_pathMemory0[j] &= BIT_MASK_TABLE0[k]; + else + m_pathMemory0[j] |= BIT_MASK_TABLE1[k]; + + // Pres. state = S1, Prev. state = S0 & S2 + m1 = metric[1U] + m_pathMetric[0U]; + m2 = metric[5U] + m_pathMetric[2U]; + tempMetric[1U] = m1 < m2 ? m1 : m2; + if (m1 < m2) + m_pathMemory1[j] &= BIT_MASK_TABLE0[k]; + else + m_pathMemory1[j] |= BIT_MASK_TABLE1[k]; + + // Pres. state = S2, Prev. state = S2 & S3 + m1 = metric[2U] + m_pathMetric[1U]; + m2 = metric[6U] + m_pathMetric[3U]; + tempMetric[2U] = m1 < m2 ? m1 : m2; + if (m1 < m2) + m_pathMemory2[j] &= BIT_MASK_TABLE0[k]; + else + m_pathMemory2[j] |= BIT_MASK_TABLE1[k]; + + // Pres. state = S3, Prev. state = S1 & S3 + m1 = metric[3U] + m_pathMetric[1U]; + m2 = metric[7U] + m_pathMetric[3U]; + tempMetric[3U] = m1 < m2 ? m1 : m2; + if (m1 < m2) + m_pathMemory3[j] &= BIT_MASK_TABLE0[k]; + else + m_pathMemory3[j] |= BIT_MASK_TABLE1[k]; + + for (unsigned int i = 0U; i < 4U; i++) + m_pathMetric[i] = tempMetric[i]; + + m_mar++; +} + +void CDStarRX::viterbiDecode(int* data) +{ + int metric[8U]; + + metric[0] = (data[1] ^ 0) + (data[0] ^ 0); + metric[1] = (data[1] ^ 1) + (data[0] ^ 1); + metric[2] = (data[1] ^ 1) + (data[0] ^ 0); + metric[3] = (data[1] ^ 0) + (data[0] ^ 1); + metric[4] = (data[1] ^ 1) + (data[0] ^ 1); + metric[5] = (data[1] ^ 0) + (data[0] ^ 0); + metric[6] = (data[1] ^ 0) + (data[0] ^ 1); + metric[7] = (data[1] ^ 1) + (data[0] ^ 0); + + acs(metric); +} + +void CDStarRX::traceBack() +{ + // Start from the S0, t=31 + unsigned int j = 0U; + unsigned int k = 0U; + for (int i = 329; i >= 0; i--) { + switch (j) { + case 0U: // if state = S0 + if (!READ_BIT1(m_pathMemory0, i)) + j = 0U; + else + j = 2U; + WRITE_BIT1(m_fecOutput, k, false); + k++; + break; + + + case 1U: // if state = S1 + if (!READ_BIT1(m_pathMemory1, i)) + j = 0U; + else + j = 2U; + WRITE_BIT1(m_fecOutput, k, true); + k++; + break; + + case 2U: // if state = S1 + if (!READ_BIT1(m_pathMemory2, i)) + j = 1U; + else + j = 3U; + WRITE_BIT1(m_fecOutput, k, false); + k++; + break; + + case 3U: // if state = S1 + if (!READ_BIT1(m_pathMemory3, i)) + j = 1U; + else + j = 3U; + WRITE_BIT1(m_fecOutput, k, true); + k++; + break; + } + } +} + +bool CDStarRX::checksum(const uint8_t* header) const +{ + union { + uint16_t crc16; + uint8_t crc8[2U]; + }; + + crc16 = 0xFFFFU; + for (uint8_t i = 0U; i < (DSTAR_HEADER_LENGTH_BYTES - 2U); i++) + crc16 = uint16_t(crc8[1U]) ^ CCITT_TABLE[crc8[0U] ^ header[i]]; + + crc16 = ~crc16; + + return crc8[0U] == header[DSTAR_HEADER_LENGTH_BYTES - 2U] && crc8[1U] == header[DSTAR_HEADER_LENGTH_BYTES - 1U]; +} + diff --git a/DStarRX.h b/DStarRX.h new file mode 100644 index 0000000..ee5c2a9 --- /dev/null +++ b/DStarRX.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2015 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(DSTARRX_H) +#define DSTARRX_H + +#include "Config.h" +#include "DStarDefines.h" + +enum DSRX_STATE { + DSRXS_NONE, + DSRXS_HEADER, + DSRXS_DATA +}; + +class CDStarRX { +public: + CDStarRX(); + + void samples(const q15_t* samples, uint8_t length); + + void reset(); + +private: + uint32_t m_pll; + bool m_prev; + DSRX_STATE m_rxState; + uint32_t m_patternBuffer; + uint8_t m_rxBuffer[100U]; + unsigned int m_rxBufferBits; + unsigned int m_dataBits; + unsigned int m_mar; + int m_pathMetric[4U]; + unsigned int m_pathMemory0[42U]; + unsigned int m_pathMemory1[42U]; + unsigned int m_pathMemory2[42U]; + unsigned int m_pathMemory3[42U]; + uint8_t m_fecOutput[42U]; + q15_t m_samples[DSTAR_DATA_SYNC_LENGTH_BITS]; + uint8_t m_samplesPtr; + + void processNone(bool bit); + void processHeader(bool bit); + void processData(bool bit); + bool rxHeader(uint8_t* in, uint8_t* out); + void acs(int* metric); + void viterbiDecode(int* data); + void traceBack(); + bool checksum(const uint8_t* header) const; +}; + +#endif + diff --git a/DStarTX.cpp b/DStarTX.cpp new file mode 100644 index 0000000..613831d --- /dev/null +++ b/DStarTX.cpp @@ -0,0 +1,435 @@ +/* + * Copyright (C) 2009-2015 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. + */ + +#include "Config.h" +#include "Globals.h" +#include "DStarTX.h" + +#include "DStarDefines.h" + +const uint8_t BIT_SYNC = 0xAAU; + +const uint8_t FRAME_SYNC[] = {0xEAU, 0xA6U, 0x00U}; + +// Generated using gaussfir(0.5, 4, 5) in MATLAB +static q15_t DSTAR_GMSK_FILTER[] = {8, 104, 760, 3158, 7421, 9866, 7421, 3158, 760, 104, 8, 0}; +const uint16_t DSTAR_GMSK_FILTER_LEN = 12U; + +q15_t DSTAR_1[] = { 1827, 1827, 1827, 1827, 1827}; +q15_t DSTAR_0[] = {-1827, -1827, -1827, -1827, -1827}; + +const uint8_t BIT_MASK_TABLE[] = {0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U}; + +const uint8_t INTERLEAVE_TABLE_TX[] = { + 0x00U, 0x04U, 0x04U, 0x00U, 0x07U, 0x04U, 0x0BU, 0x00U, 0x0EU, 0x04U, + 0x12U, 0x00U, 0x15U, 0x04U, 0x19U, 0x00U, 0x1CU, 0x04U, 0x20U, 0x00U, + 0x23U, 0x04U, 0x27U, 0x00U, 0x2AU, 0x04U, 0x2DU, 0x07U, 0x31U, 0x02U, + 0x34U, 0x05U, 0x38U, 0x00U, 0x3BU, 0x03U, 0x3EU, 0x06U, 0x42U, 0x01U, + 0x45U, 0x04U, 0x48U, 0x07U, 0x4CU, 0x02U, 0x4FU, 0x05U, 0x00U, 0x05U, + 0x04U, 0x01U, 0x07U, 0x05U, 0x0BU, 0x01U, 0x0EU, 0x05U, 0x12U, 0x01U, + 0x15U, 0x05U, 0x19U, 0x01U, 0x1CU, 0x05U, 0x20U, 0x01U, 0x23U, 0x05U, + 0x27U, 0x01U, 0x2AU, 0x05U, 0x2EU, 0x00U, 0x31U, 0x03U, 0x34U, 0x06U, + 0x38U, 0x01U, 0x3BU, 0x04U, 0x3EU, 0x07U, 0x42U, 0x02U, 0x45U, 0x05U, + 0x49U, 0x00U, 0x4CU, 0x03U, 0x4FU, 0x06U, 0x00U, 0x06U, 0x04U, 0x02U, + 0x07U, 0x06U, 0x0BU, 0x02U, 0x0EU, 0x06U, 0x12U, 0x02U, 0x15U, 0x06U, + 0x19U, 0x02U, 0x1CU, 0x06U, 0x20U, 0x02U, 0x23U, 0x06U, 0x27U, 0x02U, + 0x2AU, 0x06U, 0x2EU, 0x01U, 0x31U, 0x04U, 0x34U, 0x07U, 0x38U, 0x02U, + 0x3BU, 0x05U, 0x3FU, 0x00U, 0x42U, 0x03U, 0x45U, 0x06U, 0x49U, 0x01U, + 0x4CU, 0x04U, 0x4FU, 0x07U, 0x00U, 0x07U, 0x04U, 0x03U, 0x07U, 0x07U, + 0x0BU, 0x03U, 0x0EU, 0x07U, 0x12U, 0x03U, 0x15U, 0x07U, 0x19U, 0x03U, + 0x1CU, 0x07U, 0x20U, 0x03U, 0x23U, 0x07U, 0x27U, 0x03U, 0x2AU, 0x07U, + 0x2EU, 0x02U, 0x31U, 0x05U, 0x35U, 0x00U, 0x38U, 0x03U, 0x3BU, 0x06U, + 0x3FU, 0x01U, 0x42U, 0x04U, 0x45U, 0x07U, 0x49U, 0x02U, 0x4CU, 0x05U, + 0x50U, 0x00U, 0x01U, 0x00U, 0x04U, 0x04U, 0x08U, 0x00U, 0x0BU, 0x04U, + 0x0FU, 0x00U, 0x12U, 0x04U, 0x16U, 0x00U, 0x19U, 0x04U, 0x1DU, 0x00U, + 0x20U, 0x04U, 0x24U, 0x00U, 0x27U, 0x04U, 0x2BU, 0x00U, 0x2EU, 0x03U, + 0x31U, 0x06U, 0x35U, 0x01U, 0x38U, 0x04U, 0x3BU, 0x07U, 0x3FU, 0x02U, + 0x42U, 0x05U, 0x46U, 0x00U, 0x49U, 0x03U, 0x4CU, 0x06U, 0x50U, 0x01U, + 0x01U, 0x01U, 0x04U, 0x05U, 0x08U, 0x01U, 0x0BU, 0x05U, 0x0FU, 0x01U, + 0x12U, 0x05U, 0x16U, 0x01U, 0x19U, 0x05U, 0x1DU, 0x01U, 0x20U, 0x05U, + 0x24U, 0x01U, 0x27U, 0x05U, 0x2BU, 0x01U, 0x2EU, 0x04U, 0x31U, 0x07U, + 0x35U, 0x02U, 0x38U, 0x05U, 0x3CU, 0x00U, 0x3FU, 0x03U, 0x42U, 0x06U, + 0x46U, 0x01U, 0x49U, 0x04U, 0x4CU, 0x07U, 0x50U, 0x02U, 0x01U, 0x02U, + 0x04U, 0x06U, 0x08U, 0x02U, 0x0BU, 0x06U, 0x0FU, 0x02U, 0x12U, 0x06U, + 0x16U, 0x02U, 0x19U, 0x06U, 0x1DU, 0x02U, 0x20U, 0x06U, 0x24U, 0x02U, + 0x27U, 0x06U, 0x2BU, 0x02U, 0x2EU, 0x05U, 0x32U, 0x00U, 0x35U, 0x03U, + 0x38U, 0x06U, 0x3CU, 0x01U, 0x3FU, 0x04U, 0x42U, 0x07U, 0x46U, 0x02U, + 0x49U, 0x05U, 0x4DU, 0x00U, 0x50U, 0x03U, 0x01U, 0x03U, 0x04U, 0x07U, + 0x08U, 0x03U, 0x0BU, 0x07U, 0x0FU, 0x03U, 0x12U, 0x07U, 0x16U, 0x03U, + 0x19U, 0x07U, 0x1DU, 0x03U, 0x20U, 0x07U, 0x24U, 0x03U, 0x27U, 0x07U, + 0x2BU, 0x03U, 0x2EU, 0x06U, 0x32U, 0x01U, 0x35U, 0x04U, 0x38U, 0x07U, + 0x3CU, 0x02U, 0x3FU, 0x05U, 0x43U, 0x00U, 0x46U, 0x03U, 0x49U, 0x06U, + 0x4DU, 0x01U, 0x50U, 0x04U, 0x01U, 0x04U, 0x05U, 0x00U, 0x08U, 0x04U, + 0x0CU, 0x00U, 0x0FU, 0x04U, 0x13U, 0x00U, 0x16U, 0x04U, 0x1AU, 0x00U, + 0x1DU, 0x04U, 0x21U, 0x00U, 0x24U, 0x04U, 0x28U, 0x00U, 0x2BU, 0x04U, + 0x2EU, 0x07U, 0x32U, 0x02U, 0x35U, 0x05U, 0x39U, 0x00U, 0x3CU, 0x03U, + 0x3FU, 0x06U, 0x43U, 0x01U, 0x46U, 0x04U, 0x49U, 0x07U, 0x4DU, 0x02U, + 0x50U, 0x05U, 0x01U, 0x05U, 0x05U, 0x01U, 0x08U, 0x05U, 0x0CU, 0x01U, + 0x0FU, 0x05U, 0x13U, 0x01U, 0x16U, 0x05U, 0x1AU, 0x01U, 0x1DU, 0x05U, + 0x21U, 0x01U, 0x24U, 0x05U, 0x28U, 0x01U, 0x2BU, 0x05U, 0x2FU, 0x00U, + 0x32U, 0x03U, 0x35U, 0x06U, 0x39U, 0x01U, 0x3CU, 0x04U, 0x3FU, 0x07U, + 0x43U, 0x02U, 0x46U, 0x05U, 0x4AU, 0x00U, 0x4DU, 0x03U, 0x50U, 0x06U, + 0x01U, 0x06U, 0x05U, 0x02U, 0x08U, 0x06U, 0x0CU, 0x02U, 0x0FU, 0x06U, + 0x13U, 0x02U, 0x16U, 0x06U, 0x1AU, 0x02U, 0x1DU, 0x06U, 0x21U, 0x02U, + 0x24U, 0x06U, 0x28U, 0x02U, 0x2BU, 0x06U, 0x2FU, 0x01U, 0x32U, 0x04U, + 0x35U, 0x07U, 0x39U, 0x02U, 0x3CU, 0x05U, 0x40U, 0x00U, 0x43U, 0x03U, + 0x46U, 0x06U, 0x4AU, 0x01U, 0x4DU, 0x04U, 0x50U, 0x07U, 0x01U, 0x07U, + 0x05U, 0x03U, 0x08U, 0x07U, 0x0CU, 0x03U, 0x0FU, 0x07U, 0x13U, 0x03U, + 0x16U, 0x07U, 0x1AU, 0x03U, 0x1DU, 0x07U, 0x21U, 0x03U, 0x24U, 0x07U, + 0x28U, 0x03U, 0x2BU, 0x07U, 0x2FU, 0x02U, 0x32U, 0x05U, 0x36U, 0x00U, + 0x39U, 0x03U, 0x3CU, 0x06U, 0x40U, 0x01U, 0x43U, 0x04U, 0x46U, 0x07U, + 0x4AU, 0x02U, 0x4DU, 0x05U, 0x51U, 0x00U, 0x02U, 0x00U, 0x05U, 0x04U, + 0x09U, 0x00U, 0x0CU, 0x04U, 0x10U, 0x00U, 0x13U, 0x04U, 0x17U, 0x00U, + 0x1AU, 0x04U, 0x1EU, 0x00U, 0x21U, 0x04U, 0x25U, 0x00U, 0x28U, 0x04U, + 0x2CU, 0x00U, 0x2FU, 0x03U, 0x32U, 0x06U, 0x36U, 0x01U, 0x39U, 0x04U, + 0x3CU, 0x07U, 0x40U, 0x02U, 0x43U, 0x05U, 0x47U, 0x00U, 0x4AU, 0x03U, + 0x4DU, 0x06U, 0x51U, 0x01U, 0x02U, 0x01U, 0x05U, 0x05U, 0x09U, 0x01U, + 0x0CU, 0x05U, 0x10U, 0x01U, 0x13U, 0x05U, 0x17U, 0x01U, 0x1AU, 0x05U, + 0x1EU, 0x01U, 0x21U, 0x05U, 0x25U, 0x01U, 0x28U, 0x05U, 0x2CU, 0x01U, + 0x2FU, 0x04U, 0x32U, 0x07U, 0x36U, 0x02U, 0x39U, 0x05U, 0x3DU, 0x00U, + 0x40U, 0x03U, 0x43U, 0x06U, 0x47U, 0x01U, 0x4AU, 0x04U, 0x4DU, 0x07U, + 0x51U, 0x02U, 0x02U, 0x02U, 0x05U, 0x06U, 0x09U, 0x02U, 0x0CU, 0x06U, + 0x10U, 0x02U, 0x13U, 0x06U, 0x17U, 0x02U, 0x1AU, 0x06U, 0x1EU, 0x02U, + 0x21U, 0x06U, 0x25U, 0x02U, 0x28U, 0x06U, 0x2CU, 0x02U, 0x2FU, 0x05U, + 0x33U, 0x00U, 0x36U, 0x03U, 0x39U, 0x06U, 0x3DU, 0x01U, 0x40U, 0x04U, + 0x43U, 0x07U, 0x47U, 0x02U, 0x4AU, 0x05U, 0x4EU, 0x00U, 0x51U, 0x03U, + 0x02U, 0x03U, 0x05U, 0x07U, 0x09U, 0x03U, 0x0CU, 0x07U, 0x10U, 0x03U, + 0x13U, 0x07U, 0x17U, 0x03U, 0x1AU, 0x07U, 0x1EU, 0x03U, 0x21U, 0x07U, + 0x25U, 0x03U, 0x28U, 0x07U, 0x2CU, 0x03U, 0x2FU, 0x06U, 0x33U, 0x01U, + 0x36U, 0x04U, 0x39U, 0x07U, 0x3DU, 0x02U, 0x40U, 0x05U, 0x44U, 0x00U, + 0x47U, 0x03U, 0x4AU, 0x06U, 0x4EU, 0x01U, 0x51U, 0x04U, 0x02U, 0x04U, + 0x06U, 0x00U, 0x09U, 0x04U, 0x0DU, 0x00U, 0x10U, 0x04U, 0x14U, 0x00U, + 0x17U, 0x04U, 0x1BU, 0x00U, 0x1EU, 0x04U, 0x22U, 0x00U, 0x25U, 0x04U, + 0x29U, 0x00U, 0x2CU, 0x04U, 0x2FU, 0x07U, 0x33U, 0x02U, 0x36U, 0x05U, + 0x3AU, 0x00U, 0x3DU, 0x03U, 0x40U, 0x06U, 0x44U, 0x01U, 0x47U, 0x04U, + 0x4AU, 0x07U, 0x4EU, 0x02U, 0x51U, 0x05U, 0x02U, 0x05U, 0x06U, 0x01U, + 0x09U, 0x05U, 0x0DU, 0x01U, 0x10U, 0x05U, 0x14U, 0x01U, 0x17U, 0x05U, + 0x1BU, 0x01U, 0x1EU, 0x05U, 0x22U, 0x01U, 0x25U, 0x05U, 0x29U, 0x01U, + 0x2CU, 0x05U, 0x30U, 0x00U, 0x33U, 0x03U, 0x36U, 0x06U, 0x3AU, 0x01U, + 0x3DU, 0x04U, 0x40U, 0x07U, 0x44U, 0x02U, 0x47U, 0x05U, 0x4BU, 0x00U, + 0x4EU, 0x03U, 0x51U, 0x06U, 0x02U, 0x06U, 0x06U, 0x02U, 0x09U, 0x06U, + 0x0DU, 0x02U, 0x10U, 0x06U, 0x14U, 0x02U, 0x17U, 0x06U, 0x1BU, 0x02U, + 0x1EU, 0x06U, 0x22U, 0x02U, 0x25U, 0x06U, 0x29U, 0x02U, 0x2CU, 0x06U, + 0x30U, 0x01U, 0x33U, 0x04U, 0x36U, 0x07U, 0x3AU, 0x02U, 0x3DU, 0x05U, + 0x41U, 0x00U, 0x44U, 0x03U, 0x47U, 0x06U, 0x4BU, 0x01U, 0x4EU, 0x04U, + 0x51U, 0x07U, 0x02U, 0x07U, 0x06U, 0x03U, 0x09U, 0x07U, 0x0DU, 0x03U, + 0x10U, 0x07U, 0x14U, 0x03U, 0x17U, 0x07U, 0x1BU, 0x03U, 0x1EU, 0x07U, + 0x22U, 0x03U, 0x25U, 0x07U, 0x29U, 0x03U, 0x2CU, 0x07U, 0x30U, 0x02U, + 0x33U, 0x05U, 0x37U, 0x00U, 0x3AU, 0x03U, 0x3DU, 0x06U, 0x41U, 0x01U, + 0x44U, 0x04U, 0x47U, 0x07U, 0x4BU, 0x02U, 0x4EU, 0x05U, 0x52U, 0x00U, + 0x03U, 0x00U, 0x06U, 0x04U, 0x0AU, 0x00U, 0x0DU, 0x04U, 0x11U, 0x00U, + 0x14U, 0x04U, 0x18U, 0x00U, 0x1BU, 0x04U, 0x1FU, 0x00U, 0x22U, 0x04U, + 0x26U, 0x00U, 0x29U, 0x04U, 0x2DU, 0x00U, 0x30U, 0x03U, 0x33U, 0x06U, + 0x37U, 0x01U, 0x3AU, 0x04U, 0x3DU, 0x07U, 0x41U, 0x02U, 0x44U, 0x05U, + 0x48U, 0x00U, 0x4BU, 0x03U, 0x4EU, 0x06U, 0x52U, 0x01U, 0x03U, 0x01U, + 0x06U, 0x05U, 0x0AU, 0x01U, 0x0DU, 0x05U, 0x11U, 0x01U, 0x14U, 0x05U, + 0x18U, 0x01U, 0x1BU, 0x05U, 0x1FU, 0x01U, 0x22U, 0x05U, 0x26U, 0x01U, + 0x29U, 0x05U, 0x2DU, 0x01U, 0x30U, 0x04U, 0x33U, 0x07U, 0x37U, 0x02U, + 0x3AU, 0x05U, 0x3EU, 0x00U, 0x41U, 0x03U, 0x44U, 0x06U, 0x48U, 0x01U, + 0x4BU, 0x04U, 0x4EU, 0x07U, 0x52U, 0x02U, 0x03U, 0x02U, 0x06U, 0x06U, + 0x0AU, 0x02U, 0x0DU, 0x06U, 0x11U, 0x02U, 0x14U, 0x06U, 0x18U, 0x02U, + 0x1BU, 0x06U, 0x1FU, 0x02U, 0x22U, 0x06U, 0x26U, 0x02U, 0x29U, 0x06U, + 0x2DU, 0x02U, 0x30U, 0x05U, 0x34U, 0x00U, 0x37U, 0x03U, 0x3AU, 0x06U, + 0x3EU, 0x01U, 0x41U, 0x04U, 0x44U, 0x07U, 0x48U, 0x02U, 0x4BU, 0x05U, + 0x4FU, 0x00U, 0x52U, 0x03U, 0x03U, 0x03U, 0x06U, 0x07U, 0x0AU, 0x03U, + 0x0DU, 0x07U, 0x11U, 0x03U, 0x14U, 0x07U, 0x18U, 0x03U, 0x1BU, 0x07U, + 0x1FU, 0x03U, 0x22U, 0x07U, 0x26U, 0x03U, 0x29U, 0x07U, 0x2DU, 0x03U, + 0x30U, 0x06U, 0x34U, 0x01U, 0x37U, 0x04U, 0x3AU, 0x07U, 0x3EU, 0x02U, + 0x41U, 0x05U, 0x45U, 0x00U, 0x48U, 0x03U, 0x4BU, 0x06U, 0x4FU, 0x01U, + 0x52U, 0x04U, 0x03U, 0x04U, 0x07U, 0x00U, 0x0AU, 0x04U, 0x0EU, 0x00U, + 0x11U, 0x04U, 0x15U, 0x00U, 0x18U, 0x04U, 0x1CU, 0x00U, 0x1FU, 0x04U, + 0x23U, 0x00U, 0x26U, 0x04U, 0x2AU, 0x00U, 0x2DU, 0x04U, 0x30U, 0x07U, + 0x34U, 0x02U, 0x37U, 0x05U, 0x3BU, 0x00U, 0x3EU, 0x03U, 0x41U, 0x06U, + 0x45U, 0x01U, 0x48U, 0x04U, 0x4BU, 0x07U, 0x4FU, 0x02U, 0x52U, 0x05U, + 0x03U, 0x05U, 0x07U, 0x01U, 0x0AU, 0x05U, 0x0EU, 0x01U, 0x11U, 0x05U, + 0x15U, 0x01U, 0x18U, 0x05U, 0x1CU, 0x01U, 0x1FU, 0x05U, 0x23U, 0x01U, + 0x26U, 0x05U, 0x2AU, 0x01U, 0x2DU, 0x05U, 0x31U, 0x00U, 0x34U, 0x03U, + 0x37U, 0x06U, 0x3BU, 0x01U, 0x3EU, 0x04U, 0x41U, 0x07U, 0x45U, 0x02U, + 0x48U, 0x05U, 0x4CU, 0x00U, 0x4FU, 0x03U, 0x52U, 0x06U, 0x03U, 0x06U, + 0x07U, 0x02U, 0x0AU, 0x06U, 0x0EU, 0x02U, 0x11U, 0x06U, 0x15U, 0x02U, + 0x18U, 0x06U, 0x1CU, 0x02U, 0x1FU, 0x06U, 0x23U, 0x02U, 0x26U, 0x06U, + 0x2AU, 0x02U, 0x2DU, 0x06U, 0x31U, 0x01U, 0x34U, 0x04U, 0x37U, 0x07U, + 0x3BU, 0x02U, 0x3EU, 0x05U, 0x42U, 0x00U, 0x45U, 0x03U, 0x48U, 0x06U, + 0x4CU, 0x01U, 0x4FU, 0x04U, 0x52U, 0x07U, 0x03U, 0x07U, 0x07U, 0x03U, + 0x0AU, 0x07U, 0x0EU, 0x03U, 0x11U, 0x07U, 0x15U, 0x03U, 0x18U, 0x07U, + 0x1CU, 0x03U, 0x1FU, 0x07U, 0x23U, 0x03U, 0x26U, 0x07U, 0x2AU, 0x03U +}; + +const uint8_t SCRAMBLE_TABLE_TX[] = { + 0x00U, 0xF7U, 0x34U, 0x09U, 0x44U, 0x46U, 0xD7U, 0x06U, 0xB3U, 0x72U, + 0xDEU, 0x42U, 0xF5U, 0xA5U, 0xD8U, 0xF1U, 0x87U, 0x7BU, 0x9AU, 0x04U, + 0x22U, 0xA3U, 0x6BU, 0x83U, 0x59U, 0x39U, 0x6FU, 0xA1U, 0xFAU, 0x52U, + 0xECU, 0xF8U, 0xC3U, 0x3DU, 0x4DU, 0x02U, 0x91U, 0xD1U, 0xB5U, 0xC1U, + 0xACU, 0x9CU, 0xB7U, 0x50U, 0x7DU, 0x29U, 0x76U, 0xFCU, 0xE1U, 0x9EU, + 0x26U, 0x81U, 0xC8U, 0xE8U, 0xDAU, 0x60U, 0x56U, 0xCEU, 0x5BU, 0xA8U, + 0xBEU, 0x14U, 0x3BU, 0xFEU, 0x70U, 0x4FU, 0x93U, 0x40U, 0x64U, 0x74U, + 0x6DU, 0x30U, 0x2BU, 0xE7U, 0x2DU, 0x54U, 0x5FU, 0x8AU, 0x1DU, 0x7FU, + 0xB8U, 0xA7U, 0x49U, 0x20U, 0x32U, 0xBAU, 0x36U, 0x98U, 0x95U, 0xF3U, + 0x06U}; + +const uint8_t DSTAR_HEADER = 0x00U; +const uint8_t DSTAR_DATA = 0x01U; +const uint8_t DSTAR_EOT = 0x02U; + +CDStarTX::CDStarTX() : +m_buffer(), +m_modFilter(), +m_modState(), +m_poBuffer(), +m_poLen(0U), +m_poPtr(0U), +m_txDelay(60U) // 100ms +{ + ::memset(m_modState, 0x00U, 60U * sizeof(q15_t)); + + m_modFilter.numTaps = DSTAR_GMSK_FILTER_LEN; + m_modFilter.pState = m_modState; + m_modFilter.pCoeffs = DSTAR_GMSK_FILTER; +} + +void CDStarTX::process() +{ + if (m_buffer.getData() == 0U && m_poLen == 0U) + return; + + uint8_t type = m_buffer.peek(); + + if (type == DSTAR_HEADER && m_poLen == 0U) { + if (!m_tx) { + for (uint16_t i = 0U; i < m_txDelay; i++) + m_poBuffer[m_poLen++] = BIT_SYNC; + } else { + // Pop the type byte off + m_buffer.get(); + + uint8_t header[DSTAR_HEADER_LENGTH_BYTES]; + for (uint8_t i = 0U; i < DSTAR_HEADER_LENGTH_BYTES; i++) + header[i] = m_buffer.get(); + + uint8_t buffer[86U]; + txHeader(header, buffer + 2U); + + buffer[0U] = FRAME_SYNC[0U]; + buffer[1U] = FRAME_SYNC[1U]; + buffer[2U] |= FRAME_SYNC[2U]; + + for (uint8_t i = 0U; i < 85U; i++) + m_poBuffer[m_poLen++] = buffer[i]; + } + + m_poPtr = 0U; + } + + if (type == DSTAR_DATA && m_poLen == 0U) { + // Pop the type byte off + m_buffer.get(); + + for (uint8_t i = 0U; i < DSTAR_DATA_LENGTH_BYTES; i++) + m_poBuffer[m_poLen++] = m_buffer.get(); + + m_poPtr = 0U; + } + + if (type == DSTAR_EOT && m_poLen == 0U) { + // Pop the type byte off + m_buffer.get(); + + for (uint8_t j = 0U; j < 3U; j++) { + for (uint8_t i = 0U; i < DSTAR_EOT_LENGTH_BYTES; i++) + m_poBuffer[m_poLen++] = DSTAR_EOT_BYTES[i]; + } + + m_poPtr = 0U; + } + + if (m_poLen > 0U) { + uint16_t space = io.getSpace(); + + while (space > (8U * DSTAR_RADIO_BIT_LENGTH)) { + uint8_t c = m_poBuffer[m_poPtr++]; + writeByte(c); + + space -= 8U * DSTAR_RADIO_BIT_LENGTH; + + if (m_poPtr >= m_poLen) { + m_poPtr = 0U; + m_poLen = 0U; + return; + } + } + } +} + +uint8_t CDStarTX::writeHeader(const uint8_t* header, uint8_t length) +{ + if (length != DSTAR_HEADER_LENGTH_BYTES) + return 4U; + + uint16_t space = m_buffer.getSpace(); + if (space < (DSTAR_HEADER_LENGTH_BYTES + 1U)) + return 5U; + + m_buffer.put(DSTAR_HEADER); + + for (uint8_t i = 0U; i < DSTAR_HEADER_LENGTH_BYTES; i++) + m_buffer.put(header[i]); + + return 0U; +} + +uint8_t CDStarTX::writeData(const uint8_t* data, uint8_t length) +{ + if (length != DSTAR_DATA_LENGTH_BYTES) + return 4U; + + uint16_t space = m_buffer.getSpace(); + if (space < (DSTAR_DATA_LENGTH_BYTES + 1U)) + return 5U; + + m_buffer.put(DSTAR_DATA); + + for (uint8_t i = 0U; i < DSTAR_DATA_LENGTH_BYTES; i++) + m_buffer.put(data[i]); + + return 0U; +} + +uint8_t CDStarTX::writeEOT() +{ + uint16_t space = m_buffer.getSpace(); + if (space < 1U) + return 5U; + + m_buffer.put(DSTAR_EOT); + + return 0U; +} + +void CDStarTX::txHeader(const uint8_t* in, uint8_t* out) const +{ + uint8_t intermediate[84U]; + uint32_t i; + + for (i = 0U; i < 83U; i++) { + intermediate[i] = 0x00U; + out[i] = 0x00U; + } + + // Convolve the header + uint8_t d, d1 = 0U, d2 = 0U, g0, g1; + uint32_t k = 0U; + for (i = 0U; i < 42U; i++) { + for (uint8_t j = 0U; j < 8U; j++) { + uint8_t mask = (0x01U << j); + d = 0U; + + if (in[i] & mask) + d = 1U; + + g0 = (d + d2) & 1; + g1 = (d + d1 + d2) & 1; + d2 = d1; + d1 = d; + + if (g1) + intermediate[k >> 3] |= BIT_MASK_TABLE[k & 7]; + k++; + + if (g0) + intermediate[k >> 3] |= BIT_MASK_TABLE[k & 7]; + k++; + } + } + + // Interleave the header + i = 0U; + while (i < 660U) { + unsigned char d = intermediate[i >> 3]; + + if (d & 0x80U) + out[INTERLEAVE_TABLE_TX[i * 2U]] |= (0x01U << INTERLEAVE_TABLE_TX[i * 2U + 1U]); + i++; + + if (d & 0x40U) + out[INTERLEAVE_TABLE_TX[i * 2U]] |= (0x01U << INTERLEAVE_TABLE_TX[i * 2U + 1U]); + i++; + + if (d & 0x20U) + out[INTERLEAVE_TABLE_TX[i * 2U]] |= (0x01U << INTERLEAVE_TABLE_TX[i * 2U + 1U]); + i++; + + if (d & 0x10U) + out[INTERLEAVE_TABLE_TX[i * 2U]] |= (0x01U << INTERLEAVE_TABLE_TX[i * 2U + 1U]); + i++; + + if (i < 660U) { + if (d & 0x08U) + out[INTERLEAVE_TABLE_TX[i * 2U]] |= (0x01U << INTERLEAVE_TABLE_TX[i * 2U + 1U]); + i++; + + if (d & 0x04U) + out[INTERLEAVE_TABLE_TX[i * 2U]] |= (0x01U << INTERLEAVE_TABLE_TX[i * 2U + 1U]); + i++; + + if (d & 0x02U) + out[INTERLEAVE_TABLE_TX[i * 2U]] |= (0x01U << INTERLEAVE_TABLE_TX[i * 2U + 1U]); + i++; + + if (d & 0x01U) + out[INTERLEAVE_TABLE_TX[i * 2U]] |= (0x01U << INTERLEAVE_TABLE_TX[i * 2U + 1U]); + i++; + } + } + + // Scramble the header + for (i = 0U; i < 83U; i++) + out[i] ^= SCRAMBLE_TABLE_TX[i]; +} + +void CDStarTX::writeByte(uint8_t c) +{ + q15_t inBuffer[DSTAR_RADIO_BIT_LENGTH * 8U]; + q15_t outBuffer[DSTAR_RADIO_BIT_LENGTH * 8U]; + + uint8_t mask = 0x01U; + + q15_t* p = inBuffer; + for (uint8_t i = 0U; i < 8U; i++, p += DSTAR_RADIO_BIT_LENGTH) { + if ((c & mask) == mask) + ::memcpy(p, DSTAR_0, DSTAR_RADIO_BIT_LENGTH * sizeof(q15_t)); + else + ::memcpy(p, DSTAR_1, DSTAR_RADIO_BIT_LENGTH * sizeof(q15_t)); + + mask <<= 1; + } + + ::arm_fir_fast_q15(&m_modFilter, inBuffer, outBuffer, DSTAR_RADIO_BIT_LENGTH * 8U); + + io.write(outBuffer, DSTAR_RADIO_BIT_LENGTH * 8U); +} + +void CDStarTX::setTXDelay(uint8_t delay) +{ + m_txDelay = 60U + uint16_t(delay) * 6U; // 100ms + tx delay +} + +uint16_t CDStarTX::getSpace() const +{ + return m_buffer.getSpace() / (DSTAR_DATA_LENGTH_BYTES + 1U); +} + diff --git a/DStarTX.h b/DStarTX.h new file mode 100644 index 0000000..22e3fd0 --- /dev/null +++ b/DStarTX.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2015 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(DSTARTX_H) +#define DSTARTX_H + +#include "Config.h" + +#include "SerialRB.h" + +class CDStarTX { +public: + CDStarTX(); + + uint8_t writeHeader(const uint8_t* header, uint8_t length); + uint8_t writeData(const uint8_t* data, uint8_t length); + uint8_t writeEOT(); + + void process(); + + void setTXDelay(uint8_t delay); + + uint16_t getSpace() const; + +private: + CSerialRB m_buffer; + arm_fir_instance_q15 m_modFilter; + q15_t m_modState[60U]; // NoTaps + BlockSize - 1, 12 + 40 - 1 plus some spare + uint8_t m_poBuffer[400U]; + uint16_t m_poLen; + uint16_t m_poPtr; + uint16_t m_txDelay; // In bytes + + void txHeader(const uint8_t* in, uint8_t* out) const; + void writeByte(uint8_t c); +}; + +#endif + diff --git a/Debug.h b/Debug.h new file mode 100644 index 0000000..bb16563 --- /dev/null +++ b/Debug.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2015 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(DEBUG_H) +#define DEBUG_H + +#include "Config.h" + +#if defined(WANT_DEBUG) + +#include "Globals.h" + +#define DEBUG1(a) serial.writeDebug((a)) +#define DEBUG2(a,b) serial.writeDebug((a),(b)) +#define DEBUG3(a,b,c) serial.writeDebug((a),(b),(c)) +#define DEBUG4(a,b,c,d) serial.writeDebug((a),(b),(c),(d)) +#define DEBUG5(a,b,c,d,e) serial.writeDebug((a),(b),(c),(d),(e)) +#define ASSERT(a) serial.writeAssert((a),#a,__FILE__,__LINE__) +#define DUMP(a,b) serial.writeDump((a),(b)) +#define SAMPLES(a,b) serial.writeSamples((a),(b)) + +#else + +#define DEBUG1(a) +#define DEBUG2(a,b) +#define DEBUG3(a,b,c) +#define DEBUG4(a,b,c,d) +#define DEBUG5(a,b,c,d,e) +#define ASSERT(a) +#define DUMP(a,b) +#define SAMPLES(a,b) + +#endif + +#endif + diff --git a/Globals.h b/Globals.h new file mode 100644 index 0000000..31f012b --- /dev/null +++ b/Globals.h @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2015 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(GLOBALS_H) +#define GLOBALS_H + +#if defined(__MBED__) +#include "mbed.h" +#else +#include +#endif + +#if defined(__SAM3X8E__) || defined(__STM32F1__) || defined(__STM32F2__) +#define ARM_MATH_CM3 +#elif defined(__MK20DX256__) || defined(__STM32F3__) || defined(__STM32F4__) +#define ARM_MATH_CM4 +#else +#error "Unknown processor type" +#endif +#include + +enum MMDVM_STATE { + STATE_IDLE = 0, + STATE_DSTAR = 1, + STATE_DMR = 2, + STATE_YSF = 3, + STATE_CALIBRATE = 99 +}; + +#include "SerialPort.h" +#include "DMRIdleRX.h" +#include "DStarRX.h" +#include "DStarTX.h" +#include "DMRRX.h" +#include "DMRTX.h" +#include "YSFRX.h" +#include "YSFTX.h" +#include "CalRX.h" +#include "CalTX.h" +#include "Debug.h" +#include "IO.h" + +const uint8_t MARK_SLOT1 = 0x08U; +const uint8_t MARK_SLOT2 = 0x04U; +const uint8_t MARK_END = 0x0CU; +const uint8_t MARK_NONE = 0x00U; + +const uint16_t RX_BLOCK_SIZE = 20U; + +extern MMDVM_STATE m_modemState; + +extern bool m_dstarEnable; +extern bool m_dmrEnable; +extern bool m_ysfEnable; + +extern bool m_tx; + +extern CSerialPort serial; +extern CIO io; + +extern CDStarRX dstarRX; +extern CDStarTX dstarTX; + +extern CDMRIdleRX dmrIdleRX; +extern CDMRRX dmrRX; +extern CDMRTX dmrTX; + +extern CYSFRX ysfRX; +extern CYSFTX ysfTX; + +extern CCalRX calRX; +extern CCalTX calTX; + +#endif + diff --git a/IO.cpp b/IO.cpp new file mode 100644 index 0000000..d6d6e21 --- /dev/null +++ b/IO.cpp @@ -0,0 +1,403 @@ +/* + * Copyright (C) 2015,2016 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. + */ + +#include "Config.h" +#include "Globals.h" +#include "IO.h" + +// Generated using rcosdesign(0.2, 8, 5, 'sqrt') in MATLAB +static q15_t C4FSK_FILTER[] = {401, 104, -340, -731, -847, -553, 112, 909, 1472, 1450, 683, -675, -2144, -3040, -2706, -770, 2667, 6995, + 11237, 14331, 15464, 14331, 11237, 6995, 2667, -770, -2706, -3040, -2144, -675, 683, 1450, 1472, 909, 112, + -553, -847, -731, -340, 104, 401, 0}; +const uint16_t C4FSK_FILTER_LEN = 42U; + +// Generated using gaussfir(0.5, 4, 5) in MATLAB +static q15_t GMSK_FILTER[] = {8, 104, 760, 3158, 7421, 9866, 7421, 3158, 760, 104, 8, 0}; +const uint16_t GMSK_FILTER_LEN = 12U; + +const uint16_t DC_OFFSET = 2048U; + +const uint16_t TX_BUFFER_SIZE = 451U; +const uint16_t RX_BUFFER_SIZE = 301U; + +#if defined(__SAM3X8E__) +// An Arduino Due +#if defined(ARDUINO_DUE_PAPA) +#define PIN_COS 7 +#define PIN_PTT 8 +#define PIN_COSLED 11 +#define ADC_CHER_Chan (1<<7) // ADC on Due pin A0 - Due AD7 - (1 << 7) +#define ADC_ISR_EOC_Chan ADC_ISR_EOC7 +#define ADC_CDR_Chan 7 +#define DACC_MR_USER_SEL_Chan DACC_MR_USER_SEL_CHANNEL0 // DAC on Due DAC0 +#define DACC_CHER_Chan DACC_CHER_CH0 +#elif defined(ARDUINO_DUE_ZUM) +#define PIN_COS 52 +#define PIN_PTT 23 +#define PIN_COSLED 22 +#define ADC_CHER_Chan (1<<13) // ADC on Due pin A11 - Due AD13 - (1 << 13) (PB20) +#define ADC_ISR_EOC_Chan ADC_ISR_EOC13 +#define ADC_CDR_Chan 13 +#define DACC_MR_USER_SEL_Chan DACC_MR_USER_SEL_CHANNEL1 // DAC on Due DAC1 +#define DACC_CHER_Chan DACC_CHER_CH1 +#elif defined(ARDUINO_DUE_NTH) +#define PIN_COS A7 +#define PIN_PTT A8 +#define PIN_COSLED A11 +#define ADC_CHER_Chan (1<<7) // ADC on Due pin A0 - Due AD7 - (1 << 7) +#define ADC_ISR_EOC_Chan ADC_ISR_EOC7 +#define ADC_CDR_Chan 7 +#define DACC_MR_USER_SEL_Chan DACC_MR_USER_SEL_CHANNEL0 // DAC on Due DAC0 +#define DACC_CHER_Chan DACC_CHER_CH0 +#error "Either ARDUINO_DUE_PAPA, ARDUINO_DUE_ZUM, or ARDUINO_DUE_NTH need to be defined" +#endif +#elif defined(__MK20DX256__) +// A Teensy 3.1/3.2 +#define PIN_COS 14 +#define PIN_PTT 15 +#define PIN_COSLED 13 +#elif defined(__MBED__) +// A generic MBED platform +#define PIN_COS PC_1 +#define PIN_PTT PA_1 +#define PIN_COSLED PB_0 +#define PIN_ADC PA_0 +#define PIN_DAC PA_2 +#else +#error "Unknown hardware type" +#endif + +extern "C" { + void ADC_Handler() + { +#if defined(__SAM3X8E__) + if (ADC->ADC_ISR & ADC_ISR_EOC_Chan) // Ensure there was an End-of-Conversion and we read the ISR reg + io.interrupt(); +#elif defined(__MK20DX256__) +#elif defined(__MBED__) + io.interrupt(); +#endif + } +} + +CIO::CIO() : +#if defined(__MBED__) +m_pinPTT(PIN_PTT), +m_pinCOSLED(PIN_COSLED), +m_pinLED(LED1), +m_pinADC(PIN_ADC), +m_pinDAC(PIN_DAC), +m_ticker(), +#endif +m_started(false), +m_rxBuffer(RX_BUFFER_SIZE), +m_txBuffer(TX_BUFFER_SIZE), +m_C4FSKFilter(), +m_GMSKFilter(), +m_C4FSKState(), +m_GMSKState(), +m_pttInvert(false), +m_rxLevel(128 * 128), +m_txLevel(128 * 128), +m_ledCount(0U), +m_ledValue(true), +m_dcd(false), +m_overflow(0U), +m_overcount(0U) +{ + ::memset(m_C4FSKState, 0x00U, 70U * sizeof(q15_t)); + ::memset(m_GMSKState, 0x00U, 40U * sizeof(q15_t)); + + m_C4FSKFilter.numTaps = C4FSK_FILTER_LEN; + m_C4FSKFilter.pState = m_C4FSKState; + m_C4FSKFilter.pCoeffs = C4FSK_FILTER; + + m_GMSKFilter.numTaps = GMSK_FILTER_LEN; + m_GMSKFilter.pState = m_GMSKState; + m_GMSKFilter.pCoeffs = GMSK_FILTER; + +#if !defined(__MBED__) + // Set up the TX, COS and LED pins + pinMode(PIN_PTT, OUTPUT); + pinMode(PIN_COSLED, OUTPUT); + pinMode(PIN_LED, OUTPUT); +#endif +} + +void CIO::start() +{ + if (m_started) + return; + +#if defined(__SAM3X8E__) + if (ADC->ADC_ISR & ADC_ISR_EOC_Chan) // Ensure there was an End-of-Conversion and we read the ISR reg + io.interrupt(); + + // Set up the ADC + NVIC_EnableIRQ(ADC_IRQn); // Enable ADC interrupt vector + ADC->ADC_IDR = 0xFFFFFFFF; // Disable interrupts + ADC->ADC_IER = ADC_CHER_Chan; // Enable End-Of-Conv interrupt + ADC->ADC_CHDR = 0xFFFF; // Disable all channels + ADC->ADC_CHER = ADC_CHER_Chan; // Enable just one channel + ADC->ADC_CGR = 0x15555555; // All gains set to x1 + ADC->ADC_COR = 0x00000000; // All offsets off + ADC->ADC_MR = (ADC->ADC_MR & 0xFFFFFFF0) | (1 << 1) | ADC_MR_TRGEN; // 1 = trig source TIO from TC0 + + // Set up the timer + pmc_enable_periph_clk(TC_INTERFACE_ID + 0*3+0) ; // Clock the TC0 channel 0 + TcChannel* t = &(TC0->TC_CHANNEL)[0]; // Pointer to TC0 registers for its channel 0 + t->TC_CCR = TC_CCR_CLKDIS; // Disable internal clocking while setup regs + t->TC_IDR = 0xFFFFFFFF; // Disable interrupts + t->TC_SR; // Read int status reg to clear pending + t->TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK1 | // Use TCLK1 (prescale by 2, = 42MHz) + TC_CMR_WAVE | // Waveform mode + TC_CMR_WAVSEL_UP_RC | // Count-up PWM using RC as threshold + TC_CMR_EEVT_XC0 | // Set external events from XC0 (this setup TIOB as output) + TC_CMR_ACPA_CLEAR | TC_CMR_ACPC_CLEAR | + TC_CMR_BCPB_CLEAR | TC_CMR_BCPC_CLEAR; + t->TC_RC = 1750; // Counter resets on RC, so sets period in terms of 42MHz clock (was 875) + t->TC_RA = 880; // Roughly square wave (was 440) + t->TC_CMR = (t->TC_CMR & 0xFFF0FFFF) | TC_CMR_ACPA_CLEAR | TC_CMR_ACPC_SET; // Set clear and set from RA and RC compares + t->TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG ; // re-enable local clocking and switch to hardware trigger source. + + // Set up the DAC + pmc_enable_periph_clk(DACC_INTERFACE_ID); // Start clocking DAC + DACC->DACC_CR = DACC_CR_SWRST; // Reset DAC + DACC->DACC_MR = + DACC_MR_TRGEN_EN | DACC_MR_TRGSEL(1) | // Trigger 1 = TIO output of TC0 + DACC_MR_USER_SEL_Chan | // Select channel + (24 << DACC_MR_STARTUP_Pos); // 24 = 1536 cycles which I think is in range 23..45us since DAC clock = 42MHz + DACC->DACC_IDR = 0xFFFFFFFF; // No interrupts + DACC->DACC_CHER = DACC_CHER_Chan; // Enable channel + + digitalWrite(PIN_PTT, m_pttInvert ? HIGH : LOW); + digitalWrite(PIN_COSLED, LOW); + digitalWrite(PIN_LED, HIGH); +#elif defined(__MK20DX256__) + digitalWrite(PIN_PTT, m_pttInvert ? HIGH : LOW); + digitalWrite(PIN_COSLED, LOW); + digitalWrite(PIN_LED, HIGH); +#elif defined(__MBED__) + m_ticker.attach(&ADC_Handler, 1.0 / 24000.0); + + m_pinPTT.write(m_pttInvert ? 1 : 0); + m_pinCOSLED.write(0); + m_pinLED.write(1); +#endif + + m_started = true; +} + +void CIO::process() +{ + m_ledCount++; + if (m_started) { + if (m_ledCount >= 24000U) { + m_ledCount = 0U; + m_ledValue = !m_ledValue; +#if defined(__MBED__) + m_pinLED.write(m_ledValue ? 1 : 0); +#else + digitalWrite(PIN_LED, m_ledValue ? HIGH : LOW); +#endif + } + } else { + if (m_ledCount >= 240000U) { + m_ledCount = 0U; + m_ledValue = !m_ledValue; +#if defined(__MBED__) + m_pinLED.write(m_ledValue ? 1 : 0); +#else + digitalWrite(PIN_LED, m_ledValue ? HIGH : LOW); +#endif + } + return; + } + + // Switch off the transmitter if needed + if (m_txBuffer.getData() == 0U && m_tx) { + m_tx = false; +#if defined(__MBED__) + m_pinPTT.write(m_pttInvert ? 1 : 0); +#else + digitalWrite(PIN_PTT, m_pttInvert ? HIGH : LOW); +#endif + } + + if (m_rxBuffer.getData() >= RX_BLOCK_SIZE) { + q15_t samples[RX_BLOCK_SIZE]; + uint8_t control[RX_BLOCK_SIZE]; + + for (uint16_t i = 0U; i < RX_BLOCK_SIZE; i++) { + uint16_t sample; + m_rxBuffer.get(sample, control[i]); + + // Detect ADC overflow + if (m_dcd && (sample == 0U || sample == 4095U)) + m_overflow++; + m_overcount++; + + q15_t res1 = q15_t(sample) - DC_OFFSET; + q31_t res2 = res1 * m_rxLevel; + samples[i] = q15_t(__SSAT((res2 >> 15), 16)); + } + + if (m_modemState == STATE_IDLE) { + if (m_dstarEnable) { + q15_t GMSKVals[RX_BLOCK_SIZE]; + ::arm_fir_fast_q15(&m_GMSKFilter, samples, GMSKVals, RX_BLOCK_SIZE); + + dstarRX.samples(GMSKVals, RX_BLOCK_SIZE); + } + + if (m_dmrEnable || m_ysfEnable) { + q15_t C4FSKVals[RX_BLOCK_SIZE]; + ::arm_fir_fast_q15(&m_C4FSKFilter, samples, C4FSKVals, RX_BLOCK_SIZE); + + if (m_dmrEnable) + dmrIdleRX.samples(C4FSKVals, RX_BLOCK_SIZE); + + if (m_ysfEnable) + ysfRX.samples(C4FSKVals, RX_BLOCK_SIZE); + } + } else if (m_modemState == STATE_DSTAR) { + if (m_dstarEnable) { + q15_t GMSKVals[RX_BLOCK_SIZE]; + ::arm_fir_fast_q15(&m_GMSKFilter, samples, GMSKVals, RX_BLOCK_SIZE); + + dstarRX.samples(GMSKVals, RX_BLOCK_SIZE); + } + } else if (m_modemState == STATE_DMR) { + if (m_dmrEnable) { + q15_t C4FSKVals[RX_BLOCK_SIZE]; + ::arm_fir_fast_q15(&m_C4FSKFilter, samples, C4FSKVals, RX_BLOCK_SIZE); + + // If the transmitter isn't on, use the DMR idle RX to detect the wakeup CSBKs + if (m_tx) + dmrRX.samples(C4FSKVals, control, RX_BLOCK_SIZE); + else + dmrIdleRX.samples(C4FSKVals, RX_BLOCK_SIZE); + } + } else if (m_modemState == STATE_YSF) { + if (m_ysfEnable) { + q15_t C4FSKVals[RX_BLOCK_SIZE]; + ::arm_fir_fast_q15(&m_C4FSKFilter, samples, C4FSKVals, RX_BLOCK_SIZE); + + ysfRX.samples(C4FSKVals, RX_BLOCK_SIZE); + } + } else if (m_modemState == STATE_CALIBRATE) { + q15_t GMSKVals[RX_BLOCK_SIZE]; + ::arm_fir_fast_q15(&m_GMSKFilter, samples, GMSKVals, RX_BLOCK_SIZE); + + calRX.samples(GMSKVals, RX_BLOCK_SIZE); + } + } +} + +void CIO::write(q15_t* samples, uint16_t length, const uint8_t* control) +{ + if (!m_started) + return; + + // Switch the transmitter on if needed + if (!m_tx) { + m_tx = true; +#if defined(__MBED__) + m_pinPTT.write(m_pttInvert ? 0 : 1); +#else + digitalWrite(PIN_PTT, m_pttInvert ? LOW : HIGH); +#endif + } + + for (uint16_t i = 0U; i < length; i++) { + q31_t res1 = samples[i] * m_txLevel; + q15_t res2 = q15_t(__SSAT((res1 >> 15), 16)); + + if (control == NULL) + m_txBuffer.put(uint16_t(res2 + DC_OFFSET), MARK_NONE); + else + m_txBuffer.put(uint16_t(res2 + DC_OFFSET), control[i]); + } +} + +uint16_t CIO::getSpace() const +{ + return m_txBuffer.getSpace(); +} + +void CIO::interrupt() +{ + uint8_t control = MARK_NONE; + uint16_t sample = DC_OFFSET; + + if (m_txBuffer.getData() > 0U) + m_txBuffer.get(sample, control); + +#if defined(__SAM3X8E__) + DACC->DACC_CDR = sample; + sample = ADC->ADC_CDR[ADC_CDR_Chan]; +#elif defined(__MK20DX256__) +#elif defined(__MBED__) + m_pinDAC.write_u16(sample); + sample = m_pinADC.read_u16(); +#endif + + m_rxBuffer.put(sample, control); +} + +void CIO::setDecode(bool dcd) +{ + if (dcd != m_dcd) +#if defined(__MBED__) + m_pinCOSLED.write(dcd ? 1 : 0); +#elif defined(PIN_COSLED) + digitalWrite(PIN_COSLED, dcd ? HIGH : LOW); +#endif + + m_dcd = dcd; +} + +void CIO::setParameters(bool rxInvert, bool txInvert, bool pttInvert, uint8_t rxLevel, uint8_t txLevel) +{ + m_pttInvert = pttInvert; + + m_rxLevel = q15_t(rxLevel * 128); + m_txLevel = q15_t(txLevel * 128); + + if (rxInvert) + m_rxLevel = -m_rxLevel; + + if (txInvert) + m_txLevel = -m_txLevel; +} + +bool CIO::hasADCOverflow() +{ + bool overflow = m_overflow > 0U; + +#if defined(WANT_DEBUG) + if (m_overflow > 0U) + DEBUG3("IO: Overflow, n/count", m_overflow, m_overcount); +#endif + + m_overflow = 0U; + m_overcount = 0U; + + return overflow; +} + diff --git a/IO.h b/IO.h new file mode 100644 index 0000000..f4cbff9 --- /dev/null +++ b/IO.h @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2015,2016 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(IO_H) +#define IO_H + +#include "Globals.h" + +#include "SampleRB.h" + +class CIO { +public: + CIO(); + + void start(); + + void process(); + + void write(q15_t* samples, uint16_t length, const uint8_t* control = NULL); + + uint16_t getSpace() const; + + void setDecode(bool dcd); + + void interrupt(); + + void setParameters(bool rxInvert, bool txInvert, bool pttInvert, uint8_t rxLevel, uint8_t txLevel); + + bool hasADCOverflow(); + +private: +#if defined(__MBED__) + DigitalOut m_pinPTT; + DigitalOut m_pinCOSLED; + DigitalOut m_pinLED; + + AnalogIn m_pinADC; + AnalogOut m_pinDAC; + + Ticker m_ticker; +#endif + + bool m_started; + + CSampleRB m_rxBuffer; + CSampleRB m_txBuffer; + + arm_fir_instance_q15 m_C4FSKFilter; + arm_fir_instance_q15 m_GMSKFilter; + q15_t m_C4FSKState[70U]; // NoTaps + BlockSize - 1, 42 + 20 - 1 plus some spare + q15_t m_GMSKState[40U]; // NoTaps + BlockSize - 1, 12 + 20 - 1 plus some spare + + bool m_pttInvert; + q15_t m_rxLevel; + q15_t m_txLevel; + + uint32_t m_ledCount; + bool m_ledValue; + + bool m_dcd; + + uint16_t m_overflow; + uint16_t m_overcount; +}; + +#endif + diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..3912109 --- /dev/null +++ b/LICENCE @@ -0,0 +1,340 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/MMDVM.ino b/MMDVM.ino new file mode 100644 index 0000000..773a660 --- /dev/null +++ b/MMDVM.ino @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2015 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. + */ + +#include "Config.h" +#include "Globals.h" + +// Global variables +MMDVM_STATE m_modemState = STATE_IDLE; + +bool m_dstarEnable = true; +bool m_dmrEnable = true; +bool m_ysfEnable = true; + +bool m_tx = false; + +CDStarRX dstarRX; +CDStarTX dstarTX; + +CDMRIdleRX dmrIdleRX; +CDMRRX dmrRX; +CDMRTX dmrTX; + +CYSFRX ysfRX; +CYSFTX ysfTX; + +CCalRX calRX; +CCalTX calTX; + +CSerialPort serial; +CIO io; + +void setup() +{ + serial.start(); +} + +void loop() +{ + serial.process(); + + io.process(); + + // The following is for transmitting + if (m_dstarEnable && m_modemState == STATE_DSTAR) + dstarTX.process(); + + if (m_dmrEnable && m_modemState == STATE_DMR) + dmrTX.process(); + + if (m_ysfEnable && m_modemState == STATE_YSF) + ysfTX.process(); + + if (m_modemState == STATE_CALIBRATE) + calTX.process(); +} + diff --git a/README.md b/README.md new file mode 100644 index 0000000..c55d822 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +This is the source code of the MMDVM firmware that supports D-Star, DMR< and +System Fusion. + +Vurrently it only runs on the Arduino Due, but other platforms are planned. diff --git a/SampleRB.cpp b/SampleRB.cpp new file mode 100644 index 0000000..ff1a7ae --- /dev/null +++ b/SampleRB.cpp @@ -0,0 +1,83 @@ +/* +TX fifo control - Copyright (C) KI6ZUM 2015 +Copyright (C) 2015,2016 by Jonathan Naylor G4KLX + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public +License as published by the Free Software Foundation; either +version 2 of the License, or (at your option) any later version. + +This library 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 +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with this library; if not, write to the +Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +Boston, MA 02110-1301, USA. +*/ + +#include "SampleRB.h" +#include "Debug.h" + +CSampleRB::CSampleRB(uint16_t length) : +m_length(length), +m_samples(NULL), +m_control(NULL), +m_head(0U), +m_tail(0U), +m_full(false) +{ + m_samples = new uint16_t[length]; + m_control = new uint8_t[length]; +} + +uint16_t CSampleRB::getSpace() const +{ + if (m_tail == m_head) + return m_full ? 0U : m_length; + else if (m_tail < m_head) + return m_length - m_head + m_tail; + else + return m_tail - m_head; +} + +uint16_t CSampleRB::getData() const +{ + if (m_tail == m_head) + return m_full ? m_length : 0U; + else if (m_tail < m_head) + return m_head - m_tail; + else + return m_length - m_tail + m_head; +} + +void CSampleRB::put(uint16_t sample, uint8_t control) +{ + if (m_full) + return; + + m_samples[m_head] = sample; + m_control[m_head] = control; + + m_head++; + if (m_head >= m_length) + m_head = 0U; + + if (m_head == m_tail) + m_full = true; +} + +void CSampleRB::get(uint16_t& sample, uint8_t& control) +{ + sample = m_samples[m_tail]; + control = m_control[m_tail]; + + m_full = false; + + m_tail++; + if (m_tail >= m_length) + m_tail = 0U; +} + diff --git a/SampleRB.h b/SampleRB.h new file mode 100644 index 0000000..0ac2fbe --- /dev/null +++ b/SampleRB.h @@ -0,0 +1,52 @@ +/* +Serial fifo control - Copyright (C) KI6ZUM 2015 +Copyright (C) 2015,2016 by Jonathan Naylor G4KLX + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public +License as published by the Free Software Foundation; either +version 2 of the License, or (at your option) any later version. + +This library 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 +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with this library; if not, write to the +Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +Boston, MA 02110-1301, USA. +*/ + +#if !defined(SAMPLERB_H) +#define SAMPLERB_H + +#if defined(__MBED__) +#include "mbed.h" +#else +#include +#endif + +class CSampleRB { +public: + CSampleRB(uint16_t length); + + uint16_t getSpace() const; + + uint16_t getData() const; + + void put(uint16_t sample, uint8_t control); + + void get(uint16_t& sample, uint8_t& control); + +private: + uint16_t m_length; + volatile uint16_t* m_samples; + volatile uint8_t* m_control; + volatile uint16_t m_head; + volatile uint16_t m_tail; + volatile bool m_full; +}; + +#endif + diff --git a/SerialPort.cpp b/SerialPort.cpp new file mode 100644 index 0000000..9f32de8 --- /dev/null +++ b/SerialPort.cpp @@ -0,0 +1,858 @@ +/* + * Copyright (C) 2013,2015,2016 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. + */ + +#include "Config.h" +#include "Globals.h" + +#include "SerialPort.h" + +const uint8_t MMDVM_FRAME_START = 0xE0U; + +const uint8_t MMDVM_GET_VERSION = 0x00U; +const uint8_t MMDVM_GET_STATUS = 0x01U; +const uint8_t MMDVM_SET_CONFIG = 0x02U; +const uint8_t MMDVM_SET_MODE = 0x03U; + +const uint8_t MMDVM_CAL_DATA = 0x08U; + +const uint8_t MMDVM_DSTAR_HEADER = 0x10U; +const uint8_t MMDVM_DSTAR_DATA = 0x11U; +const uint8_t MMDVM_DSTAR_LOST = 0x12U; +const uint8_t MMDVM_DSTAR_EOT = 0x13U; + +const uint8_t MMDVM_DMR_DATA1 = 0x18U; +const uint8_t MMDVM_DMR_LOST1 = 0x19U; +const uint8_t MMDVM_DMR_DATA2 = 0x1AU; +const uint8_t MMDVM_DMR_LOST2 = 0x1BU; +const uint8_t MMDVM_DMR_SHORTLC = 0x1CU; +const uint8_t MMDVM_DMR_START = 0x1DU; + +const uint8_t MMDVM_YSF_DATA = 0x20U; +const uint8_t MMDVM_YSF_LOST = 0x21U; + +const uint8_t MMDVM_ACK = 0x70U; +const uint8_t MMDVM_NAK = 0x7FU; + +const uint8_t MMDVM_DUMP = 0xF0U; +const uint8_t MMDVM_DEBUG1 = 0xF1U; +const uint8_t MMDVM_DEBUG2 = 0xF2U; +const uint8_t MMDVM_DEBUG3 = 0xF3U; +const uint8_t MMDVM_DEBUG4 = 0xF4U; +const uint8_t MMDVM_DEBUG5 = 0xF5U; +const uint8_t MMDVM_SAMPLES = 0xF8U; + +const uint8_t HARDWARE[] = "MMDVM 20160113 (D-Star/DMR/System Fusion)"; + +const uint8_t PROTOCOL_VERSION = 1U; + + +CSerialPort::CSerialPort() : +#if defined(__MBED__) +m_serial(SERIAL_TX, SERIAL_RX), +#endif +m_buffer(), +m_ptr(0U), +m_len(0U) +{ +} + +void CSerialPort::sendACK() const +{ + uint8_t reply[4U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 4U; + reply[2U] = MMDVM_ACK; + reply[3U] = m_buffer[2U]; + + write(reply, 4); +} + +void CSerialPort::sendNAK(uint8_t err) const +{ + uint8_t reply[5U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 5U; + reply[2U] = MMDVM_NAK; + reply[3U] = m_buffer[2U]; + reply[4U] = err; + + write(reply, 5); +} + +void CSerialPort::getStatus() const +{ + uint8_t reply[11U]; + + // Send all sorts of interesting internal values + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 10U; + reply[2U] = MMDVM_GET_STATUS; + + reply[3U] = 0x00U; + if (m_dstarEnable) + reply[3U] |= 0x01U; + if (m_dmrEnable) + reply[3U] |= 0x02U; + if (m_ysfEnable) + reply[3U] |= 0x04U; + + reply[4U] = uint8_t(m_modemState); + + reply[5U] = m_tx ? 0x01U : 0x00U; + + if (io.hasADCOverflow()) + reply[5U] |= 0x02U; + + if (m_dstarEnable) + reply[6U] = dstarTX.getSpace(); + else + reply[6U] = 0U; + + if (m_dmrEnable) { + reply[7U] = dmrTX.getSpace1(); + reply[8U] = dmrTX.getSpace2(); + } else { + reply[7U] = 0U; + reply[8U] = 0U; + } + + if (m_ysfEnable) + reply[9U] = ysfTX.getSpace(); + else + reply[9U] = 0U; + + write(reply, 10); +} + +void CSerialPort::getVersion() const +{ + uint8_t reply[100U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = MMDVM_GET_VERSION; + + reply[3U] = PROTOCOL_VERSION; + + uint8_t count = 4U; + for (uint8_t i = 0U; HARDWARE[i] != 0x00U; i++, count++) + reply[count] = HARDWARE[i]; + + reply[1U] = count; + + write(reply, count); +} + +uint8_t CSerialPort::setConfig(const uint8_t* data, uint8_t length) +{ + if (length < 7U) + return 4U; + + bool rxInvert = (data[0U] & 0x01U) == 0x01U; + bool txInvert = (data[0U] & 0x02U) == 0x02U; + bool pttInvert = (data[0U] & 0x04U) == 0x04U; + + bool dstarEnable = (data[1U] & 0x01U) == 0x01U; + bool dmrEnable = (data[1U] & 0x02U) == 0x02U; + bool ysfEnable = (data[1U] & 0x04U) == 0x04U; + + uint8_t txDelay = data[2U]; + if (txDelay > 50U) + return 4U; + + MMDVM_STATE modemState = MMDVM_STATE(data[3U]); + + if (modemState != STATE_IDLE && modemState != STATE_DSTAR && modemState != STATE_DMR && modemState != STATE_YSF && modemState != STATE_CALIBRATE) + return 4U; + if (modemState == STATE_DSTAR && !dstarEnable) + return 4U; + if (modemState == STATE_DMR && !dmrEnable) + return 4U; + if (modemState == STATE_YSF && !ysfEnable) + return 4U; + + uint8_t rxLevel = data[4U]; + uint8_t txLevel = data[5U]; + + uint8_t colorCode = data[6U]; + if (colorCode > 15U) + return 4U; + + m_modemState = modemState; + + m_dstarEnable = dstarEnable; + m_dmrEnable = dmrEnable; + m_ysfEnable = ysfEnable; + + dstarTX.setTXDelay(txDelay); + ysfTX.setTXDelay(txDelay); + + dmrTX.setColorCode(colorCode); + dmrRX.setColorCode(colorCode); + dmrIdleRX.setColorCode(colorCode); + + io.setParameters(rxInvert, txInvert, pttInvert, rxLevel, txLevel); + + io.start(); + + return 0U; +} + +uint8_t CSerialPort::setMode(const uint8_t* data, uint8_t length) +{ + if (length < 1U) + return 4U; + + MMDVM_STATE modemState = MMDVM_STATE(data[0U]); + + if (modemState == m_modemState) + return 0U; + + if (modemState != STATE_IDLE && modemState != STATE_DSTAR && modemState != STATE_DMR && modemState != STATE_YSF && modemState != STATE_CALIBRATE) + return 4U; + if (modemState == STATE_DSTAR && !m_dstarEnable) + return 4U; + if (modemState == STATE_DMR && !m_dmrEnable) + return 4U; + if (modemState == STATE_YSF && !m_ysfEnable) + return 4U; + + setMode(modemState); + + return 0U; +} + +void CSerialPort::setMode(MMDVM_STATE modemState) +{ + switch (modemState) { + case STATE_DMR: + DEBUG1("Mode set to DMR"); + dstarRX.reset(); + ysfRX.reset(); + break; + case STATE_DSTAR: + DEBUG1("Mode set to D-Star"); + dmrIdleRX.reset(); + dmrRX.reset(); + ysfRX.reset(); + break; + case STATE_YSF: + DEBUG1("Mode set to System Fusion"); + dmrIdleRX.reset(); + dmrRX.reset(); + dstarRX.reset(); + break; + case STATE_CALIBRATE: + DEBUG1("Mode set to Calibrate"); + dmrIdleRX.reset(); + dmrRX.reset(); + dstarRX.reset(); + ysfRX.reset(); + break; + default: + DEBUG1("Mode set to Idle"); + // STATE_IDLE + break; + } + + m_modemState = modemState; +} + +void CSerialPort::start() +{ +#if defined(__MBED__) + m_serial.baud(115200); +#else + Serial.begin(115200); +#endif +} + +void CSerialPort::process() +{ +#if defined(__MBED__) + while (m_serial.readable()) { + uint8_t c = m_serial.getc(); +#else + while (Serial.available()) { + uint8_t c = Serial.read(); +#endif + + if (m_ptr == 0U && c == MMDVM_FRAME_START) { + // Handle the frame start correctly + m_buffer[0U] = c; + m_ptr = 1U; + m_len = 0U; + } else if (m_ptr > 0U) { + // Any other bytes are added to the buffer + m_buffer[m_ptr] = c; + m_ptr++; + + // Once we have enough bytes, calculate the expected length + if (m_ptr == 2U) + m_len = m_buffer[1U]; + + if (m_ptr == 3U && m_len > 130U) { + sendNAK(3U); + m_ptr = 0U; + m_len = 0U; + } + + // The full packet has been received, process it + if (m_ptr == m_len) { + uint8_t err = 2U; + + switch (m_buffer[2U]) { + case MMDVM_GET_STATUS: + getStatus(); + break; + + case MMDVM_GET_VERSION: + getVersion(); + break; + + case MMDVM_SET_CONFIG: + err = setConfig(m_buffer + 3U, m_len - 3U); + if (err == 0U) + sendACK(); + else + sendNAK(err); + break; + + case MMDVM_SET_MODE: + err = setMode(m_buffer + 3U, m_len - 3U); + if (err == 0U) + sendACK(); + else + sendNAK(err); + break; + + case MMDVM_CAL_DATA: + if (m_modemState == STATE_CALIBRATE) + err = calTX.write(m_buffer + 3U, m_len - 3U); + if (err == 0U) { + sendACK(); + } else { + DEBUG2("Received invalid calibration data", err); + sendNAK(err); + } + break; + + case MMDVM_DSTAR_HEADER: + if (m_dstarEnable) { + if (m_modemState == STATE_IDLE || m_modemState == STATE_DSTAR) + err = dstarTX.writeHeader(m_buffer + 3U, m_len - 3U); + } + if (err == 0U) { + if (m_modemState == STATE_IDLE) + setMode(STATE_DSTAR); + } else { + DEBUG2("Received invalid D-Star header", err); + sendNAK(err); + } + break; + + case MMDVM_DSTAR_DATA: + if (m_dstarEnable) { + if (m_modemState == STATE_IDLE || m_modemState == STATE_DSTAR) + err = dstarTX.writeData(m_buffer + 3U, m_len - 3U); + } + if (err == 0U) { + if (m_modemState == STATE_IDLE) + setMode(STATE_DSTAR); + } else { + DEBUG2("Received invalid D-Star data", err); + sendNAK(err); + } + break; + + case MMDVM_DSTAR_EOT: + if (m_dstarEnable) { + if (m_modemState == STATE_IDLE || m_modemState == STATE_DSTAR) + err = dstarTX.writeEOT(); + } + if (err == 0U) { + if (m_modemState == STATE_IDLE) + setMode(STATE_DSTAR); + } else { + DEBUG2("Received invalid D-Star EOT", err); + sendNAK(err); + } + break; + + case MMDVM_DMR_DATA1: + if (m_dmrEnable) { + if (m_modemState == STATE_IDLE || m_modemState == STATE_DMR) + err = dmrTX.writeData1(m_buffer + 3U, m_len - 3U); + } + if (err == 0U) { + if (m_modemState == STATE_IDLE) + setMode(STATE_DMR); + } else { + DEBUG2("Received invalid DMR data", err); + sendNAK(err); + } + break; + + case MMDVM_DMR_DATA2: + if (m_dmrEnable) { + if (m_modemState == STATE_IDLE || m_modemState == STATE_DMR) + err = dmrTX.writeData2(m_buffer + 3U, m_len - 3U); + } + if (err == 0U) { + if (m_modemState == STATE_IDLE) + setMode(STATE_DMR); + } else { + DEBUG2("Received invalid DMR data", err); + sendNAK(err); + } + break; + + case MMDVM_DMR_START: + if (m_dmrEnable) { + err = 4U; + if (m_len == 4U) { + if (m_buffer[3U] == 0x01U && m_modemState == STATE_IDLE) { + dmrTX.setStart(true); + setMode(STATE_DMR); + err = 0U; + } else if (m_buffer[3U] == 0x00U && m_modemState == STATE_DMR) { + dmrTX.setStart(false); + setMode(STATE_IDLE); + err = 0U; + } + } + } + if (err != 0U) { + DEBUG2("Received invalid DMR start", err); + sendNAK(err); + } + break; + + case MMDVM_DMR_SHORTLC: + if (m_dmrEnable) + err = dmrTX.writeShortLC(m_buffer + 3U, m_len - 3U); + if (err != 0U) { + DEBUG2("Received invalid DMR Short LC", err); + sendNAK(err); + } + break; + + case MMDVM_YSF_DATA: + if (m_ysfEnable) { + if (m_modemState == STATE_IDLE || m_modemState == STATE_YSF) + err = ysfTX.writeData(m_buffer + 3U, m_len - 3U); + } + if (err == 0U) { + if (m_modemState == STATE_IDLE) + setMode(STATE_YSF); + } else { + DEBUG2("Received invalid System Fusion data", err); + sendNAK(err); + } + break; + + default: + // Handle this, send a NAK back + sendNAK(1U); + break; + } + + m_ptr = 0U; + m_len = 0U; + } + } + } +} + +void CSerialPort::writeDStarHeader(const uint8_t* header, uint8_t length) +{ + if (m_modemState != STATE_DSTAR && m_modemState != STATE_IDLE) + return; + + if (!m_dstarEnable) + return; + + uint8_t reply[50U]; + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = MMDVM_DSTAR_HEADER; + + uint8_t count = 3U; + for (uint8_t i = 0U; i < length; i++, count++) + reply[count] = header[i]; + + reply[1U] = count; + + write(reply, count); +} + +void CSerialPort::writeDStarData(const uint8_t* data, uint8_t length) +{ + if (m_modemState != STATE_DSTAR && m_modemState != STATE_IDLE) + return; + + if (!m_dstarEnable) + return; + + uint8_t reply[20U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = MMDVM_DSTAR_DATA; + + uint8_t count = 3U; + for (uint8_t i = 0U; i < length; i++, count++) + reply[count] = data[i]; + + reply[1U] = count; + + write(reply, count); +} + +void CSerialPort::writeDStarLost() +{ + if (m_modemState != STATE_DSTAR && m_modemState != STATE_IDLE) + return; + + if (!m_dstarEnable) + return; + + uint8_t reply[3U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 3U; + reply[2U] = MMDVM_DSTAR_LOST; + + write(reply, 3); +} + +void CSerialPort::writeDStarEOT() +{ + if (m_modemState != STATE_DSTAR && m_modemState != STATE_IDLE) + return; + + if (!m_dstarEnable) + return; + + uint8_t reply[3U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 3U; + reply[2U] = MMDVM_DSTAR_EOT; + + write(reply, 3); +} + +void CSerialPort::writeDMRData(bool slot, const uint8_t* data, uint8_t length) +{ + if (m_modemState != STATE_DMR && m_modemState != STATE_IDLE) + return; + + if (!m_dmrEnable) + return; + + uint8_t reply[40U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = slot ? MMDVM_DMR_DATA2 : MMDVM_DMR_DATA1; + + uint8_t count = 3U; + for (uint8_t i = 0U; i < length; i++, count++) + reply[count] = data[i]; + + reply[1U] = count; + + write(reply, count); +} + +void CSerialPort::writeDMRLost(bool slot) +{ + if (m_modemState != STATE_DMR && m_modemState != STATE_IDLE) + return; + + if (!m_dmrEnable) + return; + + uint8_t reply[3U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 3U; + reply[2U] = slot ? MMDVM_DMR_LOST2 : MMDVM_DMR_LOST1; + + write(reply, 3); +} + +void CSerialPort::writeYSFData(const uint8_t* data, uint8_t length) +{ + if (m_modemState != STATE_YSF && m_modemState != STATE_IDLE) + return; + + if (!m_ysfEnable) + return; + + uint8_t reply[130U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = MMDVM_YSF_DATA; + + uint8_t count = 3U; + for (uint8_t i = 0U; i < length; i++, count++) + reply[count] = data[i]; + + reply[1U] = count; + + write(reply, count); +} + +void CSerialPort::writeYSFLost() +{ + if (m_modemState != STATE_YSF && m_modemState != STATE_IDLE) + return; + + if (!m_ysfEnable) + return; + + uint8_t reply[3U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 3U; + reply[2U] = MMDVM_YSF_LOST; + + write(reply, 3); +} + +void CSerialPort::writeCalData(const uint8_t* data, uint8_t length) +{ + if (m_modemState != STATE_CALIBRATE) + return; + + uint8_t reply[130U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = MMDVM_CAL_DATA; + + uint8_t count = 3U; + for (uint8_t i = 0U; i < length; i++, count++) + reply[count] = data[i]; + + reply[1U] = count; + + write(reply, count); +} + +void CSerialPort::write(const uint8_t* data, uint16_t length, bool flush) const +{ +#if defined(__MBED__) + for (uint16_t i = 0U; i < length; i++) + m_serial.putc(data[i]); + // No explicit flush function +#else + Serial.write(data, length); + if (flush) + Serial.flush(); +#endif +} + +#if defined(WANT_DEBUG) + +void CSerialPort::writeDump(const uint8_t* data, uint8_t length) +{ + ASSERT(length <= 252U); + + uint8_t reply[255U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = MMDVM_DUMP; + + uint8_t count = length * sizeof(uint8_t) + 3U; + ::memcpy(reply + 3U, data, length * sizeof(uint8_t)); + + reply[1U] = count; + + write(reply, count, true); +} + +void CSerialPort::writeSamples(const q15_t* data, uint8_t length) +{ + ASSERT(length <= 126U); + + uint8_t reply[255U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = MMDVM_SAMPLES; + + uint8_t count = length * sizeof(q15_t) + 3U; + ::memcpy(reply + 3U, data, length * sizeof(q15_t)); + + reply[1U] = count; + + write(reply, count, true); +} + +void CSerialPort::writeDebug(const char* text) +{ + uint8_t reply[130U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = MMDVM_DEBUG1; + + uint8_t count = 3U; + for (uint8_t i = 0U; text[i] != '\0'; i++, count++) + reply[count] = text[i]; + + reply[1U] = count; + + write(reply, count, true); +} + +void CSerialPort::writeDebug(const char* text, int16_t n1) +{ + uint8_t reply[130U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = MMDVM_DEBUG2; + + uint8_t count = 3U; + for (uint8_t i = 0U; text[i] != '\0'; i++, count++) + reply[count] = text[i]; + + reply[count++] = (n1 >> 8) & 0xFF; + reply[count++] = (n1 >> 0) & 0xFF; + + reply[1U] = count; + + write(reply, count, true); +} + +void CSerialPort::writeDebug(const char* text, int16_t n1, int16_t n2) +{ + uint8_t reply[130U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = MMDVM_DEBUG3; + + uint8_t count = 3U; + for (uint8_t i = 0U; text[i] != '\0'; i++, count++) + reply[count] = text[i]; + + reply[count++] = (n1 >> 8) & 0xFF; + reply[count++] = (n1 >> 0) & 0xFF; + + reply[count++] = (n2 >> 8) & 0xFF; + reply[count++] = (n2 >> 0) & 0xFF; + + reply[1U] = count; + + write(reply, count, true); +} + +void CSerialPort::writeDebug(const char* text, int16_t n1, int16_t n2, int16_t n3) +{ + uint8_t reply[130U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = MMDVM_DEBUG4; + + uint8_t count = 3U; + for (uint8_t i = 0U; text[i] != '\0'; i++, count++) + reply[count] = text[i]; + + reply[count++] = (n1 >> 8) & 0xFF; + reply[count++] = (n1 >> 0) & 0xFF; + + reply[count++] = (n2 >> 8) & 0xFF; + reply[count++] = (n2 >> 0) & 0xFF; + + reply[count++] = (n3 >> 8) & 0xFF; + reply[count++] = (n3 >> 0) & 0xFF; + + reply[1U] = count; + + write(reply, count, true); +} + +void CSerialPort::writeDebug(const char* text, int16_t n1, int16_t n2, int16_t n3, int16_t n4) +{ + uint8_t reply[130U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = MMDVM_DEBUG5; + + uint8_t count = 3U; + for (uint8_t i = 0U; text[i] != '\0'; i++, count++) + reply[count] = text[i]; + + reply[count++] = (n1 >> 8) & 0xFF; + reply[count++] = (n1 >> 0) & 0xFF; + + reply[count++] = (n2 >> 8) & 0xFF; + reply[count++] = (n2 >> 0) & 0xFF; + + reply[count++] = (n3 >> 8) & 0xFF; + reply[count++] = (n3 >> 0) & 0xFF; + + reply[count++] = (n4 >> 8) & 0xFF; + reply[count++] = (n4 >> 0) & 0xFF; + + reply[1U] = count; + + write(reply, count, true); +} + +void CSerialPort::writeAssert(bool cond, const char* text, const char* file, long line) +{ + if (cond) + return; + + uint8_t reply[200U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = MMDVM_DEBUG2; + + uint8_t count = 3U; + for (uint8_t i = 0U; text[i] != '\0'; i++, count++) + reply[count] = text[i]; + + reply[count++] = ' '; + + for (uint8_t i = 0U; file[i] != '\0'; i++, count++) + reply[count] = file[i]; + + reply[count++] = (line >> 8) & 0xFF; + reply[count++] = (line >> 0) & 0xFF; + + reply[1U] = count; + + write(reply, count, true); +} + +#endif + diff --git a/SerialPort.h b/SerialPort.h new file mode 100644 index 0000000..850799a --- /dev/null +++ b/SerialPort.h @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2015 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(SERIALPORT_H) +#define SERIALPORT_H + +#include "Config.h" +#include "Globals.h" + + +class CSerialPort { +public: + CSerialPort(); + + void start(); + + void process(); + + void writeDStarHeader(const uint8_t* header, uint8_t length); + void writeDStarData(const uint8_t* data, uint8_t length); + void writeDStarLost(); + void writeDStarEOT(); + + void writeDMRData(bool slot, const uint8_t* data, uint8_t length); + void writeDMRLost(bool slot); + + void writeYSFData(const uint8_t* data, uint8_t length); + void writeYSFLost(); + + void writeCalData(const uint8_t* data, uint8_t length); + +#if defined(WANT_DEBUG) + void writeDump(const uint8_t* data, uint8_t length); + void writeSamples(const q15_t* data, uint8_t length); + void writeDebug(const char* text); + void writeDebug(const char* text, int16_t n1); + void writeDebug(const char* text, int16_t n1, int16_t n2); + void writeDebug(const char* text, int16_t n1, int16_t n2, int16_t n3); + void writeDebug(const char* text, int16_t n1, int16_t n2, int16_t n3, int16_t n4); + void writeAssert(bool cond, const char* text, const char* file, long line); +#endif + +private: +#if defined(__MBED__) + Serial m_serial; +#endif + uint8_t m_buffer[130U]; + uint8_t m_ptr; + uint8_t m_len; + + void sendACK() const; + void sendNAK(uint8_t err) const; + void getStatus() const; + void getVersion() const; + uint8_t setConfig(const uint8_t* data, uint8_t length); + uint8_t setMode(const uint8_t* data, uint8_t length); + void write(const uint8_t* data, uint16_t length, bool flush = false) const; + void setMode(MMDVM_STATE modemState); +}; + +#endif + diff --git a/SerialRB.cpp b/SerialRB.cpp new file mode 100644 index 0000000..5abe18d --- /dev/null +++ b/SerialRB.cpp @@ -0,0 +1,93 @@ +/* +Serial RB control - Copyright (C) KI6ZUM 2015 +Copyright (C) 2015,2016 by Jonathan Naylor G4KLX + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public +License as published by the Free Software Foundation; either +version 2 of the License, or (at your option) any later version. + +This library 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 +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with this library; if not, write to the +Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +Boston, MA 02110-1301, USA. +*/ + +#include "SerialRB.h" +#include "Debug.h" + +CSerialRB::CSerialRB(uint16_t length) : +m_length(length), +m_buffer(NULL), +m_head(0U), +m_tail(0U), +m_full(false) +{ + m_buffer = new uint8_t[length]; +} + +void CSerialRB::reset() +{ + m_head = 0U; + m_tail = 0U; + m_full = false; +} + +uint16_t CSerialRB::getSpace() const +{ + if (m_tail == m_head) + return m_full ? 0U : m_length; + else if (m_tail < m_head) + return m_length - m_head + m_tail; + else + return m_tail - m_head; +} + +uint16_t CSerialRB::getData() const +{ + if (m_tail == m_head) + return m_full ? m_length : 0U; + else if (m_tail < m_head) + return m_head - m_tail; + else + return m_length - m_tail + m_head; +} + +void CSerialRB::put(uint8_t c) +{ + if (m_full) + return; + + m_buffer[m_head] = c; + + m_head++; + if (m_head >= m_length) + m_head = 0U; + + if (m_head == m_tail) + m_full = true; +} + +uint8_t CSerialRB::peek() const +{ + return m_buffer[m_tail]; +} + +uint8_t CSerialRB::get() +{ + uint8_t value = m_buffer[m_tail]; + + m_full = false; + + m_tail++; + if (m_tail >= m_length) + m_tail = 0U; + + return value; +} + diff --git a/SerialRB.h b/SerialRB.h new file mode 100644 index 0000000..a01b928 --- /dev/null +++ b/SerialRB.h @@ -0,0 +1,57 @@ +/* +Serial fifo control - Copyright (C) KI6ZUM 2015 +Copyright (C) 2015,2016 by Jonathan Naylor G4KLX + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public +License as published by the Free Software Foundation; either +version 2 of the License, or (at your option) any later version. + +This library 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 +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with this library; if not, write to the +Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +Boston, MA 02110-1301, USA. +*/ + +#if !defined(SERIALRB_H) +#define SERIALRB_H + +#if defined(__MBED__) +#include "mbed.h" +#else +#include +#endif + +const uint16_t SERIAL_RINGBUFFER_SIZE = 370U; + +class CSerialRB { +public: + CSerialRB(uint16_t length = SERIAL_RINGBUFFER_SIZE); + + uint16_t getSpace() const; + + uint16_t getData() const; + + void reset(); + + void put(uint8_t c); + + uint8_t peek() const; + + uint8_t get(); + +private: + uint16_t m_length; + volatile uint8_t* m_buffer; + volatile uint16_t m_head; + volatile uint16_t m_tail; + volatile bool m_full; +}; + +#endif + diff --git a/Utils.cpp b/Utils.cpp new file mode 100644 index 0000000..6a380f4 --- /dev/null +++ b/Utils.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2015 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. + */ + +#include "Utils.h" + +const uint8_t BITS_TABLE[] = { +# define B2(n) n, n+1, n+1, n+2 +# define B4(n) B2(n), B2(n+1), B2(n+1), B2(n+2) +# define B6(n) B4(n), B4(n+1), B4(n+1), B4(n+2) + B6(0), B6(1), B6(1), B6(2) +}; + +uint8_t countBits8(uint8_t bits) +{ + return BITS_TABLE[bits]; +} + +uint8_t countBits32(uint32_t bits) +{ + uint8_t* p = (uint8_t*)&bits; + uint8_t n = 0U; + n += BITS_TABLE[p[0U]]; + n += BITS_TABLE[p[1U]]; + n += BITS_TABLE[p[2U]]; + n += BITS_TABLE[p[3U]]; + return n; +} + +uint8_t countBits64(uint64_t bits) +{ + uint8_t* p = (uint8_t*)&bits; + uint8_t n = 0U; + n += BITS_TABLE[p[0U]]; + n += BITS_TABLE[p[1U]]; + n += BITS_TABLE[p[2U]]; + n += BITS_TABLE[p[3U]]; + n += BITS_TABLE[p[4U]]; + n += BITS_TABLE[p[5U]]; + n += BITS_TABLE[p[6U]]; + n += BITS_TABLE[p[7U]]; + return n; +} + + diff --git a/Utils.h b/Utils.h new file mode 100644 index 0000000..b037ca9 --- /dev/null +++ b/Utils.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2015 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(UTILS_H) +#define UTILS_H + +#if defined(__MBED__) +#include "mbed.h" +#else +#include +#endif + +uint8_t countBits8(uint8_t bits); + +uint8_t countBits32(uint32_t bits); + +uint8_t countBits64(uint64_t bits); + +#endif + diff --git a/YSFDefines.h b/YSFDefines.h new file mode 100644 index 0000000..afbd60e --- /dev/null +++ b/YSFDefines.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2009-2015 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(YSFDEFINES_H) +#define YSFDEFINES_H + +const unsigned int YSF_RADIO_SYMBOL_LENGTH = 5U; // At 24 kHz sample rate + +const unsigned int YSF_FRAME_LENGTH_BYTES = 120U; +const unsigned int YSF_FRAME_LENGTH_BITS = YSF_FRAME_LENGTH_BYTES * 8U; +const unsigned int YSF_FRAME_LENGTH_SYMBOLS = YSF_FRAME_LENGTH_BYTES * 4U; + +const unsigned int YSF_SYNC_LENGTH_BYTES = 5U; +const unsigned int YSF_SYNC_LENGTH_BITS = YSF_SYNC_LENGTH_BYTES * 8U; +const unsigned int YSF_SYNC_LENGTH_SYMBOLS = YSF_SYNC_LENGTH_BYTES * 4U; + +const unsigned int YSF_FICH_LENGTH_BITS = 200U; +const unsigned int YSF_FICH_LENGTH_SYMBOLS = 100U; + +const uint8_t YSF_SYNC_BYTES[] = {0xD4U, 0x71U, 0xC9U, 0x63U, 0x4DU}; +const uint8_t YSF_SYNC_BYTES_LENGTH = 5U; + +const uint64_t YSF_SYNC_BITS = 0x000000D471C9634DU; +const uint64_t YSF_SYNC_BITS_MASK = 0x000000FFFFFFFFFFU; + +// D 4 7 1 C 9 6 3 4 D +// 11 01 01 00 01 11 00 01 11 00 10 01 01 10 00 11 01 00 11 01 +// -3 +3 +3 +1 +3 -3 +1 +3 -3 +1 -1 +3 +3 -1 +3 -3 +3 +1 -3 +3 +const uint32_t YSF_SYNC_SYMBOLS = 0x0007B5ADU; +const uint32_t YSF_SYNC_SYMBOLS_MASK = 0x000FFFFFU; + +#endif + diff --git a/YSFRX.cpp b/YSFRX.cpp new file mode 100644 index 0000000..98561a4 --- /dev/null +++ b/YSFRX.cpp @@ -0,0 +1,696 @@ +/* + * Copyright (C) 2009-2015 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. + */ + +#include "Config.h" +#include "Globals.h" +#include "YSFRX.h" +#include "Utils.h" + +const unsigned int BUFFER_LENGTH = 200U; + +const q15_t SCALING_FACTOR = 19505; // Q15(0.60) + +const uint32_t PLLMAX = 0x10000U; +const uint32_t PLLINC = PLLMAX / YSF_RADIO_SYMBOL_LENGTH; +const uint32_t INC = PLLINC / 32U; + +const uint8_t SYNC_SYMBOL_ERRS = 2U; +const uint8_t SYNC_BIT_ERRS = 4U; + +const unsigned int MAX_SYNC_FRAMES = 4U + 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]) +#define READ_BIT1(p,i) (p[(i)>>3] & BIT_MASK_TABLE[(i)&7]) + +const uint8_t INTERLEAVE_TABLE_RX[] = { + 0U, 40U, 80U, 120U, 160U, + 2U, 42U, 82U, 122U, 162U, + 4U, 44U, 84U, 124U, 164U, + 6U, 46U, 86U, 126U, 166U, + 8U, 48U, 88U, 128U, 168U, + 10U, 50U, 90U, 130U, 170U, + 12U, 52U, 92U, 132U, 172U, + 14U, 54U, 94U, 134U, 174U, + 16U, 56U, 96U, 136U, 176U, + 18U, 58U, 98U, 138U, 178U, + 20U, 60U, 100U, 140U, 180U, + 22U, 62U, 102U, 142U, 182U, + 24U, 64U, 104U, 144U, 184U, + 26U, 66U, 106U, 146U, 186U, + 28U, 68U, 108U, 148U, 188U, + 30U, 70U, 110U, 150U, 190U, + 32U, 72U, 112U, 152U, 192U, + 34U, 74U, 114U, 154U, 194U, + 36U, 76U, 116U, 156U, 196U, + 38U, 78U, 118U, 158U, 198U}; + +const uint8_t BRANCH_TABLE1[] = {0U, 0U, 0U, 0U, 1U, 1U, 1U, 1U}; + +const uint8_t BRANCH_TABLE2[] = {0U, 1U, 1U, 0U, 0U, 1U, 1U, 0U}; + +const uint32_t DECODING_TABLE_23127[] = { + 0x000000U, 0x000001U, 0x000002U, 0x000003U, 0x000004U, 0x000005U, 0x000006U, 0x000007U, 0x000008U, 0x000009U, + 0x00000AU, 0x00000BU, 0x00000CU, 0x00000DU, 0x00000EU, 0x024020U, 0x000010U, 0x000011U, 0x000012U, 0x000013U, + 0x000014U, 0x000015U, 0x000016U, 0x412000U, 0x000018U, 0x000019U, 0x00001AU, 0x180800U, 0x00001CU, 0x200300U, + 0x048040U, 0x001480U, 0x000020U, 0x000021U, 0x000022U, 0x000023U, 0x000024U, 0x000025U, 0x000026U, 0x024008U, + 0x000028U, 0x000029U, 0x00002AU, 0x024004U, 0x00002CU, 0x024002U, 0x024001U, 0x024000U, 0x000030U, 0x000031U, + 0x000032U, 0x008180U, 0x000034U, 0x000C40U, 0x301000U, 0x0C0200U, 0x000038U, 0x043000U, 0x400600U, 0x210040U, + 0x090080U, 0x508000U, 0x002900U, 0x024010U, 0x000040U, 0x000041U, 0x000042U, 0x000043U, 0x000044U, 0x000045U, + 0x000046U, 0x280080U, 0x000048U, 0x000049U, 0x00004AU, 0x002500U, 0x00004CU, 0x111000U, 0x048010U, 0x400A00U, + 0x000050U, 0x000051U, 0x000052U, 0x021200U, 0x000054U, 0x000C20U, 0x048008U, 0x104100U, 0x000058U, 0x404080U, + 0x048004U, 0x210020U, 0x048002U, 0x0A2000U, 0x048000U, 0x048001U, 0x000060U, 0x000061U, 0x000062U, 0x540000U, + 0x000064U, 0x000C10U, 0x010300U, 0x00B000U, 0x000068U, 0x088200U, 0x001880U, 0x210010U, 0x602000U, 0x040180U, + 0x180400U, 0x024040U, 0x000070U, 0x000C04U, 0x086000U, 0x210008U, 0x000C01U, 0x000C00U, 0x420080U, 0x000C02U, + 0x120100U, 0x210002U, 0x210001U, 0x210000U, 0x005200U, 0x000C08U, 0x048020U, 0x210004U, 0x000080U, 0x000081U, + 0x000082U, 0x000083U, 0x000084U, 0x000085U, 0x000086U, 0x280040U, 0x000088U, 0x000089U, 0x00008AU, 0x050200U, + 0x00008CU, 0x00A800U, 0x500100U, 0x001410U, 0x000090U, 0x000091U, 0x000092U, 0x008120U, 0x000094U, 0x160000U, + 0x004A00U, 0x001408U, 0x000098U, 0x404040U, 0x222000U, 0x001404U, 0x090020U, 0x001402U, 0x001401U, 0x001400U, + 0x0000A0U, 0x0000A1U, 0x0000A2U, 0x008110U, 0x0000A4U, 0x401200U, 0x042400U, 0x110800U, 0x0000A8U, 0x300400U, + 0x001840U, 0x482000U, 0x090010U, 0x040140U, 0x208200U, 0x024080U, 0x0000B0U, 0x008102U, 0x008101U, 0x008100U, + 0x090008U, 0x206000U, 0x420040U, 0x008104U, 0x090004U, 0x020A00U, 0x144000U, 0x008108U, 0x090000U, 0x090001U, + 0x090002U, 0x001420U, 0x0000C0U, 0x0000C1U, 0x0000C2U, 0x280004U, 0x0000C4U, 0x280002U, 0x280001U, 0x280000U, + 0x0000C8U, 0x404010U, 0x001820U, 0x128000U, 0x020600U, 0x040120U, 0x016000U, 0x280008U, 0x0000D0U, 0x404008U, + 0x110400U, 0x042800U, 0x003100U, 0x018200U, 0x420020U, 0x280010U, 0x404001U, 0x404000U, 0x080300U, 0x404002U, + 0x300800U, 0x404004U, 0x048080U, 0x001440U, 0x0000E0U, 0x032000U, 0x001808U, 0x004600U, 0x10C000U, 0x040108U, + 0x420010U, 0x280020U, 0x001802U, 0x040104U, 0x001800U, 0x001801U, 0x040101U, 0x040100U, 0x001804U, 0x040102U, + 0x240200U, 0x181000U, 0x420004U, 0x008140U, 0x420002U, 0x000C80U, 0x420000U, 0x420001U, 0x00A400U, 0x404020U, + 0x001810U, 0x210080U, 0x090040U, 0x040110U, 0x420008U, 0x102200U, 0x000100U, 0x000101U, 0x000102U, 0x000103U, + 0x000104U, 0x000105U, 0x000106U, 0x041800U, 0x000108U, 0x000109U, 0x00010AU, 0x002440U, 0x00010CU, 0x200210U, + 0x500080U, 0x098000U, 0x000110U, 0x000111U, 0x000112U, 0x0080A0U, 0x000114U, 0x200208U, 0x0A0400U, 0x104040U, + 0x000118U, 0x200204U, 0x015000U, 0x460000U, 0x200201U, 0x200200U, 0x002820U, 0x200202U, 0x000120U, 0x000121U, + 0x000122U, 0x008090U, 0x000124U, 0x182000U, 0x010240U, 0x600400U, 0x000128U, 0x410800U, 0x2C0000U, 0x101200U, + 0x009400U, 0x0400C0U, 0x002810U, 0x024100U, 0x000130U, 0x008082U, 0x008081U, 0x008080U, 0x444000U, 0x031000U, + 0x002808U, 0x008084U, 0x120040U, 0x084400U, 0x002804U, 0x008088U, 0x002802U, 0x200220U, 0x002800U, 0x002801U, + 0x000140U, 0x000141U, 0x000142U, 0x002408U, 0x000144U, 0x428000U, 0x010220U, 0x104010U, 0x000148U, 0x002402U, + 0x002401U, 0x002400U, 0x084800U, 0x0400A0U, 0x221000U, 0x002404U, 0x000150U, 0x0D0000U, 0x600800U, 0x104004U, + 0x003080U, 0x104002U, 0x104001U, 0x104000U, 0x120020U, 0x009800U, 0x080280U, 0x002410U, 0x410400U, 0x200240U, + 0x048100U, 0x104008U, 0x000160U, 0x205000U, 0x010204U, 0x0A0800U, 0x010202U, 0x040088U, 0x010200U, 0x010201U, + 0x120010U, 0x040084U, 0x40C000U, 0x002420U, 0x040081U, 0x040080U, 0x010208U, 0x040082U, 0x120008U, 0x402200U, + 0x041400U, 0x0080C0U, 0x288000U, 0x000D00U, 0x010210U, 0x104020U, 0x120000U, 0x120001U, 0x120002U, 0x210100U, + 0x120004U, 0x040090U, 0x002840U, 0x481000U, 0x000180U, 0x000181U, 0x000182U, 0x008030U, 0x000184U, 0x014400U, + 0x500008U, 0x022200U, 0x000188U, 0x0A1000U, 0x500004U, 0x204800U, 0x500002U, 0x040060U, 0x500000U, 0x500001U, + 0x000190U, 0x008022U, 0x008021U, 0x008020U, 0x003040U, 0x480800U, 0x250000U, 0x008024U, 0x040C00U, 0x112000U, + 0x080240U, 0x008028U, 0x02C000U, 0x200280U, 0x500010U, 0x001500U, 0x0001A0U, 0x008012U, 0x008011U, 0x008010U, + 0x220800U, 0x040048U, 0x085000U, 0x008014U, 0x006200U, 0x040044U, 0x030400U, 0x008018U, 0x040041U, 0x040040U, + 0x500020U, 0x040042U, 0x008003U, 0x008002U, 0x008001U, 0x008000U, 0x100600U, 0x008006U, 0x008005U, 0x008004U, + 0x601000U, 0x00800AU, 0x008009U, 0x008008U, 0x090100U, 0x040050U, 0x002880U, 0x00800CU, 0x0001C0U, 0x100A00U, + 0x064000U, 0x411000U, 0x003010U, 0x040028U, 0x008C00U, 0x280100U, 0x218000U, 0x040024U, 0x080210U, 0x002480U, + 0x040021U, 0x040020U, 0x500040U, 0x040022U, 0x003004U, 0x220400U, 0x080208U, 0x008060U, 0x003000U, 0x003001U, + 0x003002U, 0x104080U, 0x080202U, 0x404100U, 0x080200U, 0x080201U, 0x003008U, 0x040030U, 0x080204U, 0x030800U, + 0x480400U, 0x04000CU, 0x302000U, 0x008050U, 0x040009U, 0x040008U, 0x010280U, 0x04000AU, 0x040005U, 0x040004U, + 0x001900U, 0x040006U, 0x040001U, 0x040000U, 0x040003U, 0x040002U, 0x014800U, 0x008042U, 0x008041U, 0x008040U, + 0x003020U, 0x040018U, 0x420100U, 0x008044U, 0x120080U, 0x040014U, 0x080220U, 0x008048U, 0x040011U, 0x040010U, + 0x204400U, 0x040012U, 0x000200U, 0x000201U, 0x000202U, 0x000203U, 0x000204U, 0x000205U, 0x000206U, 0x108400U, + 0x000208U, 0x000209U, 0x00020AU, 0x050080U, 0x00020CU, 0x200110U, 0x083000U, 0x400840U, 0x000210U, 0x000211U, + 0x000212U, 0x021040U, 0x000214U, 0x200108U, 0x004880U, 0x0C0020U, 0x000218U, 0x200104U, 0x400420U, 0x00E000U, + 0x200101U, 0x200100U, 0x130000U, 0x200102U, 0x000220U, 0x000221U, 0x000222U, 0x202800U, 0x000224U, 0x401080U, + 0x010140U, 0x0C0010U, 0x000228U, 0x088040U, 0x400410U, 0x101100U, 0x140800U, 0x012400U, 0x208080U, 0x024200U, + 0x000230U, 0x114000U, 0x400408U, 0x0C0004U, 0x02A000U, 0x0C0002U, 0x0C0001U, 0x0C0000U, 0x400402U, 0x020880U, + 0x400400U, 0x400401U, 0x005040U, 0x200120U, 0x400404U, 0x0C0008U, 0x000240U, 0x000241U, 0x000242U, 0x021010U, + 0x000244U, 0x046000U, 0x010120U, 0x400808U, 0x000248U, 0x088020U, 0x304000U, 0x400804U, 0x020480U, 0x400802U, + 0x400801U, 0x400800U, 0x000250U, 0x021002U, 0x021001U, 0x021000U, 0x580000U, 0x018080U, 0x202400U, 0x021004U, + 0x012800U, 0x140400U, 0x080180U, 0x021008U, 0x005020U, 0x200140U, 0x048200U, 0x400810U, 0x000260U, 0x088008U, + 0x010104U, 0x004480U, 0x010102U, 0x320000U, 0x010100U, 0x010101U, 0x088001U, 0x088000U, 0x062000U, 0x088002U, + 0x005010U, 0x088004U, 0x010108U, 0x400820U, 0x240080U, 0x402100U, 0x108800U, 0x021020U, 0x005008U, 0x000E00U, + 0x010110U, 0x0C0040U, 0x005004U, 0x088010U, 0x400440U, 0x210200U, 0x005000U, 0x005001U, 0x005002U, 0x102080U, + 0x000280U, 0x000281U, 0x000282U, 0x050008U, 0x000284U, 0x401020U, 0x004810U, 0x022100U, 0x000288U, 0x050002U, + 0x050001U, 0x050000U, 0x020440U, 0x184000U, 0x208020U, 0x050004U, 0x000290U, 0x082400U, 0x004804U, 0x700000U, + 0x004802U, 0x018040U, 0x004800U, 0x004801U, 0x109000U, 0x020820U, 0x080140U, 0x050010U, 0x442000U, 0x200180U, + 0x004808U, 0x001600U, 0x0002A0U, 0x401004U, 0x1A0000U, 0x004440U, 0x401001U, 0x401000U, 0x208008U, 0x401002U, + 0x006100U, 0x020810U, 0x208004U, 0x050020U, 0x208002U, 0x401008U, 0x208000U, 0x208001U, 0x240040U, 0x020808U, + 0x013000U, 0x008300U, 0x100500U, 0x401010U, 0x004820U, 0x0C0080U, 0x020801U, 0x020800U, 0x400480U, 0x020802U, + 0x090200U, 0x020804U, 0x208010U, 0x102040U, 0x0002C0U, 0x100900U, 0x40A000U, 0x004420U, 0x020408U, 0x018010U, + 0x141000U, 0x280200U, 0x020404U, 0x203000U, 0x080110U, 0x050040U, 0x020400U, 0x020401U, 0x020402U, 0x400880U, + 0x240020U, 0x018004U, 0x080108U, 0x021080U, 0x018001U, 0x018000U, 0x004840U, 0x018002U, 0x080102U, 0x404200U, + 0x080100U, 0x080101U, 0x020410U, 0x018008U, 0x080104U, 0x102020U, 0x240010U, 0x004402U, 0x004401U, 0x004400U, + 0x082800U, 0x401040U, 0x010180U, 0x004404U, 0x510000U, 0x088080U, 0x001A00U, 0x004408U, 0x020420U, 0x040300U, + 0x208040U, 0x102010U, 0x240000U, 0x240001U, 0x240002U, 0x004410U, 0x240004U, 0x018020U, 0x420200U, 0x102008U, + 0x240008U, 0x020840U, 0x080120U, 0x102004U, 0x005080U, 0x102002U, 0x102001U, 0x102000U, 0x000300U, 0x000301U, + 0x000302U, 0x484000U, 0x000304U, 0x200018U, 0x010060U, 0x022080U, 0x000308U, 0x200014U, 0x028800U, 0x101020U, + 0x200011U, 0x200010U, 0x044400U, 0x200012U, 0x000310U, 0x20000CU, 0x142000U, 0x010C00U, 0x200009U, 0x200008U, + 0x409000U, 0x20000AU, 0x200005U, 0x200004U, 0x0800C0U, 0x200006U, 0x200001U, 0x200000U, 0x200003U, 0x200002U, + 0x000320U, 0x060400U, 0x010044U, 0x101008U, 0x010042U, 0x00C800U, 0x010040U, 0x010041U, 0x006080U, 0x101002U, + 0x101001U, 0x101000U, 0x4A0000U, 0x200030U, 0x010048U, 0x101004U, 0x081800U, 0x402040U, 0x224000U, 0x008280U, + 0x100480U, 0x200028U, 0x010050U, 0x0C0100U, 0x058000U, 0x200024U, 0x400500U, 0x101010U, 0x200021U, 0x200020U, + 0x002A00U, 0x200022U, 0x000340U, 0x100880U, 0x010024U, 0x248000U, 0x010022U, 0x081400U, 0x010020U, 0x010021U, + 0x441000U, 0x034000U, 0x080090U, 0x002600U, 0x10A000U, 0x200050U, 0x010028U, 0x400900U, 0x00C400U, 0x402020U, + 0x080088U, 0x021100U, 0x060800U, 0x200048U, 0x010030U, 0x104200U, 0x080082U, 0x200044U, 0x080080U, 0x080081U, + 0x200041U, 0x200040U, 0x080084U, 0x200042U, 0x010006U, 0x402010U, 0x010004U, 0x010005U, 0x010002U, 0x010003U, + 0x010000U, 0x010001U, 0x200C00U, 0x088100U, 0x01000CU, 0x101040U, 0x01000AU, 0x040280U, 0x010008U, 0x010009U, + 0x402001U, 0x402000U, 0x010014U, 0x402002U, 0x010012U, 0x402004U, 0x010010U, 0x010011U, 0x120200U, 0x402008U, + 0x0800A0U, 0x044800U, 0x005100U, 0x200060U, 0x010018U, 0x028400U, 0x000380U, 0x100840U, 0x201400U, 0x022004U, + 0x0C8000U, 0x022002U, 0x022001U, 0x022000U, 0x006020U, 0x408400U, 0x080050U, 0x050100U, 0x011800U, 0x200090U, + 0x500200U, 0x022008U, 0x430000U, 0x045000U, 0x080048U, 0x008220U, 0x100420U, 0x200088U, 0x004900U, 0x022010U, + 0x080042U, 0x200084U, 0x080040U, 0x080041U, 0x200081U, 0x200080U, 0x080044U, 0x200082U, 0x006008U, 0x290000U, + 0x440800U, 0x008210U, 0x100410U, 0x401100U, 0x0100C0U, 0x022020U, 0x006000U, 0x006001U, 0x006002U, 0x101080U, + 0x006004U, 0x040240U, 0x208100U, 0x080C00U, 0x100404U, 0x008202U, 0x008201U, 0x008200U, 0x100400U, 0x100401U, + 0x100402U, 0x008204U, 0x006010U, 0x020900U, 0x080060U, 0x008208U, 0x100408U, 0x2000A0U, 0x061000U, 0x414000U, + 0x100801U, 0x100800U, 0x080018U, 0x100802U, 0x604000U, 0x100804U, 0x0100A0U, 0x022040U, 0x080012U, 0x100808U, + 0x080010U, 0x080011U, 0x020500U, 0x040220U, 0x080014U, 0x00D000U, 0x08000AU, 0x100810U, 0x080008U, 0x080009U, + 0x003200U, 0x018100U, 0x08000CU, 0x440400U, 0x080002U, 0x080003U, 0x080000U, 0x080001U, 0x080006U, 0x2000C0U, + 0x080004U, 0x080005U, 0x029000U, 0x100820U, 0x010084U, 0x004500U, 0x010082U, 0x040208U, 0x010080U, 0x010081U, + 0x006040U, 0x040204U, 0x080030U, 0x620000U, 0x040201U, 0x040200U, 0x010088U, 0x040202U, 0x240100U, 0x402080U, + 0x080028U, 0x008240U, 0x100440U, 0x0A4000U, 0x010090U, 0x201800U, 0x080022U, 0x011400U, 0x080020U, 0x080021U, + 0x408800U, 0x040210U, 0x080024U, 0x102100U, 0x000400U, 0x000401U, 0x000402U, 0x000403U, 0x000404U, 0x000405U, + 0x000406U, 0x108200U, 0x000408U, 0x000409U, 0x00040AU, 0x002140U, 0x00040CU, 0x4C0000U, 0x210800U, 0x001090U, + 0x000410U, 0x000411U, 0x000412U, 0x244000U, 0x000414U, 0x000860U, 0x0A0100U, 0x001088U, 0x000418U, 0x038000U, + 0x400220U, 0x001084U, 0x106000U, 0x001082U, 0x001081U, 0x001080U, 0x000420U, 0x000421U, 0x000422U, 0x091000U, + 0x000424U, 0x000850U, 0x042080U, 0x600100U, 0x000428U, 0x300080U, 0x400210U, 0x048800U, 0x009100U, 0x012200U, + 0x180040U, 0x024400U, 0x000430U, 0x000844U, 0x400208U, 0x122000U, 0x000841U, 0x000840U, 0x01C000U, 0x000842U, + 0x400202U, 0x084100U, 0x400200U, 0x400201U, 0x260000U, 0x000848U, 0x400204U, 0x0010A0U, 0x000440U, 0x000441U, + 0x000442U, 0x002108U, 0x000444U, 0x000830U, 0x405000U, 0x070000U, 0x000448U, 0x002102U, 0x002101U, 0x002100U, + 0x020280U, 0x20C000U, 0x180020U, 0x002104U, 0x000450U, 0x000824U, 0x110080U, 0x488000U, 0x000821U, 0x000820U, + 0x202200U, 0x000822U, 0x281000U, 0x140200U, 0x024800U, 0x002110U, 0x410100U, 0x000828U, 0x048400U, 0x0010C0U, + 0x000460U, 0x000814U, 0x228000U, 0x004280U, 0x000811U, 0x000810U, 0x180008U, 0x000812U, 0x054000U, 0x421000U, + 0x180004U, 0x002120U, 0x180002U, 0x000818U, 0x180000U, 0x180001U, 0x000805U, 0x000804U, 0x041100U, 0x000806U, + 0x000801U, 0x000800U, 0x000803U, 0x000802U, 0x00A080U, 0x00080CU, 0x400240U, 0x210400U, 0x000809U, 0x000808U, + 0x180010U, 0x00080AU, 0x000480U, 0x000481U, 0x000482U, 0x420800U, 0x000484U, 0x014100U, 0x042020U, 0x001018U, + 0x000488U, 0x300020U, 0x08C000U, 0x001014U, 0x020240U, 0x001012U, 0x001011U, 0x001010U, 0x000490U, 0x082200U, + 0x110040U, 0x00100CU, 0x608000U, 0x00100AU, 0x001009U, 0x001008U, 0x040900U, 0x001006U, 0x001005U, 0x001004U, + 0x001003U, 0x001002U, 0x001001U, 0x001000U, 0x0004A0U, 0x300008U, 0x042004U, 0x004240U, 0x042002U, 0x0A8000U, + 0x042000U, 0x042001U, 0x300001U, 0x300000U, 0x030100U, 0x300002U, 0x404800U, 0x300004U, 0x042008U, 0x001030U, + 0x025000U, 0x450000U, 0x280800U, 0x008500U, 0x100300U, 0x0008C0U, 0x042010U, 0x001028U, 0x00A040U, 0x300010U, + 0x400280U, 0x001024U, 0x090400U, 0x001022U, 0x001021U, 0x001020U, 0x0004C0U, 0x049000U, 0x110010U, 0x004220U, + 0x020208U, 0x502000U, 0x008900U, 0x280400U, 0x020204U, 0x090800U, 0x640000U, 0x002180U, 0x020200U, 0x020201U, + 0x020202U, 0x001050U, 0x110002U, 0x220100U, 0x110000U, 0x110001U, 0x0C4000U, 0x0008A0U, 0x110004U, 0x001048U, + 0x00A020U, 0x404400U, 0x110008U, 0x001044U, 0x020210U, 0x001042U, 0x001041U, 0x001040U, 0x480100U, 0x004202U, + 0x004201U, 0x004200U, 0x211000U, 0x000890U, 0x042040U, 0x004204U, 0x00A010U, 0x300040U, 0x001C00U, 0x004208U, + 0x020220U, 0x040500U, 0x180080U, 0x418000U, 0x00A008U, 0x000884U, 0x110020U, 0x004210U, 0x000881U, 0x000880U, + 0x420400U, 0x000882U, 0x00A000U, 0x00A001U, 0x00A002U, 0x0E0000U, 0x00A004U, 0x000888U, 0x204100U, 0x001060U, + 0x000500U, 0x000501U, 0x000502U, 0x002048U, 0x000504U, 0x014080U, 0x0A0010U, 0x600020U, 0x000508U, 0x002042U, + 0x002041U, 0x002040U, 0x009020U, 0x120800U, 0x044200U, 0x002044U, 0x000510U, 0x501000U, 0x0A0004U, 0x010A00U, + 0x0A0002U, 0x04A000U, 0x0A0000U, 0x0A0001U, 0x040880U, 0x084020U, 0x308000U, 0x002050U, 0x410040U, 0x200600U, + 0x0A0008U, 0x001180U, 0x000520U, 0x060200U, 0x104800U, 0x600004U, 0x009008U, 0x600002U, 0x600001U, 0x600000U, + 0x009004U, 0x084010U, 0x030080U, 0x002060U, 0x009000U, 0x009001U, 0x009002U, 0x600008U, 0x212000U, 0x084008U, + 0x041040U, 0x008480U, 0x100280U, 0x000940U, 0x0A0020U, 0x600010U, 0x084001U, 0x084000U, 0x400300U, 0x084002U, + 0x009010U, 0x084004U, 0x002C00U, 0x150000U, 0x000540U, 0x00200AU, 0x002009U, 0x002008U, 0x340000U, 0x081200U, + 0x008880U, 0x00200CU, 0x002003U, 0x002002U, 0x002001U, 0x002000U, 0x410010U, 0x002006U, 0x002005U, 0x002004U, + 0x00C200U, 0x220080U, 0x041020U, 0x002018U, 0x410008U, 0x000920U, 0x0A0040U, 0x104400U, 0x410004U, 0x002012U, + 0x002011U, 0x002010U, 0x410000U, 0x410001U, 0x410002U, 0x002014U, 0x480080U, 0x118000U, 0x041010U, 0x002028U, + 0x026000U, 0x000910U, 0x010600U, 0x600040U, 0x200A00U, 0x002022U, 0x002021U, 0x002020U, 0x009040U, 0x040480U, + 0x180100U, 0x002024U, 0x041002U, 0x000904U, 0x041000U, 0x041001U, 0x000901U, 0x000900U, 0x041004U, 0x000902U, + 0x120400U, 0x084040U, 0x041008U, 0x002030U, 0x410020U, 0x000908U, 0x204080U, 0x028200U, 0x000580U, 0x014004U, + 0x201200U, 0x1C0000U, 0x014001U, 0x014000U, 0x008840U, 0x014002U, 0x040810U, 0x408200U, 0x030020U, 0x0020C0U, + 0x282000U, 0x014008U, 0x500400U, 0x001110U, 0x040808U, 0x220040U, 0x406000U, 0x008420U, 0x100220U, 0x014010U, + 0x0A0080U, 0x001108U, 0x040800U, 0x040801U, 0x040802U, 0x001104U, 0x040804U, 0x001102U, 0x001101U, 0x001100U, + 0x480040U, 0x003800U, 0x030008U, 0x008410U, 0x100210U, 0x014020U, 0x042100U, 0x600080U, 0x030002U, 0x300100U, + 0x030000U, 0x030001U, 0x009080U, 0x040440U, 0x030004U, 0x080A00U, 0x100204U, 0x008402U, 0x008401U, 0x008400U, + 0x100200U, 0x100201U, 0x100202U, 0x008404U, 0x040820U, 0x084080U, 0x030010U, 0x008408U, 0x100208U, 0x422000U, + 0x204040U, 0x001120U, 0x480020U, 0x220010U, 0x008804U, 0x002088U, 0x008802U, 0x014040U, 0x008800U, 0x008801U, + 0x105000U, 0x002082U, 0x002081U, 0x002080U, 0x020300U, 0x040420U, 0x008808U, 0x002084U, 0x220001U, 0x220000U, + 0x110100U, 0x220002U, 0x003400U, 0x220004U, 0x008810U, 0x440200U, 0x040840U, 0x220008U, 0x080600U, 0x002090U, + 0x410080U, 0x188000U, 0x204020U, 0x001140U, 0x480000U, 0x480001U, 0x480002U, 0x004300U, 0x480004U, 0x040408U, + 0x008820U, 0x121000U, 0x480008U, 0x040404U, 0x030040U, 0x0020A0U, 0x040401U, 0x040400U, 0x204010U, 0x040402U, + 0x480010U, 0x220020U, 0x041080U, 0x008440U, 0x100240U, 0x000980U, 0x204008U, 0x092000U, 0x00A100U, 0x011200U, + 0x204004U, 0x500800U, 0x204002U, 0x040410U, 0x204000U, 0x204001U, 0x000600U, 0x000601U, 0x000602U, 0x108004U, + 0x000604U, 0x108002U, 0x108001U, 0x108000U, 0x000608U, 0x005800U, 0x400030U, 0x2A0000U, 0x0200C0U, 0x012020U, + 0x044100U, 0x108008U, 0x000610U, 0x082080U, 0x400028U, 0x010900U, 0x051000U, 0x424000U, 0x202040U, 0x108010U, + 0x400022U, 0x140040U, 0x400020U, 0x400021U, 0x088800U, 0x200500U, 0x400024U, 0x001280U, 0x000620U, 0x060100U, + 0x400018U, 0x0040C0U, 0x284000U, 0x012008U, 0x021800U, 0x108020U, 0x400012U, 0x012004U, 0x400010U, 0x400011U, + 0x012001U, 0x012000U, 0x400014U, 0x012002U, 0x40000AU, 0x209000U, 0x400008U, 0x400009U, 0x100180U, 0x000A40U, + 0x40000CU, 0x0C0400U, 0x400002U, 0x400003U, 0x400000U, 0x400001U, 0x400006U, 0x012010U, 0x400004U, 0x400005U, + 0x000640U, 0x610000U, 0x0C0800U, 0x0040A0U, 0x020088U, 0x081100U, 0x202010U, 0x108040U, 0x020084U, 0x140010U, + 0x019000U, 0x002300U, 0x020080U, 0x020081U, 0x020082U, 0x400C00U, 0x00C100U, 0x140008U, 0x202004U, 0x021400U, + 0x202002U, 0x000A20U, 0x202000U, 0x202001U, 0x140001U, 0x140000U, 0x400060U, 0x140002U, 0x020090U, 0x140004U, + 0x202008U, 0x094000U, 0x103000U, 0x004082U, 0x004081U, 0x004080U, 0x448000U, 0x000A10U, 0x010500U, 0x004084U, + 0x200900U, 0x088400U, 0x400050U, 0x004088U, 0x0200A0U, 0x012040U, 0x180200U, 0x241000U, 0x0B0000U, 0x000A04U, + 0x400048U, 0x004090U, 0x000A01U, 0x000A00U, 0x202020U, 0x000A02U, 0x400042U, 0x140020U, 0x400040U, 0x400041U, + 0x005400U, 0x000A08U, 0x400044U, 0x028100U, 0x000680U, 0x082010U, 0x201100U, 0x004060U, 0x020048U, 0x240800U, + 0x490000U, 0x108080U, 0x020044U, 0x408100U, 0x102800U, 0x050400U, 0x020040U, 0x020041U, 0x020042U, 0x001210U, + 0x082001U, 0x082000U, 0x068000U, 0x082002U, 0x100120U, 0x082004U, 0x004C00U, 0x001208U, 0x214000U, 0x082008U, + 0x4000A0U, 0x001204U, 0x020050U, 0x001202U, 0x001201U, 0x001200U, 0x018800U, 0x004042U, 0x004041U, 0x004040U, + 0x100110U, 0x401400U, 0x042200U, 0x004044U, 0x0C1000U, 0x300200U, 0x400090U, 0x004048U, 0x020060U, 0x012080U, + 0x208400U, 0x080900U, 0x100104U, 0x082020U, 0x400088U, 0x004050U, 0x100100U, 0x100101U, 0x100102U, 0x230000U, + 0x400082U, 0x020C00U, 0x400080U, 0x400081U, 0x100108U, 0x04C000U, 0x400084U, 0x001220U, 0x02000CU, 0x004022U, + 0x004021U, 0x004020U, 0x020008U, 0x020009U, 0x02000AU, 0x004024U, 0x020004U, 0x020005U, 0x020006U, 0x004028U, + 0x020000U, 0x020001U, 0x020002U, 0x020003U, 0x401800U, 0x082040U, 0x110200U, 0x004030U, 0x020018U, 0x018400U, + 0x202080U, 0x440100U, 0x020014U, 0x140080U, 0x080500U, 0x208800U, 0x020010U, 0x020011U, 0x020012U, 0x001240U, + 0x004003U, 0x004002U, 0x004001U, 0x004000U, 0x020028U, 0x004006U, 0x004005U, 0x004004U, 0x020024U, 0x00400AU, + 0x004009U, 0x004008U, 0x020020U, 0x020021U, 0x020022U, 0x00400CU, 0x240400U, 0x004012U, 0x004011U, 0x004010U, + 0x100140U, 0x000A80U, 0x089000U, 0x004014U, 0x00A200U, 0x011100U, 0x4000C0U, 0x004018U, 0x020030U, 0x680000U, + 0x050800U, 0x102400U, 0x000700U, 0x060020U, 0x201080U, 0x010810U, 0x402800U, 0x081040U, 0x044008U, 0x108100U, + 0x190000U, 0x408080U, 0x044004U, 0x002240U, 0x044002U, 0x200410U, 0x044000U, 0x044001U, 0x00C040U, 0x010802U, + 0x010801U, 0x010800U, 0x1000A0U, 0x200408U, 0x0A0200U, 0x010804U, 0x023000U, 0x200404U, 0x400120U, 0x010808U, + 0x200401U, 0x200400U, 0x044010U, 0x200402U, 0x060001U, 0x060000U, 0x08A000U, 0x060002U, 0x100090U, 0x060004U, + 0x010440U, 0x600200U, 0x200840U, 0x060008U, 0x400110U, 0x101400U, 0x009200U, 0x012100U, 0x044020U, 0x080880U, + 0x100084U, 0x060010U, 0x400108U, 0x010820U, 0x100080U, 0x100081U, 0x100082U, 0x007000U, 0x400102U, 0x084200U, + 0x400100U, 0x400101U, 0x100088U, 0x200420U, 0x400104U, 0x028040U, 0x00C010U, 0x081004U, 0x520000U, 0x002208U, + 0x081001U, 0x081000U, 0x010420U, 0x081002U, 0x200820U, 0x002202U, 0x002201U, 0x002200U, 0x020180U, 0x081008U, + 0x044040U, 0x002204U, 0x00C000U, 0x00C001U, 0x00C002U, 0x010840U, 0x00C004U, 0x081010U, 0x202100U, 0x440080U, + 0x00C008U, 0x140100U, 0x080480U, 0x002210U, 0x410200U, 0x200440U, 0x101800U, 0x028020U, 0x200808U, 0x060040U, + 0x010404U, 0x004180U, 0x010402U, 0x081020U, 0x010400U, 0x010401U, 0x200800U, 0x200801U, 0x200802U, 0x002220U, + 0x200804U, 0x504000U, 0x010408U, 0x028010U, 0x00C020U, 0x402400U, 0x041200U, 0x380000U, 0x1000C0U, 0x000B00U, + 0x010410U, 0x028008U, 0x200810U, 0x011080U, 0x400140U, 0x028004U, 0x0C2000U, 0x028002U, 0x028001U, 0x028000U, + 0x201002U, 0x408008U, 0x201000U, 0x201001U, 0x100030U, 0x014200U, 0x201004U, 0x022400U, 0x408001U, 0x408000U, + 0x201008U, 0x408002U, 0x020140U, 0x408004U, 0x044080U, 0x080820U, 0x100024U, 0x082100U, 0x201010U, 0x010880U, + 0x100020U, 0x100021U, 0x100022U, 0x440040U, 0x040A00U, 0x408010U, 0x080440U, 0x124000U, 0x100028U, 0x200480U, + 0x01A000U, 0x001300U, 0x100014U, 0x060080U, 0x201020U, 0x004140U, 0x100010U, 0x100011U, 0x100012U, 0x080808U, + 0x006400U, 0x408020U, 0x030200U, 0x080804U, 0x100018U, 0x080802U, 0x080801U, 0x080800U, 0x100004U, 0x100005U, + 0x100006U, 0x008600U, 0x100000U, 0x100001U, 0x100002U, 0x100003U, 0x10000CU, 0x011040U, 0x400180U, 0x242000U, + 0x100008U, 0x100009U, 0x10000AU, 0x080810U, 0x052000U, 0x100C00U, 0x201040U, 0x004120U, 0x020108U, 0x081080U, + 0x008A00U, 0x440010U, 0x020104U, 0x408040U, 0x080410U, 0x002280U, 0x020100U, 0x020101U, 0x020102U, 0x310000U, + 0x00C080U, 0x220200U, 0x080408U, 0x440004U, 0x100060U, 0x440002U, 0x440001U, 0x440000U, 0x080402U, 0x011020U, + 0x080400U, 0x080401U, 0x020110U, 0x006800U, 0x080404U, 0x440008U, 0x480200U, 0x004102U, 0x004101U, 0x004100U, + 0x100050U, 0x20A000U, 0x010480U, 0x004104U, 0x200880U, 0x011010U, 0x148000U, 0x004108U, 0x020120U, 0x040600U, + 0x403000U, 0x080840U, 0x100044U, 0x011008U, 0x022800U, 0x004110U, 0x100040U, 0x100041U, 0x100042U, 0x440020U, + 0x011001U, 0x011000U, 0x080420U, 0x011002U, 0x100048U, 0x011004U, 0x204200U, 0x028080U}; + +static const unsigned short CCITT_TABLE[] = { + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, + 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, + 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, + 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, + 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, + 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, + 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, + 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, + 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, + 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, + 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, + 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, + 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, + 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, + 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, + 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, + 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, + 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, + 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, + 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, + 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, + 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, + 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, + 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, + 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, + 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, + 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, + 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, + 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, + 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0}; + +CYSFRX::CYSFRX() : +m_pll(0U), +m_prev(false), +m_state(YSFRXS_NONE), +m_bitBuffer(0x00U), +m_symbols(), +m_outBuffer(), +m_buffer(NULL), +m_bufferPtr(0U), +m_symbolPtr(0U), +m_lostCount(0U), +m_centre(0), +m_threshold(0), +m_metrics1(), +m_metrics2(), +m_oldMetrics(NULL), +m_newMetrics(NULL), +m_decisions(), +m_dp(NULL) +{ + m_buffer = m_outBuffer + 1U; +} + +void CYSFRX::reset() +{ + m_pll = 0U; + m_prev = false; + m_state = YSFRXS_NONE; + m_bitBuffer = 0x00U; + m_bufferPtr = 0U; + m_symbolPtr = 0U; + m_lostCount = 0U; + m_centre = 0; + m_threshold = 0; +} + +void CYSFRX::samples(const q15_t* samples, uint8_t length) +{ + for (uint16_t i = 0U; i < length; i++) { + bool bit = samples[i] < 0; + + if (bit != m_prev) { + if (m_pll < (PLLMAX / 2U)) + m_pll += INC; + else + m_pll -= INC; + } + + m_prev = bit; + + m_pll += PLLINC; + + if (m_pll >= PLLMAX) { + m_pll -= PLLMAX; + + if (m_state == YSFRXS_NONE) + processNone(samples[i]); + else + processData(samples[i]); + } + } +} + +void CYSFRX::processNone(q15_t sample) +{ + m_symbolBuffer <<= 1; + if (sample < 0) + m_symbolBuffer |= 0x01U; + + m_symbols[m_symbolPtr] = sample; + + // Fuzzy matching of the data sync bit sequence + if (countBits32((m_symbolBuffer & YSF_SYNC_SYMBOLS_MASK) ^ YSF_SYNC_SYMBOLS) <= SYNC_SYMBOL_ERRS) { + uint16_t ptr = m_symbolPtr + 1U; + if (ptr >= YSF_SYNC_LENGTH_SYMBOLS) + ptr = 0U; + + q15_t max = -16000; + q15_t min = 16000; + + for (uint8_t i = 0U; i < YSF_SYNC_LENGTH_SYMBOLS; i++) { + if (m_symbols[ptr] > max) + max = m_symbols[ptr]; + if (m_symbols[ptr] < min) + min = m_symbols[ptr]; + + ptr++; + if (ptr >= YSF_SYNC_LENGTH_SYMBOLS) + ptr = 0U; + } + + q15_t centre = (max + min) >> 1; + + q31_t v1 = (max - centre) * SCALING_FACTOR; + q15_t threshold = q15_t(v1 >> 15); + + ptr = m_symbolPtr + 1U; + if (ptr >= YSF_SYNC_LENGTH_SYMBOLS) + ptr = 0U; + + for (uint8_t i = 0U; i < YSF_SYNC_LENGTH_SYMBOLS; i++) { + q15_t sample = m_symbols[ptr] - centre; + + if (sample < -threshold) { + m_bitBuffer <<= 2; + m_bitBuffer |= 0x01U; + } else if (sample < 0) { + m_bitBuffer <<= 2; + m_bitBuffer |= 0x00U; + } else if (sample < threshold) { + m_bitBuffer <<= 2; + m_bitBuffer |= 0x02U; + } else { + m_bitBuffer <<= 2; + m_bitBuffer |= 0x03U; + } + + ptr++; + if (ptr >= YSF_SYNC_LENGTH_SYMBOLS) + ptr = 0U; + } + + // Fuzzy matching of the data sync bit sequence + if (countBits64((m_bitBuffer & YSF_SYNC_BITS_MASK) ^ YSF_SYNC_BITS) <= SYNC_BIT_ERRS) { + DEBUG5("YSFRX: sync found in None min/max/centre/threshold", min, max, centre, threshold); + for (uint8_t i = 0U; i < YSF_SYNC_LENGTH_BYTES; i++) + m_buffer[i] = YSF_SYNC_BYTES[i]; + m_centre = centre; + m_threshold = threshold; + m_lostCount = MAX_SYNC_FRAMES; + m_bufferPtr = YSF_SYNC_LENGTH_BITS; + m_state = YSFRXS_DATA; + io.setDecode(true); + } + } + + m_symbolPtr++; + if (m_symbolPtr >= YSF_SYNC_LENGTH_SYMBOLS) + m_symbolPtr = 0U; +} + +void CYSFRX::processData(q15_t sample) +{ + sample -= m_centre; + + if (sample < -m_threshold) { + m_bitBuffer <<= 2; + m_bitBuffer |= 0x01U; + WRITE_BIT1(m_buffer, m_bufferPtr, false); + m_bufferPtr++; + WRITE_BIT1(m_buffer, m_bufferPtr, true); + m_bufferPtr++; + } else if (sample < 0) { + m_bitBuffer <<= 2; + m_bitBuffer |= 0x00U; + WRITE_BIT1(m_buffer, m_bufferPtr, false); + m_bufferPtr++; + WRITE_BIT1(m_buffer, m_bufferPtr, false); + m_bufferPtr++; + } else if (sample < m_threshold) { + m_bitBuffer <<= 2; + m_bitBuffer |= 0x02U; + WRITE_BIT1(m_buffer, m_bufferPtr, true); + m_bufferPtr++; + WRITE_BIT1(m_buffer, m_bufferPtr, false); + m_bufferPtr++; + } else { + m_bitBuffer <<= 2; + m_bitBuffer |= 0x03U; + WRITE_BIT1(m_buffer, m_bufferPtr, true); + m_bufferPtr++; + WRITE_BIT1(m_buffer, m_bufferPtr, true); + m_bufferPtr++; + } + + // Only search for a sync in the right place +-1 symbol + if (m_bufferPtr >= (YSF_SYNC_LENGTH_BITS - 2U) && m_bufferPtr <= (YSF_SYNC_LENGTH_BITS + 2U)) { + // Fuzzy matching of the data sync bit sequence + if (countBits64((m_bitBuffer & YSF_SYNC_BITS_MASK) ^ YSF_SYNC_BITS) <= SYNC_BIT_ERRS) { +#if defined(WANT_DEBUG) + if (m_bufferPtr < YSF_SYNC_LENGTH_BITS) + DEBUG2("YSFRX: found sync in Data, early", YSF_SYNC_LENGTH_BITS - m_bufferPtr); + else if (m_bufferPtr > YSF_SYNC_LENGTH_BITS) + DEBUG2("YSFRX: found sync in Data, late", m_bufferPtr - YSF_SYNC_LENGTH_BITS); + else + DEBUG1("YSFRX: found sync in Data"); +#endif + m_lostCount = MAX_SYNC_FRAMES; + m_bufferPtr = YSF_SYNC_LENGTH_BITS; + } + } + + // Send a data frame to the host if the required number of bits have been received, or if a data sync has been seen + if (m_bufferPtr == YSF_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("YSFRX: sync timed out, lost lock"); + io.setDecode(false); + + serial.writeYSFLost(); + + m_state = YSFRXS_NONE; + return; + } else { + uint8_t FICH[6U]; + bool ok = rxFICH(m_buffer + YSF_SYNC_LENGTH_BYTES, FICH); + +#if defined(WANT_DEBUG) + if (ok) + DEBUG5("YSFRX: Valid FICH", FICH[0U], FICH[1U], FICH[2U], FICH[3U]); + else + DEBUG5("YSFRX: Invalid FICH", FICH[0U], FICH[1U], FICH[2U], FICH[3U]); +#endif + + m_outBuffer[0U] = (FICH[0U] & 0xC0U) << 0; // FI + m_outBuffer[0U] |= (FICH[2U] & 0x03U) << 4; // DT + m_outBuffer[0U] |= ok ? 0x01U : 0x00U; + + serial.writeYSFData(m_outBuffer, YSF_FRAME_LENGTH_BYTES + 1U); + + if (ok && (FICH[0U] & 0xC0U) == 0x80U) { + DEBUG1("YSFRX: end of transmission"); + io.setDecode(false); + m_state = YSFRXS_NONE; + } else { + // Start the next frame + ::memset(m_outBuffer, 0x00U, YSF_FRAME_LENGTH_BYTES + 1U); + m_bufferPtr = 0U; + } + } + } +} + +const unsigned int NUM_OF_STATES_D2 = 8U; +const unsigned int NUM_OF_STATES = 16U; +const uint32_t M = 3U; +const unsigned int K = 5U; + +bool CYSFRX::rxFICH(uint8_t* data, uint8_t* FICH) +{ + ::memset(m_metrics1, 0x00U, NUM_OF_STATES * sizeof(uint32_t)); + m_oldMetrics = m_metrics1; + m_newMetrics = m_metrics2; + m_dp = m_decisions; + + uint8_t m = 0U; + // Deinterleave the FICH and send bits to the Viterbi decoder + for (uint8_t i = 0U; i < 100U; i++) { + uint8_t n = INTERLEAVE_TABLE_RX[i]; + uint8_t s0 = READ_BIT1(data, n) ? 1U : 0U; + + n++; + uint8_t s1 = READ_BIT1(data, n) ? 1U : 0U; + + viterbiDecode(s0, s1); + } + + uint8_t output[13U]; + chainback(output); + + uint32_t b0 = golay24128(output + 0U); + uint32_t b1 = golay24128(output + 3U); + uint32_t b2 = golay24128(output + 6U); + uint32_t b3 = golay24128(output + 9U); + + FICH[0U] = (b0 >> 16) & 0xFFU; + FICH[1U] = ((b0 >> 8) & 0xF0U) | ((b1 >> 20) & 0x0FU); + FICH[2U] = (b1 >> 12) & 0xFFU; + FICH[3U] = (b2 >> 16) & 0xFFU; + FICH[4U] = ((b2 >> 8) & 0xF0U) | ((b3 >> 20) & 0x0FU); + FICH[5U] = (b3 >> 12) & 0xFFU; + + return checksum(FICH); +} + +void CYSFRX::viterbiDecode(uint8_t s0, uint8_t s1) +{ + *m_dp = 0U; + + for (uint8_t i = 0U; i < NUM_OF_STATES_D2; i++) { + uint8_t j = i * 2U; + + uint16_t metric = (BRANCH_TABLE1[i] ^ s0) + (BRANCH_TABLE2[i] ^ s1); + + uint16_t m0 = m_oldMetrics[i] + metric; + uint16_t m1 = m_oldMetrics[i + NUM_OF_STATES_D2] + (M - metric); + uint8_t decision0 = (m0 >= m1) ? 1U : 0U; + m_newMetrics[j + 0U] = decision0 != 0U ? m1 : m0; + + m0 = m_oldMetrics[i] + (M - metric); + m1 = m_oldMetrics[i + NUM_OF_STATES_D2] + metric; + uint8_t decision1 = (m0 >= m1) ? 1U : 0U; + m_newMetrics[j + 1U] = decision1 != 0U ? m1 : m0; + + *m_dp |= (uint64_t(decision1) << (j + 1U)) | (uint64_t(decision0) << (j + 0U)); + } + + ++m_dp; + + uint16_t* tmp = m_oldMetrics; + m_oldMetrics = m_newMetrics; + m_newMetrics = tmp; +} + +void CYSFRX::chainback(uint8_t *out) +{ + uint32_t state = 0U; + + uint8_t nbits = 96U; + while (nbits-- > 0) { + --m_dp; + + uint32_t i = state >> (9 - K); + uint8_t bit = uint8_t(*m_dp >> i) & 1; + state = (bit << 7) | (state >> 1); + + WRITE_BIT1(out, nbits, bit != 0U); + } +} + +#define X22 0x00400000 /* vector representation of X^{22} */ +#define X11 0x00000800 /* vector representation of X^{11} */ +#define MASK12 0xfffff800 /* auxiliary vector for testing */ +#define GENPOL 0x00000c75 /* generator polinomial, g(x) */ + +uint32_t CYSFRX::getSyndrome23127(uint32_t pattern) const +/* + * Compute the syndrome corresponding to the given pattern, i.e., the + * remainder after dividing the pattern (when considering it as the vector + * representation of a polynomial) by the generator polynomial, GENPOL. + * In the program this pattern has several meanings: (1) pattern = infomation + * bits, when constructing the encoding table; (2) pattern = error pattern, + * when constructing the decoding table; and (3) pattern = received vector, to + * obtain its syndrome in decoding. + */ +{ + uint32_t aux = X22; + + if (pattern >= X11) { + while (pattern & MASK12) { + while (!(aux & pattern)) + aux = aux >> 1; + + pattern ^= (aux / X11) * GENPOL; + } + } + + return pattern; +} + +uint32_t CYSFRX::golay24128(const uint8_t* bytes) const +{ + uint32_t code = bytes[0U]; + code <<= 8; + code |= bytes[1U]; + code <<= 8; + code |= bytes[2U]; + + code >>= 1; + + uint32_t syndrome = getSyndrome23127(code); + uint32_t error_pattern = DECODING_TABLE_23127[syndrome]; + + code ^= error_pattern; + code <<= 1; + + return code; +} + +bool CYSFRX::checksum(const uint8_t* fich) const +{ + union { + uint16_t crc16; + uint8_t crc8[2U]; + }; + + crc16 = 0U; + crc16 = (uint16_t(crc8[0U]) << 8) ^ CCITT_TABLE[crc8[1U] ^ fich[0U]]; + crc16 = (uint16_t(crc8[0U]) << 8) ^ CCITT_TABLE[crc8[1U] ^ fich[1U]]; + crc16 = (uint16_t(crc8[0U]) << 8) ^ CCITT_TABLE[crc8[1U] ^ fich[2U]]; + crc16 = (uint16_t(crc8[0U]) << 8) ^ CCITT_TABLE[crc8[1U] ^ fich[3U]]; + + crc16 = ~crc16; + + return crc8[0U] == fich[5U] && crc8[1U] == fich[4U]; +} + diff --git a/YSFRX.h b/YSFRX.h new file mode 100644 index 0000000..46950c0 --- /dev/null +++ b/YSFRX.h @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2015 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(YSFRX_H) +#define YSFRX_H + +#include "Config.h" +#include "YSFDefines.h" + +enum YSFRX_STATE { + YSFRXS_NONE, + YSFRXS_DATA +}; + +class CYSFRX { +public: + CYSFRX(); + + void samples(const q15_t* samples, uint8_t length); + + void reset(); + +private: + uint32_t m_pll; + bool m_prev; + YSFRX_STATE m_state; + uint32_t m_symbolBuffer; + uint64_t m_bitBuffer; + q15_t m_symbols[YSF_SYNC_LENGTH_SYMBOLS]; + uint8_t m_outBuffer[YSF_FRAME_LENGTH_BYTES + 1U]; + uint8_t* m_buffer; + uint16_t m_bufferPtr; + uint16_t m_symbolPtr; + uint16_t m_lostCount; + q15_t m_centre; + q15_t m_threshold; + uint16_t m_metrics1[16U]; + uint16_t m_metrics2[16U]; + uint16_t* m_oldMetrics; + uint16_t* m_newMetrics; + uint64_t m_decisions[100U]; + uint64_t* m_dp; + + void processNone(q15_t sample); + void processData(q15_t sample); + bool rxFICH(uint8_t* data, uint8_t* FICH); + void viterbiDecode(uint8_t s0, uint8_t s1); + void chainback(uint8_t* out); + uint32_t getSyndrome23127(uint32_t pattern) const; + uint32_t golay24128(const uint8_t* bytes) const; + bool checksum(const uint8_t* fich) const; +}; + +#endif + diff --git a/YSFTX.cpp b/YSFTX.cpp new file mode 100644 index 0000000..d8d1123 --- /dev/null +++ b/YSFTX.cpp @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2009-2015 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. + */ + +#include "Config.h" +#include "Globals.h" +#include "YSFTX.h" + +#include "YSFDefines.h" + +// Generated using rcosdesign(0.2, 8, 5, 'sqrt') in MATLAB +static q15_t YSF_C4FSK_FILTER[] = {401, 104, -340, -731, -847, -553, 112, 909, 1472, 1450, 683, -675, -2144, -3040, -2706, -770, 2667, 6995, + 11237, 14331, 15464, 14331, 11237, 6995, 2667, -770, -2706, -3040, -2144, -675, 683, 1450, 1472, 909, 112, + -553, -847, -731, -340, 104, 401, 0}; +const uint16_t YSF_C4FSK_FILTER_LEN = 42U; + +const uint8_t YSF_START_SYNC = 0x77U; +const uint8_t YSF_END_SYNC = 0xFFU; + +q15_t YSF_A[] = { 1856, 1856, 1856, 1856, 1856}; +q15_t YSF_B[] = { 619, 619, 619, 619, 619}; +q15_t YSF_C[] = { -619, -619, -619, -619, -619}; +q15_t YSF_D[] = {-1806, -1856, -1856, -1856, -1856}; + + +CYSFTX::CYSFTX() : +m_buffer(), +m_modFilter(), +m_modState(), +m_poBuffer(), +m_poLen(0U), +m_poPtr(0U), +m_txDelay(120U) // 100ms +{ + ::memset(m_modState, 0x00U, 70U * sizeof(q15_t)); + + m_modFilter.numTaps = YSF_C4FSK_FILTER_LEN; + m_modFilter.pState = m_modState; + m_modFilter.pCoeffs = YSF_C4FSK_FILTER; +} + +void CYSFTX::process() +{ + if (m_buffer.getData() == 0U && m_poLen == 0U) + return; + + if (m_poLen == 0U) { + if (!m_tx) { + for (uint16_t i = 0U; i < m_txDelay; i++) + m_poBuffer[m_poLen++] = YSF_START_SYNC; + } else { + for (uint8_t i = 0U; i < YSF_FRAME_LENGTH_BYTES; i++) { + uint8_t c = m_buffer.get(); + m_poBuffer[m_poLen++] = c; + } + } + + m_poPtr = 0U; + } + + if (m_poLen > 0U) { + uint16_t space = io.getSpace(); + + while (space > (4U * YSF_RADIO_SYMBOL_LENGTH)) { + uint8_t c = m_poBuffer[m_poPtr++]; + writeByte(c); + + space -= 4U * YSF_RADIO_SYMBOL_LENGTH; + + if (m_poPtr >= m_poLen) { + m_poPtr = 0U; + m_poLen = 0U; + return; + } + } + } +} + +uint8_t CYSFTX::writeData(const uint8_t* data, uint8_t length) +{ + if (length != (YSF_FRAME_LENGTH_BYTES + 1U)) + return 4U; + + uint16_t space = m_buffer.getSpace(); + if (space < YSF_FRAME_LENGTH_BYTES) + return 5U; + + for (uint8_t i = 0U; i < YSF_FRAME_LENGTH_BYTES; i++) + m_buffer.put(data[i + 1U]); + + return 0U; +} + +void CYSFTX::writeByte(uint8_t c) +{ + q15_t inBuffer[YSF_RADIO_SYMBOL_LENGTH * 4U]; + q15_t outBuffer[YSF_RADIO_SYMBOL_LENGTH * 4U]; + + const uint8_t MASK = 0xC0U; + + q15_t* p = inBuffer; + for (uint8_t i = 0U; i < 4U; i++, c <<= 2, p += YSF_RADIO_SYMBOL_LENGTH) { + switch (c & MASK) { + case 0xC0U: + ::memcpy(p, YSF_A, YSF_RADIO_SYMBOL_LENGTH * sizeof(q15_t)); + break; + case 0x80U: + ::memcpy(p, YSF_B, YSF_RADIO_SYMBOL_LENGTH * sizeof(q15_t)); + break; + case 0x00U: + ::memcpy(p, YSF_C, YSF_RADIO_SYMBOL_LENGTH * sizeof(q15_t)); + break; + default: + ::memcpy(p, YSF_D, YSF_RADIO_SYMBOL_LENGTH * sizeof(q15_t)); + break; + } + } + + ::arm_fir_fast_q15(&m_modFilter, inBuffer, outBuffer, YSF_RADIO_SYMBOL_LENGTH * 4U); + + io.write(outBuffer, YSF_RADIO_SYMBOL_LENGTH * 4U); +} + +void CYSFTX::setTXDelay(uint8_t delay) +{ + m_txDelay = 120U + uint16_t(delay) * 12U; // 100ms + tx delay +} + +uint16_t CYSFTX::getSpace() const +{ + return m_buffer.getSpace() / YSF_FRAME_LENGTH_BYTES; +} + diff --git a/YSFTX.h b/YSFTX.h new file mode 100644 index 0000000..bcaba20 --- /dev/null +++ b/YSFTX.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2015 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(YSFTX_H) +#define YSFTX_H + +#include "Config.h" + +#include "SerialRB.h" + +class CYSFTX { +public: + CYSFTX(); + + 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; + arm_fir_instance_q15 m_modFilter; + q15_t m_modState[70U]; // NoTaps + BlockSize - 1, 42 + 20 - 1 plus some spare + uint8_t m_poBuffer[720U]; + uint16_t m_poLen; + uint16_t m_poPtr; + uint16_t m_txDelay; + + void writeByte(uint8_t c); +}; + +#endif +