Team Walter / USBDevice

Fork of USBDevice by Walter Luu

Committer:
jessexm
Date:
Mon Jun 12 23:05:18 2017 +0000
Revision:
67:8ca8b458434a
Parent:
66:c5e178adb138
Child:
68:17ac7abb27a7
Workaround for API incompatibility

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 49:bee5808e91e3 1 /*******************************************************************************
enginerd 64: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
jessexm 66:c5e178adb138 40 #if defined(TARGET_MAX32625) || defined(TARGET_MAX32630)
jessexm 66:c5e178adb138 41 #include "pwrman_regs.h"
jessexm 66:c5e178adb138 42 #endif
jessexm 66:c5e178adb138 43
mbed_official 49:bee5808e91e3 44 #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 45
mbed_official 49:bee5808e91e3 46 USBHAL *USBHAL::instance;
mbed_official 49:bee5808e91e3 47
mbed_official 49:bee5808e91e3 48 typedef struct {
mbed_official 49:bee5808e91e3 49 volatile uint32_t buf0_desc;
mbed_official 49:bee5808e91e3 50 volatile uint32_t buf0_address;
mbed_official 49:bee5808e91e3 51 volatile uint32_t buf1_desc;
mbed_official 49:bee5808e91e3 52 volatile uint32_t buf1_address;
mbed_official 49:bee5808e91e3 53 } ep_buffer_t;
mbed_official 49:bee5808e91e3 54
mbed_official 49:bee5808e91e3 55 typedef struct {
mbed_official 49:bee5808e91e3 56 ep_buffer_t out_buffer;
mbed_official 49:bee5808e91e3 57 ep_buffer_t in_buffer;
mbed_official 49:bee5808e91e3 58 } ep0_buffer_t;
mbed_official 49:bee5808e91e3 59
mbed_official 49:bee5808e91e3 60 typedef struct {
mbed_official 49:bee5808e91e3 61 ep0_buffer_t ep0;
mbed_official 49:bee5808e91e3 62 ep_buffer_t ep[MXC_USB_NUM_EP - 1];
mbed_official 49:bee5808e91e3 63 } ep_buffer_descriptor_t;
mbed_official 49:bee5808e91e3 64
enginerd 64:6e1433359654 65 // Static storage for endpoint buffer descriptor table. Must be 512 byte aligned for DMA.
mbed_official 49:bee5808e91e3 66 #ifdef __IAR_SYSTEMS_ICC__
mbed_official 49:bee5808e91e3 67 #pragma data_alignment = 512
mbed_official 49:bee5808e91e3 68 #else
mbed_official 49:bee5808e91e3 69 __attribute__ ((aligned (512)))
mbed_official 49:bee5808e91e3 70 #endif
mbed_official 49:bee5808e91e3 71 ep_buffer_descriptor_t ep_buffer_descriptor;
mbed_official 49:bee5808e91e3 72
enginerd 64:6e1433359654 73 // static storage for temporary data buffers. Must be 32 byte aligned.
mbed_official 49:bee5808e91e3 74 #ifdef __IAR_SYSTEMS_ICC__
mbed_official 49:bee5808e91e3 75 #pragma data_alignment = 4
mbed_official 49:bee5808e91e3 76 #else
mbed_official 49:bee5808e91e3 77 __attribute__ ((aligned (4)))
mbed_official 49:bee5808e91e3 78 #endif
mbed_official 49:bee5808e91e3 79 static uint8_t aligned_buffer[NUMBER_OF_LOGICAL_ENDPOINTS][MXC_USB_MAX_PACKET];
mbed_official 49:bee5808e91e3 80
enginerd 64:6e1433359654 81 // control packet state
mbed_official 49:bee5808e91e3 82 static enum {
mbed_official 49:bee5808e91e3 83 CTRL_NONE = 0,
mbed_official 49:bee5808e91e3 84 CTRL_SETUP,
mbed_official 49:bee5808e91e3 85 CTRL_OUT,
mbed_official 49:bee5808e91e3 86 CTRL_IN,
mbed_official 49:bee5808e91e3 87 } control_state;
mbed_official 49:bee5808e91e3 88
mbed_official 49:bee5808e91e3 89 USBHAL::USBHAL(void)
mbed_official 49:bee5808e91e3 90 {
mbed_official 49:bee5808e91e3 91 NVIC_DisableIRQ(USB_IRQn);
mbed_official 49:bee5808e91e3 92
enginerd 64:6e1433359654 93 #if defined(TARGET_MAX32600)
mbed_official 49:bee5808e91e3 94 // The PLL must be enabled for USB
mbed_official 49:bee5808e91e3 95 MBED_ASSERT(MXC_CLKMAN->clk_config & MXC_F_CLKMAN_CLK_CONFIG_PLL_ENABLE);
mbed_official 49:bee5808e91e3 96
mbed_official 49:bee5808e91e3 97 // Enable the USB clock
mbed_official 49:bee5808e91e3 98 MXC_CLKMAN->clk_ctrl |= MXC_F_CLKMAN_CLK_CTRL_USB_GATE_N;
enginerd 64:6e1433359654 99 #elif defined(TARGET_MAX32620)
enginerd 64:6e1433359654 100 // Enable the USB clock
enginerd 64:6e1433359654 101 MXC_CLKMAN->clk_ctrl |= MXC_F_CLKMAN_CLK_CTRL_USB_CLOCK_ENABLE;
jessexm 65:e62755e5cd1e 102 #elif defined(TARGET_MAX32625) || defined(TARGET_MAX32630)
jessexm 65:e62755e5cd1e 103 MXC_PWRMAN->pwr_rst_ctrl |= MXC_F_PWRMAN_PWR_RST_CTRL_USB_POWERED;
jessexm 65:e62755e5cd1e 104 MXC_CLKMAN->clk_ctrl |= MXC_F_CLKMAN_CLK_CTRL_USB_CLOCK_ENABLE;
enginerd 64:6e1433359654 105 #endif
mbed_official 49:bee5808e91e3 106
mbed_official 49:bee5808e91e3 107 // reset the device
mbed_official 49:bee5808e91e3 108 MXC_USB->cn = 0;
enginerd 64:6e1433359654 109 MXC_USB->cn = MXC_F_USB_CN_USB_EN;
mbed_official 49:bee5808e91e3 110 MXC_USB->dev_inten = 0;
mbed_official 49:bee5808e91e3 111 MXC_USB->dev_cn = 0;
mbed_official 49:bee5808e91e3 112 MXC_USB->dev_cn = MXC_F_USB_DEV_CN_URST;
mbed_official 49:bee5808e91e3 113 MXC_USB->dev_cn = 0;
mbed_official 49:bee5808e91e3 114
mbed_official 49:bee5808e91e3 115 // fill in callback arrays
mbed_official 49:bee5808e91e3 116 epCallback[EP0OUT] = NULL;
mbed_official 49:bee5808e91e3 117 epCallback[EP0IN] = NULL;
mbed_official 49:bee5808e91e3 118 epCallback[EP1OUT] = &USBHAL::EP1_OUT_callback;
mbed_official 49:bee5808e91e3 119 epCallback[EP1IN ] = &USBHAL::EP1_IN_callback;
mbed_official 49:bee5808e91e3 120 epCallback[EP2OUT] = &USBHAL::EP2_OUT_callback;
mbed_official 49:bee5808e91e3 121 epCallback[EP2IN ] = &USBHAL::EP2_IN_callback;
mbed_official 49:bee5808e91e3 122 epCallback[EP3OUT] = &USBHAL::EP3_OUT_callback;
mbed_official 49:bee5808e91e3 123 epCallback[EP3IN ] = &USBHAL::EP3_IN_callback;
mbed_official 49:bee5808e91e3 124 epCallback[EP4OUT] = &USBHAL::EP4_OUT_callback;
mbed_official 49:bee5808e91e3 125 epCallback[EP4IN ] = &USBHAL::EP4_IN_callback;
mbed_official 49:bee5808e91e3 126 epCallback[EP5OUT] = &USBHAL::EP5_OUT_callback;
mbed_official 49:bee5808e91e3 127 epCallback[EP5IN ] = &USBHAL::EP5_IN_callback;
mbed_official 49:bee5808e91e3 128 epCallback[EP6OUT] = &USBHAL::EP6_OUT_callback;
mbed_official 49:bee5808e91e3 129 epCallback[EP6IN ] = &USBHAL::EP6_IN_callback;
mbed_official 49:bee5808e91e3 130 epCallback[EP7OUT] = &USBHAL::EP7_OUT_callback;
mbed_official 49:bee5808e91e3 131 epCallback[EP7IN ] = &USBHAL::EP7_IN_callback;
mbed_official 49:bee5808e91e3 132
mbed_official 49:bee5808e91e3 133 // clear driver state
mbed_official 49:bee5808e91e3 134 control_state = CTRL_NONE;
mbed_official 49:bee5808e91e3 135
mbed_official 49:bee5808e91e3 136 // set the descriptor location
mbed_official 49:bee5808e91e3 137 MXC_USB->ep_base = (uint32_t)&ep_buffer_descriptor;
mbed_official 49:bee5808e91e3 138
enginerd 64:6e1433359654 139 // enable VBUS interrupts
enginerd 64:6e1433359654 140 MXC_USB->dev_inten = MXC_F_USB_DEV_INTEN_NO_VBUS | MXC_F_USB_DEV_INTEN_VBUS;
enginerd 64:6e1433359654 141
mbed_official 49:bee5808e91e3 142 // attach IRQ handler and enable interrupts
mbed_official 49:bee5808e91e3 143 instance = this;
jessexm 67:8ca8b458434a 144 #if defined(TARGET_MAX32620C) || defined(TARGET_MAX32625) || defined(TARGET_MAX32630)
jessexm 67:8ca8b458434a 145 NVIC_SetVector(USB_IRQn, _usbisr);
jessexm 67:8ca8b458434a 146 #else
jessexm 67:8ca8b458434a 147 NVIC_SetVector(USB_IRQn, (uint32_t)&_usbisr);
jessexm 67:8ca8b458434a 148 #endif
mbed_official 49:bee5808e91e3 149 NVIC_EnableIRQ(USB_IRQn);
mbed_official 49:bee5808e91e3 150 }
mbed_official 49:bee5808e91e3 151
mbed_official 49:bee5808e91e3 152 USBHAL::~USBHAL(void)
mbed_official 49:bee5808e91e3 153 {
mbed_official 49:bee5808e91e3 154 MXC_USB->dev_cn = MXC_F_USB_DEV_CN_URST;
mbed_official 49:bee5808e91e3 155 MXC_USB->dev_cn = 0;
mbed_official 49:bee5808e91e3 156 MXC_USB->cn = 0;
mbed_official 49:bee5808e91e3 157 }
mbed_official 49:bee5808e91e3 158
mbed_official 49:bee5808e91e3 159 void USBHAL::connect(void)
mbed_official 49:bee5808e91e3 160 {
mbed_official 49:bee5808e91e3 161 // enable interrupts
mbed_official 49:bee5808e91e3 162 MXC_USB->dev_inten |= CONNECT_INTS;
mbed_official 49:bee5808e91e3 163
mbed_official 49:bee5808e91e3 164 // allow interrupts on ep0
mbed_official 49:bee5808e91e3 165 MXC_USB->ep[0] |= MXC_F_USB_EP_INT_EN;
mbed_official 49:bee5808e91e3 166
mbed_official 49:bee5808e91e3 167 // pullup enable
mbed_official 49:bee5808e91e3 168 MXC_USB->dev_cn |= (MXC_F_USB_DEV_CN_CONNECT | MXC_F_USB_DEV_CN_FIFO_MODE);
mbed_official 49:bee5808e91e3 169 }
mbed_official 49:bee5808e91e3 170
mbed_official 49:bee5808e91e3 171 void USBHAL::disconnect(void)
mbed_official 49:bee5808e91e3 172 {
mbed_official 49:bee5808e91e3 173 // disable interrupts
mbed_official 49:bee5808e91e3 174 MXC_USB->dev_inten &= ~CONNECT_INTS;
mbed_official 49:bee5808e91e3 175
mbed_official 49:bee5808e91e3 176 // disable pullup
mbed_official 49:bee5808e91e3 177 MXC_USB->dev_cn &= ~MXC_F_USB_DEV_CN_CONNECT;
mbed_official 49:bee5808e91e3 178 }
mbed_official 49:bee5808e91e3 179
mbed_official 49:bee5808e91e3 180 void USBHAL::configureDevice(void)
mbed_official 49:bee5808e91e3 181 {
mbed_official 49:bee5808e91e3 182 // do nothing
mbed_official 49:bee5808e91e3 183 }
mbed_official 49:bee5808e91e3 184
mbed_official 49:bee5808e91e3 185 void USBHAL::unconfigureDevice(void)
mbed_official 49:bee5808e91e3 186 {
mbed_official 49:bee5808e91e3 187 // reset endpoints
mbed_official 49:bee5808e91e3 188 for (int i = 0; i < MXC_USB_NUM_EP; i++) {
mbed_official 49:bee5808e91e3 189 // Disable endpoint and clear the data toggle
mbed_official 49:bee5808e91e3 190 MXC_USB->ep[i] &= ~MXC_F_USB_EP_DIR;
mbed_official 49:bee5808e91e3 191 MXC_USB->ep[i] |= MXC_F_USB_EP_DT;
mbed_official 49:bee5808e91e3 192 }
mbed_official 49:bee5808e91e3 193 }
mbed_official 49:bee5808e91e3 194
mbed_official 49:bee5808e91e3 195 void USBHAL::setAddress(uint8_t address)
mbed_official 49:bee5808e91e3 196 {
mbed_official 49:bee5808e91e3 197 // do nothing
mbed_official 49:bee5808e91e3 198 }
mbed_official 49:bee5808e91e3 199
mbed_official 49:bee5808e91e3 200 void USBHAL::remoteWakeup(void)
mbed_official 49:bee5808e91e3 201 {
mbed_official 49:bee5808e91e3 202 // do nothing
mbed_official 49:bee5808e91e3 203 }
mbed_official 49:bee5808e91e3 204
mbed_official 49:bee5808e91e3 205 static ep_buffer_t *get_desc(uint8_t endpoint)
mbed_official 49:bee5808e91e3 206 {
mbed_official 49:bee5808e91e3 207 uint8_t epnum = EP_NUM(endpoint);
mbed_official 49:bee5808e91e3 208 ep_buffer_t *desc;
mbed_official 49:bee5808e91e3 209
mbed_official 49:bee5808e91e3 210 if (epnum == 0) {
mbed_official 49:bee5808e91e3 211 if (IN_EP(endpoint)) {
mbed_official 49:bee5808e91e3 212 desc = &ep_buffer_descriptor.ep0.in_buffer;
mbed_official 49:bee5808e91e3 213 } else {
mbed_official 49:bee5808e91e3 214 desc = &ep_buffer_descriptor.ep0.out_buffer;
mbed_official 49:bee5808e91e3 215 }
mbed_official 49:bee5808e91e3 216 } else {
mbed_official 49:bee5808e91e3 217 desc = &ep_buffer_descriptor.ep[epnum - 1];
mbed_official 49:bee5808e91e3 218 }
mbed_official 49:bee5808e91e3 219
mbed_official 49:bee5808e91e3 220 return desc;
mbed_official 49:bee5808e91e3 221 }
mbed_official 49:bee5808e91e3 222
mbed_official 49:bee5808e91e3 223 void USBHAL::EP0setup(uint8_t *buffer)
mbed_official 49:bee5808e91e3 224 {
enginerd 64:6e1433359654 225 // Setup packet is fixed at 8 bytes
enginerd 64:6e1433359654 226 // Setup registers cannot be read in byte mode
enginerd 64:6e1433359654 227 uint32_t *ptr32 = (uint32_t*)buffer;
enginerd 64:6e1433359654 228 ptr32[0] = (uint32_t)MXC_USB->setup0;
enginerd 64:6e1433359654 229 ptr32[1] = (uint32_t)MXC_USB->setup1;
mbed_official 49:bee5808e91e3 230 }
mbed_official 49:bee5808e91e3 231
mbed_official 49:bee5808e91e3 232 void USBHAL::EP0read(void)
mbed_official 49:bee5808e91e3 233 {
mbed_official 49:bee5808e91e3 234 if (control_state == CTRL_IN) {
mbed_official 49:bee5808e91e3 235 // This is the status stage. ACK.
mbed_official 49:bee5808e91e3 236 MXC_USB->ep[0] |= MXC_F_USB_EP_ST_ACK;
mbed_official 49:bee5808e91e3 237 control_state = CTRL_NONE;
mbed_official 49:bee5808e91e3 238 return;
mbed_official 49:bee5808e91e3 239 }
mbed_official 49:bee5808e91e3 240
mbed_official 49:bee5808e91e3 241 control_state = CTRL_OUT;
mbed_official 49:bee5808e91e3 242
mbed_official 49:bee5808e91e3 243 endpointRead(EP0OUT, MAX_PACKET_SIZE_EP0);
mbed_official 49:bee5808e91e3 244 }
mbed_official 49:bee5808e91e3 245
mbed_official 49:bee5808e91e3 246 void USBHAL::EP0readStage(void)
mbed_official 49:bee5808e91e3 247 {
mbed_official 49:bee5808e91e3 248 // do nothing
mbed_official 49:bee5808e91e3 249 }
mbed_official 49:bee5808e91e3 250
mbed_official 49:bee5808e91e3 251 uint32_t USBHAL::EP0getReadResult(uint8_t *buffer)
mbed_official 49:bee5808e91e3 252 {
mbed_official 49:bee5808e91e3 253 uint32_t size;
mbed_official 49:bee5808e91e3 254
mbed_official 49:bee5808e91e3 255 if (MXC_USB->out_owner & 1) {
mbed_official 49:bee5808e91e3 256 return 0;
mbed_official 49:bee5808e91e3 257 }
mbed_official 49:bee5808e91e3 258
mbed_official 49:bee5808e91e3 259 // get the packet length and contents
mbed_official 49:bee5808e91e3 260 ep_buffer_t *desc = get_desc(EP0OUT);
mbed_official 49:bee5808e91e3 261 size = desc->buf0_desc;
mbed_official 49:bee5808e91e3 262 memcpy(buffer, aligned_buffer[0], size);
mbed_official 49:bee5808e91e3 263
mbed_official 49:bee5808e91e3 264 return size;
mbed_official 49:bee5808e91e3 265 }
mbed_official 49:bee5808e91e3 266
mbed_official 49:bee5808e91e3 267 void USBHAL::EP0write(uint8_t *buffer, uint32_t size)
mbed_official 49:bee5808e91e3 268 {
mbed_official 49:bee5808e91e3 269 if ((size == 0) && (control_state != CTRL_IN)) {
mbed_official 49:bee5808e91e3 270 // This is a status stage ACK. Handle in hardware.
mbed_official 49:bee5808e91e3 271 MXC_USB->ep[0] |= MXC_F_USB_EP_ST_ACK;
mbed_official 49:bee5808e91e3 272 control_state = CTRL_NONE;
mbed_official 49:bee5808e91e3 273 return;
mbed_official 49:bee5808e91e3 274 }
mbed_official 49:bee5808e91e3 275
mbed_official 49:bee5808e91e3 276 control_state = CTRL_IN;
mbed_official 49:bee5808e91e3 277
mbed_official 49:bee5808e91e3 278 endpointWrite(EP0IN, buffer, size);
mbed_official 49:bee5808e91e3 279 }
mbed_official 49:bee5808e91e3 280
mbed_official 49:bee5808e91e3 281 void USBHAL::EP0stall(void)
mbed_official 49:bee5808e91e3 282 {
mbed_official 49:bee5808e91e3 283 stallEndpoint(0);
mbed_official 49:bee5808e91e3 284 }
mbed_official 49:bee5808e91e3 285
mbed_official 49:bee5808e91e3 286 EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize)
mbed_official 49:bee5808e91e3 287 {
mbed_official 49:bee5808e91e3 288 uint8_t epnum = EP_NUM(endpoint);
mbed_official 49:bee5808e91e3 289
mbed_official 49:bee5808e91e3 290 if ((endpoint >= NUMBER_OF_PHYSICAL_ENDPOINTS) || IN_EP(endpoint)) {
mbed_official 49:bee5808e91e3 291 return EP_INVALID;
mbed_official 49:bee5808e91e3 292 }
mbed_official 49:bee5808e91e3 293
mbed_official 49:bee5808e91e3 294 if (maximumSize > MXC_USB_MAX_PACKET) {
mbed_official 49:bee5808e91e3 295 return EP_INVALID;
mbed_official 49:bee5808e91e3 296 }
mbed_official 49:bee5808e91e3 297
mbed_official 49:bee5808e91e3 298 uint32_t mask = (1 << epnum);
mbed_official 49:bee5808e91e3 299 if (MXC_USB->out_owner & mask) {
mbed_official 49:bee5808e91e3 300 return EP_INVALID;
mbed_official 49:bee5808e91e3 301 }
mbed_official 49:bee5808e91e3 302
mbed_official 49:bee5808e91e3 303 ep_buffer_t *desc = get_desc(endpoint);
mbed_official 49:bee5808e91e3 304 desc->buf0_desc = maximumSize;
mbed_official 49:bee5808e91e3 305 desc->buf0_address = (uint32_t)aligned_buffer[epnum];
mbed_official 49:bee5808e91e3 306
mbed_official 49:bee5808e91e3 307 MXC_USB->out_owner = mask;
mbed_official 49:bee5808e91e3 308
mbed_official 49:bee5808e91e3 309 return EP_PENDING;
mbed_official 49:bee5808e91e3 310 }
mbed_official 49:bee5808e91e3 311
mbed_official 49:bee5808e91e3 312 EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t *data, uint32_t *bytesRead)
mbed_official 49:bee5808e91e3 313 {
mbed_official 49:bee5808e91e3 314 if ((endpoint >= NUMBER_OF_PHYSICAL_ENDPOINTS) || IN_EP(endpoint)) {
mbed_official 49:bee5808e91e3 315 return EP_INVALID;
mbed_official 49:bee5808e91e3 316 }
mbed_official 49:bee5808e91e3 317
mbed_official 49:bee5808e91e3 318 uint32_t mask = (1 << EP_NUM(endpoint));
mbed_official 49:bee5808e91e3 319 if (MXC_USB->out_owner & mask) {
mbed_official 49:bee5808e91e3 320 return EP_PENDING;
mbed_official 49:bee5808e91e3 321 }
mbed_official 49:bee5808e91e3 322
mbed_official 49:bee5808e91e3 323 // get the packet length and contents
mbed_official 49:bee5808e91e3 324 ep_buffer_t *desc = get_desc(endpoint);
mbed_official 49:bee5808e91e3 325 *bytesRead = desc->buf0_desc;
mbed_official 49:bee5808e91e3 326 memcpy(data, aligned_buffer[EP_NUM(endpoint)], *bytesRead);
mbed_official 49:bee5808e91e3 327
mbed_official 49:bee5808e91e3 328 return EP_COMPLETED;
mbed_official 49:bee5808e91e3 329 }
mbed_official 49:bee5808e91e3 330
mbed_official 49:bee5808e91e3 331 EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size)
mbed_official 49:bee5808e91e3 332 {
mbed_official 49:bee5808e91e3 333 uint8_t epnum = EP_NUM(endpoint);
mbed_official 49:bee5808e91e3 334
mbed_official 49:bee5808e91e3 335 if ((endpoint >= NUMBER_OF_PHYSICAL_ENDPOINTS) || OUT_EP(endpoint)) {
mbed_official 49:bee5808e91e3 336 return EP_INVALID;
mbed_official 49:bee5808e91e3 337 }
mbed_official 49:bee5808e91e3 338
mbed_official 49:bee5808e91e3 339 if (size > MXC_USB_MAX_PACKET) {
mbed_official 49:bee5808e91e3 340 return EP_INVALID;
mbed_official 49:bee5808e91e3 341 }
mbed_official 49:bee5808e91e3 342
mbed_official 49:bee5808e91e3 343 uint32_t mask = (1 << epnum);
mbed_official 49:bee5808e91e3 344 if (MXC_USB->in_owner & mask) {
mbed_official 49:bee5808e91e3 345 return EP_INVALID;
mbed_official 49:bee5808e91e3 346 }
mbed_official 49:bee5808e91e3 347
mbed_official 49:bee5808e91e3 348 memcpy(aligned_buffer[epnum], data, size);
mbed_official 49:bee5808e91e3 349
mbed_official 49:bee5808e91e3 350 ep_buffer_t *desc = get_desc(endpoint);
mbed_official 49:bee5808e91e3 351 desc->buf0_desc = size;
mbed_official 49:bee5808e91e3 352 desc->buf0_address = (uint32_t)aligned_buffer[epnum];
mbed_official 49:bee5808e91e3 353
mbed_official 49:bee5808e91e3 354 // start the DMA
mbed_official 49:bee5808e91e3 355 MXC_USB->in_owner = mask;
mbed_official 49:bee5808e91e3 356
mbed_official 49:bee5808e91e3 357 return EP_PENDING;
mbed_official 49:bee5808e91e3 358 }
mbed_official 49:bee5808e91e3 359
mbed_official 49:bee5808e91e3 360 EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint)
mbed_official 49:bee5808e91e3 361 {
mbed_official 49:bee5808e91e3 362 uint32_t mask = (1 << EP_NUM(endpoint));
mbed_official 49:bee5808e91e3 363 if (MXC_USB->in_owner & mask) {
mbed_official 49:bee5808e91e3 364 return EP_PENDING;
mbed_official 49:bee5808e91e3 365 }
mbed_official 49:bee5808e91e3 366
mbed_official 49:bee5808e91e3 367 return EP_COMPLETED;
mbed_official 49:bee5808e91e3 368 }
mbed_official 49:bee5808e91e3 369
mbed_official 49:bee5808e91e3 370 void USBHAL::stallEndpoint(uint8_t endpoint)
mbed_official 49:bee5808e91e3 371 {
mbed_official 49:bee5808e91e3 372 uint8_t epnum = EP_NUM(endpoint);
mbed_official 49:bee5808e91e3 373
mbed_official 49:bee5808e91e3 374 if (epnum == 0) {
mbed_official 49:bee5808e91e3 375 MXC_USB->ep[epnum] |= MXC_F_USB_EP_ST_STALL;
mbed_official 49:bee5808e91e3 376 }
mbed_official 49:bee5808e91e3 377
mbed_official 49:bee5808e91e3 378 MXC_USB->ep[epnum] |= MXC_F_USB_EP_STALL;
mbed_official 49:bee5808e91e3 379 }
mbed_official 49:bee5808e91e3 380
mbed_official 49:bee5808e91e3 381 void USBHAL::unstallEndpoint(uint8_t endpoint)
mbed_official 49:bee5808e91e3 382 {
mbed_official 49:bee5808e91e3 383 MXC_USB->ep[EP_NUM(endpoint)] &= ~MXC_F_USB_EP_STALL;
mbed_official 49:bee5808e91e3 384 }
mbed_official 49:bee5808e91e3 385
mbed_official 49:bee5808e91e3 386 bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t options)
mbed_official 49:bee5808e91e3 387 {
mbed_official 49:bee5808e91e3 388 uint8_t epnum = EP_NUM(endpoint);
mbed_official 49:bee5808e91e3 389 uint32_t ep_ctrl;
mbed_official 49:bee5808e91e3 390
mbed_official 49:bee5808e91e3 391 if (epnum >= NUMBER_OF_PHYSICAL_ENDPOINTS) {
mbed_official 49:bee5808e91e3 392 return false;
mbed_official 49:bee5808e91e3 393 }
mbed_official 49:bee5808e91e3 394
mbed_official 49:bee5808e91e3 395 if (IN_EP(endpoint)) {
mbed_official 49:bee5808e91e3 396 ep_ctrl = (MXC_V_USB_EP_DIR_IN << MXC_F_USB_EP_DIR_POS);
mbed_official 49:bee5808e91e3 397 } else {
mbed_official 49:bee5808e91e3 398 ep_ctrl = (MXC_S_USB_EP_DIR_OUT << MXC_F_USB_EP_DIR_POS);
mbed_official 49:bee5808e91e3 399 }
mbed_official 49:bee5808e91e3 400
mbed_official 49:bee5808e91e3 401 ep_ctrl |= (MXC_F_USB_EP_DT | MXC_F_USB_EP_INT_EN);
mbed_official 49:bee5808e91e3 402
mbed_official 49:bee5808e91e3 403 MXC_USB->ep[epnum] = ep_ctrl;
mbed_official 49:bee5808e91e3 404
mbed_official 49:bee5808e91e3 405 return true;
mbed_official 49:bee5808e91e3 406 }
mbed_official 49:bee5808e91e3 407
mbed_official 49:bee5808e91e3 408 bool USBHAL::getEndpointStallState(unsigned char endpoint)
mbed_official 49:bee5808e91e3 409 {
mbed_official 49:bee5808e91e3 410 return !!(MXC_USB->ep[endpoint] & MXC_F_USB_EP_STALL);
mbed_official 49:bee5808e91e3 411 }
mbed_official 49:bee5808e91e3 412
mbed_official 49:bee5808e91e3 413 void USBHAL::_usbisr(void)
mbed_official 49:bee5808e91e3 414 {
mbed_official 49:bee5808e91e3 415 instance->usbisr();
mbed_official 49:bee5808e91e3 416 }
mbed_official 49:bee5808e91e3 417
mbed_official 49:bee5808e91e3 418 void USBHAL::usbisr(void)
mbed_official 49:bee5808e91e3 419 {
mbed_official 49:bee5808e91e3 420 // get and clear irqs
mbed_official 49:bee5808e91e3 421 uint32_t irq_flags = MXC_USB->dev_intfl;
mbed_official 49:bee5808e91e3 422 MXC_USB->dev_intfl = irq_flags;
mbed_official 49:bee5808e91e3 423
mbed_official 49:bee5808e91e3 424 // process only enabled interrupts
mbed_official 49:bee5808e91e3 425 irq_flags &= MXC_USB->dev_inten;
mbed_official 49:bee5808e91e3 426
mbed_official 49:bee5808e91e3 427 // suspend
mbed_official 49:bee5808e91e3 428 if (irq_flags & MXC_F_USB_DEV_INTFL_SUSP) {
mbed_official 49:bee5808e91e3 429 suspendStateChanged(1);
mbed_official 49:bee5808e91e3 430 }
mbed_official 49:bee5808e91e3 431
mbed_official 49:bee5808e91e3 432 // bus reset
mbed_official 49:bee5808e91e3 433 if (irq_flags & MXC_F_USB_DEV_INTFL_BRST) {
mbed_official 49:bee5808e91e3 434
mbed_official 49:bee5808e91e3 435 // reset endpoints
mbed_official 49:bee5808e91e3 436 for (int i = 0; i < MXC_USB_NUM_EP; i++) {
mbed_official 49:bee5808e91e3 437 // Disable endpoint and clear the data toggle
mbed_official 49:bee5808e91e3 438 MXC_USB->ep[i] &= ~MXC_F_USB_EP_DIR;
mbed_official 49:bee5808e91e3 439 MXC_USB->ep[i] |= MXC_F_USB_EP_DT;
mbed_official 49:bee5808e91e3 440 }
mbed_official 49:bee5808e91e3 441
mbed_official 49:bee5808e91e3 442 // clear driver state
mbed_official 49:bee5808e91e3 443 control_state = CTRL_NONE;
mbed_official 49:bee5808e91e3 444
mbed_official 49:bee5808e91e3 445 busReset();
mbed_official 49:bee5808e91e3 446
mbed_official 49:bee5808e91e3 447 // no need to process events after reset
mbed_official 49:bee5808e91e3 448 return;
mbed_official 49:bee5808e91e3 449 }
mbed_official 49:bee5808e91e3 450
mbed_official 49:bee5808e91e3 451 // Setup packet
mbed_official 49:bee5808e91e3 452 if (irq_flags & MXC_F_USB_DEV_INTFL_SETUP) {
mbed_official 49:bee5808e91e3 453 control_state = CTRL_SETUP;
mbed_official 49:bee5808e91e3 454 EP0setupCallback();
mbed_official 49:bee5808e91e3 455 }
mbed_official 49:bee5808e91e3 456
mbed_official 49:bee5808e91e3 457 // IN packets
mbed_official 49:bee5808e91e3 458 if (irq_flags & MXC_F_USB_DEV_INTFL_EP_IN) {
mbed_official 49:bee5808e91e3 459 // get and clear IN irqs
mbed_official 49:bee5808e91e3 460 uint32_t in_irqs = MXC_USB->in_int;
mbed_official 49:bee5808e91e3 461 MXC_USB->in_int = in_irqs;
mbed_official 49:bee5808e91e3 462
mbed_official 49:bee5808e91e3 463 if (in_irqs & 1) {
mbed_official 49:bee5808e91e3 464 EP0in();
mbed_official 49:bee5808e91e3 465 }
mbed_official 49:bee5808e91e3 466
mbed_official 49:bee5808e91e3 467 for (uint8_t epnum = 1; epnum < NUMBER_OF_LOGICAL_ENDPOINTS; epnum++) {
mbed_official 49:bee5808e91e3 468 uint32_t irq_mask = (1 << epnum);
mbed_official 49:bee5808e91e3 469 if (in_irqs & irq_mask) {
mbed_official 49:bee5808e91e3 470 uint8_t endpoint = (epnum << 1) | DIR_IN;
mbed_official 49:bee5808e91e3 471 (instance->*(epCallback[endpoint]))();
mbed_official 49:bee5808e91e3 472 }
mbed_official 49:bee5808e91e3 473 }
mbed_official 49:bee5808e91e3 474 }
mbed_official 49:bee5808e91e3 475
mbed_official 49:bee5808e91e3 476 // OUT packets
mbed_official 49:bee5808e91e3 477 if (irq_flags & MXC_F_USB_DEV_INTFL_EP_OUT) {
mbed_official 49:bee5808e91e3 478 // get and clear OUT irqs
mbed_official 49:bee5808e91e3 479 uint32_t out_irqs = MXC_USB->out_int;
mbed_official 49:bee5808e91e3 480 MXC_USB->out_int = out_irqs;
mbed_official 49:bee5808e91e3 481
mbed_official 49:bee5808e91e3 482 if (out_irqs & 1) {
mbed_official 49:bee5808e91e3 483 EP0out();
mbed_official 49:bee5808e91e3 484 }
mbed_official 49:bee5808e91e3 485
mbed_official 49:bee5808e91e3 486 for (uint8_t epnum = 1; epnum < NUMBER_OF_LOGICAL_ENDPOINTS; epnum++) {
mbed_official 49:bee5808e91e3 487 uint32_t irq_mask = (1 << epnum);
mbed_official 49:bee5808e91e3 488 if (out_irqs & irq_mask) {
mbed_official 49:bee5808e91e3 489 uint8_t endpoint = (epnum << 1) | DIR_OUT;
mbed_official 49:bee5808e91e3 490 (instance->*(epCallback[endpoint]))();
mbed_official 49:bee5808e91e3 491 }
mbed_official 49:bee5808e91e3 492 }
mbed_official 49:bee5808e91e3 493 }
mbed_official 49:bee5808e91e3 494 }
mbed_official 49:bee5808e91e3 495
mbed_official 49:bee5808e91e3 496 #endif