Make ao_radio_idle keep trying to get the radio to idle.
[fw/altos] / src / ao_radio.c
index 55a0c297eda633431eb4c1a3638f77e62e02b441..0849349edb1d10a59208aebad7929c523da414f4 100644 (file)
  * RX filter:  93.75 kHz
  */
 
-/*
- * For 434.550MHz, the frequency value is:
- *
- * 434.550e6 / (24e6 / 2**16) = 1186611.2
- */
-
-#define FREQ_CONTROL   1186611
-
 /*
  * For IF freq of 140.62kHz, the IF value is:
  *
@@ -124,10 +116,6 @@ static __code uint8_t radio_setup[] = {
        RF_PA_TABLE1_OFF,       RF_POWER,
        RF_PA_TABLE0_OFF,       RF_POWER,
 
-       RF_FREQ2_OFF,           (FREQ_CONTROL >> 16) & 0xff,
-       RF_FREQ1_OFF,           (FREQ_CONTROL >> 8) & 0xff,
-       RF_FREQ0_OFF,           (FREQ_CONTROL >> 0) & 0xff,
-
        RF_FSCTRL1_OFF,         (IF_FREQ_CONTROL << RF_FSCTRL1_FREQ_IF_SHIFT),
        RF_FSCTRL0_OFF,         (0 << RF_FSCTRL0_FREQOFF_SHIFT),
 
@@ -281,6 +269,24 @@ static __code uint8_t packet_setup[] = {
                                 RF_PKTCTRL0_LENGTH_CONFIG_FIXED),
 };
 
+__xdata uint8_t        ao_radio_dma;
+__xdata uint8_t ao_radio_dma_done;
+__xdata uint8_t ao_radio_done;
+__xdata uint8_t ao_radio_mutex;
+
+void
+ao_radio_general_isr(void) interrupt 16
+{
+       S1CON &= ~0x03;
+       if (RFIF & RFIF_IM_TIMEOUT) {
+               ao_dma_abort(ao_radio_dma);
+               RFIF &= ~ RFIF_IM_TIMEOUT;
+       } else if (RFIF & RFIF_IM_DONE) {
+               ao_radio_done = 1;
+               ao_wakeup(&ao_radio_done);
+               RFIF &= ~RFIF_IM_DONE;
+       }
+}
 
 void
 ao_radio_set_telemetry(void)
@@ -306,29 +312,36 @@ ao_radio_set_rdf(void)
                RF[rdf_setup[i]] = rdf_setup[i+1];
 }
 
-__xdata uint8_t        ao_radio_dma;
-__xdata uint8_t ao_radio_dma_done;
-__xdata uint8_t ao_radio_mutex;
-
 void
 ao_radio_idle(void)
 {
        if (RF_MARCSTATE != RF_MARCSTATE_IDLE)
        {
-               RFST = RFST_SIDLE;
                do {
+                       RFST = RFST_SIDLE;
                        ao_yield();
                } while (RF_MARCSTATE != RF_MARCSTATE_IDLE);
        }
 }
 
 void
-ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant
+ao_radio_get(void)
 {
        ao_config_get();
        ao_mutex_get(&ao_radio_mutex);
        ao_radio_idle();
        RF_CHANNR = ao_config.radio_channel;
+       RF_FREQ2 = (uint8_t) (ao_config.radio_cal >> 16);
+       RF_FREQ1 = (uint8_t) (ao_config.radio_cal >> 8);
+       RF_FREQ0 = (uint8_t) (ao_config.radio_cal);
+}
+
+
+void
+ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant
+{
+       ao_radio_get();
+       ao_radio_done = 0;
        ao_dma_set_transfer(ao_radio_dma,
                            telemetry,
                            &RFDXADDR,
@@ -341,18 +354,15 @@ ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant
                            DMA_CFG1_PRIORITY_HIGH);
        ao_dma_start(ao_radio_dma);
        RFST = RFST_STX;
-       __critical while (!ao_radio_dma_done)
-               ao_sleep(&ao_radio_dma_done);
-       ao_mutex_put(&ao_radio_mutex);
+       __critical while (!ao_radio_done)
+               ao_sleep(&ao_radio_done);
+       ao_radio_put();
 }
 
 uint8_t
 ao_radio_recv(__xdata struct ao_radio_recv *radio) __reentrant
 {
-       ao_config_get();
-       ao_mutex_get(&ao_radio_mutex);
-       ao_radio_idle();
-       RF_CHANNR = ao_config.radio_channel;
+       ao_radio_get();
        ao_dma_set_transfer(ao_radio_dma,
                            &RFDXADDR,
                            radio,
@@ -367,7 +377,7 @@ ao_radio_recv(__xdata struct ao_radio_recv *radio) __reentrant
        RFST = RFST_SRX;
        __critical while (!ao_radio_dma_done)
                ao_sleep(&ao_radio_dma_done);
-       ao_mutex_put(&ao_radio_mutex);
+       ao_radio_put();
        return (ao_radio_dma_done & AO_DMA_DONE);
 }
 
@@ -379,8 +389,8 @@ ao_radio_rdf(int ms)
 {
        uint8_t i;
        uint8_t pkt_len;
-       ao_mutex_get(&ao_radio_mutex);
-       ao_radio_idle();
+
+       ao_radio_get();
        ao_radio_rdf_running = 1;
        for (i = 0; i < sizeof (rdf_setup); i += 2)
                RF[rdf_setup[i]] = rdf_setup[i+1];
@@ -416,13 +426,13 @@ ao_radio_rdf(int ms)
        ao_radio_idle();
        for (i = 0; i < sizeof (telemetry_setup); i += 2)
                RF[telemetry_setup[i]] = telemetry_setup[i+1];
-       ao_mutex_put(&ao_radio_mutex);
+       ao_radio_put();
 }
 
 void
-ao_radio_abort(uint8_t reason)
+ao_radio_abort(void)
 {
-       ao_dma_abort(ao_radio_dma, reason);
+       ao_dma_abort(ao_radio_dma);
        ao_radio_idle();
 }
 
@@ -433,18 +443,19 @@ ao_radio_rdf_abort(void)
                ao_radio_abort();
 }
 
+
 /* Output carrier */
 void
 ao_radio_test(void)
 {
-       ao_config_get();
-       ao_mutex_get(&ao_radio_mutex);
-       ao_radio_idle();
+       ao_set_monitor(0);
+       ao_packet_slave_stop();
+       ao_radio_get();
        printf ("Hit a character to stop..."); flush();
        RFST = RFST_STX;
        getchar();
        ao_radio_idle();
-       ao_mutex_put(&ao_radio_mutex);
+       ao_radio_put();
        putchar('\n');
 }
 
@@ -462,5 +473,8 @@ ao_radio_init(void)
        ao_radio_set_telemetry();
        ao_radio_dma_done = 1;
        ao_radio_dma = ao_dma_alloc(&ao_radio_dma_done);
+       RFIF = 0;
+       RFIM = RFIM_IM_TIMEOUT|RFIM_IM_DONE;
+       IEN2 |= IEN2_RFIE;
        ao_cmd_register(&ao_radio_cmds[0]);
 }