USBDevice
targets/TARGET_Silicon_Labs/inc/em_usbhal.h@72:8a9c969eb0f6, 2019-05-31 (annotated)
- Committer:
- AubinGOUHIER
- Date:
- Fri May 31 15:37:22 2019 +0000
- Revision:
- 72:8a9c969eb0f6
- Parent:
- 71:53949e6131f6
Projet pour faire un clavier
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 71:53949e6131f6 | 1 | /***************************************************************************//** |
Kojto | 71:53949e6131f6 | 2 | * @file em_usbhal.h |
Kojto | 71:53949e6131f6 | 3 | * @brief USB protocol stack library, low level USB peripheral access. |
Kojto | 71:53949e6131f6 | 4 | * @version 3.20.14 |
Kojto | 71:53949e6131f6 | 5 | ******************************************************************************* |
Kojto | 71:53949e6131f6 | 6 | * @section License |
Kojto | 71:53949e6131f6 | 7 | * <b>(C) Copyright 2014 Silicon Labs, http://www.silabs.com</b> |
Kojto | 71:53949e6131f6 | 8 | ******************************************************************************* |
Kojto | 71:53949e6131f6 | 9 | * |
Kojto | 71:53949e6131f6 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Kojto | 71:53949e6131f6 | 11 | * you may not use this file except in compliance with the License. |
Kojto | 71:53949e6131f6 | 12 | * You may obtain a copy of the License at |
Kojto | 71:53949e6131f6 | 13 | * |
Kojto | 71:53949e6131f6 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
Kojto | 71:53949e6131f6 | 15 | * |
Kojto | 71:53949e6131f6 | 16 | * Unless required by applicable law or agreed to in writing, software |
Kojto | 71:53949e6131f6 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
Kojto | 71:53949e6131f6 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Kojto | 71:53949e6131f6 | 19 | * See the License for the specific language governing permissions and |
Kojto | 71:53949e6131f6 | 20 | * limitations under the License. |
Kojto | 71:53949e6131f6 | 21 | * |
Kojto | 71:53949e6131f6 | 22 | ******************************************************************************/ |
Kojto | 71:53949e6131f6 | 23 | |
Kojto | 71:53949e6131f6 | 24 | #ifndef __EM_USBHAL_H |
Kojto | 71:53949e6131f6 | 25 | #define __EM_USBHAL_H |
Kojto | 71:53949e6131f6 | 26 | |
Kojto | 71:53949e6131f6 | 27 | #include "em_device.h" |
Kojto | 71:53949e6131f6 | 28 | #if defined( USB_PRESENT ) && ( USB_COUNT == 1 ) |
Kojto | 71:53949e6131f6 | 29 | #include "em_usb.h" |
Kojto | 71:53949e6131f6 | 30 | #if defined( USB_DEVICE ) || defined( USB_HOST ) |
Kojto | 71:53949e6131f6 | 31 | |
Kojto | 71:53949e6131f6 | 32 | #ifdef __cplusplus |
Kojto | 71:53949e6131f6 | 33 | extern "C" { |
Kojto | 71:53949e6131f6 | 34 | #endif |
Kojto | 71:53949e6131f6 | 35 | |
Kojto | 71:53949e6131f6 | 36 | /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */ |
Kojto | 71:53949e6131f6 | 37 | |
Kojto | 71:53949e6131f6 | 38 | #define USB_PID_DATA0 0 |
Kojto | 71:53949e6131f6 | 39 | #define USB_PID_DATA2 1 |
Kojto | 71:53949e6131f6 | 40 | #define USB_PID_DATA1 2 |
Kojto | 71:53949e6131f6 | 41 | #define USB_PID_SETUP 3 |
Kojto | 71:53949e6131f6 | 42 | |
Kojto | 71:53949e6131f6 | 43 | #define HPRT_F_SPEED ( 1 << _USB_HPRT_PRTSPD_SHIFT ) |
Kojto | 71:53949e6131f6 | 44 | #define HPRT_L_SPEED ( 2 << _USB_HPRT_PRTSPD_SHIFT ) |
Kojto | 71:53949e6131f6 | 45 | #define HCFG_PHYCLK_48MHZ 1 |
Kojto | 71:53949e6131f6 | 46 | #define HCFG_PHYCLK_6MHZ 2 |
Kojto | 71:53949e6131f6 | 47 | |
Kojto | 71:53949e6131f6 | 48 | #define DOEP0_XFERSIZE_PKTCNT_MASK ( _USB_DOEP0TSIZ_XFERSIZE_MASK | \ |
Kojto | 71:53949e6131f6 | 49 | _USB_DOEP0TSIZ_PKTCNT_MASK ) |
Kojto | 71:53949e6131f6 | 50 | #define DOEP_XFERSIZE_PKTCNT_MASK ( _USB_DOEP_TSIZ_XFERSIZE_MASK | \ |
Kojto | 71:53949e6131f6 | 51 | _USB_DOEP_TSIZ_PKTCNT_MASK ) |
Kojto | 71:53949e6131f6 | 52 | |
Kojto | 71:53949e6131f6 | 53 | #define DIEP0_XFERSIZE_PKTCNT_MASK ( _USB_DIEP0TSIZ_XFERSIZE_MASK | \ |
Kojto | 71:53949e6131f6 | 54 | _USB_DIEP0TSIZ_PKTCNT_MASK ) |
Kojto | 71:53949e6131f6 | 55 | #define DIEP_XFERSIZE_PKTCNT_MASK ( _USB_DIEP_TSIZ_XFERSIZE_MASK | \ |
Kojto | 71:53949e6131f6 | 56 | _USB_DIEP_TSIZ_PKTCNT_MASK | \ |
Kojto | 71:53949e6131f6 | 57 | _USB_DIEP_TSIZ_MC_MASK ) |
Kojto | 71:53949e6131f6 | 58 | |
Kojto | 71:53949e6131f6 | 59 | #define DIEPCTL_EPTYPE_CONTROL (0 << _USB_DIEP_CTL_EPTYPE_SHIFT ) |
Kojto | 71:53949e6131f6 | 60 | #define DIEPCTL_EPTYPE_ISOC (1 << _USB_DIEP_CTL_EPTYPE_SHIFT ) |
Kojto | 71:53949e6131f6 | 61 | #define DIEPCTL_EPTYPE_BULK (2 << _USB_DIEP_CTL_EPTYPE_SHIFT ) |
Kojto | 71:53949e6131f6 | 62 | #define DIEPCTL_EPTYPE_INTR (3 << _USB_DIEP_CTL_EPTYPE_SHIFT ) |
Kojto | 71:53949e6131f6 | 63 | |
Kojto | 71:53949e6131f6 | 64 | #define DOEPCTL_EPTYPE_CONTROL (0 << _USB_DOEP_CTL_EPTYPE_SHIFT ) |
Kojto | 71:53949e6131f6 | 65 | #define DOEPCTL_EPTYPE_ISOC (1 << _USB_DOEP_CTL_EPTYPE_SHIFT ) |
Kojto | 71:53949e6131f6 | 66 | #define DOEPCTL_EPTYPE_BULK (2 << _USB_DOEP_CTL_EPTYPE_SHIFT ) |
Kojto | 71:53949e6131f6 | 67 | #define DOEPCTL_EPTYPE_INTR (3 << _USB_DOEP_CTL_EPTYPE_SHIFT ) |
Kojto | 71:53949e6131f6 | 68 | |
Kojto | 71:53949e6131f6 | 69 | #define HCCHAR_EPTYPE_CTRL (0 << _USB_HC_CHAR_EPTYPE_SHIFT ) |
Kojto | 71:53949e6131f6 | 70 | #define HCCHAR_EPTYPE_ISOC (1 << _USB_HC_CHAR_EPTYPE_SHIFT ) |
Kojto | 71:53949e6131f6 | 71 | #define HCCHAR_EPTYPE_BULK (2 << _USB_HC_CHAR_EPTYPE_SHIFT ) |
Kojto | 71:53949e6131f6 | 72 | #define HCCHAR_EPTYPE_INTR (3 << _USB_HC_CHAR_EPTYPE_SHIFT ) |
Kojto | 71:53949e6131f6 | 73 | |
Kojto | 71:53949e6131f6 | 74 | #define GRXSTSP_PKTSTS_DEVICE_GOTNAK ( 1 << _USB_GRXSTSP_PKTSTS_SHIFT ) |
Kojto | 71:53949e6131f6 | 75 | #define GRXSTSP_PKTSTS_DEVICE_DATAOUTRECEIVED ( 2 << _USB_GRXSTSP_PKTSTS_SHIFT ) |
Kojto | 71:53949e6131f6 | 76 | #define GRXSTSP_PKTSTS_DEVICE_DATAOUTCOMPLETE ( 3 << _USB_GRXSTSP_PKTSTS_SHIFT ) |
Kojto | 71:53949e6131f6 | 77 | #define GRXSTSP_PKTSTS_DEVICE_SETUPCOMPLETE ( 4 << _USB_GRXSTSP_PKTSTS_SHIFT ) |
Kojto | 71:53949e6131f6 | 78 | #define GRXSTSP_PKTSTS_DEVICE_SETUPRECEIVED ( 6 << _USB_GRXSTSP_PKTSTS_SHIFT ) |
Kojto | 71:53949e6131f6 | 79 | |
Kojto | 71:53949e6131f6 | 80 | #define GRXSTSP_PKTSTS_HOST_DATAINRECEIVED ( 2 << _USB_GRXSTSP_PKTSTS_SHIFT ) |
Kojto | 71:53949e6131f6 | 81 | #define GRXSTSP_PKTSTS_HOST_DATAINCOMPLETE ( 3 << _USB_GRXSTSP_PKTSTS_SHIFT ) |
Kojto | 71:53949e6131f6 | 82 | #define GRXSTSP_PKTSTS_HOST_DATATOGGLEERROR ( 5 << _USB_GRXSTSP_PKTSTS_SHIFT ) |
Kojto | 71:53949e6131f6 | 83 | #define GRXSTSP_PKTSTS_HOST_CHANNELHALTED ( 7 << _USB_GRXSTSP_PKTSTS_SHIFT ) |
Kojto | 71:53949e6131f6 | 84 | |
Kojto | 71:53949e6131f6 | 85 | #define DCTL_WO_BITMASK \ |
Kojto | 71:53949e6131f6 | 86 | ( _USB_DCTL_CGOUTNAK_MASK | _USB_DCTL_SGOUTNAK_MASK | \ |
Kojto | 71:53949e6131f6 | 87 | _USB_DCTL_CGNPINNAK_MASK | _USB_DCTL_SGNPINNAK_MASK ) |
Kojto | 71:53949e6131f6 | 88 | #define GUSBCFG_WO_BITMASK ( USB_GUSBCFG_CORRUPTTXPKT ) |
Kojto | 71:53949e6131f6 | 89 | #define DEPCTL_WO_BITMASK \ |
Kojto | 71:53949e6131f6 | 90 | ( USB_DIEP_CTL_CNAK | USB_DIEP_CTL_SNAK | \ |
Kojto | 71:53949e6131f6 | 91 | USB_DIEP_CTL_SETD0PIDEF | USB_DIEP_CTL_SETD1PIDOF ) |
Kojto | 71:53949e6131f6 | 92 | |
Kojto | 71:53949e6131f6 | 93 | #define HPRT_WC_MASK ( USB_HPRT_PRTCONNDET | USB_HPRT_PRTENA | \ |
Kojto | 71:53949e6131f6 | 94 | USB_HPRT_PRTENCHNG | USB_HPRT_PRTOVRCURRCHNG ) |
Kojto | 71:53949e6131f6 | 95 | |
Kojto | 71:53949e6131f6 | 96 | typedef __IO uint32_t USB_FIFO_TypeDef[ 0x1000 / sizeof( uint32_t ) ]; |
Kojto | 71:53949e6131f6 | 97 | typedef __IO uint32_t USB_DIEPTXF_TypeDef; |
Kojto | 71:53949e6131f6 | 98 | |
Kojto | 71:53949e6131f6 | 99 | #define USB_DINEPS ((USB_DIEP_TypeDef *) &USB->DIEP0CTL ) |
Kojto | 71:53949e6131f6 | 100 | #define USB_DOUTEPS ((USB_DOEP_TypeDef *) &USB->DOEP0CTL ) |
Kojto | 71:53949e6131f6 | 101 | #define USB_FIFOS ((USB_FIFO_TypeDef *) &USB->FIFO0D ) |
Kojto | 71:53949e6131f6 | 102 | #define USB_DIEPTXFS ((USB_DIEPTXF_TypeDef *) &USB->DIEPTXF1 ) |
Kojto | 71:53949e6131f6 | 103 | |
Kojto | 71:53949e6131f6 | 104 | void USBHAL_CoreReset( void ); |
Kojto | 71:53949e6131f6 | 105 | |
Kojto | 71:53949e6131f6 | 106 | #if defined( USB_DEVICE ) |
Kojto | 71:53949e6131f6 | 107 | void USBDHAL_AbortAllTransfers( USB_Status_TypeDef reason ); |
Kojto | 71:53949e6131f6 | 108 | USB_Status_TypeDef USBDHAL_CoreInit( const uint32_t totalRxFifoSize, |
Kojto | 71:53949e6131f6 | 109 | const uint32_t totalTxFifoSize ); |
Kojto | 71:53949e6131f6 | 110 | void USBDHAL_Connect( void ); |
Kojto | 71:53949e6131f6 | 111 | void USBDHAL_Disconnect( void ); |
Kojto | 71:53949e6131f6 | 112 | void USBDHAL_AbortAllEps( void ); |
Kojto | 71:53949e6131f6 | 113 | void USBDHAL_AbortEpIn( USBD_Ep_TypeDef *ep ); |
Kojto | 71:53949e6131f6 | 114 | void USBDHAL_AbortEpOut( USBD_Ep_TypeDef *ep ); |
Kojto | 71:53949e6131f6 | 115 | |
Kojto | 71:53949e6131f6 | 116 | __STATIC_INLINE USB_Status_TypeDef USBDHAL_GetStallStatusEp( |
Kojto | 71:53949e6131f6 | 117 | USBD_Ep_TypeDef *ep, uint16_t *halt ); |
Kojto | 71:53949e6131f6 | 118 | __STATIC_INLINE uint32_t USBDHAL_GetInEpInts( USBD_Ep_TypeDef *ep ); |
Kojto | 71:53949e6131f6 | 119 | __STATIC_INLINE uint32_t USBDHAL_GetOutEpInts( USBD_Ep_TypeDef *ep ); |
Kojto | 71:53949e6131f6 | 120 | __STATIC_INLINE void USBDHAL_SetEPDISNAK( USBD_Ep_TypeDef *ep ); |
Kojto | 71:53949e6131f6 | 121 | #endif /* defined( USB_DEVICE ) */ |
Kojto | 71:53949e6131f6 | 122 | |
Kojto | 71:53949e6131f6 | 123 | #if defined( USB_HOST ) |
Kojto | 71:53949e6131f6 | 124 | USB_Status_TypeDef USBHHAL_CoreInit( const uint32_t rxFifoSize, |
Kojto | 71:53949e6131f6 | 125 | const uint32_t nptxFifoSize, |
Kojto | 71:53949e6131f6 | 126 | const uint32_t ptxFifoSize ); |
Kojto | 71:53949e6131f6 | 127 | void USBHHAL_HCHalt( int hcnum, uint32_t hcchar ); |
Kojto | 71:53949e6131f6 | 128 | void USBHHAL_HCInit( int hcnum ); |
Kojto | 71:53949e6131f6 | 129 | void USBHHAL_HCStart( int hcnum ); |
Kojto | 71:53949e6131f6 | 130 | #endif /* defined( USB_HOST ) */ |
Kojto | 71:53949e6131f6 | 131 | |
Kojto | 71:53949e6131f6 | 132 | __STATIC_INLINE void USBHAL_DisableGlobalInt( void ) |
Kojto | 71:53949e6131f6 | 133 | { |
Kojto | 71:53949e6131f6 | 134 | USB->GAHBCFG &= ~USB_GAHBCFG_GLBLINTRMSK; |
Kojto | 71:53949e6131f6 | 135 | } |
Kojto | 71:53949e6131f6 | 136 | |
Kojto | 71:53949e6131f6 | 137 | __STATIC_INLINE void USBHAL_DisablePhyPins( void ) |
Kojto | 71:53949e6131f6 | 138 | { |
Kojto | 71:53949e6131f6 | 139 | USB->ROUTE = _USB_ROUTE_RESETVALUE; |
Kojto | 71:53949e6131f6 | 140 | } |
Kojto | 71:53949e6131f6 | 141 | |
Kojto | 71:53949e6131f6 | 142 | __STATIC_INLINE void USBHAL_DisableUsbInt( void ) |
Kojto | 71:53949e6131f6 | 143 | { |
Kojto | 71:53949e6131f6 | 144 | USB->IEN = _USB_IEN_RESETVALUE; |
Kojto | 71:53949e6131f6 | 145 | } |
Kojto | 71:53949e6131f6 | 146 | |
Kojto | 71:53949e6131f6 | 147 | __STATIC_INLINE void USBHAL_EnableGlobalInt( void ) |
Kojto | 71:53949e6131f6 | 148 | { |
Kojto | 71:53949e6131f6 | 149 | USB->GAHBCFG |= USB_GAHBCFG_GLBLINTRMSK; |
Kojto | 71:53949e6131f6 | 150 | } |
Kojto | 71:53949e6131f6 | 151 | |
Kojto | 71:53949e6131f6 | 152 | __STATIC_INLINE void USBHAL_FlushRxFifo( void ) |
Kojto | 71:53949e6131f6 | 153 | { |
Kojto | 71:53949e6131f6 | 154 | USB->GRSTCTL = USB_GRSTCTL_RXFFLSH; |
Kojto | 71:53949e6131f6 | 155 | while ( USB->GRSTCTL & USB_GRSTCTL_RXFFLSH ) {} |
Kojto | 71:53949e6131f6 | 156 | } |
Kojto | 71:53949e6131f6 | 157 | |
Kojto | 71:53949e6131f6 | 158 | __STATIC_INLINE void USBHAL_FlushTxFifo( uint8_t fifoNum ) |
Kojto | 71:53949e6131f6 | 159 | { |
Kojto | 71:53949e6131f6 | 160 | USB->GRSTCTL = USB_GRSTCTL_TXFFLSH | ( fifoNum << _USB_GRSTCTL_TXFNUM_SHIFT ); |
Kojto | 71:53949e6131f6 | 161 | while ( USB->GRSTCTL & USB_GRSTCTL_TXFFLSH ) {} |
Kojto | 71:53949e6131f6 | 162 | } |
Kojto | 71:53949e6131f6 | 163 | |
Kojto | 71:53949e6131f6 | 164 | __STATIC_INLINE uint32_t USBHAL_GetCoreInts( void ) |
Kojto | 71:53949e6131f6 | 165 | { |
Kojto | 71:53949e6131f6 | 166 | uint32_t retVal; |
Kojto | 71:53949e6131f6 | 167 | |
Kojto | 71:53949e6131f6 | 168 | retVal = USB->GINTSTS; |
Kojto | 71:53949e6131f6 | 169 | retVal &= USB->GINTMSK; |
Kojto | 71:53949e6131f6 | 170 | |
Kojto | 71:53949e6131f6 | 171 | return retVal; |
Kojto | 71:53949e6131f6 | 172 | } |
Kojto | 71:53949e6131f6 | 173 | |
Kojto | 71:53949e6131f6 | 174 | __STATIC_INLINE bool USBHAL_VbusIsOn( void ) |
Kojto | 71:53949e6131f6 | 175 | { |
Kojto | 71:53949e6131f6 | 176 | return ( USB->STATUS & USB_STATUS_VREGOS ) != 0; |
Kojto | 71:53949e6131f6 | 177 | } |
Kojto | 71:53949e6131f6 | 178 | |
Kojto | 71:53949e6131f6 | 179 | #if defined( USB_DEVICE ) |
Kojto | 71:53949e6131f6 | 180 | __STATIC_INLINE void USBDHAL_ActivateEp( USBD_Ep_TypeDef *ep, bool forceIdle ) |
Kojto | 71:53949e6131f6 | 181 | { |
Kojto | 71:53949e6131f6 | 182 | #define DIEP_MPS_EPTYPE_TXFNUM_MASK ( _USB_DIEP_CTL_MPS_MASK | \ |
Kojto | 71:53949e6131f6 | 183 | _USB_DIEP_CTL_EPTYPE_MASK | \ |
Kojto | 71:53949e6131f6 | 184 | _USB_DIEP_CTL_TXFNUM_MASK ) |
Kojto | 71:53949e6131f6 | 185 | #define DOEP_MPS_EPTYPE_MASK ( _USB_DOEP_CTL_MPS_MASK | \ |
Kojto | 71:53949e6131f6 | 186 | _USB_DOEP_CTL_EPTYPE_MASK ) |
Kojto | 71:53949e6131f6 | 187 | uint32_t daintmask, depctl; |
Kojto | 71:53949e6131f6 | 188 | |
Kojto | 71:53949e6131f6 | 189 | if ( forceIdle ) |
Kojto | 71:53949e6131f6 | 190 | ep->state = D_EP_IDLE; |
Kojto | 71:53949e6131f6 | 191 | |
Kojto | 71:53949e6131f6 | 192 | if ( ep->in ) |
Kojto | 71:53949e6131f6 | 193 | { |
Kojto | 71:53949e6131f6 | 194 | daintmask = ep->mask; |
Kojto | 71:53949e6131f6 | 195 | depctl = USB_DINEPS[ ep->num ].CTL & ~DEPCTL_WO_BITMASK; |
Kojto | 71:53949e6131f6 | 196 | |
Kojto | 71:53949e6131f6 | 197 | if ( !( depctl & USB_DIEP_CTL_USBACTEP ) ) |
Kojto | 71:53949e6131f6 | 198 | { |
Kojto | 71:53949e6131f6 | 199 | depctl = ( depctl & |
Kojto | 71:53949e6131f6 | 200 | ~( DIEP_MPS_EPTYPE_TXFNUM_MASK | |
Kojto | 71:53949e6131f6 | 201 | USB_DIEP_CTL_STALL ) ) | |
Kojto | 71:53949e6131f6 | 202 | ( ep->packetSize << _USB_DIEP_CTL_MPS_SHIFT ) | |
Kojto | 71:53949e6131f6 | 203 | ( ep->type << _USB_DIEP_CTL_EPTYPE_SHIFT ) | |
Kojto | 71:53949e6131f6 | 204 | ( ep->txFifoNum << _USB_DIEP_CTL_TXFNUM_SHIFT ) | |
Kojto | 71:53949e6131f6 | 205 | USB_DIEP_CTL_SETD0PIDEF | |
Kojto | 71:53949e6131f6 | 206 | USB_DIEP_CTL_USBACTEP | |
Kojto | 71:53949e6131f6 | 207 | USB_DIEP_CTL_SNAK; |
Kojto | 71:53949e6131f6 | 208 | } |
Kojto | 71:53949e6131f6 | 209 | else |
Kojto | 71:53949e6131f6 | 210 | { |
Kojto | 71:53949e6131f6 | 211 | depctl |= USB_DIEP_CTL_SETD0PIDEF; |
Kojto | 71:53949e6131f6 | 212 | } |
Kojto | 71:53949e6131f6 | 213 | USB_DINEPS[ ep->num ].CTL = depctl; |
Kojto | 71:53949e6131f6 | 214 | } |
Kojto | 71:53949e6131f6 | 215 | else |
Kojto | 71:53949e6131f6 | 216 | { |
Kojto | 71:53949e6131f6 | 217 | daintmask = ep->mask << _USB_DAINTMSK_OUTEPMSK0_SHIFT; |
Kojto | 71:53949e6131f6 | 218 | depctl = USB_DOUTEPS[ ep->num ].CTL & ~DEPCTL_WO_BITMASK; |
Kojto | 71:53949e6131f6 | 219 | |
Kojto | 71:53949e6131f6 | 220 | if ( !( depctl & USB_DOEP_CTL_USBACTEP ) ) |
Kojto | 71:53949e6131f6 | 221 | { |
Kojto | 71:53949e6131f6 | 222 | depctl = ( depctl & |
Kojto | 71:53949e6131f6 | 223 | ~( DOEP_MPS_EPTYPE_MASK | |
Kojto | 71:53949e6131f6 | 224 | USB_DOEP_CTL_STALL ) ) | |
Kojto | 71:53949e6131f6 | 225 | ( ep->packetSize << _USB_DOEP_CTL_MPS_SHIFT ) | |
Kojto | 71:53949e6131f6 | 226 | ( ep->type << _USB_DOEP_CTL_EPTYPE_SHIFT ) | |
Kojto | 71:53949e6131f6 | 227 | USB_DOEP_CTL_SETD0PIDEF | |
Kojto | 71:53949e6131f6 | 228 | USB_DOEP_CTL_USBACTEP | |
Kojto | 71:53949e6131f6 | 229 | USB_DOEP_CTL_SNAK; |
Kojto | 71:53949e6131f6 | 230 | } |
Kojto | 71:53949e6131f6 | 231 | else |
Kojto | 71:53949e6131f6 | 232 | { |
Kojto | 71:53949e6131f6 | 233 | depctl |= USB_DOEP_CTL_SETD0PIDEF; |
Kojto | 71:53949e6131f6 | 234 | } |
Kojto | 71:53949e6131f6 | 235 | USB_DOUTEPS[ ep->num ].CTL = depctl; |
Kojto | 71:53949e6131f6 | 236 | } |
Kojto | 71:53949e6131f6 | 237 | |
Kojto | 71:53949e6131f6 | 238 | /* Enable interrupt for this EP */ |
Kojto | 71:53949e6131f6 | 239 | USB->DAINTMSK |= daintmask; |
Kojto | 71:53949e6131f6 | 240 | |
Kojto | 71:53949e6131f6 | 241 | #undef DIEP_MPS_EPTYPE_TXFNUM_MASK |
Kojto | 71:53949e6131f6 | 242 | #undef DOEP_MPS_EPTYPE_MASK |
Kojto | 71:53949e6131f6 | 243 | } |
Kojto | 71:53949e6131f6 | 244 | |
Kojto | 71:53949e6131f6 | 245 | __STATIC_INLINE void USBDHAL_ClearRemoteWakeup( void ) |
Kojto | 71:53949e6131f6 | 246 | { |
Kojto | 71:53949e6131f6 | 247 | USB->DCTL &= ~( DCTL_WO_BITMASK | USB_DCTL_RMTWKUPSIG ); |
Kojto | 71:53949e6131f6 | 248 | } |
Kojto | 71:53949e6131f6 | 249 | |
Kojto | 71:53949e6131f6 | 250 | __STATIC_INLINE void USBDHAL_DeactivateEp( USBD_Ep_TypeDef *ep ) |
Kojto | 71:53949e6131f6 | 251 | { |
Kojto | 71:53949e6131f6 | 252 | uint32_t daintmask; |
Kojto | 71:53949e6131f6 | 253 | |
Kojto | 71:53949e6131f6 | 254 | if ( ep->in ) |
Kojto | 71:53949e6131f6 | 255 | { |
Kojto | 71:53949e6131f6 | 256 | USB_DINEPS[ ep->num ].CTL = 0; |
Kojto | 71:53949e6131f6 | 257 | daintmask = ep->mask; |
Kojto | 71:53949e6131f6 | 258 | } |
Kojto | 71:53949e6131f6 | 259 | else |
Kojto | 71:53949e6131f6 | 260 | { |
Kojto | 71:53949e6131f6 | 261 | USB_DOUTEPS[ ep->num ].CTL = 0; |
Kojto | 71:53949e6131f6 | 262 | daintmask = ep->mask << _USB_DAINTMSK_OUTEPMSK0_SHIFT; |
Kojto | 71:53949e6131f6 | 263 | } |
Kojto | 71:53949e6131f6 | 264 | |
Kojto | 71:53949e6131f6 | 265 | /* Disable interrupt for this EP */ |
Kojto | 71:53949e6131f6 | 266 | USB->DAINTMSK &= ~daintmask; |
Kojto | 71:53949e6131f6 | 267 | } |
Kojto | 71:53949e6131f6 | 268 | |
Kojto | 71:53949e6131f6 | 269 | __STATIC_INLINE void USBDHAL_EnableInts( USBD_Device_TypeDef *dev ) |
Kojto | 71:53949e6131f6 | 270 | { |
Kojto | 71:53949e6131f6 | 271 | uint32_t mask; |
Kojto | 71:53949e6131f6 | 272 | |
Kojto | 71:53949e6131f6 | 273 | /* Disable all interrupts. */ |
Kojto | 71:53949e6131f6 | 274 | USB->GINTMSK = 0; |
Kojto | 71:53949e6131f6 | 275 | |
Kojto | 71:53949e6131f6 | 276 | /* Clear pending interrupts */ |
Kojto | 71:53949e6131f6 | 277 | USB->GINTSTS = 0xFFFFFFFF; |
Kojto | 71:53949e6131f6 | 278 | |
Kojto | 71:53949e6131f6 | 279 | mask = USB_GINTMSK_USBSUSPMSK | |
Kojto | 71:53949e6131f6 | 280 | USB_GINTMSK_USBRSTMSK | |
Kojto | 71:53949e6131f6 | 281 | USB_GINTMSK_ENUMDONEMSK | |
Kojto | 71:53949e6131f6 | 282 | USB_GINTMSK_IEPINTMSK | |
Kojto | 71:53949e6131f6 | 283 | USB_GINTMSK_OEPINTMSK | |
Kojto | 71:53949e6131f6 | 284 | USB_GINTMSK_WKUPINTMSK; |
Kojto | 71:53949e6131f6 | 285 | |
Kojto | 71:53949e6131f6 | 286 | if ( dev->callbacks->sofInt ) |
Kojto | 71:53949e6131f6 | 287 | { |
Kojto | 71:53949e6131f6 | 288 | mask |= USB_GINTMSK_SOFMSK; |
Kojto | 71:53949e6131f6 | 289 | } |
Kojto | 71:53949e6131f6 | 290 | |
Kojto | 71:53949e6131f6 | 291 | USB->GINTMSK = mask; |
Kojto | 71:53949e6131f6 | 292 | } |
Kojto | 71:53949e6131f6 | 293 | |
Kojto | 71:53949e6131f6 | 294 | __STATIC_INLINE void USBDHAL_EnableUsbResetAndSuspendInt( void ) |
Kojto | 71:53949e6131f6 | 295 | { |
Kojto | 71:53949e6131f6 | 296 | /* Disable all interrupts. */ |
Kojto | 71:53949e6131f6 | 297 | USB->GINTMSK = 0; |
Kojto | 71:53949e6131f6 | 298 | |
Kojto | 71:53949e6131f6 | 299 | USB->GINTMSK = USB_GINTMSK_USBRSTMSK | USB_GINTMSK_USBSUSPMSK; |
Kojto | 71:53949e6131f6 | 300 | } |
Kojto | 71:53949e6131f6 | 301 | |
Kojto | 71:53949e6131f6 | 302 | __STATIC_INLINE void USBDHAL_Ep0Activate( uint32_t ep0mps ) |
Kojto | 71:53949e6131f6 | 303 | { |
Kojto | 71:53949e6131f6 | 304 | USB->DCTL = ( USB->DCTL & ~DCTL_WO_BITMASK ) | USB_DCTL_CGNPINNAK; |
Kojto | 71:53949e6131f6 | 305 | |
Kojto | 71:53949e6131f6 | 306 | USB->DOEP0CTL = ( USB->DOEP0CTL & ~DEPCTL_WO_BITMASK ) |
Kojto | 71:53949e6131f6 | 307 | | USB_DOEP0CTL_CNAK | USB_DOEP0CTL_EPENA |
Kojto | 71:53949e6131f6 | 308 | | ep0mps; |
Kojto | 71:53949e6131f6 | 309 | } |
Kojto | 71:53949e6131f6 | 310 | |
Kojto | 71:53949e6131f6 | 311 | __STATIC_INLINE bool USBDHAL_EpIsStalled( USBD_Ep_TypeDef *ep ) |
Kojto | 71:53949e6131f6 | 312 | { |
Kojto | 71:53949e6131f6 | 313 | bool retVal = false; |
Kojto | 71:53949e6131f6 | 314 | uint16_t stallStatus; |
Kojto | 71:53949e6131f6 | 315 | |
Kojto | 71:53949e6131f6 | 316 | if ( USBDHAL_GetStallStatusEp( ep, &stallStatus ) == USB_STATUS_OK ) |
Kojto | 71:53949e6131f6 | 317 | { |
Kojto | 71:53949e6131f6 | 318 | retVal = stallStatus & 1 ? true : false; |
Kojto | 71:53949e6131f6 | 319 | } |
Kojto | 71:53949e6131f6 | 320 | return retVal; |
Kojto | 71:53949e6131f6 | 321 | } |
Kojto | 71:53949e6131f6 | 322 | |
Kojto | 71:53949e6131f6 | 323 | __STATIC_INLINE uint32_t USBDHAL_GetAllInEpInts( void ) |
Kojto | 71:53949e6131f6 | 324 | { |
Kojto | 71:53949e6131f6 | 325 | uint32_t retVal; |
Kojto | 71:53949e6131f6 | 326 | |
Kojto | 71:53949e6131f6 | 327 | retVal = USB->DAINT; |
Kojto | 71:53949e6131f6 | 328 | retVal &= USB->DAINTMSK; |
Kojto | 71:53949e6131f6 | 329 | return retVal & 0xFFFF; |
Kojto | 71:53949e6131f6 | 330 | } |
Kojto | 71:53949e6131f6 | 331 | |
Kojto | 71:53949e6131f6 | 332 | __STATIC_INLINE uint32_t USBDHAL_GetAllOutEpInts( void ) |
Kojto | 71:53949e6131f6 | 333 | { |
Kojto | 71:53949e6131f6 | 334 | uint32_t retVal; |
Kojto | 71:53949e6131f6 | 335 | |
Kojto | 71:53949e6131f6 | 336 | retVal = USB->DAINT; |
Kojto | 71:53949e6131f6 | 337 | retVal &= USB->DAINTMSK; |
Kojto | 71:53949e6131f6 | 338 | return retVal >> 16; |
Kojto | 71:53949e6131f6 | 339 | } |
Kojto | 71:53949e6131f6 | 340 | |
Kojto | 71:53949e6131f6 | 341 | __STATIC_INLINE uint32_t USBDHAL_GetInEpInts( USBD_Ep_TypeDef *ep ) |
Kojto | 71:53949e6131f6 | 342 | { |
Kojto | 71:53949e6131f6 | 343 | uint32_t retVal, msk; |
Kojto | 71:53949e6131f6 | 344 | |
Kojto | 71:53949e6131f6 | 345 | msk = USB->DIEPMSK; |
Kojto | 71:53949e6131f6 | 346 | retVal = USB_DINEPS[ ep->num ].INT; |
Kojto | 71:53949e6131f6 | 347 | |
Kojto | 71:53949e6131f6 | 348 | return retVal & msk; |
Kojto | 71:53949e6131f6 | 349 | } |
Kojto | 71:53949e6131f6 | 350 | |
Kojto | 71:53949e6131f6 | 351 | __STATIC_INLINE uint32_t USBDHAL_GetOutEpInts( USBD_Ep_TypeDef *ep ) |
Kojto | 71:53949e6131f6 | 352 | { |
Kojto | 71:53949e6131f6 | 353 | uint32_t retVal; |
Kojto | 71:53949e6131f6 | 354 | |
Kojto | 71:53949e6131f6 | 355 | retVal = USB_DOUTEPS[ ep->num ].INT; |
Kojto | 71:53949e6131f6 | 356 | #if defined( USB_DOEP0INT_STUPPKTRCVD ) |
Kojto | 71:53949e6131f6 | 357 | retVal &= USB->DOEPMSK | USB_DOEP0INT_STUPPKTRCVD; |
Kojto | 71:53949e6131f6 | 358 | #else |
Kojto | 71:53949e6131f6 | 359 | retVal &= USB->DOEPMSK; |
Kojto | 71:53949e6131f6 | 360 | #endif |
Kojto | 71:53949e6131f6 | 361 | |
Kojto | 71:53949e6131f6 | 362 | return retVal; |
Kojto | 71:53949e6131f6 | 363 | } |
Kojto | 71:53949e6131f6 | 364 | |
Kojto | 71:53949e6131f6 | 365 | __STATIC_INLINE USB_Status_TypeDef USBDHAL_GetStallStatusEp( |
Kojto | 71:53949e6131f6 | 366 | USBD_Ep_TypeDef *ep, uint16_t *halt ) |
Kojto | 71:53949e6131f6 | 367 | { |
Kojto | 71:53949e6131f6 | 368 | uint32_t depctl, eptype; |
Kojto | 71:53949e6131f6 | 369 | USB_Status_TypeDef retVal = USB_STATUS_REQ_ERR; |
Kojto | 71:53949e6131f6 | 370 | |
Kojto | 71:53949e6131f6 | 371 | if ( ep->in == true ) |
Kojto | 71:53949e6131f6 | 372 | { |
Kojto | 71:53949e6131f6 | 373 | depctl = USB_DINEPS[ ep->num ].CTL; |
Kojto | 71:53949e6131f6 | 374 | eptype = depctl & _USB_DIEP_CTL_EPTYPE_MASK; |
Kojto | 71:53949e6131f6 | 375 | |
Kojto | 71:53949e6131f6 | 376 | if (( eptype == DIEPCTL_EPTYPE_INTR ) || ( eptype == DIEPCTL_EPTYPE_BULK )) |
Kojto | 71:53949e6131f6 | 377 | { |
Kojto | 71:53949e6131f6 | 378 | *halt = depctl & USB_DIEP_CTL_STALL ? 1 : 0; |
Kojto | 71:53949e6131f6 | 379 | retVal = USB_STATUS_OK; |
Kojto | 71:53949e6131f6 | 380 | } |
Kojto | 71:53949e6131f6 | 381 | } |
Kojto | 71:53949e6131f6 | 382 | else |
Kojto | 71:53949e6131f6 | 383 | { |
Kojto | 71:53949e6131f6 | 384 | depctl = USB_DOUTEPS[ ep->num ].CTL; |
Kojto | 71:53949e6131f6 | 385 | eptype = depctl & _USB_DOEP_CTL_EPTYPE_MASK; |
Kojto | 71:53949e6131f6 | 386 | |
Kojto | 71:53949e6131f6 | 387 | if (( eptype == DOEPCTL_EPTYPE_INTR ) || ( eptype == DOEPCTL_EPTYPE_BULK )) |
Kojto | 71:53949e6131f6 | 388 | { |
Kojto | 71:53949e6131f6 | 389 | *halt = depctl & USB_DOEP_CTL_STALL ? 1 : 0; |
Kojto | 71:53949e6131f6 | 390 | retVal = USB_STATUS_OK; |
Kojto | 71:53949e6131f6 | 391 | } |
Kojto | 71:53949e6131f6 | 392 | } |
Kojto | 71:53949e6131f6 | 393 | |
Kojto | 71:53949e6131f6 | 394 | return retVal; |
Kojto | 71:53949e6131f6 | 395 | } |
Kojto | 71:53949e6131f6 | 396 | |
Kojto | 71:53949e6131f6 | 397 | __STATIC_INLINE void USBDHAL_ReenableEp0Setup( USBD_Device_TypeDef *dev ) |
Kojto | 71:53949e6131f6 | 398 | |
Kojto | 71:53949e6131f6 | 399 | { |
Kojto | 71:53949e6131f6 | 400 | USB->DOEP0DMAADDR = (uint32_t)dev->setupPkt; |
Kojto | 71:53949e6131f6 | 401 | USB->DOEP0CTL = ( USB->DOEP0CTL & ~DEPCTL_WO_BITMASK ) |
Kojto | 71:53949e6131f6 | 402 | | USB_DOEP0CTL_EPENA |
Kojto | 71:53949e6131f6 | 403 | | dev->ep0MpsCode; |
Kojto | 71:53949e6131f6 | 404 | } |
Kojto | 71:53949e6131f6 | 405 | |
Kojto | 71:53949e6131f6 | 406 | __STATIC_INLINE void USBDHAL_SetAddr( uint8_t addr ) |
Kojto | 71:53949e6131f6 | 407 | { |
Kojto | 71:53949e6131f6 | 408 | USB->DCFG = ( USB->DCFG & |
Kojto | 71:53949e6131f6 | 409 | ~_USB_DCFG_DEVADDR_MASK ) | |
Kojto | 71:53949e6131f6 | 410 | (addr << _USB_DCFG_DEVADDR_SHIFT ); |
Kojto | 71:53949e6131f6 | 411 | } |
Kojto | 71:53949e6131f6 | 412 | |
Kojto | 71:53949e6131f6 | 413 | __STATIC_INLINE void USBDHAL_SetEp0InDmaPtr( uint8_t* addr ) |
Kojto | 71:53949e6131f6 | 414 | { |
Kojto | 71:53949e6131f6 | 415 | USB->DIEP0DMAADDR = (uint32_t)addr; |
Kojto | 71:53949e6131f6 | 416 | } |
Kojto | 71:53949e6131f6 | 417 | |
Kojto | 71:53949e6131f6 | 418 | __STATIC_INLINE void USBDHAL_SetEp0OutDmaPtr( uint8_t* addr ) |
Kojto | 71:53949e6131f6 | 419 | { |
Kojto | 71:53949e6131f6 | 420 | USB->DOEP0DMAADDR = (uint32_t)addr; |
Kojto | 71:53949e6131f6 | 421 | } |
Kojto | 71:53949e6131f6 | 422 | |
Kojto | 71:53949e6131f6 | 423 | __STATIC_INLINE void USBDHAL_SetEPDISNAK( USBD_Ep_TypeDef *ep ) |
Kojto | 71:53949e6131f6 | 424 | { |
Kojto | 71:53949e6131f6 | 425 | if ( ep->in ) |
Kojto | 71:53949e6131f6 | 426 | { |
Kojto | 71:53949e6131f6 | 427 | USB_DINEPS[ ep->num ].CTL = ( USB_DINEPS[ ep->num ].CTL & |
Kojto | 71:53949e6131f6 | 428 | ~DEPCTL_WO_BITMASK ) | |
Kojto | 71:53949e6131f6 | 429 | USB_DIEP_CTL_SNAK | |
Kojto | 71:53949e6131f6 | 430 | USB_DIEP_CTL_EPDIS; |
Kojto | 71:53949e6131f6 | 431 | } |
Kojto | 71:53949e6131f6 | 432 | else |
Kojto | 71:53949e6131f6 | 433 | { |
Kojto | 71:53949e6131f6 | 434 | USB_DOUTEPS[ ep->num ].CTL = ( USB_DOUTEPS[ ep->num ].CTL & |
Kojto | 71:53949e6131f6 | 435 | ~DEPCTL_WO_BITMASK ) | |
Kojto | 71:53949e6131f6 | 436 | USB_DOEP_CTL_EPENA; |
Kojto | 71:53949e6131f6 | 437 | |
Kojto | 71:53949e6131f6 | 438 | USB_DOUTEPS[ ep->num ].CTL = ( USB_DOUTEPS[ ep->num ].CTL & |
Kojto | 71:53949e6131f6 | 439 | ~DEPCTL_WO_BITMASK ) | |
Kojto | 71:53949e6131f6 | 440 | USB_DOEP_CTL_SNAK | |
Kojto | 71:53949e6131f6 | 441 | USB_DOEP_CTL_EPDIS; |
Kojto | 71:53949e6131f6 | 442 | } |
Kojto | 71:53949e6131f6 | 443 | } |
Kojto | 71:53949e6131f6 | 444 | |
Kojto | 71:53949e6131f6 | 445 | __STATIC_INLINE void USBDHAL_SetRemoteWakeup( void ) |
Kojto | 71:53949e6131f6 | 446 | { |
Kojto | 71:53949e6131f6 | 447 | USB->DCTL = ( USB->DCTL & ~DCTL_WO_BITMASK ) | USB_DCTL_RMTWKUPSIG; |
Kojto | 71:53949e6131f6 | 448 | } |
Kojto | 71:53949e6131f6 | 449 | |
Kojto | 71:53949e6131f6 | 450 | __STATIC_INLINE USB_Status_TypeDef USBDHAL_StallEp( USBD_Ep_TypeDef *ep ) |
Kojto | 71:53949e6131f6 | 451 | { |
Kojto | 71:53949e6131f6 | 452 | uint32_t depctl, eptype; |
Kojto | 71:53949e6131f6 | 453 | USB_Status_TypeDef retVal = USB_STATUS_REQ_ERR; |
Kojto | 71:53949e6131f6 | 454 | |
Kojto | 71:53949e6131f6 | 455 | if ( ep->in == true ) |
Kojto | 71:53949e6131f6 | 456 | { |
Kojto | 71:53949e6131f6 | 457 | depctl = USB_DINEPS[ ep->num ].CTL & ~DEPCTL_WO_BITMASK; |
Kojto | 71:53949e6131f6 | 458 | eptype = depctl & _USB_DIEP_CTL_EPTYPE_MASK; |
Kojto | 71:53949e6131f6 | 459 | |
Kojto | 71:53949e6131f6 | 460 | if ( eptype != DIEPCTL_EPTYPE_ISOC ) |
Kojto | 71:53949e6131f6 | 461 | { |
Kojto | 71:53949e6131f6 | 462 | if ( depctl & USB_DIEP_CTL_EPENA ) |
Kojto | 71:53949e6131f6 | 463 | { |
Kojto | 71:53949e6131f6 | 464 | depctl |= USB_DIEP_CTL_EPDIS; |
Kojto | 71:53949e6131f6 | 465 | } |
Kojto | 71:53949e6131f6 | 466 | USB_DINEPS[ ep->num ].CTL = depctl | USB_DIEP_CTL_STALL; |
Kojto | 71:53949e6131f6 | 467 | retVal = USB_STATUS_OK; |
Kojto | 71:53949e6131f6 | 468 | } |
Kojto | 71:53949e6131f6 | 469 | } |
Kojto | 71:53949e6131f6 | 470 | else |
Kojto | 71:53949e6131f6 | 471 | { |
Kojto | 71:53949e6131f6 | 472 | depctl = USB_DOUTEPS[ ep->num ].CTL & ~DEPCTL_WO_BITMASK; |
Kojto | 71:53949e6131f6 | 473 | eptype = depctl & _USB_DOEP_CTL_EPTYPE_MASK; |
Kojto | 71:53949e6131f6 | 474 | |
Kojto | 71:53949e6131f6 | 475 | if ( eptype != DIEPCTL_EPTYPE_ISOC ) |
Kojto | 71:53949e6131f6 | 476 | { |
Kojto | 71:53949e6131f6 | 477 | USB_DOUTEPS[ ep->num ].CTL = depctl | USB_DOEP_CTL_STALL; |
Kojto | 71:53949e6131f6 | 478 | retVal = USB_STATUS_OK; |
Kojto | 71:53949e6131f6 | 479 | } |
Kojto | 71:53949e6131f6 | 480 | } |
Kojto | 71:53949e6131f6 | 481 | |
Kojto | 71:53949e6131f6 | 482 | return retVal; |
Kojto | 71:53949e6131f6 | 483 | } |
Kojto | 71:53949e6131f6 | 484 | |
Kojto | 71:53949e6131f6 | 485 | __STATIC_INLINE void USBDHAL_StartEp0In( uint32_t len, uint32_t ep0mps ) |
Kojto | 71:53949e6131f6 | 486 | { |
Kojto | 71:53949e6131f6 | 487 | USB->DIEP0TSIZ = ( len << _USB_DIEP0TSIZ_XFERSIZE_SHIFT ) | |
Kojto | 71:53949e6131f6 | 488 | ( 1 << _USB_DIEP0TSIZ_PKTCNT_SHIFT ); |
Kojto | 71:53949e6131f6 | 489 | |
Kojto | 71:53949e6131f6 | 490 | USB->DIEP0CTL = ( USB->DIEP0CTL & ~DEPCTL_WO_BITMASK ) |
Kojto | 71:53949e6131f6 | 491 | | USB_DIEP0CTL_CNAK | USB_DIEP0CTL_EPENA |
Kojto | 71:53949e6131f6 | 492 | | ep0mps; |
Kojto | 71:53949e6131f6 | 493 | } |
Kojto | 71:53949e6131f6 | 494 | |
Kojto | 71:53949e6131f6 | 495 | __STATIC_INLINE void USBDHAL_StartEp0Out( uint32_t len, uint32_t ep0mps ) |
Kojto | 71:53949e6131f6 | 496 | { |
Kojto | 71:53949e6131f6 | 497 | USB->DOEP0TSIZ = ( len << _USB_DOEP0TSIZ_XFERSIZE_SHIFT ) | |
Kojto | 71:53949e6131f6 | 498 | ( 1 << _USB_DOEP0TSIZ_PKTCNT_SHIFT ); |
Kojto | 71:53949e6131f6 | 499 | |
Kojto | 71:53949e6131f6 | 500 | USB->DOEP0CTL = ( USB->DOEP0CTL & ~DEPCTL_WO_BITMASK ) |
Kojto | 71:53949e6131f6 | 501 | | USB_DOEP0CTL_CNAK | USB_DOEP0CTL_EPENA |
Kojto | 71:53949e6131f6 | 502 | | ep0mps; |
Kojto | 71:53949e6131f6 | 503 | } |
Kojto | 71:53949e6131f6 | 504 | |
Kojto | 71:53949e6131f6 | 505 | __STATIC_INLINE void USBDHAL_StartEp0Setup( USBD_Device_TypeDef *dev ) |
Kojto | 71:53949e6131f6 | 506 | { |
Kojto | 71:53949e6131f6 | 507 | dev->ep[ 0 ].in = false; |
Kojto | 71:53949e6131f6 | 508 | |
Kojto | 71:53949e6131f6 | 509 | #if defined( USB_DOEP0INT_STUPPKTRCVD ) |
Kojto | 71:53949e6131f6 | 510 | USB->DOEP0TSIZ = ( 8*3 << _USB_DOEP0TSIZ_XFERSIZE_SHIFT ) | |
Kojto | 71:53949e6131f6 | 511 | ( 1 << _USB_DOEP0TSIZ_PKTCNT_SHIFT ) | |
Kojto | 71:53949e6131f6 | 512 | ( 3 << _USB_DOEP0TSIZ_SUPCNT_SHIFT ); |
Kojto | 71:53949e6131f6 | 513 | #else |
Kojto | 71:53949e6131f6 | 514 | USB->DOEP0TSIZ = 3 << _USB_DOEP0TSIZ_SUPCNT_SHIFT; |
Kojto | 71:53949e6131f6 | 515 | #endif |
Kojto | 71:53949e6131f6 | 516 | |
Kojto | 71:53949e6131f6 | 517 | dev->setup = dev->setupPkt; |
Kojto | 71:53949e6131f6 | 518 | USB->DOEP0DMAADDR = (uint32_t)dev->setup; |
Kojto | 71:53949e6131f6 | 519 | |
Kojto | 71:53949e6131f6 | 520 | #if defined( USB_DOEP0INT_STUPPKTRCVD ) |
Kojto | 71:53949e6131f6 | 521 | USB->DOEP0CTL = ( USB->DOEP0CTL & ~DEPCTL_WO_BITMASK ) |
Kojto | 71:53949e6131f6 | 522 | | USB_DOEP0CTL_EPENA |
Kojto | 71:53949e6131f6 | 523 | | dev->ep0MpsCode; |
Kojto | 71:53949e6131f6 | 524 | #else |
Kojto | 71:53949e6131f6 | 525 | USB->DOEP0CTL = ( USB->DOEP0CTL & ~DEPCTL_WO_BITMASK ) |
Kojto | 71:53949e6131f6 | 526 | | USB_DOEP0CTL_CNAK | USB_DOEP0CTL_EPENA |
Kojto | 71:53949e6131f6 | 527 | | dev->ep0MpsCode; |
Kojto | 71:53949e6131f6 | 528 | #endif |
Kojto | 71:53949e6131f6 | 529 | } |
Kojto | 71:53949e6131f6 | 530 | |
Kojto | 71:53949e6131f6 | 531 | __STATIC_INLINE void USBDHAL_StartEpIn( USBD_Ep_TypeDef *ep ) |
Kojto | 71:53949e6131f6 | 532 | { |
Kojto | 71:53949e6131f6 | 533 | uint32_t pktcnt, xfersize; |
Kojto | 71:53949e6131f6 | 534 | |
Kojto | 71:53949e6131f6 | 535 | if ( ep->remaining == 0 ) /* ZLP ? */ |
Kojto | 71:53949e6131f6 | 536 | { |
Kojto | 71:53949e6131f6 | 537 | pktcnt = 1; |
Kojto | 71:53949e6131f6 | 538 | xfersize = 0; |
Kojto | 71:53949e6131f6 | 539 | } |
Kojto | 71:53949e6131f6 | 540 | else |
Kojto | 71:53949e6131f6 | 541 | { |
Kojto | 71:53949e6131f6 | 542 | pktcnt = ( ep->remaining - 1 + ep->packetSize ) / ep->packetSize; |
Kojto | 71:53949e6131f6 | 543 | xfersize = ep->remaining; |
Kojto | 71:53949e6131f6 | 544 | } |
Kojto | 71:53949e6131f6 | 545 | |
Kojto | 71:53949e6131f6 | 546 | USB_DINEPS[ ep->num ].TSIZ = |
Kojto | 71:53949e6131f6 | 547 | ( USB_DINEPS[ ep->num ].TSIZ & |
Kojto | 71:53949e6131f6 | 548 | ~DIEP_XFERSIZE_PKTCNT_MASK ) | |
Kojto | 71:53949e6131f6 | 549 | ( xfersize << _USB_DIEP_TSIZ_XFERSIZE_SHIFT ) | |
Kojto | 71:53949e6131f6 | 550 | ( pktcnt << _USB_DIEP_TSIZ_PKTCNT_SHIFT ); |
Kojto | 71:53949e6131f6 | 551 | |
Kojto | 71:53949e6131f6 | 552 | USB_DINEPS[ ep->num ].DMAADDR = (uint32_t)ep->buf; |
Kojto | 71:53949e6131f6 | 553 | USB_DINEPS[ ep->num ].CTL = |
Kojto | 71:53949e6131f6 | 554 | ( USB_DINEPS[ ep->num ].CTL & ~DEPCTL_WO_BITMASK ) | |
Kojto | 71:53949e6131f6 | 555 | USB_DIEP_CTL_CNAK | |
Kojto | 71:53949e6131f6 | 556 | USB_DIEP_CTL_EPENA; |
Kojto | 71:53949e6131f6 | 557 | } |
Kojto | 71:53949e6131f6 | 558 | |
Kojto | 71:53949e6131f6 | 559 | __STATIC_INLINE void USBDHAL_StartEpOut( USBD_Ep_TypeDef *ep ) |
Kojto | 71:53949e6131f6 | 560 | { |
Kojto | 71:53949e6131f6 | 561 | uint32_t pktcnt, xfersize; |
Kojto | 71:53949e6131f6 | 562 | |
Kojto | 71:53949e6131f6 | 563 | if ( ep->remaining == 0 ) /* ZLP ? */ |
Kojto | 71:53949e6131f6 | 564 | { |
Kojto | 71:53949e6131f6 | 565 | pktcnt = 1; |
Kojto | 71:53949e6131f6 | 566 | xfersize = ep->packetSize; |
Kojto | 71:53949e6131f6 | 567 | } |
Kojto | 71:53949e6131f6 | 568 | else |
Kojto | 71:53949e6131f6 | 569 | { |
Kojto | 71:53949e6131f6 | 570 | pktcnt = ( ep->remaining - 1 + ep->packetSize ) / ep->packetSize; |
Kojto | 71:53949e6131f6 | 571 | xfersize = pktcnt * ep->packetSize; |
Kojto | 71:53949e6131f6 | 572 | } |
Kojto | 71:53949e6131f6 | 573 | |
Kojto | 71:53949e6131f6 | 574 | USB_DOUTEPS[ ep->num ].TSIZ = |
Kojto | 71:53949e6131f6 | 575 | ( USB_DOUTEPS[ ep->num ].TSIZ & |
Kojto | 71:53949e6131f6 | 576 | ~DOEP_XFERSIZE_PKTCNT_MASK ) | |
Kojto | 71:53949e6131f6 | 577 | ( xfersize << _USB_DOEP_TSIZ_XFERSIZE_SHIFT ) | |
Kojto | 71:53949e6131f6 | 578 | ( pktcnt << _USB_DOEP_TSIZ_PKTCNT_SHIFT ); |
Kojto | 71:53949e6131f6 | 579 | |
Kojto | 71:53949e6131f6 | 580 | ep->hwXferSize = xfersize; |
Kojto | 71:53949e6131f6 | 581 | USB_DOUTEPS[ ep->num ].DMAADDR = (uint32_t)ep->buf; |
Kojto | 71:53949e6131f6 | 582 | USB_DOUTEPS[ ep->num ].CTL = |
Kojto | 71:53949e6131f6 | 583 | ( USB_DOUTEPS[ ep->num ].CTL & |
Kojto | 71:53949e6131f6 | 584 | ~DEPCTL_WO_BITMASK ) | |
Kojto | 71:53949e6131f6 | 585 | USB_DOEP_CTL_CNAK | |
Kojto | 71:53949e6131f6 | 586 | USB_DOEP_CTL_EPENA; |
Kojto | 71:53949e6131f6 | 587 | } |
Kojto | 71:53949e6131f6 | 588 | |
Kojto | 71:53949e6131f6 | 589 | __STATIC_INLINE USB_Status_TypeDef USBDHAL_UnStallEp( USBD_Ep_TypeDef *ep ) |
Kojto | 71:53949e6131f6 | 590 | { |
Kojto | 71:53949e6131f6 | 591 | uint32_t depctl, eptype; |
Kojto | 71:53949e6131f6 | 592 | USB_Status_TypeDef retVal = USB_STATUS_REQ_ERR; |
Kojto | 71:53949e6131f6 | 593 | |
Kojto | 71:53949e6131f6 | 594 | if ( ep->in == true ) |
Kojto | 71:53949e6131f6 | 595 | { |
Kojto | 71:53949e6131f6 | 596 | depctl = USB_DINEPS[ ep->num ].CTL & ~DEPCTL_WO_BITMASK; |
Kojto | 71:53949e6131f6 | 597 | eptype = depctl & _USB_DIEP_CTL_EPTYPE_MASK; |
Kojto | 71:53949e6131f6 | 598 | |
Kojto | 71:53949e6131f6 | 599 | if (( eptype == DIEPCTL_EPTYPE_INTR ) || ( eptype == DIEPCTL_EPTYPE_BULK )) |
Kojto | 71:53949e6131f6 | 600 | { |
Kojto | 71:53949e6131f6 | 601 | depctl |= USB_DIEP_CTL_SETD0PIDEF; |
Kojto | 71:53949e6131f6 | 602 | depctl &= ~USB_DIEP_CTL_STALL; |
Kojto | 71:53949e6131f6 | 603 | USB_DINEPS[ ep->num ].CTL = depctl; |
Kojto | 71:53949e6131f6 | 604 | retVal = USB_STATUS_OK; |
Kojto | 71:53949e6131f6 | 605 | } |
Kojto | 71:53949e6131f6 | 606 | } |
Kojto | 71:53949e6131f6 | 607 | else |
Kojto | 71:53949e6131f6 | 608 | { |
Kojto | 71:53949e6131f6 | 609 | depctl = USB_DOUTEPS[ ep->num ].CTL & ~DEPCTL_WO_BITMASK; |
Kojto | 71:53949e6131f6 | 610 | eptype = depctl & _USB_DOEP_CTL_EPTYPE_MASK; |
Kojto | 71:53949e6131f6 | 611 | |
Kojto | 71:53949e6131f6 | 612 | if (( eptype == DIEPCTL_EPTYPE_INTR ) || ( eptype == DIEPCTL_EPTYPE_BULK )) |
Kojto | 71:53949e6131f6 | 613 | { |
Kojto | 71:53949e6131f6 | 614 | depctl |= USB_DOEP_CTL_SETD0PIDEF; |
Kojto | 71:53949e6131f6 | 615 | depctl &= ~USB_DOEP_CTL_STALL; |
Kojto | 71:53949e6131f6 | 616 | USB_DOUTEPS[ ep->num ].CTL = depctl; |
Kojto | 71:53949e6131f6 | 617 | retVal = USB_STATUS_OK; |
Kojto | 71:53949e6131f6 | 618 | } |
Kojto | 71:53949e6131f6 | 619 | } |
Kojto | 71:53949e6131f6 | 620 | |
Kojto | 71:53949e6131f6 | 621 | return retVal; |
Kojto | 71:53949e6131f6 | 622 | } |
Kojto | 71:53949e6131f6 | 623 | #endif /* defined( USB_DEVICE ) */ |
Kojto | 71:53949e6131f6 | 624 | |
Kojto | 71:53949e6131f6 | 625 | #if defined( USB_HOST ) |
Kojto | 71:53949e6131f6 | 626 | __STATIC_INLINE void USBHHAL_HCActivate( int hcnum, uint32_t hcchar, bool intep ) |
Kojto | 71:53949e6131f6 | 627 | { |
Kojto | 71:53949e6131f6 | 628 | uint32_t oddframe; |
Kojto | 71:53949e6131f6 | 629 | |
Kojto | 71:53949e6131f6 | 630 | if ( intep ) |
Kojto | 71:53949e6131f6 | 631 | { |
Kojto | 71:53949e6131f6 | 632 | oddframe = USB->HFNUM & 1; |
Kojto | 71:53949e6131f6 | 633 | |
Kojto | 71:53949e6131f6 | 634 | USB->HC[ hcnum ].CHAR = |
Kojto | 71:53949e6131f6 | 635 | ( hcchar & |
Kojto | 71:53949e6131f6 | 636 | ~( USB_HC_CHAR_CHDIS | _USB_HC_CHAR_ODDFRM_MASK ) ) | |
Kojto | 71:53949e6131f6 | 637 | |
Kojto | 71:53949e6131f6 | 638 | /* Schedule INT transfers to start in next frame. */ |
Kojto | 71:53949e6131f6 | 639 | ( oddframe & 1 ? 0 : USB_HC_CHAR_ODDFRM ) | |
Kojto | 71:53949e6131f6 | 640 | |
Kojto | 71:53949e6131f6 | 641 | USB_HC_CHAR_CHENA; |
Kojto | 71:53949e6131f6 | 642 | } |
Kojto | 71:53949e6131f6 | 643 | else |
Kojto | 71:53949e6131f6 | 644 | { |
Kojto | 71:53949e6131f6 | 645 | USB->HC[ hcnum ].CHAR = ( hcchar & ~USB_HC_CHAR_CHDIS ) | |
Kojto | 71:53949e6131f6 | 646 | USB_HC_CHAR_CHENA; |
Kojto | 71:53949e6131f6 | 647 | } |
Kojto | 71:53949e6131f6 | 648 | } |
Kojto | 71:53949e6131f6 | 649 | |
Kojto | 71:53949e6131f6 | 650 | __STATIC_INLINE bool USBHHAL_InitializedAndPowered( void ) |
Kojto | 71:53949e6131f6 | 651 | { |
Kojto | 71:53949e6131f6 | 652 | if ( ( USB->ROUTE & USB_ROUTE_PHYPEN ) && |
Kojto | 71:53949e6131f6 | 653 | ( USB->HPRT & USB_HPRT_PRTPWR ) ) |
Kojto | 71:53949e6131f6 | 654 | return true; |
Kojto | 71:53949e6131f6 | 655 | return false; |
Kojto | 71:53949e6131f6 | 656 | } |
Kojto | 71:53949e6131f6 | 657 | |
Kojto | 71:53949e6131f6 | 658 | __STATIC_INLINE void USBHHAL_EnableInts( void ) |
Kojto | 71:53949e6131f6 | 659 | { |
Kojto | 71:53949e6131f6 | 660 | /* Disable all interrupts. */ |
Kojto | 71:53949e6131f6 | 661 | USB->GINTMSK = 0; |
Kojto | 71:53949e6131f6 | 662 | |
Kojto | 71:53949e6131f6 | 663 | /* Clear pending OTG interrupts */ |
Kojto | 71:53949e6131f6 | 664 | USB->GOTGINT = 0xFFFFFFFF; |
Kojto | 71:53949e6131f6 | 665 | |
Kojto | 71:53949e6131f6 | 666 | /* Clear pending interrupts */ |
Kojto | 71:53949e6131f6 | 667 | USB->GINTSTS = 0xFFFFFFFF; |
Kojto | 71:53949e6131f6 | 668 | |
Kojto | 71:53949e6131f6 | 669 | USB->GINTMSK = USB_GINTMSK_PRTINTMSK | |
Kojto | 71:53949e6131f6 | 670 | USB_GINTMSK_HCHINTMSK | |
Kojto | 71:53949e6131f6 | 671 | USB_GINTMSK_DISCONNINTMSK; |
Kojto | 71:53949e6131f6 | 672 | } |
Kojto | 71:53949e6131f6 | 673 | |
Kojto | 71:53949e6131f6 | 674 | __STATIC_INLINE uint16_t USBHHAL_GetFrameNum( void ) |
Kojto | 71:53949e6131f6 | 675 | { |
Kojto | 71:53949e6131f6 | 676 | return USB->HFNUM; |
Kojto | 71:53949e6131f6 | 677 | } |
Kojto | 71:53949e6131f6 | 678 | |
Kojto | 71:53949e6131f6 | 679 | __STATIC_INLINE uint32_t USBHHAL_GetHcChar( uint8_t hcnum ) |
Kojto | 71:53949e6131f6 | 680 | { |
Kojto | 71:53949e6131f6 | 681 | return USB->HC[ hcnum ].CHAR; |
Kojto | 71:53949e6131f6 | 682 | } |
Kojto | 71:53949e6131f6 | 683 | |
Kojto | 71:53949e6131f6 | 684 | __STATIC_INLINE uint32_t USBHHAL_GetHcInts( uint8_t hcnum ) |
Kojto | 71:53949e6131f6 | 685 | { |
Kojto | 71:53949e6131f6 | 686 | uint32_t retVal; |
Kojto | 71:53949e6131f6 | 687 | |
Kojto | 71:53949e6131f6 | 688 | retVal = USB->HC[ hcnum ].INT; |
Kojto | 71:53949e6131f6 | 689 | return retVal; |
Kojto | 71:53949e6131f6 | 690 | } |
Kojto | 71:53949e6131f6 | 691 | |
Kojto | 71:53949e6131f6 | 692 | __STATIC_INLINE uint32_t USBHHAL_GetHostChannelInts( void ) |
Kojto | 71:53949e6131f6 | 693 | { |
Kojto | 71:53949e6131f6 | 694 | return USB->HAINT; |
Kojto | 71:53949e6131f6 | 695 | } |
Kojto | 71:53949e6131f6 | 696 | |
Kojto | 71:53949e6131f6 | 697 | __STATIC_INLINE uint8_t USBHHAL_GetPortSpeed( void ) |
Kojto | 71:53949e6131f6 | 698 | { |
Kojto | 71:53949e6131f6 | 699 | return ( USB->HPRT & _USB_HPRT_PRTSPD_MASK ) >> _USB_HPRT_PRTSPD_SHIFT; |
Kojto | 71:53949e6131f6 | 700 | } |
Kojto | 71:53949e6131f6 | 701 | |
Kojto | 71:53949e6131f6 | 702 | __STATIC_INLINE void USBHHAL_PortReset( bool on ) |
Kojto | 71:53949e6131f6 | 703 | { |
Kojto | 71:53949e6131f6 | 704 | if ( on ) |
Kojto | 71:53949e6131f6 | 705 | { |
Kojto | 71:53949e6131f6 | 706 | DEBUG_USB_INT_LO_PUTCHAR( '+' ); |
Kojto | 71:53949e6131f6 | 707 | USB->HPRT = ( USB->HPRT & ~HPRT_WC_MASK ) | USB_HPRT_PRTRST; |
Kojto | 71:53949e6131f6 | 708 | } |
Kojto | 71:53949e6131f6 | 709 | else |
Kojto | 71:53949e6131f6 | 710 | { |
Kojto | 71:53949e6131f6 | 711 | DEBUG_USB_INT_LO_PUTCHAR( '-' ); |
Kojto | 71:53949e6131f6 | 712 | USB->HPRT &= ~( HPRT_WC_MASK | USB_HPRT_PRTRST ); |
Kojto | 71:53949e6131f6 | 713 | } |
Kojto | 71:53949e6131f6 | 714 | } |
Kojto | 71:53949e6131f6 | 715 | |
Kojto | 71:53949e6131f6 | 716 | __STATIC_INLINE void USBHHAL_PortResume( bool on ) |
Kojto | 71:53949e6131f6 | 717 | { |
Kojto | 71:53949e6131f6 | 718 | if ( on ) |
Kojto | 71:53949e6131f6 | 719 | { |
Kojto | 71:53949e6131f6 | 720 | USB->HPRT = ( USB->HPRT & ~( HPRT_WC_MASK | USB_HPRT_PRTSUSP ) ) | |
Kojto | 71:53949e6131f6 | 721 | USB_HPRT_PRTRES; |
Kojto | 71:53949e6131f6 | 722 | } |
Kojto | 71:53949e6131f6 | 723 | else |
Kojto | 71:53949e6131f6 | 724 | { |
Kojto | 71:53949e6131f6 | 725 | USB->HPRT &= ~( HPRT_WC_MASK | USB_HPRT_PRTSUSP | USB_HPRT_PRTRES ); |
Kojto | 71:53949e6131f6 | 726 | } |
Kojto | 71:53949e6131f6 | 727 | } |
Kojto | 71:53949e6131f6 | 728 | |
Kojto | 71:53949e6131f6 | 729 | __STATIC_INLINE void USBHHAL_PortSuspend( void ) |
Kojto | 71:53949e6131f6 | 730 | { |
Kojto | 71:53949e6131f6 | 731 | USB->HPRT = ( USB->HPRT & ~HPRT_WC_MASK ) | USB_HPRT_PRTSUSP; |
Kojto | 71:53949e6131f6 | 732 | } |
Kojto | 71:53949e6131f6 | 733 | |
Kojto | 71:53949e6131f6 | 734 | __STATIC_INLINE void USBHHAL_VbusOn( bool on ) |
Kojto | 71:53949e6131f6 | 735 | { |
Kojto | 71:53949e6131f6 | 736 | if ( on ) |
Kojto | 71:53949e6131f6 | 737 | { |
Kojto | 71:53949e6131f6 | 738 | USB->HPRT = ( USB->HPRT & ~HPRT_WC_MASK ) | USB_HPRT_PRTPWR; |
Kojto | 71:53949e6131f6 | 739 | DEBUG_USB_INT_LO_PUTCHAR( '/' ); |
Kojto | 71:53949e6131f6 | 740 | } |
Kojto | 71:53949e6131f6 | 741 | else |
Kojto | 71:53949e6131f6 | 742 | { |
Kojto | 71:53949e6131f6 | 743 | USB->HPRT &= ~( HPRT_WC_MASK | USB_HPRT_PRTPWR ); |
Kojto | 71:53949e6131f6 | 744 | DEBUG_USB_INT_LO_PUTCHAR( '\\' ); |
Kojto | 71:53949e6131f6 | 745 | } |
Kojto | 71:53949e6131f6 | 746 | } |
Kojto | 71:53949e6131f6 | 747 | #endif /* defined( USB_HOST ) */ |
Kojto | 71:53949e6131f6 | 748 | |
Kojto | 71:53949e6131f6 | 749 | /** @endcond */ |
Kojto | 71:53949e6131f6 | 750 | |
Kojto | 71:53949e6131f6 | 751 | #ifdef __cplusplus |
Kojto | 71:53949e6131f6 | 752 | } |
Kojto | 71:53949e6131f6 | 753 | #endif |
Kojto | 71:53949e6131f6 | 754 | |
Kojto | 71:53949e6131f6 | 755 | #endif /* defined( USB_DEVICE ) || defined( USB_HOST ) */ |
Kojto | 71:53949e6131f6 | 756 | #endif /* defined( USB_PRESENT ) && ( USB_COUNT == 1 ) */ |
Kojto | 71:53949e6131f6 | 757 | #endif /* __EM_USBHAL_H */ |