USB device stack

Dependents:   mbed-mX-USB-TEST1 USBMSD_SD_HID_HelloWorld HidTest MIDI_usb_bridge ... more

Legacy Warning

This is an mbed 2 library. To learn more about mbed OS 5, visit the docs.

Pull requests against this repository are no longer supported. Please raise against mbed OS 5 as documented above.

Committer:
jessexm
Date:
Fri Nov 11 17:34:06 2016 +0000
Revision:
68:e62755e5cd1e
Parent:
67:6e1433359654
Child:
69:c5e178adb138
[MAX32630FTHR] Adding new platform

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 49:bee5808e91e3 1 /*******************************************************************************
enginerd 67:6e1433359654 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
mbed_official 49:bee5808e91e3 3 *
mbed_official 49:bee5808e91e3 4 * Permission is hereby granted, free of charge, to any person obtaining a
mbed_official 49:bee5808e91e3 5 * copy of this software and associated documentation files (the "Software"),
mbed_official 49:bee5808e91e3 6 * to deal in the Software without restriction, including without limitation
mbed_official 49:bee5808e91e3 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
mbed_official 49:bee5808e91e3 8 * and/or sell copies of the Software, and to permit persons to whom the
mbed_official 49:bee5808e91e3 9 * Software is furnished to do so, subject to the following conditions:
mbed_official 49:bee5808e91e3 10 *
mbed_official 49:bee5808e91e3 11 * The above copyright notice and this permission notice shall be included
mbed_official 49:bee5808e91e3 12 * in all copies or substantial portions of the Software.
mbed_official 49:bee5808e91e3 13 *
mbed_official 49:bee5808e91e3 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
mbed_official 49:bee5808e91e3 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
mbed_official 49:bee5808e91e3 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
mbed_official 49:bee5808e91e3 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
mbed_official 49:bee5808e91e3 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
mbed_official 49:bee5808e91e3 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
mbed_official 49:bee5808e91e3 20 * OTHER DEALINGS IN THE SOFTWARE.
mbed_official 49:bee5808e91e3 21 *
mbed_official 49:bee5808e91e3 22 * Except as contained in this notice, the name of Maxim Integrated
mbed_official 49:bee5808e91e3 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
mbed_official 49:bee5808e91e3 24 * Products, Inc. Branding Policy.
mbed_official 49:bee5808e91e3 25 *
mbed_official 49:bee5808e91e3 26 * The mere transfer of this software does not imply any licenses
mbed_official 49:bee5808e91e3 27 * of trade secrets, proprietary technology, copyrights, patents,
mbed_official 49:bee5808e91e3 28 * trademarks, maskwork rights, or any other form of intellectual
mbed_official 49:bee5808e91e3 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
mbed_official 49:bee5808e91e3 30 * ownership rights.
mbed_official 49:bee5808e91e3 31 *******************************************************************************
mbed_official 49:bee5808e91e3 32 */
mbed_official 49:bee5808e91e3 33
mbed_official 49:bee5808e91e3 34 #if defined(TARGET_Maxim)
mbed_official 49:bee5808e91e3 35
mbed_official 49:bee5808e91e3 36 #include "USBHAL.h"
mbed_official 49:bee5808e91e3 37 #include "usb_regs.h"
mbed_official 49:bee5808e91e3 38 #include "clkman_regs.h"
mbed_official 49:bee5808e91e3 39
mbed_official 49:bee5808e91e3 40 #define CONNECT_INTS (MXC_F_USB_DEV_INTEN_BRST | MXC_F_USB_DEV_INTEN_SETUP | MXC_F_USB_DEV_INTEN_EP_IN | MXC_F_USB_DEV_INTEN_EP_OUT | MXC_F_USB_DEV_INTEN_DMA_ERR)
mbed_official 49:bee5808e91e3 41
mbed_official 49:bee5808e91e3 42 USBHAL *USBHAL::instance;
mbed_official 49:bee5808e91e3 43
mbed_official 49:bee5808e91e3 44 typedef struct {
mbed_official 49:bee5808e91e3 45 volatile uint32_t buf0_desc;
mbed_official 49:bee5808e91e3 46 volatile uint32_t buf0_address;
mbed_official 49:bee5808e91e3 47 volatile uint32_t buf1_desc;
mbed_official 49:bee5808e91e3 48 volatile uint32_t buf1_address;
mbed_official 49:bee5808e91e3 49 } ep_buffer_t;
mbed_official 49:bee5808e91e3 50
mbed_official 49:bee5808e91e3 51 typedef struct {
mbed_official 49:bee5808e91e3 52 ep_buffer_t out_buffer;
mbed_official 49:bee5808e91e3 53 ep_buffer_t in_buffer;
mbed_official 49:bee5808e91e3 54 } ep0_buffer_t;
mbed_official 49:bee5808e91e3 55
mbed_official 49:bee5808e91e3 56 typedef struct {
mbed_official 49:bee5808e91e3 57 ep0_buffer_t ep0;
mbed_official 49:bee5808e91e3 58 ep_buffer_t ep[MXC_USB_NUM_EP - 1];
mbed_official 49:bee5808e91e3 59 } ep_buffer_descriptor_t;
mbed_official 49:bee5808e91e3 60
enginerd 67:6e1433359654 61 // Static storage for endpoint buffer descriptor table. Must be 512 byte aligned for DMA.
mbed_official 49:bee5808e91e3 62 #ifdef __IAR_SYSTEMS_ICC__
mbed_official 49:bee5808e91e3 63 #pragma data_alignment = 512
mbed_official 49:bee5808e91e3 64 #else
mbed_official 49:bee5808e91e3 65 __attribute__ ((aligned (512)))
mbed_official 49:bee5808e91e3 66 #endif
mbed_official 49:bee5808e91e3 67 ep_buffer_descriptor_t ep_buffer_descriptor;
mbed_official 49:bee5808e91e3 68
enginerd 67:6e1433359654 69 // static storage for temporary data buffers. Must be 32 byte aligned.
mbed_official 49:bee5808e91e3 70 #ifdef __IAR_SYSTEMS_ICC__
mbed_official 49:bee5808e91e3 71 #pragma data_alignment = 4
mbed_official 49:bee5808e91e3 72 #else
mbed_official 49:bee5808e91e3 73 __attribute__ ((aligned (4)))
mbed_official 49:bee5808e91e3 74 #endif
mbed_official 49:bee5808e91e3 75 static uint8_t aligned_buffer[NUMBER_OF_LOGICAL_ENDPOINTS][MXC_USB_MAX_PACKET];
mbed_official 49:bee5808e91e3 76
enginerd 67:6e1433359654 77 // control packet state
mbed_official 49:bee5808e91e3 78 static enum {
mbed_official 49:bee5808e91e3 79 CTRL_NONE = 0,
mbed_official 49:bee5808e91e3 80 CTRL_SETUP,
mbed_official 49:bee5808e91e3 81 CTRL_OUT,
mbed_official 49:bee5808e91e3 82 CTRL_IN,
mbed_official 49:bee5808e91e3 83 } control_state;
mbed_official 49:bee5808e91e3 84
mbed_official 49:bee5808e91e3 85 USBHAL::USBHAL(void)
mbed_official 49:bee5808e91e3 86 {
mbed_official 49:bee5808e91e3 87 NVIC_DisableIRQ(USB_IRQn);
mbed_official 49:bee5808e91e3 88
enginerd 67:6e1433359654 89 #if defined(TARGET_MAX32600)
mbed_official 49:bee5808e91e3 90 // The PLL must be enabled for USB
mbed_official 49:bee5808e91e3 91 MBED_ASSERT(MXC_CLKMAN->clk_config & MXC_F_CLKMAN_CLK_CONFIG_PLL_ENABLE);
mbed_official 49:bee5808e91e3 92
mbed_official 49:bee5808e91e3 93 // Enable the USB clock
mbed_official 49:bee5808e91e3 94 MXC_CLKMAN->clk_ctrl |= MXC_F_CLKMAN_CLK_CTRL_USB_GATE_N;
enginerd 67:6e1433359654 95 #elif defined(TARGET_MAX32620)
enginerd 67:6e1433359654 96 // Enable the USB clock
enginerd 67:6e1433359654 97 MXC_CLKMAN->clk_ctrl |= MXC_F_CLKMAN_CLK_CTRL_USB_CLOCK_ENABLE;
jessexm 68:e62755e5cd1e 98 #elif defined(TARGET_MAX32625) || defined(TARGET_MAX32630)
jessexm 68:e62755e5cd1e 99 MXC_PWRMAN->pwr_rst_ctrl |= MXC_F_PWRMAN_PWR_RST_CTRL_USB_POWERED;
jessexm 68:e62755e5cd1e 100 MXC_CLKMAN->clk_ctrl |= MXC_F_CLKMAN_CLK_CTRL_USB_CLOCK_ENABLE;
enginerd 67:6e1433359654 101 #endif
mbed_official 49:bee5808e91e3 102
mbed_official 49:bee5808e91e3 103 // reset the device
mbed_official 49:bee5808e91e3 104 MXC_USB->cn = 0;
enginerd 67:6e1433359654 105 MXC_USB->cn = MXC_F_USB_CN_USB_EN;
mbed_official 49:bee5808e91e3 106 MXC_USB->dev_inten = 0;
mbed_official 49:bee5808e91e3 107 MXC_USB->dev_cn = 0;
mbed_official 49:bee5808e91e3 108 MXC_USB->dev_cn = MXC_F_USB_DEV_CN_URST;
mbed_official 49:bee5808e91e3 109 MXC_USB->dev_cn = 0;
mbed_official 49:bee5808e91e3 110
mbed_official 49:bee5808e91e3 111 // fill in callback arrays
mbed_official 49:bee5808e91e3 112 epCallback[EP0OUT] = NULL;
mbed_official 49:bee5808e91e3 113 epCallback[EP0IN] = NULL;
mbed_official 49:bee5808e91e3 114 epCallback[EP1OUT] = &USBHAL::EP1_OUT_callback;
mbed_official 49:bee5808e91e3 115 epCallback[EP1IN ] = &USBHAL::EP1_IN_callback;
mbed_official 49:bee5808e91e3 116 epCallback[EP2OUT] = &USBHAL::EP2_OUT_callback;
mbed_official 49:bee5808e91e3 117 epCallback[EP2IN ] = &USBHAL::EP2_IN_callback;
mbed_official 49:bee5808e91e3 118 epCallback[EP3OUT] = &USBHAL::EP3_OUT_callback;
mbed_official 49:bee5808e91e3 119 epCallback[EP3IN ] = &USBHAL::EP3_IN_callback;
mbed_official 49:bee5808e91e3 120 epCallback[EP4OUT] = &USBHAL::EP4_OUT_callback;
mbed_official 49:bee5808e91e3 121 epCallback[EP4IN ] = &USBHAL::EP4_IN_callback;
mbed_official 49:bee5808e91e3 122 epCallback[EP5OUT] = &USBHAL::EP5_OUT_callback;
mbed_official 49:bee5808e91e3 123 epCallback[EP5IN ] = &USBHAL::EP5_IN_callback;
mbed_official 49:bee5808e91e3 124 epCallback[EP6OUT] = &USBHAL::EP6_OUT_callback;
mbed_official 49:bee5808e91e3 125 epCallback[EP6IN ] = &USBHAL::EP6_IN_callback;
mbed_official 49:bee5808e91e3 126 epCallback[EP7OUT] = &USBHAL::EP7_OUT_callback;
mbed_official 49:bee5808e91e3 127 epCallback[EP7IN ] = &USBHAL::EP7_IN_callback;
mbed_official 49:bee5808e91e3 128
mbed_official 49:bee5808e91e3 129 // clear driver state
mbed_official 49:bee5808e91e3 130 control_state = CTRL_NONE;
mbed_official 49:bee5808e91e3 131
mbed_official 49:bee5808e91e3 132 // set the descriptor location
mbed_official 49:bee5808e91e3 133 MXC_USB->ep_base = (uint32_t)&ep_buffer_descriptor;
mbed_official 49:bee5808e91e3 134
enginerd 67:6e1433359654 135 // enable VBUS interrupts
enginerd 67:6e1433359654 136 MXC_USB->dev_inten = MXC_F_USB_DEV_INTEN_NO_VBUS | MXC_F_USB_DEV_INTEN_VBUS;
enginerd 67:6e1433359654 137
mbed_official 49:bee5808e91e3 138 // attach IRQ handler and enable interrupts
mbed_official 49:bee5808e91e3 139 instance = this;
mbed_official 49:bee5808e91e3 140 NVIC_SetVector(USB_IRQn, (uint32_t)&_usbisr);
mbed_official 49:bee5808e91e3 141 NVIC_EnableIRQ(USB_IRQn);
mbed_official 49:bee5808e91e3 142 }
mbed_official 49:bee5808e91e3 143
mbed_official 49:bee5808e91e3 144 USBHAL::~USBHAL(void)
mbed_official 49:bee5808e91e3 145 {
mbed_official 49:bee5808e91e3 146 MXC_USB->dev_cn = MXC_F_USB_DEV_CN_URST;
mbed_official 49:bee5808e91e3 147 MXC_USB->dev_cn = 0;
mbed_official 49:bee5808e91e3 148 MXC_USB->cn = 0;
mbed_official 49:bee5808e91e3 149 }
mbed_official 49:bee5808e91e3 150
mbed_official 49:bee5808e91e3 151 void USBHAL::connect(void)
mbed_official 49:bee5808e91e3 152 {
mbed_official 49:bee5808e91e3 153 // enable interrupts
mbed_official 49:bee5808e91e3 154 MXC_USB->dev_inten |= CONNECT_INTS;
mbed_official 49:bee5808e91e3 155
mbed_official 49:bee5808e91e3 156 // allow interrupts on ep0
mbed_official 49:bee5808e91e3 157 MXC_USB->ep[0] |= MXC_F_USB_EP_INT_EN;
mbed_official 49:bee5808e91e3 158
mbed_official 49:bee5808e91e3 159 // pullup enable
mbed_official 49:bee5808e91e3 160 MXC_USB->dev_cn |= (MXC_F_USB_DEV_CN_CONNECT | MXC_F_USB_DEV_CN_FIFO_MODE);
mbed_official 49:bee5808e91e3 161 }
mbed_official 49:bee5808e91e3 162
mbed_official 49:bee5808e91e3 163 void USBHAL::disconnect(void)
mbed_official 49:bee5808e91e3 164 {
mbed_official 49:bee5808e91e3 165 // disable interrupts
mbed_official 49:bee5808e91e3 166 MXC_USB->dev_inten &= ~CONNECT_INTS;
mbed_official 49:bee5808e91e3 167
mbed_official 49:bee5808e91e3 168 // disable pullup
mbed_official 49:bee5808e91e3 169 MXC_USB->dev_cn &= ~MXC_F_USB_DEV_CN_CONNECT;
mbed_official 49:bee5808e91e3 170 }
mbed_official 49:bee5808e91e3 171
mbed_official 49:bee5808e91e3 172 void USBHAL::configureDevice(void)
mbed_official 49:bee5808e91e3 173 {
mbed_official 49:bee5808e91e3 174 // do nothing
mbed_official 49:bee5808e91e3 175 }
mbed_official 49:bee5808e91e3 176
mbed_official 49:bee5808e91e3 177 void USBHAL::unconfigureDevice(void)
mbed_official 49:bee5808e91e3 178 {
mbed_official 49:bee5808e91e3 179 // reset endpoints
mbed_official 49:bee5808e91e3 180 for (int i = 0; i < MXC_USB_NUM_EP; i++) {
mbed_official 49:bee5808e91e3 181 // Disable endpoint and clear the data toggle
mbed_official 49:bee5808e91e3 182 MXC_USB->ep[i] &= ~MXC_F_USB_EP_DIR;
mbed_official 49:bee5808e91e3 183 MXC_USB->ep[i] |= MXC_F_USB_EP_DT;
mbed_official 49:bee5808e91e3 184 }
mbed_official 49:bee5808e91e3 185 }
mbed_official 49:bee5808e91e3 186
mbed_official 49:bee5808e91e3 187 void USBHAL::setAddress(uint8_t address)
mbed_official 49:bee5808e91e3 188 {
mbed_official 49:bee5808e91e3 189 // do nothing
mbed_official 49:bee5808e91e3 190 }
mbed_official 49:bee5808e91e3 191
mbed_official 49:bee5808e91e3 192 void USBHAL::remoteWakeup(void)
mbed_official 49:bee5808e91e3 193 {
mbed_official 49:bee5808e91e3 194 // do nothing
mbed_official 49:bee5808e91e3 195 }
mbed_official 49:bee5808e91e3 196
mbed_official 49:bee5808e91e3 197 static ep_buffer_t *get_desc(uint8_t endpoint)
mbed_official 49:bee5808e91e3 198 {
mbed_official 49:bee5808e91e3 199 uint8_t epnum = EP_NUM(endpoint);
mbed_official 49:bee5808e91e3 200 ep_buffer_t *desc;
mbed_official 49:bee5808e91e3 201
mbed_official 49:bee5808e91e3 202 if (epnum == 0) {
mbed_official 49:bee5808e91e3 203 if (IN_EP(endpoint)) {
mbed_official 49:bee5808e91e3 204 desc = &ep_buffer_descriptor.ep0.in_buffer;
mbed_official 49:bee5808e91e3 205 } else {
mbed_official 49:bee5808e91e3 206 desc = &ep_buffer_descriptor.ep0.out_buffer;
mbed_official 49:bee5808e91e3 207 }
mbed_official 49:bee5808e91e3 208 } else {
mbed_official 49:bee5808e91e3 209 desc = &ep_buffer_descriptor.ep[epnum - 1];
mbed_official 49:bee5808e91e3 210 }
mbed_official 49:bee5808e91e3 211
mbed_official 49:bee5808e91e3 212 return desc;
mbed_official 49:bee5808e91e3 213 }
mbed_official 49:bee5808e91e3 214
mbed_official 49:bee5808e91e3 215 void USBHAL::EP0setup(uint8_t *buffer)
mbed_official 49:bee5808e91e3 216 {
enginerd 67:6e1433359654 217 // Setup packet is fixed at 8 bytes
enginerd 67:6e1433359654 218 // Setup registers cannot be read in byte mode
enginerd 67:6e1433359654 219 uint32_t *ptr32 = (uint32_t*)buffer;
enginerd 67:6e1433359654 220 ptr32[0] = (uint32_t)MXC_USB->setup0;
enginerd 67:6e1433359654 221 ptr32[1] = (uint32_t)MXC_USB->setup1;
mbed_official 49:bee5808e91e3 222 }
mbed_official 49:bee5808e91e3 223
mbed_official 49:bee5808e91e3 224 void USBHAL::EP0read(void)
mbed_official 49:bee5808e91e3 225 {
mbed_official 49:bee5808e91e3 226 if (control_state == CTRL_IN) {
mbed_official 49:bee5808e91e3 227 // This is the status stage. ACK.
mbed_official 49:bee5808e91e3 228 MXC_USB->ep[0] |= MXC_F_USB_EP_ST_ACK;
mbed_official 49:bee5808e91e3 229 control_state = CTRL_NONE;
mbed_official 49:bee5808e91e3 230 return;
mbed_official 49:bee5808e91e3 231 }
mbed_official 49:bee5808e91e3 232
mbed_official 49:bee5808e91e3 233 control_state = CTRL_OUT;
mbed_official 49:bee5808e91e3 234
mbed_official 49:bee5808e91e3 235 endpointRead(EP0OUT, MAX_PACKET_SIZE_EP0);
mbed_official 49:bee5808e91e3 236 }
mbed_official 49:bee5808e91e3 237
mbed_official 49:bee5808e91e3 238 void USBHAL::EP0readStage(void)
mbed_official 49:bee5808e91e3 239 {
mbed_official 49:bee5808e91e3 240 // do nothing
mbed_official 49:bee5808e91e3 241 }
mbed_official 49:bee5808e91e3 242
mbed_official 49:bee5808e91e3 243 uint32_t USBHAL::EP0getReadResult(uint8_t *buffer)
mbed_official 49:bee5808e91e3 244 {
mbed_official 49:bee5808e91e3 245 uint32_t size;
mbed_official 49:bee5808e91e3 246
mbed_official 49:bee5808e91e3 247 if (MXC_USB->out_owner & 1) {
mbed_official 49:bee5808e91e3 248 return 0;
mbed_official 49:bee5808e91e3 249 }
mbed_official 49:bee5808e91e3 250
mbed_official 49:bee5808e91e3 251 // get the packet length and contents
mbed_official 49:bee5808e91e3 252 ep_buffer_t *desc = get_desc(EP0OUT);
mbed_official 49:bee5808e91e3 253 size = desc->buf0_desc;
mbed_official 49:bee5808e91e3 254 memcpy(buffer, aligned_buffer[0], size);
mbed_official 49:bee5808e91e3 255
mbed_official 49:bee5808e91e3 256 return size;
mbed_official 49:bee5808e91e3 257 }
mbed_official 49:bee5808e91e3 258
mbed_official 49:bee5808e91e3 259 void USBHAL::EP0write(uint8_t *buffer, uint32_t size)
mbed_official 49:bee5808e91e3 260 {
mbed_official 49:bee5808e91e3 261 if ((size == 0) && (control_state != CTRL_IN)) {
mbed_official 49:bee5808e91e3 262 // This is a status stage ACK. Handle in hardware.
mbed_official 49:bee5808e91e3 263 MXC_USB->ep[0] |= MXC_F_USB_EP_ST_ACK;
mbed_official 49:bee5808e91e3 264 control_state = CTRL_NONE;
mbed_official 49:bee5808e91e3 265 return;
mbed_official 49:bee5808e91e3 266 }
mbed_official 49:bee5808e91e3 267
mbed_official 49:bee5808e91e3 268 control_state = CTRL_IN;
mbed_official 49:bee5808e91e3 269
mbed_official 49:bee5808e91e3 270 endpointWrite(EP0IN, buffer, size);
mbed_official 49:bee5808e91e3 271 }
mbed_official 49:bee5808e91e3 272
mbed_official 49:bee5808e91e3 273 void USBHAL::EP0stall(void)
mbed_official 49:bee5808e91e3 274 {
mbed_official 49:bee5808e91e3 275 stallEndpoint(0);
mbed_official 49:bee5808e91e3 276 }
mbed_official 49:bee5808e91e3 277
mbed_official 49:bee5808e91e3 278 EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize)
mbed_official 49:bee5808e91e3 279 {
mbed_official 49:bee5808e91e3 280 uint8_t epnum = EP_NUM(endpoint);
mbed_official 49:bee5808e91e3 281
mbed_official 49:bee5808e91e3 282 if ((endpoint >= NUMBER_OF_PHYSICAL_ENDPOINTS) || IN_EP(endpoint)) {
mbed_official 49:bee5808e91e3 283 return EP_INVALID;
mbed_official 49:bee5808e91e3 284 }
mbed_official 49:bee5808e91e3 285
mbed_official 49:bee5808e91e3 286 if (maximumSize > MXC_USB_MAX_PACKET) {
mbed_official 49:bee5808e91e3 287 return EP_INVALID;
mbed_official 49:bee5808e91e3 288 }
mbed_official 49:bee5808e91e3 289
mbed_official 49:bee5808e91e3 290 uint32_t mask = (1 << epnum);
mbed_official 49:bee5808e91e3 291 if (MXC_USB->out_owner & mask) {
mbed_official 49:bee5808e91e3 292 return EP_INVALID;
mbed_official 49:bee5808e91e3 293 }
mbed_official 49:bee5808e91e3 294
mbed_official 49:bee5808e91e3 295 ep_buffer_t *desc = get_desc(endpoint);
mbed_official 49:bee5808e91e3 296 desc->buf0_desc = maximumSize;
mbed_official 49:bee5808e91e3 297 desc->buf0_address = (uint32_t)aligned_buffer[epnum];
mbed_official 49:bee5808e91e3 298
mbed_official 49:bee5808e91e3 299 MXC_USB->out_owner = mask;
mbed_official 49:bee5808e91e3 300
mbed_official 49:bee5808e91e3 301 return EP_PENDING;
mbed_official 49:bee5808e91e3 302 }
mbed_official 49:bee5808e91e3 303
mbed_official 49:bee5808e91e3 304 EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t *data, uint32_t *bytesRead)
mbed_official 49:bee5808e91e3 305 {
mbed_official 49:bee5808e91e3 306 if ((endpoint >= NUMBER_OF_PHYSICAL_ENDPOINTS) || IN_EP(endpoint)) {
mbed_official 49:bee5808e91e3 307 return EP_INVALID;
mbed_official 49:bee5808e91e3 308 }
mbed_official 49:bee5808e91e3 309
mbed_official 49:bee5808e91e3 310 uint32_t mask = (1 << EP_NUM(endpoint));
mbed_official 49:bee5808e91e3 311 if (MXC_USB->out_owner & mask) {
mbed_official 49:bee5808e91e3 312 return EP_PENDING;
mbed_official 49:bee5808e91e3 313 }
mbed_official 49:bee5808e91e3 314
mbed_official 49:bee5808e91e3 315 // get the packet length and contents
mbed_official 49:bee5808e91e3 316 ep_buffer_t *desc = get_desc(endpoint);
mbed_official 49:bee5808e91e3 317 *bytesRead = desc->buf0_desc;
mbed_official 49:bee5808e91e3 318 memcpy(data, aligned_buffer[EP_NUM(endpoint)], *bytesRead);
mbed_official 49:bee5808e91e3 319
mbed_official 49:bee5808e91e3 320 return EP_COMPLETED;
mbed_official 49:bee5808e91e3 321 }
mbed_official 49:bee5808e91e3 322
mbed_official 49:bee5808e91e3 323 EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size)
mbed_official 49:bee5808e91e3 324 {
mbed_official 49:bee5808e91e3 325 uint8_t epnum = EP_NUM(endpoint);
mbed_official 49:bee5808e91e3 326
mbed_official 49:bee5808e91e3 327 if ((endpoint >= NUMBER_OF_PHYSICAL_ENDPOINTS) || OUT_EP(endpoint)) {
mbed_official 49:bee5808e91e3 328 return EP_INVALID;
mbed_official 49:bee5808e91e3 329 }
mbed_official 49:bee5808e91e3 330
mbed_official 49:bee5808e91e3 331 if (size > MXC_USB_MAX_PACKET) {
mbed_official 49:bee5808e91e3 332 return EP_INVALID;
mbed_official 49:bee5808e91e3 333 }
mbed_official 49:bee5808e91e3 334
mbed_official 49:bee5808e91e3 335 uint32_t mask = (1 << epnum);
mbed_official 49:bee5808e91e3 336 if (MXC_USB->in_owner & mask) {
mbed_official 49:bee5808e91e3 337 return EP_INVALID;
mbed_official 49:bee5808e91e3 338 }
mbed_official 49:bee5808e91e3 339
mbed_official 49:bee5808e91e3 340 memcpy(aligned_buffer[epnum], data, size);
mbed_official 49:bee5808e91e3 341
mbed_official 49:bee5808e91e3 342 ep_buffer_t *desc = get_desc(endpoint);
mbed_official 49:bee5808e91e3 343 desc->buf0_desc = size;
mbed_official 49:bee5808e91e3 344 desc->buf0_address = (uint32_t)aligned_buffer[epnum];
mbed_official 49:bee5808e91e3 345
mbed_official 49:bee5808e91e3 346 // start the DMA
mbed_official 49:bee5808e91e3 347 MXC_USB->in_owner = mask;
mbed_official 49:bee5808e91e3 348
mbed_official 49:bee5808e91e3 349 return EP_PENDING;
mbed_official 49:bee5808e91e3 350 }
mbed_official 49:bee5808e91e3 351
mbed_official 49:bee5808e91e3 352 EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint)
mbed_official 49:bee5808e91e3 353 {
mbed_official 49:bee5808e91e3 354 uint32_t mask = (1 << EP_NUM(endpoint));
mbed_official 49:bee5808e91e3 355 if (MXC_USB->in_owner & mask) {
mbed_official 49:bee5808e91e3 356 return EP_PENDING;
mbed_official 49:bee5808e91e3 357 }
mbed_official 49:bee5808e91e3 358
mbed_official 49:bee5808e91e3 359 return EP_COMPLETED;
mbed_official 49:bee5808e91e3 360 }
mbed_official 49:bee5808e91e3 361
mbed_official 49:bee5808e91e3 362 void USBHAL::stallEndpoint(uint8_t endpoint)
mbed_official 49:bee5808e91e3 363 {
mbed_official 49:bee5808e91e3 364 uint8_t epnum = EP_NUM(endpoint);
mbed_official 49:bee5808e91e3 365
mbed_official 49:bee5808e91e3 366 if (epnum == 0) {
mbed_official 49:bee5808e91e3 367 MXC_USB->ep[epnum] |= MXC_F_USB_EP_ST_STALL;
mbed_official 49:bee5808e91e3 368 }
mbed_official 49:bee5808e91e3 369
mbed_official 49:bee5808e91e3 370 MXC_USB->ep[epnum] |= MXC_F_USB_EP_STALL;
mbed_official 49:bee5808e91e3 371 }
mbed_official 49:bee5808e91e3 372
mbed_official 49:bee5808e91e3 373 void USBHAL::unstallEndpoint(uint8_t endpoint)
mbed_official 49:bee5808e91e3 374 {
mbed_official 49:bee5808e91e3 375 MXC_USB->ep[EP_NUM(endpoint)] &= ~MXC_F_USB_EP_STALL;
mbed_official 49:bee5808e91e3 376 }
mbed_official 49:bee5808e91e3 377
mbed_official 49:bee5808e91e3 378 bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t options)
mbed_official 49:bee5808e91e3 379 {
mbed_official 49:bee5808e91e3 380 uint8_t epnum = EP_NUM(endpoint);
mbed_official 49:bee5808e91e3 381 uint32_t ep_ctrl;
mbed_official 49:bee5808e91e3 382
mbed_official 49:bee5808e91e3 383 if (epnum >= NUMBER_OF_PHYSICAL_ENDPOINTS) {
mbed_official 49:bee5808e91e3 384 return false;
mbed_official 49:bee5808e91e3 385 }
mbed_official 49:bee5808e91e3 386
mbed_official 49:bee5808e91e3 387 if (IN_EP(endpoint)) {
mbed_official 49:bee5808e91e3 388 ep_ctrl = (MXC_V_USB_EP_DIR_IN << MXC_F_USB_EP_DIR_POS);
mbed_official 49:bee5808e91e3 389 } else {
mbed_official 49:bee5808e91e3 390 ep_ctrl = (MXC_S_USB_EP_DIR_OUT << MXC_F_USB_EP_DIR_POS);
mbed_official 49:bee5808e91e3 391 }
mbed_official 49:bee5808e91e3 392
mbed_official 49:bee5808e91e3 393 ep_ctrl |= (MXC_F_USB_EP_DT | MXC_F_USB_EP_INT_EN);
mbed_official 49:bee5808e91e3 394
mbed_official 49:bee5808e91e3 395 MXC_USB->ep[epnum] = ep_ctrl;
mbed_official 49:bee5808e91e3 396
mbed_official 49:bee5808e91e3 397 return true;
mbed_official 49:bee5808e91e3 398 }
mbed_official 49:bee5808e91e3 399
mbed_official 49:bee5808e91e3 400 bool USBHAL::getEndpointStallState(unsigned char endpoint)
mbed_official 49:bee5808e91e3 401 {
mbed_official 49:bee5808e91e3 402 return !!(MXC_USB->ep[endpoint] & MXC_F_USB_EP_STALL);
mbed_official 49:bee5808e91e3 403 }
mbed_official 49:bee5808e91e3 404
mbed_official 49:bee5808e91e3 405 void USBHAL::_usbisr(void)
mbed_official 49:bee5808e91e3 406 {
mbed_official 49:bee5808e91e3 407 instance->usbisr();
mbed_official 49:bee5808e91e3 408 }
mbed_official 49:bee5808e91e3 409
mbed_official 49:bee5808e91e3 410 void USBHAL::usbisr(void)
mbed_official 49:bee5808e91e3 411 {
mbed_official 49:bee5808e91e3 412 // get and clear irqs
mbed_official 49:bee5808e91e3 413 uint32_t irq_flags = MXC_USB->dev_intfl;
mbed_official 49:bee5808e91e3 414 MXC_USB->dev_intfl = irq_flags;
mbed_official 49:bee5808e91e3 415
mbed_official 49:bee5808e91e3 416 // process only enabled interrupts
mbed_official 49:bee5808e91e3 417 irq_flags &= MXC_USB->dev_inten;
mbed_official 49:bee5808e91e3 418
mbed_official 49:bee5808e91e3 419 // suspend
mbed_official 49:bee5808e91e3 420 if (irq_flags & MXC_F_USB_DEV_INTFL_SUSP) {
mbed_official 49:bee5808e91e3 421 suspendStateChanged(1);
mbed_official 49:bee5808e91e3 422 }
mbed_official 49:bee5808e91e3 423
mbed_official 49:bee5808e91e3 424 // bus reset
mbed_official 49:bee5808e91e3 425 if (irq_flags & MXC_F_USB_DEV_INTFL_BRST) {
mbed_official 49:bee5808e91e3 426
mbed_official 49:bee5808e91e3 427 // reset endpoints
mbed_official 49:bee5808e91e3 428 for (int i = 0; i < MXC_USB_NUM_EP; i++) {
mbed_official 49:bee5808e91e3 429 // Disable endpoint and clear the data toggle
mbed_official 49:bee5808e91e3 430 MXC_USB->ep[i] &= ~MXC_F_USB_EP_DIR;
mbed_official 49:bee5808e91e3 431 MXC_USB->ep[i] |= MXC_F_USB_EP_DT;
mbed_official 49:bee5808e91e3 432 }
mbed_official 49:bee5808e91e3 433
mbed_official 49:bee5808e91e3 434 // clear driver state
mbed_official 49:bee5808e91e3 435 control_state = CTRL_NONE;
mbed_official 49:bee5808e91e3 436
mbed_official 49:bee5808e91e3 437 busReset();
mbed_official 49:bee5808e91e3 438
mbed_official 49:bee5808e91e3 439 // no need to process events after reset
mbed_official 49:bee5808e91e3 440 return;
mbed_official 49:bee5808e91e3 441 }
mbed_official 49:bee5808e91e3 442
mbed_official 49:bee5808e91e3 443 // Setup packet
mbed_official 49:bee5808e91e3 444 if (irq_flags & MXC_F_USB_DEV_INTFL_SETUP) {
mbed_official 49:bee5808e91e3 445 control_state = CTRL_SETUP;
mbed_official 49:bee5808e91e3 446 EP0setupCallback();
mbed_official 49:bee5808e91e3 447 }
mbed_official 49:bee5808e91e3 448
mbed_official 49:bee5808e91e3 449 // IN packets
mbed_official 49:bee5808e91e3 450 if (irq_flags & MXC_F_USB_DEV_INTFL_EP_IN) {
mbed_official 49:bee5808e91e3 451 // get and clear IN irqs
mbed_official 49:bee5808e91e3 452 uint32_t in_irqs = MXC_USB->in_int;
mbed_official 49:bee5808e91e3 453 MXC_USB->in_int = in_irqs;
mbed_official 49:bee5808e91e3 454
mbed_official 49:bee5808e91e3 455 if (in_irqs & 1) {
mbed_official 49:bee5808e91e3 456 EP0in();
mbed_official 49:bee5808e91e3 457 }
mbed_official 49:bee5808e91e3 458
mbed_official 49:bee5808e91e3 459 for (uint8_t epnum = 1; epnum < NUMBER_OF_LOGICAL_ENDPOINTS; epnum++) {
mbed_official 49:bee5808e91e3 460 uint32_t irq_mask = (1 << epnum);
mbed_official 49:bee5808e91e3 461 if (in_irqs & irq_mask) {
mbed_official 49:bee5808e91e3 462 uint8_t endpoint = (epnum << 1) | DIR_IN;
mbed_official 49:bee5808e91e3 463 (instance->*(epCallback[endpoint]))();
mbed_official 49:bee5808e91e3 464 }
mbed_official 49:bee5808e91e3 465 }
mbed_official 49:bee5808e91e3 466 }
mbed_official 49:bee5808e91e3 467
mbed_official 49:bee5808e91e3 468 // OUT packets
mbed_official 49:bee5808e91e3 469 if (irq_flags & MXC_F_USB_DEV_INTFL_EP_OUT) {
mbed_official 49:bee5808e91e3 470 // get and clear OUT irqs
mbed_official 49:bee5808e91e3 471 uint32_t out_irqs = MXC_USB->out_int;
mbed_official 49:bee5808e91e3 472 MXC_USB->out_int = out_irqs;
mbed_official 49:bee5808e91e3 473
mbed_official 49:bee5808e91e3 474 if (out_irqs & 1) {
mbed_official 49:bee5808e91e3 475 EP0out();
mbed_official 49:bee5808e91e3 476 }
mbed_official 49:bee5808e91e3 477
mbed_official 49:bee5808e91e3 478 for (uint8_t epnum = 1; epnum < NUMBER_OF_LOGICAL_ENDPOINTS; epnum++) {
mbed_official 49:bee5808e91e3 479 uint32_t irq_mask = (1 << epnum);
mbed_official 49:bee5808e91e3 480 if (out_irqs & irq_mask) {
mbed_official 49:bee5808e91e3 481 uint8_t endpoint = (epnum << 1) | DIR_OUT;
mbed_official 49:bee5808e91e3 482 (instance->*(epCallback[endpoint]))();
mbed_official 49:bee5808e91e3 483 }
mbed_official 49:bee5808e91e3 484 }
mbed_official 49:bee5808e91e3 485 }
mbed_official 49:bee5808e91e3 486 }
mbed_official 49:bee5808e91e3 487
mbed_official 49:bee5808e91e3 488 #endif