Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file |
| 3 | * |
| 4 | * \brief SAM USB HPL |
| 5 | * |
| 6 | * Copyright (c) 2015-2018 Microchip Technology Inc. and its subsidiaries. |
| 7 | * |
| 8 | * \asf_license_start |
| 9 | * |
| 10 | * \page License |
| 11 | * |
| 12 | * Subject to your compliance with these terms, you may use Microchip |
| 13 | * software and any derivatives exclusively with Microchip products. |
| 14 | * It is your responsibility to comply with third party license terms applicable |
| 15 | * to your use of third party software (including open source software) that |
| 16 | * may accompany Microchip software. |
| 17 | * |
| 18 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, |
| 19 | * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, |
| 20 | * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, |
| 21 | * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE |
| 22 | * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL |
| 23 | * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE |
| 24 | * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE |
| 25 | * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT |
| 26 | * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY |
| 27 | * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, |
| 28 | * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. |
| 29 | * |
| 30 | * \asf_license_stop |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #ifndef _HPL_USB_H_INCLUDED |
| 35 | #define _HPL_USB_H_INCLUDED |
| 36 | |
| 37 | #include <compiler.h> |
| 38 | #include <utils.h> |
| 39 | |
| 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
| 44 | /** \name USB Spec definitions */ |
| 45 | /*@{*/ |
| 46 | |
| 47 | /** Return 8-bit unsigned USB data. */ |
| 48 | #define USB_GET_U8(addr, offset) (((uint8_t *)(addr))[(offset)]) |
| 49 | |
| 50 | /** Return 16-bit unsigned USB data. */ |
| 51 | #define USB_GET_U16(addr, offset) ((((uint8_t *)(addr))[(offset) + 0] << 0) + (((uint8_t *)(addr))[(offset) + 1] << 8)) |
| 52 | |
| 53 | /** Return 32-bit unsigned USB data. */ |
| 54 | #define USB_GET_U32(addr, offset) \ |
| 55 | ((((uint8_t *)(addr))[(offset) + 0] << 0) + (((uint8_t *)(addr))[(offset) + 1] << 8) \ |
| 56 | + (((uint8_t *)(addr))[(offset) + 2] << 16) + (((uint8_t *)(addr))[(offset) + 3] << 32)) |
| 57 | |
| 58 | /** Offset of bmRequestType in USB request buffer. */ |
| 59 | #define USB_bmRequestType_Offset 0 |
| 60 | |
| 61 | /** Offset of bRequest in USB request buffer. */ |
| 62 | #define USB_bRequest_Offset 1 |
| 63 | |
| 64 | /** Offset of wValue in USB request buffer. */ |
| 65 | #define USB_wValue_Offset 2 |
| 66 | |
| 67 | /** Offset of wIndex in USB request buffer. */ |
| 68 | #define USB_wIndex_Offset 4 |
| 69 | |
| 70 | /** Offset of wLength in USB request buffer. */ |
| 71 | #define USB_wLength_Offset 6 |
| 72 | |
| 73 | /** Get value of bmRequestType from USB request. */ |
| 74 | #define USB_GET_bmRequestType(req) USB_GET_U8((req), USB_bmRequestType_Offset) |
| 75 | |
| 76 | /** Get value of bRequest from USB request. */ |
| 77 | #define USB_GET_bRequest(req) USB_GET_U8((req), USB_bRequest_Offset) |
| 78 | |
| 79 | /** Get value of wValue from USB request. */ |
| 80 | #define USB_GET_wValue(req) USB_GET_U16((req), USB_wValue_Offset) |
| 81 | |
| 82 | /** Get value of wIndex from USB request. */ |
| 83 | #define USB_GET_wIndex(req) USB_GET_U16((req), USB_wIndex_Offset) |
| 84 | |
| 85 | /** Get value of wLength from USB request. */ |
| 86 | #define USB_GET_wLength(req) USB_GET_U16((req), USB_wLength_Offset) |
| 87 | |
| 88 | /** USB request IN indication of bmRequestType. */ |
| 89 | #define USB_REQ_TYPE_IN 0x80u |
| 90 | |
| 91 | /** USB endpoint number mask of bEndpointAddress. */ |
| 92 | #define USB_EP_N_MASK 0x0Fu |
| 93 | |
| 94 | /** USB endpoint direction bit of bEndpointAddress. */ |
| 95 | #define USB_EP_DIR 0x80u |
| 96 | |
| 97 | /** Get USB endpoint direction from endpoint address. */ |
| 98 | #define USB_EP_GET_DIR(ep_addr) ((ep_addr)&USB_EP_DIR) |
| 99 | |
| 100 | /** Get USB endpoint number from endpoint address. */ |
| 101 | #define USB_EP_GET_N(ep_addr) ((ep_addr)&USB_EP_N_MASK) |
| 102 | |
| 103 | /** Transfer type in EP descriptor bmAttributes: Control. */ |
| 104 | #define USB_EP_XTYPE_CTRL 0x0 |
| 105 | |
| 106 | /** Transfer type in EP descriptor bmAttributes: Isochronous. */ |
| 107 | #define USB_EP_XTYPE_ISOCH 0x1 |
| 108 | |
| 109 | /** Transfer type in EP descriptor bmAttributes: Bulk. */ |
| 110 | #define USB_EP_XTYPE_BULK 0x2 |
| 111 | |
| 112 | /** Transfer type in EP descriptor bmAttributes: Interrupt. */ |
| 113 | #define USB_EP_XTYPE_INTERRUPT 0x3 |
| 114 | |
| 115 | /** Transfer type mask in EP descriptor bmAttributes. */ |
| 116 | #define USB_EP_XTYPE_MASK 0x3u |
| 117 | |
| 118 | /*@}*/ |
| 119 | |
| 120 | /** \name USB status codes |
| 121 | *@{ |
| 122 | */ |
| 123 | /** USB operation is done successfully. */ |
| 124 | #define USB_OK 0 |
| 125 | /** USB (endpoint) is busy. */ |
| 126 | #define USB_BUSY 1 |
| 127 | /** USB (endpoint) is halted. */ |
| 128 | #define USB_HALTED 2 |
| 129 | |
| 130 | /** General error. */ |
| 131 | #define USB_ERROR 0x10 |
| 132 | /** Operation is denied by hardware (e.g., syncing). */ |
| 133 | #define USB_ERR_DENIED 0x11 |
| 134 | /** Input parameter error. */ |
| 135 | #define USB_ERR_PARAM 0x12 |
| 136 | /** Functionality is not supported (e.g., initialize endpoint without cache to be control). */ |
| 137 | #define USB_ERR_FUNC 0x13 |
| 138 | /** Re-initialize, re-enable ... */ |
| 139 | #define USB_ERR_REDO 0x14 |
| 140 | /** Not enough resource (memory, endpoints ...). */ |
| 141 | #define USB_ERR_ALLOC_FAIL 0x15 |
| 142 | /*@}*/ |
| 143 | |
| 144 | /** USB speed. */ |
| 145 | enum usb_speed { |
| 146 | /** USB Low Speed. */ |
| 147 | USB_SPEED_LS, |
| 148 | /** USB Full Speed. */ |
| 149 | USB_SPEED_FS, |
| 150 | /** USB High Speed. */ |
| 151 | USB_SPEED_HS, |
| 152 | /** USB Super Speed. */ |
| 153 | USB_SPEED_SS |
| 154 | }; |
| 155 | |
| 156 | /** USB transaction type. */ |
| 157 | enum usb_trans_type { |
| 158 | /** USB SETUP transaction. */ |
| 159 | USB_TRANS_SETUP, |
| 160 | /** USB IN transaction. */ |
| 161 | USB_TRANS_IN, |
| 162 | /** USB OUT transaction. */ |
| 163 | USB_TRANS_OUT |
| 164 | }; |
| 165 | |
| 166 | /** USB events that generates the device callbacks. */ |
| 167 | enum usb_event { |
| 168 | /** USB VBus changed, with parameter as present/not present. */ |
| 169 | USB_EV_VBUS, |
| 170 | /** USB RESET detected on bus. */ |
| 171 | USB_EV_RESET, |
| 172 | /** USB wakeup. */ |
| 173 | USB_EV_WAKEUP, |
| 174 | /** USB LPM suspend, with parameter as \ref usb_lpm_attributes. */ |
| 175 | USB_EV_LPM_SUSPEND, |
| 176 | /** USB suspend. */ |
| 177 | USB_EV_SUSPEND, |
| 178 | /** USB error, with parameter as error code. */ |
| 179 | USB_EV_ERROR, |
| 180 | /** Number of USB event types. */ |
| 181 | USB_EV_N |
| 182 | }; |
| 183 | |
| 184 | /** Control action for USB device endpoint stall. */ |
| 185 | enum usb_ep_stall_ctrl { |
| 186 | /** Clear stall of the endpoint. */ |
| 187 | USB_EP_STALL_CLR, |
| 188 | /** Stall the endpoint. */ |
| 189 | USB_EP_STALL_SET, |
| 190 | /** Return the stall status. */ |
| 191 | USB_EP_STALL_GET |
| 192 | }; |
| 193 | |
| 194 | /** Control action for USB device endpoint halt. */ |
| 195 | enum usb_ep_halt_ctrl { |
| 196 | /** Clear halt of the endpoint. */ |
| 197 | USB_EP_HALT_CLR = USB_EP_STALL_CLR, |
| 198 | /** Stall the endpoint. */ |
| 199 | USB_EP_HALT_SET = USB_EP_STALL_SET, |
| 200 | /** Return the halt status. */ |
| 201 | USB_EP_HALT_GET = USB_EP_STALL_GET |
| 202 | }; |
| 203 | |
| 204 | /** USB transactions status codes. */ |
| 205 | enum usb_trans_code { |
| 206 | /** TX or RX has been done without error. */ |
| 207 | USB_TRANS_DONE, |
| 208 | /** The endpoint is stalled. */ |
| 209 | USB_TRANS_STALL, |
| 210 | /** The endpoint transactions are aborted (cancel, control setup/status). */ |
| 211 | USB_TRANS_ABORT, |
| 212 | /** The endpoint transactions are aborted by reset/disable. */ |
| 213 | USB_TRANS_RESET, |
| 214 | /** Error is reported on the endpoint. */ |
| 215 | USB_TRANS_ERROR |
| 216 | }; |
| 217 | |
| 218 | /** Transfer status codes. */ |
| 219 | enum usb_xfer_code { |
| 220 | /** Transfer is done without error, for ctrl it means status packet done. */ |
| 221 | USB_XFER_DONE, |
| 222 | /** For control transfer only, data stage is done without error. */ |
| 223 | USB_XFER_DATA, |
| 224 | /** Endpoint stall is set. */ |
| 225 | USB_XFER_HALT, |
| 226 | /** Endpoint stall is cleared. */ |
| 227 | USB_XFER_UNHALT, |
| 228 | /** Transfer is aborted. */ |
| 229 | USB_XFER_ABORT, |
| 230 | /** Transfer is aborted because endpoint reset/disable. */ |
| 231 | USB_XFER_RESET, |
| 232 | /** There was an error. */ |
| 233 | USB_XFER_ERROR |
| 234 | }; |
| 235 | |
| 236 | /** USB endpoint errors. */ |
| 237 | enum usb_ep_error { |
| 238 | /** No error. */ |
| 239 | USB_EP_NO_ERROR, |
| 240 | /** CRC error. */ |
| 241 | USB_EP_ERR_CRC, |
| 242 | /** Endpoint transfer overflow. */ |
| 243 | USB_EP_ERR_OVERFLOW, |
| 244 | /** Other endpoint errors. */ |
| 245 | USB_EP_ERR_GENERAL |
| 246 | }; |
| 247 | |
| 248 | /** Endpoint transfer state. */ |
| 249 | enum usb_ep_state { |
| 250 | /** Endpoint is disabled. */ |
| 251 | USB_EP_S_DISABLED, |
| 252 | /** Endpoint is not busy. */ |
| 253 | USB_EP_S_IDLE, |
| 254 | /** Control transfer only, endpoint is transferring setup packet. */ |
| 255 | USB_EP_S_X_SETUP, |
| 256 | /** Endpoint is transferring data. */ |
| 257 | USB_EP_S_X_DATA, |
| 258 | /** Control transfer only, endpoint is in status stage. */ |
| 259 | USB_EP_S_X_STATUS, |
| 260 | /** Endpoint is halted. */ |
| 261 | USB_EP_S_HALTED, |
| 262 | /** Endpoint error. */ |
| 263 | USB_EP_S_ERROR |
| 264 | }; |
| 265 | |
| 266 | #ifdef __cplusplus |
| 267 | } |
| 268 | #endif |
| 269 | |
| 270 | #endif /* _HPL_USB_H_INCLUDED */ |