]> mj.ucw.cz Git - home-hw.git/blobdiff - Src/usb.c
More USB debugging
[home-hw.git] / Src / usb.c
index 2c366e2c725780ae192e226571570c8d68953ce7..b7f731331e89a3b51f7fadb1714db6c80d98d6dc 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>
@@ -51,7 +52,7 @@ static const byte desc_config[] = {
   USB_DESC_TYPE_INTERFACE,     // bDescriptorType
   0x00,                                // bInterfaceNumber
   0x00,                                // bAlternateSetting
-  0x01,                                // bNumEndpoints
+  0x02,                                // bNumEndpoints
   0xff,                                // bInterfaceClass: vendor-defined
   0x00,                                // bInterfaceSubClass
   0x00,                                // nInterfaceProtocol
@@ -73,7 +74,7 @@ static const byte desc_config[] = {
 };
 
 static const char * const desc_string[] = {
-  "",                          // DESC_STR_NONE
+  NULL,                                // DESC_STR_NONE
   "United Computer Wizards",   // DESC_STR_MANUFACTURER
   "Mysterious Gadget",         // DESC_STR_PRODUCT
   "00000042",                  // DESC_STR_SERIAL
@@ -81,17 +82,27 @@ static const char * const desc_string[] = {
   "Default Interface",         // DESC_STR_INTERFACE
 };
 
+static const byte desc_languages[] = {
+  4,                           // bLength
+  USB_DESC_TYPE_STRING,                // bDescriptorType
+  DESC_U16(1033),              // English
+};
+
 void usb_init(struct usb *usb, PCD_HandleTypeDef *hpcd)
 {
   memset(usb, 0, sizeof(*usb));
   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,29 +116,22 @@ 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);
 }
 
-#if 0  // FIXME
 static void usb_ctl_recv_status(struct usb *usb)
 {
   usb->ep0_state = USB_EP0_STATUS_OUT;
   usb_ep_receive(usb, 0x00, NULL, 0);
 }
-#endif
 
 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 +141,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 +163,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 +178,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);
 }
 
@@ -272,6 +279,8 @@ static void dev_get_descriptor(struct usb *usb, struct setup_request *setup)
     case USB_DESC_TYPE_CONFIGURATION:
       return dev_desc_send(usb, setup, desc_config, sizeof(desc_config));
     case USB_DESC_TYPE_STRING:
+      if (!desc_index)
+       return dev_desc_send(usb, setup, desc_languages, sizeof(desc_languages));
       if (desc_index < sizeof(desc_string) / sizeof(desc_string[0]))
        return dev_desc_send_string(usb, setup, desc_string[desc_index]);
       break;
@@ -473,6 +482,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 +517,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);
 }
@@ -519,6 +530,7 @@ void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
   if (!epnum)
     {
       // HAL/LL handle EP0 transfers in a completely different way, we have to do many things ourselves
+      usb_debug("Ep0 OUT: state=%u rem=%u total=%u\n", usb->ep0_state, usb->ep0_remaining_length, usb->ep0_total_length);
       if (usb->ep0_state != USB_EP0_DATA_OUT)
        return;
       if (usb->ep0_remaining_length > ep->maxpacket)
@@ -552,6 +564,7 @@ void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
   if (!epnum)
     {
       // HAL/LL handle EP0 transfers in a completely different way, we have to do many things ourselves
+      usb_debug("Ep0 IN: state=%u rem=%u total=%u want=%u\n", usb->ep0_state, usb->ep0_remaining_length, usb->ep0_total_length, usb->ep0_setup_data_length);
       if (usb->ep0_state != USB_EP0_DATA_IN)
        return;
       if (usb->ep0_remaining_length > ep->maxpacket)
@@ -578,6 +591,7 @@ void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
              // FIXME: Custom data callback
              // All data have been sent
            }
+         usb_ctl_recv_status(usb);
        }
     }
   else