2022-12-14 07:00 AM
I've tried to create a simple project which includes 2 threads. Each thread toggles two separate pins respectively . To create a 10 ms delay, I am using tx_thread_sleep (TX_TIMER_TICKS_PER_SECOND is 100) . Everything works okay, however, I've noticed that it takes too long for a pin to be toggled the first time after reset (~1 s, see the example below). Then it works properly. If I delete tx_thread_sleep function and change it with HAL_delay, then it behaves after RESET as expected. What can cause this issue?
Here is my code for App_ThreadX_Init:
UINT App_ThreadX_Init(VOID *memory_ptr)
{
UINT ret = TX_SUCCESS;
TX_BYTE_POOL *byte_pool = (TX_BYTE_POOL*)memory_ptr;
/* USER CODE BEGIN App_ThreadX_MEM_POOL */
(void)byte_pool;
/* USER CODE END App_ThreadX_MEM_POOL */
/* USER CODE BEGIN App_ThreadX_Init */
tx_thread_create(&thread_1_ptr, "thread_1", thread_1_entry, 0, thread_1_stack, THREAD_STACK_SIZE, 15, 15, 1, TX_AUTO_START);
tx_thread_create(&thread_2_ptr, "thread_2", thread_2_entry, 0, thread_2_stack, THREAD_STACK_SIZE, 15, 15, 1, TX_AUTO_START);
/* USER CODE END App_ThreadX_Init */
return ret;
}
And my two threads functions:
void thread_1_entry(ULONG initial_input)
{
while(1){
HAL_GPIO_TogglePin(PA8_LED_GPIO_Port, PA8_LED_Pin);
tx_thread_sleep(1);
}
}
void thread_2_entry(ULONG initial_input)
{
while(1){
HAL_GPIO_TogglePin(PA9_LED_GPIO_Port, PA9_LED_Pin);
tx_thread_sleep(1);
}
}
Any help will be appreciated. Thanks!
2022-12-14 11:33 AM
Is the initial configuration of those two pins high (see MX_GPIO_Init()). If so, it looks like it takes 1 second from reset to when your tasks start toggling the pins. So what else is your program doing on startup? Waiting for a slow oscillator to start (32KHz crystal)?
2022-12-15 01:56 AM
Hello, the initial configuration is low. So it seems that the thread starts to execute (toggles the pin from low to high after RESET), proceeds to tx_thread_sleep and then the delay for 1 s is observed. When the thread starts to execute over again, then there is no problem with tx_thread_sleep. I've also tried debugging the code, the return value of tx_thread_sleep is always TX_SUCCESS.