/**************************************************************************//** * @file nu_hsusbd.h * @version V3.00 * @brief M460 series HSUSBD driver header file * * @copyright SPDX-License-Identifier: Apache-2.0 * @copyright Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved. ****************************************************************************/ #ifndef __NU_HSUSBD_H__ #define __NU_HSUSBD_H__ #ifdef __cplusplus extern "C" { #endif /** @addtogroup Standard_Driver Standard Driver @{ */ /** @addtogroup HSUSBD_Driver HSUSBD Driver @{ */ /** @addtogroup HSUSBD_EXPORTED_CONSTANTS HSUSBD Exported Constants @{ */ /** @cond HIDDEN_SYMBOLS */ #define HSUSBD_MAX_EP 12ul #define Maximum(a,b) (a)>(b) ? (a) : (b) #define Minimum(a,b) (((a)<(b)) ? (a) : (b)) #define CEP 0xfful /*!< Control Endpoint \hideinitializer */ #define EPA 0ul /*!< Endpoint A \hideinitializer */ #define EPB 1ul /*!< Endpoint B \hideinitializer */ #define EPC 2ul /*!< Endpoint C \hideinitializer */ #define EPD 3ul /*!< Endpoint D \hideinitializer */ #define EPE 4ul /*!< Endpoint E \hideinitializer */ #define EPF 5ul /*!< Endpoint F \hideinitializer */ #define EPG 6ul /*!< Endpoint G \hideinitializer */ #define EPH 7ul /*!< Endpoint H \hideinitializer */ #define EPI 8ul /*!< Endpoint I \hideinitializer */ #define EPJ 9ul /*!< Endpoint J \hideinitializer */ #define EPK 10ul /*!< Endpoint K \hideinitializer */ #define EPL 11ul /*!< Endpoint L \hideinitializer */ /** @endcond HIDDEN_SYMBOLS */ /********************* Bit definition of CEPCTL register **********************/ #define HSUSBD_CEPCTL_NAKCLR ((uint32_t)0x00000000ul) /*!PHYCTL |= (HSUSBD_PHYCTL_PHYEN_Msk|HSUSBD_PHYCTL_DPPUEN_Msk))) /*!PHYCTL &= ~HSUSBD_PHYCTL_DPPUEN_Msk)) /*!PHYCTL |= HSUSBD_PHYCTL_PHYEN_Msk)) /*!PHYCTL &= ~HSUSBD_PHYCTL_PHYEN_Msk)) /*!PHYCTL &= ~HSUSBD_PHYCTL_DPPUEN_Msk)) /*!PHYCTL |= HSUSBD_PHYCTL_DPPUEN_Msk)) /*!FADDR = (addr)) /*!FADDR)) /*!GINTEN = (intr)) /*!BUSINTEN = (intr)) /*!BUSINTSTS) /*!BUSINTSTS = (flag)) /*!CEPINTEN = (intr)) /*!CEPINTSTS = (flag)) /*!CEPCTL = (flag)) /*!CEPTXCNT = (size)) /*!EP[(ep)].EPMPS = (size)) /*!EP[(ep)].EPINTEN = (intr)) /*!EP[(ep)].EPINTSTS) /*!EP[(ep)].EPINTSTS = (flag)) /*!DMACNT = (len)) /*!DMAADDR = (addr)) /*!DMACTL = (HSUSBD->DMACTL & ~HSUSBD_DMACTL_EPNUM_Msk) | HSUSBD_DMACTL_DMARD_Msk | (epnum) | 0x100) /*!DMACTL = (HSUSBD->DMACTL & ~(HSUSBD_DMACTL_EPNUM_Msk | HSUSBD_DMACTL_DMARD_Msk | 0x100)) | (epnum)) /*!DMACTL |= HSUSBD_DMACTL_DMAEN_Msk) /*!PHYCTL & HSUSBD_PHYCTL_VBUSDET_Msk)) /*!BCDC |= HSUSBD_BCDC_BCDEN_Msk)) /*!BCDC &= ~HSUSBD_BCDC_BCDEN_Msk)) /*!LPMCSR |= HSUSBD_LPMCSR_LPMEN_Msk)) /*!LPMCSR &= ~HSUSBD_LPMCSR_LPMEN_Msk)) /*!DMACNT = 0ul; HSUSBD->DMACTL = 0x80ul; HSUSBD->DMACTL = 0x00ul; } /** * @brief HSUSBD_SetEpBufAddr, Set Endpoint buffer address * @param[in] u32Ep Endpoint Number * @param[in] u32Base Buffer Start Address * @param[in] u32Len Buffer length * @retval None. */ __STATIC_INLINE void HSUSBD_SetEpBufAddr(uint32_t u32Ep, uint32_t u32Base, uint32_t u32Len) { if (u32Ep == CEP) { HSUSBD->CEPBUFST = u32Base; HSUSBD->CEPBUFEND = u32Base + u32Len - 1ul; } else { HSUSBD->EP[u32Ep].EPBUFST = u32Base; HSUSBD->EP[u32Ep].EPBUFEND = u32Base + u32Len - 1ul; } } /** * @brief HSUSBD_ConfigEp, Config Endpoint * @param[in] u32Ep USB endpoint * @param[in] u32EpNum Endpoint number * @param[in] u32EpType Endpoint type * @param[in] u32EpDir Endpoint direction * @retval None. */ __STATIC_INLINE void HSUSBD_ConfigEp(uint32_t u32Ep, uint32_t u32EpNum, uint32_t u32EpType, uint32_t u32EpDir) { if (u32EpType == HSUSBD_EP_CFG_TYPE_BULK) { HSUSBD->EP[u32Ep].EPRSPCTL = (HSUSBD_EP_RSPCTL_FLUSH | HSUSBD_EP_RSPCTL_MODE_AUTO); } else if (u32EpType == HSUSBD_EP_CFG_TYPE_INT) { HSUSBD->EP[u32Ep].EPRSPCTL = (HSUSBD_EP_RSPCTL_FLUSH | HSUSBD_EP_RSPCTL_MODE_MANUAL); } else if (u32EpType == HSUSBD_EP_CFG_TYPE_ISO) { HSUSBD->EP[u32Ep].EPRSPCTL = (HSUSBD_EP_RSPCTL_FLUSH | HSUSBD_EP_RSPCTL_MODE_FLY); } HSUSBD->EP[u32Ep].EPCFG = (u32EpType | u32EpDir | HSUSBD_EP_CFG_VALID | (u32EpNum << 4)); } /** * @brief Set USB endpoint stall state * @param[in] u32Ep The USB endpoint ID. * @return None * @details Set USB endpoint stall state for the specified endpoint ID. Endpoint will respond STALL token automatically. */ __STATIC_INLINE void HSUSBD_SetEpStall(uint32_t u32Ep) { if (u32Ep == CEP) { HSUSBD_SET_CEP_STATE(HSUSBD_CEPCTL_STALL); } else { HSUSBD->EP[u32Ep].EPRSPCTL = (HSUSBD->EP[u32Ep].EPRSPCTL & 0xf7ul) | HSUSBD_EP_RSPCTL_HALT; } } /** * @brief Set USB endpoint stall state * * @param[in] u32EpNum USB endpoint * @return None * * @details Set USB endpoint stall state, endpoint will return STALL token. */ __STATIC_INLINE void HSUSBD_SetStall(uint32_t u32EpNum) { uint32_t i; if (u32EpNum == 0ul) { HSUSBD_SET_CEP_STATE(HSUSBD_CEPCTL_STALL); } else { for (i = 0ul; i < HSUSBD_MAX_EP; i++) { if (((HSUSBD->EP[i].EPCFG & 0xf0ul) >> 4) == u32EpNum) { HSUSBD->EP[i].EPRSPCTL = (HSUSBD->EP[i].EPRSPCTL & 0xf7ul) | HSUSBD_EP_RSPCTL_HALT; } } } } /** * @brief Clear USB endpoint stall state * @param[in] u32Ep The USB endpoint ID. * @return None * @details Clear USB endpoint stall state for the specified endpoint ID. Endpoint will respond ACK/NAK token. */ __STATIC_INLINE void HSUSBD_ClearEpStall(uint32_t u32Ep) { HSUSBD->EP[u32Ep].EPRSPCTL = HSUSBD_EP_RSPCTL_TOGGLE; } /** * @brief Clear USB endpoint stall state * * @param[in] u32EpNum USB endpoint * @return None * * @details Clear USB endpoint stall state, endpoint will return ACK/NAK token. */ __STATIC_INLINE void HSUSBD_ClearStall(uint32_t u32EpNum) { uint32_t i; for (i = 0ul; i < HSUSBD_MAX_EP; i++) { if (((HSUSBD->EP[i].EPCFG & 0xf0ul) >> 4) == u32EpNum) { HSUSBD->EP[i].EPRSPCTL = HSUSBD_EP_RSPCTL_TOGGLE; } } } /** * @brief Get USB endpoint stall state * @param[in] u32Ep The USB endpoint ID. * @retval 0 USB endpoint is not stalled. * @retval Others USB endpoint is stalled. * @details Get USB endpoint stall state of the specified endpoint ID. */ __STATIC_INLINE uint32_t HSUSBD_GetEpStall(uint32_t u32Ep) { return (HSUSBD->EP[u32Ep].EPRSPCTL & HSUSBD_EP_RSPCTL_HALT); } /** * @brief Get USB endpoint stall state * * @param[in] u32EpNum USB endpoint * @retval 0: USB endpoint is not stalled. * @retval non-0: USB endpoint is stalled. * * @details Get USB endpoint stall state. */ __STATIC_INLINE uint32_t HSUSBD_GetStall(uint32_t u32EpNum) { uint32_t i; uint32_t val = 0ul; for (i = 0ul; i < HSUSBD_MAX_EP; i++) { if (((HSUSBD->EP[i].EPCFG & 0xf0ul) >> 4) == u32EpNum) { val = (HSUSBD->EP[i].EPRSPCTL & HSUSBD_EP_RSPCTL_HALT); break; } } return val; } /*-------------------------------------------------------------------------------------------*/ typedef void (*HSUSBD_VENDOR_REQ)(void); /*!