X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=Src%2Fusb_device.c;h=d9bbefe15b9ccf52fb36ae21cf11a2c6b8525fd1;hb=50cdc6ca4e8e5e3a45ea4c5c211e94e875e322db;hp=cf6223bb11a168d08bb1a0f763bc219925eeccc6;hpb=4e21fb78c9abaf69adc651de506caae734ac0e3b;p=home-hw.git diff --git a/Src/usb_device.c b/Src/usb_device.c index cf6223b..d9bbefe 100644 --- a/Src/usb_device.c +++ b/Src/usb_device.c @@ -84,6 +84,191 @@ USBD_HandleTypeDef hUsbDeviceFS; */ /* USER CODE BEGIN 1 */ +static uint8_t xxx_rx_buf[64]; + +static uint8_t xxx_init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) +{ + USBD_LL_OpenEP(pdev, 0x01, USBD_EP_TYPE_BULK, 64); + pdev->ep_out[1].maxpacket = 64; + + USBD_LL_OpenEP(pdev, 0x81, USBD_EP_TYPE_BULK, 64); + pdev->ep_in[1].maxpacket = 64; + + // USBD_LL_PrepareReceive(pdev, 0x01, xxx_rx_buf, 64); + USBD_LL_Transmit(pdev, 0x81, "brum", 4); + + return USBD_OK; +} + +static uint8_t xxx_deinit(USBD_HandleTypeDef *pdev, uint8_t cfgidx) +{ + USBD_LL_CloseEP(pdev, 0x01); + USBD_LL_CloseEP(pdev, 0x81); + return USBD_OK; +} + +static uint8_t xxx_zero; + +static uint8_t xxx_setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) +{ + switch (req->bmRequest & USB_REQ_TYPE_MASK) + { + case USB_REQ_TYPE_STANDARD: + switch (req->bRequest) + { + case USB_REQ_GET_DESCRIPTOR: + { + USBD_CtlSendData(pdev, NULL, 0); + return USBD_OK; + } + case USB_REQ_GET_INTERFACE: + { + USBD_CtlSendData(pdev, &xxx_zero, 1); + return USBD_OK; + } + case USB_REQ_SET_INTERFACE: + if ((uint8_t) req->wValue == 0) + return USBD_OK; + break; + case USB_REQ_SET_FEATURE: + case USB_REQ_CLEAR_FEATURE: + // Handled by USBD, we are only notified + return USBD_OK; + } + break; + } + + USBD_CtlError(pdev, req); + return USBD_FAIL; +} + +static uint8_t xxx_ep0_rx_ready(USBD_HandleTypeDef *pdev) +{ + return USBD_OK; +} + +static uint8_t xxx_ep0_tx_ready(USBD_HandleTypeDef *pdev) +{ + return USBD_OK; +} + +static uint8_t xxx_data_in(USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + USBD_EndpointTypeDef *pep = &pdev->ep_in[1]; + return USBD_OK; +} + +static uint8_t xxx_data_out(USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + USBD_EndpointTypeDef *pep = &pdev->ep_out[1]; + unsigned int len = USBD_LL_GetRxDataSize(pdev, 0x01); + return USBD_OK; +} + +static uint8_t xxx_sof(USBD_HandleTypeDef *pdev) +{ + return USBD_OK; +} + +static uint8_t xxx_iso_in_incomplete(USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + return USBD_OK; +} + +static uint8_t xxx_iso_out_incomplete(USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + return USBD_OK; +} + +// FIXME: constify +static uint8_t xxx_cfg_desc[] = { + // Configuration descriptor + 9, // bLength + USB_DESC_TYPE_CONFIGURATION, // bDescriptorType + 32, // wTotalLength + 0, + 0x01, // bNumInterfaces + 0x01, // bConfigurationValue + USBD_IDX_CONFIG_STR, // iConfiguration: index of string descriptor + 0xc0, // bmAttributes: bus-powered, supports remote wakeup + 0x32, // Max power: 100 mA + // Interface descriptor + 9, // bLength + USB_DESC_TYPE_INTERFACE, // bDescriptorType + 0x00, // bInterfaceNumber + 0x00, // bAlternateSetting + 0x01, // bNumEndpoints + 0xff, // bInterfaceClass: vendor-defined + 0x00, // bInterfaceSubClass + 0x00, // nInterfaceProtocol + USBD_IDX_INTERFACE_STR, // iInterface: index of string descriptor + // End-point descriptor + 7, // bLength + USB_DESC_TYPE_ENDPOINT, // bDescriptorType + 0x01, // bEndpointAddress + USBD_EP_TYPE_BULK, // bmAttributes + 0x40, 0x00, // wMaxPacketSize + 0x00, // bInterval: unused + // End-point descriptor + 7, // bLength + USB_DESC_TYPE_ENDPOINT, // bDescriptorType + 0x81, // bEndpointAddress + USBD_EP_TYPE_BULK, // bmAttributes + 0x40, 0x00, // wMaxPacketSize + 0x00, // bInterval: unused +}; + +static uint8_t *xxx_get_cfg_desc(uint16_t *len) +{ + *len = sizeof(xxx_cfg_desc); + return xxx_cfg_desc; +} + +// FIXME: constify +static uint8_t xxx_qual_desc[] = { + 10, // bLength + USB_DESC_TYPE_DEVICE_QUALIFIER, // bDescriptorType + 0x00, 0x02, // bcdUSB + 0x00, // bDeviceClass + 0x00, // bDeviceSubClass + 0x00, // bDeviceProtocol + 0x40, // bMaxPacketSize0 + 0x01, // bNumConfigurations + 0x00, // bReserved +}; + +static uint8_t *xxx_get_dev_qual_desc(uint16_t *len) +{ + *len = sizeof(xxx_qual_desc); + return xxx_qual_desc; +} + +static uint8_t *xxx_get_usr_string_desc(USBD_HandleTypeDef *pdev, uint8_t index, uint16_t *len) +{ + *len = 0; + return NULL; +} + +USBD_ClassTypeDef xxx_class = { + xxx_init, + xxx_deinit, + xxx_setup, + xxx_ep0_tx_ready, + xxx_ep0_rx_ready, + xxx_data_in, + xxx_data_out, + xxx_sof, + xxx_iso_in_incomplete, + xxx_iso_out_incomplete, + xxx_get_cfg_desc, + xxx_get_cfg_desc, + xxx_get_cfg_desc, + xxx_get_dev_qual_desc, +#if USBD_SUPPORT_USER_STRING == 1 + xxx_get_usr_string_desc, +#endif +}; + /* USER CODE END 1 */ /** @@ -99,9 +284,13 @@ void MX_USB_DEVICE_Init(void) /* Init Device Library, add supported class and start the library. */ USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS); +#if 0 USBD_RegisterClass(&hUsbDeviceFS, &USBD_DFU); USBD_DFU_RegisterMedia(&hUsbDeviceFS, &USBD_DFU_fops_FS); +#else + USBD_RegisterClass(&hUsbDeviceFS, &xxx_class); +#endif USBD_Start(&hUsbDeviceFS);