HELP! How to stop FreeRTOS (Or How to stop WiFi during esp_http_client_write)

User avatar
gunar.kroeger
Posts: 143
Joined: Fri Jul 27, 2018 6:48 pm

HELP! How to stop FreeRTOS (Or How to stop WiFi during esp_http_client_write)

Postby gunar.kroeger » Thu Nov 22, 2018 7:18 pm

How do I stop execution of all tasks?

I am able to detect that the supercapacitor voltage is dropping and save everything and deinit all modules before the voltage drops too low for the esp to work. After I detect the voltage drop I need to stop execution of all other tasks.

I know that the function I need is vTaskEndScheduler or vTaskSuspendAll, except that they don't work with the IDF implementation of freertos and calling them throws a Guru Meditation Error: Core 0 panic'ed (Interrupt wdt timeout on CPU0)

I saw somewhere in the docs that it is recommended using semaphores instead of creating critical sections with the above sections.

Until now I saved the handle for every task I created and deleted them with vTaskDelete before deiniting every module
But now I have http POST happening, and if I call esp_wifi_disconnect during an esp_http_client_write I also get a crash:

Code: Select all

I (12232) BAT: Disconnect wifi
I (12242) wifi: state: run -> init (0)
I (12242) wifi: pm stop, total sleep time: 4207612 us / 7481433 us

I (12252) wifi: n:11 0, o:11 0, ap:255 255, sta:11 0, prof:1
I (12252) BAT: C:/msys32/home/gunar.kroeger/esp/esp-idf/components/freertos/queue.c:719 (xQueueGenericSend)- assert failed!
abort() was called at PC 0x4009431c on core 1
I imagine this is because wifi creates underlying tasks that are not interrupted properly before calling esp_wifi_disconnect.
I need to stop wifi as fast as possible so that I don't drain all the energy from the supercap.

So, how do I fully stop the FreeRTOS scheduler or how do I stop the underlying task that is trying to send data to the server?

Thanks
Last edited by gunar.kroeger on Fri Nov 23, 2018 6:07 pm, edited 1 time in total.
"Running was invented in 1612 by Thomas Running when he tried to walk twice at the same time."

User avatar
gunar.kroeger
Posts: 143
Joined: Fri Jul 27, 2018 6:48 pm

Re: HELP! How to stop FreeRTOS

Postby gunar.kroeger » Thu Nov 22, 2018 7:26 pm

Increasing the priority of the BAT task I got the following:

Code: Select all

I (12232) BAT: Disconnect wifi
I (12242) wifi: state: run -> init (0)
I (12242) wifi: pm stop, total sleep time: 3612453 us / 7502217 us

I (12252) wifi: n:11 0, o:11 0, ap:255 255, sta:11 0, prof:1
I (12252) BAT: Stop wifi
assertion "res == coreID || res == portMUX_FREE_VAL" failed: file "C:/msys32/home/gunar.kroeger/esp/esp-idf/components/freertos/portmux_impl.inc.h", line 105, function: vPortCPUAcquireMutexIntsDisabledInternal
abort() was called at PC 0x400e176b on core 1

"Running was invented in 1612 by Thomas Running when he tried to walk twice at the same time."

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: HELP! How to stop FreeRTOS

Postby chegewara » Thu Nov 22, 2018 8:01 pm

Maybe its not what you are looking for but did you try

Code: Select all

esp_deep_sleep()
it should enter deep sleep immediately which will save your power usage and should protect you from guru or assertion failure.

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

Re: HELP! How to stop FreeRTOS

Postby ESP_Angus » Fri Nov 23, 2018 3:25 am

Hi gunar,

A clean disconnect and stop should be possible, but you will need to post a few more technical details to try and figure out why it's not working for you.

Firstly, what threshold voltage are you triggering this shutdown process at? Is it possible the ESP32 may drop below its minimum operating voltage?

If you're using IDF monitor, can you please post the full stack trace from the abort() logs posted above. Also, please post some code samples of how you're calling esp_wifi_disconnect() and what other API calls you are making at the same time.

It should be safe to call esp_wifi_disconnect() from any task (although not from an ISR), however you'll have to make sure all of your other code is prepared for sudden network disconnection and isn't going to corrupt memory or cause other problems if all network sockets are suddenly closed. If some part of your program corrupts memory then you can get crashes more or less anywhere.

By themselves, open TCP sockets shouldn't be a problem that prevent you from disconnecting from WiFi. The socket will just close and socket API calls will fail.

