altos: Don't access cc1120 SPI bus during recv after abort has started
[fw/altos] / src / drivers / ao_cc1120.c
index 1b907940f6f1d5104c13fa43da120f9dfc1b882e..f9fc6b2dc2a5c8fda902869f8887451fb087475e 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -44,7 +45,10 @@ extern const uint32_t        ao_radio_cal;
 #define ao_radio_try_select(task_id)   ao_spi_try_get_mask(AO_CC1120_SPI_CS_PORT,(1 << AO_CC1120_SPI_CS_PIN),AO_CC1120_SPI_BUS,AO_SPI_SPEED_4MHz, task_id)
 #define ao_radio_select()      ao_spi_get_mask(AO_CC1120_SPI_CS_PORT,(1 << AO_CC1120_SPI_CS_PIN),AO_CC1120_SPI_BUS,AO_SPI_SPEED_4MHz)
 #define ao_radio_deselect()    ao_spi_put_mask(AO_CC1120_SPI_CS_PORT,(1 << AO_CC1120_SPI_CS_PIN),AO_CC1120_SPI_BUS)
-#define ao_radio_spi_send_sync(d,l)    ao_spi_send_sync((d), (l), AO_CC1120_SPI_BUS)
+#define ao_radio_spi_start_bytes()     ao_spi_start_bytes(AO_CC1120_SPI_BUS)
+#define ao_radio_spi_stop_bytes()      ao_spi_stop_bytes(AO_CC1120_SPI_BUS)
+#define ao_radio_spi_send_byte(b)      ao_spi_send_byte(b, AO_CC1120_SPI_BUS)
+#define ao_radio_spi_recv_byte()       ao_spi_recv_byte(AO_CC1120_SPI_BUS)
 #define ao_radio_spi_send(d,l) ao_spi_send((d), (l), AO_CC1120_SPI_BUS)
 #define ao_radio_spi_send_fixed(d,l) ao_spi_send_fixed((d), (l), AO_CC1120_SPI_BUS)
 #define ao_radio_spi_recv(d,l) ao_spi_recv((d), (l), AO_CC1120_SPI_BUS)
@@ -111,28 +115,23 @@ ao_radio_reg_write(uint16_t addr, uint8_t value)
 static void
 _ao_radio_burst_read_start (uint16_t addr)
 {
-       uint8_t data[2];
-       uint8_t d;
+       ao_radio_spi_start_bytes();
 
        if (CC1120_IS_EXTENDED(addr)) {
-               data[0] = ((1 << CC1120_READ)  |
-                          (1 << CC1120_BURST) |
-                          CC1120_EXTENDED);
-               data[1] = addr;
-               d = 2;
+               ao_radio_spi_send_byte((1 << CC1120_READ)  |
+                                      (1 << CC1120_BURST) |
+                                      CC1120_EXTENDED);
        } else {
-               data[0] = ((1 << CC1120_READ)  |
-                          (1 << CC1120_BURST) |
-                          addr);
-               d = 1;
+               addr |= ((1 << CC1120_READ)  |
+                        (1 << CC1120_BURST));
        }
-
-       ao_radio_spi_send_sync(data, d);
+       ao_radio_spi_send_byte(addr);
 }
 
 static void
 ao_radio_burst_read_stop (void)
 {
+       ao_radio_spi_stop_bytes();
        ao_radio_deselect();
 }
 
@@ -206,9 +205,9 @@ ao_radio_fifo_write_fixed(uint8_t data, uint8_t len)
 }
 
 static uint8_t
-ao_radio_tx_fifo_space(void)
+ao_radio_int_pin(void)
 {
-       return CC1120_FIFO_SIZE - ao_radio_reg_read(CC1120_NUM_TXBYTES);
+       return ao_gpio_get(AO_CC1120_INT_PORT, AO_CC1120_INT_PIN, AO_CC1120_INT);
 }
 
 #if CC1120_DEBUG
@@ -264,11 +263,16 @@ ao_radio_isr(void)
 }
 
 static void
-ao_radio_start_tx(void)
+ao_radio_enable_isr(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);
+}
+
+static void
+ao_radio_start_tx(void)
+{
+       ao_exti_set_callback(AO_CC1120_INT_PORT, AO_CC1120_INT_PIN, ao_radio_isr);
        ao_radio_tx_finished = 0;
        ao_radio_strobe(CC1120_STX);
 }
