English
Language : 

AN91184 Datasheet, PDF (23/36 Pages) Ramtron International Corporation – Designing BLE Applications
PSoC® 4 BLE – Designing BLE Applications
4.2.1
System Initialization
When the device is reset or wakes up from the Hibernate mode, the firmware performs initialization, which includes
starting the SAR ADC, enabling global interrupts, starting the opamps, and starting the watchdog timer. After the
system is initialized, it initializes the BLE Component, which handles the initialization of the complete BLE subsystem.
Note: As part of the BLE Component initialization, the user code must pass a pointer to the event-handler function
that should be called to receive events from the BLE stack. The Generic Event Handler shown in Figure 25 is
registered as a part of the BLE initialization. Code 1 shows the code to start the BLE Component and register the
Generic Event Handler.
Code 1. BLE Initialization
apiResult = CyBle_Start(GenericEventHandler);
If the BLE Component initializes successfully, the firmware registers the function that is called to receive the events
for the Health Thermometer Service and switches to the normal operation mode. Code 2 shows the snippet for
registering the Health Thermometer Service.
Code 2. Health Thermometer Service Event Handler
CyBle_HtsRegisterAttrCallback(HealthThermometetEventHandler);
4.2.2
Event Handler
In the BLE Component, results of any operation performed on the BLE stack are relayed to the application firmware
via a list of events. These events provide the BLE interface status and data. Events can be categorized as follows:
 Common events
Operations performed at the GAP layer, the GATT layer, and the stack’s L2CAP layer generate these events. For
example, a CYBLE_EVT_STACK_ON event is received when the BLE stack is initialized and turned ON, a
CYBLE_EVT_GAP_DEVICE_CONNECTED event is received when a connection with a remote device is
established, and a CYBLE_EVT_GATTS_WRITE_CMD_REQ event is generated when a Write Command is
received from the client. For more details on common events, refer to the API documentation of the BLE
Component (right-click the BLE Component in PSoC Creator and select Open API Documentation).
The application firmware must include an event handler function to successfully establish and maintain the BLE
link. Code 3 shows the implementation of the GenericEventHandler function, where events generated on the
initialization of the BLE stack, device connection, disconnection, and timeout are handled.
 Service-specific events
Service-specific events are generated because of operations performed on the standard services defined by the
Bluetooth SIG. For example, a CYBLE_EVT_HTSS_INDICATION_ENABLED event is received by the server
when the client writes the client configuration characteristic descriptor to enable the indication for the
Temperature Measurement Characteristic. For more details on service-specific events, refer to the API
documentation of the BLE Component.
The BLE Component can route these events to a service-specific event handler. The application firmware should
include a service-specific event handler function to handle these events. If a service-specific event handler is not
supported, then these events must be handled by the common event handler (GenericEventHandler). Code 4
shows the implementation of the service-specific event handler called HtssEventHandler.
Code 3. Generic Event Handler
void GenericEventHandler(uint32 event, void *eventParam)
{
switch(event)
{
/* This event is received when component is Started */
case CYBLE_EVT_STACK_ON:
/* Stop watchdog to reduce power consumption during advertising */
WatchdogTimer_Stop();
/* Start Advertisement and enter Discoverable mode*/
CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);
www.cypress.com
Document No. 001-91184 Rev. *B
23