1 /***************************************************************************//**
2 * \file cy_ble.h
3 * \version 3.60
4 *
5 * \brief
6 *  Contains the prototypes and constants used in the PSoC 6 BLE Middleware.
7 *
8 ********************************************************************************
9 * \copyright
10 * Copyright 2017-2021, Cypress Semiconductor Corporation.  All rights reserved.
11 * You may use this file only in accordance with the license, terms, conditions,
12 * disclaimers, and limitations in the end user license agreement accompanying
13 * the software package with which this file was provided.
14 *******************************************************************************/
15 #ifndef CY_BLE_H
16 #define CY_BLE_H
17 
18 
19 #include <string.h>
20 #include <stdint.h>
21 #include <stdbool.h>
22 #include <cy_device_headers.h>
23 
24 #include "cy_syslib.h"
25 #include "cy_sysint.h"
26 #include "cy_syspm.h"
27 #include "cy_scb_uart.h"
28 
29 // #include "cy_ble_gatt.h"
30 // #include "cy_ble_gap.h"
31 
32 #if defined(CY_IP_MXBLESS)
33 
34 
35 /***************************************
36 * BLE Stack includes
37 ***************************************/
38 
39 #include "cy_ble_stack.h"
40 // #include "cy_ble_stack_host_main.h"
41 // #include "cy_ble_stack_host_error.h"
42 // #include "cy_ble_stack_gap.h"
43 // #include "cy_ble_stack_gap_central.h"
44 // #include "cy_ble_stack_gap_peripheral.h"
45 // #include "cy_ble_stack_gatt_server.h"
46 // #include "cy_ble_stack_gatt_client.h"
47 // #include "cy_ble_stack_gatt_db.h"
48 // #include "cy_ble_stack_l2cap.h"
49 
50 /* The C binding of definitions if building with the C++ compiler */
51 #ifdef __cplusplus
52 extern "C" {
53 #endif /* __cplusplus */
54 
55 
56 /*******************************************************************************
57 * API Constants
58 *******************************************************************************/
59 
60 /** Library major version */
61 #define CY_BLE_MW_VERSION_MAJOR              (3)
62 
63 /** Library minor version */
64 #define CY_BLE_MW_VERSION_MINOR              (60)
65 
66 #define CY_BLE_M0S8BLESS_VERSION             (4u)
67 #define CY_BLE_SEMA                          (3u)
68 
69 /* Align the buffer size value to 4 */
70 #define CY_BLE_ALIGN_TO_4(x)                 ((((x) & 3u) == 0u) ? (x) : (((x) - ((x) & 3u)) + 4u))
71 
72 #define CY_BLE_MTU_MIN_VALUE                 (23u)
73 #define CY_BLE_MTU_MIN_BUFF_NUM              (3u)
74 
75 /* Package type constants */
76 #define CY_BLE_PACKAGE_TYPE_MASK             (0x00FFu)
77 #define CY_BLE_PACKAGE_PIN_NUM_MASK          (0xFF00u)
78 #define CY_BLE_PACKAGE_PIN_NUM_OFFSET        (8u)
79 
80 
81 /* Support Host side only on CM4 core */
82 #if !defined(CY_BLE_LIB_HOST_CORE)
83     #if defined(COMPONENT_BLESS_HOST) || defined(COMPONENT_BLESS_HOST_IPC)
84         #define CY_BLE_LIB_HOST_CORE                 (1)
85     #else
86         #define CY_BLE_LIB_HOST_CORE                 (0)
87     #endif /* defined(COMPONENT_BLESS_HOST) || defined(COMPONENT_BLESS_HOST_IPC) */
88 #endif /* !defined(CY_BLE_LIB_HOST_CORE) */
89 
90 /*
91 * These defines are obsolete and kept for backward compatibility only.
92 * They will be removed in the future versions.
93 */
94 #define CY_BLE_MIDDLEWARE_VERSION_MAJOR      (CY_BLE_MW_VERSION_MAJOR)
95 #define CY_BLE_MIDDLEWARE_VERSION_MINOR      (CY_BLE_MW_VERSION_MINOR)
96 
97 
98 /*******************************************************************************
99 * Data Types
100 *******************************************************************************/
101 
102 /**
103  * \addtogroup group_ble_common_api_definitions
104  * \{
105  */
106 /** Interrupt callback feature */
107 typedef enum
108 {
109     /** Interrupt callback feature is disabled */
110     CY_BLE_INTR_CALLBACK_NONE                        = (0x00),
111 
112     /** Executed on every trigger of BLESS interrupt. */
113     CY_BLE_INTR_CALLBACK_BLESS_STACK_ISR             = (0x01UL << 1u),
114 
115     /** Executed when the BLESS exits BLESS deep sleep mode and enters BLESS active mode.
116      *  BLESS deep sleep exit can be triggered automatically by link layer hardware
117      *  or by different BLE_PDL data transfer APIs that need BLESS to be active.
118      */
119     CY_BLE_INTR_CALLBACK_BLESS_INTR_STAT_DSM_EXITED  = (0x01UL << 2u),
120 
121     /** Executed when the BLESS connection engine in slave mode detects a BLE packet
122      *  that matches its access address.
123      */
124     CY_BLE_INTR_CALLBACK_BLELL_CONN_EXT_INTR_EARLY   = (0x01UL << 3u),
125 
126     /** Executed when the BLESS connection engine receives a non-empty packet from
127      *  the peer device.
128      */
129     CY_BLE_INTR_CALLBACK_BLELL_CONN_INTR_CE_RX       = (0x01UL << 4u),
130 
131     /** Executed when the BLESS connection engine receives an ACK packet from the peer
132      *  device for the previously transmitted packet.
133      */
134     CY_BLE_INTR_CALLBACK_BLELL_CONN_INTR_CE_TX_ACK   = (0x01UL << 5u),
135 
136     /** Executed when the BLESS connection engine closes the connection event.
137      *  This interrupt will be executed on every connection interval for a connection,
138      *  irrespective of data tx/rx state.
139      */
140     CY_BLE_INTR_CALLBACK_BLELL_CONN_INTR_CLOSE_CE    = (0x01UL << 6u),
141 
142     /** Executed when the BLESS enters deep sleep mode. User call to Cy_SysPm_DeepSleep API
143      *  will trigger BLESS deep sleep entry sequence.
144      */
145     CY_BLE_INTR_CALLBACK_BLESS_INTR_STAT_DSM_ENTERED = (0x01UL << 7u),
146 
147     /** Executed when the BLESS scan engine receives an advertisement packet from the
148      *  peer device
149      */
150     CY_BLE_INTR_CALLBACK_BLELL_SCAN_INTR_ADV_RX      =  (0x01UL << 8u),
151 
152     /** Executed when the BLESS scan engine receives a scan response packet from the peer
153      *  device in response to a scan request from the scanner.
154      */
155     CY_BLE_INTR_CALLBACK_BLELL_SCAN_INTR_SCAN_RSP_RX = (0x01UL << 9u),
156 
157     /** Executed when the BLESS advertisement engine receives a connection request from
158      *  the peer central device
159      */
160     CY_BLE_INTR_CALLBACK_BLELL_ADV_INTR_CONN_REQ_RX  = (0x01UL << 10UL),
161 } cy_en_ble_interrupt_callback_feature_t;
162 /** \} group_ble_common_api_definitions */
163 
164 /**
165  * \addtogroup group_ble_common_api_gap_definitions
166  * \{
167  */
168 /** The event callback function prototype to receive events from the PSoC 6 BLE Middleware */
169 typedef void (* cy_ble_callback_t) (uint32_t eventCode, void *eventParam);
170 
171 /** Application Notify Callback */
172 typedef void (* cy_ble_app_notify_callback_t) (void);
173 /** \} */
174 
175 typedef void (* cy_ble_intr_callback_t) (uint32_t interruptType);
176 
177 /**
178 * Pointer to function initializes services.
179 */
180 typedef void (* cy_fn_service_init_t)(void);
181 
182 /** \cond IGNORE */
183 /* Interrupt notification feature structure */
184 typedef struct
185 {
186     uint8_t  clientID;
187     uint8_t  pktType;
188     uint16_t intrRelMask;
189 
190     bool     configFlag;        /* Flag indicates that Controller receive
191                                  * address of structure */
192     uint32_t mask;              /* Mask of interrupts   */
193     uint32_t data;              /* Detected interrupt type */
194 
195     uint8_t userIpcChan;        /* IPC channel. Used only in BLE dual CPU mode */
196     uint8_t userIpcIntr;        /* IPC Interrupt structure. Used only in BLE dual CPU mode */
197     uint8_t userIpcIntrPrior;   /* IPC Interrupt priority. Used only in BLE dual CPU mode */
198 
199 } cy_stc_ble_intr_notify_t;
200 /** \endcond */
201 
202 /** Structure to store bonding data */
203 typedef struct
204 {
205     /** Bonding data storage */
206     uint8_t *stackFlashPtr;     /** Pointer to bonding data storage in auxiliary flash */
207     uint32_t stackFlashSize;    /** Buffer size in auxiliary flash */
208 
209     /** CCCD data storage */
210     uint8_t *cccdFlashPtr;      /** Pointer to CCCD values storage in auxiliary flash */
211     uint8_t *cccdRamPtr;        /** Pointer to CCCD values storage in RAM */
212     uint8_t  cccdCount;         /** Number of CCCD */
213 
214 } cy_stc_ble_flash_storage_t;
215 
216 /** BLE Configuration parameters structure */
217 typedef struct
218 {
219     /** Silicon Device Address Enabled */
220     bool                      siliconDeviceAddressEn;
221 
222     /** Using external buffer for Prepare Write Request */
223     bool                      gattPrepareWriteExtBuffEn;
224 
225     /** The parameter to enable the configuration of the
226      *  L2CAP logical channels */
227     bool                      l2capEn;
228 
229     /** BLESS Power reflecting power level values supported
230      *  by BLESS radio (for advertising channel) */
231     cy_en_ble_bless_pwr_lvl_t txPowerLevelAdv;
232 
233     /** BLESS power reflecting power level values supported
234      *  by BLESS radio (for connection channel) */
235     cy_en_ble_bless_pwr_lvl_t txPowerLevelConn;
236 
237 #if CY_BLE_LIB_HOST_CORE
238     /** IO capability */
239     cy_en_ble_gap_iocap_t     securityIoCapability;
240 #endif
241 
242     /** L2CAP MTU Size */
243     uint16_t                  l2capMtuSize;
244 
245     /** L2CAP MPS Size */
246     uint16_t                  l2capMps;
247 
248     /** The number of L2CAP Logical channels  */
249     uint16_t                  l2capChanCount;
250 
251     /** Number of L2CAP PSMs */
252     uint16_t                  l2capPsmCount;
253 
254     /** Size of prepare write queue buffer */
255     uint16_t                  gattPrepareWriteQueueSize;
256 
257     /** Length of attribute value */
258     uint16_t                  totalAttrValueLength;
259 
260     /** The GATT Maximum attribute length */
261     uint16_t                  gattAttrMaxLen;
262 
263     /** Index count for GATT DB */
264     uint16_t                  gattDbIndexCount;
265 
266     /** The MTU Size */
267     uint16_t                  mtuSize;
268 
269     /** The GAP Role */
270     uint8_t                   gapRole;
271 
272     /** The number of GAP Peripheral configurations */
273     uint8_t                   gappConfCount;
274 
275     /** The number of GAP Central configurations */
276     uint8_t                   gapcConfCount;
277 
278     /** The GATT Role */
279     uint8_t                   gattRole;
280 
281     /** The number of BLE connections (client) */
282     uint8_t                   maxClientCount;
283 
284     /** The Bonding Requirement */
285     uint8_t                   isBondingReq;
286 } cy_stc_ble_params_t;
287 
288 #if CY_BLE_LIB_HOST_CORE
289 
290 /** BLE internal context structure */
291 typedef struct
292 {
293     /** Structure with information about registered service callbacks */
294     cy_stc_ble_event_handler_t  *eventHandlerPool;
295 
296     /** Discovery information structure  */
297     cy_stc_ble_discovery_t      *discovery;
298 
299     /** Service data received with read by group type response during discovery process */
300     cy_stc_ble_disc_srvc_info_t *serverInfo;
301 
302     /** Total Count of client services that will be discovered */
303     uint32_t                     discServiCount;
304 
305 } cy_stc_ble_context_t;
306 
307 #endif
308 
309 /** BLE Stack parameters */
310 typedef struct
311 {
312     /** Tx 5dbm mode enable */
313     bool                           tx5dbmModeEn;
314 
315     /**  Configuration parameter for Radio PA calibration */
316     cy_stc_ble_mxd_pa_cal_param_t *paCalConfig;
317 
318     /** The feature set mask used to control usage of
319      *  specified feature in the BLE stack */
320     uint16_t                       featureMask;
321 
322     /** Max Tx payload size */
323     uint16_t                       dleMaxTxCapability;
324 
325     /** Max Rx payload size */
326     uint16_t                       dleMaxRxCapability;
327 
328     /** Memory heap pointer size */
329     uint16_t                       totalHeapSz;
330 
331     /** Memory heap pointer */
332     uint8_t                       *memoryHeapPtr;
333 
334     /** Controller memory heap size (uses only in dual CPU mode) */
335     uint16_t                       controllerTotalHeapSz;
336 
337     /** Controller memory heap pointer (uses only in dual CPU mode) */
338     uint8_t                       *controllerMemoryHeapPtr;
339 
340     /** Configuration for the L2CAP buffer for data transmission */
341     uint8_t                        l2capBufferPerConn;
342 
343     /** The maximum number of devices that can be added to the resolve list */
344     uint8_t                        maxResolvableDevListSize;
345 
346     /** The maximum number of devices that can be added to the white list */
347     uint8_t                        maxBondedDevListSize;
348 
349     /** The maximum number of bonded devices to be supported by this device */
350     uint8_t                        maxWhiteListSize;
351 
352     /** Maximum number of BLE connections */
353     uint8_t                        maxConnCount;
354 
355     /** Package type */
356     uint16_t                       packageType;
357 
358     /** Package type */
359 } cy_stc_ble_stack_params_t;
360 
361 
362 /** BLE HW Configuration structure */
363 typedef struct
364 {
365     /** BLESS interrupt configuration */
366     const cy_stc_sysint_t          *blessIsrConfig;
367 
368     /** HCI mode */
369     /** UART interrupt configuration */
370     const cy_stc_sysint_t          *uartIsrConfig;
371 
372     /** UART driver configuration */
373     const cy_stc_scb_uart_config_t *uartConfig;
374 
375     /** UART driver hardware instance */
376     CySCB_Type                     *uartHw;
377 } cy_stc_ble_hw_config_t;
378 
379 
380 /** BLE Configuration structure */
381 typedef struct
382 {
383 
384 #if CY_BLE_LIB_HOST_CORE
385     /** Pointer to advertising information structure */
386     cy_stc_ble_gapp_disc_mode_info_t *discoveryModeInfo;
387 
388     /** Pointer to advertising configuration structure */
389     cy_stc_ble_gapp_adv_params_t     *gappAdvParams;
390 
391     /** Pointer to Discovery information collected by client structure */
392     cy_stc_ble_gapc_disc_info_t      *discoveryInfo;
393 
394     /** Pointer to scan configuration structure */
395     cy_stc_ble_gapc_scan_params_t    *gapcScanParams;
396 
397     /** Pointer to structure to store bonding data */
398     const cy_stc_ble_flash_storage_t *flashStorage;
399 
400     /** Pointer to cy_ble_attValuesCCCD */
401     uint8_t                          *attValuesCCCD;
402 
403     /** Pointer to GATT database structure used in the GAP server */
404     const cy_stc_ble_gatts_db_t      *gattDB;
405 
406     /** Pointer to Authentication Parameters Information structure */
407     cy_stc_ble_gap_auth_info_t       *authInfo;
408 
409     /** Bluetooth Device Address */
410     cy_stc_ble_gap_bd_addr_t         *deviceAddress;
411 
412     /** Services initialize function */
413     cy_fn_service_init_t             ServiceInitFunc;
414 
415     /** Pointer to BLE Configuration Parameters structure */
416     const cy_stc_ble_params_t        *params;
417 
418     /** Internal usage context */
419     cy_stc_ble_context_t             *context;
420 
421 #endif /* CY_BLE_LIB_HOST_CORE */
422 
423     /** BLE Stack configuration */
424     cy_stc_ble_stack_params_t        *stackParam;
425 
426     /** BLE HW configuration */
427     cy_stc_ble_hw_config_t           *hw;
428 
429 }cy_stc_ble_config_t;
430 
431 
432 /*******************************************************************************
433 * Function Prototypes
434 *******************************************************************************/
435 
436 /**
437  * \addtogroup group_ble_common_api_functions
438  * \{
439  */
440 /* Host  API */
441 void Cy_BLE_RegisterEventCallback(cy_ble_callback_t callbackFunc);
442 
443 /** \cond IGNORE */
444 /* Low Power mode API */
445 void Cy_BLE_EnableSystemLowPowerMode(void);
446 void Cy_BLE_EnableHostLowPowerMode(void);
447 void Cy_BLE_EnableControllerLowPowerMode(void);
448 /** \endcond */
449 
450 /* BLE application Host callback feature API */
451 cy_en_ble_api_result_t Cy_BLE_RegisterAppHostCallback(cy_ble_app_notify_callback_t CallBack);
452 void Cy_BLE_UnRegisterAppHostCallback(void);
453 
454 /* Interrupt functions */
455 void Cy_BLE_BlessIsrHandler(void);
456 /** \} group_ble_common_api_functions */
457 
458 
459 /* Host  API */
460 cy_en_ble_api_result_t Cy_BLE_InitHost(const cy_stc_ble_config_t *config);
461 cy_en_ble_api_result_t Cy_BLE_EnableHost(void);
462 cy_en_ble_api_result_t Cy_BLE_DisableHost(void);
463 
464 /* Controller  API */
465 cy_en_ble_api_result_t Cy_BLE_InitController(const cy_stc_ble_config_t *config);
466 cy_en_ble_api_result_t Cy_BLE_EnableController(void);
467 cy_en_ble_api_result_t Cy_BLE_EnableHCIModeController(void);
468 cy_en_ble_api_result_t Cy_BLE_EnableControllerFromHost(void);/** \endcond */
469 
470 /**
471  * \addtogroup group_ble_common_Intr_feature_api_functions
472  * \{
473  */
474 /* BLE interrupt notifications feature API */
475 cy_en_ble_api_result_t Cy_BLE_RegisterInterruptCallback(uint32_t intrMask,
476                                                         cy_ble_intr_callback_t CallBack);
477 cy_en_ble_api_result_t Cy_BLE_UnRegisterInterruptCallback(void);
478 
479 cy_en_ble_api_result_t Cy_BLE_ConfigureIpcForInterruptCallback(uint32_t ipcChan, uint32_t ipcIntr,
480                                                                uint32_t ipcIntrPrior);
481 
482 /* Interrupt functions for interrupt notifications feature */
483 void Cy_BLE_IntrNotifyIsrHandler(void);
484 /** \} */
485 
486 
487 /*******************************************************************************
488 * Private Function Prototypes
489 *******************************************************************************/
490 
491 void Cy_BLE_EventHandlerWrapper(cy_en_ble_event_t event, void *evParam);
492 
493 
494 /*******************************************************************************
495 * External Data references
496 *******************************************************************************/
497 
498 /* BLE application callback */
499 extern cy_ble_callback_t Cy_BLE_ApplCallback;
500 
501 /* Global structure to initialize Cy_BLE_Init */
502 extern const cy_stc_ble_config_t *cy_ble_configPtr;
503 extern cy_stc_ble_intr_notify_t intrNotify;
504 
505 /* BLE application IPC notification handler */
506 extern cy_ble_app_notify_callback_t Cy_BLE_HostRegisteredCallback;
507 
508 
509 /*******************************************************************************
510 * Function Name: Cy_BLE_ConfigureExtPA
511 ****************************************************************************//**
512 *
513 *  This function controls the external Power Amplifier and Low Noise Amplifier
514 *  outputs.
515 *
516 * \param controlValue Enables and controls polarity of the outputs.
517 *   To control external PA and LNA, the following mask defines are used:
518 *
519 *   - BLE_BLESS_EXT_PA_LNA_CTRL_ENABLE_EXT_PA_LNA_Msk - When set to 1, enables
520 *       the external PA & LNA and BLESS power profile for energy profiler
521 *   - BLE_BLESS_EXT_PA_LNA_CTRL_CHIP_EN_POL_Msk - Controls the polarity of the
522 *       chip-enable control signal:
523 *       * 0 - HIGH enable, LOW disable
524 *       * 1 - LOW enable, HIGH disable, Incorrect power profile
525 *   - BLE_BLESS_EXT_PA_LNA_CTRL_PA_CTRL_POL_Msk - Controls the polarity of the PA
526 *       control signal:
527 *       * 0 - HIGH enable, LOW disable
528 *       * 1 - LOW enable, HIGH disable, Incorrect power profile
529 *   - BLE_BLESS_EXT_PA_LNA_CTRL_LNA_CTRL_POL_Msk - Controls the polarity of the LNA
530 *       control signal:
531 *       * 0 - HIGH enable, LOW disable
532 *       * 1 - LOW enable, HIGH disable, Incorrect power profile
533 *   - BLE_BLESS_EXT_PA_LNA_CTRL_OUT_EN_DRIVE_VAL_Msk  - Configures the drive value
534 *       on the output enables of PA, LNA and CHI_EN signals
535 *       * 0 - drive 0 on the output enable signals
536 *       * 1 - drive 1 on the output enable signals
537 *
538 *******************************************************************************/
Cy_BLE_ConfigureExtPA(uint32_t controlValue)539 __STATIC_INLINE void Cy_BLE_ConfigureExtPA(uint32_t controlValue)
540 {
541     BLE->BLESS.EXT_PA_LNA_CTRL = controlValue;
542 }
543 
544 
545 
546 #ifdef __cplusplus
547 }
548 #endif /* __cplusplus */
549 #endif /* CY_IP_MXBLESS */
550 #endif /* CY_BLE_H */
551 
552 /* [] END OF FILE */
553