Understanding task preemption and context switch

sasikalaswathi wrote on Friday, September 20, 2019:

I have nucleo-h743zi board.I wanted to understand context switch and measure the time for it. I created 10 task each of priority 1 and set 10 GPIO pins as outputs. Inside each task I make a GPIO pin high then set the same tasks priority as 1 and then next tasks priority as 2. In the next task I disable that pin (set high in the previous task) and high another pin follwed by set that tasks priority as 1 and set the next task priority as 2. I’ve done the same for each of the ten tasks. Then I used oscilloscope to see the falling and the rising edges for the two pins in their two respective tasks.

I am now able to understand the context switch time but when I place breakpoints in my code, after setting the same task priority as 1 it goes to the next statement where I set the next task priority as 2 and then it AGAIN goes to the previous statement where I set the same task priority as 1 and then It goes the next task (who’s priority is now 2).
I expected it to directly go the next task once its priority was set as 2 but it goes to the prevous statement and then to the next task.
Not able to understand this behaviour.

Note: I’ve enabled all correct free rtos settings in the .h file(enable preemption etc). My time slice is 5Hz, I dont think that would be issue.

richard_damon wrote on Friday, September 20, 2019:

Debuggers tend not to follow interrupts being executed when run in the ‘Statement’ mode, and using the ‘Step’ instruction, This happens becuase the ‘Step’ instruction basically sets a breakpoint on the beginning of the next statement that it is expecting, and thus it doesn’t catch the timer interrupt, switching to the next task and that task running.

rtel wrote on Friday, September 20, 2019:

It would be much simpler for you to read the CPU cycle count, call
taskYIELD(), then read the CPU cycle count again - then discount the
time taken to read the cycle count in the same task.

The thing that takes the longest in the code that switches from one task
to another is checking for stack overflow - which is not actually
anything to do with context switching - and can be turned off.