SEGGER_RTT_esp32.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright 2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "string.h"
  14. #include "freertos/FreeRTOS.h"
  15. #include "SEGGER_RTT.h"
  16. #include "SEGGER_SYSVIEW.h"
  17. #include "SEGGER_SYSVIEW_Conf.h"
  18. #if CONFIG_IDF_TARGET_ESP32
  19. #include "esp32/rom/ets_sys.h"
  20. #elif CONFIG_IDF_TARGET_ESP32S2
  21. #include "esp32s2/rom/ets_sys.h"
  22. #endif
  23. #include "esp_app_trace.h"
  24. #include "esp_log.h"
  25. const static char *TAG = "segger_rtt";
  26. #define SYSVIEW_EVENTS_BUF_SZ 255U
  27. // size of down channel data buf
  28. #define SYSVIEW_DOWN_BUF_SIZE 32
  29. #define SEGGER_STOP_WAIT_TMO 1000000 //us
  30. #if CONFIG_SYSVIEW_BUF_WAIT_TMO == -1
  31. #define SEGGER_HOST_WAIT_TMO ESP_APPTRACE_TMO_INFINITE
  32. #else
  33. #define SEGGER_HOST_WAIT_TMO CONFIG_SYSVIEW_BUF_WAIT_TMO
  34. #endif
  35. static uint8_t s_events_buf[SYSVIEW_EVENTS_BUF_SZ];
  36. static uint16_t s_events_buf_filled;
  37. static uint8_t s_down_buf[SYSVIEW_DOWN_BUF_SIZE];
  38. /*********************************************************************
  39. *
  40. * Public code
  41. *
  42. **********************************************************************
  43. */
  44. /*********************************************************************
  45. *
  46. * SEGGER_RTT_ESP32_FlushNoLock()
  47. *
  48. * Function description
  49. * Flushes buffered events.
  50. *
  51. * Parameters
  52. * min_sz Threshold for flushing data. If current filling level is above this value, data will be flushed. TRAX destinations only.
  53. * tmo Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinetly.
  54. *
  55. * Return value
  56. * None.
  57. */
  58. void SEGGER_RTT_ESP32_FlushNoLock(unsigned long min_sz, unsigned long tmo)
  59. {
  60. esp_err_t res;
  61. if (s_events_buf_filled > 0) {
  62. res = esp_apptrace_write(ESP_APPTRACE_DEST_TRAX, s_events_buf, s_events_buf_filled, tmo);
  63. if (res != ESP_OK) {
  64. ESP_LOGE(TAG, "Failed to flush buffered events (%d)!\n", res);
  65. }
  66. }
  67. // flush even if we failed to write buffered events, because no new events will be sent after STOP
  68. res = esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, min_sz, tmo);
  69. if (res != ESP_OK) {
  70. ESP_LOGE(TAG, "Failed to flush apptrace data (%d)!\n", res);
  71. }
  72. s_events_buf_filled = 0;
  73. }
  74. /*********************************************************************
  75. *
  76. * SEGGER_RTT_ESP32_Flush()
  77. *
  78. * Function description
  79. * Flushes buffered events.
  80. *
  81. * Parameters
  82. * min_sz Threshold for flushing data. If current filling level is above this value, data will be flushed. TRAX destinations only.
  83. * tmo Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinetly.
  84. *
  85. * Return value
  86. * None.
  87. */
  88. void SEGGER_RTT_ESP32_Flush(unsigned long min_sz, unsigned long tmo)
  89. {
  90. SEGGER_SYSVIEW_LOCK();
  91. SEGGER_RTT_ESP32_FlushNoLock(min_sz, tmo);
  92. SEGGER_SYSVIEW_UNLOCK();
  93. }
  94. /*********************************************************************
  95. *
  96. * SEGGER_RTT_ReadNoLock()
  97. *
  98. * Function description
  99. * Reads characters from SEGGER real-time-terminal control block
  100. * which have been previously stored by the host.
  101. * Do not lock against interrupts and multiple access.
  102. *
  103. * Parameters
  104. * BufferIndex Index of Down-buffer to be used (e.g. 0 for "Terminal").
  105. * pBuffer Pointer to buffer provided by target application, to copy characters from RTT-down-buffer to.
  106. * BufferSize Size of the target application buffer.
  107. *
  108. * Return value
  109. * Number of bytes that have been read.
  110. */
  111. unsigned SEGGER_RTT_ReadNoLock(unsigned BufferIndex, void* pData, unsigned BufferSize) {
  112. uint32_t size = BufferSize;
  113. esp_err_t res = esp_apptrace_read(ESP_APPTRACE_DEST_TRAX, pData, &size, 0);
  114. if (res != ESP_OK) {
  115. return 0;
  116. }
  117. return size;
  118. }
  119. /*********************************************************************
  120. *
  121. * SEGGER_RTT_WriteSkipNoLock
  122. *
  123. * Function description
  124. * Stores a specified number of characters in SEGGER RTT
  125. * control block which is then read by the host.
  126. * SEGGER_RTT_WriteSkipNoLock does not lock the application and
  127. * skips all data, if the data does not fit into the buffer.
  128. *
  129. * Parameters
  130. * BufferIndex Index of "Up"-buffer to be used (e.g. 0 for "Terminal").
  131. * pBuffer Pointer to character array. Does not need to point to a \0 terminated string.
  132. * NumBytes Number of bytes to be stored in the SEGGER RTT control block.
  133. *
  134. * Return value
  135. * Number of bytes which have been stored in the "Up"-buffer.
  136. *
  137. * Notes
  138. * (1) If there is not enough space in the "Up"-buffer, all data is dropped.
  139. * (2) For performance reasons this function does not call Init()
  140. * and may only be called after RTT has been initialized.
  141. * Either by calling SEGGER_RTT_Init() or calling another RTT API function first.
  142. */
  143. unsigned SEGGER_RTT_WriteSkipNoLock(unsigned BufferIndex, const void* pBuffer, unsigned NumBytes) {
  144. uint8_t *pbuf = (uint8_t *)pBuffer;
  145. uint8_t event_id = *pbuf;
  146. if (NumBytes > SYSVIEW_EVENTS_BUF_SZ) {
  147. ESP_LOGE(TAG, "Too large event %u bytes!", NumBytes);
  148. return 0;
  149. }
  150. if (xPortGetCoreID()) { // dual core specific code
  151. // use the highest - 1 bit of event ID to indicate core ID
  152. // the highest bit can not be used due to event ID encoding method
  153. // this reduces supported ID range to [0..63] (for 1 byte IDs) plus [128..16383] (for 2 bytes IDs)
  154. if (*pbuf & 0x80) { // 2 bytes ID
  155. *(pbuf + 1) |= (1 << 6);
  156. } else if (NumBytes != 10 || *pbuf != 0) { // ignore sync sequence
  157. *pbuf |= (1 << 6);
  158. }
  159. }
  160. if (s_events_buf_filled + NumBytes > SYSVIEW_EVENTS_BUF_SZ) {
  161. esp_err_t res = esp_apptrace_write(ESP_APPTRACE_DEST_TRAX, s_events_buf, s_events_buf_filled, SEGGER_HOST_WAIT_TMO);
  162. if (res != ESP_OK) {
  163. return 0; // skip current data buffer only, accumulated events are kept
  164. }
  165. s_events_buf_filled = 0;
  166. }
  167. memcpy(&s_events_buf[s_events_buf_filled], pBuffer, NumBytes);
  168. s_events_buf_filled += NumBytes;
  169. if (event_id == SYSVIEW_EVTID_TRACE_STOP) {
  170. SEGGER_RTT_ESP32_FlushNoLock(0, SEGGER_STOP_WAIT_TMO);
  171. }
  172. return NumBytes;
  173. }
  174. /*********************************************************************
  175. *
  176. * SEGGER_RTT_ConfigUpBuffer
  177. *
  178. * Function description
  179. * Run-time configuration of a specific up-buffer (T->H).
  180. * Buffer to be configured is specified by index.
  181. * This includes: Buffer address, size, name, flags, ...
  182. *
  183. * Parameters
  184. * BufferIndex Index of the buffer to configure.
  185. * sName Pointer to a constant name string.
  186. * pBuffer Pointer to a buffer to be used.
  187. * BufferSize Size of the buffer.
  188. * Flags Operating modes. Define behavior if buffer is full (not enough space for entire message).
  189. *
  190. * Return value
  191. * >= 0 - O.K.
  192. * < 0 - Error
  193. *
  194. * Additional information
  195. * Buffer 0 is configured on compile-time.
  196. * May only be called once per buffer.
  197. * Buffer name and flags can be reconfigured using the appropriate functions.
  198. */
  199. int SEGGER_RTT_ConfigUpBuffer(unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags) {
  200. s_events_buf_filled = 0;
  201. return 0;
  202. }
  203. /*********************************************************************
  204. *
  205. * SEGGER_RTT_ConfigDownBuffer
  206. *
  207. * Function description
  208. * Run-time configuration of a specific down-buffer (H->T).
  209. * Buffer to be configured is specified by index.
  210. * This includes: Buffer address, size, name, flags, ...
  211. *
  212. * Parameters
  213. * BufferIndex Index of the buffer to configure.
  214. * sName Pointer to a constant name string.
  215. * pBuffer Pointer to a buffer to be used.
  216. * BufferSize Size of the buffer.
  217. * Flags Operating modes. Define behavior if buffer is full (not enough space for entire message).
  218. *
  219. * Return value
  220. * >= 0 O.K.
  221. * < 0 Error
  222. *
  223. * Additional information
  224. * Buffer 0 is configured on compile-time.
  225. * May only be called once per buffer.
  226. * Buffer name and flags can be reconfigured using the appropriate functions.
  227. */
  228. int SEGGER_RTT_ConfigDownBuffer(unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags) {
  229. esp_apptrace_down_buffer_config(s_down_buf, sizeof(s_down_buf));
  230. return 0;
  231. }
  232. /*************************** End of file ****************************/