-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.c
188 lines (151 loc) · 6.28 KB
/
main.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
/***********************************************************************************************//**
* \file main.c
* \brief Silicon Labs Empty Example Project
*
* This example demonstrates the bare minimum needed for a Blue Gecko C application
* that allows Over-the-Air Device Firmware Upgrading (OTA DFU). The application
* starts advertising after boot and restarts advertising after a connection is closed.
***************************************************************************************************
* <b> (C) Copyright 2016 Silicon Labs, http://www.silabs.com</b>
***************************************************************************************************
* This file is licensed under the Silabs License Agreement. See the file
* "Silabs_License_Agreement.txt" for details. Before using this software for
* any purpose, you must agree to the terms of that agreement.
**************************************************************************************************/
/* Board headers */
#include "init_mcu.h"
#include "init_board.h"
#include "init_app.h"
#include "ble-configuration.h"
#include "board_features.h"
/* Bluetooth stack headers */
#include "bg_types.h"
#include "native_gecko.h"
#include "gatt_db.h"
/* Libraries containing default Gecko configuration values */
#include "em_emu.h"
#include "em_cmu.h"
/* Device initialization header */
#include "hal-config.h"
#if defined(HAL_CONFIG)
#include "bsphalconfig.h"
#else
#include "bspconfig.h"
#endif
// Custom LED Application
#include "led_app.h"
/***********************************************************************************************//**
* @addtogroup Application
* @{
**************************************************************************************************/
/***********************************************************************************************//**
* @addtogroup app
* @{
**************************************************************************************************/
#ifndef MAX_CONNECTIONS
#define MAX_CONNECTIONS 4
#endif
uint8_t bluetooth_stack_heap[DEFAULT_BLUETOOTH_HEAP(MAX_CONNECTIONS)];
// Gecko configuration parameters (see gecko_configuration.h)
static const gecko_configuration_t config = {
.config_flags = 0,
.sleep.flags = SLEEP_FLAGS_DEEP_SLEEP_ENABLE,
.bluetooth.max_connections = MAX_CONNECTIONS,
.bluetooth.heap = bluetooth_stack_heap,
.bluetooth.heap_size = sizeof(bluetooth_stack_heap),
.bluetooth.sleep_clock_accuracy = 100, // ppm
.gattdb = &bg_gattdb_data,
.ota.flags = 0,
.ota.device_name_len = 3,
.ota.device_name_ptr = "OTA",
#if (HAL_PA_ENABLE) && defined(FEATURE_PA_HIGH_POWER)
.pa.config_enable = 1, // Enable high power PA
.pa.input = GECKO_RADIO_PA_INPUT_VBAT, // Configure PA input to VBAT
#endif // (HAL_PA_ENABLE) && defined(FEATURE_PA_HIGH_POWER)
};
// Flag for indicating DFU Reset must be performed
uint8_t boot_to_dfu = 0;
uint8_t ledState = 0; // Stores LED state to pass to the read request
/**
* @brief Main function
*/
void main(void)
{
// Initialize device
initMcu();
// Initialize board
initBoard();
// Initialize application
initApp();
// Initialize stack
gecko_init(&config);
while (1) {
/* Event pointer for handling events */
struct gecko_cmd_packet* evt;
/* Check for stack event. */
evt = gecko_wait_event();
/* Handle events */
switch (BGLIB_MSG_ID(evt->header)) {
/* This boot event is generated when the system boots up after reset.
* Do not call any stack commands before receiving the boot event.
* Here the system is set to start advertising immediately after boot procedure. */
case gecko_evt_system_boot_id:
/* Set LEDs to their initial condition */
initLEDs();
/* Set advertising parameters. 100ms advertisement interval. All channels used.
* The first two parameters are minimum and maximum advertising interval, both in
* units of (milliseconds * 1.6). The third parameter '7' sets advertising on all channels. */
gecko_cmd_le_gap_set_adv_parameters(160, 160, 7);
/* Start general advertising and enable connections. */
gecko_cmd_le_gap_set_mode(le_gap_general_discoverable, le_gap_undirected_connectable);
break;
case gecko_evt_le_connection_closed_id:
/* Check if need to boot to dfu mode */
if (boot_to_dfu) {
/* Enter to DFU OTA mode */
gecko_cmd_system_reset(2);
} else {
/* Restart advertising after client has disconnected */
gecko_cmd_le_gap_set_mode(le_gap_general_discoverable, le_gap_undirected_connectable);
}
break;
case gecko_evt_gatt_server_user_write_request_id:
/* Automation IO digital control */
if (evt->data.evt_gatt_server_user_write_request.characteristic == gattdb_digital_out) {
LED_control(evt->data.evt_gatt_server_attribute_value.value.data[0]);
gecko_cmd_gatt_server_send_user_write_response(
evt->data.evt_gatt_server_user_write_request.connection,
gattdb_digital_out,
bg_err_success);
}
/* Check if the user-type OTA Control Characteristic was written.
* If ota_control was written, boot the device into Device Firmware Upgrade (DFU) mode. */
if (evt->data.evt_gatt_server_user_write_request.characteristic == gattdb_ota_control) {
/* Set flag to enter to OTA mode */
boot_to_dfu = 1;
/* Send response to Write Request */
gecko_cmd_gatt_server_send_user_write_response(
evt->data.evt_gatt_server_user_write_request.connection,
gattdb_ota_control,
bg_err_success);
/* Close connection to enter to DFU OTA mode */
gecko_cmd_endpoint_close(evt->data.evt_gatt_server_user_write_request.connection);
}
break;
case gecko_evt_gatt_server_user_read_request_id:
/* returns the state of the LEDs */
ledState = LED_status();
gecko_cmd_gatt_server_send_user_read_response(
evt->data.evt_gatt_server_user_read_request.connection,
gattdb_digital_out,
bg_err_success,
sizeof(ledState),
&ledState);
break;
default:
break;
}
}
}
/** @} (end addtogroup app) */
/** @} (end addtogroup Application) */