nrf_cli_uart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * Copyright (c) 2017 - 2019, 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 "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(NRF_CLI_UART)
  42. #include "nrf_cli_uart.h"
  43. #include "nrf_drv_uart.h"
  44. #include "nrf_assert.h"
  45. #define NRF_LOG_MODULE_NAME cli_uart
  46. #define NRF_LOG_LEVEL (NRF_CLI_UART_CONFIG_LOG_ENABLED ? NRF_CLI_UART_CONFIG_LOG_LEVEL : 0)
  47. #define NRF_LOG_INFO_COLOR NRF_CLI_UART_CONFIG_INFO_COLOR
  48. #define NRF_LOG_DEBUG_COLOR NRF_CLI_UART_CONFIG_DEBUG_COLOR
  49. #include "nrf_log.h"
  50. NRF_LOG_MODULE_REGISTER();
  51. #define CLI_UART_RX_TIMEOUT 100
  52. static ret_code_t rx_try(nrf_cli_uart_internal_t * p_internal)
  53. {
  54. ret_code_t err_code;
  55. size_t len = 255;
  56. uint8_t * p_data;
  57. err_code = nrf_ringbuf_alloc(p_internal->p_rx_ringbuf, &p_data, &len, true);
  58. ASSERT(err_code == NRF_SUCCESS);
  59. if ((err_code == NRF_SUCCESS) && len)
  60. {
  61. err_code = nrf_drv_uart_rx(p_internal->p_uart, p_data, len);
  62. if (err_code == NRF_SUCCESS)
  63. {
  64. err_code = app_timer_start(*p_internal->p_timer,
  65. APP_TIMER_TICKS(CLI_UART_RX_TIMEOUT),
  66. p_internal);
  67. }
  68. }
  69. return err_code;
  70. }
  71. static void uart_event_handler(nrf_drv_uart_event_t * p_event, void * p_context)
  72. {
  73. nrf_cli_uart_internal_t * p_internal = (nrf_cli_uart_internal_t *)p_context;
  74. ret_code_t err_code = NRF_SUCCESS;
  75. UNUSED_VARIABLE(err_code);
  76. uint8_t * p_data;
  77. size_t len = 255;
  78. switch (p_event->type)
  79. {
  80. case NRF_DRV_UART_EVT_ERROR:
  81. NRF_LOG_WARNING("id:%d, evt: ERROR:%d",
  82. p_internal->p_uart->inst_idx,
  83. p_event->data.error.error_mask);
  84. err_code = nrf_ringbuf_put(p_internal->p_rx_ringbuf, p_event->data.error.rxtx.bytes);
  85. ASSERT((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_NO_MEM));
  86. err_code = rx_try(p_internal);
  87. ASSERT(err_code == NRF_SUCCESS);
  88. break;
  89. case NRF_DRV_UART_EVT_RX_DONE:
  90. err_code = nrf_ringbuf_put(p_internal->p_rx_ringbuf, p_event->data.rxtx.bytes);
  91. ASSERT((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_NO_MEM));
  92. if (p_event->data.rxtx.bytes)
  93. {
  94. NRF_LOG_INFO("id:%d, evt: RXRDY len:%d",
  95. p_internal->p_uart->inst_idx,
  96. p_event->data.rxtx.bytes);
  97. NRF_LOG_HEXDUMP_DEBUG(p_event->data.rxtx.p_data, p_event->data.rxtx.bytes);
  98. p_internal->p_cb->handler(NRF_CLI_TRANSPORT_EVT_RX_RDY,
  99. p_internal->p_cb->p_context);
  100. }
  101. err_code = rx_try(p_internal);
  102. ASSERT(err_code == NRF_SUCCESS);
  103. break;
  104. case NRF_DRV_UART_EVT_TX_DONE:
  105. err_code = nrf_ringbuf_free(p_internal->p_tx_ringbuf, p_event->data.rxtx.bytes);
  106. ASSERT(err_code == NRF_SUCCESS);
  107. len = 255;
  108. err_code = nrf_ringbuf_get(p_internal->p_tx_ringbuf, &p_data, &len, true);
  109. ASSERT(err_code == NRF_SUCCESS);
  110. if (len)
  111. {
  112. NRF_LOG_INFO("id:%d, evt uart_tx, len:%d", p_internal->p_uart->inst_idx, len);
  113. err_code = nrf_drv_uart_tx(p_internal->p_uart, p_data, len);
  114. ASSERT(err_code == NRF_SUCCESS);
  115. }
  116. p_internal->p_cb->handler(NRF_CLI_TRANSPORT_EVT_TX_RDY, p_internal->p_cb->p_context);
  117. NRF_LOG_INFO("id:%d, evt: TXRDY, len:%d",
  118. p_internal->p_uart->inst_idx,
  119. p_event->data.rxtx.bytes);
  120. break;
  121. default:
  122. NRF_LOG_ERROR("Unknown event");
  123. ASSERT(false);
  124. }
  125. }
  126. static void timer_handler(void * p_context)
  127. {
  128. nrf_cli_uart_internal_t * p_internal = (nrf_cli_uart_internal_t *)p_context;
  129. NRF_LOG_DEBUG("id:%d, evt: Timeout", p_internal->p_uart->inst_idx);
  130. nrf_drv_uart_rx_abort(p_internal->p_uart);
  131. }
  132. static ret_code_t cli_uart_init(nrf_cli_transport_t const * p_transport,
  133. void const * p_config,
  134. nrf_cli_transport_handler_t evt_handler,
  135. void * p_context)
  136. {
  137. nrf_cli_uart_internal_t * p_internal =
  138. CONTAINER_OF(p_transport,
  139. nrf_cli_uart_internal_t,
  140. transport);
  141. p_internal->p_cb->handler = evt_handler;
  142. p_internal->p_cb->p_context = p_context;
  143. p_internal->p_cb->timer_created = false;
  144. p_internal->p_cb->blocking = false;
  145. nrf_drv_uart_config_t * p_uart_config = (nrf_drv_uart_config_t *)p_config;
  146. memcpy(&p_internal->p_cb->uart_config, p_uart_config, sizeof(nrf_drv_uart_config_t));
  147. p_uart_config->p_context = (void *)p_internal;
  148. ret_code_t err_code = nrf_drv_uart_init(p_internal->p_uart,
  149. p_uart_config,
  150. uart_event_handler);
  151. if (err_code == NRF_SUCCESS)
  152. {
  153. nrf_ringbuf_init(p_internal->p_rx_ringbuf);
  154. nrf_ringbuf_init(p_internal->p_tx_ringbuf);
  155. }
  156. return err_code;
  157. }
  158. static ret_code_t cli_uart_uninit(nrf_cli_transport_t const * p_transport)
  159. {
  160. nrf_cli_uart_internal_t * p_internal =
  161. CONTAINER_OF(p_transport,
  162. nrf_cli_uart_internal_t,
  163. transport);
  164. nrf_drv_uart_uninit(p_internal->p_uart);
  165. return app_timer_stop(*p_internal->p_timer);
  166. }
  167. static ret_code_t cli_uart_enable(nrf_cli_transport_t const * p_transport,
  168. bool blocking)
  169. {
  170. nrf_cli_uart_internal_t * p_internal =
  171. CONTAINER_OF(p_transport,
  172. nrf_cli_uart_internal_t,
  173. transport);
  174. ret_code_t err_code = NRF_SUCCESS;
  175. if (p_internal->p_cb->timer_created)
  176. {
  177. err_code = app_timer_stop(*p_internal->p_timer); //Timer may be running or inactive
  178. if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_INVALID_STATE))
  179. {
  180. return err_code;
  181. }
  182. else
  183. {
  184. err_code = NRF_SUCCESS;
  185. }
  186. }
  187. if (blocking)
  188. {
  189. nrf_drv_uart_uninit(p_internal->p_uart);
  190. err_code = nrf_drv_uart_init(p_internal->p_uart, &p_internal->p_cb->uart_config, NULL);
  191. if (err_code == NRF_SUCCESS)
  192. {
  193. p_internal->p_cb->blocking = true;
  194. return NRF_SUCCESS;
  195. }
  196. else
  197. {
  198. return NRF_ERROR_NOT_SUPPORTED;
  199. }
  200. }
  201. else
  202. {
  203. if (!p_internal->p_cb->timer_created)
  204. {
  205. err_code = app_timer_create(p_internal->p_timer,
  206. APP_TIMER_MODE_SINGLE_SHOT,
  207. timer_handler);
  208. p_internal->p_cb->timer_created = true;
  209. }
  210. if (err_code == NRF_SUCCESS)
  211. {
  212. err_code = rx_try(p_internal);
  213. }
  214. }
  215. return err_code;
  216. }
  217. static ret_code_t cli_uart_read(nrf_cli_transport_t const * p_transport,
  218. void * p_data,
  219. size_t length,
  220. size_t * p_cnt)
  221. {
  222. ASSERT(p_cnt);
  223. nrf_cli_uart_internal_t * p_instance =
  224. CONTAINER_OF(p_transport, nrf_cli_uart_internal_t, transport);
  225. *p_cnt = length;
  226. ret_code_t err_code = nrf_ringbuf_cpy_get(p_instance->p_rx_ringbuf, p_data, p_cnt);
  227. if (*p_cnt)
  228. {
  229. NRF_LOG_INFO("id:%d, read:%d", p_instance->p_uart->inst_idx, *p_cnt);
  230. }
  231. return err_code;
  232. }
  233. static ret_code_t cli_uart_write(nrf_cli_transport_t const * p_transport,
  234. void const * p_data,
  235. size_t length,
  236. size_t * p_cnt)
  237. {
  238. ASSERT(p_cnt);
  239. nrf_cli_uart_internal_t * p_instance =
  240. CONTAINER_OF(p_transport, nrf_cli_uart_internal_t, transport);
  241. ret_code_t err_code;
  242. *p_cnt = length;
  243. err_code = nrf_ringbuf_cpy_put(p_instance->p_tx_ringbuf, p_data, p_cnt);
  244. if (err_code == NRF_SUCCESS)
  245. {
  246. NRF_LOG_INFO("id:%d, write, req:%d, done:%d",
  247. p_instance->p_uart->inst_idx,
  248. length,
  249. *p_cnt);
  250. if (!nrf_drv_uart_tx_in_progress(p_instance->p_uart))
  251. {
  252. uint8_t * p_buf;
  253. size_t len = 255;
  254. if (nrf_ringbuf_get(p_instance->p_tx_ringbuf, &p_buf, &len, true) == NRF_SUCCESS)
  255. {
  256. NRF_LOG_INFO("id:%d, uart_tx, len:%d", p_instance->p_uart->inst_idx, len);
  257. err_code = nrf_drv_uart_tx(p_instance->p_uart, p_buf, len);
  258. if (p_instance->p_cb->blocking && (err_code == NRF_SUCCESS))
  259. {
  260. (void)nrf_ringbuf_free(p_instance->p_tx_ringbuf, len);
  261. }
  262. }
  263. }
  264. }
  265. return err_code;
  266. }
  267. const nrf_cli_transport_api_t nrf_cli_uart_transport_api = {
  268. .init = cli_uart_init,
  269. .uninit = cli_uart_uninit,
  270. .enable = cli_uart_enable,
  271. .read = cli_uart_read,
  272. .write = cli_uart_write,
  273. };
  274. #endif