[PSOC™] 【英飞凌 CY8CKIT-062S2-AI评测】-3- BLE Server测试

[复制链接]
15|0
南来之风 发表于 2025-10-29 21:14 | 显示全部楼层 |阅读模式


本文体验一下CY8CKIT-062S2-AI开发板的BLE功能。


初始化BLE协议栈:
  1.     wiced_result = wiced_bt_stack_init(app_bt_management_callback,
  2.                                  &wiced_bt_cfg_settings);

  3.     /* Check if stack initialization was successful */
  4.     if (WICED_BT_SUCCESS == wiced_result) {
  5.         printf("Bluetooth Stack Initialization Successful \n");
  6.     } else {
  7.         printf("Bluetooth Stack Initialization failed!!\n");
  8.     }


注册GATT事件回调函数:
  1. wiced_bt_gatt_status_t
  2. app_bt_gatt_event_callback(wiced_bt_gatt_evt_t event,
  3.                            wiced_bt_gatt_event_data_t *p_event_data)
  4. {

  5.     wiced_bt_gatt_status_t gatt_status = WICED_BT_GATT_ERROR;
  6.     wiced_bt_gatt_attribute_request_t *p_attr_req = NULL;

  7.     uint16_t error_handle = 0;

  8.     switch (event)
  9.     {

  10.     case GATT_CONNECTION_STATUS_EVT:
  11.         gatt_status = app_gatt_connect_handler(&p_event_data->connection_status);
  12.         break;

  13.     case GATT_ATTRIBUTE_REQUEST_EVT:
  14.         p_attr_req = &p_event_data->attribute_request;
  15.         gatt_status = app_gatts_attr_req_handler(p_attr_req,
  16.                                                  &error_handle);

  17.         if(gatt_status != WICED_BT_GATT_SUCCESS)
  18.         {
  19.            wiced_bt_gatt_server_send_error_rsp(p_attr_req->conn_id,
  20.                                                p_attr_req->opcode,
  21.                                                error_handle,
  22.                                                gatt_status);
  23.         }

  24.         break;

  25.     case GATT_GET_RESPONSE_BUFFER_EVT:
  26.     {
  27.         wiced_bt_gatt_buffer_request_t *p_buf_req = &p_event_data->buffer_request;
  28.         printf("len_req %d \n", p_buf_req->len_requested);
  29.         p_buf_req->buffer.p_app_rsp_buffer = app_alloc_buffer(p_buf_req->len_requested);
  30.         p_buf_req->buffer.p_app_ctxt = (void *)app_free_buffer;
  31.         gatt_status = WICED_BT_GATT_SUCCESS;
  32.     }
  33.         break;

  34.     case GATT_APP_BUFFER_TRANSMITTED_EVT:
  35.     {
  36.         pfn_free_buffer_t pfn_free;
  37.         pfn_free = (pfn_free_buffer_t)p_event_data->buffer_xmitted.p_app_ctxt;
  38.         /* If the buffer is dynamic, the context will point to a function to
  39.          * free it.
  40.          */
  41.         if (pfn_free)
  42.         {
  43.             pfn_free(p_event_data->buffer_xmitted.p_app_data);
  44.         }
  45.         gatt_status = WICED_BT_GATT_SUCCESS;
  46.     }
  47.        break;

  48.     default:
  49.         printf("Unhandled GATT Event %d", event);
  50.         break;

  51.     }

  52.     return gatt_status;
  53. }



串口打印初始化:
  1.     cy_retarget_io_init(CYBSP_DEBUG_UART_TX,
  2.                         CYBSP_DEBUG_UART_RX,
  3.                         CY_RETARGET_IO_BAUDRATE);


