蓝牙BLE实用教程(转载)

欢迎使用 小书匠(xiaoshujiang)编辑器,您可以通过 设置 里的修改模板来改变新建文章的内容。

 

 

1.蓝牙BLE常见问答

Q: SMART READY 和 SMART 以及传统蓝牙之间是什么关系?

A:
1.Smart Ready 可以和 Smart Ready、传统蓝牙,以及 Smart 之间相互连接和通信。
2.传统蓝牙可以和 Smart Ready、传统蓝牙之间连接和通信
3.Smart 可以和 Smart、 Smart Ready 之间连接和通信

 

enter description here

2016-06-13_174221.jpg

 

Q: 双模(DUAL-MODE)和单模(SINGLE-MODE)有什么区别?

A: 都是蓝牙芯片,双模指的是同时支持1:BasicRate ( BR), 2:Bluetooth Low Energy ( BLE),如手机, PC 等采用的均是双模的蓝牙芯片。很多 android手机都表明支持蓝牙 4.0,其实很大部分只支持 Smart Ready 里的 BR。不是硬件的问题而是软件里还不支持LE。目前 Android4.3 系统才开始全面支持 BLE。iOS 设备对蓝牙 4.0 支持的最好,只要是 iPhone4S 和以后的设备均完美完全支持蓝牙 4.0。单模指的是只支持Bluetooth Low Energy ( 缩写:BLE或者LE),目前我们在使用的如Cypress的PSoC和PRoC系列,TI的CC2640等都是单模芯片,只支持Smart。BLE最主要特点是低功耗和低速率,网上关于蓝牙 4.0 一节纽扣电池能够使用一年均是针对 BLE 而言。

 

enter description here

1465828530504.jpg

 

Q: THERE IS A CONFUSION BETWEEN SERVER-CLIENT VS MASTER-SLAVE VS CENTRAL-PERIPHERAL. I NEED A SIMPLIFIED EXPLANATION.

A: These are the different roles defined at different layers of the BLE protocol stack.
Link Layer roles:

  • Master - Initiates a connection
  • Slave - Advertises and waits for a connection initiation

GATT roles:

  • Server - Has data
  • Client - Wants data

GAP roles:

  • Central - The primary device in a connection
  • Peripheral - The secondary device in a connection

The GAP Central is always a Link Layer Master, while the GAP Peripheral is always a Link Layer Slave.

Q: I AM NEW TO BLUETOOTH LOW ENERGY AND LOOKING FOR A QUICK BRINGUP. WHERE DO I START?

A: For a quick bringup with Bluetooth Low Energy, please refer to the following:

If you want to know about the technology, please refer to the videos available at BLE Developer Portal.

2.BLE协议栈介绍

2.1 ARCHITECTURE / CONFIGURATIONS

 

enter description here

1465808274415.jpg

 

  • Protocol stack consists of two main sections:
    绿色 Controller
    蓝色 Host
  • Controller Block Diagram

 

enter description here

2016-06-24_120236.jpg

 

  • Profiles and Application sit on top of the GAP and GATT layers of the host
  • In a “single-device solution” (or “single-chip solution”), the host, controller, profiles, and application are all implemented together on the same chip
  • In a “dual-device solution”, the BLE controller is implemented on one device, while the host,application, and profiles are implemented separately
  • In a “network processor”, the host and controller are implemented together, but the application and profiles sit on another device (such as a PC or external microcontroller)

2.2 PHYSICAL LAYER

  • RF Specifications
    • Operates in 2.4 GHz ISM band
    • GFSK modulation
    • 40 channels with 2 MHz spacing
      • 3 fixed advertisting channels for broadcasting, which avoid 802.11 interferance
      • 37 adaptively frequency hoppeddynamic data channels
  • Physical layer can be combined with standard Bluetooth RF in a dual-mode device
  • 2 MHz spacing allows for better adjacent channel rejection

2.3 BLE LINK LAYER

  • **Channl :**3 Advertising Channels and 37 Data Channels

 

enter description here

