main.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <stdio.h>
  41. #include <stdbool.h>
  42. #include <stddef.h>
  43. #include "nrf.h"
  44. #include "nrf_drv_clock.h"
  45. #include "nrf_gpio.h"
  46. #include "nrf_delay.h"
  47. #include "app_timer.h"
  48. #include "fds.h"
  49. #include "app_error.h"
  50. #include "app_util.h"
  51. #include "nrf_cli.h"
  52. #include "nrf_cli_rtt.h"
  53. #include "nrf_cli_types.h"
  54. #include "boards.h"
  55. #include "nrf_log.h"
  56. #include "nrf_log_ctrl.h"
  57. #include "nrf_log_default_backends.h"
  58. #include "nrf_log_backend_flash.h"
  59. #include "nrf_fstorage_nvmc.h"
  60. #include "nrf_mpu_lib.h"
  61. #include "nrf_stack_guard.h"
  62. #if defined(APP_USBD_ENABLED) && APP_USBD_ENABLED
  63. #define CLI_OVER_USB_CDC_ACM 1
  64. #else
  65. #define CLI_OVER_USB_CDC_ACM 0
  66. #endif
  67. #if CLI_OVER_USB_CDC_ACM
  68. #include "nrf_cli_cdc_acm.h"
  69. #include "nrf_drv_usbd.h"
  70. #include "app_usbd_core.h"
  71. #include "app_usbd.h"
  72. #include "app_usbd_string_desc.h"
  73. #include "app_usbd_cdc_acm.h"
  74. #endif //CLI_OVER_USB_CDC_ACM
  75. #if defined(TX_PIN_NUMBER) && defined(RX_PIN_NUMBER)
  76. #define CLI_OVER_UART 1
  77. #else
  78. #define CLI_OVER_UART 0
  79. #endif
  80. #if CLI_OVER_UART
  81. #include "nrf_cli_uart.h"
  82. #endif
  83. /* If enabled then CYCCNT (high resolution) timestamp is used for the logger. */
  84. #define USE_CYCCNT_TIMESTAMP_FOR_LOG 0
  85. /**@file
  86. * @defgroup CLI_example main.c
  87. *
  88. * @{
  89. *
  90. */
  91. #if NRF_LOG_BACKEND_FLASHLOG_ENABLED
  92. NRF_LOG_BACKEND_FLASHLOG_DEF(m_flash_log_backend);
  93. #endif
  94. #if NRF_LOG_BACKEND_CRASHLOG_ENABLED
  95. NRF_LOG_BACKEND_CRASHLOG_DEF(m_crash_log_backend);
  96. #endif
  97. /* Counter timer. */
  98. APP_TIMER_DEF(m_timer_0);
  99. /* Declared in demo_cli.c */
  100. extern uint32_t m_counter;
  101. extern bool m_counter_active;
  102. #if CLI_OVER_USB_CDC_ACM
  103. /**
  104. * @brief Enable power USB detection
  105. *
  106. * Configure if example supports USB port connection
  107. */
  108. #ifndef USBD_POWER_DETECTION
  109. #define USBD_POWER_DETECTION true
  110. #endif
  111. static void usbd_user_ev_handler(app_usbd_event_type_t event)
  112. {
  113. switch (event)
  114. {
  115. case APP_USBD_EVT_STOPPED:
  116. app_usbd_disable();
  117. break;
  118. case APP_USBD_EVT_POWER_DETECTED:
  119. if (!nrf_drv_usbd_is_enabled())
  120. {
  121. app_usbd_enable();
  122. }
  123. break;
  124. case APP_USBD_EVT_POWER_REMOVED:
  125. app_usbd_stop();
  126. break;
  127. case APP_USBD_EVT_POWER_READY:
  128. app_usbd_start();
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. #endif //CLI_OVER_USB_CDC_ACM
  135. /**
  136. * @brief Command line interface instance
  137. * */
  138. #define CLI_EXAMPLE_LOG_QUEUE_SIZE (4)
  139. #if CLI_OVER_USB_CDC_ACM
  140. NRF_CLI_CDC_ACM_DEF(m_cli_cdc_acm_transport);
  141. NRF_CLI_DEF(m_cli_cdc_acm,
  142. "usb_cli:~$ ",
  143. &m_cli_cdc_acm_transport.transport,
  144. '\r',
  145. CLI_EXAMPLE_LOG_QUEUE_SIZE);
  146. #endif //CLI_OVER_USB_CDC_ACM
  147. #if CLI_OVER_UART
  148. NRF_CLI_UART_DEF(m_cli_uart_transport, 0, 64, 16);
  149. NRF_CLI_DEF(m_cli_uart,
  150. "uart_cli:~$ ",
  151. &m_cli_uart_transport.transport,
  152. '\r',
  153. CLI_EXAMPLE_LOG_QUEUE_SIZE);
  154. #endif
  155. NRF_CLI_RTT_DEF(m_cli_rtt_transport);
  156. NRF_CLI_DEF(m_cli_rtt,
  157. "rtt_cli:~$ ",
  158. &m_cli_rtt_transport.transport,
  159. '\n',
  160. CLI_EXAMPLE_LOG_QUEUE_SIZE);
  161. static void timer_handle(void * p_context)
  162. {
  163. UNUSED_PARAMETER(p_context);
  164. if (m_counter_active)
  165. {
  166. m_counter++;
  167. NRF_LOG_RAW_INFO("counter = %d\n", m_counter);
  168. }
  169. }
  170. static void cli_start(void)
  171. {
  172. ret_code_t ret;
  173. #if CLI_OVER_USB_CDC_ACM
  174. ret = nrf_cli_start(&m_cli_cdc_acm);
  175. APP_ERROR_CHECK(ret);
  176. #endif
  177. #if CLI_OVER_UART
  178. ret = nrf_cli_start(&m_cli_uart);
  179. APP_ERROR_CHECK(ret);
  180. #endif
  181. ret = nrf_cli_start(&m_cli_rtt);
  182. APP_ERROR_CHECK(ret);
  183. }
  184. static void cli_init(void)
  185. {
  186. ret_code_t ret;
  187. #if CLI_OVER_USB_CDC_ACM
  188. ret = nrf_cli_init(&m_cli_cdc_acm, NULL, true, true, NRF_LOG_SEVERITY_INFO);
  189. APP_ERROR_CHECK(ret);
  190. #endif
  191. #if CLI_OVER_UART
  192. nrf_drv_uart_config_t uart_config = NRF_DRV_UART_DEFAULT_CONFIG;
  193. uart_config.pseltxd = TX_PIN_NUMBER;
  194. uart_config.pselrxd = RX_PIN_NUMBER;
  195. uart_config.hwfc = NRF_UART_HWFC_DISABLED;
  196. ret = nrf_cli_init(&m_cli_uart, &uart_config, true, true, NRF_LOG_SEVERITY_INFO);
  197. APP_ERROR_CHECK(ret);
  198. #endif
  199. ret = nrf_cli_init(&m_cli_rtt, NULL, true, true, NRF_LOG_SEVERITY_INFO);
  200. APP_ERROR_CHECK(ret);
  201. }
  202. static void usbd_init(void)
  203. {
  204. #if CLI_OVER_USB_CDC_ACM
  205. ret_code_t ret;
  206. static const app_usbd_config_t usbd_config = {
  207. .ev_handler = app_usbd_event_execute,
  208. .ev_state_proc = usbd_user_ev_handler
  209. };
  210. ret = app_usbd_init(&usbd_config);
  211. APP_ERROR_CHECK(ret);
  212. app_usbd_class_inst_t const * class_cdc_acm =
  213. app_usbd_cdc_acm_class_inst_get(&nrf_cli_cdc_acm);
  214. ret = app_usbd_class_append(class_cdc_acm);
  215. APP_ERROR_CHECK(ret);
  216. if (USBD_POWER_DETECTION)
  217. {
  218. ret = app_usbd_power_events_enable();
  219. APP_ERROR_CHECK(ret);
  220. }
  221. else
  222. {
  223. NRF_LOG_INFO("No USB power detection enabled\nStarting USB now");
  224. app_usbd_enable();
  225. app_usbd_start();
  226. }
  227. /* Give some time for the host to enumerate and connect to the USB CDC port */
  228. nrf_delay_ms(1000);
  229. #endif
  230. }
  231. static void cli_process(void)
  232. {
  233. #if CLI_OVER_USB_CDC_ACM
  234. nrf_cli_process(&m_cli_cdc_acm);
  235. #endif
  236. #if CLI_OVER_UART
  237. nrf_cli_process(&m_cli_uart);
  238. #endif
  239. nrf_cli_process(&m_cli_rtt);
  240. }
  241. static void flashlog_init(void)
  242. {
  243. ret_code_t ret;
  244. int32_t backend_id;
  245. ret = nrf_log_backend_flash_init(&nrf_fstorage_nvmc);
  246. APP_ERROR_CHECK(ret);
  247. #if NRF_LOG_BACKEND_FLASHLOG_ENABLED
  248. backend_id = nrf_log_backend_add(&m_flash_log_backend, NRF_LOG_SEVERITY_WARNING);
  249. APP_ERROR_CHECK_BOOL(backend_id >= 0);
  250. nrf_log_backend_enable(&m_flash_log_backend);
  251. #endif
  252. #if NRF_LOG_BACKEND_CRASHLOG_ENABLED
  253. backend_id = nrf_log_backend_add(&m_crash_log_backend, NRF_LOG_SEVERITY_INFO);
  254. APP_ERROR_CHECK_BOOL(backend_id >= 0);
  255. nrf_log_backend_enable(&m_crash_log_backend);
  256. #endif
  257. }
  258. static inline void stack_guard_init(void)
  259. {
  260. APP_ERROR_CHECK(nrf_mpu_lib_init());
  261. APP_ERROR_CHECK(nrf_stack_guard_init());
  262. }
  263. uint32_t cyccnt_get(void)
  264. {
  265. return DWT->CYCCNT;
  266. }
  267. int main(void)
  268. {
  269. ret_code_t ret;
  270. if (USE_CYCCNT_TIMESTAMP_FOR_LOG)
  271. {
  272. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  273. DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
  274. DWT->CYCCNT = 0;
  275. APP_ERROR_CHECK(NRF_LOG_INIT(cyccnt_get, 64000000));
  276. }
  277. else
  278. {
  279. APP_ERROR_CHECK(NRF_LOG_INIT(app_timer_cnt_get));
  280. }
  281. ret = nrf_drv_clock_init();
  282. APP_ERROR_CHECK(ret);
  283. nrf_drv_clock_lfclk_request(NULL);
  284. ret = app_timer_init();
  285. APP_ERROR_CHECK(ret);
  286. ret = app_timer_create(&m_timer_0, APP_TIMER_MODE_REPEATED, timer_handle);
  287. APP_ERROR_CHECK(ret);
  288. ret = app_timer_start(m_timer_0, APP_TIMER_TICKS(1000), NULL);
  289. APP_ERROR_CHECK(ret);
  290. cli_init();
  291. usbd_init();
  292. ret = fds_init();
  293. APP_ERROR_CHECK(ret);
  294. UNUSED_RETURN_VALUE(nrf_log_config_load());
  295. cli_start();
  296. flashlog_init();
  297. stack_guard_init();
  298. NRF_LOG_RAW_INFO("Command Line Interface example started.\n");
  299. NRF_LOG_RAW_INFO("Please press the Tab key to see all available commands.\n");
  300. while (true)
  301. {
  302. UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
  303. #if CLI_OVER_USB_CDC_ACM && APP_USBD_CONFIG_EVENT_QUEUE_ENABLE
  304. while (app_usbd_event_queue_process())
  305. {
  306. /* Nothing to do */
  307. }
  308. #endif
  309. cli_process();
  310. }
  311. }
  312. /** @} */