-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
329 lines (290 loc) · 12.4 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
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
/******************************************************************************
* File Name: main.c
*
* Description: This is the source code for the Empty PSoC6 Application
* for ModusToolbox.
*
* Related Document: See Readme.md
*
*******************************************************************************
* (c) 2019-2020, 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
* subsidiaries ("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.
*******************************************************************************/
/******************************************************************************/
/* Includes */
/******************************************************************************/
#include "cy_pdl.h"
#include "cyhal.h"
#include "cybsp.h"
#include "transport_ble.h"
/******************************************************************************/
/* Defines */
/******************************************************************************/
/******************************************************************************/
/* Function Prototypes */
/******************************************************************************/
void AppCallBack(uint32_t event, void* eventParam);
/******************************************************************************/
/* Interrupt Service Routines */
/******************************************************************************/
/******************************************************************************/
/* Global Variables */
/******************************************************************************/
/******************************************************************************/
/* Function Definitions */
/******************************************************************************/
/*******************************************************************************
* Function: main
* Author:
* Description: The entry point of the program.
* Date: 03-23-20
*******************************************************************************/
int main(void)
{
cy_rslt_t result;
uint32_t count; // Used to count seconds
cy_en_dfu_status_t status; // Status codes for DFU API
uint32_t state; // DFU state
const uint32_t paramsTimeout = 20u; // Cy_DFU_Continue() timeout (ms)
/* Buffer to store DFU commands */
CY_ALIGN(4) static uint8_t buffer[CY_DFU_SIZEOF_DATA_BUFFER];
/* Buffer for DFU data packets for transport API */
CY_ALIGN(4) static uint8_t packet[CY_DFU_SIZEOF_CMD_BUFFER ];
/* DFU params, used to configure DFU */
cy_stc_dfu_params_t dfuParams;
/* Initialize dfuParams structure */
dfuParams.timeout = paramsTimeout;
dfuParams.dataBuffer = &buffer[0];
dfuParams.packetBuffer = &packet[0];
status = Cy_DFU_Init(&state, &dfuParams);
/* Initialize the device and board peripherals */
result = cybsp_init() ;
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
__enable_irq();
/* Initialize DFU communication */
Cy_DFU_TransportStart();
count = 0;
for(;;)
{
status = Cy_DFU_Continue(&state, &dfuParams);
++count;
if (state == CY_DFU_STATE_FINISHED)
{
/* Finished loading the application image */
/* Validate DFU application, if it is valid then switch to it */
status = Cy_DFU_ValidateApp(1u, &dfuParams);
if (status == CY_DFU_SUCCESS)
{
Cy_DFU_TransportStop();
Cy_DFU_ExecuteApp(1u);
}
else if (status == CY_DFU_ERROR_VERIFY)
{
/*
* Restarts loading, an alternatives are to Halt MCU here
* or switch to the other app if it is valid.
* Error code may be handled here, i.e. print to debug UART.
*/
status = Cy_DFU_Init(&state, &dfuParams);
Cy_DFU_TransportReset();
}
}
else if (state == CY_DFU_STATE_FAILED)
{
/* An error has happened during the loading process */
/* Handle it here */
/* In this Code Example just restart loading process */
status = Cy_DFU_Init(&state, &dfuParams);
Cy_DFU_TransportReset();
}
else if (state == CY_DFU_STATE_UPDATING)
{
uint32_t passed5seconds = (count >= (5000ul/paramsTimeout)) ? 1 : 0;
/*
* if no command has been received during 5 seconds when the loading
* has started then restart loading.
*/
if (status == CY_DFU_SUCCESS)
{
count = 0u;
}
else if (status == CY_DFU_ERROR_TIMEOUT)
{
if (passed5seconds != 0u)
{
count = 0u;
Cy_DFU_Init(&state, &dfuParams);
Cy_DFU_TransportReset();
}
}
else
{
count = 0u;
/* Delay because Transport still may be sending error response to
* a host
*/
Cy_SysLib_Delay(paramsTimeout);
Cy_DFU_Init(&state, &dfuParams);
Cy_DFU_TransportReset();
}
}
/* If SW2 pressed, switch to App1 if it is valid */
if (Cy_GPIO_Read(PIN_SW2_PORT, PIN_SW2_PIN) == 0u)
{
/* 50 ms delay for button debounce on button press */
Cy_SysLib_Delay(50u);
if (Cy_GPIO_Read(PIN_SW2_PORT, PIN_SW2_PIN) == 0u)
{
while (Cy_GPIO_Read(PIN_SW2_PORT, PIN_SW2_PIN) == 0u)
{ /* 50 ms delay for button debounce on button release */
Cy_SysLib_Delay(50u);
}
/* Validate and switch to App1 */
status = Cy_DFU_ValidateApp(1u, &dfuParams);
if (status == CY_DFU_SUCCESS)
{
Cy_DFU_TransportStop();
Cy_DFU_ExecuteApp(1u);
}
}
}
}
}
/*******************************************************************************
* Function: Cy_OnResetUser
* Author: Cypress Semiconductor
* Description: This function is called at the start of Reset_Handler(). DFU
* requires it to call Cy_DFU_OnResetApp0() in app#0.
* Date: 05-20-19
*******************************************************************************/
void Cy_OnResetUser(void)
{
Cy_DFU_OnResetApp0();
}
/*******************************************************************************
* Function: AppCallBack
* Author: Cypress Semiconductor (modified by Matt Mielke)
* Description: This is an event callback function to receive events from the
* BLE Component. Used in Cy_DFU_TransportStart()
* Date: 03-23-20
*******************************************************************************/
void AppCallBack(uint32_t event, void* eventParam)
{
static cy_stc_ble_gap_sec_key_info_t keyInfo =
{
.localKeysFlag = CY_BLE_GAP_SMP_INIT_ENC_KEY_DIST |
CY_BLE_GAP_SMP_INIT_IRK_KEY_DIST |
CY_BLE_GAP_SMP_INIT_CSRK_KEY_DIST,
.exchangeKeysFlag = CY_BLE_GAP_SMP_INIT_ENC_KEY_DIST |
CY_BLE_GAP_SMP_INIT_IRK_KEY_DIST |
CY_BLE_GAP_SMP_INIT_CSRK_KEY_DIST |
CY_BLE_GAP_SMP_RESP_ENC_KEY_DIST |
CY_BLE_GAP_SMP_RESP_IRK_KEY_DIST |
CY_BLE_GAP_SMP_RESP_CSRK_KEY_DIST,
};
switch(event)
{
/**********************************************************************
* General events
*********************************************************************/
/* This event is received when the BLE stack is started */
case CY_BLE_EVT_STACK_ON:
{
/* Enter into discoverable mode so that remote can search it. */
Cy_BLE_GAPP_StartAdvertisement(CY_BLE_ADVERTISING_FAST, 0u);
Cy_BLE_GAP_GenerateKeys(&keyInfo);
break;
}
/**********************************************************************
* GAP events
*********************************************************************/
case CY_BLE_EVT_GAP_AUTH_REQ:
{
if (cy_ble_configPtr->authInfo[CY_BLE_SECURITY_CONFIGURATION_0_INDEX].security
== (CY_BLE_GAP_SEC_MODE_1 | CY_BLE_GAP_SEC_LEVEL_1))
{
cy_ble_configPtr->authInfo[CY_BLE_SECURITY_CONFIGURATION_0_INDEX].authErr =
CY_BLE_GAP_AUTH_ERROR_PAIRING_NOT_SUPPORTED;
}
cy_ble_configPtr->authInfo[CY_BLE_SECURITY_CONFIGURATION_0_INDEX].bdHandle =
((cy_stc_ble_gap_auth_info_t *)eventParam)->bdHandle;
Cy_BLE_GAPP_AuthReqReply(&cy_ble_configPtr->authInfo[CY_BLE_SECURITY_CONFIGURATION_0_INDEX]);
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:
{
/* sets the security keys that are to be exchanged with a peer
* device during key exchange stage of the authentication procedure
*/
keyInfo.SecKeyParam.bdHandle =
(*(cy_stc_ble_gap_enhance_conn_complete_param_t *)eventParam).bdHandle;
Cy_BLE_GAP_SetSecurityKeys(&keyInfo);
break;
}
/* This event indicates security key generation complete */
case CY_BLE_EVT_GAP_KEYS_GEN_COMPLETE:
{
keyInfo.SecKeyParam = (*(cy_stc_ble_gap_sec_key_param_t *)eventParam);
Cy_BLE_GAP_SetIdAddress(&cy_ble_deviceAddress);
break;
}
/* This event is generated when disconnected from remote device or
* failed to establish connection
*/
case CY_BLE_EVT_GAP_DEVICE_DISCONNECTED:
{
/* Enter into discoverable mode so that remote can search it. */
Cy_BLE_GAPP_StartAdvertisement(CY_BLE_ADVERTISING_FAST, 0u);
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:
{
appConnHandle = *(cy_stc_ble_conn_handle_t *)eventParam;
break;
}
default:
{
break;
}
}
}
/* [] END OF FILE */