USBDevice with MAX32620HSP platform support

Fork of USBDevice by mbed official

Committer:
enginerd
Date:
Fri Oct 07 15:33:16 2016 +0000
Revision:
64:71deba5aa9e9
Parent:
49:bee5808e91e3
[MAX32620HSP] Added support for new platform

Who changed what in which revision?

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