main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /**
  2. * Copyright (c) 2018 - 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 <ctype.h>
  44. #include "nrf.h"
  45. #include "nrf_gpio.h"
  46. #include "nrf_delay.h"
  47. #include "app_timer.h"
  48. #include "app_error.h"
  49. #include "app_util.h"
  50. #include "nrf_cli.h"
  51. #include "nrf_cli_rtt.h"
  52. #include "nrf_cli_types.h"
  53. #include "boards.h"
  54. #include "nrf_log.h"
  55. #include "nrf_log_ctrl.h"
  56. #include "nrf_log_default_backends.h"
  57. #include "nrf_cli_libuarte.h"
  58. #include "nrf_drv_clock.h"
  59. /* Counter timer. */
  60. APP_TIMER_DEF(m_timer_0);
  61. static uint32_t m_counter;
  62. static bool m_counter_active = false;
  63. /** @brief Command line interface instance. */
  64. #define CLI_EXAMPLE_LOG_QUEUE_SIZE (4)
  65. NRF_CLI_LIBUARTE_DEF(m_cli_libuarte_transport, 256, 256);
  66. NRF_CLI_DEF(m_cli_libuarte,
  67. "libuarte_cli:~$ ",
  68. &m_cli_libuarte_transport.transport,
  69. '\r',
  70. CLI_EXAMPLE_LOG_QUEUE_SIZE);
  71. NRF_CLI_RTT_DEF(m_cli_rtt_transport);
  72. NRF_CLI_DEF(m_cli_rtt,
  73. "rtt_cli:~$ ",
  74. &m_cli_rtt_transport.transport,
  75. '\n',
  76. CLI_EXAMPLE_LOG_QUEUE_SIZE);
  77. static void timer_handle(void * p_context)
  78. {
  79. UNUSED_PARAMETER(p_context);
  80. if (m_counter_active)
  81. {
  82. m_counter++;
  83. NRF_LOG_RAW_INFO("counter = %d\n", m_counter);
  84. }
  85. }
  86. static void cli_start(void)
  87. {
  88. ret_code_t ret;
  89. ret = nrf_cli_start(&m_cli_libuarte);
  90. APP_ERROR_CHECK(ret);
  91. ret = nrf_cli_start(&m_cli_rtt);
  92. APP_ERROR_CHECK(ret);
  93. }
  94. static void cli_init(void)
  95. {
  96. ret_code_t ret;
  97. cli_libuarte_config_t libuarte_config;
  98. libuarte_config.tx_pin = TX_PIN_NUMBER;
  99. libuarte_config.rx_pin = RX_PIN_NUMBER;
  100. libuarte_config.baudrate = NRF_UARTE_BAUDRATE_115200;
  101. libuarte_config.parity = NRF_UARTE_PARITY_EXCLUDED;
  102. libuarte_config.hwfc = NRF_UARTE_HWFC_DISABLED;
  103. ret = nrf_cli_init(&m_cli_libuarte, &libuarte_config, true, true, NRF_LOG_SEVERITY_INFO);
  104. APP_ERROR_CHECK(ret);
  105. ret = nrf_cli_init(&m_cli_rtt, NULL, true, true, NRF_LOG_SEVERITY_INFO);
  106. APP_ERROR_CHECK(ret);
  107. }
  108. static void cli_process(void)
  109. {
  110. nrf_cli_process(&m_cli_libuarte);
  111. nrf_cli_process(&m_cli_rtt);
  112. }
  113. int main(void)
  114. {
  115. ret_code_t ret;
  116. APP_ERROR_CHECK(NRF_LOG_INIT(app_timer_cnt_get));
  117. ret = nrf_drv_clock_init();
  118. APP_ERROR_CHECK(ret);
  119. nrf_drv_clock_lfclk_request(NULL);
  120. ret = app_timer_init();
  121. APP_ERROR_CHECK(ret);
  122. ret = app_timer_create(&m_timer_0, APP_TIMER_MODE_REPEATED, timer_handle);
  123. APP_ERROR_CHECK(ret);
  124. ret = app_timer_start(m_timer_0, APP_TIMER_TICKS(1000), NULL);
  125. APP_ERROR_CHECK(ret);
  126. cli_init();
  127. cli_start();
  128. NRF_LOG_RAW_INFO("Command Line Interface example started.\n");
  129. NRF_LOG_RAW_INFO("Please press the Tab key to see all available commands.\n");
  130. while (true)
  131. {
  132. UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
  133. cli_process();
  134. }
  135. }
  136. /* Command handlers */
  137. static void cmd_print_param(nrf_cli_t const * p_cli, size_t argc, char **argv)
  138. {
  139. for (size_t i = 1; i < argc; i++)
  140. {
  141. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "argv[%d] = %s\n", i, argv[i]);
  142. }
  143. }
  144. static void cmd_print_all(nrf_cli_t const * p_cli, size_t argc, char **argv)
  145. {
  146. for (size_t i = 1; i < argc; i++)
  147. {
  148. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "%s ", argv[i]);
  149. }
  150. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\n");
  151. }
  152. static void cmd_print(nrf_cli_t const * p_cli, size_t argc, char **argv)
  153. {
  154. ASSERT(p_cli);
  155. ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
  156. if ((argc == 1) || nrf_cli_help_requested(p_cli))
  157. {
  158. nrf_cli_help_print(p_cli, NULL, 0);
  159. return;
  160. }
  161. if (argc != 2)
  162. {
  163. nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s: bad parameter count\n", argv[0]);
  164. return;
  165. }
  166. nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s: unknown parameter: %s\n", argv[0], argv[1]);
  167. }
  168. static void cmd_counter_start(nrf_cli_t const * p_cli, size_t argc, char **argv)
  169. {
  170. if (argc != 1)
  171. {
  172. nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s: bad parameter count\n", argv[0]);
  173. return;
  174. }
  175. m_counter_active = true;
  176. }
  177. static void cmd_counter_stop(nrf_cli_t const * p_cli, size_t argc, char **argv)
  178. {
  179. if (argc != 1)
  180. {
  181. nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s: bad parameter count\n", argv[0]);
  182. return;
  183. }
  184. m_counter_active = false;
  185. }
  186. static void cmd_counter_reset(nrf_cli_t const * p_cli, size_t argc, char **argv)
  187. {
  188. if (argc != 1)
  189. {
  190. nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s: bad parameter count\n", argv[0]);
  191. return;
  192. }
  193. m_counter = 0;
  194. }
  195. static void cmd_counter(nrf_cli_t const * p_cli, size_t argc, char **argv)
  196. {
  197. ASSERT(p_cli);
  198. ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
  199. /* Extra defined dummy option */
  200. static const nrf_cli_getopt_option_t opt[] = {
  201. NRF_CLI_OPT(
  202. "--test",
  203. "-t",
  204. "dummy option help string"
  205. )
  206. };
  207. if ((argc == 1) || nrf_cli_help_requested(p_cli))
  208. {
  209. nrf_cli_help_print(p_cli, opt, ARRAY_SIZE(opt));
  210. return;
  211. }
  212. if (argc != 2)
  213. {
  214. nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s: bad parameter count\n", argv[0]);
  215. return;
  216. }
  217. if (!strcmp(argv[1], "-t") || !strcmp(argv[1], "--test"))
  218. {
  219. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "Dummy test option.\n");
  220. return;
  221. }
  222. /* subcommands have their own handlers and they are not processed here */
  223. nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s: unknown parameter: %s\n", argv[0], argv[1]);
  224. }
  225. static void cmd_nordic(nrf_cli_t const * p_cli, size_t argc, char **argv)
  226. {
  227. UNUSED_PARAMETER(argc);
  228. UNUSED_PARAMETER(argv);
  229. if (nrf_cli_help_requested(p_cli))
  230. {
  231. nrf_cli_help_print(p_cli, NULL, 0);
  232. return;
  233. }
  234. nrf_cli_fprintf(p_cli, NRF_CLI_OPTION,
  235. "\n"
  236. " .co:. 'xo, \n"
  237. " .,collllc,. 'ckOOo::,.. \n"
  238. " .:ooooollllllll:'. .;dOOOOOOo:::;;;'. \n"
  239. " 'okxddoooollllllllllll;'ckOOOOOOOOOo:::;;;,,,' \n"
  240. " OOOkxdoooolllllllllllllllldxOOOOOOOo:::;;;,,,'.\n"
  241. " OOOOOOkdoolllllllllllllllllllldxOOOo:::;;;,,,'.\n"
  242. " OOOOOOOOOkxollllllllllllllllllcccldl:::;;;,,,'.\n"
  243. " OOOOOOOOOOOOOxdollllllllllllllccccc::::;;;,,,'.\n"
  244. " OOOOOOOOOOOOOOOOkxdlllllllllllccccc::::;;;,,,'.\n"
  245. " kOOOOOOOOOOOOOOOOOOOkdolllllllccccc::::;;;,,,'.\n"
  246. " kOOOOOOOOOOOOOOOOOOOOOOOxdllllccccc::::;;;,,,'.\n"
  247. " kOOOOOOOOOOOOOOOOOOOOOOOOOOkxolcccc::::;;;,,,'.\n"
  248. " kOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOkdlc::::;;;,,,'.\n"
  249. " xOOOOOOOOOOOxdkOOOOOOOOOOOOOOOOOOOOxoc:;;;,,,'.\n"
  250. " xOOOOOOOOOOOdc::ldkOOOOOOOOOOOOOOOOOOOkdc;,,,''\n"
  251. " xOOOOOOOOOOOdc::;;,;cdkOOOOOOOOOOOOOOOOOOOxl;''\n"
  252. " .lkOOOOOOOOOdc::;;,,''..;oOOOOOOOOOOOOOOOOOOOx'\n"
  253. " .;oOOOOOOdc::;;,. .:xOOOOOOOOOOOOd;. \n"
  254. " .:xOOdc:,. 'ckOOOOkl' \n"
  255. " .od' 'xk, \n"
  256. "\n");
  257. nrf_cli_fprintf(p_cli,NRF_CLI_NORMAL,
  258. " Nordic Semiconductor \n\n");
  259. }
  260. /** @brief Command set array. */
  261. NRF_CLI_CMD_REGISTER(nordic, NULL, "Print Nordic Semiconductor logo.", cmd_nordic);
  262. NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_print)
  263. {
  264. NRF_CLI_CMD(all, NULL, "Print all entered parameters.", cmd_print_all),
  265. NRF_CLI_CMD(param, NULL, "Print each parameter in new line.", cmd_print_param),
  266. NRF_CLI_SUBCMD_SET_END
  267. };
  268. NRF_CLI_CMD_REGISTER(print, &m_sub_print, "print", cmd_print);
  269. NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_counter)
  270. {
  271. NRF_CLI_CMD(reset, NULL, "Reset seconds counter.", cmd_counter_reset),
  272. NRF_CLI_CMD(start, NULL, "Start seconds counter.", cmd_counter_start),
  273. NRF_CLI_CMD(stop, NULL, "Stop seconds counter.", cmd_counter_stop),
  274. NRF_CLI_SUBCMD_SET_END
  275. };
  276. NRF_CLI_CMD_REGISTER(counter,
  277. &m_sub_counter,
  278. "Display seconds on terminal screen",
  279. cmd_counter);