1 /********************************** (C) COPYRIGHT ******************************* 2 * File Name : ch32v10x_usb.h 3 * Author : WCH 4 * Version : V1.0.0 5 * Date : 2020/04/30 6 * Description : This file contains all the functions prototypes for the USB 7 * firmware library. 8 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. 9 * SPDX-License-Identifier: Apache-2.0 10 *******************************************************************************/ 11 #ifndef __CH32V10x_USB_H 12 #define __CH32V10x_USB_H 13 14 #ifdef __cplusplus 15 extern "C" { 16 #endif 17 18 #ifndef NULL 19 #define NULL 0 20 #endif 21 22 #ifndef VOID 23 #define VOID void 24 #endif 25 #ifndef CONST 26 #define CONST const 27 #endif 28 #ifndef BOOL 29 typedef unsigned char BOOL; 30 #endif 31 #ifndef BOOLEAN 32 typedef unsigned char BOOLEAN; 33 #endif 34 #ifndef CHAR 35 typedef char CHAR; 36 #endif 37 #ifndef INT8 38 typedef char INT8; 39 #endif 40 #ifndef INT16 41 typedef short INT16; 42 #endif 43 #ifndef INT32 44 typedef long INT32; 45 #endif 46 #ifndef UINT8 47 typedef unsigned char UINT8; 48 #endif 49 #ifndef UINT16 50 typedef unsigned short UINT16; 51 #endif 52 #ifndef UINT32 53 typedef unsigned long UINT32; 54 #endif 55 #ifndef UINT8V 56 typedef unsigned char volatile UINT8V; 57 #endif 58 #ifndef UINT16V 59 typedef unsigned short volatile UINT16V; 60 #endif 61 #ifndef UINT32V 62 typedef unsigned long volatile UINT32V; 63 #endif 64 65 #ifndef PVOID 66 typedef void *PVOID; 67 #endif 68 #ifndef PCHAR 69 typedef char *PCHAR; 70 #endif 71 #ifndef PCHAR 72 typedef const char *PCCHAR; 73 #endif 74 #ifndef PINT8 75 typedef char *PINT8; 76 #endif 77 #ifndef PINT16 78 typedef short *PINT16; 79 #endif 80 #ifndef PINT32 81 typedef long *PINT32; 82 #endif 83 #ifndef PUINT8 84 typedef unsigned char *PUINT8; 85 #endif 86 #ifndef PUINT16 87 typedef unsigned short *PUINT16; 88 #endif 89 #ifndef PUINT32 90 typedef unsigned long *PUINT32; 91 #endif 92 #ifndef PUINT8V 93 typedef volatile unsigned char *PUINT8V; 94 #endif 95 #ifndef PUINT16V 96 typedef volatile unsigned short *PUINT16V; 97 #endif 98 #ifndef PUINT32V 99 typedef volatile unsigned long *PUINT32V; 100 #endif 101 102 /******************************************************************************/ 103 /* Peripheral memory map */ 104 /******************************************************************************/ 105 /* USB */ 106 #define R32_USB_CONTROL (*((PUINT32V)(0x40023400))) // USB control & interrupt enable & device address 107 #define R8_USB_CTRL (*((PUINT8V)(0x40023400))) // USB base control 108 #define RB_UC_HOST_MODE 0x80 // enable USB host mode: 0=device mode, 1=host mode 109 #define RB_UC_LOW_SPEED 0x40 // enable USB low speed: 0=12Mbps, 1=1.5Mbps 110 #define RB_UC_DEV_PU_EN 0x20 // USB device enable and internal pullup resistance enable 111 #define RB_UC_SYS_CTRL1 0x20 // USB system control high bit 112 #define RB_UC_SYS_CTRL0 0x10 // USB system control low bit 113 #define MASK_UC_SYS_CTRL 0x30 // bit mask of USB system control 114 // bUC_HOST_MODE & bUC_SYS_CTRL1 & bUC_SYS_CTRL0: USB system control 115 // 0 00: disable USB device and disable internal pullup resistance 116 // 0 01: enable USB device and disable internal pullup resistance, need external pullup resistance 117 // 0 1x: enable USB device and enable internal pullup resistance 118 // 1 00: enable USB host and normal status 119 // 1 01: enable USB host and force UDP/UDM output SE0 state 120 // 1 10: enable USB host and force UDP/UDM output J state 121 // 1 11: enable USB host and force UDP/UDM output resume or K state 122 #define RB_UC_INT_BUSY 0x08 // enable automatic responding busy for device mode or automatic pause for host mode during interrupt flag UIF_TRANSFER valid 123 #define RB_UC_RESET_SIE 0x04 // force reset USB SIE, need software clear 124 #define RB_UC_CLR_ALL 0x02 // force clear FIFO and count of USB 125 #define RB_UC_DMA_EN 0x01 // DMA enable and DMA interrupt enable for USB 126 127 #define R8_UDEV_CTRL (*((PUINT8V)(0x40023401))) // USB device physical prot control 128 #define RB_UD_PD_DIS 0x80 // disable USB UDP/UDM pulldown resistance: 0=enable pulldown, 1=disable 129 #define RB_UD_DP_PIN 0x20 // ReadOnly: indicate current UDP pin level 130 #define RB_UD_DM_PIN 0x10 // ReadOnly: indicate current UDM pin level 131 #define RB_UD_LOW_SPEED 0x04 // enable USB physical port low speed: 0=full speed, 1=low speed 132 #define RB_UD_GP_BIT 0x02 // general purpose bit 133 #define RB_UD_PORT_EN 0x01 // enable USB physical port I/O: 0=disable, 1=enable 134 135 #define R8_UHOST_CTRL R8_UDEV_CTRL // USB host physical prot control 136 #define RB_UH_PD_DIS 0x80 // disable USB UDP/UDM pulldown resistance: 0=enable pulldown, 1=disable 137 #define RB_UH_DP_PIN 0x20 // ReadOnly: indicate current UDP pin level 138 #define RB_UH_DM_PIN 0x10 // ReadOnly: indicate current UDM pin level 139 #define RB_UH_LOW_SPEED 0x04 // enable USB port low speed: 0=full speed, 1=low speed 140 #define RB_UH_BUS_RESET 0x02 // control USB bus reset: 0=normal, 1=force bus reset 141 #define RB_UH_PORT_EN 0x01 // enable USB port: 0=disable, 1=enable port, automatic disabled if USB device detached 142 143 #define R8_USB_INT_EN (*((PUINT8V)(0x40023402))) // USB interrupt enable 144 #define RB_UIE_DEV_SOF 0x80 // enable interrupt for SOF received for USB device mode 145 #define RB_UIE_DEV_NAK 0x40 // enable interrupt for NAK responded for USB device mode 146 #define RB_UIE_FIFO_OV 0x10 // enable interrupt for FIFO overflow 147 #define RB_UIE_HST_SOF 0x08 // enable interrupt for host SOF timer action for USB host mode 148 #define RB_UIE_SUSPEND 0x04 // enable interrupt for USB suspend or resume event 149 #define RB_UIE_TRANSFER 0x02 // enable interrupt for USB transfer completion 150 #define RB_UIE_DETECT 0x01 // enable interrupt for USB device detected event for USB host mode 151 #define RB_UIE_BUS_RST 0x01 // enable interrupt for USB bus reset event for USB device mode 152 153 #define R8_USB_DEV_AD (*((PUINT8V)(0x40023403))) // USB device address 154 #define RB_UDA_GP_BIT 0x80 // general purpose bit 155 #define MASK_USB_ADDR 0x7F // bit mask for USB device address 156 157 #define R32_USB_STATUS (*((PUINT32V)(0x40023404))) // USB miscellaneous status & interrupt flag & interrupt status 158 #define R8_USB_MIS_ST (*((PUINT8V)(0x40023405))) // USB miscellaneous status 159 #define RB_UMS_SOF_PRES 0x80 // RO, indicate host SOF timer presage status 160 #define RB_UMS_SOF_ACT 0x40 // RO, indicate host SOF timer action status for USB host 161 #define RB_UMS_SIE_FREE 0x20 // RO, indicate USB SIE free status 162 #define RB_UMS_R_FIFO_RDY 0x10 // RO, indicate USB receiving FIFO ready status (not empty) 163 #define RB_UMS_BUS_RESET 0x08 // RO, indicate USB bus reset status 164 #define RB_UMS_SUSPEND 0x04 // RO, indicate USB suspend status 165 #define RB_UMS_DM_LEVEL 0x02 // RO, indicate UDM level saved at device attached to USB host 166 #define RB_UMS_DEV_ATTACH 0x01 // RO, indicate device attached status on USB host 167 168 #define R8_USB_INT_FG (*((PUINT8V)(0x40023406))) // USB interrupt flag 169 #define RB_U_IS_NAK 0x80 // RO, indicate current USB transfer is NAK received 170 #define RB_U_TOG_OK 0x40 // RO, indicate current USB transfer toggle is OK 171 #define RB_U_SIE_FREE 0x20 // RO, indicate USB SIE free status 172 #define RB_UIF_FIFO_OV 0x10 // FIFO overflow interrupt flag for USB, direct bit address clear or write 1 to clear 173 #define RB_UIF_HST_SOF 0x08 // host SOF timer interrupt flag for USB host, direct bit address clear or write 1 to clear 174 #define RB_UIF_SUSPEND 0x04 // USB suspend or resume event interrupt flag, direct bit address clear or write 1 to clear 175 #define RB_UIF_TRANSFER 0x02 // USB transfer completion interrupt flag, direct bit address clear or write 1 to clear 176 #define RB_UIF_DETECT 0x01 // device detected event interrupt flag for USB host mode, direct bit address clear or write 1 to clear 177 #define RB_UIF_BUS_RST 0x01 // bus reset event interrupt flag for USB device mode, direct bit address clear or write 1 to clear 178 179 #define R8_USB_INT_ST (*((PUINT8V)(0x40023407))) // USB interrupt status 180 #define RB_UIS_IS_NAK 0x80 // RO, indicate current USB transfer is NAK received for USB device mode 181 #define RB_UIS_TOG_OK 0x40 // RO, indicate current USB transfer toggle is OK 182 #define RB_UIS_TOKEN1 0x20 // RO, current token PID code bit 1 received for USB device mode 183 #define RB_UIS_TOKEN0 0x10 // RO, current token PID code bit 0 received for USB device mode 184 #define MASK_UIS_TOKEN 0x30 // RO, bit mask of current token PID code received for USB device mode 185 #define UIS_TOKEN_OUT 0x00 186 #define UIS_TOKEN_SOF 0x10 187 #define UIS_TOKEN_IN 0x20 188 #define UIS_TOKEN_SETUP 0x30 189 // bUIS_TOKEN1 & bUIS_TOKEN0: current token PID code received for USB device mode 190 // 00: OUT token PID received 191 // 01: SOF token PID received 192 // 10: IN token PID received 193 // 11: SETUP token PID received 194 #define MASK_UIS_ENDP 0x0F // RO, bit mask of current transfer endpoint number for USB device mode 195 #define MASK_UIS_H_RES 0x0F // RO, bit mask of current transfer handshake response for USB host mode: 0000=no response, time out from device, others=handshake response PID received 196 197 #define R8_USB_RX_LEN (*((PUINT8V)(0x40023408))) // USB receiving length 198 #define R32_USB_BUF_MODE (*((PUINT32V)(0x4002340c))) // USB endpoint buffer mode 199 #define R8_UEP4_1_MOD (*((PUINT8V)(0x4002340c))) // endpoint 4/1 mode 200 #define RB_UEP1_RX_EN 0x80 // enable USB endpoint 1 receiving (OUT) 201 #define RB_UEP1_TX_EN 0x40 // enable USB endpoint 1 transmittal (IN) 202 #define RB_UEP1_BUF_MOD 0x10 // buffer mode of USB endpoint 1 203 // bUEPn_RX_EN & bUEPn_TX_EN & bUEPn_BUF_MOD: USB endpoint 1/2/3 buffer mode, buffer start address is UEPn_DMA 204 // 0 0 x: disable endpoint and disable buffer 205 // 1 0 0: 64 bytes buffer for receiving (OUT endpoint) 206 // 1 0 1: dual 64 bytes buffer by toggle bit bUEP_R_TOG selection for receiving (OUT endpoint), total=128bytes 207 // 0 1 0: 64 bytes buffer for transmittal (IN endpoint) 208 // 0 1 1: dual 64 bytes buffer by toggle bit bUEP_T_TOG selection for transmittal (IN endpoint), total=128bytes 209 // 1 1 0: 64 bytes buffer for receiving (OUT endpoint) + 64 bytes buffer for transmittal (IN endpoint), total=128bytes 210 // 1 1 1: dual 64 bytes buffer by bUEP_R_TOG selection for receiving (OUT endpoint) + dual 64 bytes buffer by bUEP_T_TOG selection for transmittal (IN endpoint), total=256bytes 211 #define RB_UEP4_RX_EN 0x08 // enable USB endpoint 4 receiving (OUT) 212 #define RB_UEP4_TX_EN 0x04 // enable USB endpoint 4 transmittal (IN) 213 // bUEP4_RX_EN & bUEP4_TX_EN: USB endpoint 4 buffer mode, buffer start address is UEP0_DMA 214 // 0 0: single 64 bytes buffer for endpoint 0 receiving & transmittal (OUT & IN endpoint) 215 // 1 0: single 64 bytes buffer for endpoint 0 receiving & transmittal (OUT & IN endpoint) + 64 bytes buffer for endpoint 4 receiving (OUT endpoint), total=128bytes 216 // 0 1: single 64 bytes buffer for endpoint 0 receiving & transmittal (OUT & IN endpoint) + 64 bytes buffer for endpoint 4 transmittal (IN endpoint), total=128bytes 217 // 1 1: single 64 bytes buffer for endpoint 0 receiving & transmittal (OUT & IN endpoint) 218 // + 64 bytes buffer for endpoint 4 receiving (OUT endpoint) + 64 bytes buffer for endpoint 4 transmittal (IN endpoint), total=192bytes 219 220 #define R8_UEP2_3_MOD (*((PUINT8V)(0x4002340d))) // endpoint 2/3 mode 221 #define RB_UEP3_RX_EN 0x80 // enable USB endpoint 3 receiving (OUT) 222 #define RB_UEP3_TX_EN 0x40 // enable USB endpoint 3 transmittal (IN) 223 #define RB_UEP3_BUF_MOD 0x10 // buffer mode of USB endpoint 3 224 #define RB_UEP2_RX_EN 0x08 // enable USB endpoint 2 receiving (OUT) 225 #define RB_UEP2_TX_EN 0x04 // enable USB endpoint 2 transmittal (IN) 226 #define RB_UEP2_BUF_MOD 0x01 // buffer mode of USB endpoint 2 227 228 #define R8_UH_EP_MOD R8_UEP2_3_MOD //host endpoint mode 229 #define RB_UH_EP_TX_EN 0x40 // enable USB host OUT endpoint transmittal 230 #define RB_UH_EP_TBUF_MOD 0x10 // buffer mode of USB host OUT endpoint 231 // bUH_EP_TX_EN & bUH_EP_TBUF_MOD: USB host OUT endpoint buffer mode, buffer start address is UH_TX_DMA 232 // 0 x: disable endpoint and disable buffer 233 // 1 0: 64 bytes buffer for transmittal (OUT endpoint) 234 // 1 1: dual 64 bytes buffer by toggle bit bUH_T_TOG selection for transmittal (OUT endpoint), total=128bytes 235 #define RB_UH_EP_RX_EN 0x08 // enable USB host IN endpoint receiving 236 #define RB_UH_EP_RBUF_MOD 0x01 // buffer mode of USB host IN endpoint 237 // bUH_EP_RX_EN & bUH_EP_RBUF_MOD: USB host IN endpoint buffer mode, buffer start address is UH_RX_DMA 238 // 0 x: disable endpoint and disable buffer 239 // 1 0: 64 bytes buffer for receiving (IN endpoint) 240 // 1 1: dual 64 bytes buffer by toggle bit bUH_R_TOG selection for receiving (IN endpoint), total=128bytes 241 242 #define R8_UEP5_6_MOD (*((PUINT8V)(0x4002340e))) // endpoint 5/6 mode 243 #define RB_UEP6_RX_EN 0x80 // enable USB endpoint 6 receiving (OUT) 244 #define RB_UEP6_TX_EN 0x40 // enable USB endpoint 6 transmittal (IN) 245 #define RB_UEP6_BUF_MOD 0x10 // buffer mode of USB endpoint 6 246 #define RB_UEP5_RX_EN 0x08 // enable USB endpoint 5 receiving (OUT) 247 #define RB_UEP5_TX_EN 0x04 // enable USB endpoint 5 transmittal (IN) 248 #define RB_UEP5_BUF_MOD 0x01 // buffer mode of USB endpoint 5 249 250 #define R8_UEP7_MOD (*((PUINT8V)(0x4002340f))) // endpoint 7 mode 251 #define RB_UEP7_RX_EN 0x08 // enable USB endpoint 7 receiving (OUT) 252 #define RB_UEP7_TX_EN 0x04 // enable USB endpoint 7 transmittal (IN) 253 #define RB_UEP7_BUF_MOD 0x01 // buffer mode of USB endpoint 7 254 255 #define R16_UEP0_DMA (*((PUINT16V)(0x40023410))) // endpoint 0 DMA buffer address 256 #define R16_UEP1_DMA (*((PUINT16V)(0x40023414))) // endpoint 1 DMA buffer address 257 #define R16_UEP2_DMA (*((PUINT16V)(0x40023418))) // endpoint 2 DMA buffer address 258 #define R16_UH_RX_DMA R16_UEP2_DMA // host rx endpoint buffer high address 259 #define R16_UEP3_DMA (*((PUINT16V)(0x4002341c))) // endpoint 3 DMA buffer address 260 261 #define R16_UEP4_DMA (*((PUINT16V)(0x40023420))) // endpoint 4 DMA buffer address 262 #define R16_UEP5_DMA (*((PUINT16V)(0x40023424))) // endpoint 5 DMA buffer address 263 #define R16_UEP6_DMA (*((PUINT16V)(0x40023428))) // endpoint 6 DMA buffer address 264 #define R16_UEP7_DMA (*((PUINT16V)(0x4002342c))) // endpoint 7 DMA buffer address 265 266 #define R16_UH_TX_DMA R16_UEP3_DMA // host tx endpoint buffer high address 267 #define R32_USB_EP0_CTRL (*((PUINT32V)(0x40023430))) // endpoint 0 control & transmittal length 268 #define R8_UEP0_T_LEN (*((PUINT8V)(0x40023430))) // endpoint 0 transmittal length 269 #define R8_UEP0_CTRL (*((PUINT8V)(0x40023432))) // endpoint 0 control 270 #define R32_USB_EP1_CTRL (*((PUINT32V)(0x40023434))) // endpoint 1 control & transmittal length 271 #define R8_UEP1_T_LEN (*((PUINT8V)(0x40023434))) // endpoint 1 transmittal length 272 #define R8_UEP1_CTRL (*((PUINT8V)(0x40023436))) // endpoint 1 control 273 #define RB_UEP_R_TOG 0x80 // expected data toggle flag of USB endpoint X receiving (OUT): 0=DATA0, 1=DATA1 274 #define RB_UEP_T_TOG 0x40 // prepared data toggle flag of USB endpoint X transmittal (IN): 0=DATA0, 1=DATA1 275 #define RB_UEP_AUTO_TOG 0x10 // enable automatic toggle after successful transfer completion on endpoint 1/2/3: 0=manual toggle, 1=automatic toggle 276 #define RB_UEP_R_RES1 0x08 // handshake response type high bit for USB endpoint X receiving (OUT) 277 #define RB_UEP_R_RES0 0x04 // handshake response type low bit for USB endpoint X receiving (OUT) 278 #define MASK_UEP_R_RES 0x0C // bit mask of handshake response type for USB endpoint X receiving (OUT) 279 #define UEP_R_RES_ACK 0x00 280 #define UEP_R_RES_TOUT 0x04 281 #define UEP_R_RES_NAK 0x08 282 #define UEP_R_RES_STALL 0x0C 283 // RB_UEP_R_RES1 & RB_UEP_R_RES0: handshake response type for USB endpoint X receiving (OUT) 284 // 00: ACK (ready) 285 // 01: no response, time out to host, for non-zero endpoint isochronous transactions 286 // 10: NAK (busy) 287 // 11: STALL (error) 288 #define RB_UEP_T_RES1 0x02 // handshake response type high bit for USB endpoint X transmittal (IN) 289 #define RB_UEP_T_RES0 0x01 // handshake response type low bit for USB endpoint X transmittal (IN) 290 #define MASK_UEP_T_RES 0x03 // bit mask of handshake response type for USB endpoint X transmittal (IN) 291 #define UEP_T_RES_ACK 0x00 292 #define UEP_T_RES_TOUT 0x01 293 #define UEP_T_RES_NAK 0x02 294 #define UEP_T_RES_STALL 0x03 295 // bUEP_T_RES1 & bUEP_T_RES0: handshake response type for USB endpoint X transmittal (IN) 296 // 00: DATA0 or DATA1 then expecting ACK (ready) 297 // 01: DATA0 or DATA1 then expecting no response, time out from host, for non-zero endpoint isochronous transactions 298 // 10: NAK (busy) 299 // 11: STALL (error) 300 301 #define R8_UH_SETUP R8_UEP1_CTRL // host aux setup 302 #define RB_UH_PRE_PID_EN 0x80 // USB host PRE PID enable for low speed device via hub 303 #define RB_UH_SOF_EN 0x40 // USB host automatic SOF enable 304 305 #define R32_USB_EP2_CTRL (*((PUINT32V)(0x40023438))) // endpoint 2 control & transmittal length 306 #define R8_UEP2_T_LEN (*((PUINT8V)(0x40023438))) // endpoint 2 transmittal length 307 #define R8_UEP2_CTRL (*((PUINT8V)(0x4002343a))) // endpoint 2 control 308 309 #define R8_UH_EP_PID R8_UEP2_T_LEN // host endpoint and PID 310 #define MASK_UH_TOKEN 0xF0 // bit mask of token PID for USB host transfer 311 #define MASK_UH_ENDP 0x0F // bit mask of endpoint number for USB host transfer 312 313 #define R8_UH_RX_CTRL R8_UEP2_CTRL // host receiver endpoint control 314 #define RB_UH_R_TOG 0x80 // expected data toggle flag of host receiving (IN): 0=DATA0, 1=DATA1 315 #define RB_UH_R_AUTO_TOG 0x10 // enable automatic toggle after successful transfer completion: 0=manual toggle, 1=automatic toggle 316 #define RB_UH_R_RES 0x04 // prepared handshake response type for host receiving (IN): 0=ACK (ready), 1=no response, time out to device, for isochronous transactions 317 318 #define R32_USB_EP3_CTRL (*((PUINT32V)(0x4002343c))) // endpoint 3 control & transmittal length 319 #define R8_UEP3_T_LEN (*((PUINT16V)(0x4002343c))) // endpoint 3 transmittal length 320 #define R8_UEP3_CTRL (*((PUINT8V)(0x4002343e))) // endpoint 3 control 321 #define R8_UH_TX_LEN (*((PUINT16V)(0x4002343c))) //R8_UEP3_T_LEN // host transmittal endpoint transmittal length 322 323 #define R8_UH_TX_CTRL R8_UEP3_CTRL // host transmittal endpoint control 324 #define RB_UH_T_TOG 0x40 // prepared data toggle flag of host transmittal (SETUP/OUT): 0=DATA0, 1=DATA1 325 #define RB_UH_T_AUTO_TOG 0x10 // enable automatic toggle after successful transfer completion: 0=manual toggle, 1=automatic toggle 326 #define RB_UH_T_RES 0x01 // expected handshake response type for host transmittal (SETUP/OUT): 0=ACK (ready), 1=no response, time out from device, for isochronous transactions 327 328 #define R32_USB_EP4_CTRL (*((PUINT32V)(0x40023440))) // endpoint 4 control & transmittal length 329 #define R8_UEP4_T_LEN (*((PUINT8V)(0x40023440))) // endpoint 4 transmittal length 330 #define R8_UEP4_CTRL (*((PUINT8V)(0x40023442))) // endpoint 4 control 331 332 #define R32_USB_EP5_CTRL (*((PUINT32V)(0x40023444))) // endpoint 5 control & transmittal length 333 #define R8_UEP5_T_LEN (*((PUINT8V)(0x40023444))) // endpoint 5 transmittal length 334 #define R8_UEP5_CTRL (*((PUINT8V)(0x40023446))) // endpoint 5 control 335 336 #define R32_USB_EP6_CTRL (*((PUINT32V)(0x40023448))) // endpoint 6 control & transmittal length 337 #define R8_UEP6_T_LEN (*((PUINT8V)(0x40023448))) // endpoint 6 transmittal length 338 #define R8_UEP6_CTRL (*((PUINT8V)(0x4002344a))) // endpoint 6 control 339 340 #define R32_USB_EP7_CTRL (*((PUINT32V)(0x4002344c))) // endpoint 7 control & transmittal length 341 #define R8_UEP7_T_LEN (*((PUINT8V)(0x4002344c))) // endpoint 7 transmittal length 342 #define R8_UEP7_CTRL (*((PUINT8V)(0x4002344e))) // endpoint 7 control 343 344 #ifdef __cplusplus 345 } 346 #endif 347 348 #endif //__CH32V10x_USB_H 349 350 #ifndef __USB_TYPE__ 351 #define __USB_TYPE__ 352 353 #ifdef __cplusplus 354 extern "C" { 355 #endif 356 357 /* USB constant and structure define */ 358 359 /* USB PID */ 360 #ifndef USB_PID_SETUP 361 #define USB_PID_NULL 0x00 /* reserved PID */ 362 #define USB_PID_SOF 0x05 363 #define USB_PID_SETUP 0x0D 364 #define USB_PID_IN 0x09 365 #define USB_PID_OUT 0x01 366 #define USB_PID_ACK 0x02 367 #define USB_PID_NAK 0x0A 368 #define USB_PID_STALL 0x0E 369 #define USB_PID_DATA0 0x03 370 #define USB_PID_DATA1 0x0B 371 #define USB_PID_PRE 0x0C 372 #endif 373 374 /* USB standard device request code */ 375 #ifndef USB_GET_DESCRIPTOR 376 #define USB_GET_STATUS 0x00 377 #define USB_CLEAR_FEATURE 0x01 378 #define USB_SET_FEATURE 0x03 379 #define USB_SET_ADDRESS 0x05 380 #define USB_GET_DESCRIPTOR 0x06 381 #define USB_SET_DESCRIPTOR 0x07 382 #define USB_GET_CONFIGURATION 0x08 383 #define USB_SET_CONFIGURATION 0x09 384 #define USB_GET_INTERFACE 0x0A 385 #define USB_SET_INTERFACE 0x0B 386 #define USB_SYNCH_FRAME 0x0C 387 #endif 388 389 /* USB hub class request code */ 390 #ifndef HUB_GET_DESCRIPTOR 391 #define HUB_GET_STATUS 0x00 392 #define HUB_CLEAR_FEATURE 0x01 393 #define HUB_GET_STATE 0x02 394 #define HUB_SET_FEATURE 0x03 395 #define HUB_GET_DESCRIPTOR 0x06 396 #define HUB_SET_DESCRIPTOR 0x07 397 #endif 398 399 /* USB HID class request code */ 400 #ifndef HID_GET_REPORT 401 #define HID_GET_REPORT 0x01 402 #define HID_GET_IDLE 0x02 403 #define HID_GET_PROTOCOL 0x03 404 #define HID_SET_REPORT 0x09 405 #define HID_SET_IDLE 0x0A 406 #define HID_SET_PROTOCOL 0x0B 407 #endif 408 409 /* Bit define for USB request type */ 410 #ifndef USB_REQ_TYP_MASK 411 #define USB_REQ_TYP_IN 0x80 /* control IN, device to host */ 412 #define USB_REQ_TYP_OUT 0x00 /* control OUT, host to device */ 413 #define USB_REQ_TYP_READ 0x80 /* control read, device to host */ 414 #define USB_REQ_TYP_WRITE 0x00 /* control write, host to device */ 415 #define USB_REQ_TYP_MASK 0x60 /* bit mask of request type */ 416 #define USB_REQ_TYP_STANDARD 0x00 417 #define USB_REQ_TYP_CLASS 0x20 418 #define USB_REQ_TYP_VENDOR 0x40 419 #define USB_REQ_TYP_RESERVED 0x60 420 #define USB_REQ_RECIP_MASK 0x1F /* bit mask of request recipient */ 421 #define USB_REQ_RECIP_DEVICE 0x00 422 #define USB_REQ_RECIP_INTERF 0x01 423 #define USB_REQ_RECIP_ENDP 0x02 424 #define USB_REQ_RECIP_OTHER 0x03 425 #endif 426 427 /* USB request type for hub class request */ 428 #ifndef HUB_GET_HUB_DESCRIPTOR 429 #define HUB_CLEAR_HUB_FEATURE 0x20 430 #define HUB_CLEAR_PORT_FEATURE 0x23 431 #define HUB_GET_BUS_STATE 0xA3 432 #define HUB_GET_HUB_DESCRIPTOR 0xA0 433 #define HUB_GET_HUB_STATUS 0xA0 434 #define HUB_GET_PORT_STATUS 0xA3 435 #define HUB_SET_HUB_DESCRIPTOR 0x20 436 #define HUB_SET_HUB_FEATURE 0x20 437 #define HUB_SET_PORT_FEATURE 0x23 438 #endif 439 440 /* Hub class feature selectors */ 441 #ifndef HUB_PORT_RESET 442 #define HUB_C_HUB_LOCAL_POWER 0 443 #define HUB_C_HUB_OVER_CURRENT 1 444 #define HUB_PORT_CONNECTION 0 445 #define HUB_PORT_ENABLE 1 446 #define HUB_PORT_SUSPEND 2 447 #define HUB_PORT_OVER_CURRENT 3 448 #define HUB_PORT_RESET 4 449 #define HUB_PORT_POWER 8 450 #define HUB_PORT_LOW_SPEED 9 451 #define HUB_C_PORT_CONNECTION 16 452 #define HUB_C_PORT_ENABLE 17 453 #define HUB_C_PORT_SUSPEND 18 454 #define HUB_C_PORT_OVER_CURRENT 19 455 #define HUB_C_PORT_RESET 20 456 #endif 457 458 /* USB descriptor type */ 459 #ifndef USB_DESCR_TYP_DEVICE 460 #define USB_DESCR_TYP_DEVICE 0x01 461 #define USB_DESCR_TYP_CONFIG 0x02 462 #define USB_DESCR_TYP_STRING 0x03 463 #define USB_DESCR_TYP_INTERF 0x04 464 #define USB_DESCR_TYP_ENDP 0x05 465 #define USB_DESCR_TYP_QUALIF 0x06 466 #define USB_DESCR_TYP_SPEED 0x07 467 #define USB_DESCR_TYP_OTG 0x09 468 #define USB_DESCR_TYP_HID 0x21 469 #define USB_DESCR_TYP_REPORT 0x22 470 #define USB_DESCR_TYP_PHYSIC 0x23 471 #define USB_DESCR_TYP_CS_INTF 0x24 472 #define USB_DESCR_TYP_CS_ENDP 0x25 473 #define USB_DESCR_TYP_HUB 0x29 474 #endif 475 476 /* USB device class */ 477 #ifndef USB_DEV_CLASS_HUB 478 #define USB_DEV_CLASS_RESERVED 0x00 479 #define USB_DEV_CLASS_AUDIO 0x01 480 #define USB_DEV_CLASS_COMMUNIC 0x02 481 #define USB_DEV_CLASS_HID 0x03 482 #define USB_DEV_CLASS_MONITOR 0x04 483 #define USB_DEV_CLASS_PHYSIC_IF 0x05 484 #define USB_DEV_CLASS_POWER 0x06 485 #define USB_DEV_CLASS_PRINTER 0x07 486 #define USB_DEV_CLASS_STORAGE 0x08 487 #define USB_DEV_CLASS_HUB 0x09 488 #define USB_DEV_CLASS_VEN_SPEC 0xFF 489 #endif 490 491 /* USB endpoint type and attributes */ 492 #ifndef USB_ENDP_TYPE_MASK 493 #define USB_ENDP_DIR_MASK 0x80 494 #define USB_ENDP_ADDR_MASK 0x0F 495 #define USB_ENDP_TYPE_MASK 0x03 496 #define USB_ENDP_TYPE_CTRL 0x00 497 #define USB_ENDP_TYPE_ISOCH 0x01 498 #define USB_ENDP_TYPE_BULK 0x02 499 #define USB_ENDP_TYPE_INTER 0x03 500 #endif 501 502 #ifndef USB_DEVICE_ADDR 503 #define USB_DEVICE_ADDR 0x02 504 #endif 505 #ifndef DEFAULT_ENDP0_SIZE 506 #define DEFAULT_ENDP0_SIZE 8 /* default maximum packet size for endpoint 0 */ 507 #endif 508 #ifndef MAX_PACKET_SIZE 509 #define MAX_PACKET_SIZE 64 /* maximum packet size */ 510 #endif 511 #ifndef USB_BO_CBW_SIZE 512 #define USB_BO_CBW_SIZE 0x1F 513 #define USB_BO_CSW_SIZE 0x0D 514 #endif 515 #ifndef USB_BO_CBW_SIG0 516 #define USB_BO_CBW_SIG0 0x55 517 #define USB_BO_CBW_SIG1 0x53 518 #define USB_BO_CBW_SIG2 0x42 519 #define USB_BO_CBW_SIG3 0x43 520 #define USB_BO_CSW_SIG0 0x55 521 #define USB_BO_CSW_SIG1 0x53 522 #define USB_BO_CSW_SIG2 0x42 523 #define USB_BO_CSW_SIG3 0x53 524 #endif 525 526 #ifndef __PACKED 527 #define __PACKED __attribute__((packed)) 528 #endif 529 530 typedef struct __PACKED _USB_SETUP_REQ 531 { 532 UINT8 bRequestType; 533 UINT8 bRequest; 534 UINT16 wValue; 535 UINT16 wIndex; 536 UINT16 wLength; 537 } USB_SETUP_REQ, *PUSB_SETUP_REQ; 538 539 typedef struct __PACKED _USB_DEVICE_DESCR 540 { 541 UINT8 bLength; 542 UINT8 bDescriptorType; 543 UINT16 bcdUSB; 544 UINT8 bDeviceClass; 545 UINT8 bDeviceSubClass; 546 UINT8 bDeviceProtocol; 547 UINT8 bMaxPacketSize0; 548 UINT16 idVendor; 549 UINT16 idProduct; 550 UINT16 bcdDevice; 551 UINT8 iManufacturer; 552 UINT8 iProduct; 553 UINT8 iSerialNumber; 554 UINT8 bNumConfigurations; 555 } USB_DEV_DESCR, *PUSB_DEV_DESCR; 556 557 typedef struct __PACKED _USB_CONFIG_DESCR 558 { 559 UINT8 bLength; 560 UINT8 bDescriptorType; 561 UINT16 wTotalLength; 562 UINT8 bNumInterfaces; 563 UINT8 bConfigurationValue; 564 UINT8 iConfiguration; 565 UINT8 bmAttributes; 566 UINT8 MaxPower; 567 } USB_CFG_DESCR, *PUSB_CFG_DESCR; 568 569 typedef struct __PACKED _USB_INTERF_DESCR 570 { 571 UINT8 bLength; 572 UINT8 bDescriptorType; 573 UINT8 bInterfaceNumber; 574 UINT8 bAlternateSetting; 575 UINT8 bNumEndpoints; 576 UINT8 bInterfaceClass; 577 UINT8 bInterfaceSubClass; 578 UINT8 bInterfaceProtocol; 579 UINT8 iInterface; 580 } USB_ITF_DESCR, *PUSB_ITF_DESCR; 581 582 typedef struct __PACKED _USB_ENDPOINT_DESCR 583 { 584 UINT8 bLength; 585 UINT8 bDescriptorType; 586 UINT8 bEndpointAddress; 587 UINT8 bmAttributes; 588 UINT16 wMaxPacketSize; 589 UINT8 bInterval; 590 } USB_ENDP_DESCR, *PUSB_ENDP_DESCR; 591 592 typedef struct __PACKED _USB_CONFIG_DESCR_LONG 593 { 594 USB_CFG_DESCR cfg_descr; 595 USB_ITF_DESCR itf_descr; 596 USB_ENDP_DESCR endp_descr[1]; 597 } USB_CFG_DESCR_LONG, *PUSB_CFG_DESCR_LONG; 598 599 typedef struct __PACKED _USB_HUB_DESCR 600 { 601 UINT8 bDescLength; 602 UINT8 bDescriptorType; 603 UINT8 bNbrPorts; 604 UINT8 wHubCharacteristicsL; 605 UINT8 wHubCharacteristicsH; 606 UINT8 bPwrOn2PwrGood; 607 UINT8 bHubContrCurrent; 608 UINT8 DeviceRemovable; 609 UINT8 PortPwrCtrlMask; 610 } USB_HUB_DESCR, *PUSB_HUB_DESCR; 611 612 typedef struct __PACKED _USB_HID_DESCR 613 { 614 UINT8 bLength; 615 UINT8 bDescriptorType; 616 UINT16 bcdHID; 617 UINT8 bCountryCode; 618 UINT8 bNumDescriptors; 619 UINT8 bDescriptorTypeX; 620 UINT8 wDescriptorLengthL; 621 UINT8 wDescriptorLengthH; 622 } USB_HID_DESCR, *PUSB_HID_DESCR; 623 624 typedef struct __PACKED _UDISK_BOC_CBW 625 { /* command of BulkOnly USB-FlashDisk */ 626 UINT32 mCBW_Sig; 627 UINT32 mCBW_Tag; 628 UINT32 mCBW_DataLen; /* uppest byte of data length, always is 0 */ 629 UINT8 mCBW_Flag; /* transfer direction and etc. */ 630 UINT8 mCBW_LUN; 631 UINT8 mCBW_CB_Len; /* length of command block */ 632 UINT8 mCBW_CB_Buf[16]; /* command block buffer */ 633 } UDISK_BOC_CBW, *PXUDISK_BOC_CBW; 634 635 typedef struct __PACKED _UDISK_BOC_CSW 636 { /* status of BulkOnly USB-FlashDisk */ 637 UINT32 mCBW_Sig; 638 UINT32 mCBW_Tag; 639 UINT32 mCSW_Residue; /* return: remainder bytes */ /* uppest byte of remainder length, always is 0 */ 640 UINT8 mCSW_Status; /* return: result status */ 641 } UDISK_BOC_CSW, *PXUDISK_BOC_CSW; 642 643 extern PUINT8 pEP0_RAM_Addr; //ep0(64) 644 extern PUINT8 pEP1_RAM_Addr; //ep1_out(64)+ep1_in(64) 645 extern PUINT8 pEP2_RAM_Addr; //ep2_out(64)+ep2_in(64) 646 extern PUINT8 pEP3_RAM_Addr; //ep3_out(64)+ep3_in(64) 647 extern PUINT8 pEP4_RAM_Addr; //ep4_out(64)+ep4_in(64) 648 extern PUINT8 pEP5_RAM_Addr; //ep5_out(64)+ep5_in(64) 649 extern PUINT8 pEP6_RAM_Addr; //ep6_out(64)+ep6_in(64) 650 extern PUINT8 pEP7_RAM_Addr; //ep7_out(64)+ep7_in(64) 651 652 #define pSetupReqPak ((PUSB_SETUP_REQ)pEP0_RAM_Addr) 653 #define pEP0_DataBuf (pEP0_RAM_Addr) 654 #define pEP1_OUT_DataBuf (pEP1_RAM_Addr) 655 #define pEP1_IN_DataBuf (pEP1_RAM_Addr + 64) 656 #define pEP2_OUT_DataBuf (pEP2_RAM_Addr) 657 #define pEP2_IN_DataBuf (pEP2_RAM_Addr + 64) 658 #define pEP3_OUT_DataBuf (pEP3_RAM_Addr) 659 #define pEP3_IN_DataBuf (pEP3_RAM_Addr + 64) 660 #define pEP4_OUT_DataBuf (pEP4_RAM_Addr) 661 #define pEP4_IN_DataBuf (pEP4_RAM_Addr + 64) 662 #define pEP5_OUT_DataBuf (pEP5_RAM_Addr) 663 #define pEP5_IN_DataBuf (pEP5_RAM_Addr + 64) 664 #define pEP6_OUT_DataBuf (pEP6_RAM_Addr) 665 #define pEP6_IN_DataBuf (pEP6_RAM_Addr + 64) 666 #define pEP7_OUT_DataBuf (pEP7_RAM_Addr) 667 #define pEP7_IN_DataBuf (pEP7_RAM_Addr + 64) 668 669 void USB_DeviceInit(void); 670 void USB_DevTransProcess(void); 671 672 void DevEP1_OUT_Deal(UINT8 l); 673 void DevEP2_OUT_Deal(UINT8 l); 674 void DevEP3_OUT_Deal(UINT8 l); 675 void DevEP4_OUT_Deal(UINT8 l); 676 void DevEP5_OUT_Deal(UINT8 l); 677 void DevEP6_OUT_Deal(UINT8 l); 678 void DevEP7_OUT_Deal(UINT8 l); 679 680 void DevEP1_IN_Deal(UINT8 l); 681 void DevEP2_IN_Deal(UINT8 l); 682 void DevEP3_IN_Deal(UINT8 l); 683 void DevEP4_IN_Deal(UINT8 l); 684 void DevEP5_IN_Deal(UINT8 l); 685 void DevEP6_IN_Deal(UINT8 l); 686 void DevEP7_IN_Deal(UINT8 l); 687 688 #define EP1_GetINSta() (R8_UEP1_CTRL & UEP_T_RES_NAK) 689 #define EP2_GetINSta() (R8_UEP2_CTRL & UEP_T_RES_NAK) 690 #define EP3_GetINSta() (R8_UEP3_CTRL & UEP_T_RES_NAK) 691 #define EP4_GetINSta() (R8_UEP4_CTRL & UEP_T_RES_NAK) 692 #define EP5_GetINSta() (R8_UEP5_CTRL & UEP_T_RES_NAK) 693 #define EP6_GetINSta() (R8_UEP6_CTRL & UEP_T_RES_NAK) 694 #define EP7_GetINSta() (R8_UEP7_CTRL & UEP_T_RES_NAK) 695 696 #ifdef __cplusplus 697 } 698 #endif 699 700 #endif // __USB_TYPE__ 701