Critical Code section

kilobyte_ch
Posts: 12
Joined: Wed Apr 19, 2017 2:17 pm

Critical Code section

Postby kilobyte_ch » Wed Apr 19, 2017 2:20 pm

Hello

I need a critical code section inside my esp-idf project. Cricital means that during a short time the timing should be 100% reproducable. I'm using nop's to time it. But I see big variations when WiFi is turned on or other tasks are running. No surprise. But I'm asking how can I disable "everything" to get this short section reproducable and then turn everything on again?

Thanks

f.h-f.s.
Posts: 214
Joined: Thu Dec 08, 2016 2:53 pm

Re: Critical Code section

Postby f.h-f.s. » Wed Apr 19, 2017 2:53 pm

Code: Select all

portMUX_TYPE myMutex = portMUX_INITIALIZER_UNLOCKED;
taskENTER_CRITICAL(&myMutex);
//critical section
taskEXIT_CRITICAL(&myMutex);
from FreeRTOS/task.h & FreeRTOS/portmacro.h
or

Code: Select all

uint32_t volatile register ilevel = XTOS_DISABLE_ALL_INTERRUPTS;
//critical section
XTOS_RESTORE_INTLEVEL(ilevel);
from xtruntime.h

You can read the cpu ticks with

Code: Select all

uint32_t r;
asm volatile("rsr %0, ccount" : "=r"(r));//read special register cpu tick count

User avatar
urbanze
Posts: 295
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: Critical Code section

Postby urbanze » Mon Jul 09, 2018 11:14 am

f.h-f.s. wrote:

Code: Select all

uint32_t volatile register ilevel = XTOS_DISABLE_ALL_INTERRUPTS;
//critical section
XTOS_RESTORE_INTLEVEL(ilevel);
from xtruntime.h
This critical section disable mutual cpu acess? I need to prevent both core to acess same "line in code" with more fast way possible... (Without method of freertos like semphr)

michprev
Posts: 92
Joined: Fri Aug 04, 2017 8:57 pm

Re: Critical Code section

Postby michprev » Mon Jul 09, 2018 12:42 pm

urbanze wrote:
f.h-f.s. wrote:

Code: Select all

uint32_t volatile register ilevel = XTOS_DISABLE_ALL_INTERRUPTS;
//critical section
XTOS_RESTORE_INTLEVEL(ilevel);
from xtruntime.h
This critical section disable mutual cpu acess? I need to prevent both core to acess same "line in code" with more fast way possible... (Without method of freertos like semphr)
This one only disables C callable interrupts (level 3 or lower) on the current core.

You will have to go for
f.h-f.s. wrote:

Code: Select all

portMUX_TYPE myMutex = portMUX_INITIALIZER_UNLOCKED;
taskENTER_CRITICAL(&myMutex);
//critical section
taskEXIT_CRITICAL(&myMutex);
taskENTER_CRITICAL disables C callable interrupts (on the current core) and locks the mutex by the current core.

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

Re: Critical Code section

Postby ESP_Sprite » Tue Jul 10, 2018 2:19 am

Note that it's very hard to guarantee 100% precise CPU timings: you always run the risk of the other CPU, a DMA access, a lingering value in the CPU load/store unit or something else delaying a memory fetch and delaying your code. What issue exactly are you trying to solve? Chances are very large that there's a better solution than doing NOP delaying.

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 98 guests