altos: Clean up radio CRC handling
authorKeith Packard <keithp@keithp.com>
Thu, 28 Jun 2012 00:17:44 +0000 (17:17 -0700)
committerKeith Packard <keithp@keithp.com>
Thu, 28 Jun 2012 00:17:44 +0000 (17:17 -0700)
Make the FEC code just set the CRC_OK bit like the cc1111 radio does;
eliminates a bunch of weird conventions across the FEC API.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/core/ao.h
src/core/ao_fec.h
src/core/ao_fec_rx.c
src/drivers/ao_cc1120.c
src/drivers/ao_packet.c
src/test/ao_fec_test.c

index 62eb488e41f269dd54f98679404a0874a5b0bdf3..861a0fd479dc70aed5863cafb3591ec5a088b07b 100644 (file)
@@ -511,6 +511,13 @@ extern __xdata uint8_t ao_radio_dma_done;
 extern __xdata uint8_t ao_radio_done;
 extern __xdata uint8_t ao_radio_mutex;
 
+#ifdef PKT_APPEND_STATUS_1_CRC_OK
+#define AO_RADIO_STATUS_CRC_OK PKT_APPEND_STATUS_1_CRC_OK
+#else
+#include <ao_fec.h>
+#define AO_RADIO_STATUS_CRC_OK AO_FEC_DECODE_CRC_OK
+#endif
+
 void
 ao_radio_general_isr(void) ao_arch_interrupt(16);
 
index f1192b62890218c3b3ac84e2e4b959ec89334d45..771732bdf955b85f7ab3da55eaf967f104bb535e 100644 (file)
@@ -70,6 +70,8 @@ ao_fec_encode(const uint8_t *in, uint8_t len, uint8_t *out);
 
 #define AO_FEC_DECODE_BLOCK    (32)    /* callback must return multiples of this many bits */
 
+#define AO_FEC_DECODE_CRC_OK   0x80    /* stored in out[out_len-1] */
+
 uint8_t
 ao_fec_decode(const uint8_t *in, uint16_t in_len, uint8_t *out, uint8_t out_len, uint16_t (*callback)());
 
