35#ifndef PORTABLE_ST_STM32F0_DCD_STM32F0_FSDEV_PVT_ST_H_
36#define PORTABLE_ST_STM32F0_DCD_STM32F0_FSDEV_PVT_ST_H_
39#ifdef __PROJECT_MIKROSDK_MIKROE__
41#include "interrupts.h"
44#if CFG_TUSB_MCU == OPT_MCU_STM32F0
46 #ifndef __PROJECT_MIKROSDK_MIKROE__
47 #include "stm32f0xx.h"
49 #define PMA_LENGTH (1024u)
55#elif CFG_TUSB_MCU == OPT_MCU_STM32F1
57 #ifndef __PROJECT_MIKROSDK_MIKROE__
60 #define PMA_LENGTH (512u)
65 #define USB_CNTR_LPMODE USB_CNTR_LP_MODE
69#elif defined(STM32F302xB) || defined(STM32F302xC) || \
70 defined(STM32F303xB) || defined(STM32F303xC) || \
71 defined(STM32F373xB) || defined(STM32F373xC)
72 #ifndef __PROJECT_MIKROSDK_MIKROE__
73 #include "stm32f3xx.h"
75 #define PMA_LENGTH (512u)
82#elif defined(STM32F302x6) || defined(STM32F302x8) || \
83 defined(STM32F302xD) || defined(STM32F302xE) || \
84 defined(STM32F303xD) || defined(STM32F303xE) || \
86 #ifndef __PROJECT_MIKROSDK_MIKROE__
87 #include "stm32f3xx.h"
89 #define PMA_LENGTH (1024u)
94#elif CFG_TUSB_MCU == OPT_MCU_STM32L0
96 #ifndef __PROJECT_MIKROSDK_MIKROE__
97 #include "stm32l0xx.h"
99 #define PMA_LENGTH (1024u)
101#elif CFG_TUSB_MCU == OPT_MCU_STM32L1
103 #ifndef __PROJECT_MIKROSDK_MIKROE__
104 #include "stm32l1xx.h"
106 #define PMA_LENGTH (512u)
108#elif CFG_TUSB_MCU == OPT_MCU_STM32G4
109 #include "stm32g4xx.h"
110 #define PMA_LENGTH (1024u)
112#elif CFG_TUSB_MCU == OPT_MCU_STM32G0
114 #ifndef __PROJECT_MIKROSDK_MIKROE__
115 #include "stm32g0xx.h"
117 #define PMA_32BIT_ACCESS
118 #define PMA_LENGTH (2048u)
120 #define USB_PMAADDR USB_DRD_PMAADDR
121 #define USB_TypeDef USB_DRD_TypeDef
123 #define USB_EP_CTR_RX USB_EP_VTRX
124 #define USB_EP_CTR_TX USB_EP_VTTX
125 #define USB_EP_T_FIELD USB_CHEP_UTYPE
126 #define USB_EPREG_MASK USB_CHEP_REG_MASK
127 #define USB_EPTX_DTOGMASK USB_CHEP_TX_DTOGMASK
128 #define USB_EPRX_DTOGMASK USB_CHEP_RX_DTOGMASK
129 #define USB_EPTX_DTOG1 USB_CHEP_TX_DTOG1
130 #define USB_EPTX_DTOG2 USB_CHEP_TX_DTOG2
131 #define USB_EPRX_DTOG1 USB_CHEP_RX_DTOG1
132 #define USB_EPRX_DTOG2 USB_CHEP_RX_DTOG2
133 #define USB_EPRX_STAT USB_CH_RX_VALID
134 #define USB_EPKIND_MASK USB_EP_KIND_MASK
135 #define USB USB_DRD_FS
136 #define USB_CNTR_FRES USB_CNTR_USBRST
137 #define USB_CNTR_RESUME USB_CNTR_L2RES
138 #define USB_ISTR_EP_ID USB_ISTR_IDN
139 #define USB_EPADDR_FIELD USB_CHEP_ADDR
140 #define USB_CNTR_LPMODE USB_CNTR_SUSPRDY
141 #define USB_CNTR_FSUSP USB_CNTR_SUSPEN
143#elif CFG_TUSB_MCU == OPT_MCU_STM32WB
144 #include "stm32wbxx.h"
145 #define PMA_LENGTH (1024u)
148 #define USB_PMAADDR USB1_PMAADDR
150#elif CFG_TUSB_MCU == OPT_MCU_STM32L4
152 #ifndef __PROJECT_MIKROSDK_MIKROE__
153 #include "stm32l4xx.h"
155 #define PMA_LENGTH (1024u)
157#elif CFG_TUSB_MCU == OPT_MCU_STM32L5
158 #include "stm32l5xx.h"
159 #define PMA_LENGTH (1024u)
162 #define USB_PMAADDR (USB_BASE + (USB_PMAADDR_NS - USB_BASE_NS))
166 #error You are using an untested or unimplemented STM32 variant. Please update the driver.
171#if ((PMA_LENGTH) == 512u)
172 #define PMA_STRIDE (2u)
173#elif ((PMA_LENGTH) == 1024u)
174 #define PMA_STRIDE (1u)
179#ifdef PMA_32BIT_ACCESS
180static __IO uint32_t *
const pma32 = (
__IO uint32_t*)USB_PMAADDR;
183static __IO uint16_t *
const pma = (
__IO uint16_t*)USB_PMAADDR;
185TU_ATTR_ALWAYS_INLINE
static inline __IO uint16_t * pcd_btable_word_ptr(USB_TypeDef * USBx,
size_t x)
187 size_t total_word_offset = (((USBx)->BTABLE)>>1) + x;
188 total_word_offset *= PMA_STRIDE;
189 return &(pma[total_word_offset]);
192TU_ATTR_ALWAYS_INLINE
static inline __IO uint16_t* pcd_ep_tx_cnt_ptr(USB_TypeDef * USBx, uint32_t bEpIdx)
194 return pcd_btable_word_ptr(USBx,(bEpIdx)*4u + 1u);
197TU_ATTR_ALWAYS_INLINE
static inline __IO uint16_t* pcd_ep_rx_cnt_ptr(USB_TypeDef * USBx, uint32_t bEpIdx)
199 return pcd_btable_word_ptr(USBx,(bEpIdx)*4u + 3u);
204TU_ATTR_ALWAYS_INLINE
static inline uint16_t pcd_aligned_buffer_size(uint16_t size)
208 uint16_t blocksize = (size > 62) ? 32 : 2;
211 uint16_t numblocks = (size + blocksize - 1) / blocksize ;
213 return numblocks * blocksize;
217TU_ATTR_ALWAYS_INLINE
static inline void pcd_set_endpoint(USB_TypeDef * USBx, uint32_t bEpIdx, uint32_t wRegValue)
219#ifdef PMA_32BIT_ACCESS
221 __O uint32_t *reg = (
__O uint32_t *)(USB_DRD_BASE + bEpIdx*4);
224 __O uint16_t *reg = (
__O uint16_t *)((&USBx->EP0R) + bEpIdx*2u);
225 *reg = (uint16_t)wRegValue;
230TU_ATTR_ALWAYS_INLINE
static inline uint32_t pcd_get_endpoint(USB_TypeDef * USBx, uint32_t bEpIdx) {
231#ifdef PMA_32BIT_ACCESS
233 __I uint32_t *reg = (
__I uint32_t *)(USB_DRD_BASE + bEpIdx*4);
235 __I uint16_t *reg = (
__I uint16_t *)((&USBx->EP0R) + bEpIdx*2u);
240TU_ATTR_ALWAYS_INLINE
static inline void pcd_set_eptype(USB_TypeDef * USBx, uint32_t bEpIdx, uint32_t wType)
242 uint32_t regVal = pcd_get_endpoint(USBx, bEpIdx);
243 regVal &= (uint32_t)USB_EP_T_MASK;
245 regVal |= USB_EP_CTR_RX | USB_EP_CTR_TX;
246 pcd_set_endpoint(USBx, bEpIdx, regVal);
249TU_ATTR_ALWAYS_INLINE
static inline uint32_t pcd_get_eptype(USB_TypeDef * USBx, uint32_t bEpIdx)
251 uint32_t regVal = pcd_get_endpoint(USBx, bEpIdx);
252 regVal &= USB_EP_T_FIELD;
261TU_ATTR_ALWAYS_INLINE
static inline void pcd_clear_rx_ep_ctr(USB_TypeDef * USBx, uint32_t bEpIdx)
263 uint32_t regVal = pcd_get_endpoint(USBx, bEpIdx);
264 regVal &= USB_EPREG_MASK;
265 regVal &= ~USB_EP_CTR_RX;
266 regVal |= USB_EP_CTR_TX;
267 pcd_set_endpoint(USBx, bEpIdx, regVal);
270TU_ATTR_ALWAYS_INLINE
static inline void pcd_clear_tx_ep_ctr(USB_TypeDef * USBx, uint32_t bEpIdx)
272 uint32_t regVal = pcd_get_endpoint(USBx, bEpIdx);
273 regVal &= USB_EPREG_MASK;
274 regVal &= ~USB_EP_CTR_TX;
275 regVal |= USB_EP_CTR_RX;
276 pcd_set_endpoint(USBx, bEpIdx,regVal);
284TU_ATTR_ALWAYS_INLINE
static inline uint32_t pcd_get_ep_tx_cnt(USB_TypeDef * USBx, uint32_t bEpIdx)
286#ifdef PMA_32BIT_ACCESS
288 return (pma32[2*bEpIdx] & 0x03FF0000) >> 16;
290 __I uint16_t *regPtr = pcd_ep_tx_cnt_ptr(USBx, bEpIdx);
291 return *regPtr & 0x3ffU;
295TU_ATTR_ALWAYS_INLINE
static inline uint32_t pcd_get_ep_rx_cnt(USB_TypeDef * USBx, uint32_t bEpIdx)
297#ifdef PMA_32BIT_ACCESS
299 return (pma32[2*bEpIdx + 1] & 0x03FF0000) >> 16;
301 __I uint16_t *regPtr = pcd_ep_rx_cnt_ptr(USBx, bEpIdx);
302 return *regPtr & 0x3ffU;
313TU_ATTR_ALWAYS_INLINE
static inline void pcd_set_ep_address(USB_TypeDef * USBx, uint32_t bEpIdx, uint32_t bAddr)
315 uint32_t regVal = pcd_get_endpoint(USBx, bEpIdx);
316 regVal &= USB_EPREG_MASK;
318 regVal |= USB_EP_CTR_RX|USB_EP_CTR_TX;
319 pcd_set_endpoint(USBx, bEpIdx,regVal);
322TU_ATTR_ALWAYS_INLINE
static inline uint32_t pcd_get_ep_tx_address(USB_TypeDef * USBx, uint32_t bEpIdx)
324#ifdef PMA_32BIT_ACCESS
326 return pma32[2*bEpIdx] & 0x0000FFFFu ;
328 return *pcd_btable_word_ptr(USBx,(bEpIdx)*4u + 0u);
332TU_ATTR_ALWAYS_INLINE
static inline uint32_t pcd_get_ep_rx_address(USB_TypeDef * USBx, uint32_t bEpIdx)
334#ifdef PMA_32BIT_ACCESS
336 return pma32[2*bEpIdx + 1] & 0x0000FFFFu;
338 return *pcd_btable_word_ptr(USBx,(bEpIdx)*4u + 2u);
342TU_ATTR_ALWAYS_INLINE
static inline void pcd_set_ep_tx_address(USB_TypeDef * USBx, uint32_t bEpIdx, uint32_t addr)
344#ifdef PMA_32BIT_ACCESS
346 pma32[2*bEpIdx] = (pma32[2*bEpIdx] & 0xFFFF0000u) | (addr & 0x0000FFFCu);
348 *pcd_btable_word_ptr(USBx,(bEpIdx)*4u + 0u) = addr;
352TU_ATTR_ALWAYS_INLINE
static inline void pcd_set_ep_rx_address(USB_TypeDef * USBx, uint32_t bEpIdx, uint32_t addr)
354#ifdef PMA_32BIT_ACCESS
356 pma32[2*bEpIdx + 1] = (pma32[2*bEpIdx + 1] & 0xFFFF0000u) | (addr & 0x0000FFFCu);
358 *pcd_btable_word_ptr(USBx,(bEpIdx)*4u + 2u) = addr;
362TU_ATTR_ALWAYS_INLINE
static inline void pcd_set_ep_tx_cnt(USB_TypeDef * USBx, uint32_t bEpIdx, uint32_t wCount)
364#ifdef PMA_32BIT_ACCESS
366 pma32[2*bEpIdx] = (pma32[2*bEpIdx] & ~0x03FF0000u) | ((wCount & 0x3FFu) << 16);
368 __IO uint16_t * reg = pcd_ep_tx_cnt_ptr(USBx, bEpIdx);
369 *reg = (uint16_t) (*reg & (uint16_t) ~0x3FFU) | (wCount & 0x3FFU);
373TU_ATTR_ALWAYS_INLINE
static inline void pcd_set_ep_rx_cnt(USB_TypeDef * USBx, uint32_t bEpIdx, uint32_t wCount)
375#ifdef PMA_32BIT_ACCESS
377 pma32[2*bEpIdx + 1] = (pma32[2*bEpIdx + 1] & ~0x03FF0000u) | ((wCount & 0x3FFu) << 16);
379 __IO uint16_t * reg = pcd_ep_rx_cnt_ptr(USBx, bEpIdx);
380 *reg = (uint16_t) (*reg & (uint16_t) ~0x3FFU) | (wCount & 0x3FFU);
384TU_ATTR_ALWAYS_INLINE
static inline void pcd_set_ep_blsize_num_blocks(USB_TypeDef * USBx, uint32_t rxtx_idx, uint32_t blocksize, uint32_t numblocks)
387#ifdef PMA_32BIT_ACCESS
389 pma32[rxtx_idx] = (pma32[rxtx_idx] & 0x0000FFFFu) | (blocksize << 31) | ((numblocks - blocksize) << 26);
391 __IO uint16_t *pdwReg = pcd_btable_word_ptr(USBx, rxtx_idx*2u + 1u);
392 *pdwReg = (blocksize << 15) | ((numblocks - blocksize) << 10);
396TU_ATTR_ALWAYS_INLINE
static inline void pcd_set_ep_bufsize(USB_TypeDef * USBx, uint32_t rxtx_idx, uint32_t wCount)
398 wCount = pcd_aligned_buffer_size(wCount);
401 uint16_t blocksize = (wCount > 62) ? 1 : 0;
402 uint16_t numblocks = wCount / (blocksize ? 32 : 2);
405 TU_ASSERT((wCount - (numblocks * (blocksize ? 32 : 2))) == 0, );
408 pcd_set_ep_blsize_num_blocks(USBx, rxtx_idx, blocksize, numblocks);
411TU_ATTR_ALWAYS_INLINE
static inline void pcd_set_ep_tx_bufsize(USB_TypeDef * USBx, uint32_t bEpIdx, uint32_t wCount)
413 pcd_set_ep_bufsize(USBx, 2*bEpIdx, wCount);
416TU_ATTR_ALWAYS_INLINE
static inline void pcd_set_ep_rx_bufsize(USB_TypeDef * USBx, uint32_t bEpIdx, uint32_t wCount)
418 pcd_set_ep_bufsize(USBx, 2*bEpIdx + 1, wCount);
428TU_ATTR_ALWAYS_INLINE
static inline void pcd_set_ep_tx_status(USB_TypeDef * USBx, uint32_t bEpIdx, uint32_t wState)
430 uint32_t regVal = pcd_get_endpoint(USBx, bEpIdx);
431 regVal &= USB_EPTX_DTOGMASK;
434 if((USB_EPTX_DTOG1 & (wState))!= 0U)
436 regVal ^= USB_EPTX_DTOG1;
439 if((USB_EPTX_DTOG2 & ((uint32_t)(wState)))!= 0U)
441 regVal ^= USB_EPTX_DTOG2;
444 regVal |= USB_EP_CTR_RX|USB_EP_CTR_TX;
445 pcd_set_endpoint(USBx, bEpIdx, regVal);
456TU_ATTR_ALWAYS_INLINE
static inline void pcd_set_ep_rx_status(USB_TypeDef * USBx, uint32_t bEpIdx, uint32_t wState)
458 uint32_t regVal = pcd_get_endpoint(USBx, bEpIdx);
459 regVal &= USB_EPRX_DTOGMASK;
462 if((USB_EPRX_DTOG1 & wState)!= 0U)
464 regVal ^= USB_EPRX_DTOG1;
467 if((USB_EPRX_DTOG2 & wState)!= 0U)
469 regVal ^= USB_EPRX_DTOG2;
472 regVal |= USB_EP_CTR_RX|USB_EP_CTR_TX;
473 pcd_set_endpoint(USBx, bEpIdx, regVal);
476TU_ATTR_ALWAYS_INLINE
static inline uint32_t pcd_get_ep_rx_status(USB_TypeDef * USBx, uint32_t bEpIdx)
478 uint32_t regVal = pcd_get_endpoint(USBx, bEpIdx);
479 return (regVal & USB_EPRX_STAT) >> (12u);
489TU_ATTR_ALWAYS_INLINE
static inline void pcd_rx_dtog(USB_TypeDef * USBx, uint32_t bEpIdx)
491 uint32_t regVal = pcd_get_endpoint(USBx, bEpIdx);
492 regVal &= USB_EPREG_MASK;
493 regVal |= USB_EP_CTR_RX|USB_EP_CTR_TX|USB_EP_DTOG_RX;
494 pcd_set_endpoint(USBx, bEpIdx, regVal);
497TU_ATTR_ALWAYS_INLINE
static inline void pcd_tx_dtog(USB_TypeDef * USBx, uint32_t bEpIdx)
499 uint32_t regVal = pcd_get_endpoint(USBx, bEpIdx);
500 regVal &= USB_EPREG_MASK;
501 regVal |= USB_EP_CTR_RX|USB_EP_CTR_TX|USB_EP_DTOG_TX;
502 pcd_set_endpoint(USBx, bEpIdx, regVal);
512TU_ATTR_ALWAYS_INLINE
static inline void pcd_clear_rx_dtog(USB_TypeDef * USBx, uint32_t bEpIdx)
514 uint32_t regVal = pcd_get_endpoint(USBx, bEpIdx);
515 if((regVal & USB_EP_DTOG_RX) != 0)
517 pcd_rx_dtog(USBx,bEpIdx);
521TU_ATTR_ALWAYS_INLINE
static inline void pcd_clear_tx_dtog(USB_TypeDef * USBx, uint32_t bEpIdx)
523 uint32_t regVal = pcd_get_endpoint(USBx, bEpIdx);
524 if((regVal & USB_EP_DTOG_TX) != 0)
526 pcd_tx_dtog(USBx,bEpIdx);
537TU_ATTR_ALWAYS_INLINE
static inline void pcd_set_ep_kind(USB_TypeDef * USBx, uint32_t bEpIdx)
539 uint32_t regVal = pcd_get_endpoint(USBx, bEpIdx);
540 regVal |= USB_EP_KIND;
541 regVal &= USB_EPREG_MASK;
542 regVal |= USB_EP_CTR_RX|USB_EP_CTR_TX;
543 pcd_set_endpoint(USBx, bEpIdx, regVal);
545TU_ATTR_ALWAYS_INLINE
static inline void pcd_clear_ep_kind(USB_TypeDef * USBx, uint32_t bEpIdx)
547 uint32_t regVal = pcd_get_endpoint(USBx, bEpIdx);
548 regVal &= USB_EPKIND_MASK;
549 regVal |= USB_EP_CTR_RX|USB_EP_CTR_TX;
550 pcd_set_endpoint(USBx, bEpIdx, regVal);
554#if defined(USB_ISTR_L1REQ)
555#define USB_ISTR_L1REQ_FORCED (USB_ISTR_L1REQ)
557#define USB_ISTR_L1REQ_FORCED ((uint16_t)0x0000U)
560#define USB_ISTR_ALL_EVENTS (USB_ISTR_PMAOVR | USB_ISTR_ERR | USB_ISTR_WKUP | USB_ISTR_SUSP | \
561 USB_ISTR_RESET | USB_ISTR_SOF | USB_ISTR_ESOF | USB_ISTR_L1REQ_FORCED )
565#define STFSDEV_EP_COUNT (8u)
#define __O
Definition core_cm3.h:169
#define __IO
Definition core_cm3.h:170
#define __I
Definition core_cm3.h:167
CMSIS STM32F1xx Device Peripheral Access Layer Header File.