nrf_cli_libuarte.c 9.5 KB

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