-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
411 lines (333 loc) · 10.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
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
/**
* Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdio.h>
#include <stdbool.h>
#include <stddef.h>
#include "nrf.h"
#include "nrf_drv_clock.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "app_timer.h"
#include "fds.h"
#include "app_error.h"
#include "app_util.h"
#include "nrf_cli.h"
#include "nrf_cli_rtt.h"
#include "nrf_cli_types.h"
#include "boards.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_log_backend_flash.h"
#include "nrf_fstorage_nvmc.h"
#include "nrf_mpu_lib.h"
#include "nrf_stack_guard.h"
#if defined(APP_USBD_ENABLED) && APP_USBD_ENABLED
#define CLI_OVER_USB_CDC_ACM 1
#else
#define CLI_OVER_USB_CDC_ACM 0
#endif
#if CLI_OVER_USB_CDC_ACM
#include "nrf_cli_cdc_acm.h"
#include "nrf_drv_usbd.h"
#include "app_usbd_core.h"
#include "app_usbd.h"
#include "app_usbd_string_desc.h"
#include "app_usbd_cdc_acm.h"
#endif //CLI_OVER_USB_CDC_ACM
#include "pca10056/blank/ses/adxl362.h"
#if defined(TX_PIN_NUMBER) && defined(RX_PIN_NUMBER)
#define CLI_OVER_UART 1
#else
#define CLI_OVER_UART 0
#endif
#if CLI_OVER_UART
#include "nrf_cli_uart.h"
#endif
/* If enabled then CYCCNT (high resolution) timestamp is used for the logger. */
#define USE_CYCCNT_TIMESTAMP_FOR_LOG 0
/**@file
* @defgroup CLI_example main.c
*
* @{
*
*/
#if NRF_LOG_BACKEND_FLASHLOG_ENABLED
NRF_LOG_BACKEND_FLASHLOG_DEF(m_flash_log_backend);
#endif
#if NRF_LOG_BACKEND_CRASHLOG_ENABLED
NRF_LOG_BACKEND_CRASHLOG_DEF(m_crash_log_backend);
#endif
/* Counter timer. */
APP_TIMER_DEF(m_timer_0);
/* Declared in demo_cli.c */
extern uint32_t m_counter;
extern bool m_counter_active;
/* ADXL362 read timer. */
APP_TIMER_DEF(adxl_timer_0);
extern bool adxl362_start_read;
#if CLI_OVER_USB_CDC_ACM
/**
* @brief Enable power USB detection
*
* Configure if example supports USB port connection
*/
#ifndef USBD_POWER_DETECTION
#define USBD_POWER_DETECTION true
#endif
static void usbd_user_ev_handler(app_usbd_event_type_t event)
{
switch (event)
{
case APP_USBD_EVT_STOPPED:
app_usbd_disable();
break;
case APP_USBD_EVT_POWER_DETECTED:
if (!nrf_drv_usbd_is_enabled())
{
app_usbd_enable();
}
break;
case APP_USBD_EVT_POWER_REMOVED:
app_usbd_stop();
break;
case APP_USBD_EVT_POWER_READY:
app_usbd_start();
break;
default:
break;
}
}
#endif //CLI_OVER_USB_CDC_ACM
/**
* @brief Command line interface instance
* */
#define CLI_EXAMPLE_LOG_QUEUE_SIZE (4)
#if CLI_OVER_USB_CDC_ACM
NRF_CLI_CDC_ACM_DEF(m_cli_cdc_acm_transport);
NRF_CLI_DEF(m_cli_cdc_acm,
"usb_cli:~$ ",
&m_cli_cdc_acm_transport.transport,
'\r',
CLI_EXAMPLE_LOG_QUEUE_SIZE);
#endif //CLI_OVER_USB_CDC_ACM
#if CLI_OVER_UART
NRF_CLI_UART_DEF(m_cli_uart_transport, 0, 64, 16);
NRF_CLI_DEF(m_cli_uart,
"uart_cli:~$ ",
&m_cli_uart_transport.transport,
'\r',
CLI_EXAMPLE_LOG_QUEUE_SIZE);
#endif
NRF_CLI_RTT_DEF(m_cli_rtt_transport);
NRF_CLI_DEF(m_cli_rtt,
"rtt_cli:~$ ",
&m_cli_rtt_transport.transport,
'\n',
CLI_EXAMPLE_LOG_QUEUE_SIZE);
static void timer_handle(void * p_context)
{
UNUSED_PARAMETER(p_context);
if (m_counter_active)
{
m_counter++;
NRF_LOG_RAW_INFO("counter = %d\n", m_counter);
}
}
static void adxl_timer_handle(void * p_context)
{
int16_t x = 0, y = 0, z = 0;
UNUSED_PARAMETER(p_context);
if (is_adxl_inited() && adxl362_start_read)
{
adxl_get_xyz(&x, &y, &z);
NRF_LOG_RAW_INFO("x: %d y: %d z: %d\n", x, y, z);
}
// else
// NRF_LOG_RAW_INFO("ADXL362 not even started");
}
static void cli_start(void)
{
ret_code_t ret;
#if CLI_OVER_USB_CDC_ACM
ret = nrf_cli_start(&m_cli_cdc_acm);
APP_ERROR_CHECK(ret);
#endif
#if CLI_OVER_UART
ret = nrf_cli_start(&m_cli_uart);
APP_ERROR_CHECK(ret);
#endif
ret = nrf_cli_start(&m_cli_rtt);
APP_ERROR_CHECK(ret);
}
static void cli_init(void)
{
ret_code_t ret;
#if CLI_OVER_USB_CDC_ACM
ret = nrf_cli_init(&m_cli_cdc_acm, NULL, true, true, NRF_LOG_SEVERITY_INFO);
APP_ERROR_CHECK(ret);
#endif
#if CLI_OVER_UART
nrf_drv_uart_config_t uart_config = NRF_DRV_UART_DEFAULT_CONFIG;
uart_config.pseltxd = TX_PIN_NUMBER;
uart_config.pselrxd = RX_PIN_NUMBER;
uart_config.hwfc = NRF_UART_HWFC_DISABLED;
ret = nrf_cli_init(&m_cli_uart, &uart_config, true, true, NRF_LOG_SEVERITY_INFO);
APP_ERROR_CHECK(ret);
#endif
ret = nrf_cli_init(&m_cli_rtt, NULL, true, true, NRF_LOG_SEVERITY_INFO);
APP_ERROR_CHECK(ret);
}
static void usbd_init(void)
{
#if CLI_OVER_USB_CDC_ACM
ret_code_t ret;
static const app_usbd_config_t usbd_config = {
.ev_handler = app_usbd_event_execute,
.ev_state_proc = usbd_user_ev_handler
};
ret = app_usbd_init(&usbd_config);
APP_ERROR_CHECK(ret);
app_usbd_class_inst_t const * class_cdc_acm =
app_usbd_cdc_acm_class_inst_get(&nrf_cli_cdc_acm);
ret = app_usbd_class_append(class_cdc_acm);
APP_ERROR_CHECK(ret);
if (USBD_POWER_DETECTION)
{
ret = app_usbd_power_events_enable();
APP_ERROR_CHECK(ret);
}
else
{
NRF_LOG_INFO("No USB power detection enabled\nStarting USB now");
app_usbd_enable();
app_usbd_start();
}
/* Give some time for the host to enumerate and connect to the USB CDC port */
nrf_delay_ms(1000);
#endif
}
static void cli_process(void)
{
#if CLI_OVER_USB_CDC_ACM
nrf_cli_process(&m_cli_cdc_acm);
#endif
#if CLI_OVER_UART
nrf_cli_process(&m_cli_uart);
#endif
nrf_cli_process(&m_cli_rtt);
}
static void flashlog_init(void)
{
ret_code_t ret;
int32_t backend_id;
ret = nrf_log_backend_flash_init(&nrf_fstorage_nvmc);
APP_ERROR_CHECK(ret);
#if NRF_LOG_BACKEND_FLASHLOG_ENABLED
backend_id = nrf_log_backend_add(&m_flash_log_backend, NRF_LOG_SEVERITY_WARNING);
APP_ERROR_CHECK_BOOL(backend_id >= 0);
nrf_log_backend_enable(&m_flash_log_backend);
#endif
#if NRF_LOG_BACKEND_CRASHLOG_ENABLED
backend_id = nrf_log_backend_add(&m_crash_log_backend, NRF_LOG_SEVERITY_INFO);
APP_ERROR_CHECK_BOOL(backend_id >= 0);
nrf_log_backend_enable(&m_crash_log_backend);
#endif
}
static inline void stack_guard_init(void)
{
APP_ERROR_CHECK(nrf_mpu_lib_init());
APP_ERROR_CHECK(nrf_stack_guard_init());
}
uint32_t cyccnt_get(void)
{
return DWT->CYCCNT;
}
int main(void)
{
ret_code_t ret;
if (USE_CYCCNT_TIMESTAMP_FOR_LOG)
{
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
DWT->CYCCNT = 0;
APP_ERROR_CHECK(NRF_LOG_INIT(cyccnt_get, 64000000));
}
else
{
APP_ERROR_CHECK(NRF_LOG_INIT(app_timer_cnt_get));
}
ret = nrf_drv_clock_init();
APP_ERROR_CHECK(ret);
nrf_drv_clock_lfclk_request(NULL);
ret = app_timer_init();
APP_ERROR_CHECK(ret);
ret = app_timer_create(&m_timer_0, APP_TIMER_MODE_REPEATED, timer_handle);
APP_ERROR_CHECK(ret);
ret = app_timer_start(m_timer_0, APP_TIMER_TICKS(1000), NULL);
APP_ERROR_CHECK(ret);
adxl362_init();
ret = app_timer_create(&adxl_timer_0, APP_TIMER_MODE_REPEATED, adxl_timer_handle);
APP_ERROR_CHECK(ret);
ret = app_timer_start(adxl_timer_0, APP_TIMER_TICKS(500), NULL);
APP_ERROR_CHECK(ret);
cli_init();
usbd_init();
ret = fds_init();
APP_ERROR_CHECK(ret);
UNUSED_RETURN_VALUE(nrf_log_config_load());
cli_start();
flashlog_init();
stack_guard_init();
NRF_LOG_RAW_INFO("Command Line Interface example started.\n");
NRF_LOG_RAW_INFO("Please press the Tab key to see all available commands.\n");
while (true)
{
UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
#if CLI_OVER_USB_CDC_ACM && APP_USBD_CONFIG_EVENT_QUEUE_ENABLE
while (app_usbd_event_queue_process())
{
/* Nothing to do */
}
#endif
cli_process();
}
}
/** @} */