altos: Make cc1120 driver return false on recv timeout
[fw/altos] / src / drivers / ao_cc1120.c
index 5add45e437ac84daf7a81c49a94d3c4b3562ea43..3bef6c90fbc9f62ab43232f2f5926c3e3cc3cbdd 100644 (file)
@@ -30,6 +30,9 @@ static uint8_t ao_radio_wake;         /* radio ready. Also used as sleep address */
 static uint8_t ao_radio_abort;         /* radio operation should abort */
 static uint8_t ao_radio_mcu_wake;      /* MARC status change */
 static uint8_t ao_radio_marc_status;   /* Last read MARC status value */
+static uint8_t ao_radio_tx_finished;   /* MARC status indicates TX finished */
+
+int8_t ao_radio_rssi;                  /* Last received RSSI value */
 
 #define CC1120_DEBUG   AO_FEC_DEBUG
 #define CC1120_TRACE   0
@@ -242,6 +245,8 @@ ao_radio_check_marc_status(void)
        /* Anyt other than 'tx/rx finished' means an error occurred */
        if (ao_radio_marc_status & ~(CC1120_MARC_STATUS1_TX_FINISHED|CC1120_MARC_STATUS1_RX_FINISHED))
                ao_radio_abort = 1;
+       if (ao_radio_marc_status & (CC1120_MARC_STATUS1_TX_FINISHED))
+               ao_radio_tx_finished = 1;
 }
 
 static void
@@ -258,6 +263,7 @@ ao_radio_start_tx(void)
        ao_exti_set_callback(AO_CC1120_INT_PORT, AO_CC1120_INT_PIN, ao_radio_isr);
        ao_exti_enable(AO_CC1120_INT_PORT, AO_CC1120_INT_PIN);
        ao_exti_enable(AO_CC1120_MCU_WAKEUP_PORT, AO_CC1120_MCU_WAKEUP_PIN);
+       ao_radio_tx_finished = 0;
        ao_radio_strobe(CC1120_STX);
 }
 
@@ -268,6 +274,8 @@ ao_radio_idle(void)
                uint8_t state = ao_radio_strobe(CC1120_SIDLE);
                if ((state >> CC1120_STATUS_STATE) == CC1120_STATUS_STATE_IDLE)
                        break;
+               if ((state >> CC1120_STATUS_STATE) == CC1120_STATUS_STATE_TX_FIFO_ERROR)
+                       ao_radio_strobe(CC1120_SFTX);
        }
        /* Flush any pending TX bytes */
        ao_radio_strobe(CC1120_SFTX);
@@ -546,6 +554,7 @@ ao_radio_get(uint8_t len)
        static uint32_t last_radio_setting;
 
        ao_mutex_get(&ao_radio_mutex);
+
        if (!ao_radio_configured)
                ao_radio_setup();
        if (ao_config.radio_setting != last_radio_setting) {
@@ -711,11 +720,17 @@ ao_radio_send(const void *d, uint8_t size)
        uint8_t         this_len;
        uint8_t         started = 0;
        uint8_t         fifo_space;
+       uint8_t         q;
 
        encode_len = ao_fec_encode(d, size, tx_data);
 
        ao_radio_get(encode_len);
 
+       ao_radio_abort = 0;
+
+       /* Flush any pending TX bytes */
+       ao_radio_strobe(CC1120_SFTX);
+
        started = 0;
        fifo_space = CC1120_FIFO_SIZE;
        while (encode_len) {
@@ -746,6 +761,8 @@ ao_radio_send(const void *d, uint8_t size)
                        break;
                }
        }
+       while (started && !ao_radio_abort && !ao_radio_tx_finished)
+               ao_radio_wait_isr(0);
        ao_radio_put();
 }
 
@@ -895,7 +912,8 @@ ao_radio_recv(__xdata void *d, uint8_t size, uint8_t timeout)
 {
        uint8_t         len;
        uint16_t        i;
-       uint8_t         rssi;
+       uint8_t         radio_rssi = 0;
+       uint8_t         rssi0;
        uint8_t         ret;
        static int been_here = 0;
 
@@ -946,8 +964,10 @@ ao_radio_recv(__xdata void *d, uint8_t size, uint8_t timeout)
 
        /* Wait for the preamble to appear */
        ao_radio_wait_isr(timeout);
-       if (ao_radio_abort)
+       if (ao_radio_abort) {
+               ret = 0;
                goto abort;
+       }
 
        ao_radio_reg_write(AO_CC1120_INT_GPIO_IOCFG, CC1120_IOCFG_GPIO_CFG_CLKEN_SOFT);
        ao_exti_set_mode(AO_CC1120_INT_PORT, AO_CC1120_INT_PIN,
@@ -963,17 +983,26 @@ ao_radio_recv(__xdata void *d, uint8_t size, uint8_t timeout)
        ao_radio_burst_read_stop();
 
 abort:
-       ao_radio_strobe(CC1120_SIDLE);
-
        /* Convert from 'real' rssi to cc1111-style values */
 
-       rssi = AO_RADIO_FROM_RSSI(ao_radio_reg_read(CC1120_RSSI1));
+       rssi0 = ao_radio_reg_read(CC1120_RSSI0);
+       if (rssi0 & 1) {
+               int8_t rssi = ao_radio_reg_read(CC1120_RSSI1);
+               ao_radio_rssi = rssi;
+
+               /* Bound it to the representable range */
+               if (rssi > -11)
+                       rssi = -11;
+               radio_rssi = AO_RADIO_FROM_RSSI (rssi);
+       }
+
+       ao_radio_strobe(CC1120_SIDLE);
 
        ao_radio_put();
 
        /* Store the received RSSI value; the crc-OK byte is already done */
 
-       ((uint8_t *) d)[size] = (uint8_t) rssi;
+       ((uint8_t *) d)[size] = radio_rssi;
 
 #if AO_PROFILE
        rx_last_done_tick = rx_done_tick;