你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

[STM32WB 蓝牙探学] 4 对建立蓝牙过程的理解

[复制链接]
ziziran 发布时间:2020-6-13 22:03
0 前言
STM32WB的应用的建立,比起STM32F1XX/3X系列多了一个M0的MCU,两个MCU之间的协调也变得复杂,因此在理解的时候也较为难懂。
重要的参考文献如下:
1  AN5289,Building wirelessapplications with STM32WB Series microcontrollers
2  STM32WB BLE 应用软件架构
1 初始化阶段
$ S5 u7 |3 }1 `& m

8 b8 a( N; b; ?! ^& x
q2.png
  e+ l/ N" G5 a/ k- r4 m5 m8 O' f% }
8 t3 J5 |0 ~3 p' C; ], ?

. b: c0 `' q' d8 V5 m) X) h6 w5 ~8 n3 ?0 x' i- x. K
- i9 ]2 N4 c6 d# p, ~
在初始化中,这个初始化和常规的STM32的初始化不一样,其分阶段初始化:
q3.png
! A) T$ C3 [9 D  @0 j" K# A

& Y9 [/ _, @! i3 h' T- O2 a4 S
其中,需要注意的用棕色标识,只有在一个阶段完成后才进入下一阶段。
6 f# p& p" P2 q2 L3 `" B. ]# |1 }
& G+ h; n: j% y  L
q4.png
只有当shci_resume_flow()的时候,才会有接收事件。同理在hci中。
q5.png

2 `7 k* V3 r3 d& \
当弄清楚shci的接收过程,也就差不多清楚了了hci的过程。

; L# U2 x, {* r0 D9 b; s& u* |% u2 s2 消息接收、事件处理
事件的处理,主要集中在BLE事件的处理,可以将其等价于中断事件来看,其中中断源在hci_notify_asynch_evt()
这个中断还会调用:SVCCTL_UserEvtRx()
SVCCTL_UserEvtRx中,会去查询SVCCTL_RegisterSvcHandler,就是查看下面挂了多少个事件。
而我们需要的事件HearRate_Event_Handler,注册进去了。
SVCCTL_RegisterSvcHandler(HearRate_Event_Handler);
HearRate_Event_Handler中,又分很多种情况,
* x& z0 D: S+ b% Q! M* t. A; c9 b' |

* q0 ~8 X! U2 V6 S$ ?6 k
部分代码如下:
  switch(event_pckt->evt)
  {
    case EVT_VENDOR:
    {
      blue_evt = (evt_blue_aci*)event_pckt->data;
      switch(blue_evt->ecode)
      {

5 z+ F: s  p. ?/ a2 ^1 r' q$ P" `# B  W5 Z
#if (BLE_CFG_HRS_ENERGY_EXPENDED_INFO_FLAG != 0)
  B" w. i) P8 I1 i. d
     //这是 文档对                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;
2 O  `" S  t: h! C( X' y0 @3 `) \
          BLE_DBG_HRS_MSG("EVT_BLUE_GATT_WRITE_PERMIT_REQ\n");
          write_perm_req = (aci_gatt_write_permit_req_event_rp0*)blue_evt->data;

3 l- J  L4 K6 e! T4 ?( ]4 C" Y4 W
          if(write_perm_req->Attribute_Handle == (HRS_Context.ControlPointCharHdle + 1))
          {
            return_value = SVCCTL_EvtAckFlowEnable;
3 g( U, N  e2 ~+ J- B1 N
            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]);

. h! X; d0 g3 q
              /**
               * 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

+ r+ ]; f% e# A. K
//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                        
5 D# l2 B0 A( B- T( `
        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;

/ Q, A' n: ~" A0 \# P; p
            /**
             * 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);
            }

* ^8 L$ Y, T6 F: n9 W7 W- r
其中最重要的HRS_Notification()
在这个里面有打开定时器的功能,将注册的测量函数使能。这样当我们使能Notify的时候,就会有数据发送过来。
q1.PNG
基本上就完成了下图所示的逻辑。
; K! |$ I) c5 `; W$ o! d: ?- t

" {+ k, z) o; q4 ]3 小结
         自己觉得BLE的使用还是比较不易懂得,虽然AN5289已经很详细了,但是自己在看的时候还是不那么理解,后面消化一小段时间,结合STM32WB BLE 应用软件架构,才将其初始化过程理解的多了点。
另外,这是自己兴趣所在的自学,不是给必须的要求时间的投入和持续,导致进展并不是很快。
此外,相对于TI的蓝牙,真的学的人太少了。

6 Z& U$ ~  N$ u7 [" Z4 d. O$ @0 X/ u. w7 j
4 ?" @. }1 N7 w1 q. o

评分

参与人数 1 ST金币 +100 收起 理由
STMCU + 100

查看全部评分

收藏 评论1 发布时间:2020-6-13 22:03

举报

1个回答
神圣雅诗人 回答时间:2020-6-16 08:11:03
签到

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版