只从ThreadX开源之后,凭借降维打击的优势吸引了一票开发者,也吸引越来越多的厂家加入其中。
ST也在U5的时候,第一次完全摒弃了以前大杂烩的模式(FreeRTOS+lwip+FatFS+自家USB),完全投入ThreadX怀抱。当然为了保护自己TouchGFX的尊严,ThreadX GUIX是没有引入滴。看这形式,老一套大杂烩在新的芯片是不会在被支持了,兄弟们,ThreadX搞起来。当然也可以研究一下CMSIS RTOS适配层,一招吃遍天。
今天就简单拿ThreadX跑个LED,小小一颗LED,让我水了多少文章啊,哈哈哈。
打开STM32CubeMX,创建一个工程,具体可以看我之前的文章。
简单配个时钟
开一下ThreadX
配置一下,加大一下内存池,10ms的节拍实在不习惯,改成1ms
配置一下俩小灯
Icache老是弹窗,太烦人了,给它打开。
更改一下库ms延时的定时器,滴答让给ThreadX。
配置一下别的,然后生成代码。
任务配置
- #define APP_LED1_TASK_SIZE 512
- #define APP_LED1_TASK_PRIO 4
- TX_THREAD MsgSenderThreadLED1;
- #define APP_LED2_TASK_SIZE 512
- #define APP_LED2_TASK_PRIO 4
- TX_THREAD MsgSenderThreadLED2;
复制代码
任务初始化代码
- 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;
- if (tx_byte_allocate(byte_pool, (VOID **) &pointer,
- APP_LED1_TASK_SIZE, TX_NO_WAIT) != TX_SUCCESS)
- {
- ret = TX_POOL_ERROR;
- }
-
- if(tx_thread_create(&MsgSenderThreadLED1, "LED1 Task",
- MsgSenderThreadLED1_Entry, 0, pointer, APP_LED1_TASK_SIZE,
- APP_LED1_TASK_PRIO, APP_LED1_TASK_PRIO,
- TX_NO_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS)
- {
- ret = TX_THREAD_ERROR;
- }
-
- if (tx_byte_allocate(byte_pool, (VOID **) &pointer,
- APP_LED2_TASK_SIZE, TX_NO_WAIT) != TX_SUCCESS)
- {
- ret = TX_POOL_ERROR;
- }
-
- if(tx_thread_create(&MsgSenderThreadLED2, "LED2 Task",
- MsgSenderThreadLED2_Entry, 0, pointer, APP_LED2_TASK_SIZE,
- APP_LED2_TASK_PRIO, APP_LED2_TASK_PRIO,
- TX_NO_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS)
- {
- ret = TX_THREAD_ERROR;
- }
- /* USER CODE END App_ThreadX_Init */
- return ret;
- }
复制代码
俩任务函数,绿灯1S翻转一次。红灯0.5S翻转一次。
- void MsgSenderThreadLED1_Entry(ULONG thread_input)
- {
- (void) thread_input;
- while(1)
- {
- HAL_GPIO_TogglePin(LED_G_GPIO_Port, LED_G_Pin);
- tx_thread_sleep(1000);
- }
- }
- void MsgSenderThreadLED2_Entry(ULONG thread_input)
- {
- (void) thread_input;
- while(1)
- {
- HAL_GPIO_TogglePin(LED_R_GPIO_Port, LED_R_Pin);
- tx_thread_sleep(500);
- }
- }
复制代码
下载验证,完事。
|