-
Notifications
You must be signed in to change notification settings - Fork 0
/
ble_findme.c
532 lines (462 loc) · 18.4 KB
/
ble_findme.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
/******************************************************************************
* File Name: ble_findme.c
*
* Description: This file contains BLE related functions.
*
* Related Document: README.md
*
*******************************************************************************
* Copyright 2019-2021, Cypress Semiconductor Corporation (an Infineon company) or
* an affiliate of Cypress Semiconductor Corporation. All rights reserved.
*
* This software, including source code, documentation and related
* materials ("Software") is owned by Cypress Semiconductor Corporation
* or one of its affiliates ("Cypress") and is protected by and subject to
* worldwide patent protection (United States and foreign),
* United States copyright laws and international treaty provisions.
* Therefore, you may use this Software only as provided in the license
* agreement accompanying the software package from which you
* obtained this Software ("EULA").
* If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
* non-transferable license to copy, modify, and compile the Software
* source code solely for use in connection with Cypress's
* integrated circuit products. Any reproduction, modification, translation,
* compilation, or representation of this Software except as specified
* above is prohibited without the express written permission of Cypress.
*
* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
* reserves the right to make changes to the Software without notice. Cypress
* does not assume any liability arising out of the application or use of the
* Software or any product or circuit described in the Software. Cypress does
* not authorize its products for use in any products where a malfunction or
* failure of the Cypress product may reasonably be expected to result in
* significant property damage, injury or death ("High Risk Product"). By
* including Cypress's product in a High Risk Product, the manufacturer
* of such system or application assumes all risk of such use and in doing
* so agrees to indemnify Cypress against all liability.
*******************************************************************************/
/******************************************************************************
* Include header files
*****************************************************************************/
#include "ble_findme.h"
#include "cyhal.h"
#include "cy_retarget_io.h"
#include "cybsp.h"
#include "cycfg_ble.h"
/*******************************************************************************
* Macros
********************************************************************************/
#define BLESS_INTR_PRIORITY (1u)
#define WAKEUP_INTR_PRIORITY (7u)
#define WAKEUP_TIMER_DELAY_MS (250)
/* Timer value to get 0.25 sec with wakeup timer input clock of 32768 Hz,
* where 32768Hz is LFCLK in PSoC 6 MCU */
#define WAKEUP_TIMER_MATCH_VALUE (WAKEUP_TIMER_DELAY_MS * 32768 / 1000)
/*******************************************************************************
* Global Variables
********************************************************************************/
cyhal_lptimer_t wakeup_timer;
bool wakeup_intr_flag = false;
bool gpio_intr_flag = false;
uint8 alert_level = CY_BLE_NO_ALERT;
cy_stc_ble_conn_handle_t app_conn_handle;
/*******************************************************************************
* Function Prototypes
********************************************************************************/
static void ble_init(void);
static void bless_interrupt_handler(void);
static void stack_event_handler(uint32 event, void* eventParam);
static void ble_start_advertisement(void);
static void wakeup_timer_init(void);
static void wakeup_timer_interrupt_handler(void *handler_arg, cyhal_lptimer_event_t event);
static void ble_ias_callback(uint32 event, void *eventParam);
static void enter_low_power_mode(void);
/*******************************************************************************
* Function Name: ble_findme_init
********************************************************************************
* Summary:
* This function initializes the BLE and a deep sleep wakeup timer.
*
*******************************************************************************/
void ble_findme_init(void)
{
/* Configure BLE */
ble_init();
/* Configure deep sleep wakeup timer */
wakeup_timer_init();
/* Enable global interrupts */
__enable_irq();
}
/*******************************************************************************
* Function Name: ble_findme_process
********************************************************************************
* Summary:
* This function processes the BLE events and configures the device to enter
* low power mode as required.
*
*******************************************************************************/
void ble_findme_process(void)
{
/* Enter low power mode. The call to enter_low_power_mode also causes the
* device to enter hibernate mode if the BLE stack is shutdown.
*/
enter_low_power_mode();
/* Cy_BLE_ProcessEvents() allows the BLE stack to process pending events */
Cy_BLE_ProcessEvents();
if(wakeup_intr_flag)
{
wakeup_intr_flag = false;
/* Update LED1 to indicate current BLE status */
if(CY_BLE_ADV_STATE_ADVERTISING == Cy_BLE_GetAdvertisementState())
{
cyhal_gpio_toggle((cyhal_gpio_t)LED1);
}
else if(CY_BLE_CONN_STATE_CONNECTED == Cy_BLE_GetConnectionState(app_conn_handle))
{
cyhal_gpio_write((cyhal_gpio_t)LED1, CYBSP_LED_STATE_ON);
}
else
{
cyhal_gpio_write((cyhal_gpio_t)LED1, CYBSP_LED_STATE_OFF);
}
/* Update LED2 to indicate current alert level */
switch(alert_level)
{
case CY_BLE_NO_ALERT:
{
cyhal_gpio_write((cyhal_gpio_t)LED2, CYBSP_LED_STATE_OFF);
break;
}
case CY_BLE_MILD_ALERT:
{
cyhal_gpio_toggle((cyhal_gpio_t)LED2);
break;
}
case CY_BLE_HIGH_ALERT:
{
cyhal_gpio_write((cyhal_gpio_t)LED2, CYBSP_LED_STATE_ON);
break;
}
default:
{
break;
}
}
}
}
/*******************************************************************************
* Function Name: ble_init
********************************************************************************
* Summary:
* This function initializes the BLE and registers IAS callback function.
*
*******************************************************************************/
static void ble_init(void)
{
static const cy_stc_sysint_t bless_isr_config =
{
/* The BLESS interrupt */
.intrSrc = bless_interrupt_IRQn,
/* The interrupt priority number */
.intrPriority = BLESS_INTR_PRIORITY
};
/* Hook interrupt service routines for BLESS */
(void) Cy_SysInt_Init(&bless_isr_config, bless_interrupt_handler);
/* Store the pointer to blessIsrCfg in the BLE configuration structure */
cy_ble_config.hw->blessIsrConfig = &bless_isr_config;
/* Registers the generic callback functions */
Cy_BLE_RegisterEventCallback(stack_event_handler);
/* Initializes the BLE host */
Cy_BLE_Init(&cy_ble_config);
/* Enables BLE */
Cy_BLE_Enable();
/* Enables BLE Low-power mode (LPM)*/
Cy_BLE_EnableLowPowerMode();
/* Register IAS event handler */
Cy_BLE_IAS_RegisterAttrCallback(ble_ias_callback);
}
/******************************************************************************
* Function Name: bless_interrupt_handler
*******************************************************************************
* Summary:
* Wrapper function for handling interrupts from BLESS.
*
******************************************************************************/
static void bless_interrupt_handler(void)
{
Cy_BLE_BlessIsrHandler();
}
/*******************************************************************************
* Function Name: stack_event_handler
********************************************************************************
*
* Summary:
* This is an event callback function to receive events from the BLE Component.
*
* Parameters:
* uint32 event: event from the BLE component
* void* eventParam: parameters related to the event
*
*******************************************************************************/
static void stack_event_handler(uint32_t event, void* eventParam)
{
switch(event)
{
/**********************************************************************
* General events
*********************************************************************/
/* This event is received when the BLE stack is started */
case CY_BLE_EVT_STACK_ON:
{
printf("[INFO] : BLE stack started \r\n");
ble_start_advertisement();
break;
}
/* This event is received when there is a timeout */
case CY_BLE_EVT_TIMEOUT:
{
/* Reason for Timeout */
cy_en_ble_to_reason_code_t reason_code =
((cy_stc_ble_timeout_param_t*)eventParam)->reasonCode;
switch(reason_code)
{
case CY_BLE_GAP_ADV_TO:
{
printf("[INFO] : Advertisement timeout event \r\n");
break;
}
case CY_BLE_GATT_RSP_TO:
{
printf("[INFO] : GATT response timeout\r\n");
break;
}
default:
{
printf("[INFO] : BLE timeout event\r\n");
break;
}
}
break;
}
/* This event indicates completion of Set LE event mask */
case CY_BLE_EVT_LE_SET_EVENT_MASK_COMPLETE:
{
printf("[INFO] : Set LE mask event mask command completed\r\n");
break;
}
/* This event indicates set device address command completed */
case CY_BLE_EVT_SET_DEVICE_ADDR_COMPLETE:
{
printf("[INFO] : Set device address command has completed \r\n");
break;
}
/* This event indicates set Tx Power command completed */
case CY_BLE_EVT_SET_TX_PWR_COMPLETE:
{
printf("[INFO] : Set Tx power command completed\r\n");
break;
}
/* This event indicates BLE Stack Shutdown is completed */
case CY_BLE_EVT_STACK_SHUTDOWN_COMPLETE:
{
printf("[INFO] : BLE shutdown complete\r\n");
break;
}
/**********************************************************************
* GAP events
*********************************************************************/
/* This event is generated at the GAP Peripheral end after connection
* is completed with peer Central device
*/
case CY_BLE_EVT_GAP_DEVICE_CONNECTED:
{
printf("[INFO] : GAP device connected \r\n");
break;
}
/* This event is triggered instead of 'CY_BLE_EVT_GAP_DEVICE_CONNECTED',
* if Link Layer Privacy is enabled in component customizer
*/
case CY_BLE_EVT_GAP_ENHANCE_CONN_COMPLETE:
{
printf("[INFO] : GAP enhanced connection complete \r\n");
break;
}
/* This event is generated when disconnected from remote device or
* failed to establish connection
*/
case CY_BLE_EVT_GAP_DEVICE_DISCONNECTED:
{
if(CY_BLE_CONN_STATE_DISCONNECTED ==
Cy_BLE_GetConnectionState(app_conn_handle))
{
printf("[INFO] : GAP device disconnected\r\n");
alert_level = CY_BLE_NO_ALERT;
ble_start_advertisement();
}
break;
}
/* This event indicates that the GAP Peripheral device has
* started/stopped advertising
*/
case CY_BLE_EVT_GAPP_ADVERTISEMENT_START_STOP:
{
if(CY_BLE_ADV_STATE_ADVERTISING == Cy_BLE_GetAdvertisementState())
{
printf("[INFO] : BLE advertisement started\r\n");
}
else
{
printf("[INFO] : BLE advertisement stopped\r\n");
Cy_BLE_Disable();
}
break;
}
/**********************************************************************
* GATT events
*********************************************************************/
/* This event is generated at the GAP Peripheral end after connection
* is completed with peer Central device
*/
case CY_BLE_EVT_GATT_CONNECT_IND:
{
app_conn_handle = *(cy_stc_ble_conn_handle_t *)eventParam;
printf("[INFO] : GATT device connected\r\n");
break;
}
/* This event is generated at the GAP Peripheral end after disconnection */
case CY_BLE_EVT_GATT_DISCONNECT_IND:
{
printf("[INFO] : GATT device disconnected\r\n");
break;
}
/* This event indicates that the 'GATT MTU Exchange Request' is received */
case CY_BLE_EVT_GATTS_XCNHG_MTU_REQ:
{
printf("[INFO] : GATT MTU Exchange Request received \r\n");
break;
}
/* This event received when GATT read characteristic request received */
case CY_BLE_EVT_GATTS_READ_CHAR_VAL_ACCESS_REQ:
{
printf("[INFO] : GATT read characteristic request received \r\n");
break;
}
default:
{
printf("[INFO] : BLE Event 0x%lX\r\n", (unsigned long) event);
}
}
}
/*******************************************************************************
* Function Name: ble_ias_callback
********************************************************************************
* Summary:
* This is an event callback function to receive events from the BLE, which are
* specific to Immediate Alert Service.
*
* Parameters:
* uint32 event: event from the BLE component
* void* eventParams: parameters related to the event
*
*******************************************************************************/
void ble_ias_callback(uint32 event, void *eventParam)
{
/* Alert Level Characteristic write event */
if(event == CY_BLE_EVT_IASS_WRITE_CHAR_CMD)
{
/* Read the updated Alert Level value from the GATT database */
Cy_BLE_IASS_GetCharacteristicValue(CY_BLE_IAS_ALERT_LEVEL,
sizeof(alert_level), &alert_level);
}
/* Remove warning for unused parameter */
(void)eventParam;
}
/******************************************************************************
* Function Name: ble_start_advertisement
*******************************************************************************
* Summary:
* This function starts the advertisement.
*
******************************************************************************/
static void ble_start_advertisement(void)
{
cy_en_ble_api_result_t ble_api_result;
if((CY_BLE_ADV_STATE_ADVERTISING != Cy_BLE_GetAdvertisementState()) &&
(Cy_BLE_GetNumOfActiveConn() < CY_BLE_CONN_COUNT))
{
ble_api_result = Cy_BLE_GAPP_StartAdvertisement(CY_BLE_ADVERTISING_FAST,CY_BLE_PERIPHERAL_CONFIGURATION_0_INDEX);
if(CY_BLE_SUCCESS != ble_api_result)
{
printf("[ERROR] : Failed to start advertisement \r\n");
}
}
}
/*******************************************************************************
* Function Name: wakeup_timer_interrupt_handler
********************************************************************************
* Summary:
* wakeup_timer interrupt handler.
*
* Parameters:
* void *handler_arg (unused)
* cyhal_lptimer_irq_event_t event (unused)
*
*******************************************************************************/
void wakeup_timer_interrupt_handler(void *handler_arg, cyhal_lptimer_event_t event)
{
/* Set the interrupt flag */
wakeup_intr_flag = true;
/* Reload the timer to get periodic interrupt */
cyhal_lptimer_reload(&wakeup_timer);
cyhal_lptimer_set_match(&wakeup_timer, WAKEUP_TIMER_MATCH_VALUE);
}
/*******************************************************************************
* Function Name: wakeup_timer_init
********************************************************************************
* Summary:
* Initialize deep sleep wakeup timer for generating interrupt.
*
*******************************************************************************/
static void wakeup_timer_init(void)
{
cyhal_lptimer_init(&wakeup_timer);
cyhal_lptimer_set_match(&wakeup_timer, WAKEUP_TIMER_MATCH_VALUE);
cyhal_lptimer_reload(&wakeup_timer);
cyhal_lptimer_register_callback(&wakeup_timer, wakeup_timer_interrupt_handler, NULL);
cyhal_lptimer_enable_event(&wakeup_timer, CYHAL_LPTIMER_COMPARE_MATCH,
WAKEUP_INTR_PRIORITY, true);
}
/*******************************************************************************
* Function Name: enter_low_power_mode
********************************************************************************
* Summary:
* Configures the device to enter low power mode.
*
* The function configures the device to enter deep sleep - whenever the
* BLE is idle and the UART transmission/reception is not happening.
*
* In case if BLE is turned off, the function configures the device to
* enter hibernate mode.
*
*******************************************************************************/
static void enter_low_power_mode(void)
{
/* Enter hibernate mode if BLE is turned off */
if(CY_BLE_STATE_STOPPED == Cy_BLE_GetState())
{
printf("[INFO] : Entering hibernate mode\r\n");
/* Turn of user LEDs */
cyhal_gpio_write((cyhal_gpio_t)LED1, CYBSP_LED_STATE_OFF);
cyhal_gpio_write((cyhal_gpio_t)LED2, CYBSP_LED_STATE_OFF);
/* Wait until UART transfer complete */
while(1UL == cyhal_uart_is_tx_active(&cy_retarget_io_uart_obj));
cyhal_syspm_hibernate(CYHAL_SYSPM_HIBERNATE_PINA_LOW);
}
else
{
cyhal_syspm_deepsleep();
}
}
/* [] END OF FILE */