3 #include <libopencm3/stm32/usart.h>
8 /*** Configuration ***/
10 // You should set the following parameters in config.h
12 // Use the semi-hosting interface for debugging messages
13 // #define DEBUG_SEMIHOSTING
15 // Use this USART for debugging messages
16 // #define DEBUG_USART USART1
18 /*** Implementation ***/
20 #ifdef DEBUG_SEMIHOSTING
22 void semi_put_char(char c)
24 // This is tricky, we need to work around GCC bugs
27 "mov r0, #0x03\n" /* SYS_WRITEC */
36 void semi_write_string(char *c)
39 "mov r0, #0x04\n" /* SYS_WRITE0 */
50 void debug_putc(int c)
52 #ifdef DEBUG_SEMIHOSTING
53 static char debug_buf[128];
55 debug_buf[debug_i++] = c;
56 if (c == '\n' || debug_i >= sizeof(debug_buf) - 1) {
57 debug_buf[debug_i] = 0;
58 semi_write_string(debug_buf);
64 usart_send_blocking(USART2, '\r');
65 usart_send_blocking(USART2, c);
69 void debug_puts(const char *s)
83 static void printf_string(const char *s, uint width, uint flags)
86 uint pad = (len < width) ? width - len : 0;
87 char pad_char = (flags & PF_ZERO_PAD) ? '0' : ' ';
93 if (!(flags & PF_LEFT))
97 static void printf_number(uint i, uint width, uint flags, uint base)
100 char *w = buf + sizeof(buf);
102 if (flags & PF_SIGNED) {
105 flags |= PF_NEGATIVE;
111 uint digit = i % base;
115 *--w = ((flags & PF_UPPERCASE) ? 'A' : 'a') + digit - 10;
120 if (flags & PF_NEGATIVE)
123 printf_string(w, width, flags);
126 void debug_printf(const char *fmt, ...)
148 flags |= PF_ZERO_PAD;
151 while (*fmt >= '0' && *fmt <= '9')
152 width = 10*width + *fmt++ - '0';
157 printf_number(va_arg(args, int), width, flags | PF_SIGNED, 10);
160 printf_number(va_arg(args, int), width, flags, 10);
163 flags |= PF_UPPERCASE;
166 printf_number(va_arg(args, int), width, flags, 16);
169 printf_string(va_arg(args, char *), width, flags);