@@ -290,15 +294,32 @@ ao_radio_idle(void)
 }
 
 /*
- * Packet deviation is 20.5kHz
+ * Packet deviation
  *
  *     fdev = fosc >> 24 * (256 + dev_m) << dev_e
  *
+ * Deviation for 38400 baud should be 20.5kHz:
+ *
  *             32e6Hz / (2 ** 24) * (256 + 80) * (2 ** 5) = 20508Hz
+ *
+ * Deviation for 9600 baud should be 5.125kHz:
+ *
+ *             32e6Hz / (2 ** 24) * (256 + 80) * (2 ** 3) = 5127Hz
+ *
+ * Deviation for 2400 baud should be 1.28125kHz, but cc1111 and
+ * cc115l can't do that, so we'll use 1.5kHz instead:
+ *
+ *             32e6Hz / (2 ** 24) * (256 + 137) * (2 ** 1) = 1499Hz
  */
 
-#define PACKET_DEV_E   5
-#define PACKET_DEV_M   80
+#define PACKET_DEV_M_384       80
+#define PACKET_DEV_E_384       5
+
+#define PACKET_DEV_M_96                80
+#define PACKET_DEV_E_96                3
+
+#define PACKET_DEV_M_24                137
+#define PACKET_DEV_E_24                1
 
 /*
  * For our packet data
@@ -307,41 +328,55 @@ ao_radio_idle(void)
  *     Rdata = -------------------------------------- * fosc
  *                          2 ** 39
  *
+ * Given the bit period of the baseband, T, the bandwidth of the
+ * baseband signal is B = 1/(2T).  The overall bandwidth of the
+ * modulated signal is then Channel bandwidth = 2Δf + 2B.
+ *
+ * 38400 -- 2 * 20500 + 38400 = 79.4 kHz
+ *  9600 -- 2 * 5.125 +  9600 = 19.9 kHz
+ *  2400 -- 2 * 1.5   +  2400 =  5.4 khz
+ *
  * Symbol rate 38400 Baud:
  *
  *     DATARATE_M = 239914
  *     DATARATE_E = 9
- *     CHANBW = 74.42 (round to 100)
+ *     CHANBW = 79.4 (round to 100)
  *
  * Symbol rate 9600 Baud:
  *
  *     DATARATE_M = 239914
  *     DATARATE_E = 7
- *     CHANBW = 58.58 (round to 62.5)
+ *     CHANBW = 19.9 (round to 20)
  *
  * Symbol rate 2400 Baud:
  *
  *     DATARATE_M = 239914
  *     DATARATE_E = 5
- *     CHANBW = 47.61 (round to 50)
+ *     CHANBW = 5.0 (round to 8.0)
  */
 
 #define PACKET_DRATE_M 239914
 
 #define PACKET_DRATE_E_384     9
-#define PACKET_CHAN_BW_384     0x02    /* 200 / 2 = 100 */
+
+/* 200 / 2 = 100 */
+#define PACKET_CHAN_BW_384     ((0 << CC1120_CHAN_BW_CHFILT_BYPASS) | \
+                                (CC1120_CHAN_BW_ADC_CIC_DECFACT_20 << CC1120_CHAN_BW_ADC_CIC_DECFACT) | \
+                                (2 << CC1120_CHAN_BW_BB_CIC_DECFACT))
 
 #define PACKET_DRATE_E_96      7
-#define PACKET_CHAN_BW_96      0x42    /* 125 / 2 = 62.5 */
+/* 200 / 10 = 20 */
+#define PACKET_CHAN_BW_96      ((0 << CC1120_CHAN_BW_CHFILT_BYPASS) | \
+                                (CC1120_CHAN_BW_ADC_CIC_DECFACT_20 << CC1120_CHAN_BW_ADC_CIC_DECFACT) | \
+                                (10 << CC1120_CHAN_BW_BB_CIC_DECFACT))
 
 #define PACKET_DRATE_E_24      5
