|
| 1 | +/***************************************************************************//** |
| 2 | + * @file em_usbd.h |
| 3 | + * @brief USB protocol stack library API for EFM32. |
| 4 | + * @version 3.20.14 |
| 5 | + ******************************************************************************* |
| 6 | + * @section License |
| 7 | + * <b>(C) Copyright 2014 Silicon Labs, http://www.silabs.com</b> |
| 8 | + ******************************************************************************* |
| 9 | + * |
| 10 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | + * you may not use this file except in compliance with the License. |
| 12 | + * You may obtain a copy of the License at |
| 13 | + * |
| 14 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | + * |
| 16 | + * Unless required by applicable law or agreed to in writing, software |
| 17 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | + * See the License for the specific language governing permissions and |
| 20 | + * limitations under the License. |
| 21 | + * |
| 22 | + ******************************************************************************/ |
| 23 | + |
| 24 | +#ifndef __EM_USBD_H |
| 25 | +#define __EM_USBD_H |
| 26 | + |
| 27 | +#include "em_device.h" |
| 28 | +#if defined( USB_PRESENT ) && ( USB_COUNT == 1 ) |
| 29 | +#include "em_usb.h" |
| 30 | +#if defined( USB_DEVICE ) |
| 31 | + |
| 32 | +#ifdef __cplusplus |
| 33 | +extern "C" { |
| 34 | +#endif |
| 35 | + |
| 36 | +/** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */ |
| 37 | + |
| 38 | +#if defined( DEBUG_USB_API ) |
| 39 | +#define DEBUG_TRACE_ABORT( x ) \ |
| 40 | +{ \ |
| 41 | + if ( x == USB_STATUS_EP_STALLED ) \ |
| 42 | + { DEBUG_USB_API_PUTS( "\nEP cb(), EP stalled" ); } \ |
| 43 | + else if ( x == USB_STATUS_EP_ABORTED ) \ |
| 44 | + { DEBUG_USB_API_PUTS( "\nEP cb(), EP aborted" ); } \ |
| 45 | + else if ( x == USB_STATUS_DEVICE_UNCONFIGURED ) \ |
| 46 | + { DEBUG_USB_API_PUTS( "\nEP cb(), device unconfigured" ); } \ |
| 47 | + else if ( x == USB_STATUS_DEVICE_SUSPENDED ) \ |
| 48 | + { DEBUG_USB_API_PUTS( "\nEP cb(), device suspended" ); } \ |
| 49 | + else /* ( x == USB_STATUS_DEVICE_RESET ) */ \ |
| 50 | + { DEBUG_USB_API_PUTS( "\nEP cb(), device reset" ); } \ |
| 51 | +} |
| 52 | +#else |
| 53 | +#define DEBUG_TRACE_ABORT( x ) |
| 54 | +#endif |
| 55 | + |
| 56 | +extern USBD_Device_TypeDef *dev; |
| 57 | +extern volatile bool USBD_poweredDown; |
| 58 | + |
| 59 | +__STATIC_INLINE void USBD_ArmEp0( USBD_Ep_TypeDef *ep ); |
| 60 | +__STATIC_INLINE void USBD_ArmEpN( USBD_Ep_TypeDef *ep ); |
| 61 | +__STATIC_INLINE void USBD_AbortEp( USBD_Ep_TypeDef *ep ); |
| 62 | + |
| 63 | +void USBD_SetUsbState( USBD_State_TypeDef newState ); |
| 64 | + |
| 65 | +int USBDCH9_SetupCmd( USBD_Device_TypeDef *device ); |
| 66 | + |
| 67 | +void USBDEP_Ep0Handler( USBD_Device_TypeDef *device ); |
| 68 | +void USBDEP_EpHandler( uint8_t epAddr ); |
| 69 | + |
| 70 | +__STATIC_INLINE void USBD_ActivateAllEps( bool forceIdle ) |
| 71 | +{ |
| 72 | + int i; |
| 73 | + |
| 74 | + for ( i = 1; i <= NUM_EP_USED; i++ ) |
| 75 | + { |
| 76 | + USBDHAL_ActivateEp( &dev->ep[ i ], forceIdle ); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +__STATIC_INLINE void USBD_ArmEp( USBD_Ep_TypeDef *ep ) |
| 81 | +{ |
| 82 | + if ( ep->num == 0 ) |
| 83 | + { |
| 84 | + USBD_ArmEp0( ep ); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + USBD_ArmEpN( ep ); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +__STATIC_INLINE void USBD_ArmEp0( USBD_Ep_TypeDef *ep ) |
| 93 | +{ |
| 94 | + if ( ep->in ) |
| 95 | + { |
| 96 | + if ( ep->remaining == 0 ) /* Zero Length Packet? */ |
| 97 | + { |
| 98 | + ep->zlp = 1; |
| 99 | + } |
| 100 | + |
| 101 | + USBDHAL_SetEp0InDmaPtr( ep->buf ); |
| 102 | + USBDHAL_StartEp0In( EFM32_MIN( ep->remaining, ep->packetSize ), |
| 103 | + dev->ep0MpsCode ); |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + USBDHAL_SetEp0OutDmaPtr( ep->buf ); |
| 108 | + USBDHAL_StartEp0Out( ep->packetSize, dev->ep0MpsCode ); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +__STATIC_INLINE void USBD_ArmEpN( USBD_Ep_TypeDef *ep ) |
| 113 | +{ |
| 114 | + if ( ep->in ) |
| 115 | + { |
| 116 | + USBDHAL_StartEpIn( ep ); |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + USBDHAL_StartEpOut( ep ); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +__STATIC_INLINE void USBD_DeactivateAllEps( USB_Status_TypeDef reason ) |
| 125 | +{ |
| 126 | + int i; |
| 127 | + USBD_Ep_TypeDef *ep; |
| 128 | + |
| 129 | + for ( i = 1; i <= NUM_EP_USED; i++ ) |
| 130 | + { |
| 131 | + ep = &dev->ep[ i ]; |
| 132 | + |
| 133 | + if ( ep->state == D_EP_IDLE ) |
| 134 | + { |
| 135 | + USBDHAL_DeactivateEp( ep ); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + USBDHAL_AbortAllTransfers( reason ); |
| 140 | +} |
| 141 | + |
| 142 | +__STATIC_INLINE USBD_Ep_TypeDef *USBD_GetEpFromAddr( uint8_t epAddr ) |
| 143 | +{ |
| 144 | + int epIndex; |
| 145 | + USBD_Ep_TypeDef *ep = NULL; |
| 146 | + |
| 147 | + if ( epAddr & USB_SETUP_DIR_MASK ) |
| 148 | + { |
| 149 | + epIndex = dev->inEpAddr2EpIndex[ epAddr & USB_EPNUM_MASK ]; |
| 150 | + } |
| 151 | + else |
| 152 | + { |
| 153 | + epIndex = dev->outEpAddr2EpIndex[ epAddr & USB_EPNUM_MASK ]; |
| 154 | + } |
| 155 | + |
| 156 | + if ( epIndex ) |
| 157 | + { |
| 158 | + ep = &dev->ep[ epIndex ]; |
| 159 | + } |
| 160 | + else if ( ( epAddr & USB_EPNUM_MASK ) == 0 ) |
| 161 | + { |
| 162 | + ep = &dev->ep[ 0 ]; |
| 163 | + } |
| 164 | + |
| 165 | + return ep; |
| 166 | +} |
| 167 | + |
| 168 | +__STATIC_INLINE void USBD_ReArmEp0( USBD_Ep_TypeDef *ep ) |
| 169 | +{ |
| 170 | + if ( ep->in ) |
| 171 | + { |
| 172 | + USBDHAL_StartEp0In( EFM32_MIN( ep->remaining, ep->packetSize ), |
| 173 | + dev->ep0MpsCode ); |
| 174 | + } |
| 175 | + else |
| 176 | + { |
| 177 | + USBDHAL_StartEp0Out( ep->packetSize, dev->ep0MpsCode ); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +__STATIC_INLINE void USBD_AbortEp( USBD_Ep_TypeDef *ep ) |
| 182 | +{ |
| 183 | + if ( ep->state == D_EP_IDLE ) |
| 184 | + { |
| 185 | + return; |
| 186 | + } |
| 187 | + |
| 188 | + if ( ep->in ) |
| 189 | + { |
| 190 | + USBDHAL_AbortEpIn( ep ); |
| 191 | + } |
| 192 | + else |
| 193 | + { |
| 194 | + USBDHAL_AbortEpOut( ep ); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +/** @endcond */ |
| 199 | + |
| 200 | +#ifdef __cplusplus |
| 201 | +} |
| 202 | +#endif |
| 203 | + |
| 204 | +#endif /* defined( USB_DEVICE ) */ |
| 205 | +#endif /* defined( USB_PRESENT ) && ( USB_COUNT == 1 ) */ |
| 206 | +#endif /* __EM_USBD_H */ |
0 commit comments