2 * Debugging Utilities for STM32
4 * (c) 2018--2019 Martin Mareš <mj@ucw.cz>
9 #include <libopencm3/stm32/gpio.h>
10 #include <libopencm3/stm32/usart.h>
15 /*** Configuration ***/
17 // You should set the following parameters in config.h
19 // Use the semi-hosting interface for debugging messages
20 // #define DEBUG_SEMIHOSTING
22 // Use this USART for debugging messages
23 // #define DEBUG_USART USART1
25 // Use this LED for debugging
26 #ifdef DEBUG_LED_BLUEPILL
27 #define DEBUG_LED_GPIO GPIOC
28 #define DEBUG_LED_PIN GPIO13
29 #define DEBUG_LED_INVERSE
32 /*** Implementation ***/
34 #ifdef DEBUG_SEMIHOSTING
36 void semi_put_char(char c)
38 // This is tricky, we need to work around GCC bugs
41 "mov r0, #0x03\n" /* SYS_WRITEC */
50 void semi_write_string(char *c)
53 "mov r0, #0x04\n" /* SYS_WRITE0 */
64 void debug_putc(int c)
66 #ifdef DEBUG_SEMIHOSTING
67 static char debug_buf[128];
69 debug_buf[debug_i++] = c;
70 if (c == '\n' || debug_i >= sizeof(debug_buf) - 1) {
71 debug_buf[debug_i] = 0;
72 semi_write_string(debug_buf);
78 usart_send_blocking(DEBUG_USART, '\r');
79 usart_send_blocking(DEBUG_USART, c);
83 void debug_puts(const char *s)
97 static void printf_string(const char *s, uint width, uint flags)
100 uint pad = (len < width) ? width - len : 0;
101 char pad_char = (flags & PF_ZERO_PAD) ? '0' : ' ';
106 debug_putc(pad_char);
107 if (!(flags & PF_LEFT))
111 static void printf_number(uint i, uint width, uint flags, uint base)
114 char *w = buf + sizeof(buf);
116 if (flags & PF_SIGNED) {
119 flags |= PF_NEGATIVE;
125 uint digit = i % base;
129 *--w = ((flags & PF_UPPERCASE) ? 'A' : 'a') + digit - 10;
134 if (flags & PF_NEGATIVE)
137 printf_string(w, width, flags);
140 void debug_printf(const char *fmt, ...)
162 flags |= PF_ZERO_PAD;
165 while (*fmt >= '0' && *fmt <= '9')
166 width = 10*width + *fmt++ - '0';
171 printf_number(va_arg(args, int), width, flags | PF_SIGNED, 10);
174 printf_number(va_arg(args, int), width, flags, 10);
177 flags |= PF_UPPERCASE;
180 printf_number(va_arg(args, int), width, flags, 16);
183 printf_string(va_arg(args, char *), width, flags);
194 void debug_led(bool light)
196 #ifdef DEBUG_LED_GPIO
197 #ifdef DEBUG_LED_INVERSE
201 gpio_set(DEBUG_LED_GPIO, DEBUG_LED_PIN);
203 gpio_clear(DEBUG_LED_GPIO, DEBUG_LED_PIN);
207 void debug_led_toggle(void)
209 #ifdef DEBUG_LED_GPIO
210 gpio_toggle(DEBUG_LED_GPIO, DEBUG_LED_PIN);