portENTER_CRITICAL explaination

gibson12345
Posts: 54
Joined: Wed Jul 17, 2019 11:45 pm

portENTER_CRITICAL explaination

Postby gibson12345 » Tue Oct 08, 2019 2:03 am

Hey everyone,

trying to sort out a situation where I need to enter a critical section and trying to determine how the functionality within freeRTOS works. portENTER_CRITICAL is the function I get pointed to which points to vTaskEnterCritical. Both require a "mux" but I'm struggling to understand exactly what a "mux" is. Could someone offer some insight or explanation?

Cheers,
Gibson

ESP_Dazz
Posts: 143
Joined: Fri Jun 02, 2017 6:50 am

Re: portENTER_CRITICAL explaination

Postby ESP_Dazz » Tue Oct 08, 2019 6:33 am

The mux used in portENTER_CRITICAL is actually a spinlock. A spinlock is required in order to prevent multiple CPUs from accessing the same resource concurrently (e.g. a variable, struct, or set of registers).

You can use a mutex spinlock as follows:

Code: Select all

#include <stdio.h>
static portMUX_TYPE my_mutex;
int my_resource = 0;

void app_main()
{
	vPortCPUInitializeMutex(&my_mutex);
	portENTER_CRITICAL(&my_mutex);
	//Access your resource here.
	my_resource++;
	portEXIT_CRITICAL(&my_mutex);
}
You can also initialize the mutex as such:

Code: Select all

static portMUX_TYPE my_mutex = portMUX_INITIALIZER_UNLOCKED;

Who is online

Users browsing this forum: Baidu [Spider] and 18 guests