diff --git a/source/NanostackRfPhyAtmel.cpp b/source/NanostackRfPhyAtmel.cpp index 8d60cc3..a62e1fe 100644 --- a/source/NanostackRfPhyAtmel.cpp +++ b/source/NanostackRfPhyAtmel.cpp @@ -26,6 +26,7 @@ #include "nanostack/platform/arm_hal_phy.h" #include "mbed_trace.h" #include "mbed_toolchain.h" +#include "events/mbed_shared_queues.h" #define TRACE_GROUP "AtRF" @@ -219,17 +220,8 @@ static inline rf_trx_states_t rf_if_trx_status_from_full(uint8_t full_trx_status #ifdef MBED_CONF_RTOS_PRESENT #include "mbed.h" -#include "rtos.h" static void rf_if_irq_task_process_irq(); - -#define SIG_RADIO 1 -#define SIG_TIMER_ACK 2 -#define SIG_TIMER_CAL 4 -#define SIG_TIMER_CCA 8 - -#define SIG_TIMERS (SIG_TIMER_ACK|SIG_TIMER_CAL|SIG_TIMER_CCA) -#define SIG_ALL (SIG_RADIO|SIG_TIMERS) #endif // HW pins to RF chip @@ -256,9 +248,7 @@ class RFBits { Timeout cal_timer; Timeout cca_timer; #ifdef MBED_CONF_RTOS_PRESENT - Thread irq_thread; - Mutex mutex; - void rf_if_irq_task(); + EventQueue *equeue; #endif }; @@ -270,12 +260,9 @@ RFBits::RFBits(PinName spi_mosi, PinName spi_miso, RST(spi_rst), SLP_TR(spi_slp), IRQ(spi_irq) -#ifdef MBED_CONF_RTOS_PRESENT -,irq_thread(osPriorityRealtime, 1024) -#endif { #ifdef MBED_CONF_RTOS_PRESENT - irq_thread.start(mbed::callback(this, &RFBits::rf_if_irq_task)); + equeue = mbed_highprio_event_queue(); #endif } @@ -284,6 +271,9 @@ static uint8_t rf_part_num = 0; /*TODO: RSSI Base value setting*/ static int8_t rf_rssi_base_val = -91; +/* Lock against background operations (disable interrupts, or claim the + * background mutex). + */ static void rf_if_lock(void) { platform_enter_critical(); @@ -294,20 +284,37 @@ static void rf_if_unlock(void) platform_exit_critical(); } +/* Perform lock from a background operation (nothing, if background are + * interrupts, else claim the background mutex) + */ +static void rf_if_lock_in_bg(void) +{ +#ifdef MBED_CONF_RTOS_PRESENT + rf_if_lock(); +#endif +} + +static void rf_if_unlock_in_bg(void) +{ +#ifdef MBED_CONF_RTOS_PRESENT + rf_if_unlock(); +#endif +} + #ifdef MBED_CONF_RTOS_PRESENT static void rf_if_cca_timer_signal(void) { - rf->irq_thread.signal_set(SIG_TIMER_CCA); + rf->equeue->call(rf_cca_timer_interrupt); } static void rf_if_cal_timer_signal(void) { - rf->irq_thread.signal_set(SIG_TIMER_CAL); + rf->equeue->call(rf_calibration_timer_interrupt); } static void rf_if_ack_timer_signal(void) { - rf->irq_thread.signal_set(SIG_TIMER_ACK); + rf->equeue->call(rf_ack_wait_timer_interrupt); } #endif @@ -1033,33 +1040,7 @@ static void rf_if_disable_irq(void) #ifdef MBED_CONF_RTOS_PRESENT static void rf_if_interrupt_handler(void) { - rf->irq_thread.signal_set(SIG_RADIO); -} - -// Started during construction of rf, so variable -// rf isn't set at the start. Uses 'this' instead. -void RFBits::rf_if_irq_task(void) -{ - for (;;) { - osEvent event = irq_thread.signal_wait(0); - if (event.status != osEventSignal) { - continue; - } - rf_if_lock(); - if (event.value.signals & SIG_RADIO) { - rf_if_irq_task_process_irq(); - } - if (event.value.signals & SIG_TIMER_ACK) { - rf_ack_wait_timer_interrupt(); - } - if (event.value.signals & SIG_TIMER_CCA) { - rf_cca_timer_interrupt(); - } - if (event.value.signals & SIG_TIMER_CAL) { - rf_calibration_timer_interrupt(); - } - rf_if_unlock(); - } + rf->equeue->call(rf_if_irq_task_process_irq); } static void rf_if_irq_task_process_irq(void) @@ -1074,6 +1055,8 @@ static void rf_if_irq_task_process_irq(void) static void rf_if_interrupt_handler(void) #endif { + rf_if_lock_in_bg(); + static uint8_t last_is, last_ts; uint8_t irq_status, full_trx_status; uint8_t orig_xah_ctrl_1 = xah_ctrl_1; @@ -1107,6 +1090,8 @@ static void rf_if_interrupt_handler(void) } last_is = irq_status; last_ts = full_trx_status; + + rf_if_unlock_in_bg(); } /* @@ -1278,10 +1263,14 @@ static void rf_ack_wait_timer_interrupt(void) */ static void rf_calibration_timer_interrupt(void) { + rf_if_lock_in_bg(); + /*Calibrate RF*/ rf_calibration_cb(); /*Start new calibration timeout*/ rf_calibration_timer_start(RF_CALIBRATION_INTERVAL); + + rf_if_unlock_in_bg(); } /* @@ -1293,9 +1282,13 @@ static void rf_calibration_timer_interrupt(void) */ static void rf_cca_timer_interrupt(void) { + rf_if_lock_in_bg(); + rf_flags_set(RFF_CCA); /*Start CCA process*/ rf_if_start_cca_process(); + + rf_if_unlock_in_bg(); } /*