1465811675073.jpg

 

  • States and Network Topology

    • There are six possible Link Layer states of a BLE device:
      • Standby - device is not transmitting or receiving any data, and is not connected to any other device
      • Advertiser - periodically broadcasting advertisements
      • Scanner - actively looking for advertisers
      • Initiator - actively trying to initiate a connection with another device
      • Master - connected to another device as a master
      • Slave - connected to another device as a slave
    • BLE is a star topology network:
      • Master device “manages” the connection,and can be connected to multiple slaves
      • Slave device can only be connected to one master
  • Advertisement Events

    • During one “advertising event”, an advertisement packet is transmitted on each of the three advertising channels (37, 38, and 39)
  • Advertisement Intervals

    • The advertising device has an “advertising interval”, which is the minimum amount of time between two advertising events
    • Advertising Interval can be any amount of time between 20ms and 10.24s
  • Advertisement Types

    • Four types of advertisements:

      • Connectable undirected- any scanner device can initiate a connection with this advertiser
      • Connectable directed- only one specific device can initiate a connection with this advertiser
      • Non-connectable undirected- no devices can initiate a connection with this advertiser; primarily used for general broadcast of data
      • Discoverable undirected- any scanner device can request more information from the advertising device, but no devices can initiate a connection with it

       

      enter description here

      2016-06-13_190115.jpg

       

  • Scanning
    Active Scanning Packet Flow

 

Active

1465815899962.jpg

 

  • Connection Initiation
    Connection Request Packet Flow

 

enter description here

1465816179961.jpg

 

  • Connection Parameters

    • Channel Map- indicates which data channels are used during the connection
    • Connection Interval- multiple of 1.25ms in range of 7.5ms and 4.0s
    • Supervision Timeout- multiple of 10ms in the range of 100ms and 32.0s. Must be larger than: (1 + slaveLatency) * (ConnInterval)
    • Slave Latency- any value between 0 and 499, though it cannot exceed:((supervisionTimeout / connInterval) – 1)
  • Connection Events

    • All communications between two connected devices occur in “connection events”
    • Each event occurs on one data channel (channels 0-36), with the hop increment parameter determining the next channel for the next event
    • During each connection event, the master transmits first, and the slave responds 150us later
    • Master and slave can continue transmitting back and forth as many times as they want during a single connection event
    • Connection events occur even when one (or both) sides have no data to send (the exception to this is when slave latency is enabled; more information on next slide). This allows both devices to acknowledge that the other is still there and keeps the connection active.

     

    enter description here

    2016-06-13_192301.jpg

     

  • Slave Latency

 

enter description here

1465817247343.jpg

 

  • Connection Parameters Tradeoffs

    • Short connection interval:
      • Higher power consumption for both devices
      • Higher throughput in both directions
      • Shorter wait for data to be sent in either direction
    • Long connection interval:
      • Lower power consumption for both devices
      • Lower throughput in both directions
      • Longer wait for data to be sent in either direction
    • Low / Zero slave latency:
      • Higher power consumption for peripheral
      • Peripheral receives data sent from central device sooner
    • High slave latency:
      • Lower power consumption for peripheral during periods when it has no data to send
        to central device
      • Peripheral may not immediately receive data being sent from central device
  • Connection Update Request
    If the slave does not like the connection parameters (interval, slave latency, or supervision timeout), it can send a connection update request to the master

  • Connection Termination

    • A connection can be voluntarily terminated by either the master or the slave for any reason
    • Connection can also be terminated as a result of a supervision timeout
  • Direct Test Mode
    Allows a tester to directly control the device under test (DUT) in either Rx or Tx mode on any channel with any amount of data

 

enter description here

1465827463935.jpg

 

2.4 HOST/CONTROLLER INTERFACE (HCI)

  • Reused from standard Bluetooth specification,with new additional commands for low energy specific functions
  • Thin layer; doesn‟t perform any processing
  • In a dual-chip solution (with separate host and controller) allows for host to communicate with controller over a standard interface (UART,USB, SDIO, etc.)
  • Used internally by the CC2540 BLE protocol stack for communication between higher and lower layers
  • Also allows for custom “vendor-specific commands”. In the CC2540, vendor-specific commands can be used by an external source to directly interface with the entire stack or application. This is called a “Network Processor”

2.5 HOST -- LOGICAL LINK CONTROL AND ADAPTATION PROTOCOL (L2CAP)

  • Permits upper level protocols and applications to transmit and receive upper layer data packets up to 23 bytes in length
  • Provides channel management, allowing for logical channels between two endpoints,supported by the link layer
  • Connection Parameter Updates

2.6 SECURITY MANAGER PROTOCOL (SMP)

  • Performs authentication and key management
  • Uses AES-128 as the encryption algorithm for security procedures
  • Defines protocol to setup secure link
  • Works with GAP to manage relationships between devices:
    • Pairing – encryption between two devices once a connection has been established between them
    • Authentication – verification that a peer device can be trusted, providing protection against “Man-in-the-Middle” attacks
    • Bonding – long-term relationship between devices; security and identity information is saved for re-use next time the devices are connected

