Issue with FreeRTOS, printf and vTaskSuspend

jmcornil
Posts: 21
Joined: Mon Feb 10, 2020 1:42 pm

Issue with FreeRTOS, printf and vTaskSuspend

Postby jmcornil » Thu Jul 02, 2020 5:03 pm

Hello everybody

I am getting started with FreeRTOS and for my firs try (following the pdf "Mastering FreeRTOS") I made a task which displays a string on the monitor. Since I did not found the vPrintString() int he ESP32 port, I wrote :

Code: Select all

void vPrintString( const char *pcString ){
	vTaskSuspendAll();  // (1)
		printf( "%s", pcString );
		fflush( stdout );
	}
	xTaskResumeAll();    // (2)
}
But with these lines, the ESP32 was always rebooting .... Then I suppressed lines (1) and (2) and all worked fine.

By reading the pdf "mastering FreeRTOS" I think that it is not good to suppress these lines, and I am wondering
  • if I can use such a function vPrintString without vTaskSuspendAl()
  • or if I must use another function different from printf to output messages on the monitor.
Thanks in advance for any advice

ESP_ulipe
Posts: 7
Joined: Fri Mar 27, 2020 8:13 pm
Location: Campinas, SP - Brazil

Re: Issue with FreeRTOS, printf and vTaskSuspend

Postby ESP_ulipe » Thu Jul 02, 2020 6:39 pm

Hi jmcornil, you can use the printf without suspending the scheduler, that is it, you can remove safely the vTaskSuspendAll and xTaskResumeAll from your code. The current ESP32s printf function is reentrant, and its locking mechanism is implemented on top of FreeRTOS mutexes, so when you call the scheduler to be suspended an assert is caused because mutexes cannot be called if the scheduler is suspended.

This assert by default causes an abort resulting in chip reset.

Please let me know if this information was helpful to you.

Best.

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

Re: Issue with FreeRTOS, printf and vTaskSuspend

Postby ESP_Sprite » Fri Jul 03, 2020 6:56 am

Additionally, most APIs in esp-idf are re-entrant; you should not have to use vTaskSuspendAll at all. (And if you think you need to use it anyway, use mutexes instead - the ESP32 has a fair amount of things running in the background, and stopping them all with vTaskSuspendAll is like using a cannon on a mosquito.)

jmcornil
Posts: 21
Joined: Mon Feb 10, 2020 1:42 pm

Re: Issue with FreeRTOS, printf and vTaskSuspend

Postby jmcornil » Fri Jul 03, 2020 7:40 am

Tank you very much ESP_ulipe ans ESP_Sprite for yours replies and specially for the image of the cannon/mosquito !

I realize now that the FreeRtos port on the ESP32 is quite different from what is explained in the "Mastering FreeRTOS" paper.

Can one of them send me of reference where I can find information on FreeRTOS such as these you gave about re-entrant API and so on ... I know the ESP-IDF Programming Guide but I find it is far from a tutorial !

Best regards

jmcornil
Posts: 21
Joined: Mon Feb 10, 2020 1:42 pm

Re: Issue with FreeRTOS, printf and vTaskSuspend

Postby jmcornil » Mon Jul 06, 2020 2:44 pm

Hello

After printf, I have a similar problem with scanf ()or gets() : none of them waits and let me key in what I want, the program skip to the next instruction.

I have read I should use the esp_vfs_dev_uart_use_driver() function but, if I do so, my ESP32 reboots when it reaches the scanf() or the gets() function.

Any clue ? Thanks in advance

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

Re: Issue with FreeRTOS, printf and vTaskSuspend

Postby ESP_Sprite » Tue Jul 07, 2020 8:10 am

...that's not related at all, suggest you start a new topic for that.

Who is online

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