Problem with toggle GPIOs in 2 tasks
Hello everyone,
I’m trying to work with different tasks to understand how the priorities works in the STM32F3 and I have 2 different tasks. The first one toggle a GPIO continously, and the second one toggle other GPIO and has higher priority. The second has a loop to see that, when the task is in this loop, the first one does not work.
The problem I’m having is that the toggling is not working fine. When I use HAL_GPIO_TogglePin, the GPIO goes up, waits in the loop, and when the loop is finished and the task goes to os Delay(), the GPIO goes down again instead waiting for the next call of the task. The code is shown below:
This is the declaration of the tasks.
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* Create the thread(s) */
/* definition and creation of defaultTask */
osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
/* definition and creation of newTask */
osThreadDef(newTask, SecondTask, osPriorityHigh, 0, 128);
newTaskHandle = osThreadCreate(osThread(newTask), NULL);
/* Start scheduler */
osKernelStart(NULL, NULL);
/* We should never get here as control is now taken by the scheduler */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Then the methods for each task. StartDefaultTask is the task with the lower priority.
/* StartDefaultTask function */
void StartDefaultTask(void const * argument)
{
/* USER CODE BEGIN 5 */
/* Infinite loop */
for(;;)
{
HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_0);
}
/* USER CODE END 5 */
}
int cont_delay = 0;
void SecondTask(void const * argument)
{
/* USER CODE BEGIN SecondTask */
/* Infinite loop */
for(;;)
{
HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_3);
for (int i = 0; i < 100000; i++){
contador_led++;
}
cont_delay++;
osDelay(200);
}
/* USER CODE END SecondTask */
}
In the image attached to the post is a capture of the osciloscope signal. Each square is 100 ms and it can be seen that the upper signal, which is the highest priority task, raises the value of the GPIO, waits in the loop for about 40 ms (in this time it can be seen in the lower signal that the first task is waiting for the other to end), and after that the task go to sleep, but the value of the output goes to 0 again.
I don’t understand why this output goes down without any function changing it. I thought that maybe there was a problem in the implementation of the TogglePin function, but I checked the library and it seems to work fine.
Maybe someone had the same problem and know what could be the problem.
Thanks in advance!