2.7 GENERIC ACCESS PROFILE (GAP)

  • Overview
    • Defines generic procedures for connection-related services:
      • Device Discovery
      • Link Establishment
      • Link Management
      • Link Termination
      • Initiation of security features
    • Many GAP functions correspond directly to the functions of the Link Layer in the controller
  • Profile Roles
    • The GAP layer works in one of four profile roles:
      • Broadcaster – an advertiser that is non-connectable
      • Observer – scans for advertisements, but cannot initiate connections.
      • Peripheral – an advertiser that is connectable and can operate as a slave in a single link layer connection.
      • Central – scans for advertisements and initiates connections; operates as a master in a single or multiple link layer connections.

 

enter description here

1465875931887.jpg

 

  • Profile Multi-Roles
    • The BLE specification allows for a few different possible multiple-role configurations:
      • Peripheral and Broadcaster
      • Peripheral and Observer
      • Central and Broadcaster
  • Discoverable Modes
    • GAP supports three different discoverable modes:
      • Non-discoverable Mode – No advertisements
      • Limited Discoverable Mode – Device advertises for a limited amount of time before returning to the standby state
      • General Discoverable Mode – Devices advertises continuously
  • Advertisement and Scan Response Data
    • GAP manages the data that is sent out in advertisement and scan response packets
  • Pairing
    • Pairing can be initiated by either the central or peripheral device
    • Based on the combination of the capabilities of the two devices, one of two methods of pairing will be used:
      • Passkey entry – one device will display a randomly generator passkey, while the other will require the user to input the passkey. This allows for an authenticated link (MITM protection)
      • “Just Works” – the pairing process completes without requiring a passkey to be entered. The link will not be authenticated, but is encrypted
    • During the pairing process, each device states whether it wants authentication to the other device
      • Each device also states it‟s input/output capabilities from among these options:
        • DisplayOnly – no way user can input anything into device, but it can output data
        • DisplayYesNo – user can input “yes” or “no” but nothing else; can also display data
        • KeyboardOnly – user can input a password or PIN, but no display
        • NoInputNoOutput – device has no means for user input, and has no display
        • KeyboardDisplay – device has a means for display as well as for input

2.8 ATTRIBUTE PROTOCOL (ATT)

  • Overview

    • ATT defines the over-the-air protocol for reading, writing, and discovering attributes
    • An attribute is a discrete value that has associated with it the following three properties:
      1. A handle (address)
      2. A type
      3. A set of permissions
  • Client / Server Architecture

    • Servers have data, Clients want to use this data
  • Attribute Table Example

    • Handle – The address of the attribute in the table
    • Type – Tells what the data represents; can be a UUID (universal unique identifier) assigned by the Bluetooth SIG, or a custom type
    • Permissions – Enforces if and how the attribute client can access the attribute‟s value

2.9 GENERIC ATTRIBUTE PROFILE (GATT)

  • Overview

    • Designed for use by the application or a profile, so that an attribute client can communicate with attribute server
    • GATT defines:
      • Procedures for using the attribute protocol (ATT) to discover, read, write, and obtain indications of these attributes
      • The grouping and relationship of characteristics within a service or profile
      • Procedures for configuring the broadcast of attributes
    • 通用属性配置文件 (gatt)
  • Client / Server Architecture

    • GATT specifies the structure in which profile data is exchanged
    • Same client server architecture as Attribute Protocol, except that data is encapsulated in “Services” and data is exposed in “Characteristic”
  • Profile Hierarchy

    • A profile is composed of one or more “services” necessary to fulfill a use-case
    • A service may contain certain attributes called “characteristic values”, which are values used by a service (example: in a temperature sensor, the attribute containing the temperature itself is the characteristic value)
    • A characteristic value must have a mandatory “characteristic declaration” attribute immediately before the value,containing the properties of the characteristic
    • Characteristics may also contain optional “descriptor” attributes, with fields such as a configuration or a description

 

enter description here

1465882745971.jpg

 

