cancel
Showing results for 
Search instead for 
Did you mean: 

Blink led with 2 multithread, sinch problems

andreaspiezia9
Associate III

Hi all,

I'm starting a new app with two threads.

If I run as debug step by step it works great.

But if I run free on the board, the second thread not works good (it doesn't blink the required times).

I've use CubeMX for the code generation with Time Base TIM1 as indicated below

0690X000008AUj4QAG.png

What is the problem?

Thanks

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "cmsis_os.h"
 
 
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;
 
 
osThreadId LED_Thread1Handle;
osThreadId LED_Thread2Handle;
 
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
void LED_Thread1(void const * argument);
void LED_Thread2(void const * argument);
 
int main(void)
{
 
  HAL_Init();
 
  SystemClock_Config();
 
  MX_GPIO_Init();
  MX_USART2_UART_Init();
 
  osThreadDef(nameLED_Thread1, LED_Thread1, osPriorityNormal, 0, 128);
  LED_Thread1Handle = osThreadCreate(osThread(nameLED_Thread1), NULL);
 
  osThreadDef(nameLED_Thread2, LED_Thread2, osPriorityNormal, 0, 128);
   LED_Thread2Handle = osThreadCreate(osThread(nameLED_Thread2), NULL);
 
  osKernelStart();
 
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
 
}
 
 
void LED_Thread1(void const * argument)
{
 
	while(1){
		for(int i = 1; i <= 5; i++) { //
 
			HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
			HAL_Delay(1000);
			HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
			HAL_Delay(1000);
 
		}
		osDelay(10000);
		HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
		osSignalSet(LED_Thread2Handle, 0x0001);    // set the signal 0x0001 for thread tid_thread2
 
		osSignalWait (0x0001, osWaitForever); // wait forever for the signal 0x0001
 
	}
 
}
 
 
void LED_Thread2(void const * argument)
{
 
	while(1){
		for(int j = 1; j <= 5; j++) {
			HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
			HAL_Delay(4000);
			HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
			HAL_Delay(4000);
		}
		osDelay (15000);
		HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
		osSignalSet (LED_Thread1Handle, 0x0001);    // set the signal 0x0001 for thread tid_thread1
 
		osSignalWait (0x0001, osWaitForever); // wait forever for the signal 0x0001
	}
}
 

1 ACCEPTED SOLUTION

Accepted Solutions
Piranha
Chief II

First, You should not use default HAL_Delay() within RTOS threads because it is busy loop.

Second, when kernel is started, both threads are blinking the same LED. Better put osSignalWait() at the beginning of while loops and send initial signal to one of the threads before starting kernel.

View solution in original post

2 REPLIES 2
Piranha
Chief II

First, You should not use default HAL_Delay() within RTOS threads because it is busy loop.

Second, when kernel is started, both threads are blinking the same LED. Better put osSignalWait() at the beginning of while loops and send initial signal to one of the threads before starting kernel.

Great! Thanks a lot Piranha