]> mj.ucw.cz Git - home-hw.git/blobdiff - Src/usb.c
Debugging...
[home-hw.git] / Src / usb.c
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 "util.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;
+  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);
 }