altos: decode cc1120 received packets
authorKeith Packard <keithp@keithp.com>
Mon, 25 Jun 2012 13:38:34 +0000 (06:38 -0700)
committerKeith Packard <keithp@keithp.com>
Mon, 25 Jun 2012 13:38:34 +0000 (06:38 -0700)
Call the fec decode function, compute RSSI and check CRC

Signed-off-by: Keith Packard <keithp@keithp.com>
src/core/ao_fec.h
src/core/ao_viterbi.c
src/drivers/ao_cc1120.c

index f3f55fa82760b3cd5f6171de7810f83ed7e333cb..e3c55d6e240527a3390ebd799a15381005b350bf 100644 (file)
@@ -67,7 +67,7 @@ ao_fec_encode(uint8_t *in, uint8_t len, uint8_t *out);
  */
 
 uint8_t
-ao_fec_decode(uint8_t *in, uint16_t in_len, uint8_t *out);
+ao_fec_decode(uint8_t *in, uint16_t in_len, uint8_t *out, uint8_t out_len);
 
 /*
  * Interleave data packed in bytes. 'out' must be 'len' bytes long.
index 594c0d91bdbf51be2e79180ba7f639ba2d615df2..7768155669847c62ebb77ad87968f904bc3dd917 100644 (file)
@@ -91,7 +91,7 @@ ao_cost(struct ao_soft_sym a, struct ao_soft_sym b)
  */
 
 uint8_t
-ao_fec_decode(uint8_t *in, uint16_t len, uint8_t *out)
+ao_fec_decode(uint8_t *in, uint16_t len, uint8_t *out, uint8_t out_len)
 {
        static uint16_t cost[2][NUM_STATE];             /* path cost */
        static uint16_t bits[2][NUM_STATE];             /* save bits to quickly output them */
@@ -203,7 +203,10 @@ ao_fec_decode(uint8_t *in, uint16_t len, uint8_t *out)
                        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
-                       *out++ = (bits[p][min_state] >> dist) ^ *whiten++;
+                       if (out_len) {
+                               *out++ = (bits[p][min_state] >> dist) ^ *whiten++;
+                               --out_len;
+                       }
                        o += 8;
                }
        }
index 4378716da6755602cb33a8bb8e2340a18c23c32e..b42ca54c1fdabf74c9103eebca808f2a505ddbff 100644 (file)
@@ -401,12 +401,12 @@ ao_radio_send(void *d, uint8_t size)
 static uint8_t rx_data[2048];
 static uint16_t        rx_data_count;
 static uint16_t rx_data_cur;
-static uint8_t rx_started;
+static uint8_t rx_ignore;
 
 static void
 ao_radio_rx_isr(void)
 {
-       if (rx_started) {
+       if (rx_ignore == 0) {
                rx_data[rx_data_cur++] = stm_spi2.dr;
                if (rx_data_cur >= rx_data_count) {
                        ao_exti_disable(&AO_CC1120_INT_PORT, AO_CC1120_INT_PIN);
@@ -415,7 +415,7 @@ ao_radio_rx_isr(void)
                }
        } else {
                (void) stm_spi2.dr;
-               rx_started = 1;
+               --rx_ignore;
        }
        stm_spi2.dr = 0x00;
 }
@@ -423,12 +423,17 @@ ao_radio_rx_isr(void)
 uint8_t
 ao_radio_recv(__xdata void *d, uint8_t size)
 {
-       uint8_t         len = ((size - 2) + 4) * 2;     /* two bytes for status */
+       uint8_t         len;
        uint16_t        i;
+       uint8_t         rssi;
 
-       rx_data_count = sizeof (rx_data);
+       size -= 2;                      /* status bytes */
+       len = size + 2;                 /* CRC bytes */
+       len += 1 + ~(len & 1);          /* 1 or two pad bytes */
+       len *= 2;                       /* 1/2 rate convolution */
+       rx_data_count = len * 8;        /* bytes to bits */
        rx_data_cur = 0;
-       rx_started = 0;
+       rx_ignore = 2;
 
        printf ("len %d rx_data_count %d\n", len, rx_data_count);
 
@@ -456,31 +461,31 @@ ao_radio_recv(__xdata void *d, uint8_t size)
        ao_radio_strobe(CC1120_SRX);
 
        ao_radio_burst_read_start(CC1120_SOFT_RX_DATA_OUT);
-#if 1
        cli();
        while (!ao_radio_wake && !ao_radio_abort)
                ao_sleep(&ao_radio_wake);
        sei();
-
-#else
-       printf ("Hit a character to stop..."); flush();
-       getchar();
-       putchar('\n');
-       ao_exti_disable(&AO_CC1120_INT_PORT, AO_CC1120_INT_PIN);
-#endif
        ao_radio_burst_read_stop();
 
+       /* Convert from 'real' rssi to cc1111-style values */
+
+       rssi = (((int8_t) ao_radio_reg_read(CC1120_RSSI1)) + 74) * 2;
+
        ao_radio_strobe(CC1120_SIDLE);
 
        ao_radio_put();
 
-       printf ("Received data:");
-       for (i = 0; i < rx_data_cur; i++) {
-               if ((i & 15) == 0)
-                       printf ("\n");
-               printf (" %02x", rx_data[i]);
-       }
-       printf ("\n");
+       /* Construct final packet */
+
+       ao_fec_decode(rx_data, rx_data_cur, d, size + 2);
+
+       if (ao_fec_check_crc(d, size))
+               ((uint8_t *) d)[size + 1] = 0x80;
+       else
+               ((uint8_t *) d)[size + 1] = 0x00;
+
+       ((uint8_t *) d)[size] = (uint8_t) rssi;
+
        return 1;
 }