WIFI vs Semaphore

vgonet
Posts: 20
Joined: Thu Dec 13, 2018 10:01 am

WIFI vs Semaphore

Postby vgonet » Tue Dec 18, 2018 2:02 pm

Hello,

I try to synchronize a task with an interruption of a timer at 125us by using a semaphore (xSemaphoreGiveFromISR). The task is executed every 125us as expected until I add WIFI to my project.

Code: Select all

void SensorTask(void* pvParameter)
{
  ESP_LOGI(Tag, "Start Sensor Task");

  // Attempt to create a semaphore
  Timer125usSemaphore = xSemaphoreCreateBinary();

  timer_start(TIMER_GROUP_0, 0);

  while (1)
  {
    if (Timer125usSemaphore != NULL)
    {
      if (xSemaphoreTake(Timer125usSemaphore, 0) == pdTRUE)
      {
        Leds_RunTask();
	Sensors_RunTask();
      }
    }
    FeedWatchdog();
  }
}


void IRAM_ATTR timer_group0_isr(void* para)
{
  static BaseType_t higherPriorityTaskWoken = pdFALSE;

  int timer_idx = (int) para;

  // Retrieve the interrupt status and the counter value
  // from the timer that reported the interrupt
  uint32_t intr_status = TIMERG0.int_st_timers.val;
  TIMERG0.hw_timer[timer_idx].update = 1;

  // After the alarm has been triggered, we need enable it again, so it is triggered the next time
  TIMERG0.hw_timer[timer_idx].config.alarm_en = TIMER_ALARM_EN;

  // Clear the interrupt and update the alarm time for the timer with without reload
  if ((intr_status & BIT(timer_idx)) && timer_idx == TIMER_1)
  {
    TIMERG0.int_clr_timers.t1 = 1;

    xSemaphoreGiveFromISR(Timer125usSemaphore, &higherPriorityTaskWoken);

    if (higherPriorityTaskWoken != pdFALSE)
    {
      portYIELD_FROM_ISR();
    }
  }
}
The "esp_wifi_start" function has the effect of disrupting the semaphore mechanism.

Code: Select all

void WIFI_Init(void)
{
  EventGroup = xEventGroupCreate();

  // Initialize the TCP Stack
  tcpip_adapter_init();

  // Initialize the system event handler
  ESP_ERROR_CHECK(esp_event_loop_init(EventHandler, NULL));

  // Configure the WIFI
  wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  ESP_ERROR_CHECK(esp_wifi_init(&cfg));

  wifi_config_t wifi_config =
  {
    .sta =
    {
      .ssid = CONFIG_WIFI_SSID,
      .password = CONFIG_WIFI_PASSWORD
    },
  };

  // WIFI as Station Mode (connect to another wifi)
  ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));

  // Configure RAM as the WIFI parameters storage
  //ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));

  ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
  ESP_ERROR_CHECK(esp_wifi_start());
  ESP_ERROR_CHECK(esp_wifi_connect());
}
Do you have any idea how to solve this problem ?
Thanks

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: WIFI vs Semaphore

Postby fly135 » Tue Dec 18, 2018 4:01 pm

What you just posted helps me out with another question I posted. If you have 2 cores and pin the task to the second core, it might fix the wifi problem. Although I'm thinking that you may hit the issue again when you add a second task. I'm wondering if signals invoke the task scheduler immediately or wait until the 10msec interval. Although you running on the first core with the idle task suggests that it does invoke immediately.

John A

ESP_Sprite
Posts: 9761
Joined: Thu Nov 26, 2015 4:08 am

Re: WIFI vs Semaphore

Postby ESP_Sprite » Wed Dec 19, 2018 2:38 am

In what way is your semaphore 'disrupted'?

vgonet
Posts: 20
Joined: Thu Dec 13, 2018 10:01 am

Re: WIFI vs Semaphore

Postby vgonet » Wed Dec 19, 2018 7:41 am

Hello,

For the debug, my task toggles a pin and I check it with a scope.

With the WIFI, when my timer is configured at 270us, my task is called directly after the timer interrupt is executed (via the semaphore). So the pin toggles every 270us.

With the WIFI, when my timer is configured at 125us, the pin toggles every 125u during about 100ms (or more, it depends) and then the pin is stucked at a fixed level. So I think the semaphore is not able to "give the key" to the task.

Without the WIFI, when my timer is configured at 125us, the pin toggles every 125u.

I've tried to use notification instead of semaphore but I've the same result.

ESP_Sprite
Posts: 9761
Joined: Thu Nov 26, 2015 4:08 am

Re: WIFI vs Semaphore

Postby ESP_Sprite » Wed Dec 19, 2018 9:53 am

That is strange... Are you sure it's the semaphore that's breaking, and not the timer or the timer interrupt?

vgonet
Posts: 20
Joined: Thu Dec 13, 2018 10:01 am

Re: WIFI vs Semaphore

Postby vgonet » Wed Dec 19, 2018 1:28 pm

Hello,

Thanks to your messages, I was able to solve my problem. I've simply used the core 1 for the WIFI and the core 0 for my task. I hope it will be ok when I will add the other tasks... and when I will share data between the two cores...

Thank you very much for your help.
Vincent

ESP_Sprite
Posts: 9761
Joined: Thu Nov 26, 2015 4:08 am

Re: WIFI vs Semaphore

Postby ESP_Sprite » Thu Dec 20, 2018 2:29 am

Hm, note that this is a workaround, not a fix. There still should be no reason why semaphores should break when you enable WiFi. Is there any chance you can post your entire project here so we can take a look at what's going wrong?

vgonet
Posts: 20
Joined: Thu Dec 13, 2018 10:01 am

Re: WIFI vs Semaphore

Postby vgonet » Thu Dec 20, 2018 9:30 am

Hello,

In attachments, you will find the code that show my problem between WIFI and semaphore. I hope this simple example will help us.

To "solve" the problem, you have to open menuconfig and change the core ID:
Component config -> Wi-Fi -> WiFi Task Core ID

Thank you and best regards !!!
Vincent
Attachments
main.c
(11.44 KiB) Downloaded 871 times
gpio.h
(3.79 KiB) Downloaded 841 times
gpio.c
(7.27 KiB) Downloaded 868 times

rontec
Posts: 17
Joined: Thu Dec 13, 2018 11:06 pm

Re: WIFI vs Semaphore

Postby rontec » Fri Dec 21, 2018 12:44 am

ESP_Sprite wrote:
Thu Dec 20, 2018 2:29 am
There still should be no reason why semaphores should break when you enable WiFi.
This post, which contains some code, https://www.esp32.com/viewtopic.php?f=2&t=3980, could possibly be a related problem.

Who is online

Users browsing this forum: Bing [Bot] and 101 guests