0
mirror of https://github.com/OneOfEleven/uv-k5-firmware-custom.git synced 2025-06-19 14:48:03 +03:00

MDC1200 update

This commit is contained in:
OneOfEleven
2023-10-25 19:26:22 +01:00
parent 1c424492d9
commit 30dfb07004
16 changed files with 364 additions and 95 deletions

289
mdc1200.c
View File

@ -7,6 +7,8 @@
#include "mdc1200.h"
#include "misc.h"
#define FEC_K 7
// MDC1200 sync bit reversals and packet sync
//
// >= 24-bit pre-amble
@ -14,7 +16,7 @@
//
static const uint8_t pre_amble[] = {0x00, 0x00, 0x00, 0x00, 0x00};
static const uint8_t sync[] = {0xCC, 0x07, 0x09, 0x2a, 0x44, 0x6f};
/*
uint8_t bit_reverse_8(uint8_t n)
{
n = ((n >> 1) & 0x55u) | ((n << 1) & 0xAAu);
@ -31,7 +33,7 @@ uint16_t bit_reverse_16(uint16_t n)
n = ((n >> 8) & 0x00FFu) | ((n << 8) & 0xFF00u);
return n;
}
/*
uint32_t bit_reverse_32(uint32_t n)
{
n = ((n >> 1) & 0x55555555u) | ((n << 1) & 0xAAAAAAAAu);
@ -42,7 +44,6 @@ uint32_t bit_reverse_32(uint32_t n)
return n;
}
*/
// ************************************
#if 0
@ -105,8 +106,6 @@ uint32_t bit_reverse_32(uint32_t n)
// ************************************
#define FEC_K 7
void error_correction(uint8_t *data)
{ // can correct up to 3 or 4 corrupted bits (I think)
@ -149,7 +148,49 @@ void error_correction(uint8_t *data)
}
}
bool MDC1200_decode_data(uint8_t *data)
void xor_modulation(uint8_t *data, const unsigned int size)
{ // exclusive-or succesive bits - the entire packet
unsigned int i;
uint8_t prev_bit = 0;
for (i = 0; i < size; i++)
{
int bit_num;
uint8_t in = data[i];
uint8_t out = 0;
for (bit_num = 7; bit_num >= 0; bit_num--)
{
const uint8_t new_bit = (in >> bit_num) & 1u;
if (new_bit != prev_bit)
out |= 1u << bit_num; // previous bit and new bit are different - send a '1'
prev_bit = new_bit;
}
data[i] = out ^ 0xff;
}
}
/*
void xor_demodulation(uint8_t *data, const unsigned int size, const bool sync_inverted)
{
unsigned int i;
uint8_t prev_bit = 0;
for (i = 0; i < size; i++)
{
int bit_num;
uint8_t in = data[i];
uint8_t out = 0;
for (bit_num = 7; bit_num >= 0; bit_num--)
{
const uint8_t new_bit = (in >> bit_num) & 1u;
uint8_t bit = prev_bit ^ new_bit;
if (sync_inverted)
bit ^= 1u;
prev_bit = new_bit;
out |= bit << bit_num;
}
data[i] = out;
}
}
*/
bool decode_data(uint8_t *data)
{
uint16_t crc1;
uint16_t crc2;
@ -212,25 +253,7 @@ bool MDC1200_decode_data(uint8_t *data)
crc1 = compute_crc(data, 4);
crc2 = ((uint16_t)data[5] << 8) | (data[4] << 0);
if (crc1 != crc2)
return false; // CRC error, even after using the FEC bits
// valid packet
// extract the info
//uint8_t op = data[0];
//uint8_t arg = data[1];
//uint16_t id = ((uint16_t)data[3] << 8) | (data[2] << 0);
// TODO: pass the above received radio details over to the display function
return true;
return (crc1 == crc2) ? true : false;
}
uint8_t * encode_data(uint8_t *data)
@ -263,8 +286,7 @@ uint8_t * encode_data(uint8_t *data)
}
// 01 00 00 23 DD F0 00 65 00 00 0F 45 1F 21
// 01 00 00 23 DD F0 00 65 00 00 0F 45 1F 21
/*
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
{
const unsigned int size = FEC_K * 2;
@ -275,7 +297,7 @@ uint8_t * encode_data(uint8_t *data)
UART_SendText("\r\n");
}
#endif
*/
{ // interleave the bits
unsigned int i;
@ -329,26 +351,6 @@ uint8_t * encode_data(uint8_t *data)
return data + (FEC_K * 2);
}
void delta_modulation(uint8_t *data, const unsigned int size)
{ // exclusive-or succesive bits
unsigned int i;
uint8_t prev_bit = 0;
for (i = 0; i < size; i++)
{
int bit_num;
uint8_t in = data[i];
uint8_t out = 0;
for (bit_num = 7; bit_num >= 0; bit_num--)
{
const uint8_t new_bit = (in >> bit_num) & 1u;
if (new_bit != prev_bit)
out |= 1u << bit_num; // previous bit and new bit are different - send a '1'
prev_bit = new_bit;
}
data[i] = out ^ 0xff;
}
}
unsigned int MDC1200_encode_single_packet(uint8_t *data, const uint8_t op, const uint8_t arg, const uint16_t unit_id)
{
unsigned int size;
@ -383,7 +385,7 @@ unsigned int MDC1200_encode_single_packet(uint8_t *data, const uint8_t op, const
}
#endif
*/
delta_modulation(data, size);
xor_modulation(data, size);
return size;
}
@ -423,11 +425,198 @@ unsigned int MDC1200_encode_double_packet(uint8_t *data, const uint8_t op, const
size = (unsigned int)(p - data);
delta_modulation(data, size);
xor_modulation(data, size);
return size;
}
*/
struct {
uint8_t bit;
uint8_t prev_bit;
uint8_t xor_bit;
uint64_t shift_reg;
unsigned int bit_count;
unsigned int stage;
bool inverted_sync;
unsigned int data_index;
uint8_t data[40];
} rx;
void MDC1200_reset_rx(void)
{
memset(&rx, 0, sizeof(rx));
}
bool MDC1200_process_rx(const uint8_t rx_byte, uint8_t *op, uint8_t *arg, uint16_t *unit_id)
{
unsigned int i;
int k;
for (k = 7; k >= 0; k--)
{
rx.prev_bit = rx.bit;
rx.bit = (rx_byte >> k) & 1u;
if (rx.stage == 0)
{ // scanning for the pre-amble
rx.xor_bit = rx.bit & 1u;
}
else
{
rx.xor_bit = (rx.xor_bit ^ rx.bit) & 1u;
if (rx.inverted_sync)
rx.xor_bit ^= 1u;
}
rx.shift_reg = (rx.shift_reg << 1) | (rx.xor_bit & 1u);
rx.bit_count++;
// *********
if (rx.stage == 0)
{ // looking for pre-amble
if (rx.bit_count < 20 || (rx.shift_reg & 0xfffff) != 1u)
continue;
rx.xor_bit = 1;
rx.stage = 1;
rx.bit_count = 1;
//s.printf("%5u %2u %u pre-amble found", index, rx_bit_count, rx_packet_stage);
//Memo1->Lines->Add(s);
}
if (rx.stage < 2)
{
//s.printf("%5u %3u %u ", index, rx_bit_count, rx_packet_stage);
//for (uint64_t mask = 1ull << ((sizeof(rx_shift_reg) * 8) - 1); mask != 0; mask >>= 1)
// s += (rx_shift_reg & mask) ? '#' : '.';
//s += " ";
//for (int i = sizeof(rx_shift_reg) - 1; i >= 0; i--)
//{
// String s2;
// s2.printf(" %02X", (uint8_t)(rx_shift_reg >> (i * 8)));
// s += s2;
//}
//Memo1->Lines->Add(s);
}
if (rx.stage == 1)
{ // looking for the 40-bit sync pattern, it follows the 24-bit pre-amble
const unsigned int sync_bit_ok_threshold = 32;
if (rx.bit_count >= sync_bit_ok_threshold)
{
// 40-bit sync pattern
uint64_t sync_nor = 0x07092a446fu; // normal
uint64_t sync_inv = 0xffffffffffu ^ sync_nor; // bit inverted
sync_nor ^= rx.shift_reg;
sync_inv ^= rx.shift_reg;
unsigned int nor_count = 0;
unsigned int inv_count = 0;
for (i = 40; i > 0; i--, sync_nor >>= 1, sync_inv >>= 1)
{
nor_count += sync_nor & 1u;
inv_count += sync_inv & 1u;
}
nor_count = 40 - nor_count;
inv_count = 40 - inv_count;
if (nor_count >= sync_bit_ok_threshold || inv_count >= sync_bit_ok_threshold)
{ // good enough
rx.inverted_sync = (inv_count > nor_count) ? true : false;
//String s;
//s.printf("%5u %2u %u sync found %s %u bits ",
// index,
// rx_bit_count,
// rx_packet_stage,
// rx_inverted_sync ? "inv" : "nor",
// rx_inverted_sync ? inv_count : nor_count);
//for (int i = 4; i >= 0; i--)
//{
// String s2;
// uint8_t b = rx_shift_reg >> (8 * i);
// if (rx_inverted_sync)
// b ^= 0xff;
// s2.printf(" %02X", b);
// s += s2;
//}
//Memo1->Lines->Add(s);
rx.data_index = 0;
rx.bit_count = 0;
rx.stage++;
}
}
continue;
}
// *********
if (rx.stage < 2)
continue;
if (rx.bit_count < 8)
continue;
rx.bit_count = 0;
// 55 55 55 55 55 55 55 07 09 2A 44 6F 94 9C 22 20 32 A4 1A 37 1E 3A 00 98 2C 84
rx.data[rx.data_index++] = rx.shift_reg & 0xff; // save the last 8 bits
if (rx.data_index < (FEC_K * 2))
continue;
// s.printf("%5u %3u %u %2u ", index, rx_bit_count, rx_packet_stage, rx_buffer.size());
// for (i = 0; i < rx_data_index; i++)
// {
// String s2;
// const uint8_t b = rx_buffer[i];
// s2.printf(" %02X", b);
// s += s2;
// }
// Memo1->Lines->Add(s);
if (!decode_data(rx.data))
{
MDC1200_reset_rx();
continue;
}
// extract the info from the packet
*op = rx.data[0];
*arg = rx.data[1];
*unit_id = ((uint16_t)rx.data[3] << 8) | (rx.data[2] << 0);
//s.printf("%5u %3u %u %2u decoded ", index, rx_bit_count, rx_packet_stage, rx_buffer.size());
//for (i = 0; i < 14; i++)
//{
// String s2;
// const uint8_t b = data[i];
// s2.printf(" %02X", b);
// s += s2;
//}
//Memo1->Lines->Add(s);
// reset the detector
MDC1200_reset_rx();
return true;
}
return false;
}
/*
void test(void)
{