在BLE client开启notfiy功能后,处理相应的数据发送任务:
  1. void ess_task(void *pvParam)
  2. {
  3.     while(true)
  4.     {
  5.         ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  6.         /* Varying temperature by 1 degree on every timeout for simulation */
  7.         if (0 == alternating_flag)
  8.         {
  9.             temperature += DELTA_TEMPERATURE;
  10.             if (MAX_TEMPERATURE_LIMIT <= temperature)
  11.             {
  12.                 alternating_flag = 1;
  13.             }
  14.         }
  15.         else if ((1 == alternating_flag))
  16.         {
  17.             temperature -= DELTA_TEMPERATURE;
  18.             if (MIN_TEMPERATURE_LIMIT >= temperature)
  19.             {
  20.                 alternating_flag = 0;
  21.             }
  22.         }

  23.         printf("\nTemperature (in degree Celsius) \t\t%d.%02d\n",
  24.                 (temperature / 100), ABS(temperature % 100));

  25.         /*
  26.         * app_ess_temperature value is set both for read operation and
  27.         * notify operation.
  28.         */
  29.         app_ess_temperature[0] = (uint8_t)(temperature & 0xff);
  30.         app_ess_temperature[1] = (uint8_t)((temperature >> 8) & 0xff);

  31.         /* To check that connection is up and
  32.         * client is registered to receive notifications
  33.         * to send temperature data in Little Endian Format
  34.         * as per BT SIG's ESS Specification
  35.         */

  36.         if (IS_NOTIFIABLE (app_bt_conn_id, app_ess_temperature_client_char_config[0]) == 0)
  37.         {
  38.             if(!app_bt_conn_id)
  39.             {
  40.                 printf("This device is not connected to a central device\n");
  41.             }else{
  42.                 printf("This device is connected to a central device but\n"
  43.                         "GATT client notifications are not enabled\n");
  44.             }
  45.         }
  46.         else
  47.         {
  48.             wiced_bt_gatt_status_t gatt_status;

  49.             /*
  50.             * Sending notification, set the pv_app_context to NULL, since the
  51.             * data 'app_ess_temperature' is not to be freed
  52.             */
  53.             gatt_status = wiced_bt_gatt_server_send_notification(app_bt_conn_id,
  54.                                                                     HDLC_ESS_TEMPERATURE_VALUE,
  55.                                                                     app_ess_temperature_len,
  56.                                                                     app_ess_temperature,
  57.                                                                     NULL);

  58.             printf("Sent notification status 0x%x\n", gatt_status);

  59.         }
  60.     }
  61. }


开启BLE广播:
  1.     app_start_advertisement();


