-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
521 lines (427 loc) · 12.3 KB
/
main.cpp
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
/*
* App.cpp
*
* Created on: 8 oct. 2017
* Author: Vincent
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "hardfault.h"
#include "i2c.h"
#include "helper.h"
#include "ant.h"
#include "bsp.h"
#include "i2c.h"
#include "millis.h"
#include "boards.h"
#include "ble_api_base.h"
#include "app_scheduler.h"
#include "app_timer.h"
#include "nrf_sdm.h"
#include "nrf_strerror.h"
#include "nrf_drv_timer.h"
#include "nrf_drv_clock.h"
#include "task_manager.h"
#include "nrf_bootloader_info.h"
#include "nrfx_wdt.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "i2c_scheduler.h"
#include "Model.h"
#include "segger_wrapper.h"
#include "WString.h"
#ifdef SOFTDEVICE_PRESENT
#include "nrf_sdh.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
#include "nrf_pwr_mgmt.h"
#ifdef __cplusplus
}
#endif
#define APP_DELAY APP_TIMER_TICKS(APP_TIMEOUT_DELAY_MS)
#define SCHED_MAX_EVENT_DATA_SIZE APP_TIMER_SCHED_EVENT_DATA_SIZE /**< Maximum size of scheduler events. */
#ifdef SVCALL_AS_NORMAL_FUNCTION
#define SCHED_QUEUE_SIZE 20 /**< Maximum number of events in the scheduler queue. More is needed in case of Serialization. */
#else
#define SCHED_QUEUE_SIZE 20 /**< Maximum number of events in the scheduler queue. */
#endif
nrfx_wdt_channel_id m_channel_id;
static bsp_event_t m_bsp_evt = BSP_EVENT_NOTHING;
APP_TIMER_DEF(m_job_timer);
/**
* @brief Handler for timer events.
*/
void timer_event_handler(void* p_context)
{
W_SYSVIEW_RecordEnterISR();
if (task_manager_is_started()) {
task_tick_manage(APP_TIMEOUT_DELAY_MS);
}
W_SYSVIEW_RecordExitISR();
}
/**@brief Callback function for asserts in the SoftDevice.
*
* @details This function will be called in case of an assert in the SoftDevice.
*
* @warning This handler is an example only and does not fit a final product. You need to analyze
* how your product is supposed to react in case of Assert.
* @warning On assert from the SoftDevice, the system can only recover on reset.
*
* @param[in] line_num Line number of the failing ASSERT call.
* @param[in] file_name File name of the failing ASSERT call.
*/
extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t * file_name)
{
assert_info_t assert_info =
{
.line_num = line_num,
.p_file_name = file_name,
};
app_error_fault_handler(NRF_FAULT_ID_SDK_ASSERT, 0, (uint32_t)(&assert_info));
LOG_WARNING("System reset");
LOG_FLUSH();
NVIC_SystemReset();
UNUSED_VARIABLE(assert_info);
}
/**
*
* @param id
* @param pc
* @param info
*/
extern "C" void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
{
NRF_LOG_FLUSH();
m_app_error.err_desc.id = id;
m_app_error.err_desc.pc = pc;
m_app_error.err_desc.crc = SYSTEM_DESCR_POS_CRC;
switch (id)
{
case NRF_FAULT_ID_SD_ASSERT:
NRF_LOG_ERROR("SOFTDEVICE: ASSERTION FAILED");
break;
case NRF_FAULT_ID_APP_MEMACC:
NRF_LOG_ERROR("SOFTDEVICE: INVALID MEMORY ACCESS");
break;
case NRF_FAULT_ID_SDK_ASSERT:
{
assert_info_t * p_info = (assert_info_t *)info;
snprintf(m_app_error.err_desc._buffer, sizeof(m_app_error.err_desc._buffer),
"ASSERTION FAILED at %s : l.%u",
p_info->p_file_name,
p_info->line_num);
#if USE_SVIEW
SEGGER_SYSVIEW_Error(m_app_error.err_desc._buffer);
#else
NRF_LOG_ERROR(m_app_error.err_desc._buffer);
LOG_ERROR(m_app_error.err_desc._buffer);
#endif
break;
}
case NRF_FAULT_ID_SDK_ERROR:
{
error_info_t * p_info = (error_info_t *)info;
snprintf(m_app_error.err_desc._buffer, sizeof(m_app_error.err_desc._buffer),
"ERROR 0x%X [%s] at %s : l.%" PRIu32 "",
(unsigned int)p_info->err_code,
nrf_strerror_get(p_info->err_code),
p_info->p_file_name,
p_info->line_num);
#if USE_SVIEW
SEGGER_SYSVIEW_Error(m_app_error.err_desc._buffer);
#else
NRF_LOG_ERROR("%s", m_app_error.err_desc._buffer);
#endif
break;
}
default:
NRF_LOG_ERROR("UNKNOWN FAULT at 0x%08X", pc);
snprintf(m_app_error.err_desc._buffer, sizeof(m_app_error.err_desc._buffer),
"UNKNOWN FAULT at 0x%08X", (unsigned int)pc);
break;
}
NRF_LOG_FLUSH();
}
extern "C" void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
{
NRF_LOG_ERROR("%s : l.%" PRIu32 " code 0x%" PRIX32 "", p_file_name, line_num, error_code);
NRF_LOG_FLUSH();
m_app_error.err_desc.id = error_code;
m_app_error.err_desc.pc = 0;
m_app_error.err_desc.crc = SYSTEM_DESCR_POS_CRC;
switch (error_code)
{
case NRF_FAULT_ID_SD_ASSERT:
NRF_LOG_ERROR("SOFTDEVICE: ASSERTION FAILED");
break;
case NRF_FAULT_ID_APP_MEMACC:
NRF_LOG_ERROR("SOFTDEVICE: INVALID MEMORY ACCESS");
break;
case NRF_FAULT_ID_SDK_ASSERT:
{
snprintf(m_app_error.err_desc._buffer, sizeof(m_app_error.err_desc._buffer),
"ASSERTION FAILED at %s : l.%" PRIu32 "",
(const char*)p_file_name,
line_num);
#if USE_SVIEW
SEGGER_SYSVIEW_Error(m_app_error.err_desc._buffer);
#else
NRF_LOG_ERROR(m_app_error.err_desc._buffer);
LOG_ERROR(m_app_error.err_desc._buffer);
#endif
break;
}
case NRF_FAULT_ID_SDK_ERROR:
{
snprintf(m_app_error.err_desc._buffer, sizeof(m_app_error.err_desc._buffer),
"ERROR 0x%X [%s] at %s : l.%" PRIu32 "",
(unsigned int)error_code,
nrf_strerror_get(error_code),
p_file_name,
line_num);
#if USE_SVIEW
SEGGER_SYSVIEW_Error(m_app_error.err_desc._buffer);
#else
NRF_LOG_ERROR("%s", m_app_error.err_desc._buffer);
#endif
break;
}
default:
break;
}
NRF_LOG_FLUSH();
}
extern "C" void HardFault_process(HardFault_stack_t * p_stack)
{
NRF_LOG_ERROR("HardFault: pc= %" PRIu32 "", p_stack->pc);
NRF_LOG_FLUSH();
m_app_error.hf_desc.crc = SYSTEM_DESCR_POS_CRC;
memcpy(&m_app_error.hf_desc.stck, p_stack, sizeof(HardFault_stack_t));
// Restart the system by default
NVIC_SystemReset();
}
/**@brief Function for initializing the nrf log module.
*/
static void log_init(void)
{
ret_code_t err_code = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err_code);
NRF_LOG_DEFAULT_BACKENDS_INIT();
SVIEW_INIT();
}
/**@brief Interrupt function for handling bsp events.
*/
static void bsp_evt_handler(bsp_event_t evt)
{
m_bsp_evt = evt;
}
/**@brief Function for handling bsp events.
*/
void bsp_tasks(void)
{
switch (m_bsp_evt)
{
case BSP_EVENT_KEY_0:
{
LOG_WARNING("Going to DFU !");
nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_DFU);
} break;
default:
return; // no implementation needed
}
// clear event
m_bsp_evt = BSP_EVENT_NOTHING;
}
/**@brief Handler for shutdown preparation.
*
* @details During shutdown procedures, this function will be called at a 1 second interval
* untill the function returns true. When the function returns true, it means that the
* app is ready to reset to DFU mode.
*
* @param[in] event Power manager event.
*
* @retval True if shutdown is allowed by this power manager handler, otherwise false.
*/
static bool app_shutdown_handler(nrf_pwr_mgmt_evt_t event)
{
ret_code_t err_code;
switch (event) {
case NRF_PWR_MGMT_EVT_PREPARE_WAKEUP:
{
// TODO prepare wakeup source
}
// no break
case NRF_PWR_MGMT_EVT_PREPARE_SYSOFF:
{
LOG_WARNING("Preparing SYSOFF");
i2c_scheduling_uninit();
// nrf_gpio_pin_clear(HV_EN);
// nrf_gpio_pin_set(N_VCCINT_EN);
#if defined (ANT_STACK_SUPPORT_REQD)
ant_setup_stop();
#endif
#if defined (BLE_STACK_SUPPORT_REQD)
ble_uninit();
#endif
// Device ready to enter
err_code = app_timer_stop_all();
APP_ERROR_CHECK(err_code);
#ifdef SOFTDEVICE_PRESENT
nrf_sdh_disable_request();
#endif
} break;
case NRF_PWR_MGMT_EVT_PREPARE_DFU:
{
// nrf_gpio_pin_clear(HV_EN);
// nrf_gpio_pin_set(N_VCCINT_EN);
#if defined (ANT_STACK_SUPPORT_REQD)
ant_setup_stop();
#endif
err_code = app_timer_stop_all();
APP_ERROR_CHECK(err_code);
LOG_WARNING("Power management allowed to reset to DFU mode.");
} break;
default:
break;
}
return true;
}
NRF_PWR_MGMT_HANDLER_REGISTER(app_shutdown_handler, 0);
/**
* @brief WDT events handler.
*/
void wdt_event_handler(void)
{
//NOTE: The max amount of time we can spend in WDT interrupt is two cycles of 32768[Hz] clock - after that, reset occurs
}
/**@brief Function for initializing buttons and LEDs.
*
* @param[out] p_erase_bonds True if the clear bonds button was pressed to wake the application up.
*/
static void buttons_leds_init(void)
{
uint32_t err_code = bsp_init(BSP_INIT_BUTTONS | BSP_INIT_LEDS, bsp_evt_handler);
APP_ERROR_CHECK(err_code);
}
static void pins_init(void)
{
ret_code_t err_code;
err_code = nrfx_gpiote_init();
APP_ERROR_CHECK(err_code);
// TODO configure pins
nrf_gpio_cfg_input(LSM6_INT1, NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_input(VL53_INT , NRF_GPIO_PIN_NOPULL);
}
void wdt_reload(void) {
nrfx_wdt_channel_feed(m_channel_id);
}
void app_shutdown(void) {
nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
}
#ifdef SOFTDEVICE_PRESENT
/**@brief Function for initializing the softdevice
*
* @details Initializes the SoftDevice
*/
static void sdh_init(void)
{
ret_code_t err_code;
err_code = nrf_sdh_enable_request();
APP_ERROR_CHECK(err_code);
ASSERT(nrf_sdh_is_enabled());
}
#endif
/**
* @brief Function for configuration of REGOUT0 register.
*/
static void vddInit(void)
{
if (NRF_UICR->REGOUT0 != UICR_REGOUT0_VOUT_3V3)
{
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos; //write enable
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
NRF_UICR->REGOUT0 = UICR_REGOUT0_VOUT_3V3; //configurate REGOUT0
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
NVIC_SystemReset(); // Reset device
}
}
/**
*
* @return 0
*/
int main(void)
{
ret_code_t err_code;
#ifdef CONFIG_JLINK_MONITOR_ENABLED
NVIC_SetPriority(DebugMonitor_IRQn, _PRIO_SD_LOW);
NVIC_ClearPendingIRQ(DebugMonitor_IRQn);
NVIC_EnableIRQ(DebugMonitor_IRQn);
#warning "!! MONITORING active !!"
#endif
// Initialize.
//Configure WDT.
nrfx_wdt_config_t wdt_config = NRFX_WDT_DEAFULT_CONFIG;
err_code = nrfx_wdt_init(&wdt_config, wdt_event_handler);
APP_ERROR_CHECK(err_code);
err_code = nrfx_wdt_channel_alloc(&m_channel_id);
APP_ERROR_CHECK(err_code);
nrfx_wdt_enable();
vddInit();
log_init();
#ifdef FPU_INTERRUPT_MODE
// Enable FPU interrupt
NVIC_SetPriority(FPU_IRQn, APP_IRQ_PRIORITY_LOWEST);
NVIC_ClearPendingIRQ(FPU_IRQn);
NVIC_EnableIRQ(FPU_IRQn);
#endif
pins_init();
millis_init();
LOG_INFO("Init start");
err_code = app_timer_create(&m_job_timer, APP_TIMER_MODE_REPEATED, timer_event_handler);
APP_ERROR_CHECK(err_code);
err_code = app_timer_start(m_job_timer, APP_DELAY, NULL);
APP_ERROR_CHECK(err_code);
// check for errors
if (m_app_error.hf_desc.crc == SYSTEM_DESCR_POS_CRC) {
LOG_ERROR("Hard Fault found");
String message = "Hardfault happened: pc = 0x";
message += String(m_app_error.hf_desc.stck.pc, 16);
message += " in void ";
message += m_app_error.void_id;
LOG_ERROR(message.c_str());
memset(&m_app_error.hf_desc, 0, sizeof(m_app_error.hf_desc));
}
err_code = nrf_pwr_mgmt_init();
APP_ERROR_CHECK(err_code);
#if APP_SCHEDULER_ENABLED
APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE);
#endif
buttons_leds_init();
nrf_gpio_pin_set(LED_1);
// drivers
i2c_init();
// init BLE + ANT
#ifdef SOFTDEVICE_PRESENT
sdh_init();
#endif
#if defined (ANT_STACK_SUPPORT_REQD)
ant_stack_init();
ant_setup_init();
ant_setup_start();
#endif
#if defined (BLE_STACK_SUPPORT_REQD)
ble_init();
#endif
LOG_INFO("App init done");
NRF_LOG_FLUSH();
(void)task_create(peripherals_task , "peripherals_task" , NULL);
(void)task_create(sensing_task , "sensing_task" , NULL);
(void)task_create(actuating_task , "actuating_task" , NULL);
// does not return
task_manager_start(idle_task, NULL);
}