From 2d024d38ea3094cf50ef92e00589fb56a066ea9a Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 24 Jun 2018 15:00:28 +0200 Subject: [PATCH] Debugging... --- Inc/usb.h | 19 +++--- Inc/util.h | 19 ++++++ Makefile | 1 + Src/debug.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Src/main.c | 166 +-------------------------------------------------- Src/usb.c | 27 +++++---- 6 files changed, 215 insertions(+), 184 deletions(-) create mode 100644 Inc/util.h create mode 100644 Src/debug.c diff --git a/Inc/usb.h b/Inc/usb.h index 1d8291f..e33a71f 100644 --- a/Inc/usb.h +++ b/Inc/usb.h @@ -1,15 +1,6 @@ #define USB_SELF_POWERED #define USB_NUM_CONFIGURATIONS 1 - -typedef unsigned int uint; -typedef uint8_t byte; -typedef uint16_t u16; -typedef int16_t s16; -typedef uint32_t u32; -typedef int32_t s32; - -#define MIN(x,y) ((x) < (y) ? (x) : (y)) -#define MAX(x,y) ((x) > (y) ? (x) : (y)) +#define USB_DEBUG /*** USB state structure ***/ @@ -39,6 +30,7 @@ struct usb { }; void usb_init(struct usb *usb, PCD_HandleTypeDef *hpcd); +void usb_start(struct usb *usb); enum usb_device_state { USB_STATE_DEFAULT, @@ -57,6 +49,13 @@ enum usb_ep0_state { USB_EP0_STALL, }; +#ifdef USB_DEBUG +#define usb_debug debug_printf +#else +static inline void usb_debug(char *msg, ...) +{ } +#endif + /*** Constants from USB specs ***/ #define USB_REQ_DIRECTION 0x80 diff --git a/Inc/util.h b/Inc/util.h new file mode 100644 index 0000000..07a677b --- /dev/null +++ b/Inc/util.h @@ -0,0 +1,19 @@ +#include + +typedef unsigned int uint; +typedef uint8_t byte; +typedef uint16_t u16; +typedef int16_t s16; +typedef uint32_t u32; +typedef int32_t s32; + +#define MIN(x,y) ((x) < (y) ? (x) : (y)) +#define MAX(x,y) ((x) > (y) ? (x) : (y)) + +// debug.c + +#define DEBUG_SEMIHOSTING + +void debug_printf(const char *fmt, ...); +void debug_puts(const char *s); +void debug_putc(int c); diff --git a/Makefile b/Makefile index 7033be3..a50db71 100644 --- a/Makefile +++ b/Makefile @@ -53,6 +53,7 @@ BUILD_DIR = build C_SOURCES = \ /aux/misc/stm/F1-package/Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_exti.c \ Src/main.c \ +Src/debug.c \ Src/usb.c \ /aux/misc/stm/F1-package/Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_rcc.c \ /Src/system_stm32f1xx.c \ diff --git a/Src/debug.c b/Src/debug.c new file mode 100644 index 0000000..d8b3d63 --- /dev/null +++ b/Src/debug.c @@ -0,0 +1,167 @@ +#include "util.h" +#include "stm32f1xx_hal.h" + +#include +#include + +void semi_put_char(char c) +{ +#ifdef DEBUG_SEMIHOSTING + // This is tricky, we need to work around GCC bugs + volatile char cc = c; + asm volatile ( + "mov r0, #0x03\n" /* SYS_WRITEC */ + "mov r1, %[msg]\n" + "bkpt #0xAB\n" + : + : [msg] "r" (&cc) + : "r0", "r1" + ); +#endif +} + +void semi_write_string(char *c) +{ +#ifdef DEBUG_SEMIHOSTING + asm volatile ( + "mov r0, #0x04\n" /* SYS_WRITE0 */ + "mov r1, %[msg]\n" + "bkpt #0xAB\n" + : + : [msg] "r" (c) + : "r0", "r1" + ); +#endif +} + +void debug_putc(int c) +{ + static char debug_buf[128]; + static int debug_i; + debug_buf[debug_i++] = c; + if (c == '\n' || debug_i >= sizeof(debug_buf) - 1) + { + debug_buf[debug_i] = 0; + semi_write_string(debug_buf); + debug_i = 0; + } +} + +void debug_puts(const char *s) +{ + while (*s) + debug_putc(*s++); +} + +enum printf_flags { + PF_ZERO_PAD = 1, + PF_SIGNED = 2, + PF_NEGATIVE = 4, + PF_UPPERCASE = 8, + PF_LEFT = 16, +}; + +static void printf_string(const char *s, uint width, uint flags) +{ + uint len = strlen(s); + uint pad = (len < width) ? width - len : 0; + char pad_char = (flags & PF_ZERO_PAD) ? '0' : ' '; + + if (flags & PF_LEFT) + debug_puts(s); + while (pad--) + debug_putc(pad_char); + if (!(flags & PF_LEFT)) + debug_puts(s); +} + +static void printf_number(uint i, uint width, uint flags, uint base) +{ + char buf[16]; + char *w = buf + sizeof(buf); + + if (flags & PF_SIGNED) + { + if ((int) i < 0) + { + i = - (int) i; + flags |= PF_NEGATIVE; + } + } + + *--w = 0; + do + { + uint digit = i % base; + if (digit < 10) + *--w = '0' + digit; + else + *--w = ((flags & PF_UPPERCASE) ? 'A' : 'a') + digit - 10; + i /= base; + } + while (i); + + if (flags & PF_NEGATIVE) + *--w = '-'; + + printf_string(w, width, flags); +} + +void debug_printf(const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + + while (*fmt) + { + int c = *fmt++; + if (c != '%') + { + debug_putc(c); + continue; + } + + uint width = 0; + uint flags = 0; + + if (*fmt == '-') + { + fmt++; + flags |= PF_LEFT; + } + + if (*fmt == '0') + { + fmt++; + flags |= PF_ZERO_PAD; + } + + while (*fmt >= '0' && *fmt <= '9') + width = 10*width + *fmt++ - '0'; + + c = *fmt++; + switch (c) + { + case 'd': + printf_number(va_arg(args, int), width, flags | PF_SIGNED, 10); + break; + case 'u': + printf_number(va_arg(args, int), width, flags, 10); + break; + case 'X': + flags |= PF_UPPERCASE; + // fall-thru + case 'x': + printf_number(va_arg(args, int), width, flags, 16); + break; + case 's': + printf_string(va_arg(args, char *), width, flags); + break; + default: + debug_putc(c); + continue; + } + } + + va_end(args); +} diff --git a/Src/main.c b/Src/main.c index 9d3dbb8..1c016c3 100644 --- a/Src/main.c +++ b/Src/main.c @@ -37,6 +37,7 @@ ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ +#include "util.h" #include "main.h" #include "stm32f1xx_hal.h" #include "usb.h" @@ -69,168 +70,6 @@ static void MX_USB_PCD_Init(void); /* USER CODE BEGIN 0 */ -#include -#include -#include - -void semi_put_char(char c) -{ - // This is tricky, we need to work around GCC bugs - volatile char cc = c; - asm volatile ( - "mov r0, #0x03\n" /* SYS_WRITEC */ - "mov r1, %[msg]\n" - "bkpt #0xAB\n" - : - : [msg] "r" (&cc) - : "r0", "r1" - ); -} - -void semi_write_string(char *c) -{ - asm volatile ( - "mov r0, #0x04\n" /* SYS_WRITE0 */ - "mov r1, %[msg]\n" - "bkpt #0xAB\n" - : - : [msg] "r" (c) - : "r0", "r1" - ); -} - -void debug_putc(int c) -{ - static char debug_buf[16]; - static int debug_i; - debug_buf[debug_i++] = c; - if (c == '\n' || debug_i >= sizeof(debug_buf) - 1) - { - debug_buf[debug_i] = 0; - semi_write_string(debug_buf); - debug_i = 0; - } -} - -void debug_puts(const char *s) -{ - while (*s) - debug_putc(*s++); -} - -enum printf_flags { - PF_ZERO_PAD = 1, - PF_SIGNED = 2, - PF_NEGATIVE = 4, - PF_UPPERCASE = 8, - PF_LEFT = 16, -}; - -static void printf_string(const char *s, uint width, uint flags) -{ - uint len = strlen(s); - uint pad = (len < width) ? width - len : 0; - char pad_char = (flags & PF_ZERO_PAD) ? '0' : ' '; - - if (flags & PF_LEFT) - debug_puts(s); - while (pad--) - debug_putc(pad_char); - if (!(flags & PF_LEFT)) - debug_puts(s); -} - -static void printf_number(uint i, uint width, uint flags, uint base) -{ - char buf[16]; - char *w = buf + sizeof(buf); - - if (flags & PF_SIGNED) - { - if ((int) i < 0) - { - i = - (int) i; - flags |= PF_NEGATIVE; - } - } - - *--w = 0; - do - { - uint digit = i % base; - if (digit < 10) - *--w = '0' + digit; - else - *--w = ((flags & PF_UPPERCASE) ? 'A' : 'a') + digit - 10; - i /= base; - } - while (i); - - if (flags & PF_NEGATIVE) - *--w = '-'; - - printf_string(w, width, flags); -} - -void debug_printf(const char *fmt, ...) -{ - va_list args; - va_start(args, fmt); - - while (*fmt) - { - int c = *fmt++; - if (c != '%') - { - debug_putc(c); - continue; - } - - uint width = 0; - uint flags = 0; - - if (*fmt == '-') - { - fmt++; - flags |= PF_LEFT; - } - - if (*fmt == '0') - { - fmt++; - flags |= PF_ZERO_PAD; - } - - while (*fmt >= '0' && *fmt <= '9') - width = 10*width + *fmt++ - '0'; - - c = *fmt++; - switch (c) - { - case 'd': - printf_number(va_arg(args, int), width, flags | PF_SIGNED, 10); - break; - case 'u': - printf_number(va_arg(args, int), width, flags, 10); - break; - case 'X': - flags |= PF_UPPERCASE; - // fall-thru - case 'x': - printf_number(va_arg(args, int), width, flags, 16); - break; - case 's': - printf_string(va_arg(args, char *), width, flags); - break; - default: - debug_putc(c); - continue; - } - } - - va_end(args); -} - /* USER CODE END 0 */ /** @@ -257,6 +96,7 @@ int main(void) SystemClock_Config(); /* USER CODE BEGIN SysInit */ + usb_init(&usb, &hpcd_USB_FS); /* USER CODE END SysInit */ @@ -266,7 +106,7 @@ int main(void) MX_I2C2_Init(); MX_USB_PCD_Init(); /* USER CODE BEGIN 2 */ - usb_init(&usb, &hpcd_USB_FS); + usb_start(&usb); /* USER CODE END 2 */ diff --git a/Src/usb.c b/Src/usb.c index 2c366e2..ce0bddc 100644 --- a/Src/usb.c +++ b/Src/usb.c @@ -1,6 +1,7 @@ #include "stm32f1xx.h" #include "stm32f1xx_hal.h" +#include "util.h" #include "usb.h" #include @@ -87,11 +88,15 @@ void usb_init(struct usb *usb, PCD_HandleTypeDef *hpcd) usb->hpcd = hpcd; usb->state = USB_STATE_DEFAULT; usb->ep0_state = USB_EP0_IDLE; + hpcd->pData = usb; +} - HAL_PCDEx_PMAConfig(hpcd, 0x00, PCD_SNG_BUF, 0x18); - HAL_PCDEx_PMAConfig(hpcd, 0x80, PCD_SNG_BUF, 0x58); +void usb_start(struct usb *usb) +{ + HAL_PCDEx_PMAConfig(usb->hpcd, 0x00, PCD_SNG_BUF, 0x18); + HAL_PCDEx_PMAConfig(usb->hpcd, 0x80, PCD_SNG_BUF, 0x58); - HAL_PCD_Start(hpcd); + HAL_PCD_Start(usb->hpcd); } static inline uint get_u16(byte *p) @@ -105,15 +110,9 @@ static inline void put_u16(byte *p, u16 x) p[1] = x >> 8; } -#if 0 // FIXME -static struct usb_endpoint *ep_by_addr(struct usb *usb, byte ep_addr) -{ - return ((ep_addr & 0x80) ? usb->ep_in : usb->ep_out) + (ep_addr & 0x7f); -} -#endif - static void usb_ctl_send_status(struct usb *usb) { + usb_debug("Control send: status\n"); usb->ep0_state = USB_EP0_STATUS_IN; usb_ep_transmit(usb, 0x00, NULL, 0); } @@ -128,6 +127,7 @@ static void usb_ctl_recv_status(struct usb *usb) static void usb_ctl_send_data(struct usb *usb, const byte *data, uint len) { + usb_debug("Control send: %u bytes\n", len); usb->ep0_state = USB_EP0_DATA_IN; usb->ep0_total_length = len; usb->ep0_remaining_length = len; @@ -137,6 +137,7 @@ static void usb_ctl_send_data(struct usb *usb, const byte *data, uint len) #if 0 // FIXME static void usb_ctl_recv_data(struct usb *usb, byte *data, uint len) { + usb_debug("Control recv: %u bytes\n", len); usb->ep0_state = USB_EP0_DATA_OUT; usb->ep0_total_length = len; usb->ep0_remaining_length = len; @@ -158,6 +159,7 @@ static void usb_ctl_send_u16(struct usb *usb, u16 data) static void usb_ctl_error(struct usb *usb) { + usb_debug("Control packet error\n"); usb_ep_stall(usb, 0x00); usb_ep_stall(usb, 0x80); } @@ -172,6 +174,7 @@ struct setup_request { static void usb_ctl_setup_error(struct usb *usb, struct setup_request *setup) { + usb_debug("Setup packet error\n"); usb_ep_stall(usb, setup->bmRequest & USB_REQ_DIRECTION); } @@ -473,6 +476,8 @@ static void ep_setup(struct usb *usb, struct setup_request *setup) static void usb_handle_setup(struct usb *usb, struct setup_request *setup) { + usb_debug("Setup: type=%02x req=%02x val=%04x idx=%04x len=%04x\n", setup->bmRequest, setup->bRequest, setup->wValue, setup->wIndex, setup->wLength); + usb->ep0_state = USB_EP0_SETUP; usb->ep0_setup_data_length = setup->wLength; @@ -506,7 +511,7 @@ void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd) .bRequest = req[1], .wValue = get_u16(req+2), .wIndex = get_u16(req+4), - .wLength = get_u16(req+2), + .wLength = get_u16(req+6), }; usb_handle_setup(usb, &setup); } -- 2.39.2