|
| 1 | +/********************************** (C) COPYRIGHT ******************************* |
| 2 | +* File Name : CH56x_usb30_lib.h |
| 3 | +* Author : WCH |
| 4 | +* Version : V1.1 |
| 5 | +* Date : 2020/12/23 |
| 6 | +* Description : |
| 7 | +*******************************************************************************/ |
| 8 | +#ifndef USB30_CH56X_USB30_LIB_H_ |
| 9 | +#define USB30_CH56X_USB30_LIB_H_ |
| 10 | + |
| 11 | +#ifdef __cplusplus |
| 12 | +extern "C" { |
| 13 | +#endif |
| 14 | + |
| 15 | +#include "CH56x_common.h" |
| 16 | + |
| 17 | +// link CFG |
| 18 | +#define TERM_EN (1<<1) |
| 19 | +#define PIPE_RESET (1<<3) |
| 20 | +#define LFPS_RX_PD (1<<5) |
| 21 | +#define CFG_EQ_EN (1<<6) |
| 22 | +#define DEEMPH_CFG (1<<8) |
| 23 | + |
| 24 | +#define POWER_MODE_0 ((UINT32)0x00000000) |
| 25 | +#define POWER_MODE_1 ((UINT32)0x00000001) |
| 26 | +#define POWER_MODE_2 ((UINT32)0x00000002) |
| 27 | +#define POWER_MODE_3 ((UINT32)0x00000003) |
| 28 | + |
| 29 | +#define LINK_PRESENT (1<<0) |
| 30 | +#define RX_WARM_RESET ((UINT32)1<<1) |
| 31 | + |
| 32 | +#define LINK_TXEQ (1<<6) |
| 33 | +#define GO_DISABLED (1<<4) |
| 34 | +#define POLLING_EN (1<<12) |
| 35 | + |
| 36 | +#define TX_HOT_RESET ((UINT32)1<<16) |
| 37 | +#define RX_HOT_RESET ((UINT32)1<<24) |
| 38 | + |
| 39 | +#define TX_WARM_RESET ((UINT32)1<<8) |
| 40 | +#define TX_Ux_EXIT ((UINT32)1<<9) |
| 41 | +// link int flag |
| 42 | +#define LINK_RDY_FLAG (1<<0) |
| 43 | +#define LINK_RECOV_FLAG (1<<1) |
| 44 | +#define LINK_INACT_FLAG (1<<2) |
| 45 | +#define LINK_DISABLE_FLAG (1<<3) |
| 46 | +#define LINK_GO_U3_FLAG (1<<4) |
| 47 | +#define LINK_GO_U2_FLAG (1<<5) |
| 48 | +#define LINK_GO_U1_FLAG (1<<6) |
| 49 | +#define LINK_GO_U0_FLAG (1<<7) |
| 50 | +#define LINK_U3_WAKE_FLAG (1<<8) |
| 51 | +#define LINK_Ux_REJECT_FLAG (1<<9) |
| 52 | +#define TERM_PRESENT_FLAG (1<<10) |
| 53 | +#define LINK_TXEQ_FLAG (1<<11) |
| 54 | +#define LINK_Ux_EXIT_FLAG (1<<12) |
| 55 | +#define WARM_RESET_FLAG (1<<13) |
| 56 | +#define U3_WAKEUP_FLAG (1<<14) |
| 57 | +#define HOT_RESET_FLAG (1<<15) |
| 58 | +#define LINK_RX_DET_FLAG (1<<20) |
| 59 | + |
| 60 | +#define EP0_R_EN (1<<0) |
| 61 | +#define EP1_R_EN (1<<1) |
| 62 | +#define EP2_R_EN (1<<2) |
| 63 | +#define EP3_R_EN (1<<3) |
| 64 | +#define EP4_R_EN (1<<4) |
| 65 | +#define EP5_R_EN (1<<5) |
| 66 | +#define EP6_R_EN (1<<6) |
| 67 | +#define EP7_R_EN (1<<7) |
| 68 | + |
| 69 | +#define EP0_T_EN (1<<8) |
| 70 | +#define EP1_T_EN (1<<9) |
| 71 | +#define EP2_T_EN (1<<10) |
| 72 | +#define EP3_T_EN (1<<11) |
| 73 | +#define EP4_T_EN (1<<12) |
| 74 | +#define EP5_T_EN (1<<13) |
| 75 | +#define EP6_T_EN (1<<14) |
| 76 | +#define EP7_T_EN (1<<15) |
| 77 | +// LMP |
| 78 | +#define LMP_HP 0 |
| 79 | +#define LMP_SUBTYPE_MASK (0xf<<5) |
| 80 | +#define SET_LINK_FUNC (0x1<<5) |
| 81 | +#define U2_INACT_TOUT (0x2<<5) |
| 82 | +#define VENDOR_TEST (0x3<<5) |
| 83 | +#define PORT_CAP (0x4<<5) |
| 84 | +#define PORT_CFG (0x5<<5) |
| 85 | +#define PORT_CFG_RES (0x6<<5) |
| 86 | + |
| 87 | +#define LINK_SPEED (1<<9) |
| 88 | + |
| 89 | +#define NUM_HP_BUF (4<<0) |
| 90 | +#define DOWN_STREAM (1<<16) |
| 91 | +#define UP_STREAM (2<<16) |
| 92 | +#define TIE_BRK (1<<20) |
| 93 | + |
| 94 | +// status response |
| 95 | +#define NRDY 0 |
| 96 | +#define ACK 0x01 |
| 97 | +#define STALL 0x02 |
| 98 | +#define INVALID 0x03 |
| 99 | + |
| 100 | +#define USB_ALL_CLR (1<<1) |
| 101 | +#define USB_FORCE_RST (1<<2) |
| 102 | + |
| 103 | + |
| 104 | +#define SS_RX_CONTRL(ep) (&USBSS->UEP0_RX_CTRL)[ep*4] |
| 105 | +#define SS_TX_CONTRL(ep) (&USBSS->UEP0_TX_CTRL)[ep*4] |
| 106 | + |
| 107 | +static int USB30_Device_Open(void) |
| 108 | +{ |
| 109 | + USBSS->LINK_CFG = 0x140; |
| 110 | + USBSS->LINK_CTRL = 0x12; |
| 111 | + uint32_t t = 0x4c4b41; |
| 112 | + while(USBSS->LINK_STATUS&4){ |
| 113 | + t--; |
| 114 | + if(t == 0)return -1; |
| 115 | + } |
| 116 | + for(int i =0;i<8;i++){ |
| 117 | + SS_TX_CONTRL(i) = 0; |
| 118 | + SS_RX_CONTRL(i) = 0; |
| 119 | + } |
| 120 | + USBSS->USB_STATUS = 0x13; |
| 121 | + |
| 122 | + USBSS->USB_CONTROL = 0x30021; |
| 123 | + USBSS->UEP_CFG = 0; |
| 124 | + |
| 125 | + USBSS->LINK_CFG |= 2; |
| 126 | + |
| 127 | + USBSS->LINK_INT_CTRL = 0x10bc7d; |
| 128 | + |
| 129 | + USBSS->LINK_CTRL = 2; |
| 130 | + return 0; |
| 131 | +} |
| 132 | + |
| 133 | +static inline void USB30_Switch_Powermode( UINT8 pwr_mode ) |
| 134 | +{ |
| 135 | + USBSS->LINK_CTRL &= 0xfffffffc; |
| 136 | + USBSS->LINK_CTRL |= pwr_mode; |
| 137 | + while(USBSS->LINK_STATUS & 4); |
| 138 | +} |
| 139 | + |
| 140 | +static int USB30_Device_Close(void) |
| 141 | +{ |
| 142 | + USB30_Switch_Powermode(POWER_MODE_3); |
| 143 | + USBSS->LINK_CFG = PIPE_RESET | LFPS_RX_PD; |
| 144 | + USBSS->LINK_CTRL = GO_DISABLED | POWER_MODE_3; |
| 145 | + USBSS->LINK_INT_CTRL = 0; |
| 146 | + USBSS->USB_CONTROL = USB_FORCE_RST | USB_ALL_CLR; |
| 147 | + return 0; |
| 148 | +} |
| 149 | + |
| 150 | +static inline void USB30_BUS_RESET( ){ |
| 151 | + USB30_Device_Close(); |
| 152 | + mDelaymS(30); |
| 153 | + USB30_Device_Open(); |
| 154 | +} |
| 155 | + |
| 156 | +static inline void USB30_OUT_Set(UINT8 endp,UINT8 status,UINT8 nump) |
| 157 | +{ |
| 158 | + UINT32V* p = &USBSS->UEP0_RX_CTRL; |
| 159 | + p+= endp*4; |
| 160 | + *p = *p | ((nump)<<16) | (status << 26); |
| 161 | +} |
| 162 | + |
| 163 | +static inline void USB30_Device_Setaddress( UINT32 address ) |
| 164 | +{ |
| 165 | + USBSS->USB_CONTROL &= 0x00ffffff; |
| 166 | + USBSS->USB_CONTROL |= address<<24; |
| 167 | +} |
| 168 | + |
| 169 | +static inline uint8_t USB30_IN_Nump(UINT8 endp) |
| 170 | +{ |
| 171 | + UINT32V* p = &USBSS->UEP0_TX_CTRL; |
| 172 | + p+= endp*4; |
| 173 | + return ((*p)>>16) & 0x1f; |
| 174 | +} |
| 175 | + |
| 176 | +static inline void USB30_EP0_IN_set(UINT8 status,UINT8 nump,UINT16 TxLen, uint8_t toggle) |
| 177 | +{ |
| 178 | + USBSS->UEP0_TX_CTRL |= ((nump<<16) | (status<<26) | (TxLen & 0x3ff) | (toggle << 31)); |
| 179 | +} |
| 180 | + |
| 181 | +static inline void USB30_IN_Set(UINT8 endp,FunctionalState lpf,UINT8 status,UINT8 nump,UINT16 TxLen) |
| 182 | +{ |
| 183 | + UINT32V* p = &USBSS->UEP0_TX_CTRL; |
| 184 | + p+= endp*4; |
| 185 | + *p = *p | (nump<<16) | (status<<26) | (TxLen & 0x3ff) | (lpf << 28); |
| 186 | +} |
| 187 | + |
| 188 | +static inline void USB30_Send_ERDY(UINT8 endp,UINT8 nump) |
| 189 | +{ |
| 190 | + UINT32 t = endp & 0xf; |
| 191 | + t = (t << 2) | ((UINT32)nump << 6); |
| 192 | + if( (endp&0x80) == 0){ |
| 193 | + USBSS->USB_FC_CTRL = t | (UINT32)1; |
| 194 | + return; |
| 195 | + }else{ |
| 196 | + USBSS->USB_FC_CTRL = t | (UINT32)3; |
| 197 | + return; |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +static inline void USB30_OUT_ClearIT(UINT8 endp) |
| 202 | +{ |
| 203 | + UINT32V* p = &USBSS->UEP0_RX_CTRL; |
| 204 | + p+= endp*4; |
| 205 | + *p &= 0x03e00000; |
| 206 | + if(endp == 5){ |
| 207 | + uint32_t t = *p; |
| 208 | + *p = t; |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +static inline void USB30_IN_ClearIT(UINT8 endp) |
| 213 | +{ |
| 214 | + UINT32V* p = &USBSS->UEP0_TX_CTRL; |
| 215 | + p+= endp*4; |
| 216 | + *p &= 0x03e00000; |
| 217 | +} |
| 218 | + |
| 219 | +static inline void USB30_OUT_Status(UINT8 endp,UINT8 *nump,UINT16 *len,UINT8 *status) |
| 220 | +{ |
| 221 | + UINT32V* p = &USBSS->UEP0_RX_CTRL; |
| 222 | + p+= endp*4; |
| 223 | + uint32_t t = *p; |
| 224 | + *len = t; |
| 225 | + *nump = (t >>16) & 31; |
| 226 | + *status = (t>>28) & 7; |
| 227 | +} |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | + |
| 232 | +#ifdef __cplusplus |
| 233 | +} |
| 234 | +#endif |
| 235 | + |
| 236 | +#endif /* USB30_CH56X_USB30_LIB_H_ */ |
0 commit comments