diff --git a/ch32v003fun/ch32v003fun.h b/ch32v003fun/ch32v003fun.h index d836f673a8a919de908bdc1e12d5c8892fc3b427..844b74ed3d0261ac134dc444e500f5b6b4eb81e6 100644 --- a/ch32v003fun/ch32v003fun.h +++ b/ch32v003fun/ch32v003fun.h @@ -1435,6 +1435,7 @@ typedef struct __attribute__((packed)) __IO uint8_t RESERVED52; __IO uint16_t HOST_SPLIT_DATA; } USBHSH_TypeDef; + #endif // #if defined(CH32V30x) /* USBFS Registers */ @@ -1775,6 +1776,133 @@ typedef struct #define UDM_PUE_10 (0b10<<0) #define UDM_PUE_11 (0b11<<0) + +#define USBFSD_UEP_MOD_BASE 0x4002340C +#define USBFSD_UEP_DMA_BASE 0x40023410 +#define USBFSD_UEP_LEN_BASE 0x40023420 +#define USBFSD_UEP_CTL_BASE 0x40023422 +#define USBFSD_UEP_RX_EN 0x08 +#define USBFSD_UEP_TX_EN 0x04 +#define USBFSD_UEP_BUF_MOD 0x01 +#define DEF_UEP_DMA_LOAD 0 /* Direct the DMA address to the data to be processed */ +#define DEF_UEP_CPY_LOAD 1 /* Use memcpy to move data to a buffer */ +#define USBFSD_UEP_MOD( N ) (*((volatile uint8_t *)( USBFSD_UEP_MOD_BASE + N ))) +#define USBFSD_UEP_TX_CTRL( N ) (*((volatile uint8_t *)( USBFSD_UEP_CTL_BASE + N * 0x04 ))) +#define USBFSD_UEP_RX_CTRL( N ) (*((volatile uint8_t *)( USBFSD_UEP_CTL_BASE + N * 0x04 ))) +#define USBFSD_UEP_DMA( N ) (*((volatile uint32_t *)( USBFSD_UEP_DMA_BASE + N * 0x04 ))) +#define USBFSD_UEP_BUF( N ) ((uint8_t *)(*((volatile uint32_t *)( USBFSD_UEP_DMA_BASE + N * 0x04 ))) + 0x20000000) +#define USBFSD_UEP_TLEN( N ) (*((volatile uint16_t *)( USBFSD_UEP_LEN_BASE + N * 0x04 ))) + +/* R8_UEPn_TX_CTRL */ +#define USBFS_UEP_T_AUTO_TOG (1<<4) // enable automatic toggle after successful transfer completion on endpoint 1/2/3: 0=manual toggle, 1=automatic toggle +#define USBFS_UEP_T_TOG (1<<6) // prepared data toggle flag of USB endpoint X transmittal (IN): 0=DATA0, 1=DATA1 +#define USBFS_UEP_T_RES_MASK (3<<0) // bit mask of handshake response type for USB endpoint X transmittal (IN) +#define USBFS_UEP_T_RES_ACK (0<<1) +#define USBFS_UEP_T_RES_NONE (1<<0) +#define USBFS_UEP_T_RES_NAK (1<<1) +#define USBFS_UEP_T_RES_STALL (3<<0) +// bUEP_T_RES1 & bUEP_T_RES0: handshake response type for USB endpoint X transmittal (IN) +// 00: DATA0 or DATA1 then expecting ACK (ready) +// 01: DATA0 or DATA1 then expecting no response, time out from host, for non-zero endpoint isochronous transactions +// 10: NAK (busy) +// 11: STALL (error) +// host aux setup + +/* R8_UEPn_RX_CTRL, n=0-7 */ +#define USBFS_UEP_R_AUTO_TOG (1<<4) // enable automatic toggle after successful transfer completion on endpoint 1/2/3: 0=manual toggle, 1=automatic toggle +#define USBFS_UEP_R_TOG (1<<7) // expected data toggle flag of USB endpoint X receiving (OUT): 0=DATA0, 1=DATA1 +#define USBFS_UEP_R_RES_MASK (3<<2) // bit mask of handshake response type for USB endpoint X receiving (OUT) +#define USBFS_UEP_R_RES_ACK (0<<3) +#define USBFS_UEP_R_RES_NONE (1<<2) +#define USBFS_UEP_R_RES_NAK (1<<3) +#define USBFS_UEP_R_RES_STALL (3<<2) + + +#define EP1_T_EN (1<<6) +#define EP2_T_EN (1<<2) +#define EP3_T_EN (1<<6) +#define EP4_T_EN (1<<2) +#define EP1_R_EN (1<<7) +#define EP2_R_EN (1<<3) +#define EP3_R_EN (1<<7) +#define EP4_R_EN (1<<3) + + +/* R8_USB_CTRL */ +#define USBFS_UC_HOST_MODE 0x80 +#define USBFS_UC_LOW_SPEED 0x40 +#define USBFS_UC_DEV_PU_EN 0x20 +#define USBFS_UC_SYS_CTRL_MASK 0x30 +#define USBFS_UC_SYS_CTRL0 0x00 +#define USBFS_UC_SYS_CTRL1 0x10 +#define USBFS_UC_SYS_CTRL2 0x20 +#define USBFS_UC_SYS_CTRL3 0x30 +#define USBFS_UC_INT_BUSY 0x08 +#define USBFS_UC_RESET_SIE 0x04 +#define USBFS_UC_CLR_ALL 0x02 +#define USBFS_UC_DMA_EN 0x01 + +/* R8_USB_INT_EN */ +#define USBFS_UIE_DEV_SOF 0x80 +#define USBFS_UIE_DEV_NAK 0x40 +#define USBFS_UIE_FIFO_OV 0x10 +#define USBFS_UIE_HST_SOF 0x08 +#define USBFS_UIE_SUSPEND 0x04 +#define USBFS_UIE_TRANSFER 0x02 +#define USBFS_UIE_DETECT 0x01 +#define USBFS_UIE_BUS_RST 0x01 + +/* R8_USB_DEV_AD */ +#define USBFS_UDA_GP_BIT 0x80 +#define USBFS_USB_ADDR_MASK 0x7F + +/* R8_USB_MIS_ST */ +#define USBFS_UMS_SOF_PRES 0x80 +#define USBFS_UMS_SOF_ACT 0x40 +#define USBFS_UMS_SIE_FREE 0x20 +#define USBFS_UMS_R_FIFO_RDY 0x10 +#define USBFS_UMS_BUS_RESET 0x08 +#define USBFS_UMS_SUSPEND 0x04 +#define USBFS_UMS_DM_LEVEL 0x02 +#define USBFS_UMS_DEV_ATTACH 0x01 + + + + +#define USBFS_UDA_GP_BIT 0x80 +#define USBFS_USB_ADDR_MASK 0x7F + +#define DEF_USBD_UEP0_SIZE 64 /* usb hs/fs device end-point 0 size */ +#define UEP_SIZE 64 + +#define DEF_UEP_IN 0x80 +#define DEF_UEP_OUT 0x00 +#define DEF_UEP_BUSY 0x01 +#define DEF_UEP_FREE 0x00 + +#define DEF_UEP0 0 +#define DEF_UEP1 1 +#define DEF_UEP2 2 +#define DEF_UEP3 3 +#define DEF_UEP4 4 +#define DEF_UEP5 5 +#define DEF_UEP6 6 +#define DEF_UEP7 7 +#define UNUM_EP 8 + + +#define UDP_PUE_MASK 0x0000000C +#define UDP_PUE_DISABLE 0x00000000 +#define UDP_PUE_35UA 0x00000004 +#define UDP_PUE_10K 0x00000008 +#define UDP_PUE_1K5 0x0000000C + +#define UDM_PUE_MASK 0x00000003 +#define UDM_PUE_DISABLE 0x00000000 +#define UDM_PUE_35UA 0x00000001 +#define UDM_PUE_10K 0x00000002 +#define UDM_PUE_1K5 0x00000003 + /* USB Host Mode */ typedef struct diff --git a/examples_x035/usbdevice.incomplete/Makefile b/examples_x035/usbdevice.incomplete/Makefile index adabf99a779fb5e00e982b4db7f26cec3654269b..e28de11d4d8c5b2e5246370353a8a4809f881311 100644 --- a/examples_x035/usbdevice.incomplete/Makefile +++ b/examples_x035/usbdevice.incomplete/Makefile @@ -2,7 +2,7 @@ all : flash TARGET:=usbdevice TARGET_MCU:=CH32X035 -ADDITIONAL_C_FILES:=fsusb.c descriptor.c +ADDITIONAL_C_FILES:=fsusb.c include ../../ch32v003fun/ch32v003fun.mk diff --git a/examples_x035/usbdevice.incomplete/descriptor.c b/examples_x035/usbdevice.incomplete/descriptor.c deleted file mode 100644 index 2d1b2f6d84fc1e3af41d79bd0c7bacfa65d0a47e..0000000000000000000000000000000000000000 --- a/examples_x035/usbdevice.incomplete/descriptor.c +++ /dev/null @@ -1,266 +0,0 @@ -/********************************** (C) COPYRIGHT ******************************* - * File Name : composite_km_desc.h - * Author : WCH - * Version : V1.0.0 - * Date : 2023/04/06 - * Description : All descriptors for the keyboard and mouse composite device. -********************************************************************************* -* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. -* Attention: This software (modified or not) and binary are used for -* microcontroller manufactured by Nanjing Qinheng Microelectronics. -*******************************************************************************/ - - -/*******************************************************************************/ -/* Header File */ -#include "descriptor.h" - -/*******************************************************************************/ -/* Device Descriptor */ -const uint8_t MyDevDescr[ ] = -{ - 0x12, // bLength - 0x01, // bDescriptorType - 0x00, 0x02, // bcdUSB - 0x00, // bDeviceClass - 0x00, // bDeviceSubClass - 0x00, // bDeviceProtocol - DEF_USBD_UEP0_SIZE, // bMaxPacketSize0 - (uint8_t)DEF_USB_VID, (uint8_t)( DEF_USB_VID >> 8 ), // idVendor - (uint8_t)DEF_USB_PID, (uint8_t)( DEF_USB_PID >> 8 ), // idProduct - 0x00, DEF_IC_PRG_VER, // bcdDevice - 0x01, // iManufacturer - 0x02, // iProduct - 0x03, // iSerialNumber - 0x01, // bNumConfigurations -}; - -/* Configuration Descriptor Set */ -const uint8_t MyCfgDescr[ ] = -{ - /* Configuration Descriptor */ - 0x09, // bLength - 0x02, // bDescriptorType - 0x3B, 0x00, // wTotalLength - 0x02, // bNumInterfaces - 0x01, // bConfigurationValue - 0x00, // iConfiguration - 0xA0, // bmAttributes: Bus Powered; Remote Wakeup - 0x32, // MaxPower: 100mA - - /* Interface Descriptor (Keyboard) */ - 0x09, // bLength - 0x04, // bDescriptorType - 0x00, // bInterfaceNumber - 0x00, // bAlternateSetting - 0x01, // bNumEndpoints - 0x03, // bInterfaceClass - 0x01, // bInterfaceSubClass - 0x01, // bInterfaceProtocol: Keyboard - 0x00, // iInterface - - /* HID Descriptor (Keyboard) */ - 0x09, // bLength - 0x21, // bDescriptorType - 0x11, 0x01, // bcdHID - 0x00, // bCountryCode - 0x01, // bNumDescriptors - 0x22, // bDescriptorType - 0x3E, 0x00, // wDescriptorLength - - /* Endpoint Descriptor (Keyboard) */ - 0x07, // bLength - 0x05, // bDescriptorType - 0x81, // bEndpointAddress: IN Endpoint 1 - 0x03, // bmAttributes - 0x08, 0x00, // wMaxPacketSize - 0x0A, // bInterval: 10mS - - /* Interface Descriptor (Mouse) */ - 0x09, // bLength - 0x04, // bDescriptorType - 0x01, // bInterfaceNumber - 0x00, // bAlternateSetting - 0x01, // bNumEndpoints - 0x03, // bInterfaceClass - 0x01, // bInterfaceSubClass - 0x02, // bInterfaceProtocol: Mouse - 0x00, // iInterface - - /* HID Descriptor (Mouse) */ - 0x09, // bLength - 0x21, // bDescriptorType - 0x10, 0x01, // bcdHID - 0x00, // bCountryCode - 0x01, // bNumDescriptors - 0x22, // bDescriptorType - 0x34, 0x00, // wDescriptorLength - - /* Endpoint Descriptor (Mouse) */ - 0x07, // bLength - 0x05, // bDescriptorType - 0x82, // bEndpointAddress: IN Endpoint 2 - 0x03, // bmAttributes - 0x08, 0x00, // wMaxPacketSize - 0x01 // bInterval: 1mS -}; - -/* Keyboard Report Descriptor */ -const uint8_t KeyRepDesc[ ] = -{ - 0x05, 0x01, // Usage Page (Generic Desktop) - 0x09, 0x06, // Usage (Keyboard) - 0xA1, 0x01, // Collection (Application) - 0x05, 0x07, // Usage Page (Key Codes) - 0x19, 0xE0, // Usage Minimum (224) - 0x29, 0xE7, // Usage Maximum (231) - 0x15, 0x00, // Logical Minimum (0) - 0x25, 0x01, // Logical Maximum (1) - 0x75, 0x01, // Report Size (1) - 0x95, 0x08, // Report Count (8) - 0x81, 0x02, // Input (Data,Variable,Absolute) - 0x95, 0x01, // Report Count (1) - 0x75, 0x08, // Report Size (8) - 0x81, 0x01, // Input (Constant) - 0x95, 0x03, // Report Count (3) - 0x75, 0x01, // Report Size (1) - 0x05, 0x08, // Usage Page (LEDs) - 0x19, 0x01, // Usage Minimum (1) - 0x29, 0x03, // Usage Maximum (3) - 0x91, 0x02, // Output (Data,Variable,Absolute) - 0x95, 0x05, // Report Count (5) - 0x75, 0x01, // Report Size (1) - 0x91, 0x01, // Output (Constant,Array,Absolute) - 0x95, 0x06, // Report Count (6) - 0x75, 0x08, // Report Size (8) - 0x26, 0xFF, 0x00, // Logical Maximum (255) - 0x05, 0x07, // Usage Page (Key Codes) - 0x19, 0x00, // Usage Minimum (0) - 0x29, 0x91, // Usage Maximum (145) - 0x81, 0x00, // Input(Data,Array,Absolute) - 0xC0 // End Collection -}; - -/* Mouse Report Descriptor */ -const uint8_t MouseRepDesc[ ] = -{ - 0x05, 0x01, // Usage Page (Generic Desktop) - 0x09, 0x02, // Usage (Mouse) - 0xA1, 0x01, // Collection (Application) - 0x09, 0x01, // Usage (Pointer) - 0xA1, 0x00, // Collection (Physical) - 0x05, 0x09, // Usage Page (Button) - 0x19, 0x01, // Usage Minimum (Button 1) - 0x29, 0x03, // Usage Maximum (Button 3) - 0x15, 0x00, // Logical Minimum (0) - 0x25, 0x01, // Logical Maximum (1) - 0x75, 0x01, // Report Size (1) - 0x95, 0x03, // Report Count (3) - 0x81, 0x02, // Input (Data,Variable,Absolute) - 0x75, 0x05, // Report Size (5) - 0x95, 0x01, // Report Count (1) - 0x81, 0x01, // Input (Constant,Array,Absolute) - 0x05, 0x01, // Usage Page (Generic Desktop) - 0x09, 0x30, // Usage (X) - 0x09, 0x31, // Usage (Y) - 0x09, 0x38, // Usage (Wheel) - 0x15, 0x81, // Logical Minimum (-127) - 0x25, 0x7F, // Logical Maximum (127) - 0x75, 0x08, // Report Size (8) - 0x95, 0x03, // Report Count (3) - 0x81, 0x06, // Input (Data,Variable,Relative) - 0xC0, // End Collection - 0xC0 // End Collection -}; - -/* Qualifier Descriptor */ -const uint8_t MyQuaDesc[ ] = -{ - 0x0A, // bLength - 0x06, // bDescriptorType - 0x00, 0x02, // bcdUSB - 0x00, // bDeviceClass - 0x00, // bDeviceSubClass - 0x00, // bDeviceProtocol - 0x40, // bMaxPacketSize0 - 0x00, // bNumConfigurations - 0x00 // bReserved -}; - -/* Language Descriptor */ -const uint8_t MyLangDescr[ ] = -{ - 0x04, - 0x03, - 0x09, - 0x04 -}; - -/* Manufacturer Descriptor */ -const uint8_t MyManuInfo[ ] = -{ - 0x0E, - 0x03, - 'w', - 0, - 'c', - 0, - 'h', - 0, - '.', - 0, - 'c', - 0, - 'n', - 0 -}; - -/* Product Information */ -const uint8_t MyProdInfo[ ] = -{ - 0x12, - 0x03, - 'C', - 0, - 'H', - 0, - '3', - 0, - '2', - 0, - 'x', - 0, - '0', - 0, - '3', - 0, - '5', - 0 -}; - -/* Serial Number Information */ -const uint8_t MySerNumInfo[ ] = -{ - 0x16, - 0x03, - '0', - 0, - '1', - 0, - '2', - 0, - '3', - 0, - '4', - 0, - '5', - 0, - '6', - 0, - '7', - 0, - '8', - 0, - '9', - 0 -}; diff --git a/examples_x035/usbdevice.incomplete/descriptor.h b/examples_x035/usbdevice.incomplete/descriptor.h deleted file mode 100644 index b746eecf181c3d60b5038245638a58163145e811..0000000000000000000000000000000000000000 --- a/examples_x035/usbdevice.incomplete/descriptor.h +++ /dev/null @@ -1,92 +0,0 @@ -/********************************** (C) COPYRIGHT ******************************* - * File Name : usbd_descriptor.h - * Author : WCH - * Version : V1.0.0 - * Date : 2023/04/06 - * Description : All descriptors for the keyboard and mouse composite device. -********************************************************************************* -* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. -* Attention: This software (modified or not) and binary are used for -* microcontroller manufactured by Nanjing Qinheng Microelectronics. -*******************************************************************************/ - - -#ifndef __USBD_DESC_H -#define __USBD_DESC_H - -/*******************************************************************************/ -/* Header File */ -#include "stdint.h" - -/*******************************************************************************/ -/* Macro Definition */ - -/* File Version */ -#define DEF_FILE_VERSION 0x01 - -/* USB Device Info */ -#define DEF_USB_VID 0x1A86 -#define DEF_USB_PID 0xFE00 - -/* USB Device Descriptor, Device Serial Number(bcdDevice) */ -#define DEF_IC_PRG_VER DEF_FILE_VERSION - -/* USB Device Endpoint Size */ -#define DEF_USBD_UEP0_SIZE 64 /* usb hs/fs device end-point 0 size */ -/* HS */ -#define DEF_USBD_HS_PACK_SIZE 512 /* usb hs device max bluk/int pack size */ -#define DEF_USBD_HS_ISO_PACK_SIZE 1024 /* usb hs device max iso pack size */ -/* FS */ -#define DEF_USBD_FS_PACK_SIZE 64 /* usb fs device max bluk/int pack size */ -#define DEF_USBD_FS_ISO_PACK_SIZE 1023 /* usb fs device max iso pack size */ -/* LS */ -#define DEf_USBD_LS_UEP0_SIZE 8 /* usb ls device end-point 0 size */ -#define DEF_USBD_LS_PACK_SIZE 64 /* usb ls device max int pack size */ - -/* USB Device Endpoint1-6 Size */ -/* HS */ -#define DEF_USB_EP1_HS_SIZE DEF_USBD_HS_PACK_SIZE -#define DEF_USB_EP2_HS_SIZE DEF_USBD_HS_PACK_SIZE -#define DEF_USB_EP3_HS_SIZE DEF_USBD_HS_PACK_SIZE -#define DEF_USB_EP4_HS_SIZE DEF_USBD_HS_PACK_SIZE -#define DEF_USB_EP5_HS_SIZE DEF_USBD_HS_PACK_SIZE -#define DEF_USB_EP6_HS_SIZE DEF_USBD_HS_PACK_SIZE -/* FS */ -#define DEF_USB_EP1_FS_SIZE DEF_USBD_FS_PACK_SIZE -#define DEF_USB_EP2_FS_SIZE DEF_USBD_FS_PACK_SIZE -#define DEF_USB_EP3_FS_SIZE DEF_USBD_FS_PACK_SIZE -#define DEF_USB_EP4_FS_SIZE DEF_USBD_FS_PACK_SIZE -#define DEF_USB_EP5_FS_SIZE DEF_USBD_FS_PACK_SIZE -#define DEF_USB_EP6_FS_SIZE DEF_USBD_FS_PACK_SIZE -/* LS */ -/* ... */ - -/* USB Device Descriptor Length */ -/* Note: If a descriptor does not exist, set the length to 0. */ -#define DEF_USBD_DEVICE_DESC_LEN ( (uint16_t)MyDevDescr[ 0 ] ) -#define DEF_USBD_CONFIG_DESC_LEN ( (uint16_t)MyCfgDescr[ 2 ] + ( (uint16_t)MyCfgDescr[ 3 ] << 8 ) ) -#define DEF_USBD_REPORT_DESC_LEN_KB 0x3E -#define DEF_USBD_REPORT_DESC_LEN_MS 0x34 -#define DEF_USBD_LANG_DESC_LEN ( (uint16_t)MyLangDescr[ 0 ] ) -#define DEF_USBD_MANU_DESC_LEN ( (uint16_t)MyManuInfo[ 0 ] ) -#define DEF_USBD_PROD_DESC_LEN ( (uint16_t)MyProdInfo[ 0 ] ) -#define DEF_USBD_SN_DESC_LEN ( (uint16_t)MySerNumInfo[ 0 ] ) -#define DEF_USBD_QUALFY_DESC_LEN ( (uint16_t)MyQuaDesc[ 0 ]) -#define DEF_USBD_BOS_DESC_LEN 0 -#define DEF_USBD_FS_OTH_DESC_LEN 0 -#define DEF_USBD_HS_OTH_DESC_LEN 0 - - -/*******************************************************************************/ -/* Descriptor Declaration */ -extern const uint8_t MyDevDescr[ ]; -extern const uint8_t MyCfgDescr[ ]; -extern const uint8_t KeyRepDesc[ ]; -extern const uint8_t MouseRepDesc[ ]; -extern const uint8_t MyQuaDesc[ ]; -extern const uint8_t MyLangDescr[ ]; -extern const uint8_t MyManuInfo[ ]; -extern const uint8_t MyProdInfo[ ]; -extern const uint8_t MySerNumInfo[ ]; - -#endif diff --git a/examples_x035/usbdevice.incomplete/fsusb.c b/examples_x035/usbdevice.incomplete/fsusb.c index ea8d03ba0de8119e15c980278bd2d326fd28b828..319018c4052602275613b15592a7a457ced4a6b3 100644 --- a/examples_x035/usbdevice.incomplete/fsusb.c +++ b/examples_x035/usbdevice.incomplete/fsusb.c @@ -5,6 +5,9 @@ uint32_t USBDEBUG0, USBDEBUG1, USBDEBUG2; + +struct _USBState FSUSBCTX; + // based on https://github.com/openwch/ch32x035/tree/main/EVT/EXAM/USB/USBFS/DEVICE/CompositeKM/User // Mask for the combined USBFSD->INT_FG + USBFSD->INT_ST @@ -16,94 +19,20 @@ uint32_t USBDEBUG0, USBDEBUG1, USBDEBUG2; #define CRB_UIF_SUSPEND (1<<2) #define CRB_UIF_TRANSFER (1<<1) #define CRB_UIF_BUS_RST (1<<0) -#define CSETUP_ACT (1<<15) +#define CSETUP_ACT (1<<15) #define CRB_UIS_TOG_OK (1<<14) #define CMASK_UIS_TOKEN (3<<12) #define CMASK_UIS_ENDP (0xf<<8) -#define CUIS_TOKEN_OUT 0x0 -#define CUIS_TOKEN_SOF 0x1 -#define CUIS_TOKEN_IN 0x2 -#define CUIS_TOKEN_SETUP 0x3 - - -// Hardware specific - -/* R8_USB_CTRL */ -#define USBFS_UC_HOST_MODE 0x80 -#define USBFS_UC_LOW_SPEED 0x40 -#define USBFS_UC_DEV_PU_EN 0x20 -#define USBFS_UC_SYS_CTRL_MASK 0x30 -#define USBFS_UC_SYS_CTRL0 0x00 -#define USBFS_UC_SYS_CTRL1 0x10 -#define USBFS_UC_SYS_CTRL2 0x20 -#define USBFS_UC_SYS_CTRL3 0x30 -#define USBFS_UC_INT_BUSY 0x08 -#define USBFS_UC_RESET_SIE 0x04 -#define USBFS_UC_CLR_ALL 0x02 -#define USBFS_UC_DMA_EN 0x01 - -/* R8_USB_INT_EN */ -#define USBFS_UIE_DEV_SOF 0x80 -#define USBFS_UIE_DEV_NAK 0x40 -#define USBFS_UIE_FIFO_OV 0x10 -#define USBFS_UIE_HST_SOF 0x08 -#define USBFS_UIE_SUSPEND 0x04 -#define USBFS_UIE_TRANSFER 0x02 -#define USBFS_UIE_DETECT 0x01 -#define USBFS_UIE_BUS_RST 0x01 - -/* R8_USB_DEV_AD */ -#define USBFS_UDA_GP_BIT 0x80 -#define USBFS_USB_ADDR_MASK 0x7F - -/* R8_USB_MIS_ST */ -#define USBFS_UMS_SOF_PRES 0x80 -#define USBFS_UMS_SOF_ACT 0x40 -#define USBFS_UMS_SIE_FREE 0x20 -#define USBFS_UMS_R_FIFO_RDY 0x10 -#define USBFS_UMS_BUS_RESET 0x08 -#define USBFS_UMS_SUSPEND 0x04 -#define USBFS_UMS_DM_LEVEL 0x02 -#define USBFS_UMS_DEV_ATTACH 0x01 - -// XXX TODO: Put these in a struct. - -/* Setup Request */ -volatile uint8_t USBFS_SetupReqCode; -volatile uint8_t USBFS_SetupReqType; -volatile uint16_t USBFS_SetupReqValue; -volatile uint16_t USBFS_SetupReqIndex; -volatile uint16_t USBFS_SetupReqLen; - -/* USB Device Status */ -volatile uint8_t USBFS_DevConfig; -volatile uint8_t USBFS_DevAddr; -volatile uint8_t USBFS_DevSleepStatus; -volatile uint8_t USBFS_DevEnumStatus; - -/* Endpoint Buffer */ -__attribute__ ((aligned(4))) uint8_t USBFS_EP0_Buf[64]; -__attribute__ ((aligned(4))) uint8_t USBFS_EP1_Buf[64]; -__attribute__ ((aligned(4))) uint8_t USBFS_EP2_Buf[64]; -#define pUSBFS_SetupReqPak ((PUSB_SETUP_REQ)USBFS_EP0_Buf) - - -int USBFS_HidIdle[2]; -int USBFS_HidProtocol[2]; - -const uint8_t *pUSBFS_Descr; - -/* USB IN Endpoint Busy Flag */ -volatile uint8_t USBFS_Endp_Busy[NUM_EP]; - - +#define CUIS_TOKEN_OUT 0x0 +#define CUIS_TOKEN_SOF 0x1 +#define CUIS_TOKEN_IN 0x2 +#define CUIS_TOKEN_SETUP 0x3 void USBFS_IRQHandler() __attribute__((section(".text.vector_handler"))) __attribute__((interrupt)); - void USBFS_IRQHandler() { // Based on https://github.com/openwch/ch32x035/blob/main/EVT/EXAM/USB/USBFS/DEVICE/CompositeKM/User/ch32x035_usbfs_device.c @@ -111,7 +40,7 @@ void USBFS_IRQHandler() // Combined FG + ST flag. uint16_t intfgst = *(uint16_t*)(&USBFS->INT_FG); int len = 0, errflag = 0; - + struct _USBState * ctx = &FSUSBCTX; GPIOA->BSHR = 1; // TODO: Check if needs to be do-while to re-check. @@ -120,9 +49,6 @@ void USBFS_IRQHandler() int token = ( intfgst & CMASK_UIS_TOKEN) >> 12; int ep = ( intfgst & CMASK_UIS_ENDP ) >> 8; - if( ep == 2 ) - USBDEBUG1++; - switch ( token ) { case CUIS_TOKEN_IN: @@ -130,30 +56,30 @@ void USBFS_IRQHandler() { /* end-point 0 data in interrupt */ case DEF_UEP0: - if( USBFS_SetupReqLen == 0 ) + if( ctx->USBFS_SetupReqLen == 0 ) { USBFS->UEP0_CTRL_H = USBFS_UEP_R_TOG | USBFS_UEP_R_RES_ACK; } - if ( ( USBFS_SetupReqType & USB_REQ_TYP_MASK ) != USB_REQ_TYP_STANDARD ) + if ( ( ctx->USBFS_SetupReqType & USB_REQ_TYP_MASK ) != USB_REQ_TYP_STANDARD ) { /* Non-standard request endpoint 0 Data upload, noted by official docs, but I don't think this would ever really be used. */ } else { - switch( USBFS_SetupReqCode ) + switch( FSUSBCTX.USBFS_SetupReqCode ) { case USB_GET_DESCRIPTOR: - len = USBFS_SetupReqLen >= DEF_USBD_UEP0_SIZE ? DEF_USBD_UEP0_SIZE : USBFS_SetupReqLen; - memcpy( USBFS_EP0_Buf, pUSBFS_Descr, len ); - USBFS_SetupReqLen -= len; - pUSBFS_Descr += len; + len = ctx->USBFS_SetupReqLen >= DEF_USBD_UEP0_SIZE ? DEF_USBD_UEP0_SIZE : ctx->USBFS_SetupReqLen; + memcpy( ctx->USBFS_EP0_Buf, ctx->pUSBFS_Descr, len ); + ctx->USBFS_SetupReqLen -= len; + ctx->pUSBFS_Descr += len; USBFS->UEP0_TX_LEN = len; USBFS->UEP0_CTRL_H ^= USBFS_UEP_T_TOG; break; case USB_SET_ADDRESS: - USBFS->DEV_ADDR = ( USBFS->DEV_ADDR & USBFS_UDA_GP_BIT ) | USBFS_DevAddr; + USBFS->DEV_ADDR = ( USBFS->DEV_ADDR & USBFS_UDA_GP_BIT ) | ctx->USBFS_DevAddr; break; default: @@ -166,16 +92,14 @@ void USBFS_IRQHandler() case DEF_UEP1: USBFS->UEP1_CTRL_H = ( USBFS->UEP1_CTRL_H & ~USBFS_UEP_T_RES_MASK ) | USBFS_UEP_T_RES_NAK; USBFS->UEP1_CTRL_H ^= USBFS_UEP_T_TOG; - USBFS_Endp_Busy[ DEF_UEP1 ] = 0; - USBDEBUG0++; + ctx->USBFS_Endp_Busy[ DEF_UEP1 ] = 0; break; /* end-point 2 data in interrupt */ case DEF_UEP2: USBFS->UEP2_CTRL_H = ( USBFS->UEP2_CTRL_H & ~USBFS_UEP_T_RES_MASK ) | USBFS_UEP_T_RES_NAK; USBFS->UEP2_CTRL_H ^= USBFS_UEP_T_TOG; - USBFS_Endp_Busy[ DEF_UEP2 ] = 0; - USBDEBUG0++; + ctx->USBFS_Endp_Busy[ DEF_UEP2 ] = 0; break; default : @@ -191,16 +115,16 @@ void USBFS_IRQHandler() case DEF_UEP0: if( intfgst & CRB_UIS_TOG_OK ) { - if( ( USBFS_SetupReqType & USB_REQ_TYP_MASK ) != USB_REQ_TYP_STANDARD ) + if( ( FSUSBCTX.USBFS_SetupReqType & USB_REQ_TYP_MASK ) != USB_REQ_TYP_STANDARD ) { - if( ( USBFS_SetupReqType & USB_REQ_TYP_MASK ) == USB_REQ_TYP_CLASS ) + if( ( FSUSBCTX.USBFS_SetupReqType & USB_REQ_TYP_MASK ) == USB_REQ_TYP_CLASS ) { - switch( USBFS_SetupReqCode ) + switch( FSUSBCTX.USBFS_SetupReqCode ) { case HID_SET_REPORT: //KB_LED_Cur_Status = USBFS_EP0_Buf[ 0 ]; // Do stuff... - USBFS_SetupReqLen = 0; + FSUSBCTX.USBFS_SetupReqLen = 0; break; default: break; @@ -213,7 +137,7 @@ void USBFS_IRQHandler() /* Add your code here */ } } - if( USBFS_SetupReqLen == 0 ) + if( FSUSBCTX.USBFS_SetupReqLen == 0 ) { USBFS->UEP0_TX_LEN = 0; USBFS->UEP0_CTRL_H = USBFS_UEP_T_TOG | USBFS_UEP_T_RES_ACK; @@ -230,11 +154,11 @@ void USBFS_IRQHandler() USBFS->UEP0_CTRL_H = USBFS_UEP_T_TOG|USBFS_UEP_T_RES_NAK|USBFS_UEP_R_TOG|USBFS_UEP_R_RES_NAK; /* Store All Setup Values */ - USBFS_SetupReqType = pUSBFS_SetupReqPak->bRequestType; - USBFS_SetupReqCode = pUSBFS_SetupReqPak->bRequest; - USBFS_SetupReqLen = pUSBFS_SetupReqPak->wLength; - USBFS_SetupReqValue = pUSBFS_SetupReqPak->wValue; - USBFS_SetupReqIndex = pUSBFS_SetupReqPak->wIndex; + int USBFS_SetupReqType = FSUSBCTX.USBFS_SetupReqType = pUSBFS_SetupReqPak->bmRequestType; + int USBFS_SetupReqCode = FSUSBCTX.USBFS_SetupReqCode = pUSBFS_SetupReqPak->bRequest; + int USBFS_SetupReqLen = FSUSBCTX.USBFS_SetupReqLen = pUSBFS_SetupReqPak->wLength; + int USBFS_SetupReqIndex = pUSBFS_SetupReqPak->wIndex; + int USBFS_IndexValue = FSUSBCTX.USBFS_IndexValue = ( pUSBFS_SetupReqPak->wIndex << 16 ) | pUSBFS_SetupReqPak->wValue; len = 0; errflag = 0; @@ -250,13 +174,13 @@ void USBFS_IRQHandler() case HID_SET_IDLE: if( USBFS_SetupReqIndex < 0x02 ) - USBFS_HidIdle[ USBFS_SetupReqIndex ] = (uint8_t)( USBFS_SetupReqValue >> 8 ); + FSUSBCTX.USBFS_HidIdle[ USBFS_SetupReqIndex ] = (uint8_t)( USBFS_IndexValue >> 8 ); else errflag = 0xFF; break; case HID_SET_PROTOCOL: if ( USBFS_SetupReqIndex < 0x02 ) - USBFS_HidProtocol[USBFS_SetupReqIndex] = (uint8_t)USBFS_SetupReqValue; + FSUSBCTX.USBFS_HidProtocol[USBFS_SetupReqIndex] = (uint8_t)USBFS_IndexValue; else errflag = 0xFF; break; @@ -264,7 +188,7 @@ void USBFS_IRQHandler() case HID_GET_IDLE: if( USBFS_SetupReqIndex < 0x02 ) { - USBFS_EP0_Buf[ 0 ] = USBFS_HidIdle[ USBFS_SetupReqIndex ]; + FSUSBCTX.USBFS_EP0_Buf[ 0 ] = FSUSBCTX.USBFS_HidIdle[ USBFS_SetupReqIndex ]; len = 1; } else @@ -274,7 +198,7 @@ void USBFS_IRQHandler() case HID_GET_PROTOCOL: if( USBFS_SetupReqIndex < 0x02 ) { - USBFS_EP0_Buf[ 0 ] = USBFS_HidProtocol[ USBFS_SetupReqIndex ]; + FSUSBCTX.USBFS_EP0_Buf[ 0 ] = FSUSBCTX.USBFS_HidProtocol[ USBFS_SetupReqIndex ]; len = 1; } else @@ -294,93 +218,22 @@ void USBFS_IRQHandler() { /* get device/configuration/string/report/... descriptors */ case USB_GET_DESCRIPTOR: - switch( (uint8_t)( USBFS_SetupReqValue >> 8 ) ) + { + int match = USBFS_IndexValue; + struct descriptor_list_struct * e = descriptor_list; + struct descriptor_list_struct * e_end = e + DESCRIPTOR_LIST_ENTRIES; + for( ; e != e_end; e++ ) { - /* get usb device descriptor */ - case USB_DESCR_TYP_DEVICE: - pUSBFS_Descr = MyDevDescr; - len = DEF_USBD_DEVICE_DESC_LEN; - break; - - /* get usb configuration descriptor */ - case USB_DESCR_TYP_CONFIG: - pUSBFS_Descr = MyCfgDescr; - len = DEF_USBD_CONFIG_DESC_LEN; - break; - - /* get usb hid descriptor */ - case USB_DESCR_TYP_HID: - if( USBFS_SetupReqIndex == 0x00 ) - { - pUSBFS_Descr = &MyCfgDescr[ 18 ]; - len = 9; - } - else if( USBFS_SetupReqIndex == 0x01 ) - { - pUSBFS_Descr = &MyCfgDescr[ 43 ]; - len = 9; - } - else - { - errflag = 0xFF; - } - break; - - /* get usb report descriptor */ - case USB_DESCR_TYP_REPORT: - if( USBFS_SetupReqIndex == 0x00 ) - { - pUSBFS_Descr = KeyRepDesc; - len = DEF_USBD_REPORT_DESC_LEN_KB; - } - else if( USBFS_SetupReqIndex == 0x01 ) - { - pUSBFS_Descr = MouseRepDesc; - len = DEF_USBD_REPORT_DESC_LEN_MS; - } - else - { - errflag = 0xFF; - } - break; - - /* get usb string descriptor */ - case USB_DESCR_TYP_STRING: - switch( (uint8_t)( USBFS_SetupReqValue & 0xFF ) ) - { - /* Descriptor 0, Language descriptor */ - case DEF_STRING_DESC_LANG: - pUSBFS_Descr = MyLangDescr; - len = DEF_USBD_LANG_DESC_LEN; - break; - - /* Descriptor 1, Manufacturers String descriptor */ - case DEF_STRING_DESC_MANU: - pUSBFS_Descr = MyManuInfo; - len = DEF_USBD_MANU_DESC_LEN; - break; - - /* Descriptor 2, Product String descriptor */ - case DEF_STRING_DESC_PROD: - pUSBFS_Descr = MyProdInfo; - len = DEF_USBD_PROD_DESC_LEN; - break; - - /* Descriptor 3, Serial-number String descriptor */ - case DEF_STRING_DESC_SERN: - pUSBFS_Descr = MySerNumInfo; - len = DEF_USBD_SN_DESC_LEN; - break; - - default: - errflag = 0xFF; - break; - } - break; - - default : - errflag = 0xFF; + if( e->lIndexValue == USBFS_IndexValue ) + { + ctx->pUSBFS_Descr = e->addr; + len = e->length; break; + } + } + if( e == e_end ) + { + errflag = 0xFF; } /* Copy Descriptors to Endp0 DMA buffer */ @@ -389,28 +242,29 @@ void USBFS_IRQHandler() USBFS_SetupReqLen = len; } len = ( USBFS_SetupReqLen >= DEF_USBD_UEP0_SIZE ) ? DEF_USBD_UEP0_SIZE : USBFS_SetupReqLen; - memcpy( USBFS_EP0_Buf, pUSBFS_Descr, len ); - pUSBFS_Descr += len; + memcpy( ctx->USBFS_EP0_Buf, ctx->pUSBFS_Descr, len ); + ctx->pUSBFS_Descr += len; break; + } /* Set usb address */ case USB_SET_ADDRESS: - USBFS_DevAddr = (uint8_t)( USBFS_SetupReqValue & 0xFF ); + ctx->USBFS_DevAddr = (uint8_t)( ctx->USBFS_IndexValue & 0xFF ); break; /* Get usb configuration now set */ case USB_GET_CONFIGURATION: - USBFS_EP0_Buf[ 0 ] = USBFS_DevConfig; - if( USBFS_SetupReqLen > 1 ) + ctx->USBFS_EP0_Buf[ 0 ] = ctx->USBFS_DevConfig; + if( ctx->USBFS_SetupReqLen > 1 ) { - USBFS_SetupReqLen = 1; + ctx->USBFS_SetupReqLen = 1; } break; /* Set usb configuration to use */ case USB_SET_CONFIGURATION: - USBFS_DevConfig = (uint8_t)( USBFS_SetupReqValue & 0xFF ); - USBFS_DevEnumStatus = 0x01; + ctx->USBFS_DevConfig = (uint8_t)( ctx->USBFS_IndexValue & 0xFF ); + ctx->USBFS_DevEnumStatus = 0x01; break; /* Clear or disable one usb feature */ @@ -418,10 +272,10 @@ void USBFS_IRQHandler() if( ( USBFS_SetupReqType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_DEVICE ) { /* clear one device feature */ - if( (uint8_t)( USBFS_SetupReqValue & 0xFF ) == USB_REQ_FEAT_REMOTE_WAKEUP ) + if( (uint8_t)( USBFS_IndexValue & 0xFF ) == USB_REQ_FEAT_REMOTE_WAKEUP ) { /* clear usb sleep status, device not prepare to sleep */ - USBFS_DevSleepStatus &= ~0x01; + ctx->USBFS_DevSleepStatus &= ~0x01; } else { @@ -430,7 +284,7 @@ void USBFS_IRQHandler() } else if( ( USBFS_SetupReqType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_ENDP ) { - if( (uint8_t)( USBFS_SetupReqValue & 0xFF ) == USB_REQ_FEAT_ENDP_HALT ) + if( (uint8_t)( USBFS_IndexValue & 0xFF ) == USB_REQ_FEAT_ENDP_HALT ) { /* Clear End-point Feature */ switch( (uint8_t)( USBFS_SetupReqIndex & 0xFF ) ) @@ -465,20 +319,15 @@ void USBFS_IRQHandler() case USB_SET_FEATURE: if( ( USBFS_SetupReqType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_DEVICE ) { +#if FUSB_SUPPORTS_SLEEP /* Set Device Feature */ - if( (uint8_t)( USBFS_SetupReqValue & 0xFF ) == USB_REQ_FEAT_REMOTE_WAKEUP ) + if( (uint8_t)( USBFS_IndexValue & 0xFF ) == USB_REQ_FEAT_REMOTE_WAKEUP ) { - if( MyCfgDescr[ 7 ] & 0x20 ) - { - /* Set Wake-up flag, device prepare to sleep */ - USBFS_DevSleepStatus |= 0x01; - } - else - { - errflag = 0xFF; - } + /* Set Wake-up flag, device prepare to sleep */ + USBFS_DevSleepStatus |= 0x01; } else +#endif { errflag = 0xFF; } @@ -486,7 +335,7 @@ void USBFS_IRQHandler() else if( ( USBFS_SetupReqType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_ENDP ) { /* Set Endpoint Feature */ - if( (uint8_t)( USBFS_SetupReqValue & 0xFF ) == USB_REQ_FEAT_ENDP_HALT ) + if( (uint8_t)( USBFS_IndexValue & 0xFF ) == USB_REQ_FEAT_ENDP_HALT ) { switch( (uint8_t)( USBFS_SetupReqIndex & 0xFF ) ) { @@ -516,7 +365,7 @@ void USBFS_IRQHandler() /* This request allows the host to select another setting for the specified interface */ case USB_GET_INTERFACE: - USBFS_EP0_Buf[ 0 ] = 0x00; + ctx->USBFS_EP0_Buf[ 0 ] = 0x00; if( USBFS_SetupReqLen > 1 ) { USBFS_SetupReqLen = 1; @@ -528,17 +377,17 @@ void USBFS_IRQHandler() /* host get status of specified device/interface/end-points */ case USB_GET_STATUS: - USBFS_EP0_Buf[ 0 ] = 0x00; - USBFS_EP0_Buf[ 1 ] = 0x00; + ctx->USBFS_EP0_Buf[ 0 ] = 0x00; + ctx->USBFS_EP0_Buf[ 1 ] = 0x00; if( ( USBFS_SetupReqType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_DEVICE ) { - if( USBFS_DevSleepStatus & 0x01 ) + if( ctx->USBFS_DevSleepStatus & 0x01 ) { - USBFS_EP0_Buf[ 0 ] = 0x02; + ctx->USBFS_EP0_Buf[ 0 ] = 0x02; } else { - USBFS_EP0_Buf[ 0 ] = 0x00; + ctx->USBFS_EP0_Buf[ 0 ] = 0x00; } } else if( ( USBFS_SetupReqType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_ENDP ) @@ -547,14 +396,14 @@ void USBFS_IRQHandler() { if( ( USBFS->UEP1_CTRL_H & USBFS_UEP_T_RES_MASK ) == USBFS_UEP_T_RES_STALL ) { - USBFS_EP0_Buf[ 0 ] = 0x01; + ctx->USBFS_EP0_Buf[ 0 ] = 0x01; } } else if( (uint8_t)( USBFS_SetupReqIndex & 0xFF ) == ( DEF_UEP_IN | DEF_UEP2 ) ) { if( ( USBFS->UEP2_CTRL_H & USBFS_UEP_T_RES_MASK ) == USBFS_UEP_T_RES_STALL ) { - USBFS_EP0_Buf[ 0 ] = 0x01; + ctx->USBFS_EP0_Buf[ 0 ] = 0x01; } } else @@ -620,10 +469,10 @@ void USBFS_IRQHandler() else if( intfgst & CRB_UIF_BUS_RST ) { /* usb reset interrupt processing */ - USBFS_DevConfig = 0; - USBFS_DevAddr = 0; - USBFS_DevSleepStatus = 0; - USBFS_DevEnumStatus = 0; + ctx->USBFS_DevConfig = 0; + ctx->USBFS_DevAddr = 0; + ctx->USBFS_DevSleepStatus = 0; + ctx->USBFS_DevEnumStatus = 0; USBFS->DEV_ADDR = 0; USBFS_Device_Endp_Init( ); @@ -633,8 +482,8 @@ void USBFS_IRQHandler() /* usb suspend interrupt processing */ if( USBFS->MIS_ST & USBFS_UMS_SUSPEND ) { - USBFS_DevSleepStatus |= 0x02; - if( USBFS_DevSleepStatus == 0x03 ) + ctx->USBFS_DevSleepStatus |= 0x02; + if( ctx->USBFS_DevSleepStatus == 0x03 ) { /* Handling usb sleep here */ //TODO: MCU_Sleep_Wakeup_Operate( ); @@ -642,7 +491,7 @@ void USBFS_IRQHandler() } else { - USBFS_DevSleepStatus &= ~0x02; + ctx->USBFS_DevSleepStatus &= ~0x02; } } @@ -659,18 +508,18 @@ void USBFS_Device_Endp_Init() USBFS->UEP2_3_MOD = RB_UEP2_TX_EN; USBFS->UEP567_MOD = 0; - USBFS->UEP0_DMA = (intptr_t)USBFS_EP0_Buf; - USBFS->UEP1_DMA = (intptr_t)USBFS_EP1_Buf; - USBFS->UEP2_DMA = (intptr_t)USBFS_EP2_Buf; + USBFS->UEP0_DMA = (intptr_t)FSUSBCTX.USBFS_EP0_Buf; + USBFS->UEP1_DMA = (intptr_t)FSUSBCTX.USBFS_EP1_Buf; + USBFS->UEP2_DMA = (intptr_t)FSUSBCTX.USBFS_EP2_Buf; USBFS->UEP0_CTRL_H = USBFS_UEP_R_RES_ACK | USBFS_UEP_T_RES_NAK; USBFS->UEP1_CTRL_H = USBFS_UEP_T_RES_NAK; USBFS->UEP2_CTRL_H = USBFS_UEP_T_RES_NAK; /* Clear End-points Busy Status */ - for(uint8_t i=0; i< sizeof(USBFS_Endp_Busy)/sizeof(USBFS_Endp_Busy[0]); i++ ) + for(uint8_t i=0; i< sizeof(FSUSBCTX.USBFS_Endp_Busy)/sizeof(FSUSBCTX.USBFS_Endp_Busy[0]); i++ ) { - USBFS_Endp_Busy[ i ] = 0; + FSUSBCTX.USBFS_Endp_Busy[ i ] = 0; } } @@ -720,7 +569,12 @@ int FSUSBSetup() // GPIOC_OUTDR selects the up pull, and PC16 // corresponding to CNF=01 selects the floating input. - // XXX TODO: Handle for 5V VDD. +#if FUSB_5V_OPERATION + // XXX This is dubious, copied from x035 - checkme. + AFIO->CTLR = (AFIO->CTLR & ~(UDP_PUE_MASK | UDM_PUE_MASK | USB_PHY_V33)) | UDP_PUE_10K | USB_IOEN; +#else + AFIO->CTLR = (AFIO->CTLR & ~(UDP_PUE_MASK | UDM_PUE_MASK )) | USB_PHY_V33 | UDP_PUE_1K5 | USB_IOEN; +#endif AFIO->CTLR = (AFIO->CTLR & ~(UDP_PUE_11 | UDM_PUE_11 )) | USB_PHY_V33 | USB_IOEN | UDP_PUE_11; //1.5k pullup @@ -746,7 +600,7 @@ uint8_t USBFS_Endp_DataUp(uint8_t endp, uint8_t *pbuf, uint16_t len, uint8_t mod /* DMA config, endp_ctrl config, endp_len config */ if( ( endp >= DEF_UEP1 ) && ( endp <= DEF_UEP7 ) ) { - if( USBFS_Endp_Busy[ endp ] == 0 ) + if( FSUSBCTX.USBFS_Endp_Busy[ endp ] == 0 ) { if( (endp == DEF_UEP1) || (endp == DEF_UEP4) ) { @@ -843,7 +697,7 @@ uint8_t USBFS_Endp_DataUp(uint8_t endp, uint8_t *pbuf, uint16_t len, uint8_t mod /* response ack */ USBFSD_UEP_TX_CTRL( endp ) = ( USBFSD_UEP_TX_CTRL( endp ) & ~USBFS_UEP_T_RES_MASK ) | USBFS_UEP_T_RES_ACK; /* Set end-point busy */ - USBFS_Endp_Busy[ endp ] = 0x01; + FSUSBCTX.USBFS_Endp_Busy[ endp ] = 0x01; } } else diff --git a/examples_x035/usbdevice.incomplete/fsusb.h b/examples_x035/usbdevice.incomplete/fsusb.h index f34f9f548d62bb21e813374f16ddf6f17b4e9b72..0ea32230edf8b1f05c8626df23abdef1d264c678 100644 --- a/examples_x035/usbdevice.incomplete/fsusb.h +++ b/examples_x035/usbdevice.incomplete/fsusb.h @@ -2,8 +2,9 @@ #define _FSUSB_H #include <stdint.h> - -#include "descriptor.h" +#include "ch32v003fun.h" +#include "usb_defines.h" +#include "usb_config.h" extern uint32_t USBDEBUG0, USBDEBUG1, USBDEBUG2; @@ -11,13 +12,42 @@ int FSUSBSetup(); void USBFS_Poll(); uint8_t USBFS_Endp_DataUp(uint8_t endp, uint8_t *pbuf, uint16_t len, uint8_t mod); -// XXX TODO: Put the below in an object. -/*******************************************************************************/ -/* Variable Definition */ -/* Global */ -extern const uint8_t *pUSBFS_Descr; +struct _USBState +{ + /* Setup Request */ + volatile uint8_t USBFS_SetupReqCode; + volatile uint8_t USBFS_SetupReqType; + volatile uint16_t USBFS_SetupReqLen; + volatile uint32_t USBFS_IndexValue; + + /* USB Device Status */ + volatile uint8_t USBFS_DevConfig; + volatile uint8_t USBFS_DevAddr; + volatile uint8_t USBFS_DevSleepStatus; + volatile uint8_t USBFS_DevEnumStatus; + + /* Endpoint Buffer */ + __attribute__ ((aligned(4))) uint8_t USBFS_EP0_Buf[64]; + __attribute__ ((aligned(4))) uint8_t USBFS_EP1_Buf[64]; + __attribute__ ((aligned(4))) uint8_t USBFS_EP2_Buf[64]; + #define pUSBFS_SetupReqPak ((tusb_control_request_t*)ctx->USBFS_EP0_Buf) + + + int USBFS_HidIdle[2]; + int USBFS_HidProtocol[2]; + + const uint8_t *pUSBFS_Descr; + + /* USB IN Endpoint Busy Flag */ + volatile uint8_t USBFS_Endp_Busy[UNUM_EP]; +}; +extern struct _USBState FSUSBCTX; + + +#if 0 +extern const uint8_t *pUSBFS_Descr; /* USB Device Status */ extern volatile uint8_t USBFS_DevConfig; @@ -35,405 +65,7 @@ extern volatile uint8_t USBFS_Endp_Busy[ ]; // ABOVE: TOOD: REWRITE! - -// Copied from x035 documentation. -#define USBFSD_UEP_MOD_BASE 0x4002340C -#define USBFSD_UEP_DMA_BASE 0x40023410 -#define USBFSD_UEP_LEN_BASE 0x40023420 -#define USBFSD_UEP_CTL_BASE 0x40023422 -#define USBFSD_UEP_RX_EN 0x08 -#define USBFSD_UEP_TX_EN 0x04 -#define USBFSD_UEP_BUF_MOD 0x01 -#define DEF_UEP_DMA_LOAD 0 /* Direct the DMA address to the data to be processed */ -#define DEF_UEP_CPY_LOAD 1 /* Use memcpy to move data to a buffer */ -#define USBFSD_UEP_MOD( N ) (*((volatile uint8_t *)( USBFSD_UEP_MOD_BASE + N ))) -#define USBFSD_UEP_TX_CTRL( N ) (*((volatile uint8_t *)( USBFSD_UEP_CTL_BASE + N * 0x04 ))) -#define USBFSD_UEP_RX_CTRL( N ) (*((volatile uint8_t *)( USBFSD_UEP_CTL_BASE + N * 0x04 ))) -#define USBFSD_UEP_DMA( N ) (*((volatile uint32_t *)( USBFSD_UEP_DMA_BASE + N * 0x04 ))) -#define USBFSD_UEP_BUF( N ) ((uint8_t *)(*((volatile uint32_t *)( USBFSD_UEP_DMA_BASE + N * 0x04 ))) + 0x20000000) -#define USBFSD_UEP_TLEN( N ) (*((volatile uint16_t *)( USBFSD_UEP_LEN_BASE + N * 0x04 ))) - -/* R8_UEPn_TX_CTRL */ -#define USBFS_UEP_T_AUTO_TOG (1<<4) // enable automatic toggle after successful transfer completion on endpoint 1/2/3: 0=manual toggle, 1=automatic toggle -#define USBFS_UEP_T_TOG (1<<6) // prepared data toggle flag of USB endpoint X transmittal (IN): 0=DATA0, 1=DATA1 -#define USBFS_UEP_T_RES_MASK (3<<0) // bit mask of handshake response type for USB endpoint X transmittal (IN) -#define USBFS_UEP_T_RES_ACK (0<<1) -#define USBFS_UEP_T_RES_NONE (1<<0) -#define USBFS_UEP_T_RES_NAK (1<<1) -#define USBFS_UEP_T_RES_STALL (3<<0) -// bUEP_T_RES1 & bUEP_T_RES0: handshake response type for USB endpoint X transmittal (IN) -// 00: DATA0 or DATA1 then expecting ACK (ready) -// 01: DATA0 or DATA1 then expecting no response, time out from host, for non-zero endpoint isochronous transactions -// 10: NAK (busy) -// 11: STALL (error) -// host aux setup - -/* R8_UEPn_RX_CTRL, n=0-7 */ -#define USBFS_UEP_R_AUTO_TOG (1<<4) // enable automatic toggle after successful transfer completion on endpoint 1/2/3: 0=manual toggle, 1=automatic toggle -#define USBFS_UEP_R_TOG (1<<7) // expected data toggle flag of USB endpoint X receiving (OUT): 0=DATA0, 1=DATA1 -#define USBFS_UEP_R_RES_MASK (3<<2) // bit mask of handshake response type for USB endpoint X receiving (OUT) -#define USBFS_UEP_R_RES_ACK (0<<3) -#define USBFS_UEP_R_RES_NONE (1<<2) -#define USBFS_UEP_R_RES_NAK (1<<3) -#define USBFS_UEP_R_RES_STALL (3<<2) - - -/* R8_USB_DEV_AD */ -#define USBFS_UDA_GP_BIT 0x80 -#define USBFS_USB_ADDR_MASK 0x7F - - - -#define DEF_USBD_UEP0_SIZE 64 /* usb hs/fs device end-point 0 size */ -#define EP_SIZE 64 - -#define DEF_UEP_IN 0x80 -#define DEF_UEP_OUT 0x00 -#define DEF_UEP_BUSY 0x01 -#define DEF_UEP_FREE 0x00 - -#define DEF_UEP0 0 -#define DEF_UEP1 1 -#define DEF_UEP2 2 -#define DEF_UEP3 3 -#define DEF_UEP4 4 -#define DEF_UEP5 5 -#define DEF_UEP6 6 -#define DEF_UEP7 7 -#define NUM_EP 8 - -// Hardware specific - -#define EP1_T_EN (1<<6) -#define EP2_T_EN (1<<2) -#define EP3_T_EN (1<<6) -#define EP4_T_EN (1<<2) -#define EP1_R_EN (1<<7) -#define EP2_R_EN (1<<3) -#define EP3_R_EN (1<<7) -#define EP4_R_EN (1<<3) - - -/* R8_USB_CTRL */ -#define USBFS_UC_HOST_MODE 0x80 -#define USBFS_UC_LOW_SPEED 0x40 -#define USBFS_UC_DEV_PU_EN 0x20 -#define USBFS_UC_SYS_CTRL_MASK 0x30 -#define USBFS_UC_SYS_CTRL0 0x00 -#define USBFS_UC_SYS_CTRL1 0x10 -#define USBFS_UC_SYS_CTRL2 0x20 -#define USBFS_UC_SYS_CTRL3 0x30 -#define USBFS_UC_INT_BUSY 0x08 -#define USBFS_UC_RESET_SIE 0x04 -#define USBFS_UC_CLR_ALL 0x02 -#define USBFS_UC_DMA_EN 0x01 - -/* R8_USB_INT_EN */ -#define USBFS_UIE_DEV_SOF 0x80 -#define USBFS_UIE_DEV_NAK 0x40 -#define USBFS_UIE_FIFO_OV 0x10 -#define USBFS_UIE_HST_SOF 0x08 -#define USBFS_UIE_SUSPEND 0x04 -#define USBFS_UIE_TRANSFER 0x02 -#define USBFS_UIE_DETECT 0x01 -#define USBFS_UIE_BUS_RST 0x01 - -/* R8_USB_DEV_AD */ -#define USBFS_UDA_GP_BIT 0x80 -#define USBFS_USB_ADDR_MASK 0x7F - -/* R8_USB_MIS_ST */ -#define USBFS_UMS_SOF_PRES 0x80 -#define USBFS_UMS_SOF_ACT 0x40 -#define USBFS_UMS_SIE_FREE 0x20 -#define USBFS_UMS_R_FIFO_RDY 0x10 -#define USBFS_UMS_BUS_RESET 0x08 -#define USBFS_UMS_SUSPEND 0x04 -#define USBFS_UMS_DM_LEVEL 0x02 -#define USBFS_UMS_DEV_ATTACH 0x01 - -/* USB Setup Request */ -typedef struct __attribute__((packed)) _USB_SETUP_REQ -{ - uint8_t bRequestType; - uint8_t bRequest; - uint16_t wValue; - uint16_t wIndex; - uint16_t wLength; -} USB_SETUP_REQ, *PUSB_SETUP_REQ; - -/* USB Device Descriptor */ -typedef struct __attribute__((packed)) _USB_DEVICE_DESCR -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint16_t bcdUSB; - uint8_t bDeviceClass; - uint8_t bDeviceSubClass; - uint8_t bDeviceProtocol; - uint8_t bMaxPacketSize0; - uint16_t idVendor; - uint16_t idProduct; - uint16_t bcdDevice; - uint8_t iManufacturer; - uint8_t iProduct; - uint8_t iSerialNumber; - uint8_t bNumConfigurations; -} USB_DEV_DESCR, *PUSB_DEV_DESCR; - -/* USB Configuration Descriptor */ -typedef struct __attribute__((packed)) _USB_CONFIG_DESCR -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint16_t wTotalLength; - uint8_t bNumInterfaces; - uint8_t bConfigurationValue; - uint8_t iConfiguration; - uint8_t bmAttributes; - uint8_t MaxPower; -} USB_CFG_DESCR, *PUSB_CFG_DESCR; - -/* USB Interface Descriptor */ -typedef struct __attribute__((packed)) _USB_INTERF_DESCR -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bInterfaceNumber; - uint8_t bAlternateSetting; - uint8_t bNumEndpoints; - uint8_t bInterfaceClass; - uint8_t bInterfaceSubClass; - uint8_t bInterfaceProtocol; - uint8_t iInterface; -} USB_ITF_DESCR, *PUSB_ITF_DESCR; - -/* USB Endpoint Descriptor */ -typedef struct __attribute__((packed)) _USB_ENDPOINT_DESCR -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bEndpointAddress; - uint8_t bmAttributes; - uint8_t wMaxPacketSizeL; - uint8_t wMaxPacketSizeH; - uint8_t bInterval; -} USB_ENDP_DESCR, *PUSB_ENDP_DESCR; - -/* USB Configuration Descriptor Set */ -typedef struct __attribute__((packed)) _USB_CONFIG_DESCR_LONG -{ - USB_CFG_DESCR cfg_descr; - USB_ITF_DESCR itf_descr; - USB_ENDP_DESCR endp_descr[ 1 ]; -} USB_CFG_DESCR_LONG, *PUSB_CFG_DESCR_LONG; - -/* USB HUB Descriptor */ -typedef struct __attribute__((packed)) _USB_HUB_DESCR -{ - uint8_t bDescLength; - uint8_t bDescriptorType; - uint8_t bNbrPorts; - uint8_t wHubCharacteristicsL; - uint8_t wHubCharacteristicsH; - uint8_t bPwrOn2PwrGood; - uint8_t bHubContrCurrent; - uint8_t DeviceRemovable; - uint8_t PortPwrCtrlMask; -} USB_HUB_DESCR, *PUSB_HUB_DESCR; - -/* USB HID Descriptor */ -typedef struct __attribute__((packed)) _USB_HID_DESCR -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint16_t bcdHID; - uint8_t bCountryCode; - uint8_t bNumDescriptors; - uint8_t bDescriptorTypeX; - uint8_t wDescriptorLengthL; - uint8_t wDescriptorLengthH; -} USB_HID_DESCR, *PUSB_HID_DESCR; - - -/*******************************************************************************/ -/* USB Communication Related Macro Definition */ - -/* USB PID */ -#ifndef USB_PID_SETUP -#define USB_PID_NULL 0x00 -#define USB_PID_SOF 0x05 -#define USB_PID_SETUP 0x0D -#define USB_PID_IN 0x09 -#define USB_PID_OUT 0x01 -#define USB_PID_NYET 0x06 -#define USB_PID_ACK 0x02 -#define USB_PID_NAK 0x0A -#define USB_PID_STALL 0x0E -#define USB_PID_DATA0 0x03 -#define USB_PID_DATA1 0x0B -#define USB_PID_PRE 0x0C -#endif - -/* USB standard device request code */ -#ifndef USB_GET_DESCRIPTOR -#define USB_GET_STATUS 0x00 -#define USB_CLEAR_FEATURE 0x01 -#define USB_SET_FEATURE 0x03 -#define USB_SET_ADDRESS 0x05 -#define USB_GET_DESCRIPTOR 0x06 -#define USB_SET_DESCRIPTOR 0x07 -#define USB_GET_CONFIGURATION 0x08 -#define USB_SET_CONFIGURATION 0x09 -#define USB_GET_INTERFACE 0x0A -#define USB_SET_INTERFACE 0x0B -#define USB_SYNCH_FRAME 0x0C -#endif - -#define DEF_STRING_DESC_LANG 0x00 -#define DEF_STRING_DESC_MANU 0x01 -#define DEF_STRING_DESC_PROD 0x02 -#define DEF_STRING_DESC_SERN 0x03 - -/* USB hub class request code */ -#ifndef HUB_GET_DESCRIPTOR -#define HUB_GET_STATUS 0x00 -#define HUB_CLEAR_FEATURE 0x01 -#define HUB_GET_STATE 0x02 -#define HUB_SET_FEATURE 0x03 -#define HUB_GET_DESCRIPTOR 0x06 -#define HUB_SET_DESCRIPTOR 0x07 -#endif - -/* USB HID class request code */ -#ifndef HID_GET_REPORT -#define HID_GET_REPORT 0x01 -#define HID_GET_IDLE 0x02 -#define HID_GET_PROTOCOL 0x03 -#define HID_SET_REPORT 0x09 -#define HID_SET_IDLE 0x0A -#define HID_SET_PROTOCOL 0x0B -#endif - -/* Bit Define for USB Request Type */ -#ifndef USB_REQ_TYP_MASK -#define USB_REQ_TYP_IN 0x80 -#define USB_REQ_TYP_OUT 0x00 -#define USB_REQ_TYP_READ 0x80 -#define USB_REQ_TYP_WRITE 0x00 -#define USB_REQ_TYP_MASK 0x60 -#define USB_REQ_TYP_STANDARD 0x00 -#define USB_REQ_TYP_CLASS 0x20 -#define USB_REQ_TYP_VENDOR 0x40 -#define USB_REQ_TYP_RESERVED 0x60 -#define USB_REQ_RECIP_MASK 0x1F -#define USB_REQ_RECIP_DEVICE 0x00 -#define USB_REQ_RECIP_INTERF 0x01 -#define USB_REQ_RECIP_ENDP 0x02 -#define USB_REQ_RECIP_OTHER 0x03 -#define USB_REQ_FEAT_REMOTE_WAKEUP 0x01 -#define USB_REQ_FEAT_ENDP_HALT 0x00 -#endif - -/* USB Descriptor Type */ -#ifndef USB_DESCR_TYP_DEVICE -#define USB_DESCR_TYP_DEVICE 0x01 -#define USB_DESCR_TYP_CONFIG 0x02 -#define USB_DESCR_TYP_STRING 0x03 -#define USB_DESCR_TYP_INTERF 0x04 -#define USB_DESCR_TYP_ENDP 0x05 -#define USB_DESCR_TYP_QUALIF 0x06 -#define USB_DESCR_TYP_SPEED 0x07 -#define USB_DESCR_TYP_OTG 0x09 -#define USB_DESCR_TYP_BOS 0X0F -#define USB_DESCR_TYP_HID 0x21 -#define USB_DESCR_TYP_REPORT 0x22 -#define USB_DESCR_TYP_PHYSIC 0x23 -#define USB_DESCR_TYP_CS_INTF 0x24 -#define USB_DESCR_TYP_CS_ENDP 0x25 -#define USB_DESCR_TYP_HUB 0x29 -#endif - -/* USB Device Class */ -#ifndef USB_DEV_CLASS_HUB -#define USB_DEV_CLASS_RESERVED 0x00 -#define USB_DEV_CLASS_AUDIO 0x01 -#define USB_DEV_CLASS_COMMUNIC 0x02 -#define USB_DEV_CLASS_HID 0x03 -#define USB_DEV_CLASS_MONITOR 0x04 -#define USB_DEV_CLASS_PHYSIC_IF 0x05 -#define USB_DEV_CLASS_POWER 0x06 -#define USB_DEV_CLASS_PRINTER 0x07 -#define USB_DEV_CLASS_STORAGE 0x08 -#define USB_DEV_CLASS_HUB 0x09 -#define USB_DEV_CLASS_VEN_SPEC 0xFF -#endif - -/* USB Hub Class Request */ -#ifndef HUB_GET_HUB_DESCRIPTOR -#define HUB_CLEAR_HUB_FEATURE 0x20 -#define HUB_CLEAR_PORT_FEATURE 0x23 -#define HUB_GET_BUS_STATE 0xA3 -#define HUB_GET_HUB_DESCRIPTOR 0xA0 -#define HUB_GET_HUB_STATUS 0xA0 -#define HUB_GET_PORT_STATUS 0xA3 -#define HUB_SET_HUB_DESCRIPTOR 0x20 -#define HUB_SET_HUB_FEATURE 0x20 -#define HUB_SET_PORT_FEATURE 0x23 -#endif - -/* Hub Class Feature Selectors */ -#ifndef HUB_PORT_RESET -#define HUB_C_HUB_LOCAL_POWER 0 -#define HUB_C_HUB_OVER_CURRENT 1 -#define HUB_PORT_CONNECTION 0 -#define HUB_PORT_ENABLE 1 -#define HUB_PORT_SUSPEND 2 -#define HUB_PORT_OVER_CURRENT 3 -#define HUB_PORT_RESET 4 -#define HUB_PORT_POWER 8 -#define HUB_PORT_LOW_SPEED 9 -#define HUB_C_PORT_CONNECTION 16 -#define HUB_C_PORT_ENABLE 17 -#define HUB_C_PORT_SUSPEND 18 -#define HUB_C_PORT_OVER_CURRENT 19 -#define HUB_C_PORT_RESET 20 -#endif - -/* USB HID Class Request Code */ -#ifndef HID_GET_REPORT -#define HID_GET_REPORT 0x01 -#define HID_GET_IDLE 0x02 -#define HID_GET_PROTOCOL 0x03 -#define HID_SET_REPORT 0x09 -#define HID_SET_IDLE 0x0A -#define HID_SET_PROTOCOL 0x0B -#endif - -/* USB UDisk */ -#ifndef USB_BO_CBW_SIZE -#define USB_BO_CBW_SIZE 0x1F -#define USB_BO_CSW_SIZE 0x0D -#endif -#ifndef USB_BO_CBW_SIG0 -#define USB_BO_CBW_SIG0 0x55 -#define USB_BO_CBW_SIG1 0x53 -#define USB_BO_CBW_SIG2 0x42 -#define USB_BO_CBW_SIG3 0x43 -#define USB_BO_CSW_SIG0 0x55 -#define USB_BO_CSW_SIG1 0x53 -#define USB_BO_CSW_SIG2 0x42 -#define USB_BO_CSW_SIG3 0x53 -#endif - -/* USB CDC Class request code */ -#ifndef CDC_GET_LINE_CODING -#define CDC_GET_LINE_CODING 0X21 /* This request allows the host to find out the currently configured line coding */ -#define CDC_SET_LINE_CODING 0x20 /* Configures DTE rate, stop-bits, parity, and number-of-character */ -#define CDC_SET_LINE_CTLSTE 0X22 /* This request generates RS-232/V.24 style control signals */ -#define CDC_SEND_BREAK 0X23 /* Sends special carrier modulation used to specify RS-232 style break */ #endif -/*******************************************************************************/ #endif diff --git a/examples_x035/usbdevice.incomplete/usbdevice.c b/examples_x035/usbdevice.incomplete/usbdevice.c index 2220c2b8798795bf2c8b4e21488cf5a787f42c2e..3dfd0251756332f58a85af7b44757f6486ee1fe6 100644 --- a/examples_x035/usbdevice.incomplete/usbdevice.c +++ b/examples_x035/usbdevice.incomplete/usbdevice.c @@ -29,13 +29,13 @@ void USBFS_IRQHandler(); //extern int USBFS_IRQHandler; while(1) { - printf( "%lu %lu %lu %d %d\n", USBDEBUG0, USBDEBUG1, USBDEBUG2, USBFS_Endp_Busy[0], USBFS_Endp_Busy[1] ); + printf( "%lu %08x %lu %d %d\n", USBDEBUG0, USBDEBUG1, USBDEBUG2, 0, 0 ); USBFS_Poll(); int i; //printf( "!! %d %d %d\n", NUM_EP, USBFS_Endp_Busy[0], USBFS_Endp_Busy[1] ); for( i = 1; i < 3; i++ ) { - if( !USBFS_Endp_Busy[i] ) + if( !FSUSBCTX.USBFS_Endp_Busy[i] ) { /* @@ -46,7 +46,7 @@ USBDEBUG0+= 100; // USBFSD_UEP_TX_CTRL( i ) = ( USBFSD_UEP_TX_CTRL( i ) & ~USBFS_UEP_T_RES_MASK ) | USBFS_UEP_T_RES_ACK; // USBFS_Endp_Busy[i] = 1; - char pbuf[16] = { 0 }; + char pbuf[16] = { 0x00, 1, 1 }; uint8_t r = USBFS_Endp_DataUp( i, pbuf, (i==1)?8:4, DEF_UEP_CPY_LOAD); //USBDEBUG1+=r; }