BeRTOS
|
00001 00040 #ifndef USB_STM32_H 00041 #define USB_STM32_H 00042 00043 #include <cfg/compiler.h> 00044 #include <drv/usb.h> 00045 #include <drv/usb_endpoint.h> 00046 00047 #define USB_BASE_ADDR 0x40005C00 00048 00049 #define USB_DM_PIN (1 << 11) 00050 #define USB_DP_PIN (1 << 12) 00051 #define USB_DISC_PIN (1 << 11) 00052 00053 #define USB_EP0_MAX_SIZE CONFIG_EP0_MAX_SIZE 00054 #define USB_XFER_MAX_SIZE 64 00055 00056 #define EP_MAX_SLOTS USB_EP_MAX 00057 #define EP_MAX_NUM (EP_MAX_SLOTS << 1) 00058 00059 /* USB packet memory organization */ 00060 #define USB_PACKET_MEMORY_BASE 0x40006000 00061 #define USB_PACKET_MEMORY_SIZE 512 00062 00063 /* Offset of the buffer descriptor table inside the packet memory */ 00064 #define USB_BDT_OFFSET \ 00065 ((USB_PACKET_MEMORY_SIZE - (sizeof(stm32_UsbBd) * EP_MAX_NUM)) & ~7) 00066 00067 #define USB_MEM_ADDR(offset) \ 00068 (USB_PACKET_MEMORY_BASE + ((offset << 1) & ~3) + (offset & 1)) 00069 00070 #define EP_DTB_READ(slot, offset) \ 00071 (*((uint16_t *)(USB_MEM_ADDR((USB_BDT_OFFSET + \ 00072 (slot) * sizeof(stm32_UsbBd) + \ 00073 (offset)))))) 00074 00075 #define EP_DTB_WRITE(slot, offset, data) (EP_DTB_READ(slot, offset) = data) 00076 00077 #define ADDR_TX_OFFSET offsetof(stm32_UsbBd, AddrTx) 00078 #define COUNT_TX_OFFSET offsetof(stm32_UsbBd, CountTx) 00079 #define ADDR_RX_OFFSET offsetof(stm32_UsbBd, AddrRx) 00080 #define COUNT_RX_OFFSET offsetof(stm32_UsbBd, CountRx) 00081 00082 #define USB_CTRL_RW_MASK 0x070F 00083 #define USB_CTRL_CLEAR_ONLY_MASK 0x8080 00084 #define USB_CTRL_TOGGLE_MASK 0x7070 00085 00086 /* CNTR register flags */ 00087 #define bmCTRM 0x8000 00088 #define bmPMAOVRM 0x4000 00089 #define bmERRM 0x2000 00090 #define bmWKUPM 0x1000 00091 #define bmSUSPM 0x0800 00092 #define bmRESETM 0x0400 00093 #define bmSOFM 0x0200 00094 #define bmESOFM 0x0100 00095 00096 #define bmRESUME 0x0010 00097 #define bmFSUSP 0x0008 00098 #define bmLPMODE 0x0004 00099 #define bmPDWN 0x0002 00100 #define bmFRES 0x0001 00101 00102 /* USB error codes */ 00103 enum stm32_usb_error 00104 { 00105 USB_OK = 0, 00106 USB_INTR_ERROR, 00107 USB_INVAL_ERROR, 00108 USB_NODEV_ERROR, 00109 USB_MEMORY_FULL, 00110 USB_BUF_OVERFLOW, 00111 USB_EP_STALLED, 00112 USB_FATAL_ERROR, 00113 }; 00114 00115 /* STM32 USB endpoint types */ 00116 enum stm32_UsbEpype 00117 { 00118 EP_BULK = 0, 00119 EP_CTRL, 00120 EP_ISO, 00121 EP_INTERRUPT, 00122 00123 EP_TYPE_MAX 00124 }; 00125 00126 /* STM32 USB interrupt status register bits */ 00127 typedef union 00128 { 00129 uint32_t status; 00130 struct { 00131 uint8_t EP_ID : 4; 00132 uint8_t DIR : 1; 00133 uint8_t : 2; 00134 uint8_t SZDPR : 1; 00135 uint8_t ESOF : 1; 00136 uint8_t SOF : 1; 00137 uint8_t RESET : 1; 00138 uint8_t SUSP : 1; 00139 uint8_t WKUP : 1; 00140 uint8_t ERR : 1; 00141 uint8_t PMAOVR : 1; 00142 uint8_t CTR : 1; 00143 }; 00144 } PACKED stm32_usb_irq_status_t; 00145 00146 /* Endpoint state */ 00147 typedef enum 00148 { 00149 EP_DISABLED = 0, 00150 EP_STALL, 00151 EP_NAK, 00152 EP_VALID 00153 } stm32_UsbEpState; 00154 00155 /* STM32 USB supported endpoints */ 00156 typedef enum stm32_UsbEP 00157 { 00158 CTRL_ENP_OUT = 0, CTRL_ENP_IN, 00159 ENP1_OUT, ENP1_IN, 00160 ENP2_OUT, ENP2_IN, 00161 ENP3_OUT, ENP3_IN, 00162 ENP4_OUT, ENP4_IN, 00163 ENP5_OUT, ENP5_IN, 00164 ENP6_OUT, ENP6_IN, 00165 ENP7_OUT, ENP7_IN, 00166 ENP8_OUT, ENP8_IN, 00167 ENP9_OUT, ENP9_IN, 00168 ENP10_OUT, ENP10_IN, 00169 ENP11_OUT, ENP11_IN, 00170 ENP12_OUT, ENP12_IN, 00171 ENP13_OUT, ENP13_IN, 00172 ENP14_OUT, ENP14_IN, 00173 ENP15_OUT, ENP15_IN, 00174 00175 EP_MAX_HW_NUM 00176 } stm32_UsbEP; 00177 00178 /* STM32 USB packet memory slot */ 00179 typedef struct stm32_UsbMemSlot 00180 { 00181 stm32_UsbEP ep_addr; 00182 uint16_t Start; 00183 uint16_t Size; 00184 struct stm32_UsbMemSlot *next; 00185 } stm32_UsbMemSlot; 00186 00187 /* STM32 USB buffer descriptor (packet memory) */ 00188 typedef struct stm32_UsbBd 00189 { 00190 uint16_t AddrTx; 00191 uint16_t CountTx; 00192 uint16_t AddrRx; 00193 uint16_t CountRx; 00194 } PACKED stm32_UsbBd; 00195 00196 /* STM32 USB endpoint I/O status */ 00197 typedef enum stm32_UsbIoStatus 00198 { 00199 NOT_READY = 0, 00200 NO_SERVICED, 00201 BEGIN_SERVICED, 00202 COMPLETE, 00203 BUFFER_UNDERRUN, 00204 BUFFER_OVERRUN, 00205 SETUP_OVERWRITE, 00206 STALLED, 00207 } stm32_UsbIoStatus; 00208 00209 /* STM32 USB hardware endpoint descriptor */ 00210 typedef struct stm32_UsbEp 00211 { 00212 reg32_t *hw; 00213 uint8_t type; 00214 void (*complete)(int); 00215 ssize_t max_size; 00216 ssize_t offset; 00217 ssize_t size; 00218 stm32_UsbIoStatus status; 00219 union 00220 { 00221 uint8_t *read_buffer; 00222 const uint8_t *write_buffer; 00223 }; 00224 int32_t avail_data; 00225 uint8_t flags; 00226 } stm32_UsbEp; 00227 00228 /* STM32 USB hardware endpoint flags */ 00229 #define STM32_USB_EP_AVAIL_DATA BV(0) 00230 #define STM32_USB_EP_ZERO_PACKET BV(1) 00231 #define STM32_USB_EP_ZERO_POSSIBLE BV(2) 00232 00233 #endif /* USB_STM32_H */