USBDevice with MAX32620HSP platform support

Fork of USBDevice by mbed official

Committer:
mbed_official
Date:
Thu Apr 16 11:46:08 2015 +0100
Revision:
49:bee5808e91e3
Child:
64:71deba5aa9e9
Synchronized with git revision 29ab478a78892415a3c721cdc20b1755b7b01ba1

Full URL: https://github.com/mbedmicro/mbed/commit/29ab478a78892415a3c721cdc20b1755b7b01ba1/

LPC824, SSCI824 - Add GCC_ARM exporter support

Who changed what in which revision?

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