index 0d400bb0b882ae90dd69c46eb58e9412e7347579..d4c98475833d246ca1a66f380cbb160894703c27 100644 (file)
@@ -211,6 +211,7 @@ ao_fec_decode(const uint8_t *in, uint16_t len, uint8_t *out, uint8_t out_len, ui
                        int8_t          dist = b - (o + 8);     /* distance to last ready-for-writing bit */
                        uint32_t        min_cost;               /* lowest cost */
                        uint8_t         min_state;              /* lowest cost state */
+                       uint8_t         byte;
 
                        /* Find the best fit at the current point
                         * of the decode.
@@ -238,24 +239,27 @@ ao_fec_decode(const uint8_t *in, uint16_t len, uint8_t *out, uint8_t out_len, ui
                        printf ("\tbit %3d min_cost %5d old bit %3d old_state %x bits %02x whiten %0x\n",
                                i/2, min_cost, o + 8, min_state, (bits[p][min_state] >> dist) & 0xff, *whiten);
 #endif
-                       if (out_len) {
-                               uint8_t byte = (bits[p][min_state] >> dist) ^ *whiten++;
+                       byte = (bits[p][min_state] >> dist) ^ *whiten++;
+                       *out++ = byte;
+                       if (out_len > 2)
+                               crc = ao_fec_crc_byte(byte, crc);
 
-                               if (out_len > 2) {
-                                       crc = ao_fec_crc_byte(byte, crc);
-                                       *out++ = byte;
-                               } else {
-                                       *out++ = byte ^ (crc >> 8);
-                                       crc <<= 8;
-                               }
-                               --out_len;
+                       if (!--out_len) {
+                               if ((out[-2] == (uint8_t) (crc >> 8)) &&
+                                   out[-1] == (uint8_t) crc)
+                                       out[-1] = AO_FEC_DECODE_CRC_OK;
+                               else
+                                       out[-1] = 0;
+                               out[-2] = 0;
+                               goto done;
                        }
                        o += 8;
                }
        }
+done:
 #if AO_PROFILE
        ao_fec_decode_start = start_tick;
        ao_fec_decode_end = ao_profile_tick();
 #endif
-       return len/16;
+       return 1;
 }
index c974613e0a49f2cb60280446f54cd0bb277d7a60..30663042677668e17bcb35cf3170341268e9d738 100644 (file)
@@ -690,12 +690,7 @@ ao_radio_recv(__xdata void *d, uint8_t size)
 
        ao_radio_put();
 
-       /* Construct final packet */
-
-       if (ret && ((uint8_t *) d)[size] == 0 && ((uint8_t *)d)[size+1] == 0)
-               ((uint8_t *) d)[size + 1] = 0x80;
-       else
-               ((uint8_t *) d)[size + 1] = 0x00;
+       /* Store the received RSSI value; the crc-OK byte is already done */
 
        ((uint8_t *) d)[size] = (uint8_t) rssi;
 
index 19fe0558b91892d6de9bc316bafd0818ce6892dc..28a0c4155803dc450e81fa4ba938c6489149fef3 100644 (file)
@@ -65,10 +65,8 @@ ao_packet_recv(void)
        /* Check to see if we got a valid packet */
        if (!dma_done)
                return 0;
-#ifdef PKT_APPEND_STATUS_1_CRC_OK
-       if (!(ao_rx_packet.status & PKT_APPEND_STATUS_1_CRC_OK))
+       if (!(ao_rx_packet.status & AO_RADIO_STATUS_CRC_OK))
                return 0;
-#endif
 
        /* Accept packets with matching call signs, or any packet if
         * our callsign hasn't been configured
index 8ce532c8739dcc419aa5acd8a6b72ba6b1f90b00..671fcafcba4d27dc4e7be8fcaf9257a25e1bc50a 100644 (file)
@@ -268,18 +268,17 @@ int
 ao_real_packet(void)
 {
        uint8_t decode[64];
-       uint8_t decode_len;
-       int ok = 0;
+       int     ok;
 
-       decode_len = ao_fec_decode(real_packet, 576, decode, 34, NULL);
+       ok = ao_fec_decode(real_packet, 576, decode, 34, NULL);
 
-       if (decode[32] == 0 && decode[33] == 0) {
+       if (ok && decode[33] == AO_FEC_DECODE_CRC_OK) {
                printf ("match\n");
 
-               ao_fec_dump_bytes(decode, decode_len, "Decode");
-               ok = 1;
+               ao_fec_dump_bytes(decode, 34, "Decode");
        } else {
                printf ("actual packet crc error\n");
+               ok = 0;
        }
        return ok;
 }
@@ -302,7 +301,7 @@ main(int argc, char **argv)
        int             receive_len, receive_errors;
 
        uint8_t         decode[DECODE_LEN(sizeof(original))];
-       int             decode_len;
+       int             decode_ok;
 
        int             errors = 0;
        int             error;
@@ -327,17 +326,17 @@ main(int argc, char **argv)
                receive_len = transmit_len;
                
                /* Decode it */
-               decode_len = ao_fec_decode(receive, receive_len, decode, original_len + 2, NULL);
+               decode_ok = ao_fec_decode(receive, receive_len, decode, original_len + 2, NULL);
 
                /* Check to see if we received the right data */
                error = 0;
 
-               if (decode_len < original_len + 2) {
-                       printf ("len mis-match\n");
+               if (!decode_ok) {
+                       printf ("decode failed\n");
                        error++;
                }
 
-               if (decode[original_len] != 0 || decode[original_len+1] != 0) {
+               if (decode[original_len +1] != AO_FEC_DECODE_CRC_OK) {
                        printf ("crc mis-match\n");
                        error++;
                }