nrf_cli_libuarte.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 "sdk_common.h"
  41. #include "nrf_cli_libuarte.h"
  42. #include "nrf_libuarte_async.h"
  43. #include "nrf_assert.h"
  44. #include "nrf_atomic.h"
  45. #define NRF_LOG_MODULE_NAME cli_libuarte
  46. #if NRF_CLI_LIBUARTE_CONFIG_LOG_ENABLED
  47. #define NRF_LOG_LEVEL NRF_CLI_LIBUARTE_CONFIG_LOG_LEVEL
  48. #define NRF_LOG_INFO_COLOR NRF_CLI_LIBUARTE_CONFIG_INFO_COLOR
  49. #define NRF_LOG_DEBUG_COLOR NRF_CLI_LIBUARTE_CONFIG_DEBUG_COLOR
  50. #else //NRF_CLI_LIBUARTE_CONFIG_LOG_ENABLED
  51. #define NRF_LOG_LEVEL 0
  52. #endif //NRF_CLI_LIBUARTE_CONFIG_LOG_ENABLED
  53. #include "nrf_log.h"
  54. NRF_LOG_MODULE_REGISTER();
  55. static cli_libuarte_internal_t * mp_internal;
  56. static nrf_atomic_flag_t m_uart_busy;
  57. NRF_LIBUARTE_ASYNC_DEFINE(libuarte,
  58. NRF_CLI_LIBUARTE_UARTE_INSTANCE,
  59. NRF_CLI_LIBUARTE_TIMER_INSTANCE,
  60. NRF_CLI_LIBUARTE_TIMEOUT_RTC_INSTANCE,
  61. NRF_CLI_LIBUARTE_TIMEOUT_TIMER_INSTANCE,
  62. 3, 255);
  63. static void uart_event_handler(void * context, nrf_libuarte_async_evt_t * p_event)
  64. {
  65. cli_libuarte_internal_t * p_internal = mp_internal;
  66. ret_code_t err_code = NRF_SUCCESS;
  67. size_t len;
  68. UNUSED_VARIABLE(err_code);
  69. switch (p_event->type)
  70. {
  71. case NRF_LIBUARTE_ASYNC_EVT_ERROR:
  72. NRF_LOG_WARNING("(evt) ERROR");
  73. break;
  74. case NRF_LIBUARTE_ASYNC_EVT_RX_DATA:
  75. {
  76. len = (size_t)((uint32_t)p_event->data.rxtx.length & 0x0000FFFF);
  77. err_code = nrf_ringbuf_cpy_put(p_internal->p_rx_ringbuf,
  78. p_event->data.rxtx.p_data,
  79. &len);
  80. ASSERT(err_code == NRF_SUCCESS);
  81. if (len != p_event->data.rxtx.length)
  82. {
  83. NRF_LOG_WARNING("Data lost, no room in RX ringbuf");
  84. }
  85. nrf_libuarte_async_rx_free(&libuarte, p_event->data.rxtx.p_data, p_event->data.rxtx.length);
  86. if (p_event->data.rxtx.length)
  87. {
  88. NRF_LOG_DEBUG("(evt) RXRDY length:%d", p_event->data.rxtx.length);
  89. NRF_LOG_HEXDUMP_DEBUG(p_event->data.rxtx.p_data, p_event->data.rxtx.length);
  90. p_internal->p_cb->handler(NRF_CLI_TRANSPORT_EVT_RX_RDY,
  91. p_internal->p_cb->p_context);
  92. }
  93. break;
  94. }
  95. case NRF_LIBUARTE_ASYNC_EVT_TX_DONE:
  96. err_code = nrf_ringbuf_free(p_internal->p_tx_ringbuf, p_event->data.rxtx.length);
  97. ASSERT(err_code == NRF_SUCCESS);
  98. uint8_t * p_data;
  99. len = 255;
  100. err_code = nrf_ringbuf_get(p_internal->p_tx_ringbuf, &p_data, &len, true);
  101. ASSERT(err_code == NRF_SUCCESS);
  102. if (len)
  103. {
  104. NRF_LOG_DEBUG("(evt) Started TX (%d).", len);
  105. err_code = nrf_libuarte_async_tx(&libuarte, p_data, len);
  106. ASSERT(err_code == NRF_SUCCESS);
  107. }
  108. else
  109. {
  110. m_uart_busy = false;
  111. }
  112. p_internal->p_cb->handler(NRF_CLI_TRANSPORT_EVT_TX_RDY, p_internal->p_cb->p_context);
  113. NRF_LOG_DEBUG("(evt) TX completed (%d)", p_event->data.rxtx.length);
  114. break;
  115. default:
  116. NRF_LOG_ERROR("(evt) Unknown event");
  117. ASSERT(false);
  118. break;
  119. }
  120. }
  121. static ret_code_t cli_libuarte_init(nrf_cli_transport_t const * p_transport,
  122. void const * p_config,
  123. nrf_cli_transport_handler_t evt_handler,
  124. void * p_context)
  125. {
  126. cli_libuarte_internal_t * p_internal = CONTAINER_OF(p_transport,
  127. cli_libuarte_internal_t,
  128. transport);
  129. mp_internal = p_internal;
  130. m_uart_busy = false;
  131. p_internal->p_cb->handler = evt_handler;
  132. p_internal->p_cb->p_context = p_context;
  133. p_internal->p_cb->blocking = false;
  134. cli_libuarte_config_t const * p_cli_libuarte_config = (cli_libuarte_config_t *)p_config;
  135. nrf_libuarte_async_config_t uart_async_config = {
  136. .tx_pin = p_cli_libuarte_config->tx_pin,
  137. .rx_pin = p_cli_libuarte_config->rx_pin,
  138. .baudrate = p_cli_libuarte_config->baudrate,
  139. .parity = p_cli_libuarte_config->parity,
  140. .hwfc = p_cli_libuarte_config->hwfc,
  141. .timeout_us = 100,
  142. .int_prio = APP_IRQ_PRIORITY_LOW
  143. };
  144. ret_code_t err_code = nrf_libuarte_async_init(&libuarte, &uart_async_config, uart_event_handler, NULL);
  145. if (err_code == NRF_SUCCESS)
  146. {
  147. nrf_ringbuf_init(p_internal->p_rx_ringbuf);
  148. nrf_ringbuf_init(p_internal->p_tx_ringbuf);
  149. }
  150. return err_code;
  151. }
  152. static ret_code_t cli_libuarte_uninit(nrf_cli_transport_t const * p_transport)
  153. {
  154. UNUSED_PARAMETER(p_transport);
  155. nrf_libuarte_async_uninit(&libuarte);
  156. return NRF_SUCCESS;
  157. }
  158. static ret_code_t cli_libuarte_enable(nrf_cli_transport_t const * p_transport,
  159. bool blocking)
  160. {
  161. UNUSED_PARAMETER(p_transport);
  162. if (blocking)
  163. {
  164. return NRF_ERROR_NOT_SUPPORTED;
  165. }
  166. else
  167. {
  168. nrf_libuarte_async_enable(&libuarte);
  169. }
  170. return NRF_SUCCESS;
  171. }
  172. static ret_code_t cli_libuarte_read(nrf_cli_transport_t const * p_transport,
  173. void * p_data,
  174. size_t length,
  175. size_t * p_cnt)
  176. {
  177. ASSERT(p_cnt);
  178. cli_libuarte_internal_t * p_instance =
  179. CONTAINER_OF(p_transport, cli_libuarte_internal_t, transport);
  180. *p_cnt = length;
  181. ret_code_t err_code = nrf_ringbuf_cpy_get(p_instance->p_rx_ringbuf, p_data, p_cnt);
  182. if (*p_cnt)
  183. {
  184. NRF_LOG_DEBUG("Read %d bytes (requested %d)", *p_cnt, length);
  185. }
  186. return err_code;
  187. }
  188. static ret_code_t cli_libuarte_write(nrf_cli_transport_t const * p_transport,
  189. void const * p_data,
  190. size_t length,
  191. size_t * p_cnt)
  192. {
  193. ASSERT(p_cnt);
  194. cli_libuarte_internal_t * p_instance = CONTAINER_OF(p_transport,
  195. cli_libuarte_internal_t,
  196. transport);
  197. *p_cnt = length;
  198. ret_code_t err_code = nrf_ringbuf_cpy_put(p_instance->p_tx_ringbuf, p_data, p_cnt);
  199. if (err_code == NRF_SUCCESS)
  200. {
  201. NRF_LOG_DEBUG("Requested write: %d, copied to ringbuf: %d.", length, *p_cnt);
  202. if (nrf_atomic_flag_set_fetch(&m_uart_busy))
  203. {
  204. return err_code;
  205. }
  206. uint8_t * p_buf;
  207. size_t len = 255;
  208. if (nrf_ringbuf_get(p_instance->p_tx_ringbuf, &p_buf, &len, true) == NRF_SUCCESS)
  209. {
  210. NRF_LOG_DEBUG("Started TX (%d).", len);
  211. err_code = nrf_libuarte_async_tx(&libuarte, p_buf, len);
  212. if (p_instance->p_cb->blocking && (err_code == NRF_SUCCESS))
  213. {
  214. (void)nrf_ringbuf_free(p_instance->p_tx_ringbuf, len);
  215. m_uart_busy = false;
  216. }
  217. }
  218. }
  219. return err_code;
  220. }
  221. const nrf_cli_transport_api_t cli_libuarte_transport_api = {
  222. .init = cli_libuarte_init,
  223. .uninit = cli_libuarte_uninit,
  224. .enable = cli_libuarte_enable,
  225. .read = cli_libuarte_read,
  226. .write = cli_libuarte_write,
  227. };