GATT database:

  1. const CYBLE_GATTS_DB_T cyBle_gattDB[0x10u] = { 
  2. { 0x0001u, 0x2800u /* Primary service */, 0x00000001u /* */, 0x0007u, {{0x1800u, NULL}} }, 
  3. { 0x0002u, 0x2803u /* Characteristic */, 0x00000201u /* rd */, 0x0003u, {{0x2A00u, NULL}} }, 
  4. { 0x0003u, 0x2A00u /* Device Name */, 0x00000201u /* rd */, 0x0003u, {{0x0009u, (void *)&cyBle_attValuesLen[0]}} }, 
  5. { 0x0004u, 0x2803u /* Characteristic */, 0x00000201u /* rd */, 0x0005u, {{0x2A01u, NULL}} }, 
  6. { 0x0005u, 0x2A01u /* Appearance */, 0x00000201u /* rd */, 0x0005u, {{0x0002u, (void *)&cyBle_attValuesLen[1]}} }, 
  7. { 0x0006u, 0x2803u /* Characteristic */, 0x00000201u /* rd */, 0x0007u, {{0x2A04u, NULL}} }, 
  8. { 0x0007u, 0x2A04u /* Peripheral Preferred Connection Par */, 0x00000201u /* rd */, 0x0007u, {{0x0008u, (void *)&cyBle_attValuesLen[2]}} }, 
  9. { 0x0008u, 0x2800u /* Primary service */, 0x00000001u /* */, 0x000Bu, {{0x1801u, NULL}} }, 
  10. { 0x0009u, 0x2803u /* Characteristic */, 0x00002201u /* rd,ind */, 0x000Bu, {{0x2A05u, NULL}} }, 
  11. { 0x000Au, 0x2A05u /* Service Changed */, 0x00002201u /* rd,ind */, 0x000Bu, {{0x0004u, (void *)&cyBle_attValuesLen[3]}} }, 
  12. { 0x000Bu, 0x2902u /* Client Characteristic Configuration */, 0x00000A04u /* rd,wr */, 0x000Bu, {{0x0002u, (void *)&cyBle_attValuesLen[4]}} }, 
  13. { 0x000Cu, 0x2800u /* Primary service */, 0x00000001u /* */, 0x0010u, {{0xCBBBu, NULL}} }, 
  14. { 0x000Du, 0x2803u /* Characteristic */, 0x00001A01u /* rd,wr,ntf */, 0x0010u, {{0xCBB1u, NULL}} }, 
  15. { 0x000Eu, 0xCBB1u /* Custom Buffer */, 0x00011A04u /* rd,wr,ntf */, 0x0010u, {{0x00C8u, (void *)&cyBle_attValuesLen[5]}} }, 
  16. { 0x000Fu, 0x2901u /* Custom Descriptor */, 0x00010001u /* */, 0x000Fu, {{0x001Cu, (void *)&cyBle_attValuesLen[6]}} }, 
  17. { 0x0010u, 0x2902u /* Client Characteristic Configuration */, 0x00010A04u /* rd,wr */, 0x0010u, {{0x0002u, (void *)&cyBle_attValuesLen[7]}} }, 
  18. }; 

Custom Uart Tx Service

Handleatt TypePermissionvalue
0x000Cu 0x2800(GATT Service UUID) read 0xCBBBu(2bytes)
0x000Du 0x2803u (GATT Characteristic UUID) read,write,notify 0xCBB1u(2bytes)
0x000Eu 0xCBB1u (Custom Buffer UUID) read,write,notify (200bytes)
0x000Fu 0x2901u (Custom Descriptor UUID) read (28bytes)
0x0010u 0x2902u (Client Characteristic Configuration UUID) read,write (2bytes)
  • Client Commands

    • When two BLE devices are in the connected state, the GATT client device can perform several different sub-procedures to communicate with the GATT server device:

      • Discover Characteristic by UUID – search the GATT server for all attributes with type that matches the specified UUID
      • Read Characteristic Value – read the value of the characteristic at the specified handle
      • Write Characteristic Value – write a new value to the characteristic at the specified handle
    • A GATT server device, when configured to do so, can send out messages to the GATT client device without being prompted:

      • Notification – The value a characteristic is sent from the server to the client without receiving a read request, and does not need to be acknowledged
      • Indication – The value a characteristic is sent from the server to the client without receiving a read request, but must be acknowledged before any further data can be sent

3. BLE应用实例

3.1 BLE转串口透传例程

由 BLE_Uart_Transmission_Collector01BLE_UART_Transmission_Server组成一对主从机程序,实现串口透传功能。

硬件组成:基于 CY8CKIT-042 BLE Pioneer Kit

3.2 程序介绍

3.2.1 程序流程

  • Psoc Creator 工程图示

 

enter description here

1465890989305.jpg

 

3.2.2 主机和从机的联系

 主机从机
