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.
2023-06-20 02:51 AM
Hello, do you have any solution for your problem?
I have exactly the same problem, the first tx_thread_sleep needs about 350ms to execute regardless of the sleep time.
Regards, Alex
2023-06-20 06:18 AM
Hello. Probably I have a solution for this problem. The reason for this behavior is incorrect initialization of the SysTick timer in the port file for the Cortex-M0. It doesn't reset the SysTick Current Value Register despite the fact that its value is not initialized at startup. So if we have 0xFFFFFF (this register has 24-bit), it means we will get about 256*256*256 / 48000000 for the tact frequency of 48MHz (my MCU) to reach the zero, it makes exactly 350ms. So my assumption seems reasonable enough.
I have fixed this problem by adding these lines to tx_initialize_low_level.S directly after LDR r0, =0xE000E000:
LDR r1, =0
STR r1, [r0, #0x10] @ Reset SysTick Control
STR r1, [r0, #0x18] @ Reset SysTick Counter Value
Now it works without any issues.