[STM32WB] 【STM32WB09KE测评】-进阶任务-芯片温度自动上报

[复制链接]
1465|4
 楼主| YangTwo 发表于 2024-11-10 20:37 | 显示全部楼层 |阅读模式
本帖最后由 YangTwo 于 2024-11-10 20:36 编辑

一、进阶方向小项目背景
STM32WB09KE芯片自带了一个测量芯片结温的温度传感器,计划将该传感器数据通过蓝牙BLE发送出去。当中心设备开启Notify功能后,该传感器数据每隔1s发送一次,避免手动刷新。

二、用到的硬件模块
测量芯片温度使用到了ADC模块,发送间隔使用到了TIM2模块,无线传输用到了BLE模块。
ADC模块配置:
909646730a8283cffd.png
TIM2模块配置:
114486730a8340756e.png
BLE模块配置:

170406730a85409165.png
655926730a85c9bc00.png

57046730a86da607c.png


三、核心代码
主函数主要是初始化各种时钟,外设,然后进入UTIL序列运行状态。
  1. int main(void)
  2. {

  3.   /* USER CODE BEGIN 1 */

  4.   /* USER CODE END 1 */

  5.   /* MCU Configuration--------------------------------------------------------*/

  6.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  7.   HAL_Init();

  8.   /* USER CODE BEGIN Init */

  9.   /* USER CODE END Init */

  10.   /* Configure the system clock */
  11.   SystemClock_Config();

  12.   /* Configure the peripherals common clocks */
  13.   PeriphCommonClock_Config();

  14.   /* USER CODE BEGIN SysInit */

  15.   /* USER CODE END SysInit */

  16.   /* Initialize all configured peripherals */
  17.   MX_GPIO_Init();
  18.   MX_USART1_UART_Init();
  19.   MX_RNG_Init();
  20.   MX_RADIO_Init();
  21.   MX_RADIO_TIMER_Init();
  22.   MX_PKA_Init();
  23.   MX_ADC1_Init();
  24.   MX_TIM2_Init();
  25.   /* USER CODE BEGIN 2 */
  26.   if (HAL_ADC_Start(&hadc1) != HAL_OK)
  27.   {
  28.     /* Error: ADC conversion start could not be performed */
  29.     Error_Handler();
  30.   }
  31.   printf("** ADC Started ** \n\r");
  32.   /* Start Channel1 */
  33.   if (HAL_TIM_Base_Start_IT(&htim2) != HAL_OK)
  34.   {
  35.     /* Starting Error */
  36.     Error_Handler();
  37.   }
  38.   printf("** Tim2 Started ** \n\r");
  39.   /* USER CODE END 2 */

  40.   /* Init code for STM32_BLE */
  41.   MX_APPE_Init(NULL);

  42.   /* Infinite loop */
  43.   /* USER CODE BEGIN WHILE */
  44.   while (1)
  45.   {
  46.     /* USER CODE END WHILE */
  47.     MX_APPE_Process();

  48.     /* USER CODE BEGIN 3 */
  49.   }
  50.   /* USER CODE END 3 */
  51. }

在初始化的过程中会注册多个UTIL TASK,其中就包括注册触发发送传感器数据的UTIL任务:
  1. UTIL_SEQ_RegTask(1U << CFG_TASK_SIMPLEBLEAUTONOTIFY, UTIL_SEQ_RFU, APPE_SimpleBLENotifyAction);
该Task的触发是在TIM2的ISR内:
  1. void TIM2_IRQHandler(void)
  2. {
  3.   /* USER CODE BEGIN TIM2_IRQn 0 */

  4.   /* USER CODE END TIM2_IRQn 0 */
  5.   HAL_TIM_IRQHandler(&htim2);
  6.   /* USER CODE BEGIN TIM2_IRQn 1 */
  7.   HAL_GPIO_TogglePin(PB1_LD1_BLUE_GPIO_Port, PB1_LD1_BLUE_Pin);
  8.   UTIL_SEQ_SetTask(1U << CFG_TASK_SIMPLEBLEAUTONOTIFY, CFG_SEQ_PRIO_0);
  9.   /* USER CODE END TIM2_IRQn 1 */
  10. }
该函数进而触发具体的发送函数:

  1. void APPE_SimpleBLENotifyAction(void)
  2. {
  3.   if (bleAppContext.Device_Connection_Status == APP_BLE_CONNECTED_SERVER)
  4.   {
  5.     UTIL_SEQ_SetTask( 1<<<span style="background-color:#ffffff;padding:0px 0px 0px 2px;"><span style="color:#000000;background-color:#ffffff;font-family:&quot;Courier New&quot;;font-size:10pt;white-space:pre;"><span style="color:#000000;"></span><span style="color:#0000c0;background-color:#d4d4d4;font-style:italic;">CFG_TASK_SIMPLEBLESEND_NOTIF</span></span></span>, CFG_SEQ_PRIO_0);
  6.   }

  7.   return;
  8. }