-#define PACKET_CHAN_BW_24      0x04    /* 200 / 4 = 50 */
+/* 200 / 25 = 8 */
+#define PACKET_CHAN_BW_24      ((0 << CC1120_CHAN_BW_CHFILT_BYPASS) | \
+                                (CC1120_CHAN_BW_ADC_CIC_DECFACT_20 << CC1120_CHAN_BW_ADC_CIC_DECFACT) | \
+                                (25 << CC1120_CHAN_BW_BB_CIC_DECFACT))
 
 static const uint16_t packet_setup[] = {
-       CC1120_DEVIATION_M,     PACKET_DEV_M,
-       CC1120_MODCFG_DEV_E,    ((CC1120_MODCFG_DEV_E_MODEM_MODE_NORMAL << CC1120_MODCFG_DEV_E_MODEM_MODE) |
-                                (CC1120_MODCFG_DEV_E_MOD_FORMAT_2_GFSK << CC1120_MODCFG_DEV_E_MOD_FORMAT) |
-                                (PACKET_DEV_E << CC1120_MODCFG_DEV_E_DEV_E)),
        CC1120_DRATE1,          ((PACKET_DRATE_M >> 8) & 0xff),
        CC1120_DRATE0,          ((PACKET_DRATE_M >> 0) & 0xff),
        CC1120_PKT_CFG2,        ((CC1120_PKT_CFG2_CCA_MODE_ALWAYS_CLEAR << CC1120_PKT_CFG2_CCA_MODE) |
@@ -361,6 +396,10 @@ static const uint16_t packet_setup[] = {
 };
 
 static const uint16_t packet_setup_384[] = {
+       CC1120_DEVIATION_M,     PACKET_DEV_M_384,
+       CC1120_MODCFG_DEV_E,    ((CC1120_MODCFG_DEV_E_MODEM_MODE_NORMAL << CC1120_MODCFG_DEV_E_MODEM_MODE) |
+                                (CC1120_MODCFG_DEV_E_MOD_FORMAT_2_GFSK << CC1120_MODCFG_DEV_E_MOD_FORMAT) |
+                                (PACKET_DEV_E_384 << CC1120_MODCFG_DEV_E_DEV_E)),
        CC1120_DRATE2,          ((PACKET_DRATE_E_384 << CC1120_DRATE2_DATARATE_E) |
                                 (((PACKET_DRATE_M >> 16) & CC1120_DRATE2_DATARATE_M_19_16_MASK) << CC1120_DRATE2_DATARATE_M_19_16)),
        CC1120_CHAN_BW,         PACKET_CHAN_BW_384,
@@ -368,6 +407,10 @@ static const uint16_t packet_setup_384[] = {
 };
 
 static const uint16_t packet_setup_96[] = {
+       CC1120_DEVIATION_M,     PACKET_DEV_M_96,
+       CC1120_MODCFG_DEV_E,    ((CC1120_MODCFG_DEV_E_MODEM_MODE_NORMAL << CC1120_MODCFG_DEV_E_MODEM_MODE) |
+                                (CC1120_MODCFG_DEV_E_MOD_FORMAT_2_GFSK << CC1120_MODCFG_DEV_E_MOD_FORMAT) |
+                                (PACKET_DEV_E_96 << CC1120_MODCFG_DEV_E_DEV_E)),
        CC1120_DRATE2,          ((PACKET_DRATE_E_96 << CC1120_DRATE2_DATARATE_E) |
                                 (((PACKET_DRATE_M >> 16) & CC1120_DRATE2_DATARATE_M_19_16_MASK) << CC1120_DRATE2_DATARATE_M_19_16)),
        CC1120_CHAN_BW,         PACKET_CHAN_BW_96,
@@ -375,16 +418,23 @@ static const uint16_t packet_setup_96[] = {
 };
 
 static const uint16_t packet_setup_24[] = {
+       CC1120_DEVIATION_M,     PACKET_DEV_M_24,
+       CC1120_MODCFG_DEV_E,    ((CC1120_MODCFG_DEV_E_MODEM_MODE_NORMAL << CC1120_MODCFG_DEV_E_MODEM_MODE) |
+                                (CC1120_MODCFG_DEV_E_MOD_FORMAT_2_GFSK << CC1120_MODCFG_DEV_E_MOD_FORMAT) |
+                                (PACKET_DEV_E_24 << CC1120_MODCFG_DEV_E_DEV_E)),
        CC1120_DRATE2,          ((PACKET_DRATE_E_24 << CC1120_DRATE2_DATARATE_E) |
                                 (((PACKET_DRATE_M >> 16) & CC1120_DRATE2_DATARATE_M_19_16_MASK) << CC1120_DRATE2_DATARATE_M_19_16)),
        CC1120_CHAN_BW,         PACKET_CHAN_BW_24,
        CC1120_PA_CFG0,         0x7e,
 };
 
+#define AO_CC1120_TX_BUFFER    64
+
 static const uint16_t packet_tx_setup[] = {
        CC1120_PKT_CFG2,        ((CC1120_PKT_CFG2_CCA_MODE_ALWAYS_CLEAR << CC1120_PKT_CFG2_CCA_MODE) |
                                 (CC1120_PKT_CFG2_PKT_FORMAT_NORMAL << CC1120_PKT_CFG2_PKT_FORMAT)),
-       AO_CC1120_INT_GPIO_IOCFG,               CC1120_IOCFG_GPIO_CFG_RX0TX1_CFG,
+       CC1120_FIFO_CFG,        ((0 << CC1120_FIFO_CFG_CRC_AUTOFLUSH) |
+                                (AO_CC1120_TX_BUFFER << CC1120_FIFO_CFG_FIFO_THR)),
 };
 
 static const uint16_t packet_rx_setup[] = {
@@ -491,6 +541,8 @@ static const uint16_t aprs_setup[] = {
         CC1120_PREAMBLE_CFG1,  ((CC1120_PREAMBLE_CFG1_NUM_PREAMBLE_NONE << CC1120_PREAMBLE_CFG1_NUM_PREAMBLE) |
                                 (CC1120_PREAMBLE_CFG1_PREAMBLE_WORD_AA << CC1120_PREAMBLE_CFG1_PREAMBLE_WORD)),
        CC1120_PA_CFG0,         0x7d,
+       CC1120_FIFO_CFG,        ((0 << CC1120_FIFO_CFG_CRC_AUTOFLUSH) |
+                                (AO_CC1120_TX_BUFFER << CC1120_FIFO_CFG_FIFO_THR)),
 };
 
 /*
@@ -696,8 +748,10 @@ ao_rdf_run(void)
        ao_radio_start_tx();
 
        ao_arch_block_interrupts();
-       while (!ao_radio_wake && !ao_radio_abort && !ao_radio_mcu_wake)
+       while (!ao_radio_wake && !ao_radio_abort && !ao_radio_mcu_wake) {
+               ao_radio_enable_isr();
                ao_sleep(&ao_radio_wake);
+       }
        ao_arch_release_interrupts();
        if (ao_radio_mcu_wake)
                ao_radio_check_marc_status();
@@ -794,31 +848,23 @@ ao_radio_test_cmd(void)
 static void
 ao_radio_wait_isr(uint16_t timeout)
 {
-       if (timeout)
-               ao_alarm(timeout);
        ao_arch_block_interrupts();
        while (!ao_radio_wake && !ao_radio_mcu_wake && !ao_radio_abort)
-               if (ao_sleep(&ao_radio_wake))
+               if (ao_sleep_for(&ao_radio_wake, timeout))
                        ao_radio_abort = 1;
        ao_arch_release_interrupts();
-       if (timeout)
-               ao_clear_alarm();
        if (ao_radio_mcu_wake)
                ao_radio_check_marc_status();
 }
 
-static uint8_t
-ao_radio_wait_tx(uint8_t wait_fifo)
+static void
+ao_radio_wait_fifo(void)
 {
-       uint8_t fifo_space = 0;
-
-       do {
+       while (ao_radio_int_pin() != 0 && !ao_radio_abort) {
+               ao_radio_wake = 0;
+               ao_radio_enable_isr();
                ao_radio_wait_isr(0);
-               if (!wait_fifo)
-                       return 0;
-               fifo_space = ao_radio_tx_fifo_space();
-       } while (!fifo_space && !ao_radio_abort);
-       return fifo_space;
+       }
 }
 
 static uint8_t tx_data[(AO_RADIO_MAX_SEND + 4) * 2];
@@ -830,7 +876,6 @@ ao_radio_send(const void *d, uint8_t size)
        uint8_t         encode_len;
        uint8_t         this_len;
        uint8_t         started = 0;
-       uint8_t         fifo_space;
 
        encode_len = ao_fec_encode(d, size, tx_data);
 
@@ -841,14 +886,17 @@ ao_radio_send(const void *d, uint8_t size)
        /* Flush any pending TX bytes */
        ao_radio_strobe(CC1120_SFTX);
 
-       started = 0;
-       fifo_space = CC1120_FIFO_SIZE;
        while (encode_len) {
                this_len = encode_len;
 
-               ao_radio_wake = 0;
-               if (this_len > fifo_space) {
-                       this_len = fifo_space;
+               if (started) {
+                       ao_radio_wait_fifo();
+                       if (ao_radio_abort)
+                               break;
+               }
+
+               if (this_len > AO_CC1120_TX_BUFFER) {
+                       this_len = AO_CC1120_TX_BUFFER;
                        ao_radio_set_mode(AO_RADIO_MODE_PACKET_TX_BUF);
                } else {
                        ao_radio_set_mode(AO_RADIO_MODE_PACKET_TX_FINISH);
@@ -861,35 +909,32 @@ ao_radio_send(const void *d, uint8_t size)
                if (!started) {
                        ao_radio_start_tx();
                        started = 1;
-               } else {
-                       ao_exti_enable(AO_CC1120_INT_PORT, AO_CC1120_INT_PIN);
-               }
-
-               fifo_space = ao_radio_wait_tx(encode_len != 0);
-               if (ao_radio_abort) {
-                       ao_radio_idle();
-                       break;
                }
        }
-       while (started && !ao_radio_abort && !ao_radio_tx_finished)
+       while (started && !ao_radio_abort && !ao_radio_tx_finished) {
+               ao_radio_wake = 0;
+               ao_radio_enable_isr();
                ao_radio_wait_isr(0);
+       }
+       if (ao_radio_abort)
+               ao_radio_idle();
        ao_radio_put();
 }
 
-#define AO_RADIO_LOTS  64
-
 void
 ao_radio_send_aprs(ao_radio_fill_func fill)
 {
-       uint8_t buf[AO_RADIO_LOTS], *b;
+       uint8_t buf[AO_CC1120_TX_BUFFER];
        int     cnt;
        int     total = 0;
        uint8_t done = 0;
        uint8_t started = 0;
-       uint8_t fifo_space;
 
        ao_radio_get(0xff);
-       fifo_space = CC1120_FIFO_SIZE;
+
+       ao_radio_abort = 0;
+
+       ao_exti_set_callback(AO_CC1120_INT_PORT, AO_CC1120_INT_PIN, ao_radio_isr);
        while (!done) {
                cnt = (*fill)(buf, sizeof(buf));
                if (cnt < 0) {
@@ -898,51 +943,34 @@ ao_radio_send_aprs(ao_radio_fill_func fill)
                }
                total += cnt;
 
-               /* At the last buffer, set the total length */
-               if (done)
-                       ao_radio_set_len(total & 0xff);
-
-               b = buf;
-               while (cnt) {
-                       uint8_t this_len = cnt;
-
-                       /* Wait for some space in the fifo */
-                       while (!ao_radio_abort && (fifo_space = ao_radio_tx_fifo_space()) == 0) {
-                               ao_radio_wake = 0;
-                               ao_radio_wait_isr(0);
-                       }
+               /* Wait for some space in the fifo */
+               if (started) {
+                       ao_radio_wait_fifo();
                        if (ao_radio_abort)
                                break;
-                       if (this_len > fifo_space)
-                               this_len = fifo_space;
-
-                       cnt -= this_len;
-
-                       if (done) {
-                               if (cnt)
-                                       ao_radio_set_mode(AO_RADIO_MODE_APRS_LAST_BUF);
-                               else
-                                       ao_radio_set_mode(AO_RADIO_MODE_APRS_FINISH);
-                       } else
-                               ao_radio_set_mode(AO_RADIO_MODE_APRS_BUF);
-
-                       ao_radio_fifo_write(b, this_len);
-                       b += this_len;
-
-                       if (!started) {
-                               ao_radio_start_tx();
-                               started = 1;
-                       } else
-                               ao_exti_enable(AO_CC1120_INT_PORT, AO_CC1120_INT_PIN);
                }
-               if (ao_radio_abort) {
-                       ao_radio_idle();
-                       break;
+
+               if (done) {
+                       ao_radio_set_len(total & 0xff);
+                       ao_radio_set_mode(AO_RADIO_MODE_APRS_FINISH);
+               } else
+                       ao_radio_set_mode(AO_RADIO_MODE_APRS_BUF);
+
+               ao_radio_fifo_write(buf, cnt);
+
+               if (!started) {
+                       ao_radio_start_tx();
+                       started = 1;
                }
-               /* Wait for the transmitter to go idle */
+       }
+       /* Wait for the transmitter to go idle */
+       while (started && ao_radio_int_pin() != 0 && !ao_radio_abort) {
                ao_radio_wake = 0;
+               ao_radio_enable_isr();
                ao_radio_wait_isr(0);
        }
+       if (ao_radio_abort)
+               ao_radio_idle();
        ao_radio_put();
 }
 
@@ -971,6 +999,8 @@ ao_radio_rx_isr(void)
 {
        uint8_t d;
 
+       if (ao_radio_abort)
+               return;
        if (rx_task_id) {
                if (ao_radio_try_select(rx_task_id)) {
                        ++rx_fast_start;
@@ -990,8 +1020,7 @@ ao_radio_rx_isr(void)
                rx_starting = 0;
                ao_wakeup(&ao_radio_wake);
        }
-       d = AO_CC1120_SPI.dr;
-       AO_CC1120_SPI.dr = 0;
+       d = ao_radio_spi_recv_byte();
        if (rx_ignore == 0) {
                if (rx_data_cur < rx_data_count)
                        rx_data[rx_data_cur++] = d;
@@ -1017,19 +1046,17 @@ ao_radio_rx_isr(void)
 static uint16_t
 ao_radio_rx_wait(void)
 {
-       ao_alarm(AO_MS_TO_TICKS(100));
        ao_arch_block_interrupts();
        rx_waiting = 1;
        while (rx_data_cur - rx_data_consumed < AO_FEC_DECODE_BLOCK &&
               !ao_radio_abort &&
               !ao_radio_mcu_wake)
        {
-               if (ao_sleep(&ao_radio_wake))
+               if (ao_sleep_for(&ao_radio_wake, AO_MS_TO_TICKS(100)))
                        ao_radio_abort = 1;
        }
        rx_waiting = 0;
        ao_arch_release_interrupts();
-       ao_clear_alarm();
        if (ao_radio_abort || ao_radio_mcu_wake)
                return 0;
        rx_data_consumed += AO_FEC_DECODE_BLOCK;
@@ -1062,7 +1089,7 @@ ao_radio_recv(__xdata void *d, uint8_t size, uint8_t timeout)
        rx_data_count = len * 8;        /* bytes to bits */
        rx_data_cur = 0;
        rx_data_consumed = 0;
-       rx_ignore = 2;
+       rx_ignore = 1;
 
        /* Must be set before changing the frequency; any abort
         * after the frequency is set needs to terminate the read
@@ -1082,31 +1109,28 @@ ao_radio_recv(__xdata void *d, uint8_t size, uint8_t timeout)
        ao_exti_set_mode(AO_CC1120_INT_PORT, AO_CC1120_INT_PIN,
                         AO_EXTI_MODE_FALLING|AO_EXTI_PRIORITY_HIGH);
 
-       ao_exti_set_callback(AO_CC1120_INT_PORT, AO_CC1120_INT_PIN, ao_radio_rx_isr);
-       ao_exti_enable(AO_CC1120_INT_PORT, AO_CC1120_INT_PIN);
-
        rx_starting = 1;
        rx_task_id = ao_cur_task->task_id;
 
+       ao_exti_set_callback(AO_CC1120_INT_PORT, AO_CC1120_INT_PIN, ao_radio_rx_isr);
+       ao_exti_enable(AO_CC1120_INT_PORT, AO_CC1120_INT_PIN);
+
        ao_radio_strobe(CC1120_SRX);
 
-       if (timeout)
-               ao_alarm(timeout);
        ao_arch_block_interrupts();
        while (rx_starting && !ao_radio_abort) {
-               if (ao_sleep(&ao_radio_wake))
+               if (ao_sleep_for(&ao_radio_wake, timeout))
                        ao_radio_abort = 1;
        }
        uint8_t rx_task_id_save = rx_task_id;
        rx_task_id = 0;
        rx_starting = 0;
        ao_arch_release_interrupts();
-       if (timeout)
-               ao_clear_alarm();
 
        if (ao_radio_abort) {
+               if (rx_task_id_save == 0)
+                       ao_radio_burst_read_stop();
                ret = 0;
-               rx_task_id = 0;
                goto abort;
        }
 
@@ -1115,9 +1139,7 @@ ao_radio_recv(__xdata void *d, uint8_t size, uint8_t timeout)
                ao_radio_select();
                _ao_radio_burst_read_start(CC1120_SOFT_RX_DATA_OUT);
                if (rx_ignore) {
-                       uint8_t ignore = AO_CC1120_SPI.dr;
-                       (void) ignore;
-                       AO_CC1120_SPI.dr = 0;
+                       (void) ao_radio_spi_recv_byte();
                        --rx_ignore;
                }
        }