esp_rom_sys.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // Copyright 2021 Espressif Systems (Shanghai) CO 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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdbool.h>
  17. #include <stdarg.h>
  18. #include <assert.h>
  19. #include <unistd.h>
  20. #include "esp_rom_sys.h"
  21. static void call_linux_putc(char c);
  22. static void (*s_esp_rom_putc)(char c) = call_linux_putc;
  23. static void call_linux_putc(char c) {
  24. putc(c, stdout);
  25. }
  26. #define is_digit(c) ((c >= '0') && (c <= '9'))
  27. static int _cvt(unsigned long long val, char *buf, long radix, char *digits)
  28. {
  29. #ifdef SUPPORT_LITTLE_RADIX
  30. char temp[64];
  31. #else
  32. char temp[32];
  33. #endif
  34. char *cp = temp;
  35. int length = 0;
  36. if (val == 0) {
  37. /* Special case */
  38. *cp++ = '0';
  39. } else {
  40. while (val) {
  41. *cp++ = digits[val % radix];
  42. val /= radix;
  43. }
  44. }
  45. while (cp != temp) {
  46. *buf++ = *--cp;
  47. length++;
  48. }
  49. *buf = '\0';
  50. return (length);
  51. }
  52. static int esp_rom_vprintf(void (*putc)(char c), const char *fmt, va_list ap)
  53. {
  54. #ifdef BINARY_SUPPORT
  55. char buf[sizeof(long long)*8];
  56. int i;
  57. #else
  58. char buf[32];
  59. #endif
  60. char c, sign, *cp=buf;
  61. int left_prec, right_prec, zero_fill, pad, pad_on_right,
  62. islong, islonglong;
  63. long long val = 0;
  64. int res = 0, length = 0;
  65. while ((c = *fmt++) != '\0') {
  66. if (c == '%') {
  67. c = *fmt++;
  68. left_prec = right_prec = pad_on_right = islong = islonglong = 0;
  69. if (c == '-') {
  70. c = *fmt++;
  71. pad_on_right++;
  72. }
  73. if (c == '0') {
  74. zero_fill = true;
  75. c = *fmt++;
  76. } else {
  77. zero_fill = false;
  78. }
  79. while (is_digit(c)) {
  80. left_prec = (left_prec * 10) + (c - '0');
  81. c = *fmt++;
  82. }
  83. if (c == '.') {
  84. c = *fmt++;
  85. zero_fill++;
  86. while (is_digit(c)) {
  87. right_prec = (right_prec * 10) + (c - '0');
  88. c = *fmt++;
  89. }
  90. } else {
  91. right_prec = left_prec;
  92. }
  93. sign = '\0';
  94. if (c == 'l') {
  95. c = *fmt++;
  96. islong = 1;
  97. if (c == 'l') {
  98. c = *fmt++;
  99. islonglong = 1;
  100. }
  101. }
  102. switch (c) {
  103. case 'p':
  104. islong = 1;
  105. case 'd':
  106. case 'D':
  107. case 'x':
  108. case 'X':
  109. case 'u':
  110. case 'U':
  111. #ifdef BINARY_SUPPORT
  112. case 'b':
  113. case 'B':
  114. #endif
  115. if (islonglong) {
  116. val = va_arg(ap, long long);
  117. } else if (islong) {
  118. val = (long long)va_arg(ap, long);
  119. } else{
  120. val = (long long)va_arg(ap, int);
  121. }
  122. if ((c == 'd') || (c == 'D')) {
  123. if (val < 0) {
  124. sign = '-';
  125. val = -val;
  126. }
  127. } else {
  128. if (islong) {
  129. val &= (((long long)1) << (sizeof(long) * 8)) - 1;
  130. } else{
  131. val &= (((long long)1) << (sizeof(int) * 8)) - 1;
  132. }
  133. }
  134. break;
  135. default:
  136. break;
  137. }
  138. switch (c) {
  139. case 'p':
  140. (*putc)('0');
  141. (*putc)('x');
  142. zero_fill = true;
  143. left_prec = sizeof(unsigned long)*2;
  144. case 'd':
  145. case 'D':
  146. case 'u':
  147. case 'U':
  148. case 'x':
  149. case 'X':
  150. switch (c) {
  151. case 'd':
  152. case 'D':
  153. case 'u':
  154. case 'U':
  155. length = _cvt(val, buf, 10, "0123456789");
  156. break;
  157. case 'p':
  158. case 'x':
  159. length = _cvt(val, buf, 16, "0123456789abcdef");
  160. break;
  161. case 'X':
  162. length = _cvt(val, buf, 16, "0123456789ABCDEF");
  163. break;
  164. }
  165. cp = buf;
  166. break;
  167. case 's':
  168. case 'S':
  169. cp = va_arg(ap, char *);
  170. if (cp == NULL) {
  171. cp = "<null>";
  172. }
  173. length = 0;
  174. while (cp[length] != '\0') length++;
  175. break;
  176. case 'c':
  177. case 'C':
  178. c = va_arg(ap, int /*char*/);
  179. (*putc)(c);
  180. res++;
  181. continue;
  182. #ifdef BINARY_SUPPORT
  183. case 'b':
  184. case 'B':
  185. length = left_prec;
  186. if (left_prec == 0) {
  187. if (islonglong)
  188. length = sizeof(long long)*8;
  189. else if (islong)
  190. length = sizeof(long)*8;
  191. else
  192. length = sizeof(int)*8;
  193. }
  194. for (i = 0; i < length-1; i++) {
  195. buf[i] = ((val & ((long long)1<<i)) ? '1' : '.');
  196. }
  197. cp = buf;
  198. break;
  199. #endif
  200. case '%':
  201. (*putc)('%');
  202. break;
  203. default:
  204. (*putc)('%');
  205. (*putc)(c);
  206. res += 2;
  207. }
  208. pad = left_prec - length;
  209. if (sign != '\0') {
  210. pad--;
  211. }
  212. if (zero_fill) {
  213. c = '0';
  214. if (sign != '\0') {
  215. (*putc)(sign);
  216. res++;
  217. sign = '\0';
  218. }
  219. } else {
  220. c = ' ';
  221. }
  222. if (!pad_on_right) {
  223. while (pad-- > 0) {
  224. (*putc)(c);
  225. res++;
  226. }
  227. }
  228. if (sign != '\0') {
  229. (*putc)(sign);
  230. res++;
  231. }
  232. while (length-- > 0) {
  233. c = *cp++;
  234. (*putc)(c);
  235. res++;
  236. }
  237. if (pad_on_right) {
  238. while (pad-- > 0) {
  239. (*putc)(' ');
  240. res++;
  241. }
  242. }
  243. } else {
  244. (*putc)(c);
  245. res++;
  246. }
  247. }
  248. return (res);
  249. }
  250. int esp_rom_printf(const char *fmt, ...)
  251. {
  252. va_list list;
  253. va_start(list, fmt);
  254. int result = esp_rom_vprintf(s_esp_rom_putc, fmt, list);
  255. va_end(list);
  256. return result;
  257. }
  258. void esp_rom_delay_us(uint32_t us)
  259. {
  260. int sleep_result = usleep(us);
  261. assert(sleep_result == 0);
  262. (void)sleep_result; // Prevents compiler from optimizing out usleep() due to unused result. Also prevents warning.
  263. }
  264. void esp_rom_install_channel_putc(int channel, void (*putc)(char c))
  265. {
  266. if (putc != NULL) {
  267. s_esp_rom_putc = putc;
  268. }
  269. }
  270. void esp_rom_install_uart_printf(void)
  271. {
  272. // Since this is the linux implementation, we don't set any "UART" putc function, but the one which delegates to
  273. // the Linux libc version of putc.
  274. s_esp_rom_putc = call_linux_putc;
  275. }
  276. soc_reset_reason_t esp_rom_get_reset_reason(int cpu_no)
  277. {
  278. return RESET_REASON_CHIP_POWER_ON;
  279. }