编译,进入调试:
  1. Started by GNU MCU Eclipse
  2. Open On-Chip Debugger 0.12.0+dev-5.7.0.3672 (2025-03-04-03:08)
  3. Licensed under GNU GPL v2
  4. For bug reports, read
  5.         http://openocd.org/doc/doxygen/bugs.html
  6. adapter speed: 2000 kHz
  7. adapter srst delay: 25
  8. adapter srst pulse_width: 25
  9. ** Auto-acquire enabled, use "set ENABLE_ACQUIRE 0" to disable
  10. Info : Using CMSIS-flash algorithms 'CY8C6xxA_SMIF' for bank 'psoc6_smif0_cm0' (footprint 17632 bytes)
  11. Info : CMSIS-flash: ELF path: ../flm/cypress/cat1a/CY8C6xxA_SMIF.FLM
  12. Info : CMSIS-flash: Address range:     0x18000000-0x1FFFFFFF
  13. Info : CMSIS-flash: Program page size: 0x00001000 bytes
  14. Info : CMSIS-flash: Erase sector size: 0x00040000 bytes, unified
  15. Warn : SFlash programming allowed for regions: USER, TOC, KEY
  16. Info : Using CMSIS-DAPv2 interface with VID:PID=0x04b4:0xf155, serial=16160E5A012D2400
  17. Info : CMSIS-DAP: SWD supported
  18. Info : CMSIS-DAP: Atomic commands supported
  19. Info : CMSIS-DAP: FW Version = 2.0.0
  20. Info : CMSIS-DAP: Interface Initialised (SWD)
  21. Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 1 TDO = 1 nTRST = 0 nRESET = 1
  22. Info : CMSIS-DAP: Interface ready
  23. Info : KitProg3: FW version: 2.80.1506
  24. Info : KitProg3: Pipelined transfers enabled
  25. Info : KitProg3: Asynchronous USB transfers enabled
  26. Info : VTarget = 3.311 V
  27. Info : kitprog3: acquiring device in Test Mode using 'PSOC 6' target and 'xres' reset...
  28. Info : clock speed 2000 kHz
  29. Info : SWD DPIDR 0x6ba02477
  30. Info : [psoc6.cpu.cm0] Cortex-M0+ r0p1 processor detected
  31. Info : [psoc6.cpu.cm0] target has 4 breakpoints, 2 watchpoints
  32. ***************************************
  33. ** Silicon: 0xE453, Family: 0x102, Rev.: 0x12 (A1)
  34. ** Detected Device: CY8C624ABZI-S2D44
  35. ** Detected Main Flash size, kb: 2048
  36. ** Flash Boot version: 3.1.0.378
  37. ** SFlash version: 0x47530
  38. ** Chip Protection: NORMAL
  39. ***************************************
  40. Info : [psoc6.cpu.cm0] Examination succeed
  41. Info : [psoc6.cpu.cm4] Cortex-M4 r0p1 processor detected
  42. Info : [psoc6.cpu.cm4] target has 6 breakpoints, 4 watchpoints
  43. Info : [psoc6.cpu.cm4] Examination succeed
  44. Info : starting gdb server for psoc6.cpu.cm0 on 3332
  45. Info : Listening on port 3332 for gdb connections
  46. Info : starting gdb server for psoc6.cpu.cm4 on 3333
  47. Info : Listening on port 3333 for gdb connections
  48. Info : SWD DPIDR 0x6ba02477
  49. Info : kitprog3: acquiring device in Test Mode using 'PSOC 6' target and 'xres' reset...
  50. [psoc6.cpu.cm0] halted due to debug-request, current mode: Thread
  51. xPSR: 0x41000000 pc: 0x00000190 msp: 0x080ff800
  52. ** Device acquired successfully
  53. ** psoc6.cpu.cm4: Ran after reset and before halt...
  54. [psoc6.cpu.cm4] halted due to debug-request, current mode: Thread
  55. xPSR: 0x01000000 pc: 0x0000012a msp: 0x080ff800
  56. Started by GNU MCU Eclipse
  57. Info : Listening on port 6666 for tcl connections
  58. Info : Listening on port 4444 for telnet connections
  59. Info : accepting 'gdb' connection on tcp/3333
  60. Info : New GDB Connection: 1, Target psoc6.cpu.cm4, state: halted
  61. Warn : Prefer GDB command "target extended-remote :3333" instead of "target remote :3333"
  62. semihosting is enabled
  63. Info : Auto-detected RTOS: FreeRTOS
  64. Info : Auto-detected RTOS: FreeRTOS
  65. Verifying region (0x10000000,      6352)... Match
  66. Verifying region (0x10002000,    165228)... Mismatch
  67. Info : Data mismatch, proceeding with flash programming
  68. Info : Flash write discontinued at 0x100018d0, next section at 0x10002000
  69. Info : Padding image section 0 at 0x100018d0 with 304 bytes (bank write end alignment)

  70. [100%] [################################] [ Erasing     ]

  71. [100%] [################################] [ Programming ]
  72. Info : Padding image section 1 at 0x1002a56c with 148 bytes (bank write end alignment)

  73. [ 34%] [##########                      ] [ Erasing     ]
  74. [ 37%] [###########                     ] [ Erasing     ]
  75. [ 41%] [#############                   ] [ Erasing     ]
  76. [ 44%] [##############                  ] [ Erasing     ]
  77. [ 48%] [###############                 ] [ Erasing     ]
  78. [ 53%] [################                ] [ Erasing     ]
  79. [ 55%] [#################               ] [ Erasing     ]
  80. [ 60%] [###################             ] [ Erasing     ]
  81. [ 67%] [#####################           ] [ Erasing     ]
  82. [ 69%] [######################          ] [ Erasing     ]
  83. [ 74%] [#######################         ] [ Erasing     ]
  84. [ 76%] [########################        ] [ Erasing     ]
  85. [ 81%] [#########################       ] [ Erasing     ]
  86. [ 83%] [##########################      ] [ Erasing     ]
  87. [ 86%] [###########################     ] [ Erasing     ]
  88. [ 88%] [############################    ] [ Erasing     ]
  89. [ 95%] [##############################  ] [ Erasing     ]
  90. [ 97%] [############################### ] [ Erasing     ]
  91. [100%] [################################] [ Erasing     ]

  92. [ 17%] [#####                           ] [ Programming ]
  93. [ 19%] [######                          ] [ Programming ]
  94. [ 23%] [#######                         ] [ Programming ]
  95. [ 26%] [########                        ] [ Programming ]
  96. [ 29%] [#########                       ] [ Programming ]
  97. [ 34%] [##########                      ] [ Programming ]
  98. [ 36%] [###########                     ] [ Programming ]
  99. [ 43%] [#############                   ] [ Programming ]
  100. [ 53%] [################                ] [ Programming ]
  101. [ 54%] [#################               ] [ Programming ]
  102. [ 63%] [####################            ] [ Programming ]
  103. [ 72%] [#######################         ] [ Programming ]
  104. [ 81%] [#########################       ] [ Programming ]
  105. [ 91%] [#############################   ] [ Programming ]
  106. [100%] [################################] [ Programming ]
  107. Info : SWD DPIDR 0x6ba02477
  108. Info : kitprog3: acquiring device in Test Mode using 'PSOC 6' target and 'xres' reset...
  109. [psoc6.cpu.cm0] halted due to debug-request, current mode: Thread
  110. xPSR: 0x41000000 pc: 0x00000190 msp: 0x080ff800
  111. ** Device acquired successfully
  112. ** psoc6.cpu.cm4: Ran after reset and before halt...
  113. [psoc6.cpu.cm4] halted due to debug-request, current mode: Thread
  114. xPSR: 0x01000000 pc: 0x0000012a msp: 0x080ff800, semihosting
  115. Info : SWD DPIDR 0x6ba02477
  116. [psoc6.cpu.cm4] halted due to debug-request, current mode: Thread
  117. xPSR: 0x01000000 pc: 0x100066b2 msp: 0x080ff7b0, semihosting
  118. Info : psoc6.cpu.cm4: Waiting up to 10.0 sec for valid Vector Table address...
  119. Info : psoc6.cpu.cm4: Vector Table found at 0x10002000
  120. Info : psoc6.cpu.cm4: bkpt @0x10002371, issuing SYSRESETREQ
  121. Info : SWD DPIDR 0x6ba02477
  122. Info : [psoc6.cpu.cm4] Examination succeed
  123. [psoc6.cpu.cm4] halted due to debug-request, current mode: Thread
  124. xPSR: 0x61000000 pc: 0x10002370 msp: 0x080ff800, semihosting
  125. Info : [psoc6.cpu.cm0] external reset detected
  126. ===== arm v7m registers
  127. (0) r0 (/32): 0x08002a28
  128. (1) r1 (/32): 0x00000000
  129. (2) r2 (/32): 0x08000000
  130. (3) r3 (/32): 0x00000000
  131. (4) r4 (/32): 0x10029e14
  132. (5) r5 (/32): 0x10029e14
  133. (6) r6 (/32): 0x04c11db7
  134. (7) r7 (/32): 0x08027fff
  135. (8) r8 (/32): 0xa5a5a5a5
  136. (9) r9 (/32): 0xa5a5a5a5
  137. (10) r10 (/32): 0xa5a5a5a5
  138. (11) r11 (/32): 0xa5a5a5a5
  139. (12) r12 (/32): 0x080ff7e0
  140. (13) sp (/32): 0x080ff800
  141. (14) lr (/32): 0x100023c1
  142. (15) pc (/32): 0x10003438
  143. (16) xpsr (/32): 0x61000000
  144. (17) msp (/32): 0x080ff800
  145. (18) psp (/32): 0x080165d0
  146. (20) primask (/1): 0x01
  147. (21) basepri (/8): 0x00
  148. (22) faultmask (/1): 0x00
  149. (23) control (/3): 0x00
  150. (42) d0 (/64): 0xbe83b103bef15aea
  151. (43) d1 (/64): 0x3de2fd523c45f6d0
  152. (44) d2 (/64): 0x3bee7fa03e86da6a
  153. (45) d3 (/64): 0xbd727f443c8351f0
  154. (46) d4 (/64): 0xc012ddaac00be208
  155. (47) d5 (/64): 0x37f5000037dc0000
  156. (48) d6 (/64): 0x3db4a8c93d5b2f20
  157. (49) d7 (/64): 0x3eb67b013f000000
  158. (50) d8 (/64): 0xdfffbf6ff3dbfebf
  159. (51) d9 (/64): 0x7ea9eeefff7f74ff
  160. (52) d10 (/64): 0xbffbffbf7e7ffeed
  161. (53) d11 (/64): 0xfefafeefe7feffbf
  162. (54) d12 (/64): 0xffbffbeffffbffaf
  163. (55) d13 (/64): 0xffffff6ffffbfeaf
  164. (56) d14 (/64): 0xfffbfbff77ffbfff
  165. (57) d15 (/64): 0xeffeffdffbfcf7ff
  166. (58) fpscr (/32): 0x00000000
  167. ===== Cortex-M DWT registers



Android上安装英飞凌BLE调试工具:AIROC™ Bluetooth® Connect
进入App后,扫描BLE外设(Server):目标设备是Thermistor


点击设备名称,发起连接。点击“Enviornment Sensing service”

点击Yes:



继续点击:


点击Notify:



实时温度数据正常被推送:


串口输出结果:
  1. Temperature (in degree Celsius)                 21.00
  2. Notfication send complete
  3. Sent notification status 0x0

  4. Temperature (in degree Celsius)                 22.00
  5. Notfication send complete
  6. Sent notification status 0x0

  7. Temperature (in degree Celsius)                 23.00
  8. Notfication send complete
  9. Sent notification status 0x0

  10. Temperature (in degree Celsius)                 24.00
  11. Notfication send complete
  12. Sent notification status 0x0

  13. Temperature (in degree Celsius)                 25.00
  14. Notfication send complete
  15. Sent notification status 0x0


本帖子中包含更多资源

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

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

72

主题

297

帖子

2

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