Hi community,
I've searched in various websites to find the answer for the following questions, without finding it: does a callback need volatile variables (such as interrupts) when executed?
I know callbacks are called from events. Events could be triggered from interrupts. Since an interrupt is an asynchronuous event, also the following callback called by the interrupt should be an asynchronuous event.
If I have a global variable shared between my app_main() and a callback, I think it would be correct declare it as volatile.
However I noticed, inside the ESP-IDF examples, there aren't volatile variables in callbacks declared as volatile. I'm not referring to a specific example, I looked at various of them and there is the same situation.
Callbacks and volatile variables
Callbacks and volatile variables
Filippo
-
- Posts: 9051
- Joined: Thu Nov 26, 2015 4:08 am
Re: Callbacks and volatile variables
Even in interrupts, 'volatile' does not do what you think it does. The Linux kernel has a good bit of text about it. (Note that it mentions 'cpu_relax' as a compiler barrier that makes the C compiler assume the variable may have changed; this is true for all function calls, there's nothing special about the 'cpu_relax' function call in that respect. You can simply replace it with a vTaskDelay or vTaskYIELD or any other function call in ESP-IDF.)
Re: Callbacks and volatile variables
I read the article. Very useful because typical C programmers (as I am) may don't know how RTOS' kernel works.ESP_Sprite wrote: ↑Sat Oct 23, 2021 1:53 amEven in interrupts, 'volatile' does not do what you think it does. The Linux kernel has a good bit of text about it.
So if I hypothetically have a callback related to the usb where I only change the status of a bool variable, such as this:
Code: Select all
void
usb_cdc_rx_callback (int itf, cdcacm_event_t * event)
{
g_char_is_arrived = true;
return;
}
Code: Select all
if (g_char_is_arrived)
{
g_char_is_arrived = false;
(void) gpio_set_level(PIN_LED, 1);
}
Code: Select all
bool g_char_is_arrived;
I hope I've understood in the right way
Filippo
Who is online
Users browsing this forum: Baidu [Spider] and 241 guests