28#ifndef TUSB_FSDEV_TYPE_H
29#define TUSB_FSDEV_TYPE_H
39#ifndef FSDEV_BTABLE_BASE
40#define FSDEV_BTABLE_BASE 0U
43TU_VERIFY_STATIC(FSDEV_BTABLE_BASE % 8 == 0,
"BTABLE base must be aligned to 8 bytes");
51#if FSDEV_PMA_SIZE == 512
53 #define FSDEV_PMA_STRIDE 2
54 #define pma_access_scheme TU_ATTR_ALIGNED(4)
55#elif FSDEV_PMA_SIZE == 1024
57 #define FSDEV_PMA_STRIDE 1
58 #define pma_access_scheme
59#elif FSDEV_PMA_SIZE == 2048
61 #define FSDEV_BUS_32BIT
62 #define FSDEV_PMA_STRIDE 1
63 #define pma_access_scheme
69 #define fsdevbus_unaligned_read(_addr) tu_unaligned_read32(_addr)
70 #define fsdevbus_unaligned_write(_addr, _value) tu_unaligned_write32(_addr, _value)
73 #define fsdevbus_unaligned_read(_addr) tu_unaligned_read16(_addr)
74 #define fsdevbus_unaligned_write(_addr, _value) tu_unaligned_write16(_addr, _value)
90#define FSDEV_EP_COUNT 8
98 volatile pma_access_scheme uint16_t
addr;
99 volatile pma_access_scheme uint16_t
count;
100 } ep16[FSDEV_EP_COUNT][2];
105 } ep32[FSDEV_EP_COUNT][2];
109TU_VERIFY_STATIC(FSDEV_BTABLE_BASE + FSDEV_EP_COUNT*8 <= FSDEV_PMA_SIZE,
"BTABLE does not fit in PMA RAM");
111#define FSDEV_BTABLE ((volatile fsdev_btable_t*) (FSDEV_PMA_BASE + FSDEV_PMA_STRIDE*(FSDEV_BTABLE_BASE)))
117#define PMA_BUF_AT(_addr) ((fsdev_pma_buf_t*) (FSDEV_PMA_BASE + FSDEV_PMA_STRIDE*(_addr)))
124#define _va32 volatile TU_ATTR_ALIGNED(4)
131 _va32 uint32_t RESERVED7[8];
144#define FSDEV_REG ((fsdev_regs_t*) FSDEV_REG_BASE)
148#define USB_EPTX_STAT 0x0030U
152#define USB_EPRX_STAT 0x3000U
155#ifndef USB_EPTX_STAT_Pos
156#define USB_EPTX_STAT_Pos 4u
159#ifndef USB_EP_DTOG_TX_Pos
160#define USB_EP_DTOG_TX_Pos 6u
163#ifndef USB_EP_CTR_TX_Pos
164#define USB_EP_CTR_TX_Pos 7u
174#define EP_STAT_MASK(_dir) (3u << (USB_EPTX_STAT_Pos + ((_dir) == TUSB_DIR_IN ? 0 : 8)))
175#define EP_DTOG_MASK(_dir) (1u << (USB_EP_DTOG_TX_Pos + ((_dir) == TUSB_DIR_IN ? 0 : 8)))
183TU_ATTR_ALWAYS_INLINE
static inline uint32_t
ep_read(uint32_t ep_id) {
184 return FSDEV_REG->ep[ep_id].reg;
187TU_ATTR_ALWAYS_INLINE
static inline void ep_write(uint32_t ep_id, uint32_t value,
bool need_exclusive) {
188 if (need_exclusive) {
194 if (need_exclusive) {
200 uint32_t reg = FSDEV_REG->ep[ep_id].reg;
201 reg |= USB_EP_CTR_TX | USB_EP_CTR_RX;
202 reg &= USB_EPREG_MASK;
203 reg &= ~(1 << (USB_EP_CTR_TX_Pos + (dir ==
TUSB_DIR_IN ? 0 : 8)));
215TU_ATTR_ALWAYS_INLINE
static inline bool ep_is_iso(uint32_t reg) {
216 return (reg & USB_EP_TYPE_MASK) == USB_EP_ISOCHRONOUS;
223TU_ATTR_ALWAYS_INLINE
static inline uint32_t
btable_get_addr(uint32_t ep_id, uint8_t buf_id) {
224#ifdef FSDEV_BUS_32BIT
225 return FSDEV_BTABLE->ep32[ep_id][buf_id].count_addr & 0x0000FFFFu;
227 return FSDEV_BTABLE->ep16[ep_id][buf_id].addr;
231TU_ATTR_ALWAYS_INLINE
static inline void btable_set_addr(uint32_t ep_id, uint8_t buf_id, uint16_t addr) {
232#ifdef FSDEV_BUS_32BIT
233 uint32_t count_addr = FSDEV_BTABLE->ep32[ep_id][buf_id].count_addr;
234 count_addr = (count_addr & 0xFFFF0000u) | (addr & 0x0000FFFCu);
235 FSDEV_BTABLE->ep32[ep_id][buf_id].count_addr = count_addr;
237 FSDEV_BTABLE->ep16[ep_id][buf_id].addr = addr;
241TU_ATTR_ALWAYS_INLINE
static inline uint16_t
btable_get_count(uint32_t ep_id, uint8_t buf_id) {
243#ifdef FSDEV_BUS_32BIT
244 count = (FSDEV_BTABLE->ep32[ep_id][buf_id].count_addr >> 16);
246 count = FSDEV_BTABLE->ep16[ep_id][buf_id].count;
248 return count & 0x3FFU;
251TU_ATTR_ALWAYS_INLINE
static inline void btable_set_count(uint32_t ep_id, uint8_t buf_id, uint16_t byte_count) {
252#ifdef FSDEV_BUS_32BIT
253 uint32_t count_addr = FSDEV_BTABLE->ep32[ep_id][buf_id].count_addr;
254 count_addr = (count_addr & ~0x03FF0000u) | ((byte_count & 0x3FFu) << 16);
255 FSDEV_BTABLE->ep32[ep_id][buf_id].count_addr = count_addr;
257 uint16_t cnt = FSDEV_BTABLE->ep16[ep_id][buf_id].count;
258 cnt = (cnt & ~0x3FFU) | (byte_count & 0x3FFU);
259 FSDEV_BTABLE->ep16[ep_id][buf_id].count = cnt;
264TU_ATTR_ALWAYS_INLINE
static inline uint16_t
pma_align_buffer_size(uint16_t size, uint8_t* blsize, uint8_t* num_block) {
267 uint16_t block_in_bytes;
278 return (*num_block) * block_in_bytes;
282 uint8_t blsize, num_block;
286 uint16_t bl_nb = (blsize << 15) | ((num_block - blsize) << 10);
293#ifdef FSDEV_BUS_32BIT
294 uint32_t count_addr = FSDEV_BTABLE->ep32[ep_id][buf_id].count_addr;
295 count_addr = (bl_nb << 16) | (count_addr & 0x0000FFFFu);
296 FSDEV_BTABLE->ep32[ep_id][buf_id].count_addr = count_addr;
298 FSDEV_BTABLE->ep16[ep_id][buf_id].count = bl_nb;
void dcd_int_disable(uint8_t rhport)
void dcd_int_enable(uint8_t rhport)
static TU_ATTR_ALWAYS_INLINE uint16_t pma_align_buffer_size(uint16_t size, uint8_t *blsize, uint8_t *num_block)
static TU_ATTR_ALWAYS_INLINE void btable_set_count(uint32_t ep_id, uint8_t buf_id, uint16_t byte_count)
static TU_ATTR_ALWAYS_INLINE void ep_change_status(uint32_t *reg, tusb_dir_t dir, ep_stat_t state)
static TU_ATTR_ALWAYS_INLINE void ep_write(uint32_t ep_id, uint32_t value, bool need_exclusive)
static TU_ATTR_ALWAYS_INLINE void btable_set_addr(uint32_t ep_id, uint8_t buf_id, uint16_t addr)
static TU_ATTR_ALWAYS_INLINE void btable_set_rx_bufsize(uint32_t ep_id, uint8_t buf_id, uint16_t wCount)
static TU_ATTR_ALWAYS_INLINE uint32_t btable_get_addr(uint32_t ep_id, uint8_t buf_id)
TU_VERIFY_STATIC(sizeof(fsdev_btable_t)==FSDEV_EP_COUNT *8 *FSDEV_PMA_STRIDE, "size is not correct")
static TU_ATTR_ALWAYS_INLINE void ep_change_dtog(uint32_t *reg, tusb_dir_t dir, uint8_t state)
static TU_ATTR_ALWAYS_INLINE void ep_write_clear_ctr(uint32_t ep_id, tusb_dir_t dir)
static TU_ATTR_ALWAYS_INLINE uint32_t ep_read(uint32_t ep_id)
static TU_ATTR_ALWAYS_INLINE uint16_t btable_get_count(uint32_t ep_id, uint8_t buf_id)
static TU_ATTR_ALWAYS_INLINE bool ep_is_iso(uint32_t reg)
volatile pma_access_scheme fsdev_bus_t value
static TU_ATTR_ALWAYS_INLINE uint32_t tu_div_ceil(uint32_t v, uint32_t d)
volatile uint32_t count_addr
volatile pma_access_scheme uint16_t addr
volatile pma_access_scheme uint16_t count