[PIC32/SAM] 【Curiosity测评报告】WBZ451蓝牙BLE测试

[复制链接]
 楼主| 南来之风 发表于 2023-12-21 15:58 | 显示全部楼层 |阅读模式
本帖最后由 南来之风 于 2024-1-3 20:36 编辑

通过自定义服务,可以利用GAP与GATT协议,创建一个可通过手机app连接到BLE服务。
MCC配置BLE Stack



点击Generate后,生成的代码位于config\default\ble\service_ble\ble_my_lbs_svc.c
  1. #include <stddef.h>
  2. #include "gatt.h"
  3. #include "ble_util/byte_stream.h"
  4. #include "ble_cms/ble_my_lbs_svc.h"


  5. // *****************************************************************************
  6. // *****************************************************************************
  7. // Section: Macros
  8. // *****************************************************************************
  9. // *****************************************************************************
  10. /* Little Endian. */
  11. #define UUID_MY_LBS_PRIMARY_SVC_LE         0xea, 0xce, 0xf4, 0xac, 0x3f, 0x37, 0x11, 0xee, 0xbe, 0x56, 0x02, 0x42, 0xac, 0x08, 0x23, 0x20

  12. #define UUID_MY_LBS_CHARACTERISTIC_0_LE         0xea, 0xce, 0xf4, 0xbc, 0x3f, 0x37, 0x11, 0xee, 0xbe, 0x56, 0x02, 0x42, 0xac, 0x08, 0x23, 0x20
  13. #define UUID_MY_LBS_CHARACTERISTIC_1_LE         0xea, 0xce, 0xf4, 0xcc, 0x3f, 0x37, 0x11, 0xee, 0xbe, 0x56, 0x02, 0x42, 0xac, 0x08, 0x23, 0x20

  14. // *****************************************************************************
  15. // *****************************************************************************
  16. // Section: Local Variables
  17. // *****************************************************************************
  18. // *****************************************************************************

  19. /* Primary Service Declaration */
  20. static const uint8_t s_my_lbsSvcUuid[] = {UUID_MY_LBS_PRIMARY_SVC_LE};
  21. static const uint16_t s_my_lbsSvcUuidLen = sizeof(s_my_lbsSvcUuid);

  22. /* My_lbs Characteristic 0 Characteristic */
  23. static const uint8_t s_my_lbsChar0[] = {ATT_PROP_READ|ATT_PROP_NOTIFY, UINT16_TO_BYTES(MY_LBS_HDL_CHARVAL_0), UUID_MY_LBS_CHARACTERISTIC_0_LE};
  24. static const uint16_t s_my_lbsChar0Len = sizeof(s_my_lbsChar0);

  25. /* My_lbs Characteristic 0 Characteristic Value */
  26. static const uint8_t s_my_lbsUuidChar0[] = {UUID_MY_LBS_CHARACTERISTIC_0_LE};
  27. static uint8_t s_my_lbsChar0Val[] = {};
  28. static uint16_t s_my_lbsChar0ValLen = sizeof(s_my_lbsChar0Val);

  29. /* My_lbs Characteristic 0 Client Characteristic Configuration Descriptor */
  30. static uint8_t s_my_lbsCccChar0[] = {UINT16_TO_BYTES(0x0000)};
  31. static const uint16_t s_my_lbsCccChar0Len = sizeof(s_my_lbsCccChar0);

  32. /* My_lbs Characteristic 1 Characteristic */
  33. static const uint8_t s_my_lbsChar1[] = {ATT_PROP_READ|ATT_PROP_WRITE_REQ, UINT16_TO_BYTES(MY_LBS_HDL_CHARVAL_1), UUID_MY_LBS_CHARACTERISTIC_1_LE};
  34. static const uint16_t s_my_lbsChar1Len = sizeof(s_my_lbsChar1);

  35. /* My_lbs Characteristic 1 Characteristic Value */
  36. static const uint8_t s_my_lbsUuidChar1[] = {UUID_MY_LBS_CHARACTERISTIC_1_LE};
  37. static uint8_t s_my_lbsChar1Val[3] = {0x00, 0xFF, 0x00};
  38. static uint16_t s_my_lbsChar1ValLen = sizeof(s_my_lbsChar1Val);

  39. /* Attribute list for My_lbs service */
  40. static GATTS_Attribute_T s_my_lbsList[] = {
  41.     /* Service Declaration */
  42.     {
  43.         (uint8_t *) g_gattUuidPrimSvc,
  44.         (uint8_t *) s_my_lbsSvcUuid,
  45.         (uint16_t *) & s_my_lbsSvcUuidLen,
  46.         sizeof (s_my_lbsSvcUuid),
  47.         0,
  48.         PERMISSION_READ
  49.     },
  50.     /* Characteristic 0 Declaration */
  51.     {
  52.         (uint8_t *) g_gattUuidChar,
  53.         (uint8_t *) s_my_lbsChar0,
  54.         (uint16_t *) & s_my_lbsChar0Len,
  55.         sizeof (s_my_lbsChar0),
  56.         0,
  57.         PERMISSION_READ
  58.     },
  59.     /* Characteristic 0 Value */
  60.     {
  61.         (uint8_t *) s_my_lbsUuidChar0,
  62.         (uint8_t *) s_my_lbsChar0Val,
  63.         (uint16_t *) & s_my_lbsChar0ValLen,
  64.         sizeof(s_my_lbsChar0Val),
  65.         SETTING_MANUAL_READ_RSP|SETTING_UUID_16,
  66.         PERMISSION_READ
  67.     },
  68.     /* Client Characteristic Configuration Descriptor */
  69.     {
  70.         (uint8_t *) g_descUuidCcc,
  71.         (uint8_t *) s_my_lbsCccChar0,
  72.         (uint16_t *) & s_my_lbsCccChar0Len,
  73.         sizeof (s_my_lbsCccChar0),
  74.         SETTING_CCCD,
  75.         PERMISSION_READ|PERMISSION_WRITE
  76.     },
  77.     /* Characteristic 1 Declaration */
  78.     {
  79.         (uint8_t *) g_gattUuidChar,
  80.         (uint8_t *) s_my_lbsChar1,
  81.         (uint16_t *) & s_my_lbsChar1Len,
  82.         sizeof (s_my_lbsChar1),
  83.         0,
  84.         PERMISSION_READ
  85.     },
  86.     /* Characteristic 1 Value */
  87.     {
  88.         (uint8_t *) s_my_lbsUuidChar1,
  89.         (uint8_t *) s_my_lbsChar1Val,
  90.         (uint16_t *) & s_my_lbsChar1ValLen,
  91.         sizeof(s_my_lbsChar1Val),
  92.         SETTING_UUID_16,
  93.         PERMISSION_READ|PERMISSION_WRITE
  94.     },
  95. };

  96. static const GATTS_CccdSetting_T s_my_lbsCccdSetting[] =
  97. {
  98.     {MY_LBS_HDL_CCCD_0, NOTIFICATION},
  99. };

  100. /* My_lbs Service structure */
  101. static GATTS_Service_T s_my_lbsSvc =
  102. {
  103.     NULL,
  104.     (GATTS_Attribute_T *) s_my_lbsList,
  105.     (GATTS_CccdSetting_T const *)s_my_lbsCccdSetting,
  106.     MY_LBS_START_HDL,
  107.     MY_LBS_END_HDL,
  108.     1
  109. };

  110. // *****************************************************************************
  111. // *****************************************************************************
  112. // Section: Functions
  113. // *****************************************************************************
  114. // *****************************************************************************

  115. uint16_t BLE_MY_LBS_Add()
  116. {
  117.     return GATTS_AddService(&s_my_lbsSvc, (MY_LBS_END_HDL - MY_LBS_START_HDL + 1));
  118. }



