nrf_cli_rtt.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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_RTT)
  42. #include <SEGGER_RTT_Conf.h>
  43. #include <SEGGER_RTT.h>
  44. #include "nrf_cli_rtt.h"
  45. #include "nrf_assert.h"
  46. #include "nrf_delay.h"
  47. #define RTT_RX_TIMEOUT 100
  48. static bool m_host_present;
  49. static void timer_handler(void * p_context)
  50. {
  51. nrf_cli_rtt_internal_t * p_internal = (nrf_cli_rtt_internal_t *)p_context;
  52. if (SEGGER_RTT_HasData(0))
  53. {
  54. p_internal->p_cb->handler(NRF_CLI_TRANSPORT_EVT_RX_RDY, p_internal->p_cb->p_context);
  55. }
  56. p_internal->p_cb->handler(NRF_CLI_TRANSPORT_EVT_TX_RDY, p_internal->p_cb->p_context);
  57. ret_code_t err_code = app_timer_start(*p_internal->p_timer,
  58. APP_TIMER_TICKS(RTT_RX_TIMEOUT),
  59. p_context);
  60. ASSERT(err_code == NRF_SUCCESS);
  61. UNUSED_VARIABLE(err_code);
  62. }
  63. static ret_code_t cli_rtt_init(nrf_cli_transport_t const * p_transport,
  64. void const * p_config,
  65. nrf_cli_transport_handler_t evt_handler,
  66. void * p_context)
  67. {
  68. UNUSED_PARAMETER(p_config);
  69. nrf_cli_rtt_internal_t * p_internal =
  70. CONTAINER_OF(p_transport, nrf_cli_rtt_internal_t, transport);
  71. p_internal->p_cb->handler = evt_handler;
  72. p_internal->p_cb->p_context = p_context;
  73. p_internal->p_cb->timer_created = false;
  74. SEGGER_RTT_Init();
  75. m_host_present = true;
  76. return NRF_SUCCESS;
  77. }
  78. static ret_code_t cli_rtt_uninit(nrf_cli_transport_t const * p_transport)
  79. {
  80. nrf_cli_rtt_internal_t * p_internal =
  81. CONTAINER_OF(p_transport, nrf_cli_rtt_internal_t, transport);
  82. return app_timer_stop(*p_internal->p_timer);
  83. }
  84. static ret_code_t cli_rtt_enable(nrf_cli_transport_t const * p_transport,
  85. bool blocking)
  86. {
  87. nrf_cli_rtt_internal_t * p_internal =
  88. CONTAINER_OF(p_transport, nrf_cli_rtt_internal_t, transport);
  89. ret_code_t err_code = NRF_SUCCESS;
  90. if (p_internal->p_cb->timer_created)
  91. {
  92. err_code = app_timer_stop(*p_internal->p_timer); //Timer may be running or inactive
  93. if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_INVALID_STATE))
  94. {
  95. return err_code;
  96. }
  97. else
  98. {
  99. err_code = NRF_SUCCESS;
  100. }
  101. }
  102. if (!blocking)
  103. {
  104. if (!p_internal->p_cb->timer_created)
  105. {
  106. err_code = app_timer_create(p_internal->p_timer,
  107. APP_TIMER_MODE_SINGLE_SHOT,
  108. timer_handler);
  109. p_internal->p_cb->timer_created = true;
  110. }
  111. if (err_code == NRF_SUCCESS)
  112. {
  113. err_code = app_timer_start(*p_internal->p_timer,
  114. APP_TIMER_TICKS(RTT_RX_TIMEOUT),
  115. p_internal);
  116. SEGGER_RTT_Init();
  117. }
  118. }
  119. return err_code;
  120. }
  121. static ret_code_t cli_rtt_read(nrf_cli_transport_t const * p_transport,
  122. void * p_data,
  123. size_t length,
  124. size_t * p_cnt)
  125. {
  126. ASSERT(p_cnt);
  127. UNUSED_PARAMETER(p_transport);
  128. size_t rcnt = SEGGER_RTT_Read(NRF_CLI_RTT_TERMINAL_ID, p_data, length);
  129. *p_cnt = rcnt;
  130. return NRF_SUCCESS;
  131. }
  132. static ret_code_t cli_rtt_write(nrf_cli_transport_t const * p_transport,
  133. const void * p_data,
  134. size_t length,
  135. size_t * p_cnt)
  136. {
  137. ASSERT(p_cnt);
  138. UNUSED_PARAMETER(p_transport);
  139. if (!(CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk))
  140. {
  141. /* If an RTT session is not active, but the RTT console is processed, the program may hang.
  142. * Workaround: If the debugger is not connected, always return NRF_SUCCESS.
  143. */
  144. *p_cnt = length;
  145. return NRF_SUCCESS;
  146. }
  147. size_t idx = 0;
  148. uint32_t processed;
  149. uint32_t watchdog_counter = NRF_CLI_RTT_TX_RETRY_CNT;
  150. const uint8_t * p_buffer = (const uint8_t *)p_data;
  151. do {
  152. processed = SEGGER_RTT_Write(NRF_CLI_RTT_TERMINAL_ID, &p_buffer[idx], length);
  153. if (processed == 0)
  154. {
  155. /* There are two possible reasons for not writing any data to RTT:
  156. * - The host is not connected and not reading the data.
  157. * - The buffer got full and will be read by the host.
  158. * These two situations are distinguished using the following algorithm.
  159. * At the begining, the module assumes that the host is active,
  160. * so when no data is read, it busy waits and retries.
  161. * If, after retrying, the host reads the data, the module assumes that the host is active.
  162. * If it fails, the module assumes that the host is inactive and stores that information. On next
  163. * call, only one attempt takes place. The host is marked as active if the attempt is successful.
  164. */
  165. if (!m_host_present)
  166. {
  167. break;
  168. }
  169. else
  170. {
  171. nrf_delay_ms(NRF_CLI_RTT_TX_RETRY_DELAY_MS);
  172. watchdog_counter--;
  173. if (watchdog_counter == 0)
  174. {
  175. m_host_present = false;
  176. break;
  177. }
  178. }
  179. }
  180. m_host_present = true;
  181. idx += processed;
  182. length -= processed;
  183. } while (length);
  184. if (idx > 0)
  185. {
  186. *p_cnt = idx;
  187. }
  188. else
  189. {
  190. *p_cnt = length;
  191. }
  192. return NRF_SUCCESS;
  193. }
  194. const nrf_cli_transport_api_t nrf_cli_rtt_transport_api = {
  195. .init = cli_rtt_init,
  196. .uninit = cli_rtt_uninit,
  197. .enable = cli_rtt_enable,
  198. .read = cli_rtt_read,
  199. .write = cli_rtt_write,
  200. };
  201. #endif