1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-10-31 15:07:12 -04:00

M17: CRC16 code cleanup

This commit is contained in:
f4exb 2022-07-27 18:15:47 +02:00
parent 22539b331f
commit a035c42762
4 changed files with 27 additions and 15 deletions

View File

@ -9,23 +9,28 @@
namespace modemm17
{
template <uint16_t Poly = 0x5935, uint16_t Init = 0xFFFF>
struct CRC16
{
static constexpr uint16_t MASK = 0xFFFF;
static constexpr uint16_t LSB = 0x0001;
static constexpr uint16_t MSB = 0x8000;
static const uint16_t MASK = 0xFFFF;
static const uint16_t LSB = 0x0001;
static const uint16_t MSB = 0x8000;
uint16_t reg_ = Init;
CRC16(uint16_t poly = 0x5935, uint16_t init = 0xFFFF) :
poly_(poly),
init_(init)
{
reg_ = init_;
}
void reset()
{
reg_ = Init;
reg_ = init_;
for (size_t i = 0; i != 16; ++i)
{
auto bit = reg_ & LSB;
if (bit) reg_ ^= Poly;
if (bit) reg_ ^= poly_;
reg_ >>= 1;
if (bit) reg_ |= MSB;
}
@ -44,7 +49,7 @@ struct CRC16
{
auto msb = reg & MSB;
reg = ((reg << 1) & MASK) | ((byte >> (7 - i)) & LSB);
if (msb) reg ^= Poly;
if (msb) reg ^= poly_;
}
return reg & MASK;
}
@ -56,7 +61,7 @@ struct CRC16
{
auto msb = reg & MSB;
reg = ((reg << 1) & MASK);
if (msb) reg ^= Poly;
if (msb) reg ^= poly_;
}
return reg;
}
@ -67,6 +72,11 @@ struct CRC16
std::array<uint8_t, 2> result{uint8_t((crc >> 8) & 0xFF), uint8_t(crc & 0xFF)};
return result;
}
private:
uint16_t poly_;
uint16_t init_;
uint16_t reg_;
};
} // modemm17

View File

@ -45,7 +45,7 @@ struct M17FrameDecoder
PolynomialInterleaver<45, 92, 368> interleaver_;
Trellis<4,2> trellis_{makeTrellis<4, 2>({031,027})};
Viterbi<decltype(trellis_), 4> viterbi_{trellis_};
CRC16<0x5935, 0xFFFF> crc_;
CRC16 crc_;
enum class State { LSF, STREAM, BASIC_PACKET, FULL_PACKET, BERT };
enum class SyncWordType { LSF, STREAM, PACKET, BERT };
@ -106,6 +106,7 @@ struct M17FrameDecoder
uint8_t lich_segments{0}; ///< one bit per received LICH fragment.
M17FrameDecoder(callback_t callback) :
crc_(0x5935, 0xFFFF),
callback_(callback)
{}

View File

@ -51,7 +51,7 @@ public:
static const std::array<uint8_t, 2> BERT_SYNC_WORD;
static const std::array<uint8_t, 2> EOT_SYNC;
static constexpr int8_t bits_to_symbol(uint8_t bits)
static int8_t bits_to_symbol(uint8_t bits)
{
switch (bits)
{
@ -120,7 +120,7 @@ public:
M17Randomizer<368> randomizer;
PolynomialInterleaver<45, 92, 368> interleaver;
CRC16<0x5935, 0xFFFF> crc;
CRC16 crc(0x5935, 0xFFFF);
auto rit = std::copy(dest_.begin(), dest_.end(), lsf.begin());
std::copy(source_.begin(), source_.end(), rit);
@ -457,7 +457,8 @@ public:
source_(encode_callsign(source)),
dest_(encode_callsign(dest)),
can_(10),
rrc(makeFirFilter(rrc_taps))
rrc(makeFirFilter(rrc_taps)),
crc_(0x5935, 0xFFFF)
{
gnss_.fill(0);
gnss_on_ = false;
@ -511,7 +512,7 @@ private:
uint8_t can_;
BaseFirFilter<150> rrc;
static const std::array<float, 150> rrc_taps;
CRC16<0x5935, 0xFFFF> crc_;
CRC16 crc_;
static LinkSetupFrame::encoded_call_t encode_callsign(std::string callsign)
{

View File

@ -321,7 +321,7 @@ bool M17DemodProcessor::decode_packet(modemm17::M17FrameDecoder::packet_buffer_t
qDebug() << "M17DemodProcessor::decode_packet: last chunk size:" << packet_size << " packet size:" << m_currentPacket.size();
}
modemm17::CRC16<0x5935, 0xFFFF> crc16;
modemm17::CRC16 crc16(0x5935, 0xFFFF);
crc16.reset();
for (std::vector<uint8_t>::const_iterator it = m_currentPacket.begin(); it != m_currentPacket.end() - 2; ++it) {