altos: Clear any broken cc1120 TX fifo bits before transmitting
[fw/altos] / src / drivers / ao_cc1120.c
index 3e894f762b6809493bee519fa34cf2276504568b..07ebf8356cf5f3e108d4eaa202fd64e19d840d03 100644 (file)
@@ -30,6 +30,7 @@ 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 */
 
 #define CC1120_DEBUG   AO_FEC_DEBUG
 #define CC1120_TRACE   0
@@ -38,7 +39,7 @@ extern const uint32_t ao_radio_cal;
 
 #define FOSC   32000000
 
-#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_1MHz)
+#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(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)
@@ -242,6 +243,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 +261,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 +272,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);
@@ -671,12 +677,17 @@ ao_radio_test_cmd(void)
 }
 
 static void
-ao_radio_wait_isr(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)
-               ao_sleep(&ao_radio_wake);
+               if (ao_sleep(&ao_radio_wake))
+                       ao_radio_abort = 1;
        ao_arch_release_interrupts();
+       if (timeout)
+               ao_clear_alarm();
        if (ao_radio_mcu_wake)
                ao_radio_check_marc_status();
 }
@@ -687,7 +698,7 @@ ao_radio_wait_tx(uint8_t wait_fifo)
        uint8_t fifo_space = 0;
 
        do {
-               ao_radio_wait_isr();
+               ao_radio_wait_isr(0);
                if (!wait_fifo)
                        return 0;
                fifo_space = ao_radio_tx_fifo_space();
@@ -706,11 +717,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) {
@@ -741,13 +758,15 @@ 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();
 }
 
 #define AO_RADIO_LOTS  64
 
 void
-ao_radio_send_lots(ao_radio_fill_func fill)
+ao_radio_send_aprs(ao_radio_fill_func fill)
 {
        uint8_t buf[AO_RADIO_LOTS], *b;
        int     cnt;
@@ -777,7 +796,7 @@ ao_radio_send_lots(ao_radio_fill_func fill)
                        /* 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();
+                               ao_radio_wait_isr(0);
                        }
                        if (ao_radio_abort)
                                break;
@@ -809,7 +828,7 @@ ao_radio_send_lots(ao_radio_fill_func fill)
                }
                /* Wait for the transmitter to go idle */
                ao_radio_wake = 0;
-               ao_radio_wait_isr();
+               ao_radio_wait_isr(0);
        }
        ao_radio_put();
 }
@@ -834,8 +853,8 @@ ao_radio_rx_isr(void)
 {
        uint8_t d;
 
-       d = stm_spi2.dr;
-       stm_spi2.dr = 0;
+       d = AO_CC1120_SPI.dr;
+       AO_CC1120_SPI.dr = 0;
        if (rx_ignore == 0) {
                if (rx_data_cur >= rx_data_count)
                        ao_exti_disable(AO_CC1120_INT_PORT, AO_CC1120_INT_PIN);
@@ -862,6 +881,7 @@ ao_radio_rx_wait(void)
        do {
                if (ao_radio_mcu_wake)
                        ao_radio_check_marc_status();
+               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 &&
@@ -873,6 +893,7 @@ ao_radio_rx_wait(void)
                }
                rx_waiting = 0;
                ao_arch_release_interrupts();
+               ao_clear_alarm();
        } while (ao_radio_mcu_wake);
        if (ao_radio_abort)
                return 0;
@@ -884,7 +905,7 @@ ao_radio_rx_wait(void)
 }
 
 uint8_t
-ao_radio_recv(__xdata void *d, uint8_t size)
+ao_radio_recv(__xdata void *d, uint8_t size, uint8_t timeout)
 {
        uint8_t         len;
        uint16_t        i;
@@ -922,10 +943,10 @@ ao_radio_recv(__xdata void *d, uint8_t size)
        ao_radio_wake = 0;
        ao_radio_mcu_wake = 0;
 
-       stm_spi2.cr2 = 0;
+       AO_CC1120_SPI.cr2 = 0;
 
        /* clear any RXNE */
-       (void) stm_spi2.dr;
+       (void) AO_CC1120_SPI.dr;
 
        /* Have the radio signal when the preamble quality goes high */
        ao_radio_reg_write(AO_CC1120_INT_GPIO_IOCFG, CC1120_IOCFG_GPIO_CFG_PQT_REACHED);
@@ -938,7 +959,7 @@ ao_radio_recv(__xdata void *d, uint8_t size)
        ao_radio_strobe(CC1120_SRX);
 
        /* Wait for the preamble to appear */
-       ao_radio_wait_isr();
+       ao_radio_wait_isr(timeout);
        if (ao_radio_abort)
                goto abort;
 
@@ -1195,7 +1216,7 @@ static void ao_radio_show(void) {
 }
 
 static void ao_radio_beep(void) {
-       ao_radio_rdf(RDF_PACKET_LEN);
+       ao_radio_rdf();
 }
 
 static void ao_radio_packet(void) {
@@ -1231,6 +1252,7 @@ ao_radio_test_recv()
        }
 }
 
+#if HAS_APRS
 #include <ao_aprs.h>
 
 static void
@@ -1239,13 +1261,16 @@ ao_radio_aprs()
        ao_packet_slave_stop();
        ao_aprs_send();
 }
+#endif
 
 #endif
 
 static const struct ao_cmds ao_radio_cmds[] = {
        { ao_radio_test_cmd,    "C <1 start, 0 stop, none both>\0Radio carrier test" },
 #if CC1120_DEBUG
+#if HAS_APRS
        { ao_radio_aprs,        "G\0Send APRS packet" },
+#endif
        { ao_radio_show,        "R\0Show CC1120 status" },
        { ao_radio_beep,        "b\0Emit an RDF beacon" },
        { ao_radio_packet,      "p\0Send a test packet" },