skyr
(sky)
March 1, 2022, 3:50pm
1
I am trying to understand mutex practically with ESP32. How to test mutex for freertos
void setup()
{
Serial.begin(112500);
/* Create two tasks. */
xTaskCreate( vTask1, "Task 1", 10000, NULL, 1, NULL);
xTaskCreate( vTask2, "Task 2", 10000, NULL, 1, NULL);
}
void loop() {
// Do nothing
}
void vTask1( void *pvParameters )
{
/* As per most tasks, this task is implemented in an infinite loop. */
for(;;)
{
Serial.println("Task 1 is running");
}
}
void vTask2( void *pvParameters )
{
for(;;)
{
Serial.println("Task 2 is running");
}
}
rtel
(Richard Barry)
March 1, 2022, 3:56pm
2
Sorry but your question is not clear as the code you show doesn’t use a mutex. Do you want to protect the calls to Serial.println() using a mutex? Here are some references to start with:
About mutexes: https://freertos.org/Real-time-embedded-RTOS-mutexes.html
Mutex API, including example code: https://freertos.org/a00113.html
skyr
(sky)
March 4, 2022, 6:54pm
3
As I understand if there is single resource available and more than two Tasks need to access resource, then mutex need to be used
What is resource in this situations ?
Is the I2C, SPI UART called resource in this situation?
If not then what are the resources that we lock and unlock
aggarg
(Gaurav Aggarwal)
March 4, 2022, 7:50pm
4
Resource is a general term used for anything that must not be accessed by two tasks at the same time. This can be peripherals like I2C, SPI or UART or can just be a variable.
rtel
(Richard Barry)
March 5, 2022, 1:31am
5
Mutexes are not always the best mechanism to protect any resource that needs to be shared - it depends how long accessing the resource is going to take. Also, if you are just accessing a variable, you may not need to protect the variable at all if the tasks are just reading it.
skyr
(sky)
March 5, 2022, 1:37am
6
Do you always use a mutex whenever two tasks share these peripherals?
It will be easier to understand if you can give real situation in which mutex is used.
aggarg
(Gaurav Aggarwal)
March 5, 2022, 11:49pm
7