0 前言 STM32WB的应用的建立,比起STM32F1XX/3X系列多了一个M0的MCU,两个MCU之间的协调也变得复杂,因此在理解的时候也较为难懂。 重要的参考文献如下: 1 AN5289,Building wirelessapplications with STM32WB Series microcontrollers 2 STM32WB BLE 应用软件架构 1 初始化阶段8 x6 j* J9 r s: j. z* C4 `! B) B' N# H+ f6 ]$ N K 4 z0 T( d* l* a+ R3 `5 W: s ; J ?' K3 B: d; r0 g! p% G, \- W# ] 3 w' v6 u3 m- Q) D8 b% V7 M 在初始化中,这个初始化和常规的STM32的初始化不一样,其分阶段初始化: 其中,需要注意的用棕色标识,只有在一个阶段完成后才进入下一阶段。 4 o( Y( H% u O* i3 q/ `- L9 D2 e# U3 F, L. s) y7 P' q9 g 只有当shci_resume_flow()的时候,才会有接收事件。同理在hci中。 当弄清楚shci的接收过程,也就差不多清楚了了hci的过程。 2 消息接收、事件处理 事件的处理,主要集中在BLE事件的处理,可以将其等价于中断事件来看,其中中断源在hci_notify_asynch_evt(), 这个中断还会调用:SVCCTL_UserEvtRx(), 在SVCCTL_UserEvtRx中,会去查询SVCCTL_RegisterSvcHandler,就是查看下面挂了多少个事件。 而我们需要的事件HearRate_Event_Handler,注册进去了。 SVCCTL_RegisterSvcHandler(HearRate_Event_Handler); 在HearRate_Event_Handler中,又分很多种情况, 部分代码如下: switch(event_pckt->evt) { case EVT_VENDOR: { blue_evt = (evt_blue_aci*)event_pckt->data; switch(blue_evt->ecode) { 0 Y4 b4 p! l+ a* a' q9 N #if (BLE_CFG_HRS_ENERGY_EXPENDED_INFO_FLAG != 0) //这是 文档对 EVT_BLUE_GATT_WRITE_PERMIT_REQ 的解释 // Server receives a Write command //– HR control point characteristic value //– Resets energy expended command then: //Sends an aci_gatt_write_response() with an OK status. //Notifies the HRS application to reset expended energy //Or sends an aci_gatt_write_response() with an error. case EVT_BLUE_GATT_WRITE_PERMIT_REQ: { aci_gatt_write_permit_req_event_rp0 * write_perm_req; BLE_DBG_HRS_MSG("EVT_BLUE_GATT_WRITE_PERMIT_REQ\n"); write_perm_req = (aci_gatt_write_permit_req_event_rp0*)blue_evt->data; + Y$ v2 m1 A5 @4 A3 N if(write_perm_req->Attribute_Handle == (HRS_Context.ControlPointCharHdle + 1)) { return_value = SVCCTL_EvtAckFlowEnable; - B! a8 l# r. \9 B3 z" U! i: H if (write_perm_req->Data[0] == HRS_CNTL_POINT_RESET_ENERGY_EXPENDED) { /* received a correct value for HRM control point char */ aci_gatt_write_resp(write_perm_req->Connection_Handle, write_perm_req->Attribute_Handle, 0x00, /* write_status = 0 (no error))*/ (uint8_t)HRS_CNTL_POINT_VALUE_IS_SUPPORTED, /* err_code */ write_perm_req->Data_Length, (uint8_t *)&write_perm_req->Data[0]); /** * Notify the application to Reset The Energy Expended Value zhaoxin fixme */ Notification.HRS_Evt_Opcode = HRS_RESET_ENERGY_EXPENDED_EVT; HRS_Notification(&Notification); } else { /* received value of HRM control point char is incorrect */ aci_gatt_write_resp(write_perm_req->Connection_Handle, write_perm_req->Attribute_Handle, 0x1, /* write_status = 1 (error))*/ (uint8_t)HRS_CNTL_POINT_VALUE_NOT_SUPPORTED, /* err_code */ write_perm_req->Data_Length, (uint8_t *)&write_perm_req->Data[0]); } } } break; #endif k9 |; z5 l3 E& d8 s//HR measurement characteristic description value // 这是文档对于 EVT_BLUE_GATT_ATTRIBUTE_MODIFIED的解释 // – HR measurement characteristic description value //– ENABLE or DISABLE notification //– Notifies HRS application of the measurement notification - a- b5 y# q# k3 l$ x0 D* G case EVT_BLUE_GATT_ATTRIBUTE_MODIFIED: { attribute_modified = (aci_gatt_attribute_modified_event_rp0*)blue_evt->data; if(attribute_modified->Attr_Handle == (HRS_Context.HeartRatemeasurementCharHdle + 2)) { return_value = SVCCTL_EvtAckFlowEnable; ) z9 K/ f2 f1 ] P3 H8 s2 E% Q /** * Notify the application to start measurement NOTIFY USER APPLICATION – HRS_Notification */ if(attribute_modified->Attr_Data[0] & COMSVC_Notification) { BLE_DBG_HRS_MSG("EVT_BLUE_GATT_ATTRIBUTE_MODIFIED HRS_NOTIFICATION_ENABLED\n"); Notification.HRS_Evt_Opcode =HRS_NOTIFICATION_ENABLED; HRS_Notification(&Notification); } 其中最重要的HRS_Notification() 在这个里面有打开定时器的功能,将注册的测量函数使能。这样当我们使能Notify的时候,就会有数据发送过来。 基本上就完成了下图所示的逻辑。 3 小结 自己觉得BLE的使用还是比较不易懂得,虽然AN5289已经很详细了,但是自己在看的时候还是不那么理解,后面消化一小段时间,结合STM32WB BLE 应用软件架构,才将其初始化过程理解的多了点。 另外,这是自己兴趣所在的自学,不是给必须的要求时间的投入和持续,导致进展并不是很快。 此外,相对于TI的蓝牙,真的学的人太少了。 3 D! m# u+ n; a" P. D0 ?, e5 e: Q! s2 v |
签到 |
4月25日培训|基于ST 双核多协议STM32WB55的Matter方案开发
实战经验 | 关于STM32WB OTA 速率提升引发的讨论
基于STM32WB55的配置串口打印Debug经验分享
基于创建STM32WBA BLE_Custom工程经验分享
来看直播了,就在明天 | STM32 Matter 解决方案,轻松实现智能家居无缝连接
基于STM32WB55开发之套件概述(1)
基于STM32WB55开发之监测STM32WB连接状态(1)
基于STM32WB55开发之修改蓝牙地址(2)
STM32WB基于Custom Template实现客户定制BLE私有协议
如何基于STM32WB一次性烧写FUS+STACK+APP