-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcy_ble.c
1209 lines (1054 loc) · 47 KB
/
cy_ble.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************//**
* \file cy_ble.c
* \version 3.60
*
* \brief
* This file contains the source code for the API of the PSoC 6 BLE Middleware.
*
********************************************************************************
* \copyright
* Copyright 2017-2021, Cypress Semiconductor Corporation. All rights reserved.
* You may use this file only in accordance with the license, terms, conditions,
* disclaimers, and limitations in the end user license agreement accompanying
* the software package with which this file was provided.
*******************************************************************************/
#include <stdint.h>
#include "cy_ble_event_handler.h"
#include "cy_ble_hal_pvt.h"
#include "cy_ble.h"
#include "cy_ble_stack_pvt.h"
#if defined(CY_IP_MXBLESS)
#ifdef CY_BLE_WARNING_NO_SELECTED_BLESS_COMPONENTS
#warning The BLE Stack components are not defined in Makefile. Use COMPONENTS variable in Makefile \
to select BLE Stack component. The following COMPONENTS define patricular BLE Stack modes: \
'COMPONENTS+=BLESS_HOST_IPC CM0_BLESS' - to operate in dual CPU mode, \
'COMPONENTS+=BLESS_CONTROLLER BLESS_HOST' - to operate in single CPU mode, \
'COMPONENTS+=BLESS_CONTROLLER' - to operate in controller only (HCI) mode. \
/* For more details refer to the following documentation:
* https://cypresssemiconductorco.github.io/bless/ble_api_reference_manual/html/page_ble_section_configuration_considerations.html#group_ble_section_conf_cons_prebuild */
#endif /* ifdef WARNING_NO_SELECTED_BLESS_COMPONENT */
/*******************************************************************************
* Private Function Prototypes
*******************************************************************************/
static void Cy_BLE_RegisterPmCallbacksSingle(void);
static void Cy_BLE_RegisterPmCallbacksDual(void);
static void Cy_BLE_UnregisterHostPmCallbacksSingle(void);
static void Cy_BLE_UnregisterHostPmCallbacksDual(void);
static cy_en_syspm_status_t Cy_BLE_DeepSleepCallbackSingleCore(cy_stc_syspm_callback_params_t *callbackParams,
cy_en_syspm_callback_mode_t mode);
static cy_en_syspm_status_t Cy_BLE_DeepSleepCallbackDualCore(cy_stc_syspm_callback_params_t *callbackParams,
cy_en_syspm_callback_mode_t mode);
static cy_en_syspm_status_t Cy_BLE_SleepCallbackDualCore(cy_stc_syspm_callback_params_t *callbackParams,
cy_en_syspm_callback_mode_t mode);
/*******************************************************************************
* Global Variables
*******************************************************************************/
/** An application layer event callback function to receive
* service events from the PSoC 6 BLE Middleware. */
cy_ble_callback_t Cy_BLE_ApplCallback = NULL;
/** Pointer to the global BLE configuration structures */
__WEAK const cy_stc_ble_config_t *cy_ble_configPtr = NULL;
/** Pointers to SysPm callback function */
void (*Cy_BLE_RegisterPmCallbacksPtr)(void) = NULL;
void (*Cy_BLE_UnregisterPmCallbacksPtr)(void) = NULL;
/* Structure with the SysPm callback parameters for BLESS deep sleep */
static cy_stc_syspm_callback_params_t bleDeepSleepCallbackParams =
{
/* base */ NULL,
/* context */ NULL
};
static cy_stc_syspm_callback_t bleDeepSleepCallback =
{
/* callback */ NULL,
/* type */ CY_SYSPM_DEEPSLEEP,
/* skipMode */ CY_SYSPM_SKIP_BEFORE_TRANSITION | CY_SYSPM_SKIP_CHECK_FAIL,
/* callbackParams */ &bleDeepSleepCallbackParams,
/* prevItm */ NULL,
/* nextItm */ NULL,
/* order */ CY_BLE_LPM_SYSPM_CB_ORDER
};
/* Structure with the SysPm callback parameters for BLE sleep */
static cy_stc_syspm_callback_params_t bleSleepCallbackParams =
{
/* base */ NULL,
/* context */ NULL
};
static cy_stc_syspm_callback_t bleSleepCallback =
{
/* callback */ &Cy_BLE_SleepCallbackDualCore,
/* type */ CY_SYSPM_SLEEP,
/* skipMode */ CY_SYSPM_SKIP_BEFORE_TRANSITION | CY_SYSPM_SKIP_CHECK_FAIL,
/* callbackParams */ &bleSleepCallbackParams,
/* prevItm */ NULL,
/* nextItm */ NULL,
/* order */ CY_BLE_LPM_SYSPM_CB_ORDER
};
/******************************************************************************
* Function Name: Cy_BLE_InitHost
***************************************************************************//**
*
* Initializes the PSoC 6 BLE Middleware (Host part).
*
* \param config: The configuration structure for the PSoC 6 BLE Middleware.
*
* \return
* \ref cy_en_ble_api_result_t : Return value indicates whether the function succeeded or
* failed. The following are possible error codes.
*
* Error codes | Description
* ------------ | -----------
* CY_BLE_SUCCESS | The function completed successfully.
* CY_BLE_ERROR_INVALID_PARAMETER | On specifying NULL as the input parameter.
*
******************************************************************************/
cy_en_ble_api_result_t Cy_BLE_InitHost(const cy_stc_ble_config_t *config)
{
cy_en_ble_api_result_t apiResult = CY_BLE_SUCCESS;
#if CY_BLE_LIB_HOST_CORE
if((config == NULL) || (Cy_BLE_ApplCallback == NULL))
{
apiResult = CY_BLE_ERROR_INVALID_PARAMETER;
}
else
{
uint32_t idx;
/* Register a pointer to the configuration structure */
cy_ble_configPtr = config;
Cy_BLE_HAL_SetConfigStructure(cy_ble_configPtr);
/* Initializes cy_ble_connHandle */
for(idx = 0u; idx < CY_BLE_MAX_SUPPORTED_CONN_COUNT; idx++)
{
cy_ble_connHandle[idx].bdHandle = CY_BLE_INVALID_CONN_HANDLE_VALUE;
cy_ble_connHandle[idx].attId = CY_BLE_INVALID_CONN_HANDLE_VALUE;
}
/* Initializes client structures */
if((cy_ble_configPtr->params->gattRole & CY_BLE_GATT_CLIENT) != 0u)
{
for(idx = 0u; idx < cy_ble_configPtr->params->maxClientCount; idx++)
{
uint32 servCnt = cy_ble_configPtr->context->discServiCount;
(void)memset(&cy_ble_configPtr->context->serverInfo[idx * servCnt], 0,
(sizeof(cy_stc_ble_disc_srvc_info_t) * servCnt));
(void)memset(&cy_ble_configPtr->context->discovery[idx], 0, sizeof(cy_stc_ble_discovery_t));
/* Update cy_ble_discovery[].connIndex with init values (CY_BLE_INVALID_CONN_HANDLE_VALUE) */
cy_ble_configPtr->context->discovery[idx].connIndex = CY_BLE_INVALID_CONN_HANDLE_VALUE;
}
}
if(cy_ble_configPtr->ServiceInitFunc != NULL)
{
cy_ble_configPtr->ServiceInitFunc();
}
cy_ble_pendingFlashWrite = 0u;
cy_ble_eventHandlerFlag = 0u;
(void)memset(cy_ble_peerBonding, (int8_t)CY_BLE_GAP_BONDING_NONE, sizeof(cy_ble_peerBonding));
}
#else
/* Suppress unused variable warning */
(void) config;
#endif /* CY_BLE_LIB_HOST_CORE */
return(apiResult);
}
/******************************************************************************
* Function Name: Cy_BLE_EnableHost
***************************************************************************//**
*
* This function initializes the BLE Stack, which consists of the BLE Stack
* Manager, BLE Controller, and BLE Host modules. It takes care of initializing
* the Profile layer, schedulers, Timer, and other platform-related resources
* required for the PSoC 6 BLE Middleware.
*
* Note that this function does not reset the BLE Stack.
*
* Calling this function results in generation of a #CY_BLE_EVT_STACK_ON event
* on successful initialization of the BLE Stack.
*
* In the BLE dual CPU mode, this function should be called on Host cores.
*
* The BLE Stack enables the BLE ECO clock automatically with the default
* parameters:
* Parameter | Value
* -------------------- | -----------
* ECO Frequency | CY_BLE_DEFAULT_ECO_FREQ
* Divider | CY_BLE_DEFAULT_ECO_DIV
* Startup time | CY_BLE_DEFAULT_OSC_STARTUP_DELAY_LF
* Load cap | CY_BLE_DEFAULT_CAP_TRIM_VALUE
*
* If there is a need to start the BLE with non-default ECO parameters,
* call the Cy_BLE_EcoConfigure() function with the custom configuration each
* time before calling the Cy_BLE_EnableHost() function.
*
* NOTE: BLE requires a call to Cy_IPC_SystemSemaInit() and Cy_IPC_SystemPipeInit()
* functions before use.
* This function is called in the SystemInit() function for proper flash write
* and erase operations. If the default startup file is not used, or the function
* SystemInit() is not called in your project, call the following functions:
* -# Cy_IPC_SystemSemaInit()
* -# Cy_IPC_SystemPipeInit()
*
* return
* \ref cy_en_ble_api_result_t : Return value indicates whether the function succeeded
* or failed. The following are possible error codes.
*
* <table>
* <tr>
* <th>Error codes</th>
* <th>Description</th>
* </tr>
* <tr>
* <td>CY_BLE_SUCCESS</td>
* <td>On successful operation.</td>
* </tr>
* <tr>
* <td>CY_BLE_ERROR_REPEATED_ATTEMPTS</td>
* <td>On invoking this function more than once without calling
* Cy_BLE_Disable() function between calls to this function.</td>
* </tr>
* <tr>
* <td>CY_BLE_ERROR_MEMORY_ALLOCATION_FAILED</td>
* <td>There is insufficient memory available.</td>
* </tr>
* <tr>
* <td>CY_BLE_ERROR_INVALID_STATE</td>
* <td>The PSoC 6 BLE Middleware was not initialized.</td>
* </tr>
* </table>
*
******************************************************************************/
cy_en_ble_api_result_t Cy_BLE_EnableHost(void)
{
cy_en_ble_api_result_t apiResult = CY_BLE_ERROR_INVALID_STATE;
#if CY_BLE_LIB_HOST_CORE
if (cy_ble_configPtr != NULL)
{
/* Set of buffers to be allocated by the BLE Stack for BLE Stack operation */
/* Array dataBuff [totalDataBufferPools] shall provide the information to the BLE Stack based
* on the below table:
*
* Index | bufferSize | noOfBuffer
* ----- | --------------------------------------------------------| ---------------
* 0 | (GATT MTU + CY_BLE_MEM_EXT_SZ + CY_BLE_L2CAP_HDR_SZ) | 3
* 1 | ((CY_BLE_L2CAP_PSM_SIZE + CY_BLE_MEM_EXT_SZ) * | No of PSM supported
* | No of PSM supported) |
* | |
* 2 | ((CY_BLE_L2CAP_CBFC_CHANNEL_SIZE + CY_BLE_MEM_EXT_SZ) * | 2 * No of L2cap
* | No of L2CAP logical channels) | logical channels
* | |
* 3 | (L2CAP MTU + CY_BLE_MEM_EXT_SZ + CY_BLE_L2CAP_HDR_SZ) | 2 * No of L2cap
* | | logical channels
*/
/* Data Buffer information for ATT/GATT Configured MTU Size */
uint16_t dataBuffIdx0 = CY_BLE_ALIGN_TO_4(cy_ble_configPtr->params->mtuSize +
CY_BLE_MEM_EXT_SZ + CY_BLE_L2CAP_HDR_SZ);
/* Data Buffer information forL2CAP configuration for number of PSM Channels */
uint16_t dataBuffIdx1 = CY_BLE_ALIGN_TO_4(cy_ble_configPtr->params->l2capPsmCount *
(CY_BLE_L2CAP_PSM_SIZE + CY_BLE_MEM_EXT_SZ));
/* Data Buffer information for L2CAP configuration for number of CBFC Channels */
uint16_t dataBuffIdx2 = CY_BLE_ALIGN_TO_4(cy_ble_configPtr->params->l2capChanCount *
(CY_BLE_L2CAP_CBFC_CHANNEL_SIZE + CY_BLE_MEM_EXT_SZ));
/* Data Buffer information for L2CAP configured MTU Size */
uint16_t dataBuffIdx3 = CY_BLE_ALIGN_TO_4(cy_ble_configPtr->params->l2capMtuSize +
CY_BLE_MEM_EXT_SZ + CY_BLE_L2CAP_HDR_SZ);
#if defined(CY_BLE_STACK_APP_POOL_5_SZ)
/* Data Buffer information for GATT DB maximum entry Size */
uint16_t dbInxCount = cy_ble_configPtr->params->gattDbIndexCount;
uint16_t dataBuffIdx4 = CY_BLE_ALIGN_TO_4((dbInxCount / 8u) + CY_BLE_MEM_EXT_SZ +
(((dbInxCount % 8u) != 0u) ? 1u : 0u )) ;
#endif /* defined(CY_BLE_STACK_APP_POOL_5_SZ) */
/* If mtu > 23 stack queue depth (stack queue depth per connection - 1) * maxBleConnection */
uint16_t mtuBuffCount = (cy_ble_configPtr->stackParam->maxConnCount *
((cy_ble_configPtr->params->mtuSize > CY_BLE_MTU_MIN_VALUE) ?
((uint16_t)cy_ble_configPtr->stackParam->l2capBufferPerConn - 1u) :
CY_BLE_MTU_MIN_BUFF_NUM));
cy_stc_ble_stk_app_data_buff_t stackDataBuff[CY_BLE_STACK_APP_MIN_POOL] =
{
{ dataBuffIdx0, mtuBuffCount },
{ dataBuffIdx1, cy_ble_configPtr->params->l2capPsmCount },
{ dataBuffIdx2, 2u * cy_ble_configPtr->params->l2capChanCount },
{ dataBuffIdx3, 2u * cy_ble_configPtr->params->l2capChanCount },
#if defined(CY_BLE_STACK_APP_POOL_5_SZ)
{ dataBuffIdx4, 1u },
#endif /* defined(CY_BLE_STACK_APP_POOL_5_SZ) */
};
cy_stc_ble_stack_init_info_t stackInitParam;
(void)memset(&stackInitParam, 0, sizeof(cy_stc_ble_stack_init_info_t));
/* Application Callback Function */
stackInitParam.CyBleAppCbFunc = (cy_ble_app_ev_cb_t)&Cy_BLE_EventHandler;
/* Initialize BLE stack buffers needed for BLE Stack operation */
stackInitParam.memParam.dataBuff = stackDataBuff;
/* Total data buffer pools */
stackInitParam.memParam.totalDataBufferPools = CY_BLE_STACK_APP_MIN_POOL;
/* Flash storage */
if(cy_ble_configPtr->flashStorage->stackFlashPtr != NULL)
{
stackInitParam.memParam.bleStackFlashPointer = cy_ble_configPtr->flashStorage->stackFlashPtr;
stackInitParam.memParam.bleStackFlashSize = cy_ble_configPtr->flashStorage->stackFlashSize;
}
/* Configure l2cap queue */
stackInitParam.stackConfig.l2capConfig.l2capBufferPerConn = cy_ble_configPtr->stackParam->l2capBufferPerConn;
/* BLE Stack memory heap size */
stackInitParam.memParam.totalHeapSz = cy_ble_configPtr->stackParam->totalHeapSz;
/* Configure DLE */
stackInitParam.stackConfig.dleConfig.dleMaxTxCapability = cy_ble_configPtr->stackParam->dleMaxTxCapability;
stackInitParam.stackConfig.dleConfig.dleMaxRxCapability = cy_ble_configPtr->stackParam->dleMaxRxCapability;
stackInitParam.stackConfig.dleConfig.dleNumTxBuffer = CY_BLE_LL_DEFAULT_NUM_ACL_TX_PACKETS;
stackInitParam.stackConfig.dleConfig.dleNumRxBuffer = CY_BLE_LL_DEFAULT_NUM_ACL_RX_PACKETS;
/* Configure BLE Stack features */
stackInitParam.stackConfig.featureMask =
cy_ble_configPtr->stackParam->featureMask;
/* Configure maximum connection support */
stackInitParam.stackConfig.maxBleConnections =
cy_ble_configPtr->stackParam->maxConnCount;
/* Configure bonded device list */
stackInitParam.stackConfig.bondListConfig.bondListSize =
cy_ble_configPtr->stackParam->maxBondedDevListSize;
/* Configure white list */
stackInitParam.stackConfig.whiteListConfig.whiteListSize =
cy_ble_configPtr->stackParam->maxWhiteListSize;
/* Configure LL Privacy */
stackInitParam.stackConfig.privacyConfig.resolvingListSize =
cy_ble_configPtr->stackParam->maxResolvableDevListSize;
/* Register SysPm callback for BLESS deep sleep support */
if (Cy_BLE_RegisterPmCallbacksPtr != NULL)
{
Cy_BLE_RegisterPmCallbacksPtr();
}
/* Configure BLE features */
/* Enable DLE code in the stack */
if((cy_ble_configPtr->stackParam->featureMask & CY_BLE_DLE_FEATURE_MASK) != 0u)
{
Cy_BLE_EnableDleFeature();
}
/* Enable LL Privacy code in the stack */
if((cy_ble_configPtr->stackParam->featureMask & CY_BLE_PRIVACY_1_2_FEATURE_MASK) != 0u)
{
Cy_BLE_EnablePrivacyFeature();
}
/* Enable PHY Update code in the stack */
if((cy_ble_configPtr->stackParam->featureMask & CY_BLE_PHY_UPDATE_FEATURE_MASK) != 0u)
{
Cy_BLE_EnablePhyUpdateFeature();
}
apiResult = Cy_BLE_StackSetFeatureConfig(&stackInitParam.stackConfig);
if(apiResult == CY_BLE_SUCCESS)
{
/* Set pointer to memory RAM buffer */
stackInitParam.memParam.memoryHeapPtr = cy_ble_configPtr->stackParam->memoryHeapPtr;
/* Configure parameter for Radio PA calibration */
if(cy_ble_configPtr->stackParam->paCalConfig != NULL)
{
stackInitParam.stackConfig.paCalConfig = *cy_ble_configPtr->stackParam->paCalConfig;
}
/* Initialize the BLE Stack */
apiResult = Cy_BLE_StackInit(&stackInitParam);
}
if(apiResult == CY_BLE_SUCCESS)
{
/* Initializes internal states */
Cy_BLE_SetState(CY_BLE_STATE_INITIALIZING);
if((cy_ble_configPtr->params->gapRole & (CY_BLE_GAP_PERIPHERAL | CY_BLE_GAP_BROADCASTER)) != 0u)
{
Cy_BLE_SetAdvertisementState(CY_BLE_ADV_STATE_STOPPED);
}
if((cy_ble_configPtr->params->gapRole & (CY_BLE_GAP_OBSERVER | CY_BLE_GAP_CENTRAL)) != 0u)
{
Cy_BLE_SetScanState(CY_BLE_SCAN_STATE_STOPPED);
}
}
}
#endif /* CY_BLE_LIB_HOST_CORE */
return(apiResult);
}
/******************************************************************************
* Function Name: Cy_BLE_DisableHost
***************************************************************************//**
*
* This function stops any ongoing operation in the BLE Stack and forces the
* BLE Stack to shut down.
*
* Calling this function results in generation of a
* #CY_BLE_EVT_STACK_SHUTDOWN_COMPLETE event on a successful stack shut-down.
*
* \return
* \ref cy_en_ble_api_result_t : Return value indicates whether the function succeeded or
* failed. The following are possible error codes.
*
* <table>
* <tr>
* <th>Error codes</th>
* <th>Description</th>
* </tr>
* <tr>
* <td>CY_BLE_SUCCESS</td>
* <td>On successful operation.</td>
* </tr>
* <tr>
* <td>CY_BLE_ERROR_INVALID_OPERATION</td>
* <td>On calling Cy_BLE_Disable before calling Cy_BLE_Enable()
* or on Controller core.</td>
* </tr>
* </table>
*
******************************************************************************/
cy_en_ble_api_result_t Cy_BLE_DisableHost(void)
{
cy_en_ble_api_result_t apiResult = CY_BLE_SUCCESS;
#if CY_BLE_LIB_HOST_CORE
/* Stops all ongoing activities */
apiResult = Cy_BLE_StackShutdown();
#endif /* CY_BLE_LIB_HOST_CORE */
return(apiResult);
}
/******************************************************************************
* Function Name: Cy_BLE_RegisterEventCallback
***************************************************************************//**
*
* Registers a callback function to receive events from the PSoC 6 BLE Middleware.
*
* \param callbackFunc: An application layer event callback function to receive
* events from the PSoC 6 BLE Middleware. The definition of \ref cy_ble_callback_t
* is:<br>
* typedef void (* cy_ble_callback_t) (uint32_t eventCode, void *eventParam),
* where:
* * eventCode: Indicates the event that triggered this callback
* (e.g. #CY_BLE_EVT_STACK_ON).
* * eventParam: Contains the parameters corresponding to the
* current event.
*
******************************************************************************/
void Cy_BLE_RegisterEventCallback(cy_ble_callback_t callbackFunc)
{
/* Store an application callback function */
Cy_BLE_ApplCallback = callbackFunc;
}
/*******************************************************************************
* Function Name: Cy_BLE_RegisterInterruptCallback
****************************************************************************//**
*
* This function registers a callback to expose BLE interrupt notifications to an
* application that indicates a different link layer and radio state transition
* to the user from the BLESS interrupt context. This callback is triggered at
* the beginning of a received BLESS interrupt (based on the registered
* interrupt mask).
*
* An application can use an interrupt callback to know when:
* * the RF activity is about to begin/end;
* * the BLE device changes its state from advertising to connected;
* * BLESS transits between BLESS active and BLESS low-power modes.
*
* These BLESS real-time states can be used to synchronize
* an application with the BLESS or prevent radio interference with other
* peripherals, etc.
*
* BLE dual CPU mode requires an additional configuration IPC channel and IPC Interrupt
* structure to send notifications from the controller core to Host core.
* Refer to Cy_BLE_ConfigureIpcForInterruptCallback() for details.
*
* \note
* The user must call Cy_BLE_IntrNotifyIsrHandler() inside the user-defined BLESS
* interrupt service routine (ISR)
*
* \param intrMask
* All interrupts masks are specified in the #cy_en_ble_interrupt_callback_feature_t
* enumeration.
*
* \param CallBack
* The pointer to an application notify callback.
*
* \return
* \ref cy_en_ble_api_result_t : The return value indicates whether the function succeeded or
* failed. The possible error codes:
*
* Errors codes | Description
* ------------ | -----------
* CY_BLE_SUCCESS | The callback is registered successfully.
* CY_BLE_ERROR_INVALID_PARAMETER | Validation of the input parameters failed.
*
*******************************************************************************/
cy_en_ble_api_result_t Cy_BLE_RegisterInterruptCallback(uint32_t intrMask, cy_ble_intr_callback_t CallBack)
{
cy_en_ble_api_result_t apiResult = CY_BLE_SUCCESS;
if(CallBack != NULL)
{
/* Store the application callback and interrupt mask */
Cy_BLE_InterruptCallback = CallBack;
intrNotify.mask = intrMask;
/* Store pointer to intrNotify configuration */
intrNotifyPtr = &intrNotify;
}
else
{
apiResult = CY_BLE_ERROR_INVALID_PARAMETER;
}
return apiResult;
}
/*******************************************************************************
* Function Name: Cy_BLE_ConfigureIpcForInterruptCallback
****************************************************************************//**
*
* This function configures IPC channel for BLE interrupt notifications feature,
* when BLE is operating in BLE dual CPU mode.
*
* This function must be called only on the Host CPU core.
*
* \param ipcChan: IPC channel. Valid range: 9..15
* \param ipcIntr: IPC Interrupt structure. Valid range: 9..15
* \param ipcIntrPrior: IPC Interrupt priority. Valid range: 0..7
*
* \return
* \ref cy_en_ble_api_result_t : The return value indicates whether the function succeeded or
* failed. The possible error codes:
*
* Errors codes | Description
* ------------ | -----------
* CY_BLE_SUCCESS | The callback is registered successfully.
* CY_BLE_ERROR_INVALID_PARAMETER | Validation of the input parameters failed.
* CY_BLE_ERROR_INVALID_OPERATION | The IPC channel is busy (BLE dual CPU mode only).
*
*******************************************************************************/
cy_en_ble_api_result_t Cy_BLE_ConfigureIpcForInterruptCallback(uint32_t ipcChan, uint32_t ipcIntr, uint32_t ipcIntrPrior)
{
/* Set up the IPC for BLE dual CPU mode */
cy_en_ble_api_result_t apiResult = CY_BLE_SUCCESS;
/* IPC channel 0...8 is reserved by system */
if( (ipcChan >= 9u) && (ipcChan <= 15u) && (ipcIntr >= 9u) && (ipcIntr <= 15u) &&
(ipcIntrPrior <= 7u) )
{
cy_en_ipc_pipe_status_t ipcStatus;
cy_stc_sysint_t intrConfig;
uint32_t rTimeout = 2000u; /* us */
/* Initialize the interrupt controller for CM4 and IPC Interrupt */
intrConfig.intrPriority = ipcIntrPrior;
intrConfig.intrSrc =(IRQn_Type) CY_IPC_INTR_NUM_TO_VECT((int32_t)ipcIntr);
(void)Cy_SysInt_Init(&intrConfig, &Cy_BLE_HAL_IntrNotifyIpcHandler);
NVIC_EnableIRQ(intrConfig.intrSrc);
/* Store IPC channel information in Interrupt Notify structure */
intrNotify.userIpcChan = (uint8_t) ipcChan;
intrNotify.userIpcIntr = (uint8_t) ipcIntr;
intrNotify.userIpcIntrPrior = (uint8_t) ipcIntrPrior;
/* Do not bring up an IPC release interrupt here; only set up a notify interrupt */
Cy_IPC_Drv_SetInterruptMask(Cy_IPC_Drv_GetIntrBaseAddr(ipcIntr),
CY_IPC_NO_NOTIFICATION, (1uL << ipcChan));
/*
* Send the address of the intrNotify structure to the controller core via CyPipe
* The IPC Pipe can be busy. If so, try sending a message after 2 ms.
*/
do
{
ipcStatus = Cy_IPC_Pipe_SendMessage(CY_BLE_IPC_CONTROLLER_ADDR, CY_BLE_IPC_HOST_ADDR,
(void *)&intrNotify, NULL);
Cy_SysLib_DelayUs(1u);
rTimeout--;
}while((ipcStatus != CY_IPC_PIPE_SUCCESS) && (rTimeout != 0u));
if(ipcStatus != CY_IPC_PIPE_SUCCESS)
{
apiResult = CY_BLE_ERROR_INVALID_OPERATION;
}
}
else
{
apiResult = CY_BLE_ERROR_INVALID_PARAMETER;
}
return(apiResult);
}
/*******************************************************************************
* Function Name: Cy_BLE_UnRegisterInterruptCallback
****************************************************************************//**
*
* This function un-registers the callback that exposed BLE interrupt
* notifications to the application.
*
* \return
* \ref cy_en_ble_api_result_t : Return value indicates whether the function succeeded
* or failed. The following are possible error codes.
*
* Errors codes | Description
* ------------ | -----------
* CY_BLE_SUCCESS | The callback registered successfully.
* CY_BLE_ERROR_INVALID_OPERATION | The IPC channel is busy (BLE dual CPU mode only).
*
*******************************************************************************/
cy_en_ble_api_result_t Cy_BLE_UnRegisterInterruptCallback(void)
{
cy_en_ble_api_result_t apiResult;
/* Clean interrupt mask */
apiResult = Cy_BLE_RegisterInterruptCallback((uint32_t)CY_BLE_INTR_CALLBACK_NONE,
Cy_BLE_InterruptCallback);
if(apiResult == CY_BLE_SUCCESS)
{
/* Clean callback pointer */
Cy_BLE_InterruptCallback = NULL;
}
return(apiResult);
}
/*******************************************************************************
* Function Name: Cy_BLE_RegisterAppHostCallback
****************************************************************************//**
*
* This function registers an application Host callback (BLE RTOS hook).
* The user can trigger an RTOS BLE task if the application Host callback was
* called.
*
* Theory of operation:
* The PSoC 6 BLE Middleware triggers this callback when the BLE Stack controller needs
* to process pending Stack events (by calling Cy_BLE_ProcessEvents()). This
* callback executes from within an ISR and must be very short.
*
* \note The application Host callback is called from the context of a high-
* priority ISR. Some RTOS (e.g. FreeRTOS) have restrictions on calling the RTOS
* functions from a high-priority ISR. The user application is responsible for
* integration of the BLE RTOS hook and RTOS functions and must ensure that RTOS
* allows calling RTOS API from BLE interrupt context.
* If direct call is forbidden, one of the possible solutions is to trig a
* low-priority interrupt from the application Host callback and then call the
* RTOS functions from a low-priority interrupt. For detail, refer to specific
* RTOS documentation.
*
* \param CallBack
* The pointer to the application Host callback.
*
* \return
* Error Codes | Description
* ------------ | -----------
* CY_BLE_SUCCESS | The callback registered successfully.
* CY_BLE_ERROR_INVALID_PARAMETER | On specifying NULL as the input parameter.
*
*******************************************************************************/
cy_en_ble_api_result_t Cy_BLE_RegisterAppHostCallback(cy_ble_app_notify_callback_t CallBack)
{
cy_en_ble_api_result_t apiResult = CY_BLE_SUCCESS;
if(CallBack != NULL)
{
Cy_BLE_HostRegisteredCallback = CallBack;
}
else
{
apiResult = CY_BLE_ERROR_INVALID_PARAMETER;
}
return apiResult;
}
/*******************************************************************************
* Function Name: Cy_BLE_UnRegisterAppHostCallback
****************************************************************************//**
*
* This function un-registers an application Host callback (BLE RTOS hook).
*
*******************************************************************************/
void Cy_BLE_UnRegisterAppHostCallback(void)
{
Cy_BLE_HostRegisteredCallback = NULL;
}
/*******************************************************************************
*
* BLE LPM (Low Power mode)
*
*******************************************************************************/
/******************************************************************************
* Function Name: Cy_BLE_EnableSystemLowPowerMode
***************************************************************************//**
*
* This function enables BLE low power mode (register BLE SysPm callback)
*
* This function must be used only in BLE single CPU mode.
*
******************************************************************************/
void Cy_BLE_EnableSystemLowPowerMode(void)
{
/* Register BLE SysPm callback for CPU deep sleep */
Cy_BLE_RegisterPmCallbacksSingle();
Cy_BLE_RegisterPmCallbacksPtr = &Cy_BLE_RegisterPmCallbacksSingle;
Cy_BLE_UnregisterPmCallbacksPtr = &Cy_BLE_UnregisterHostPmCallbacksSingle;
}
/******************************************************************************
* Function Name: Cy_BLE_EnableHostLowPowerMode
***************************************************************************//**
*
* This function enables BLE Low Power mode (register BLE SysPm callbacks)
*
* This function must be used only in BLE dual CPU mode and called on the CPU core
* where the BLE Host is running.
*
******************************************************************************/
void Cy_BLE_EnableHostLowPowerMode(void)
{
/* Register BLE SysPm callback for CPU deep sleep/sleep */
Cy_BLE_RegisterPmCallbacksDual();
Cy_BLE_RegisterPmCallbacksPtr = &Cy_BLE_RegisterPmCallbacksDual;
Cy_BLE_UnregisterPmCallbacksPtr = &Cy_BLE_UnregisterHostPmCallbacksDual;
}
/******************************************************************************
* Function Name: Cy_BLE_RegisterPmCallbacksSingle
***************************************************************************//**
*
* Register BLE SysPm callback for CPU deep sleep
*
* This function is designated for internal usage.
*
******************************************************************************/
static void Cy_BLE_RegisterPmCallbacksSingle(void)
{
/* Register BLE SysPm callback for CPU deep sleep support */
bleDeepSleepCallback.callback = &Cy_BLE_DeepSleepCallbackSingleCore;
(void)Cy_SysPm_RegisterCallback(&bleDeepSleepCallback);
}
/******************************************************************************
* Function Name: Cy_BLE_RegisterPmCallbacksDual
***************************************************************************//**
*
* Register BLE SysPm callback for CPU deep sleep/CPU sleep
*
* This function is designated for internal usage.
*
******************************************************************************/
static void Cy_BLE_RegisterPmCallbacksDual(void)
{
/* Register BLE SysPm callback for CPU deep sleep support */
bleDeepSleepCallback.callback = &Cy_BLE_DeepSleepCallbackDualCore;
(void)Cy_SysPm_RegisterCallback(&bleDeepSleepCallback);
/* Register BLE SysPm callback for CPU sleep support, need only in
* BLE dual CPU mode for the Host core */
(void)Cy_SysPm_RegisterCallback(&bleSleepCallback);
}
/******************************************************************************
* Function Name: Cy_BLE_UnregisterHostPmCallbacksSingle
***************************************************************************//**
*
* Unregister BLE SysPm callback for CPU deep sleep for Host core.
*
* This function is designated for internal usage.
*
******************************************************************************/
static void Cy_BLE_UnregisterHostPmCallbacksSingle(void)
{
/* Unregister BLE SysPm callback for CPU deep sleep support */
(void)Cy_SysPm_UnregisterCallback(&bleDeepSleepCallback);
}
/******************************************************************************
* Function Name: Cy_BLE_UnregisterHostPmCallbacksDual
***************************************************************************//**
*
* Unregister BLE SysPm callback for CPU deep sleep/CPU sleep for Host core.
*
* This function is designated for internal usage.
*
******************************************************************************/
static void Cy_BLE_UnregisterHostPmCallbacksDual(void)
{
/* Unregister BLE callback for CPU deep sleep support */
(void)Cy_SysPm_UnregisterCallback(&bleDeepSleepCallback);
/* Unregister sleep mode callback only in BLE dual CPU mode for the Host core */
/* Unregister BLE callback for CPU sleep support */
(void)Cy_SysPm_UnregisterCallback(&bleSleepCallback);
}
/*******************************************************************************
* Function Name: Cy_BLE_DeepSleepCallbackSingleCore
****************************************************************************//**
*
* This function requests the BLE Stack to put Bluetooth Low Energy Sub-System
* (BLESS) to deep sleep mode for BLE single CPU mode.
*
* It is registered to the system power mode by Cy_SysPm_RegisterCallback()
* function with CY_SYSPM_DEEPSLEEP type. After registration, it is called by
* Cy_SysPm_DeepSleep() function prior entering the core into the CPU deep sleep
* power mode.
*
* When it is called with the enMode parameter set to CY_SYSPM_CHECK_READY,
* this function enters BLESS to CY_BLE_BLESS_DEEPSLEEP mode. It also checks the
* BLE Subsystem's current operational mode. When the
* Cy_BLE_StackGetBleSsState() function returns CY_BLE_BLESS_STATE_ECO_ON or
* CY_BLE_BLESS_STATE_DEEPSLEEP state, this function returns CY_SYSPM_PASS and
* allows putting the core into the CPU deep sleep power mode.
* At all other times, the function tries to send the core into CPU sleep mode.
*
* This function is available only when BLE Low Power mode is enabled (called
* Cy_BLE_EnableLPM).
*
* \param
* callbackParams Pointer to the structure with the syspm callback parameters.
*
* \param
* mode The associated syspm callback mode. See description of the
* cy_en_syspm_callback_mode_t type.
*
* \return
* * CY_SYSPM_SUCCESS - Entered and exited from BLESS deep sleep.
* * CY_SYSPM_FAIL - BLESS deep sleep not entered.
*
*******************************************************************************/
static cy_en_syspm_status_t Cy_BLE_DeepSleepCallbackSingleCore(cy_stc_syspm_callback_params_t *callbackParams,
cy_en_syspm_callback_mode_t mode)
{
/* Suppress unused variable warning */
(void) callbackParams;
(void) mode;
#if defined(COMPONENT_BLESS_HOST)
cy_en_syspm_status_t retVal;
static uint32_t interruptState = 0u;
cy_en_ble_bless_state_t blessState;
/* Local variable to store the status of BLESS Hardware block */
cy_en_ble_lp_mode_t sleepMode;
switch(mode)
{
case (CY_SYSPM_CHECK_READY):
if(Cy_BLE_GetState() == CY_BLE_STATE_INITIALIZING)
{
retVal = CY_SYSPM_FAIL;
}
else if(Cy_IPC_Sema_Status(CY_BLE_SEMA) == CY_IPC_SEMA_STATUS_LOCKED)
{
/* System do not enter CPU deep sleep if BLE Host start write operation */
retVal = CY_SYSPM_FAIL;
}
else if (Cy_BLE_HAL_IsEcoCpuClockSrc() == 1u)
{
/* System never enters CPU deep sleep if BLE ECO is CPU source */
retVal = CY_SYSPM_FAIL;
}
else
{
cy_en_ble_api_result_t retIsControllerActive;
/* Put BLESS into deep sleep and check the return status */
sleepMode = Cy_BLE_StackEnterLPM(CY_BLE_BLESS_DEEPSLEEP);
/* Disable global interrupt to prevent changes from any other interrupt ISR */
interruptState = Cy_SysLib_EnterCriticalSection();
/* Check the Status of BLESS */
blessState = Cy_BLE_StackGetBleSsState();
if(blessState == CY_BLE_BLESS_STATE_STOPPED)
{
retVal = CY_SYSPM_SUCCESS;
}
else if(sleepMode == CY_BLE_BLESS_DEEPSLEEP)
{
retIsControllerActive = Cy_BLE_MappingIsControllerActive(CY_BLE_CONTROLLER_SLEEP_MODE_DEEPSLEEP);
if(((blessState == CY_BLE_BLESS_STATE_ECO_ON) || (blessState == CY_BLE_BLESS_STATE_DEEPSLEEP))
&& (retIsControllerActive == CY_BLE_SUCCESS))
{
/* Enter device CPU deep sleep */
retVal = CY_SYSPM_SUCCESS;
}
else
{
/* The BLESS hardware block cannot go to CPU deep sleep; try CPU sleep mode */
retVal = CY_SYSPM_FAIL;
}
}
else
{
retIsControllerActive = Cy_BLE_MappingIsControllerActive(CY_BLE_CONTROLLER_SLEEP_MODE_SLEEP);
if((blessState != CY_BLE_BLESS_STATE_EVENT_CLOSE) &&
(retIsControllerActive == CY_BLE_SUCCESS))
{
/* If the BLESS hardware block cannot go to CPU deep sleep and BLE event has not
* closed yet, then place CPU to sleep */
(void)Cy_SysPm_CpuEnterSleep(CY_SYSPM_WAIT_FOR_INTERRUPT);
}
retVal = CY_SYSPM_FAIL;
}
if(retVal == CY_SYSPM_FAIL)
{
/* Enable interrupts after failing check */
Cy_SysLib_ExitCriticalSection(interruptState);
}
}
break;
/* Enable interrupts after wakeup */
case (CY_SYSPM_AFTER_TRANSITION):
Cy_SysLib_ExitCriticalSection(interruptState);
retVal = CY_SYSPM_SUCCESS;
break;
default:
retVal = CY_SYSPM_FAIL;
break;
}
return(retVal);
#else
return(CY_SYSPM_SUCCESS);
#endif /*defined(COMPONENT_BLESS_HOST) */
}
/*******************************************************************************
* Function Name: Cy_BLE_DeepSleepCallbackDualCore
****************************************************************************//**