Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use shared event queue #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 39 additions & 46 deletions source/NanostackRfPhyAtmel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand All @@ -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
};

Expand All @@ -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
}

Expand All @@ -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();
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}

/*
Expand Down Expand Up @@ -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();
}

/*
Expand All @@ -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();
}

/*
Expand Down