3 #include <libopencm3/stm32/usart.h>
8 #ifdef DEBUG_SEMIHOSTING
10 void semi_put_char(char c)
12 // This is tricky, we need to work around GCC bugs
15 "mov r0, #0x03\n" /* SYS_WRITEC */
24 void semi_write_string(char *c)
27 "mov r0, #0x04\n" /* SYS_WRITE0 */
38 void debug_putc(int c)
40 #ifdef DEBUG_SEMIHOSTING
41 static char debug_buf[128];
43 debug_buf[debug_i++] = c;
44 if (c == '\n' || debug_i >= sizeof(debug_buf) - 1) {
45 debug_buf[debug_i] = 0;
46 semi_write_string(debug_buf);
52 usart_send_blocking(USART2, '\r');
53 usart_send_blocking(USART2, c);
57 void debug_puts(const char *s)
71 static void printf_string(const char *s, uint width, uint flags)
74 uint pad = (len < width) ? width - len : 0;
75 char pad_char = (flags & PF_ZERO_PAD) ? '0' : ' ';
81 if (!(flags & PF_LEFT))
85 static void printf_number(uint i, uint width, uint flags, uint base)
88 char *w = buf + sizeof(buf);
90 if (flags & PF_SIGNED) {
99 uint digit = i % base;
103 *--w = ((flags & PF_UPPERCASE) ? 'A' : 'a') + digit - 10;
108 if (flags & PF_NEGATIVE)
111 printf_string(w, width, flags);
114 void debug_printf(const char *fmt, ...)
136 flags |= PF_ZERO_PAD;
139 while (*fmt >= '0' && *fmt <= '9')
140 width = 10*width + *fmt++ - '0';
145 printf_number(va_arg(args, int), width, flags | PF_SIGNED, 10);
148 printf_number(va_arg(args, int), width, flags, 10);
151 flags |= PF_UPPERCASE;
154 printf_number(va_arg(args, int), width, flags, 16);
157 printf_string(va_arg(args, char *), width, flags);