nrf_cli_libuarte.c 9.1 KB

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