hpm_debug_console.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2021-2024 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef __ICCRISCV__
  8. #include <sys/stat.h>
  9. #endif
  10. #include "hpm_debug_console.h"
  11. #if !defined(CONFIG_NDEBUG_CONSOLE) || !CONFIG_NDEBUG_CONSOLE
  12. #include "hpm_uart_drv.h"
  13. static UART_Type* g_console_uart = NULL;
  14. hpm_stat_t console_init(console_config_t *cfg)
  15. {
  16. hpm_stat_t stat = status_fail;
  17. if (cfg->type == CONSOLE_TYPE_UART) {
  18. uart_config_t config = {0};
  19. uart_default_config((UART_Type *)cfg->base, &config);
  20. config.src_freq_in_hz = cfg->src_freq_in_hz;
  21. config.baudrate = cfg->baudrate;
  22. stat = uart_init((UART_Type *)cfg->base, &config);
  23. if (status_success == stat) {
  24. g_console_uart = (UART_Type *)cfg->base;
  25. }
  26. }
  27. return stat;
  28. }
  29. uint8_t console_receive_byte(void)
  30. {
  31. uint8_t c;
  32. while (status_success != uart_receive_byte(g_console_uart, &c)) {
  33. };
  34. return c;
  35. }
  36. uint8_t console_try_receive_byte(void)
  37. {
  38. uint8_t c = 0;
  39. uart_try_receive_byte(g_console_uart, &c);
  40. return c;
  41. }
  42. void console_send_byte(uint8_t c)
  43. {
  44. while (status_success != uart_send_byte(g_console_uart, c)) {
  45. }
  46. }
  47. #ifdef __SEGGER_RTL_VERSION
  48. #include <stdio.h>
  49. #include "__SEGGER_RTL_Int.h"
  50. static int _stdin_ungot = EOF;
  51. struct __SEGGER_RTL_FILE_impl { /* NOTE: Provides implementation for FILE */
  52. int stub; /* only needed so impl has size != 0. */
  53. };
  54. static FILE __SEGGER_RTL_stdin_file = { 0 }; /* stdin reads from UART */
  55. static FILE __SEGGER_RTL_stdout_file = { 0 }; /* stdout writes to UART */
  56. static FILE __SEGGER_RTL_stderr_file = { 0 }; /* stderr writes to UART */
  57. __attribute__((used)) FILE *stdin = &__SEGGER_RTL_stdin_file; /* NOTE: Provide implementation of stdin for RTL. */
  58. __attribute__((used)) FILE *stdout = &__SEGGER_RTL_stdout_file; /* NOTE: Provide implementation of stdout for RTL. */
  59. __attribute__((used)) FILE *stderr = &__SEGGER_RTL_stderr_file; /* NOTE: Provide implementation of stderr for RTL. */
  60. __attribute__((used)) int __SEGGER_RTL_X_file_write(__SEGGER_RTL_FILE *file, const char *data, unsigned int size)
  61. {
  62. unsigned int count;
  63. (void)file;
  64. for (count = 0; count < size; count++) {
  65. if (data[count] == '\n') {
  66. while (status_success != uart_send_byte(g_console_uart, '\r')) {
  67. }
  68. }
  69. while (status_success != uart_send_byte(g_console_uart, data[count])) {
  70. }
  71. }
  72. while (status_success != uart_flush(g_console_uart)) {
  73. }
  74. return count;
  75. }
  76. __attribute__((used)) int __SEGGER_RTL_X_file_read(__SEGGER_RTL_FILE *file, char *s, unsigned int size)
  77. {
  78. (void)file;
  79. (void) size;
  80. while (status_success != uart_receive_byte(g_console_uart, (uint8_t *)s)) {
  81. }
  82. return 1;
  83. }
  84. __attribute__((used)) int __SEGGER_RTL_X_file_stat(__SEGGER_RTL_FILE *stream)
  85. {
  86. (void) stream;
  87. return 0;
  88. }
  89. __attribute__((used)) int __SEGGER_RTL_X_file_bufsize(__SEGGER_RTL_FILE *stream)
  90. {
  91. (void) stream;
  92. return 1;
  93. }
  94. __attribute__((used)) int __SEGGER_RTL_X_file_unget(__SEGGER_RTL_FILE *stream, int c)
  95. {
  96. if (stream == stdin) {
  97. if (c != EOF && _stdin_ungot == EOF) {
  98. _stdin_ungot = c;
  99. } else {
  100. c = EOF;
  101. }
  102. } else {
  103. c = EOF;
  104. }
  105. return c;
  106. }
  107. __attribute__((used)) int __SEGGER_RTL_X_file_flush(__SEGGER_RTL_FILE *__stream)
  108. {
  109. (void) __stream;
  110. return 1;
  111. }
  112. #endif
  113. int _write(int file, char *data, int size)
  114. {
  115. int count;
  116. (void)file;
  117. for (count = 0; count < size; count++) {
  118. if (data[count] == '\n') {
  119. while (status_success != uart_send_byte(g_console_uart, '\r')) {
  120. }
  121. }
  122. while (status_success != uart_send_byte(g_console_uart, data[count])) {
  123. }
  124. }
  125. while (status_success != uart_flush(g_console_uart)) {
  126. }
  127. return count;
  128. }
  129. int _read(int file, char *s, int size)
  130. {
  131. (void)file;
  132. (void) size;
  133. while (status_success != uart_receive_byte(g_console_uart, (uint8_t *)s)) {
  134. }
  135. return 1;
  136. }
  137. #else
  138. /* stub functions */
  139. hpm_stat_t console_init(console_config_t *cfg)
  140. {
  141. (void) cfg;
  142. return status_success;
  143. }
  144. uint8_t console_receive_byte(void)
  145. {
  146. return 0xFF;
  147. }
  148. uint8_t console_try_receive_byte(void)
  149. {
  150. uint8_t c = 0;
  151. return c;
  152. }
  153. void console_send_byte(uint8_t c)
  154. {
  155. (void) c;
  156. }
  157. #ifdef __SEGGER_RTL_VERSION
  158. #include <stdio.h>
  159. #include "__SEGGER_RTL_Int.h"
  160. struct __SEGGER_RTL_FILE_impl { /* NOTE: Provides implementation for FILE */
  161. int stub; /* only needed so impl has size != 0. */
  162. };
  163. static FILE __SEGGER_RTL_stdin_file = { 0 }; /* stdin reads from UART */
  164. static FILE __SEGGER_RTL_stdout_file = { 0 }; /* stdout writes to UART */
  165. static FILE __SEGGER_RTL_stderr_file = { 0 }; /* stderr writes to UART */
  166. __attribute__((used)) FILE *stdin = &__SEGGER_RTL_stdin_file; /* NOTE: Provide implementation of stdin for RTL. */
  167. __attribute__((used)) FILE *stdout = &__SEGGER_RTL_stdout_file; /* NOTE: Provide implementation of stdout for RTL. */
  168. __attribute__((used)) FILE *stderr = &__SEGGER_RTL_stderr_file; /* NOTE: Provide implementation of stderr for RTL. */
  169. __attribute__((used)) int __SEGGER_RTL_X_file_write(__SEGGER_RTL_FILE *file, const char *data, unsigned int size)
  170. {
  171. (void) file;
  172. (void) data;
  173. return size;
  174. }
  175. __attribute__((used)) int __SEGGER_RTL_X_file_read(__SEGGER_RTL_FILE *file, char *s, unsigned int size)
  176. {
  177. (void) file;
  178. (void) size;
  179. (void) s;
  180. return 1;
  181. }
  182. __attribute__((used)) int __SEGGER_RTL_X_file_stat(__SEGGER_RTL_FILE *stream)
  183. {
  184. (void) stream;
  185. return 0;
  186. }
  187. __attribute__((used)) int __SEGGER_RTL_X_file_bufsize(__SEGGER_RTL_FILE *stream)
  188. {
  189. (void) stream;
  190. return 1;
  191. }
  192. __attribute__((used)) int __SEGGER_RTL_X_file_unget(__SEGGER_RTL_FILE *stream, int c)
  193. {
  194. (void) stream;
  195. (void) c;
  196. return EOF;
  197. }
  198. __attribute__((used)) int __SEGGER_RTL_X_file_flush(__SEGGER_RTL_FILE *__stream)
  199. {
  200. (void) __stream;
  201. return 1;
  202. }
  203. #endif
  204. ATTR_WEAK int _write(int file, char *data, int size)
  205. {
  206. (void) file;
  207. (void) data;
  208. return size;
  209. }
  210. ATTR_WEAK int _read(int file, char *s, int size)
  211. {
  212. (void) file;
  213. (void) size;
  214. (void) s;
  215. return 1;
  216. }
  217. #endif
  218. #ifndef __ICCRISCV__
  219. int _fstat(int file, struct stat *s)
  220. {
  221. (void) file;
  222. s->st_mode = S_IFCHR;
  223. return 0;
  224. }
  225. #else
  226. #ifndef _DLIB_FILE_DESCRIPTOR
  227. #define _DLIB_FILE_DESCRIPTOR 0
  228. #endif
  229. int __write(int file, char *data, int size)
  230. {
  231. return _write(file, data, size);
  232. }
  233. int __read(int file, char *s, int size)
  234. {
  235. return _read(file, s, size);
  236. }
  237. #endif