Loading...
 

Zephyr project on STM32

   Zephyr Workbench, a VSCode extension to manage Zephyr on STM32.
It enables users to easily create, develop, and debug Zephyr applications.
Main features:
  • Install host dependencies.
  • Import toolchain and SDK.
  • Create, configure, build and manage apps.
  • Debug STM32.
You can directly download it from the VSCode marketplace
For more details, visit the Zephyr Workbench

System Workbench for STM32


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!

StartDefaultTask toggles pin without any delay? Also you mix “dummy” delays with osDelay - why?

It’s just for testing the functioning of the priorities, to check that when the highest priority task is in the for loop, the StartDefaultTask is stopped. I know is not a good implementation, but it should work anyway, and it’s not working as I expected.

Optimization of compiler nulls out useless code, even if it’s intended as dummy delay. Take this into account. Change SW4STM32 Eclipse compiler optimization and see what happens. You may also choose to declare variables with volatile as prefix. Welcome to the weird world of 21st century coding and compiling where the simpliest proof of concept code will already cause trouble. It used to work in the 1980s/1990s but today everything has changed ;)

 

Newest Forum Posts

  1. reservation car service Seattle by Jamesprede, 2025-05-01 10:06
  2. Last day: drone bonus by Danielrug, 2025-04-19 16:55
  3. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-25 07:37
  4. SPI on Nucleo_STMH533RE by royjamil, 2025-03-23 11:31
  5. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-23 09:33
  6. Configuring DMA for ADC in SW? by sam.hodgson, 2025-03-04 12:58
  7. Insightful Perspectives on This Subject by davidsycle, 2025-03-04 05:45
  8. Build a project in "release" mode by info@creosrl.it, 2025-02-20 18:12
  9. Build a project in "release" mode by info@creosrl.it, 2025-02-20 17:05
  10. Build a project in "release" mode by tang, 2025-02-20 10:36

Last-Modified Blogs