1119|2

6107

帖子

4

TA的资源

版主

楼主
 

【STM32WBA52CG】+5 THREADX使用 [复制链接]

本帖最后由 damiaa 于 2023-8-15 09:49 编辑

                 【STM32WBA52CG】+5 THREADX使用

【NUCLEO-WBA52CG】+1开箱贴

STM32WBA52CG】+2 STM32WBA Web Bluetooth试玩

STM32WBA52CG】+3 STM32WBA 外设应用一 按键和点灯

STM32WBA52CG】+4 外设应用二 串口使用之后继续

据说THREADX很不错,而且STM32免费使用,就拿STM32WBA52CG实验一下。

下载STM32Cube_FW_WBA_V1.1.0解压,例子在..\STM32Cube_FW_WBA_V1.1.0\Projects\NUCLEO-WBA52CG\Applications\ThreadX\Tx_Thread_MsgQueue

,用MDK打开。

项目如下所示,main.c负责硬件的初始化和ThreadX的初始化,MX_ThreadX_Init调用app_threadx.c的 MX_ThreadX_Init (里面调用tx_kernel_enter())。

接着程序会运行App_ThreadX_Init(VOID *memory_ptr)函数。

这里可以创建 任务和消息:

发送任务1入口函数中处理按键1和按键 2并发送消息到消息队列1:

发送任务2入口函数中处理按键3并发送消息到消息队列2:

接收任务接收到消息 并启动 相应的led闪烁

主要代码如下:

UINT App_ThreadX_Init(VOID *memory_ptr)
{
   UINT ret = TX_SUCCESS;
   TX_BYTE_POOL *byte_pool = (TX_BYTE_POOL*)memory_ptr;
   CHAR *pointer;
   /* Allocate the stack for Message Queue Sender Thread One */
   if (tx_byte_allocate(byte_pool, (VOID**) &pointer, TX_APP_STACK_SIZE, TX_NO_WAIT) != 
      TX_SUCCESS){ return TX_POOL_ERROR;}
   /* Create Message Queue Sender Thread One. */
   if (tx_thread_create(&tx_app_thread, "Message Queue Sender Thread One", 
      MsgSenderThreadOne_Entry, 0, pointer,TX_APP_STACK_SIZE, TX_APP_THREAD_PRIO, 
      TX_APP_THREAD_PREEMPTION_THRESHOLD,TX_APP_THREAD_TIME_SLICE, TX_APP_THREAD_AUTO_START) != 
      TX_SUCCESS){ return TX_THREAD_ERROR;}
   /* Allocate the stack for Message Queue One. */
   if (tx_byte_allocate(byte_pool, (VOID **) &pointer, TX_APP_MSG_QUEUE_FULL_SIZE * sizeof(ULONG), 
      TX_NO_WAIT) != TX_SUCCESS){ return TX_POOL_ERROR;}
   /* Create Message Queue One. */
   if (tx_queue_create(&tx_app_msg_queue,"Message Queue One", TX_APP_SINGLE_MSG_SIZE,pointer, 
      TX_APP_MSG_QUEUE_FULL_SIZE * sizeof(ULONG)) != TX_SUCCESS){return TX_QUEUE_ERROR; }
   /* USER CODE BEGIN App_ThreadX_Init */
   /* Allocate the stack for MsgSenderThreadTwo. */
   if (tx_byte_allocate(byte_pool, (VOID **) &pointer, TX_APP_STACK_SIZE, TX_NO_WAIT) != 
      TX_SUCCESS){ ret = TX_POOL_ERROR;}
   /* Create MsgSenderThreadTwo. */
   if (tx_thread_create(&MsgSenderThreadTwo, "Message Queue Sender Thread Two",
      MsgSenderThreadTwo_Entry, 0, pointer, TX_APP_STACK_SIZE,TX_APP_THREAD_PRIO, 
      TX_APP_THREAD_PREEMPTION_THRESHOLD,TX_NO_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS)
      { ret = TX_THREAD_ERROR;}
   /* Allocate the stack for MsgReceiverThread. */
   if (tx_byte_allocate(byte_pool, (VOID **) &pointer, TX_APP_STACK_SIZE, TX_NO_WAIT) != 
      TX_SUCCESS){ ret = TX_POOL_ERROR; }
   /* Create MsgReceiverThread. */
   if (tx_thread_create(&MsgReceiverThread, "Message Queue Receiver Thread",
      MsgReceiverThread_Entry, 0, pointer, TX_APP_STACK_SIZE,
      RECEIVER_THREAD_PRIO, RECEIVER_THREAD_PREEMPTION_THRESHOLD,
      TX_NO_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS){ ret = TX_THREAD_ERROR;}
   /* Allocate the MsgQueueTwo. */
   if (tx_byte_allocate(byte_pool, (VOID **) &pointer, TX_APP_MSG_QUEUE_FULL_SIZE *sizeof(ULONG), 
      TX_NO_WAIT) != TX_SUCCESS){ ret = TX_POOL_ERROR; }
   /* Create the MsgQueueTwo shared by MsgSenderThreadTwo and MsgReceiverThread. */
   if (tx_queue_create(&MsgQueueTwo, "Message Queue Two", TX_1_ULONG, pointer, 
      TX_APP_MSG_QUEUE_FULL_SIZE *sizeof(ULONG)) != TX_SUCCESS){ ret = TX_QUEUE_ERROR;}
   /* USER CODE END App_ThreadX_Init */
   return ret;
}
void MsgSenderThreadOne_Entry(ULONG thread_input)
{
   /* USER CODE BEGIN MsgSenderThreadOne_Entry */
   ULONG Msg = TOGGLE_LED;
   ULONG Msg2 = TOGGLE_LED2;
   (void) thread_input;
   /* Infinite loop */
   while(1)
   {
      HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
      GPIO_PinState B1Status = HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
      if(B1Status ==GPIO_PIN_RESET)
      {
         /* Send message to MsgQueueOne. */
         if (tx_queue_send(&tx_app_msg_queue, &Msg, TX_WAIT_FOREVER) != TX_SUCCESS)
         {
            Error_Handler();
         }
      }
      GPIO_PinState B2Status = HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin);
      if(B2Status ==GPIO_PIN_RESET)
      {
         /* Send message to MsgQueueOne. */
         if (tx_queue_send(&tx_app_msg_queue, &Msg2, TX_WAIT_FOREVER) != TX_SUCCESS)
         {
            Error_Handler();
         }
      }
      /* Sleep for 200ms */
      tx_thread_sleep(20);
   }
   /* USER CODE END MsgSenderThreadOne_Entry */
}
void MsgSenderThreadTwo_Entry(ULONG thread_input)
{
   ULONG Msg = TOGGLE_LED;
   (void) thread_input;
   /* Infinite loop */
   while(1)
   {
      GPIO_PinState B3Status = HAL_GPIO_ReadPin(B3_GPIO_Port, B3_Pin);
      if(B3Status ==GPIO_PIN_RESET)
      {
         /* Send message to MsgQueueTwo. */
         if (tx_queue_send(&MsgQueueTwo, &Msg, TX_WAIT_FOREVER) != TX_SUCCESS)
         {
            Error_Handler();
         }
      }
      /* Sleep for 500s */
      tx_thread_sleep(20);
   }
}
void MsgReceiverThread_Entry(ULONG thread_input)
{
   ULONG RMsg = 0;
   UINT status = 0 ;
   (void) thread_input;
   /* Infinite loop */
   while (1)
   {
      /* Determine whether a message MsgQueueOne or MsgQueueTwo is available */
      status = tx_queue_receive(&tx_app_msg_queue, &RMsg, TX_NO_WAIT);
      if (status == TX_SUCCESS)
      {
         /* Check Message value */
         if (RMsg == TOGGLE_LED)
         {
            HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
         }
         else
         {
            if (RMsg == TOGGLE_LED2)
            {
               HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin);
            }
            else
               Error_Handler();
         }
      }
      else
      {
         status = tx_queue_receive(&MsgQueueTwo, &RMsg, TX_NO_WAIT);
         if ( status == TX_SUCCESS)
         {
            /* Check Message value */
            if (RMsg != TOGGLE_LED)
            {
               Error_Handler();
            }
            else
            {
               HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
               HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin);
            }
         }
      }
   }
}

实验结果:

4

 

谢谢

此帖出自无线连接论坛

最新回复

threadx看上去也没有那么难嘛   详情 回复 发表于 2023-8-17 23:28
点赞 关注
 

回复
举报

685

帖子

0

TA的资源

纯净的硅(高级)

沙发
 

threadx看上去也没有那么难嘛

此帖出自无线连接论坛

点评

是的。楼主    详情 回复 发表于 2023-8-18 09:49
 
 

回复

6107

帖子

4

TA的资源

版主

板凳
 
jobszheng5 发表于 2023-8-17 23:28 threadx看上去也没有那么难嘛

是的。楼主  

此帖出自无线连接论坛
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
快速回复 返回顶部 返回列表