|
@@ -5,82 +5,81 @@
|
|
|
* Author: curiousmuch
|
|
|
*/
|
|
|
|
|
|
+#include "aprs_decoder.h"
|
|
|
+
|
|
|
|
|
|
-typedef enum {
|
|
|
- NORMAL,
|
|
|
- FRAME_DECODED,
|
|
|
- ERROR_FCS_MISMATCH,
|
|
|
- ERROR_PACKET_FORMAT,
|
|
|
- ERROR_BUFFER_OVERFLOW
|
|
|
-} decoder_output_t;
|
|
|
-
|
|
|
-typedef enum {
|
|
|
- NONE,
|
|
|
- BUFFER_OVERFLOW,
|
|
|
- BIT_STUFFING_FAILURE,
|
|
|
- FCS_MISMATCH,
|
|
|
-} decoder_error_t;
|
|
|
-
|
|
|
-typedef enum {
|
|
|
- FLAG_SEARCH,
|
|
|
- FLAG_FOUND,
|
|
|
- FRAME_START,
|
|
|
- PACKET_END,
|
|
|
- FRAME_END,
|
|
|
- ABORT,
|
|
|
-} decoder_state_t;
|
|
|
-
|
|
|
-typedef struct {
|
|
|
- decoder_state_t decoder_state;
|
|
|
- uint8_t *frame_buffer;
|
|
|
- uint8_t frame_buffer_index;
|
|
|
- uint8_t frame_buffer_len;
|
|
|
- uint8_t flag_buffer;
|
|
|
- uint8_t flag_buffer_index;
|
|
|
- uint8_t byte_buffer;
|
|
|
- uint8_t current_nrzi_bit;
|
|
|
- uint8_t previous_nrzi_bit;
|
|
|
- uint8_t current_bit;
|
|
|
- uint8_t one_count;
|
|
|
- uint32_t byte_buffer_index;
|
|
|
- uint8_t skip_bit_flag;
|
|
|
-} decoder_varibles_t;
|
|
|
+
|
|
|
+
|
|
|
|
|
|
decoder_varibles_t v;
|
|
|
|
|
|
#define APRS_MAX_FRAME 256
|
|
|
|
|
|
-void frame_buffer_init(uint8_t *buf, uint8_t len)
|
|
|
+uint32_t fcs_calc(uint8_t *data, uint32_t len)
|
|
|
+{
|
|
|
+ uint32_t crc, aprs_polynomial, i, j, byte_lsb, crc_lsb;
|
|
|
+ crc = 0xFFFF;
|
|
|
+ aprs_polynomial = 0x8408;
|
|
|
+ for (i=0;i<len;i++)
|
|
|
+ {
|
|
|
+ for (j=0;j<8;j++)
|
|
|
+ {
|
|
|
+ byte_lsb = (data[i] >> j) & 0x01;
|
|
|
+ crc_lsb = crc & 0x0001;
|
|
|
+ if (crc_lsb != byte_lsb)
|
|
|
+ crc = (crc >> 1) ^ aprs_polynomial;
|
|
|
+ else
|
|
|
+ crc = crc >> 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ crc = crc ^ 0xFFFF;
|
|
|
+ return crc & 0xFFFF;
|
|
|
+}
|
|
|
+
|
|
|
+void frame_buffer_init(uint8_t *buf, uint32_t len)
|
|
|
{
|
|
|
v.frame_buffer = buf;
|
|
|
v.frame_buffer_len = len;
|
|
|
}
|
|
|
|
|
|
-void aprs_decode_init(void)
|
|
|
+void aprs_decoder_init(void)
|
|
|
{
|
|
|
v.decoder_state = FLAG_SEARCH;
|
|
|
v.frame_buffer_index = 0;
|
|
|
|
|
|
v.flag_buffer = 0;
|
|
|
v.flag_buffer_index = 0;
|
|
|
- v.byte_buffer_index = 0;
|
|
|
v.byte_buffer = 0;
|
|
|
+ v.byte_buffer_index = 0;
|
|
|
v.current_nrzi_bit = 0;
|
|
|
v.previous_nrzi_bit = 0;
|
|
|
v.current_bit = 0;
|
|
|
v.one_count = 0;
|
|
|
v.skip_bit_flag = 0;
|
|
|
+ v.packet_rx = 0;
|
|
|
+}
|
|
|
+
|
|
|
+uint8_t get_rx_status(void)
|
|
|
+{
|
|
|
+ if (v.packet_rx)
|
|
|
+ return 1;
|
|
|
+ else
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
uint8_t flag_found(void)
|
|
|
{
|
|
|
if (v.flag_buffer == 0x7E)
|
|
|
+ {
|
|
|
return 1;
|
|
|
+ }
|
|
|
else
|
|
|
+ {
|
|
|
return 0;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
|
|
|
+decoder_output_t aprs_decoder_feed_bit(uint8_t nrzi_bit)
|
|
|
{
|
|
|
v.current_nrzi_bit = nrzi_bit;
|
|
|
|
|
@@ -88,7 +87,7 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
|
|
|
if (v.previous_nrzi_bit == v.current_nrzi_bit)
|
|
|
{
|
|
|
v.current_bit = 1;
|
|
|
- v.one_count =+ 1;
|
|
|
+ v.one_count += 1;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -98,6 +97,7 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
|
|
|
v.previous_nrzi_bit = v.current_nrzi_bit;
|
|
|
|
|
|
|
|
|
+
|
|
|
v.flag_buffer = (v.flag_buffer >> 1) + (v.current_bit*0x80);
|
|
|
|
|
|
switch(v.decoder_state)
|
|
@@ -107,6 +107,7 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
|
|
|
{
|
|
|
|
|
|
v.decoder_state = FLAG_FOUND;
|
|
|
+ v.packet_rx = 1;
|
|
|
|
|
|
|
|
|
v.flag_buffer_index = 0;
|
|
@@ -115,6 +116,7 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
|
|
|
else
|
|
|
{
|
|
|
v.decoder_state = FLAG_SEARCH;
|
|
|
+ v.packet_rx = 0;
|
|
|
}
|
|
|
break;
|
|
|
case FLAG_FOUND:
|
|
@@ -132,6 +134,7 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
|
|
|
|
|
|
uint8_t i, bit;
|
|
|
v.skip_bit_flag = 0;
|
|
|
+ v.byte_buffer = 0;
|
|
|
v.byte_buffer_index = 0;
|
|
|
v.one_count = 0;
|
|
|
|
|
@@ -141,7 +144,7 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
|
|
|
|
|
|
|
|
|
if (bit)
|
|
|
- v.one_count =+ 1;
|
|
|
+ v.one_count += 1;
|
|
|
else
|
|
|
v.one_count = 0;
|
|
|
|
|
@@ -152,12 +155,15 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
|
|
|
|
|
|
|
|
|
if (bit != 0)
|
|
|
+ {
|
|
|
v.decoder_state = ABORT;
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- v.byte_buffer |= bit*(0x8>>i);
|
|
|
- v.byte_buffer_index =+ 1;
|
|
|
+ v.byte_buffer |= (bit) ? (0x80>>i): 0;
|
|
|
+ v.byte_buffer_index += 1;
|
|
|
}
|
|
|
|
|
|
if (v.one_count == 5)
|
|
@@ -176,7 +182,10 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- v.flag_buffer_index =+ 1;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ v.flag_buffer_index += 1;
|
|
|
+ }
|
|
|
break;
|
|
|
case FRAME_START:
|
|
|
|
|
@@ -188,18 +197,18 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
|
|
|
|
|
|
if (v.current_bit != 0)
|
|
|
{
|
|
|
- v.decoder_state = PACKET_END;
|
|
|
+ v.decoder_state = FRAME_BREAK;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
- v.byte_buffer |= v.current_bit*(0x8>>v.byte_buffer_index);
|
|
|
- v.byte_buffer_index =+ 1;
|
|
|
+ v.byte_buffer |= v.current_bit*(0x01<<v.byte_buffer_index);
|
|
|
+ v.byte_buffer_index += 1;
|
|
|
|
|
|
|
|
|
- if (v.byte_buffer_index == 7)
|
|
|
+ if (v.byte_buffer_index > 7)
|
|
|
{
|
|
|
v.frame_buffer[v.frame_buffer_index] = v.byte_buffer;
|
|
|
v.byte_buffer = 0;
|
|
@@ -207,7 +216,7 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
|
|
|
v.frame_buffer_index += 1;
|
|
|
|
|
|
|
|
|
- if (v.frame_buffer_index == v.frame_buffer_len)
|
|
|
+ if (v.frame_buffer_index >= v.frame_buffer_len)
|
|
|
{
|
|
|
v.decoder_state = ABORT;
|
|
|
break;
|
|
@@ -224,12 +233,27 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- if (v.decoder_state == FRAME_END)
|
|
|
+ if (v.decoder_state == FRAME_BREAK)
|
|
|
{
|
|
|
+
|
|
|
+ v.decoder_state = FLAG_SEARCH;
|
|
|
+
|
|
|
|
|
|
+ volatile uint32_t fcs, frame_fcs;
|
|
|
|
|
|
-
|
|
|
- return FRAME_DECODED;
|
|
|
+ if (v.frame_buffer_index < 10)
|
|
|
+ return ERROR_PACKET_FORMAT;
|
|
|
+
|
|
|
+ v.frame_len = v.frame_buffer_index - 2;
|
|
|
+ fcs = fcs_calc(v.frame_buffer, v.frame_len);
|
|
|
+
|
|
|
+ frame_fcs = (v.frame_buffer[v.frame_len+1]<<8) |
|
|
|
+ v.frame_buffer[v.frame_len];
|
|
|
+
|
|
|
+ if (fcs == frame_fcs)
|
|
|
+ return FRAME_DECODED;
|
|
|
+ else
|
|
|
+ return ERROR_FCS_MISMATCH;
|
|
|
}
|
|
|
|
|
|
if (v.decoder_state == ABORT)
|