on
2021-09-16
08:20 AM
- edited on
2024-01-03
09:04 AM
by
Laurids_PETERSE
Is it safe to allocate the stack like this? I see in the examples it's usually allocated with tx_byte_allocate
Hi @OHaza.1
Thank you for your question, @B.Montanari I am sure you can provide some insights.
Hi @OHaza.1 ,
Yes, it is safe to allocate the stack as shown in this example. When calling the tx_thread_create(), among its passing arguments, it expects to receive the starting address of the stack's memory area and the number bytes in the stack memory area. The thread's stack area must be large enough to handle its worst-case function call nesting and local variable usage. The way it is displayed in this article, those arguments are used thru a global vector, so both conditions are properly met and I think it is easier overall to use and edit as needed (but this is just my personal preference). Of course, it is also fine to do it by calling the tx_byte_allocate and then tx_thread_create using the pointer for the stack. The code generated by the STM32CubeIDE will create a pool using the settings showed at step10 - this is done inside the app_azure_rtos.c file in the tx_application_define() . Inside it or in the App_ThreadX_Init() you can add your tx_byte_allocate() call to make use of it.
In a nutshell, these would be the code pieces:
#define DEMO_STACK_SIZE 512
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 */
CHAR *pointer;
/* Allocate the stack for thread 0. */
tx_byte_allocate(byte_pool, &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
/* Create the main thread. */
tx_thread_create(&thread_ptr, "my_thread", my_thread, 0,pointer, DEMO_STACK_SIZE,15, 15, 1, TX_AUTO_START);
}
let me know if you have any further questions
Ok, thanks for the detailed explanation. Yes it does seem a little cleaner if more involved memory management is not required/desired.
I like this post, thanks.
I'm trying to figure out the purpose of these lines in the _Init functions. What does (void)byte_pool; do ?
/* USER CODE BEGIN MX_FileX_MEM_POOL */
(void)byte_pool;
/* USER CODE END MX_FileX_MEM_POOL */
Hello @B.Montanari ,
Thank you for following my tutorial and for your kind words. I'm glad you found it well explained.
However, I am encountering an issue when powering my board. Everything works fine when I upload my program. The 4 threads execute correctly. But if I remove the power and then turn it back on, the threads no longer start. I don't understand why this happens. Could you please help me?
Thank you.