]> mj.ucw.cz Git - home-hw.git/commitdiff
Debugging...
authorMartin Mares <mj@ucw.cz>
Sun, 24 Jun 2018 13:00:28 +0000 (15:00 +0200)
committerMartin Mares <mj@ucw.cz>
Sun, 24 Jun 2018 13:00:28 +0000 (15:00 +0200)
Inc/usb.h
Inc/util.h [new file with mode: 0644]
Makefile
Src/debug.c [new file with mode: 0644]
Src/main.c
Src/usb.c

index 1d8291ff61e53a580a8be62fcd66bac1e1d1d86c..e33a71f1442faf532ab1407b531633167ccbe06f 100644 (file)
--- a/Inc/usb.h
+++ b/Inc/usb.h
@@ -1,15 +1,6 @@
 #define USB_SELF_POWERED
 #define USB_NUM_CONFIGURATIONS 1
 #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 ***/
 
 
 /*** USB state structure ***/
 
@@ -39,6 +30,7 @@ struct usb {
 };
 
 void usb_init(struct usb *usb, PCD_HandleTypeDef *hpcd);
 };
 
 void usb_init(struct usb *usb, PCD_HandleTypeDef *hpcd);
+void usb_start(struct usb *usb);
 
 enum usb_device_state {
   USB_STATE_DEFAULT,
 
 enum usb_device_state {
   USB_STATE_DEFAULT,
@@ -57,6 +49,13 @@ enum usb_ep0_state {
   USB_EP0_STALL,
 };
 
   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
 /*** Constants from USB specs ***/
 
 #define USB_REQ_DIRECTION 0x80
diff --git a/Inc/util.h b/Inc/util.h
new file mode 100644 (file)
index 0000000..07a677b
--- /dev/null
@@ -0,0 +1,19 @@
+#include <stdint.h>
+
+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);
index 7033be37c04df1df485351440be9dae11036e9dd..a50db714e8f32dcc8a7fcd35847062e6018c0da2 100644 (file)
--- 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 \
 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 \
 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 (file)
index 0000000..d8b3d63
--- /dev/null
@@ -0,0 +1,167 @@
+#include "util.h"
+#include "stm32f1xx_hal.h"
+
+#include <stdarg.h>
+#include <string.h>
+
+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);
+}
index 9d3dbb8905792a7ba4634cfea9b5e7177b88bb3f..1c016c3eaa25447bd95b23e5a925e0bd986c2710 100644 (file)
@@ -37,6 +37,7 @@
   ******************************************************************************
   */
 /* Includes ------------------------------------------------------------------*/
   ******************************************************************************
   */
 /* Includes ------------------------------------------------------------------*/
+#include "util.h"
 #include "main.h"
 #include "stm32f1xx_hal.h"
 #include "usb.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 */
 
 
 /* USER CODE BEGIN 0 */
 
-#include <stdarg.h>
-#include <stdint.h>
-#include <string.h>
-
-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 */
 
 /**
 /* USER CODE END 0 */
 
 /**
@@ -257,6 +96,7 @@ int main(void)
   SystemClock_Config();
 
   /* USER CODE BEGIN SysInit */
   SystemClock_Config();
 
   /* USER CODE BEGIN SysInit */
+  usb_init(&usb, &hpcd_USB_FS);
 
   /* USER CODE END SysInit */
 
 
   /* USER CODE END SysInit */
 
@@ -266,7 +106,7 @@ int main(void)
   MX_I2C2_Init();
   MX_USB_PCD_Init();
   /* USER CODE BEGIN 2 */
   MX_I2C2_Init();
   MX_USB_PCD_Init();
   /* USER CODE BEGIN 2 */
-  usb_init(&usb, &hpcd_USB_FS);
+  usb_start(&usb);
 
   /* USER CODE END 2 */
 
 
   /* USER CODE END 2 */
 
index 2c366e2c725780ae192e226571570c8d68953ce7..ce0bddc235500e866a149dc937023aedc8698317 100644 (file)
--- a/Src/usb.c
+++ b/Src/usb.c
@@ -1,6 +1,7 @@
 #include "stm32f1xx.h"
 #include "stm32f1xx_hal.h"
 
 #include "stm32f1xx.h"
 #include "stm32f1xx_hal.h"
 
+#include "util.h"
 #include "usb.h"
 
 #include <string.h>
 #include "usb.h"
 
 #include <string.h>
@@ -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;
   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)
 }
 
 static inline uint get_u16(byte *p)
@@ -105,15 +110,9 @@ static inline void put_u16(byte *p, u16 x)
   p[1] = x >> 8;
 }
 
   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)
 {
 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);
 }
   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)
 {
 
 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;
   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)
 {
 #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;
   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)
 {
 
 static void usb_ctl_error(struct usb *usb)
 {
+  usb_debug("Control packet error\n");
   usb_ep_stall(usb, 0x00);
   usb_ep_stall(usb, 0x80);
 }
   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)
 {
 
 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);
 }
 
   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)
 {
 
 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;
 
   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),
     .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);
 }
   };
   usb_handle_setup(usb, &setup);
 }