进而调用注册在TEMPSENSOR_APP_Init函数中的UTIL序列任务,用于发送传感器更新值。
  1. void TEMPSENSOR_APP_Init(void)
  2. {
  3.   TEMPSENSOR_APP_Context.ConnectionHandle = 0xFFFF;
  4.   TEMPSENSOR_Init();

  5.   /* USER CODE BEGIN Service1_APP_Init */
  6.   UTIL_SEQ_RegTask( 1U << CFG_TASK_SIMPLEBLESEND_NOTIF, UTIL_SEQ_RFU, TEMPSENSOR_Senval_SendNotification);
  7.   TEMPSENSOR_APP_Context.Senval_Notification_Status = Senval_NOTIFICATION_OFF;
  8.   /* USER CODE END Service1_APP_Init */
  9.   return;
  10. }

当中心设备开启Notify功能后:
  1. void TEMPSENSOR_Notification(TEMPSENSOR_NotificationEvt_t *p_Notification)
  2. {
  3.   /* USER CODE BEGIN Service1_Notification_1 */

  4.   /* USER CODE END Service1_Notification_1 */
  5.   switch(p_Notification->EvtOpcode)
  6.   {
  7.     /* USER CODE BEGIN Service1_Notification_Service1_EvtOpcode */

  8.     /* USER CODE END Service1_Notification_Service1_EvtOpcode */

  9.     case TEMPSENSOR_SENVAL_READ_EVT:
  10.       /* USER CODE BEGIN Service1Char1_READ_EVT */

  11.       /* USER CODE END Service1Char1_READ_EVT */
  12.       break;

  13.     case TEMPSENSOR_SENVAL_NOTIFY_ENABLED_EVT:
  14.       /* USER CODE BEGIN Service1Char1_NOTIFY_ENABLED_EVT */
  15.             TEMPSENSOR_APP_Context.Senval_Notification_Status = Senval_NOTIFICATION_ON;
  16.       /* USER CODE END Service1Char1_NOTIFY_ENABLED_EVT */
  17.       break;

  18.     case TEMPSENSOR_SENVAL_NOTIFY_DISABLED_EVT:
  19.       /* USER CODE BEGIN Service1Char1_NOTIFY_DISABLED_EVT */
  20.             TEMPSENSOR_APP_Context.Senval_Notification_Status = Senval_NOTIFICATION_OFF;
  21.       /* USER CODE END Service1Char1_NOTIFY_DISABLED_EVT */
  22.       break;

  23.     default:
  24.       /* USER CODE BEGIN Service1_Notification_default */

  25.       /* USER CODE END Service1_Notification_default */
  26.       break;
  27.   }
  28.   /* USER CODE BEGIN Service1_Notification_2 */

  29.   /* USER CODE END Service1_Notification_2 */
  30.   return;
  31. }

WB09就可以根据notification_on_off 标志位来判断是否发送更新值,代码如下:
  1. __USED void TEMPSENSOR_Senval_SendNotification(void) /* Property Notification */
  2. {
  3.   TEMPSENSOR_APP_SendInformation_t notification_on_off = Senval_NOTIFICATION_OFF;
  4.   TEMPSENSOR_Data_t tempsensor_notification_data;

  5.   tempsensor_notification_data.p_Payload = (uint8_t*)a_TEMPSENSOR_UpdateCharData;
  6.   tempsensor_notification_data.Length = 0;

  7.   /* USER CODE BEGIN Service1Char1_NS_1*/
  8.   HAL_ADC_PollForConversion(&hadc1, 50);
  9.   a_TEMPSENSOR_UpdateCharData[0] = __LL_ADC_CALC_TEMPERATURE( HAL_ADC_GetValue(&hadc1), LL_ADC_DS_DATA_WIDTH_12_BIT);
  10.   tempsensor_notification_data.Length = 1;

  11.   printf("\r\na_TEMPSENSOR_UpdateCharData[0] = %d , HAL_ADC_VAL = %d",
  12. a_TEMPSENSOR_UpdateCharData[0], __LL_ADC_CALC_TEMPERATURE(
  13. HAL_ADC_GetValue(&hadc1), LL_ADC_DS_DATA_WIDTH_12_BIT));
  14.   /* USER CODE END Service1Char1_NS_1*/
  15.   notification_on_off = TEMPSENSOR_APP_Context.Senval_Notification_Status;

  16.   if (notification_on_off != Senval_NOTIFICATION_OFF && TEMPSENSOR_APP_Context.ConnectionHandle != 0xFFFF)
  17.   {
  18.     TEMPSENSOR_NotifyValue(TEMPSENSOR_SENVAL, &tempsensor_notification_data, TEMPSENSOR_APP_Context.ConnectionHandle);
  19.   }

  20.   /* USER CODE BEGIN Service1Char1_NS_Last*/

  21.   /* USER CODE END Service1Char1_NS_Last*/

  22.   return;
  23. }



四、测试结果见附件。
芯片温度自动上报.zip (2.66 MB, 下载次数: 8)



AdaMaYun 发表于 2024-11-11 18:28 | 显示全部楼层
很不错的案例,深入学习一下
21mengnan 发表于 2024-11-28 18:54 | 显示全部楼层
蓝牙的一个不错应用。
yangjiaxu 发表于 2024-11-29 14:09 | 显示全部楼层
这个专业了,我平时用的蓝牙都是AT指令方式,利用串口发送,虽然简单,但是感觉还是你这种方式比较好
sujingliang 发表于 2024-12-10 21:38 | 显示全部楼层
赞!非常值得借鉴。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

27

主题

110

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部