主函数依旧非常精简:
  1. int main ( void )
  2. {
  3.     /* Initialize all modules */
  4.     SYS_Initialize ( NULL );
  5.    
  6.     //SERCOM0_USART_Write((uint8_t *)"Hello Welcome to WBZ451 World\r\n",strlen("Hello Welcome to WBZ451 World\r\n"));
  7.     uart_**();
  8.     while ( true )
  9.     {
  10.         /* Maintain state machines of all polled MPLAB Harmony modules. */
  11.         SYS_Tasks ( );
  12.     }

  13.     /* Execution should not come here during normal operation */

  14.     return ( EXIT_FAILURE );
  15. }







特征值写服务:


特征值读服务:


订阅服务测试:



连接断开后重连功能:
  1. void APP_BleGapEvtHandler(BLE_GAP_Event_T *p_event)
  2. {
  3.     switch(p_event->eventId)
  4.     {
  5.         case BLE_GAP_EVT_CONNECTED:
  6.         {
  7.             /* TODO: implement your application code.*/
  8.             SERCOM0_USART_Write((uint8_t *)"Great! ^-^ Connect to GAP Center Device\r\n",strlen("Great! ^-^ Connect to GAP Center Device\r\n"));
  9.             ble_gap_connect_state = 1;
  10.             G_ConnHandle = p_event->eventField.evtConnect.connHandle;
  11.         }
  12.         break;

  13.         case BLE_GAP_EVT_DISCONNECTED:
  14.         {
  15.             /* TODO: implement your application code.*/
  16.             SERCOM0_USART_Write((uint8_t *)"Ah oh... :( Disconnect to GAP Center Device\r\n",strlen("Ah oh... :( Disconnect to GAP Center Device\r\n"));
  17.             ble_gap_connect_state = 0;
  18.             G_ConnHandle = 0;
  19.             
  20.             BLE_GAP_SetAdvEnable(0x01, 0x00);
  21.             SERCOM0_USART_Write((uint8_t *)"Let me try to GAP Center Device\r\n",strlen("Let me try to GAP Center Device\r\n"));
加入上期测试的OLED,可以显示BLE的连接状态:
BLE未连接的时候:



BLE连接成功的时候:



连接过程演示:






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
小明的同学 发表于 2023-12-24 18:23 | 显示全部楼层
MCC好像更好用了。
代码输出机 发表于 2023-12-25 11:36 | 显示全部楼层
这个可以用keil进行编译吗?
超能电子 发表于 2023-12-25 11:45 | 显示全部楼层
UUID不一样,是不是设备的名称也会不一样的?
 楼主| 南来之风 发表于 2023-12-25 12:00 | 显示全部楼层
代码输出机 发表于 2023-12-25 11:36
这个可以用keil进行编译吗?

暂时不可以,这款单片机用到了Microchip的MPLAB Harmony V3 框架,只能在官方IDE里面使用。
答案很长吧 发表于 2023-12-26 09:32 | 显示全部楼层
每包最大发送多少数据?
勇者无惧你和我 发表于 2023-12-26 16:22 | 显示全部楼层
这个是模块化的编程,如果要是可以使用在keil里面就好了。
亚瑟 发表于 2023-12-29 10:36 来自手机 | 显示全部楼层
南来之风 发表于 2023-12-25 12:00
暂时不可以,这款单片机用到了Microchip的MPLAB Harmony V3 框架,只能在官方IDE里面使用。 ...

官方的IDE好用吗
 楼主| 南来之风 发表于 2023-12-29 11:05 | 显示全部楼层
亚瑟 发表于 2023-12-29 10:36
官方的IDE好用吗

1. 偶尔会卡,然后从MPLABX IDE进入MCC(图形化配置工具)的时候需要异常多的耐心。。。等个数分钟是常有的事情。

2. 该有的功能都有,还是十分强大的。
起飞的龙D 发表于 2023-12-29 11:19 | 显示全部楼层
现在模块化编程是趋势,我也需要考虑一下,换一个思路了。
jflahdink09 发表于 2023-12-29 11:32 | 显示全部楼层
图形开发以及模块的开发,一直是一个很好的趋势的。
ynndmalh21 发表于 2023-12-29 12:13 | 显示全部楼层
图形化的开发,最大的优点就是直接可以生成代码。
真爱吴迪迪 发表于 2023-12-29 12:21 | 显示全部楼层
可视化的开发,还是便捷很多,也不会出现错误。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

69

主题

290

帖子

2

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