profile customer profile customer profile
profile role Client(GATT Client) server(GATT Server)
GAP role Central Peripheral
  • 在主流程上主机和从机是类似的
  • 主机在BLE使能后,执行的 scan
  1. case CYBLE_EVT_STACK_ON: 
  2. printf("EVT_STACK_ON \r\n"); 
  3. goto start_scan; 
  4.  
  5. case CYBLE_EVT_GAP_DEVICE_DISCONNECTED: 
  6. BleConnected = 0; 
  7. printf("EVT_GAP_DEVICE_DISCONNECTED \r\n"); 
  8. goto start_scan; 
  9.  
  10. case CYBLE_EVT_TIMEOUT: 
  11. printf("EVT_TIMEOUT \r\n"); 
  12. start_scan: 
  13. if(BleConnected == 0) 

  14. StartScan(CYBLE_UUID_CUSTOMER_SERVICE);//CYBLE_UUID_CUSTOM_UART_TX_SERVICE 
  15. //Scanning_LED_Write(LED_ON);  
  16. ble_state = BLE_SCAN_STATE; 

  17.  
  18. break; 
  • 从机在BLE使能后,执行的是Advertisement
  1. case CYBLE_EVT_STACK_ON: 
  2. printf("EVT_STACK_ON \r\n"); 
  3. goto start_advert; 
  4. case CYBLE_EVT_GAP_DEVICE_DISCONNECTED: 
  5. //ble_state = BLE_DISCONNECTED_STATE; 
  6. case CYBLE_EVT_TIMEOUT: 
  7. start_advert: 
  8. /* Put the device into discoverable mode so that remote can search it. */ 
  9. StartAdvertisement(); 
  10. //Advertising_LED_Write(LED_ON); 
  11. ble_state = BLE_ADV_STATE; 
  12. test_ptr=0; 
  13. break; 
  • 下面是主机找到从机的关键函数,最终以 flag |= CONNECT; 为出口,并在scan结束后CYBLE_EVT_GAPC_SCAN_START_STOP,发起连接CyBle_GapcConnectDevice(&peerAddr[deviceN]), peerAddr[deviceN]就是找到的从机的mac地址。
  1. void ScanProgressEventHandler(CYBLE_GAPC_ADV_REPORT_T* eventParam) 

  2. uint8 newDevice = 0u, device = 0u; 
  3. uint8 i; 
  4. uint8 adStructPtr = 0u; 
  5. uint8 adTypePtr = 0u; 
  6. uint8 nextPtr = 0u; 
  7.  
  8. printf("SCAN_PROGRESS_RESULT: peerAddrType - %d, ", eventParam->peerAddrType); 
  9. printf("peerBdAddr - "); 
  10. for(newDevice = 1u, i = 0u; i < advDevices; i++) 

  11. if((memcmp(peerAddr[i].bdAddr, eventParam->peerBdAddr, CYBLE_GAP_BD_ADDR_SIZE) == 0)) /* same address */ 

  12. device = i; 
  13. printf("%x: ", device); 
  14. newDevice = 0u; 
  15. break; 


  16. if(newDevice != 0u) 

  17. if(advDevices < CYBLE_MAX_ADV_DEVICES) 

  18. memcpy(peerAddr[advDevices].bdAddr, eventParam->peerBdAddr, CYBLE_GAP_BD_ADDR_SIZE); 
  19. peerAddr[advDevices].type = eventParam->peerAddrType; 
  20. device = advDevices; 
  21. advDevices++; 
  22. printf("%x: ", device); 
  23.  


  24. for(i = CYBLE_GAP_BD_ADDR_SIZE; i > 0u; i--) 

  25. printf("%2.2x", eventParam->peerBdAddr[i-1]); 

  26. printf(", rssi - %d dBm, data - ", eventParam->rssi); 
  27.  
  28. /* Print and parse advertisement data and connect to device which has HRM */ 
  29. adStructPtr = 0u; 
  30. for(i = 0; i < eventParam->dataLen; i++) 

  31. printf("%2.2x ", eventParam->data[i]); 
  32.  
  33. if(i == adStructPtr) 

  34. adTypePtr = i + 1; 
  35. adStructPtr += eventParam->data[i] + 1; 
  36. nextPtr = 1; 

  37. else if(i == (adTypePtr + nextPtr)) 

  38. switch(eventParam->data[adTypePtr]) 

  39. case CYBLE_GAP_ADV_FLAGS: 
  40. break; 
  41.  
  42. case CYBLE_GAP_ADV_INCOMPL_16UUID: 
  43. case CYBLE_GAP_ADV_COMPL_16UUID: 
  44. if(serviceUuid == CyBle_Get16ByPtr(&(eventParam->data[i]))) 

  45. newDevice = 2; /* temporary use newDevice as a flag */ 

  46. else 

  47. nextPtr += 2; 

  48. break; 
  49.  
  50. default: 
  51. break; 



  52. printf("newDevice = %d \r\n",newDevice); 
  53.  
  54. if(2 == newDevice) 

  55. deviceN = device; 
  56. printf(" This device contains "); 
  57. switch(serviceUuid) 

  58. case CYBLE_UUID_HEART_RATE_SERVICE: 
  59. printf("Heart Rate Service \r\n"); 
  60. break; 
  61.  
  62. case CYBLE_UUID_GLUCOSE_SERVICE: 
  63. printf("Glucose Service \r\n"); 
  64. break; 
  65.  
  66. case CYBLE_UUID_BLOOD_PRESSURE_SERVICE: 
  67. printf("Blood Pressure Service \r\n"); 
  68. break; 
  69. case CYBLE_UUID_HIDS_SERVICE: 
  70. printf("HID Service \r\n"); 
  71. printf("Stop Scanning, waiting for Scanning event \r\n"); 
  72. flag |= CONNECT; 
  73. CyBle_GapcStopScan(); 
  74.  
  75. break; 
  76. /* other services */ 
  77. case 0x290A: 
  78. printf("Stop Scanning, waiting for Scanning event \r\n"); 
  79. flag |= CONNECT; 
  80. CyBle_GapcStopScan(); 
  81. break; 
  82.  
  83. case CYBLE_UUID_CUSTOMER_SERVICE: 
  84. printf("Customer Service \r\n"); 
  85. printf("Stop Scanning, waiting for Scanning event \r\n"); 
  86. flag |= CONNECT; 
  87. CyBle_GapcStopScan(); 
  88.  
  89. break; 
  90.  
  91. default: 
  92. printf("%x \r\n", serviceUuid); 
  93. break; 


  94. else 

  95. printf("\r\n"); 


