SPIRIT1 communication - calling twr_spirit1_tx() in scheduled task

Hi all,

I encountered a strange behavior of the twr_spirit1 library that I do not understand properly.

If I change the SPIRIT1 state to RX after TX in application_task(), the handler doesn’t get notified. However, I still get notified on timeout.

Minimal example:

#include <twr.h>
uint8_t *rx_buffer_radio;
uint8_t *tx_buffer_radio;
twr_scheduler_task_id_t app_task_scheduler_id;
void application_init(void);
static void application_task(void *task_param);
static void spirit1_event_handler(twr_spirit1_event_t event, void *event_param);
void application_init(void)
{
    twr_log_init(TWR_LOG_LEVEL_DEBUG, TWR_LOG_TIMESTAMP_ABS);
    twr_scheduler_init();
    app_task_scheduler_id = twr_scheduler_register(application_task, NULL, 1000);
    twr_spirit1_init();
    twr_spirit1_set_event_handler(spirit1_event_handler, NULL);
    rx_buffer_radio = twr_spirit1_get_rx_buffer();
    tx_buffer_radio = twr_spirit1_get_tx_buffer();
    twr_log_debug("START");
}
uint8_t mode = 0;
static void application_task(void *task_param)
{
   if (mode == 0)
    {
        twr_log_debug("Write");
        twr_spirit1_set_tx_length(3);
        twr_spirit1_tx();
    }
    else if (mode == 1)
    {
        twr_log_debug("Read");
        twr_spirit1_set_rx_timeout(3000);
        twr_spirit1_rx();
    }
    mode ^= 1;
    twr_scheduler_plan_current_from_now(5000);
}
static void spirit1_event_handler(twr_spirit1_event_t event, void *event_param)
{
    twr_log_debug("Event: %u", event);
}

In the debugger console, I get, even if I transmit messages from the second module:

[...]
6.00 <D> Read
9.00 <D> Event: 2
11.00 <D> Write
11.00 <D> Event: 2
11.02 <D> Event: 0
[...]

If I modify the example as follows and call twr_spirit1_rx() from the event handler, everything works ok. If twr_spirit1_rx() is called from application_init(), everything works ok too.

static void spirit1_event_handler(twr_spirit1_event_t event, void *event_param)
{
    twr_log_debug("Event: %u", event);
    if (event == TWR_SPIRIT1_EVENT_TX_DONE) {
        twr_spirit1_rx();
    }
}

Do you have any suggestions why the above code does not work properly?

Thanks.

Hi
application_task is already registered as task with id 0.
First I would fix this because it can toggle with that mode.

Hi, thanks for your reply. However, this is not an issue at all, since application_task is declared static and therefore not registered. Even if I rewrite the code as you probably suggest, it has no effect, the spirit1 event handler still never gets notified for RX_DONE event.