Christina Quast | b123d74 | 2014-12-23 13:03:36 +0100 | [diff] [blame] | 1 | /* ---------------------------------------------------------------------------- |
| 2 | * ATMEL Microcontroller Software Support |
| 3 | * ---------------------------------------------------------------------------- |
| 4 | * Copyright (c) 2008, Atmel Corporation |
| 5 | * |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * - Redistributions of source code must retain the above copyright notice, |
| 12 | * this list of conditions and the disclaimer below. |
| 13 | * |
| 14 | * Atmel's name may not be used to endorse or promote products derived from |
| 15 | * this software without specific prior written permission. |
| 16 | * |
| 17 | * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 23 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 24 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 25 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 26 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * ---------------------------------------------------------------------------- |
| 28 | */ |
| 29 | |
| 30 | /** |
| 31 | * \file |
| 32 | * |
| 33 | * \section Purpose |
| 34 | * |
| 35 | * Collection of methods for using the USB device controller on AT91 |
| 36 | * microcontrollers. |
| 37 | * |
| 38 | * \section Usage |
| 39 | * |
| 40 | * Please refer to the corresponding application note. |
| 41 | * - \ref usbd_framework AT91 USB device framework |
| 42 | * - \ref usbd_api USBD API |
| 43 | * |
| 44 | */ |
| 45 | |
| 46 | #ifndef USBD_H |
| 47 | #define USBD_H |
| 48 | |
| 49 | /*---------------------------------------------------------------------------- |
| 50 | * Headers |
| 51 | *----------------------------------------------------------------------------*/ |
| 52 | |
| 53 | |
| 54 | #include "USBDescriptors.h" |
| 55 | #include "USBRequests.h" |
| 56 | |
| 57 | #include "USBLib_Types.h" |
| 58 | |
| 59 | #include <stdio.h> |
| 60 | |
| 61 | /*------------------------------------------------------------------------------ |
| 62 | * Definitions |
| 63 | *------------------------------------------------------------------------------*/ |
| 64 | |
| 65 | /* Define attribute */ |
| 66 | #if defined ( __CC_ARM ) /* Keil µVision 4 */ |
| 67 | #define WEAK __attribute__ ((weak)) |
| 68 | #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */ |
| 69 | #define WEAK __weak |
| 70 | #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */ |
| 71 | #define WEAK __attribute__ ((weak)) |
| 72 | #endif |
| 73 | |
| 74 | /* Define NO_INIT attribute */ |
| 75 | #if defined ( __CC_ARM ) |
| 76 | #define NO_INIT |
| 77 | #elif defined ( __ICCARM__ ) |
| 78 | #define NO_INIT __no_init |
| 79 | #elif defined ( __GNUC__ ) |
| 80 | #define NO_INIT |
| 81 | #endif |
| 82 | |
| 83 | |
| 84 | /** \addtogroup usbd_interface |
| 85 | *@{*/ |
| 86 | |
| 87 | /** |
| 88 | * \addtogroup usbd_rc USB device API return codes |
| 89 | * @{ |
| 90 | * This section lists the return codes for the USB device driver API |
| 91 | * - \ref USBD_STATUS_SUCCESS |
| 92 | * - \ref USBD_STATUS_LOCKED |
| 93 | * - \ref USBD_STATUS_ABORTED |
| 94 | * - \ref USBD_STATUS_RESET |
| 95 | */ |
| 96 | |
| 97 | /** Indicates the operation was successful. */ |
| 98 | #define USBD_STATUS_SUCCESS USBRC_SUCCESS |
| 99 | /** Endpoint/device is already busy. */ |
| 100 | #define USBD_STATUS_LOCKED USBRC_BUSY |
| 101 | /** Operation has been aborted (error or stall). */ |
| 102 | #define USBD_STATUS_ABORTED USBRC_ABORTED |
| 103 | /** Operation has been canceled (by user). */ |
| 104 | #define USBD_STATUS_CANCELED USBRC_CANCELED |
| 105 | /** Operation has been aborted because the device init/reset/un-configure. */ |
| 106 | #define USBD_STATUS_RESET USBRC_RESET |
| 107 | /** Part ot operation successfully done. */ |
| 108 | #define USBD_STATUS_PARTIAL_DONE USBRC_PARTIAL_DONE |
| 109 | /** Operation failed because parameter error */ |
| 110 | #define USBD_STATUS_INVALID_PARAMETER USBRC_PARAM_ERR |
| 111 | /** Operation failed because in unexpected state */ |
| 112 | #define USBD_STATUS_WRONG_STATE USBRC_STATE_ERR |
| 113 | /** Operation failed because SW not supported */ |
| 114 | #define USBD_STATUS_SW_NOT_SUPPORTED USBRC_SW_NOT_SUPPORTED |
| 115 | /** Operation failed because HW not supported */ |
| 116 | #define USBD_STATUS_HW_NOT_SUPPORTED USBRC_HW_NOT_SUPPORTED |
| 117 | /** @}*/ |
| 118 | |
| 119 | /** \addtogroup usbd_states USB device states |
| 120 | * @{ |
| 121 | * This section lists the device states of the USB device driver. |
| 122 | * - \ref USBD_STATE_SUSPENDED |
| 123 | * - \ref USBD_STATE_ATTACHED |
| 124 | * - \ref USBD_STATE_POWERED |
| 125 | * - \ref USBD_STATE_DEFAULT |
| 126 | * - \ref USBD_STATE_ADDRESS |
| 127 | * - \ref USBD_STATE_CONFIGURED |
| 128 | */ |
| 129 | |
| 130 | /** The device is currently suspended. */ |
| 131 | #define USBD_STATE_SUSPENDED 0 |
| 132 | /** USB cable is plugged into the device. */ |
| 133 | #define USBD_STATE_ATTACHED 1 |
| 134 | /** Host is providing +5V through the USB cable. */ |
| 135 | #define USBD_STATE_POWERED 2 |
| 136 | /** Device has been reset. */ |
| 137 | #define USBD_STATE_DEFAULT 3 |
| 138 | /** The device has been given an address on the bus. */ |
| 139 | #define USBD_STATE_ADDRESS 4 |
| 140 | /** A valid configuration has been selected. */ |
| 141 | #define USBD_STATE_CONFIGURED 5 |
| 142 | /** @}*/ |
| 143 | |
| 144 | /*---------------------------------------------------------------------------- |
| 145 | * Types |
| 146 | *----------------------------------------------------------------------------*/ |
| 147 | |
| 148 | /** |
| 149 | * \brief Buffer struct used for multi-buffer-listed transfer. |
| 150 | * |
| 151 | * The driver can process 255 bytes of buffers or buffer list window. |
| 152 | */ |
| 153 | typedef struct _USBDTransferBuffer { |
| 154 | /** Pointer to frame buffer */ |
| 155 | uint8_t * pBuffer; |
| 156 | /** Size of the frame (up to 64K-1) */ |
| 157 | uint16_t size; |
| 158 | /** Bytes transferred */ |
| 159 | uint16_t transferred; |
| 160 | /** Bytes in FIFO */ |
| 161 | uint16_t buffered; |
| 162 | /** Bytes remaining */ |
| 163 | uint16_t remaining; |
| 164 | } USBDTransferBuffer; |
| 165 | |
| 166 | #ifdef __ICCARM__ /* IAR*/ |
| 167 | #define __attribute__(...) /* IAR*/ |
| 168 | #endif /* IAR*/ |
| 169 | |
| 170 | /** |
| 171 | * \brief Struct used for USBD DMA Link List Transfer Descriptor, must be 16-bytes |
| 172 | * aligned. |
| 173 | * |
| 174 | * (For USB, DMA transfer is linked to EPs and FIFO address is EP defined) |
| 175 | */ |
| 176 | typedef struct _USBDDmaDescriptor { |
| 177 | /** Pointer to Next Descriptor */ |
| 178 | void* pNxtDesc; |
| 179 | /** Pointer to data buffer address */ |
| 180 | void* pDataAddr; |
| 181 | /** DMA Control setting register value */ |
| 182 | uint32_t ctrlSettings:8, /** Control settings */ |
| 183 | reserved:8, /** Not used */ |
| 184 | bufferLength:16; /** Length of buffer */ |
| 185 | /** Loaded to DMA register, OK to modify */ |
| 186 | uint32_t used; |
| 187 | } __attribute__((aligned(16))) USBDDmaDescriptor; |
| 188 | |
| 189 | #ifdef __ICCARM__ /* IAR*/ |
| 190 | #pragma pack() /* IAR*/ |
| 191 | #endif /* IAR*/ |
| 192 | |
| 193 | /** |
| 194 | * Callback used by transfer functions (USBD_Read & USBD_Write) to notify |
| 195 | * that a transaction is complete. |
| 196 | */ |
| 197 | typedef void (*TransferCallback)(void *pArg, |
| 198 | uint8_t status, |
| 199 | uint32_t transferred, |
| 200 | uint32_t remaining); |
| 201 | |
| 202 | /** |
| 203 | * Callback used by MBL transfer functions (USBD_Read & USBD_Write) to notify |
| 204 | * that a transaction is complete. |
| 205 | * \param pArg Pointer to callback arguments. |
| 206 | * \param status USBD status. |
| 207 | */ |
| 208 | typedef void (*MblTransferCallback)(void *pArg, |
| 209 | uint8_t status); |
| 210 | |
| 211 | /**@}*/ |
| 212 | |
| 213 | /*------------------------------------------------------------------------------ |
| 214 | * Exported functions |
| 215 | *------------------------------------------------------------------------------*/ |
| 216 | |
| 217 | //extern void USBD_IrqHandler(void); |
| 218 | |
| 219 | extern void USBD_Init(void); |
| 220 | |
| 221 | extern void USBD_ConfigureSpeed(uint8_t forceFS); |
| 222 | |
| 223 | extern void USBD_Connect(void); |
| 224 | |
| 225 | extern void USBD_Disconnect(void); |
| 226 | |
| 227 | extern uint8_t USBD_Write( |
| 228 | uint8_t bEndpoint, |
| 229 | const void *pData, |
| 230 | uint32_t size, |
| 231 | TransferCallback callback, |
| 232 | void *pArg); |
| 233 | |
| 234 | extern uint8_t USBD_Read( |
| 235 | uint8_t bEndpoint, |
| 236 | void *pData, |
| 237 | uint32_t dLength, |
| 238 | TransferCallback fCallback, |
| 239 | void *pArg); |
| 240 | |
| 241 | extern uint8_t USBD_Stall(uint8_t bEndpoint); |
| 242 | |
| 243 | extern void USBD_Halt(uint8_t bEndpoint); |
| 244 | |
| 245 | extern void USBD_Unhalt(uint8_t bEndpoint); |
| 246 | |
| 247 | extern void USBD_ConfigureEndpoint(const USBEndpointDescriptor *pDescriptor); |
| 248 | |
| 249 | extern uint8_t USBD_IsHalted(uint8_t bEndpoint); |
| 250 | |
| 251 | extern void USBD_RemoteWakeUp(void); |
| 252 | |
| 253 | extern void USBD_SetAddress(uint8_t address); |
| 254 | |
| 255 | extern void USBD_SetConfiguration(uint8_t cfgnum); |
| 256 | |
| 257 | extern uint8_t USBD_GetState(void); |
| 258 | |
| 259 | extern uint8_t USBD_IsHighSpeed(void); |
| 260 | |
| 261 | extern void USBD_Test(uint8_t bIndex); |
| 262 | |
| 263 | extern void USBD_SuspendHandler(void); |
| 264 | extern void USBD_ResumeHandler(void); |
| 265 | extern void USBD_ResetHandler(void); |
| 266 | extern void USBD_RequestHandler(uint8_t bEndpoint, |
| 267 | const USBGenericRequest * pRequest); |
| 268 | |
| 269 | extern void USBDCallbacks_Initialized(void); |
| 270 | extern void USBDCallbacks_Reset(void); |
| 271 | extern void USBDCallbacks_Suspended(void); |
| 272 | extern void USBDCallbacks_Resumed(void); |
| 273 | extern void USBDCallbacks_RequestReceived(const USBGenericRequest *request); |
| 274 | |
| 275 | #endif /*#ifndef USBD_H*/ |
| 276 | |