Unable to write pointer from xQueueRecieve

captain_morgan
Posts: 42
Joined: Wed Dec 09, 2015 6:39 pm

Unable to write pointer from xQueueRecieve

Postby captain_morgan » Thu Dec 02, 2021 7:00 pm

The crux of my issue is that I am unable to increment a value on a pointer to a typedef struct that is acquired from xQueueRecieve. The setup is a very standard long running task that waits for xQueueReceive to be fed from the GPIO ISR. The entirety of the code can be found here and I will call out lines and provide snippets here to describe the flow.

Here's the problematic section. Starting here.

Code: Select all

  // allocate lora32_cfg_t to receive config from Queue
  lora32_cfg_t *lora = malloc(sizeof(lora32_cfg_t));
  
  while(1) {
    // wait for event over Queue
    if(xQueueReceive(dio_event_queue, (void*)lora, portMAX_DELAY) != pdPASS) continue;
    
    lora32_set_frequency(lora, lora->frequency + (lora->channels[lora->channel] * bandwidths[lora->bandwidth]));

    lora->channel++;
  }
In this snippet lora->channel does increment but the next time the interrupt is called it's back to 0. Note this is a stripped down version of the task and all pointer reads seem to work fine.

One of my suspicions is the malloc at the start but I'm unable to think of another way to handle multiple devices with a single task. Another area of concern is the path the pointer takes. The setup start around line 583. Here the lora32_cfg_t is passed to the ISR handler, which simple passes it along with xQueueSend, doing nothing else.

Line 583

Code: Select all

gpio_isr_handler_add(lora->dio0, lora32_on_dio, (void*)lora);
Line 449

Code: Select all

 static void IRAM_ATTR lora32_on_dio(void *arg) {
  xQueueSend(dio_event_queue, arg, (TickType_t)0);
}
Note: Changing this to xQueueSendFromISR does not fix the problem.

I'm pretty stumped by this one, when reading all the data seems fine and intact but writing back to the pointer clearly isn't working. Any insight would be appreciated and I'll keep updating this for clarification as I get back to hacking.

captain_morgan
Posts: 42
Joined: Wed Dec 09, 2015 6:39 pm

Re: Unable to write pointer from xQueueRecieve

Postby captain_morgan » Thu Dec 02, 2021 9:10 pm

wowee! took a fair bit of hacking but I found my issue. It's strange this worked at all and clearly I need a better understand of passing pointers to FreeRTOS Queues. It was a two part issue, first I was initializing my Queue incorrectly.

Code: Select all

dio_event_queue = xQueueCreate(10, sizeof(lora32_cfg_t));
should have been

Code: Select all

dio_event_queue = xQueueCreate(10, sizeof(lora32_cfg_t *));
With that in place I changed the ISR from

Code: Select all

static void IRAM_ATTR lora32_on_dio(void *arg) {
  xQueueSend(dio_event_queue, arg, (TickType_t)0);
}
to

Code: Select all

static void IRAM_ATTR lora32_on_dio(void *arg) {
  xQueueSend(dio_event_queue, (void*)&arg, (TickType_t)0);
}
It's this second part I dont' quite understand, if I'm already receiving a pointer, why do I need to also point to that? Anyhow, esp32-lora now support channel hopping!

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 69 guests