本帖最后由 YangTwo 于 2024-11-10 20:36 编辑
一、进阶方向小项目背景
STM32WB09KE芯片自带了一个测量芯片结温的温度传感器,计划将该传感器数据通过蓝牙BLE发送出去。当中心设备开启Notify功能后,该传感器数据每隔1s发送一次,避免手动刷新。
二、用到的硬件模块
测量芯片温度使用到了ADC模块,发送间隔使用到了TIM2模块,无线传输用到了BLE模块。
ADC模块配置:
TIM2模块配置:
BLE模块配置:
三、核心代码
主函数主要是初始化各种时钟,外设,然后进入UTIL序列运行状态。
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* Configure the peripherals common clocks */
PeriphCommonClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_RNG_Init();
MX_RADIO_Init();
MX_RADIO_TIMER_Init();
MX_PKA_Init();
MX_ADC1_Init();
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
if (HAL_ADC_Start(&hadc1) != HAL_OK)
{
/* Error: ADC conversion start could not be performed */
Error_Handler();
}
printf("** ADC Started ** \n\r");
/* Start Channel1 */
if (HAL_TIM_Base_Start_IT(&htim2) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
printf("** Tim2 Started ** \n\r");
/* USER CODE END 2 */
/* Init code for STM32_BLE */
MX_APPE_Init(NULL);
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
MX_APPE_Process();
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
在初始化的过程中会注册多个UTIL TASK,其中就包括注册触发发送传感器数据的UTIL任务:
UTIL_SEQ_RegTask(1U << CFG_TASK_SIMPLEBLEAUTONOTIFY, UTIL_SEQ_RFU, APPE_SimpleBLENotifyAction);
该Task的触发是在TIM2的ISR内:
void TIM2_IRQHandler(void)
{
/* USER CODE BEGIN TIM2_IRQn 0 */
/* USER CODE END TIM2_IRQn 0 */
HAL_TIM_IRQHandler(&htim2);
/* USER CODE BEGIN TIM2_IRQn 1 */
HAL_GPIO_TogglePin(PB1_LD1_BLUE_GPIO_Port, PB1_LD1_BLUE_Pin);
UTIL_SEQ_SetTask(1U << CFG_TASK_SIMPLEBLEAUTONOTIFY, CFG_SEQ_PRIO_0);
/* USER CODE END TIM2_IRQn 1 */
}
该函数进而触发具体的发送函数:
void APPE_SimpleBLENotifyAction(void)
{
if (bleAppContext.Device_Connection_Status == APP_BLE_CONNECTED_SERVER)
{
UTIL_SEQ_SetTask( 1<<<span style="background-color:#ffffff;padding:0px 0px 0px 2px;"><span style="color:#000000;background-color:#ffffff;font-family:"Courier New";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);
}
return;
}
进而调用注册在TEMPSENSOR_APP_Init函数中的UTIL序列任务,用于发送传感器更新值。void TEMPSENSOR_APP_Init(void)
{
TEMPSENSOR_APP_Context.ConnectionHandle = 0xFFFF;
TEMPSENSOR_Init();
/* USER CODE BEGIN Service1_APP_Init */
UTIL_SEQ_RegTask( 1U << CFG_TASK_SIMPLEBLESEND_NOTIF, UTIL_SEQ_RFU, TEMPSENSOR_Senval_SendNotification);
TEMPSENSOR_APP_Context.Senval_Notification_Status = Senval_NOTIFICATION_OFF;
/* USER CODE END Service1_APP_Init */
return;
}
当中心设备开启Notify功能后:
void TEMPSENSOR_Notification(TEMPSENSOR_NotificationEvt_t *p_Notification)
{
/* USER CODE BEGIN Service1_Notification_1 */
/* USER CODE END Service1_Notification_1 */
switch(p_Notification->EvtOpcode)
{
/* USER CODE BEGIN Service1_Notification_Service1_EvtOpcode */
/* USER CODE END Service1_Notification_Service1_EvtOpcode */
case TEMPSENSOR_SENVAL_READ_EVT:
/* USER CODE BEGIN Service1Char1_READ_EVT */
/* USER CODE END Service1Char1_READ_EVT */
break;
case TEMPSENSOR_SENVAL_NOTIFY_ENABLED_EVT:
/* USER CODE BEGIN Service1Char1_NOTIFY_ENABLED_EVT */
TEMPSENSOR_APP_Context.Senval_Notification_Status = Senval_NOTIFICATION_ON;
/* USER CODE END Service1Char1_NOTIFY_ENABLED_EVT */
break;
case TEMPSENSOR_SENVAL_NOTIFY_DISABLED_EVT:
/* USER CODE BEGIN Service1Char1_NOTIFY_DISABLED_EVT */
TEMPSENSOR_APP_Context.Senval_Notification_Status = Senval_NOTIFICATION_OFF;
/* USER CODE END Service1Char1_NOTIFY_DISABLED_EVT */
break;
default:
/* USER CODE BEGIN Service1_Notification_default */
/* USER CODE END Service1_Notification_default */
break;
}
/* USER CODE BEGIN Service1_Notification_2 */
/* USER CODE END Service1_Notification_2 */
return;
}
WB09就可以根据notification_on_off 标志位来判断是否发送更新值,代码如下:
__USED void TEMPSENSOR_Senval_SendNotification(void) /* Property Notification */
{
TEMPSENSOR_APP_SendInformation_t notification_on_off = Senval_NOTIFICATION_OFF;
TEMPSENSOR_Data_t tempsensor_notification_data;
tempsensor_notification_data.p_Payload = (uint8_t*)a_TEMPSENSOR_UpdateCharData;
tempsensor_notification_data.Length = 0;
/* USER CODE BEGIN Service1Char1_NS_1*/
HAL_ADC_PollForConversion(&hadc1, 50);
a_TEMPSENSOR_UpdateCharData[0] = __LL_ADC_CALC_TEMPERATURE( HAL_ADC_GetValue(&hadc1), LL_ADC_DS_DATA_WIDTH_12_BIT);
tempsensor_notification_data.Length = 1;
printf("\r\na_TEMPSENSOR_UpdateCharData[0] = %d , HAL_ADC_VAL = %d",
a_TEMPSENSOR_UpdateCharData[0], __LL_ADC_CALC_TEMPERATURE(
HAL_ADC_GetValue(&hadc1), LL_ADC_DS_DATA_WIDTH_12_BIT));
/* USER CODE END Service1Char1_NS_1*/
notification_on_off = TEMPSENSOR_APP_Context.Senval_Notification_Status;
if (notification_on_off != Senval_NOTIFICATION_OFF && TEMPSENSOR_APP_Context.ConnectionHandle != 0xFFFF)
{
TEMPSENSOR_NotifyValue(TEMPSENSOR_SENVAL, &tempsensor_notification_data, TEMPSENSOR_APP_Context.ConnectionHandle);
}
/* USER CODE BEGIN Service1Char1_NS_Last*/
/* USER CODE END Service1Char1_NS_Last*/
return;
}
四、测试结果见附件。
芯片温度自动上报.zip
(2.66 MB)
|