Tasks work strangely in FREERTOS
Started learning FREERTOS and ran into a problem.
I showed 3 pieces of code of the same tasks.
All 3 pieces of code work differently.
Code 1
void Task1(void *argument)
{
int j = 0;
for(;;)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
for(j=0; j<10000000; j++);
}
}
void Task2(void *argument)
{
int i = 0;
for(;;)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14);
for(i=0; i<10000000; i++);
}
}Why does the blinking order of the LEDs start to change over time?
For example: first the PB0 LED lights up first, then the PB14. After a few periods, the PB14 LED starts to light up first.
Code 2
void Task1(void *argument)
{
for(;;)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
for(int j=0; j<10000000; j++);
}
}
void Task2(void *argument)
{
for(;;)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14);
for(int i=0; i<10000000; i++);
}
}Then the blinking of the LEDs is strange.
Example: Sometimes the PB0 LED may turn on first, then turn off first.
Code 3. I made the variables j and i global.
void Task1(void *argument)
{
for(;;)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
for(int j=0; j<10000000; j++);
}
}
void Task2(void *argument)
{
for(;;)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14);
for(int i=0; i<10000000; i++);
}
}The work seems to be normal.
The two LEDs flash simultaneously.
Code optimization is disabled.
Why is this happening?
