2 #include "stm32f1xx_hal.h"
7 #ifdef DEBUG_SEMIHOSTING
9 void semi_put_char(char c)
11 // This is tricky, we need to work around GCC bugs
14 "mov r0, #0x03\n" /* SYS_WRITEC */
23 void semi_write_string(char *c)
26 "mov r0, #0x04\n" /* SYS_WRITE0 */
37 void debug_putc(int c)
39 #ifdef DEBUG_SEMIHOSTING
40 static char debug_buf[128];
42 debug_buf[debug_i++] = c;
43 if (c == '\n' || debug_i >= sizeof(debug_buf) - 1)
45 debug_buf[debug_i] = 0;
46 semi_write_string(debug_buf);
53 while (!LL_USART_IsActiveFlag_TXE(DEBUG_USART))
55 LL_USART_TransmitData8(DEBUG_USART, c);
59 void debug_puts(const char *s)
73 static void printf_string(const char *s, uint width, uint flags)
76 uint pad = (len < width) ? width - len : 0;
77 char pad_char = (flags & PF_ZERO_PAD) ? '0' : ' ';
83 if (!(flags & PF_LEFT))
87 static void printf_number(uint i, uint width, uint flags, uint base)
90 char *w = buf + sizeof(buf);
92 if (flags & PF_SIGNED)
104 uint digit = i % base;
108 *--w = ((flags & PF_UPPERCASE) ? 'A' : 'a') + digit - 10;
113 if (flags & PF_NEGATIVE)
116 printf_string(w, width, flags);
119 void debug_printf(const char *fmt, ...)
145 flags |= PF_ZERO_PAD;
148 while (*fmt >= '0' && *fmt <= '9')
149 width = 10*width + *fmt++ - '0';
155 printf_number(va_arg(args, int), width, flags | PF_SIGNED, 10);
158 printf_number(va_arg(args, int), width, flags, 10);
161 flags |= PF_UPPERCASE;
164 printf_number(va_arg(args, int), width, flags, 16);
167 printf_string(va_arg(args, char *), width, flags);