If you disconnect with esp_wifi_disconnect() and then stop wifi entirely with esp_wifi_stop() then the current draw of the ESP32 will go down significantly, which will probably temporarily increase the supercap voltage by a small amount (due to the capacitor's ESR). This will give you a little more time in which to save remaining data and go to deep sleep (or whatever final step you have planned). I would suggest trying this, and adding cleanup/shutdown logic to your existing tasks, rather than trying to force-stop the entire FreeRTOS scheduler (which will do things like disturb the WiFi shutdown process).

User avatar
gunar.kroeger
Posts: 143
Joined: Fri Jul 27, 2018 6:48 pm

Re: HELP! How to stop FreeRTOS

Postby gunar.kroeger » Fri Nov 23, 2018 3:40 pm

Hi,
Is it possible the ESP32 may drop below its minimum operating voltage?
No. we are monitoring the voltage before the power supply and shutting down if it drops bellow 8V, giving about 2 seconds to close all files, etc. while the 3.3 caps are still above 2 volts.
can you please post the full stack trace from the abort() logs posted above
If the VBAT task I get this log:

Code: Select all

I (194032) WIFI: 24576/508621 bytes sent 12.00 KiB/s
I (194042) WIFI: 25600/508621 bytes sent 12.50 KiB/s
I (194102) BAT: VADC = 7.31
I (194202) BAT: VBat shutdown
I (194202) BAT: currentTask is VBat
I (194202) BAT: waiting for critical tasks to finish
I (194202) BAT: delete CAN_Rx task
I (194202) BAT: delete CAN_Tx task
I (194202) BAT: delete xtaskBatMonitor task
I (194202) BAT: delete xtaskGPS task
I (194212) BAT: delete xhttp_task task
I (194212) BAT: delete xtaskHeartbeat task
I (194222) BAT: delete xtaskMiniz task
I (194222) BAT: delete xtaskSPP_RX task
I (194222) BAT: Deinit bt
I (194222) BAT: Disconnect wifi
I (194232) wifi: state: run -> init (0)
I (194232) wifi: pm stop, total sleep time: 101354080 us / 186592006 us

I (194242) wifi: n:11 0, o:11 0, ap:255 255, sta:11 0, prof:1
I (194242) BAT: Stop wifi
C:/msys32/home/gunar.kroeger/esp/esp-idf/components/freertos/queue.c:719 (xQueueGenericSend)- assert failed!
abort() was called at PC 0x4009431c on core 1

Backtrace: 0x40095bcb:0x3ffeacf0 0x40095ea1:0x3ffead10 0x4009431c:0x3ffead30 0x4019fd96:0x3ffead70 0x401a3dcd:0x3ffead90 0x401a5c5b:0x3ffeadc0 0x40198555:0x3ffeade0 0x40198565:0x3ffeae10 0x40198630:0x3ffeae30 0x40198b94:0x3ffeae50 0x40195729:0x3ffeae70 0x4019576f:0x3ffeaeb0 0x401007e0:0x3ffeaed0 0x40100801:0x3ffeaf10 0x40100233:0x3ffeaf30 0x4019484f:0x3ffeaf50

Rebooting...
this only occurs if the shutdown happens while xhttp_task is running this loop:
  1. while(true)
  2.     {
  3.         int length = fread(buffer, sizeof(char), HTTP_BUFFER_SIZE, file);
  4.         if(length)
  5.         {
  6.             int wret = esp_http_client_write(client, buffer, length);
  7.             if(wret < 0)
  8.             {
  9.                 esp_http_client_close(client);
  10.                 SD_close(file);
  11.                 return false;
  12.             }
  13.  
  14.             fileProgress += wret;
  15.             //ESP_LOGI(TAG, "%.*s", length, buffer);
  16.             time_t now;
  17.             time(&now);
  18.             if((float)(now - fileTransferStart) / 1024 > 0)
  19.                 ESP_LOGI(TAG, "%u/%u bytes sent %.02f KiB/s", fileProgress, fileLength, fileProgress  / (float)(now - fileTransferStart) / 1024);
  20.             else
  21.                 ESP_LOGI(TAG, "%u/%u bytes sent", fileProgress, fileLength);
  22.         }
  23.         else
  24.             break;
  25.     }
I'm running on commit [a8754cc] of IDF
Thanks for the help.
"Running was invented in 1612 by Thomas Running when he tried to walk twice at the same time."

User avatar
gunar.kroeger
Posts: 143
Joined: Fri Jul 27, 2018 6:48 pm

Re: HELP! How to stop FreeRTOS

Postby gunar.kroeger » Fri Nov 23, 2018 3:59 pm

ESP_Angus wrote: I would suggest trying this, and adding cleanup/shutdown logic to your existing tasks, rather than trying to force-stop the entire FreeRTOS scheduler (which will do things like disturb the WiFi shutdown process).
I understand that this would be a more elegant solution, but I fail to see how this should be done. How can I implement this logic in a way that I could interrupt blocking methods like esp_http_client_write() or fread() to cleanup my tasks? could you point me to the right way with an example or link?

Thanks
"Running was invented in 1612 by Thomas Running when he tried to walk twice at the same time."

User avatar
gunar.kroeger
Posts: 143
Joined: Fri Jul 27, 2018 6:48 pm

Re: HELP! How to stop FreeRTOS

Postby gunar.kroeger » Fri Nov 23, 2018 6:10 pm

chegewara wrote:
Thu Nov 22, 2018 8:01 pm
Maybe its not what you are looking for but did you try

Code: Select all

esp_deep_sleep()
it should enter deep sleep immediately which will save your power usage and should protect you from guru or assertion failure.
I believe this wouldn't work because I have to close all files and deinit everything afterwards. Also I have to restart once power comes back on
"Running was invented in 1612 by Thomas Running when he tried to walk twice at the same time."

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: HELP! How to stop FreeRTOS

Postby chegewara » Fri Nov 23, 2018 9:21 pm

gunar.kroeger wrote:
Fri Nov 23, 2018 6:10 pm
I believe this wouldn't work because I have to close all files and deinit everything afterwards. Also I have to restart once power comes back on
Its just idea, you know better what you need in app.

How about this:
- close all files if you need,
- go to deep sleep for some time, to preserve power and let to recharge,
- wake up and if power level is still low go back deep sleep for some time.
In this case you dont have to worry about wifi and pending http requests, because after deep sleep you have clean boot up.

Who is online

Users browsing this forum: Google [Bot] and 134 guests