3.2.3 初始化程序

以主机为例

  1. int main() 

  2. CYBLE_API_RESULT_T apiResult; 
  3.  
  4. CYBLE_LP_MODE_T lpMode; 
  5.  
  6. CyGlobalIntEnable; 
  7.  
  8. CommInit(); /* Start communication component */ 
  9. printf("BLE Uart Transmission Collector Example Project \r\n"); 
  10.  
  11. Scanning_LED_Write(LED_OFF); 
  12.  
  13. apiResult = CyBle_Start(AppCallBack); 
  14. if(apiResult != CYBLE_ERROR_OK) 

  15. printf("CyBle_Start API Error: %xd \r\n", apiResult); 

  16. else 

  17. printf("CyBle_Start API ok \r\n"); 

  18.  
  19. /* Enable the Interrupt component connected to interrupt */ 
  20. TC_CC_ISR_StartEx(InterruptHandler); 
  21.  
  22. /* Start the components */ 
  23. Timer_Start(); 

3.2.4 主程序

以主机为例

  1. while(1) 

  2. if(CyBle_GetState() != CYBLE_STATE_INITIALIZING) 

  3. /* Enter DeepSleep mode between connection intervals */ 
  4. lpMode = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP); 
  5. if(lpMode == CYBLE_BLESS_DEEPSLEEP)  

  6. /* Put the device into the Deep Sleep mode only when all debug information has been sent  
  7. if(UART_DEB_SpiUartGetTxBufferSize() == 0u) 

  8. CySysPmDeepSleep(); 

  9. else 

  10. CySysPmSleep(); 
  11. }*/ 
  12. CySysPmSleep(); 
  13. /* Handle scanning led blinking */ 
  14. HandleLEDs(ble_state); 

  15. HandleLEDs(ble_state); 

  16. /*********************************************************************** 
  17. * Wait for connection established with Central device 
  18. ***********************************************************************/ 
  19. if(CyBle_GetState() == CYBLE_STATE_CONNECTED) 

  20. /******************************************************************* 
  21. * Periodically measure a battery level and temperature and send  
  22. * results to the Client 
  23. *******************************************************************/  
  24. CommMonitorUart(); 
  25. CommMonitorBLE(); 
  26.  
  27.  
  28. #if 0 
  29. if(mainTimer != 0u) 

  30. mainTimer = 0u; 
  31.  
  32. if(storeBondingData == ENABLED) 

  33. cystatus retValue; 
  34. retValue = CyBle_StoreBondingData(0u); 
  35. printf("Store bonding data, status: %lx \r\n", retValue); 
  36. storeBondingData = DISABLED; 

  37.  

  38. #endif 
  39.  
  40.  

  41.  
  42.  
  43. /******************************************************************* 
  44. * Processes all pending BLE events in the stack 
  45. *******************************************************************/  
  46. CyBle_ProcessEvents(); 

  1. /////////////////////////////////////////////////////////////////////////////// 
  2. /// \brief MonitorBLE 
  3. /// \param none 
  4. /// \return none 
  5. /// \deprecated monitor ble send usd uart tx 
  6. /////////////////////////////////////////////////////////////////////////////// 
  7. void CommMonitorBLE(void) 

  8. if(uCommState.Bit.BLERxFinshed == ENABLED) 
  9. {  
  10. uCommState.Bit.BLERxFinshed = DISABLED; 
  11.  
  12. CommUartTxByte(); 
  13. // Then switch to Uart RX state 
  14. CommUartRxReady();  
  15. BLE_LED_OFF; 
  16.  
  17. // Then switch to RF RX state 
  18. CommBLERxReady(); 


  19.  
  1. /////////////////////////////////////////////////////////////////////////////// 
  2. /// \brief CommMonitorUart 
  3. /// \param none 
  4. /// \return none 
  5. /// \deprecated monitor uart send use ble 
  6. /////////////////////////////////////////////////////////////////////////////// 
  7. void CommMonitorUart(void) 

  8. CYBLE_API_RESULT_T apiResult; 
  9. CYBLE_GATT_HANDLE_VALUE_PAIR_T CustomNotificationhandle; 
  10.  
  11. if(uCommState.Bit.UartRxFinished == ENABLED) 

  12. uCommState.Bit.UartRxFinished = DISABLED; 
  13.  
  14. uCommState.Bit.BLERxRDY = DISABLED; 
  15.  
  16. CustomNotificationhandle.attrHandle = UART_TX_HANDLE; 
  17. CustomNotificationhandle.value.val = &Buffer[1]; 
  18. CustomNotificationhandle.value.len = Buffer[0]; 
  19. CustomNotificationhandle.value.actualLen = Buffer[0]; 
  20. #if 0 
  21.  
  22. /* Send notification to client using previously filled structure */ 
  23. apiResult = CyBle_GattsNotification(cyBle_connHandle, &CustomNotificationhandle); 
  24. #else 
  25.  
  26. apiResult = CyBle_GattcWriteWithoutResponse(cyBle_connHandle,&CustomNotificationhandle); 
  27. #endif 
  28. /* Send uart receive handle as attribute for read by central device */ 
  29. CyBle_GattsWriteAttributeValue(&CustomNotificationhandle,FALSE,&cyBle_connHandle,FALSE); 
  30. #ifdef test 
  31. if(apiResult != CYBLE_ERROR_OK) 

  32. printf("ble send Error: %x \r\n", apiResult); 

  33. else 

  34.  
  35. printf("ble send ok len: %x \r\n", CustomNotificationhandle.value.len); 

  36. #endif 
  37.  
  38. CommUartRxReady(); 
  39. Uart_LED_OFF; 
  40. // Then switch to RF RX state 
  41. CommBLERxReady(); 

  42.  

蓝牙的事件回调函数 接收蓝牙数据,产生uCommState.Bit.BLERxFinshed

  1. void AppCallBack(uint32 event, void* eventParam) 

  2. ... 
  3.  
  4. case CYBLE_EVT_GATTC_HANDLE_VALUE_NTF:  
  5. wrReqParam = (CYBLE_GATTS_WRITE_REQ_PARAM_T *) eventParam; 
  6. if(wrReqParam->handleValPair.attrHandle == UART_TX_HANDLE) 

  7. len = wrReqParam->handleValPair.value.len; 
  8. Buffer[0] = len; 
  9. memcpy(&Buffer[1],wrReqParam->handleValPair.value.val,len); 
  10. uCommState.Bit.BLERxFinshed = ENABLED; 
  11. //printf("len %d buf[0] %d \r\n", len,Buffer[0]); 

  12.  
  13. .... 

串口中断函数及定时器中断函数 接收串口数据,产生uCommState.Bit.UartRxFinished

  1. /******************************************************************************* 
  2. * Function Name: Transport_RX_ISR 
  3. ******************************************************************************** 

  4. * Summary: 
  5. * Handles the Interrupt Service Routine for the UART RX. 
  6. * Contains cmdRxState m/c to check for valid command reciept and triggers flag 

  7. * Parameters: 
  8. * NONE 

  9. * Return: 
  10. * NONE 

  11. * Theory: 
  12. * NONE 

  13. * Side Effects: 
  14. * Command bufffers and newCmdRxDoneFlag will be modified 

  15. * Note: 

  16. *******************************************************************************/ 
  17. CY_ISR(Transport_RX_ISR) 
  18.  
  19. /******************************************************************************* 
  20. * Function Name: Transport_Timer_ISR 
  21. ******************************************************************************** 

  22. * Summary: 
  23. * Handles the Interrupt Service Routine for the UART Timer. 
  24. * Resets RX command state m/c for every 10ms delay on RX byte 
  25. * If RX packet has started, there should not be more than 10ms delay between each RX byte with-in the packet 

  26. * Parameters: 
  27. * NONE 

  28. * Return: 
  29. * NONE 

  30. * Theory: 
  31. * NONE 

  32. * Side Effects: 
  33. * State m/c and other parameters will be reset 

  34. * Note: 

  35. *******************************************************************************/ 
  36. CY_ISR(Transport_Timer_ISR) 
  37.  

3.2.5 程序的调试

  • 从机的debug信息

 

enter description here

11.png

 

  1. * 1 AppCallBack :CYBLE_EVT_STACK_ON:StartAdvertisement() 
  2.  
  3. * 2 AppCallBack :CYBLE_EVT_GAPC_SCAN_START_STOP (SCAN_START) 
  4.  
  5. * 3&4 AppCallBack :CYBLE_EVT_GAP_DEVICE_CONNECTED 连接间隔 为 7.5ms 
  6.  
  7. * 5 AppCallBack :CYBLE_EVT_GATTC_XCHNG_MTU_RSP 变更成功 
  • 这个透传的程序目前我们用于无线打印数据,配合下图的虚拟示波器使用

 

enter description here

QQ图片20160615181218.jpg

 

 

enter description here

2次倒水的区别.jpg

 

3.3 其他精彩例程

PSoC 4 BLE is an easy-to-use, ARM® Cortex™-M0 based, single-chip solution which integrates programmable analog front ends, programmable digital peripherals, CapSense® technology for touch-sensing, and a Bluetooth® LE (Low Energy) or Bluetooth Smart radio. http://www.cypress.com/psoc4ble/ — Edit

4. BLE语音遥控器

4.1 概述

  • 利尔达BLE遥控器方案是基于蓝牙4.1的多功能智能遥控器解决方案,可以根据需求加入蓝牙语音,多指触控,体感控制等功能。本方案采样了Cypress的PRco系列,该系列是Cypress公司专门针对低功耗蓝牙设备推出的超低功耗,超高集成度的单芯片解决方案。
功能性能备注
BLE stack Bluetooth 4.2 single-mode device  
TX output power –18 dBm to +3 dBm  
RX sensitivity –89 dBm  
TX current 15.6 mA at 0 dBm  
RX current 16.4 mA  
CPU Cortex-M0 operating at up to 48 MHz
flash memory 128~256 KB  
SRAM memory 16~32 KB  
Other feature 1.3-µA Deep-Sleep mode  
  CapSense® Touch Sensing  
  Two-finger gestures  
  12-bit, 1-Msps SAR ADC with internal reference  
  OTA  

4.2 功能介绍

  • 语音部分 实现 256kbps的语音采样
    • I2S 数字语音接口

 

enter description here

1465891997716.jpg

 

  • ADC 模拟语音

 

enter description here

1465892054582.jpg

 

  • 触摸板

 

enter description here

1465892273795.jpg

 

  • 体感空鼠 6轴姿态补偿

 

enter description here

1465892334083.jpg

 

  • LED & IR 硬件实现呼吸灯 和 红外NEC编码

 

enter description here

1465892395048.jpg

 

  • 硬件实现行列键盘

 

enter description here

1465892431610.jpg

 

4.3 BLE HID PROFILE

  • Human Interface Device Service

 

enter description here

1465892594285.jpg

 

  • report map
    HID设备描述符 其中我们定义了两个私有的描述符,这个是我们用来传输语音的。因为BLE并不支持 A2DP(Advenced Audio Distribution Profile)蓝牙立体声协议,要传语音数据我们只能利用私有的通道。

 

enter description here

1465892713794.jpg

 

  • 在 android端提供相应的驱动来解析我们的语音数据,最终提供标准的PCM数据给到android的中间层。

 

enter description here
posted @ 2018-01-26 14:13  焦少  阅读(3359)  评论(0编辑  收藏  举报