esp_restart() from esp_timer callback doesn't work in 3.1.3

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: esp_restart() broken in 3.1.3

Postby ESP_igrr » Tue Mar 05, 2019 10:38 pm

The issue here might be that you are trying to call esp_restart from the timer callback, and esp_restart will call esp_wifi_stop. It is possible that esp_wifi_stop does not work from a timer callback; only from a task. Can you try moving esp_restart from a timer callback into your application task?

phatpaul
Posts: 110
Joined: Fri Aug 24, 2018 1:14 pm

Re: esp_restart() broken in 3.1.3

Postby phatpaul » Tue Mar 05, 2019 10:45 pm

Thanks igrr,
Yes I tried call esp_restart() from my http server task, it works. (currently testing in 3.2)

So is this an expected limitation? I guess I have to move my reset push-button handler to a task?

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: esp_restart() broken in 3.1.3

Postby ESP_igrr » Wed Mar 06, 2019 12:40 am

Although undocumented, currently it is indeed an expected limitation. I will create a ticket to look into lifting it. However for now it's better to move the restart handler out of the timer callback.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: esp_restart() broken in 3.1.3

Postby ESP_Angus » Wed Mar 06, 2019 12:46 am

One addition to Ivan's comment: If you still want to restart from a timer callback, and you don't need a high precision timer which runs at a high FreeRTOS priority, then you can use a FreeRTOS timer to call esp_restart(). That should work with no other changes.
https://docs.espressif.com/projects/esp ... Function_t

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: esp_restart() broken in 3.1.3

Postby Ritesh » Wed Mar 06, 2019 4:32 am

ESP_igrr wrote:
Tue Mar 05, 2019 10:38 pm
The issue here might be that you are trying to call esp_restart from the timer callback, and esp_restart will call esp_wifi_stop. It is possible that esp_wifi_stop does not work from a timer callback; only from a task. Can you try moving esp_restart from a timer callback into your application task?
Yes.

But, he has checked issue into TAG 3.1.1. in which same functionality is working fine without any issue.
Regards,
Ritesh Prajapati

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: esp_restart() broken in 3.1.3

Postby ESP_Angus » Wed Mar 06, 2019 6:21 am

Ritesh wrote:
Wed Mar 06, 2019 4:32 am
But, he has checked issue into TAG 3.1.1. in which same functionality is working fine without any issue.
Yes. The most likely root cause is the following (from the 3.1.2 release notes):
  • Add a timer which is started by esp_wifi_stop(). Stop Wi-Fi forcibly once the timer expires.
esp_restart() now calls esp_wifi_stop() which tries to start its own timer (via esp_timer) that may be needed before wifi stop can complete, but as the first esp_timer callback is still running the WiFi stop timer callback will never run. (Most likely the reason that the WiFi stop timer callback is needed is also related to esp_timer being blocked, as Wi-Fi uses esp_timer for a number of purposes.)

esp_timer callbacks are not quite as sensitive as ISRs, but they should be treated as almost as sensitive - ie to avoid any complex processing happening from inside them. I think the documentation doesn't stress this enough.

The solution, as per previous posts, is to either trigger the shutdown to happen from another lower priority task which doesn't block esp_timer, or to use a FreeRTOS timer (which runs in a lower priority timer task).

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: esp_restart() broken in 3.1.3

Postby Ritesh » Wed Mar 06, 2019 8:29 am

ESP_Angus wrote:
Wed Mar 06, 2019 6:21 am
Ritesh wrote:
Wed Mar 06, 2019 4:32 am
But, he has checked issue into TAG 3.1.1. in which same functionality is working fine without any issue.
Yes. The most likely root cause is the following (from the 3.1.2 release notes):
  • Add a timer which is started by esp_wifi_stop(). Stop Wi-Fi forcibly once the timer expires.
esp_restart() now calls esp_wifi_stop() which tries to start its own timer (via esp_timer) that may be needed before wifi stop can complete, but as the first esp_timer callback is still running the WiFi stop timer callback will never run. (Most likely the reason that the WiFi stop timer callback is needed is also related to esp_timer being blocked, as Wi-Fi uses esp_timer for a number of purposes.)

esp_timer callbacks are not quite as sensitive as ISRs, but they should be treated as almost as sensitive - ie to avoid any complex processing happening from inside them. I think the documentation doesn't stress this enough.

The solution, as per previous posts, is to either trigger the shutdown to happen from another lower priority task which doesn't block esp_timer, or to use a FreeRTOS timer (which runs in a lower priority timer task).
Thanks for providing updates regarding same. We will also take care while using that IDF version as esp_restart API is necessary or required call for any application.
Regards,
Ritesh Prajapati

Who is online

Users browsing this forum: No registered users and 121 guests