portENTER_CRITICAL explaination

Posts: 58
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?


Posts: 187
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()
	//Access your resource here.
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: No registered users and 23 guests