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.
USBDevice/USBHAL_Maxim.cpp@69:c5e178adb138, 2016-11-11 (annotated)
- Committer:
- jessexm
- Date:
- Fri Nov 11 17:59:00 2016 +0000
- Revision:
- 69:c5e178adb138
- Parent:
- 68:e62755e5cd1e
[MAX32630FTHR] Include missing header and vector function cast
Who changed what in which revision?
User | Revision | Line number | New 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 | |
jessexm | 69:c5e178adb138 | 40 | #if defined(TARGET_MAX32625) || defined(TARGET_MAX32630) |
jessexm | 69:c5e178adb138 | 41 | #include "pwrman_regs.h" |
jessexm | 69:c5e178adb138 | 42 | #endif |
jessexm | 69: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 | 67: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 | 67: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 | 67: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 | 67: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 | 67:6e1433359654 | 99 | #elif defined(TARGET_MAX32620) |
enginerd | 67:6e1433359654 | 100 | // Enable the USB clock |
enginerd | 67:6e1433359654 | 101 | MXC_CLKMAN->clk_ctrl |= MXC_F_CLKMAN_CLK_CTRL_USB_CLOCK_ENABLE; |
jessexm | 68:e62755e5cd1e | 102 | #elif defined(TARGET_MAX32625) || defined(TARGET_MAX32630) |
jessexm | 68:e62755e5cd1e | 103 | MXC_PWRMAN->pwr_rst_ctrl |= MXC_F_PWRMAN_PWR_RST_CTRL_USB_POWERED; |
jessexm | 68:e62755e5cd1e | 104 | MXC_CLKMAN->clk_ctrl |= MXC_F_CLKMAN_CLK_CTRL_USB_CLOCK_ENABLE; |
enginerd | 67: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 | 67: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 | 67:6e1433359654 | 139 | // enable VBUS interrupts |
enginerd | 67:6e1433359654 | 140 | MXC_USB->dev_inten = MXC_F_USB_DEV_INTEN_NO_VBUS | MXC_F_USB_DEV_INTEN_VBUS; |
enginerd | 67:6e1433359654 | 141 | |
mbed_official | 49:bee5808e91e3 | 142 | // attach IRQ handler and enable interrupts |
mbed_official | 49:bee5808e91e3 | 143 | instance = this; |
jessexm | 69:c5e178adb138 | 144 | NVIC_SetVector(USB_IRQn, &_usbisr); |
mbed_official | 49:bee5808e91e3 | 145 | NVIC_EnableIRQ(USB_IRQn); |
mbed_official | 49:bee5808e91e3 | 146 | } |
mbed_official | 49:bee5808e91e3 | 147 | |
mbed_official | 49:bee5808e91e3 | 148 | USBHAL::~USBHAL(void) |
mbed_official | 49:bee5808e91e3 | 149 | { |
mbed_official | 49:bee5808e91e3 | 150 | MXC_USB->dev_cn = MXC_F_USB_DEV_CN_URST; |
mbed_official | 49:bee5808e91e3 | 151 | MXC_USB->dev_cn = 0; |
mbed_official | 49:bee5808e91e3 | 152 | MXC_USB->cn = 0; |
mbed_official | 49:bee5808e91e3 | 153 | } |
mbed_official | 49:bee5808e91e3 | 154 | |
mbed_official | 49:bee5808e91e3 | 155 | void USBHAL::connect(void) |
mbed_official | 49:bee5808e91e3 | 156 | { |
mbed_official | 49:bee5808e91e3 | 157 | // enable interrupts |
mbed_official | 49:bee5808e91e3 | 158 | MXC_USB->dev_inten |= CONNECT_INTS; |
mbed_official | 49:bee5808e91e3 | 159 | |
mbed_official | 49:bee5808e91e3 | 160 | // allow interrupts on ep0 |
mbed_official | 49:bee5808e91e3 | 161 | MXC_USB->ep[0] |= MXC_F_USB_EP_INT_EN; |
mbed_official | 49:bee5808e91e3 | 162 | |
mbed_official | 49:bee5808e91e3 | 163 | // pullup enable |
mbed_official | 49:bee5808e91e3 | 164 | MXC_USB->dev_cn |= (MXC_F_USB_DEV_CN_CONNECT | MXC_F_USB_DEV_CN_FIFO_MODE); |
mbed_official | 49:bee5808e91e3 | 165 | } |
mbed_official | 49:bee5808e91e3 | 166 | |
mbed_official | 49:bee5808e91e3 | 167 | void USBHAL::disconnect(void) |
mbed_official | 49:bee5808e91e3 | 168 | { |
mbed_official | 49:bee5808e91e3 | 169 | // disable interrupts |
mbed_official | 49:bee5808e91e3 | 170 | MXC_USB->dev_inten &= ~CONNECT_INTS; |
mbed_official | 49:bee5808e91e3 | 171 | |
mbed_official | 49:bee5808e91e3 | 172 | // disable pullup |
mbed_official | 49:bee5808e91e3 | 173 | MXC_USB->dev_cn &= ~MXC_F_USB_DEV_CN_CONNECT; |
mbed_official | 49:bee5808e91e3 | 174 | } |
mbed_official | 49:bee5808e91e3 | 175 | |
mbed_official | 49:bee5808e91e3 | 176 | void USBHAL::configureDevice(void) |
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::unconfigureDevice(void) |
mbed_official | 49:bee5808e91e3 | 182 | { |
mbed_official | 49:bee5808e91e3 | 183 | // reset endpoints |
mbed_official | 49:bee5808e91e3 | 184 | for (int i = 0; i < MXC_USB_NUM_EP; i++) { |
mbed_official | 49:bee5808e91e3 | 185 | // Disable endpoint and clear the data toggle |
mbed_official | 49:bee5808e91e3 | 186 | MXC_USB->ep[i] &= ~MXC_F_USB_EP_DIR; |
mbed_official | 49:bee5808e91e3 | 187 | MXC_USB->ep[i] |= MXC_F_USB_EP_DT; |
mbed_official | 49:bee5808e91e3 | 188 | } |
mbed_official | 49:bee5808e91e3 | 189 | } |
mbed_official | 49:bee5808e91e3 | 190 | |
mbed_official | 49:bee5808e91e3 | 191 | void USBHAL::setAddress(uint8_t address) |
mbed_official | 49:bee5808e91e3 | 192 | { |
mbed_official | 49:bee5808e91e3 | 193 | // do nothing |
mbed_official | 49:bee5808e91e3 | 194 | } |
mbed_official | 49:bee5808e91e3 | 195 | |
mbed_official | 49:bee5808e91e3 | 196 | void USBHAL::remoteWakeup(void) |
mbed_official | 49:bee5808e91e3 | 197 | { |
mbed_official | 49:bee5808e91e3 | 198 | // do nothing |
mbed_official | 49:bee5808e91e3 | 199 | } |
mbed_official | 49:bee5808e91e3 | 200 | |
mbed_official | 49:bee5808e91e3 | 201 | static ep_buffer_t *get_desc(uint8_t endpoint) |
mbed_official | 49:bee5808e91e3 | 202 | { |
mbed_official | 49:bee5808e91e3 | 203 | uint8_t epnum = EP_NUM(endpoint); |
mbed_official | 49:bee5808e91e3 | 204 | ep_buffer_t *desc; |
mbed_official | 49:bee5808e91e3 | 205 | |
mbed_official | 49:bee5808e91e3 | 206 | if (epnum == 0) { |
mbed_official | 49:bee5808e91e3 | 207 | if (IN_EP(endpoint)) { |
mbed_official | 49:bee5808e91e3 | 208 | desc = &ep_buffer_descriptor.ep0.in_buffer; |
mbed_official | 49:bee5808e91e3 | 209 | } else { |
mbed_official | 49:bee5808e91e3 | 210 | desc = &ep_buffer_descriptor.ep0.out_buffer; |
mbed_official | 49:bee5808e91e3 | 211 | } |
mbed_official | 49:bee5808e91e3 | 212 | } else { |
mbed_official | 49:bee5808e91e3 | 213 | desc = &ep_buffer_descriptor.ep[epnum - 1]; |
mbed_official | 49:bee5808e91e3 | 214 | } |
mbed_official | 49:bee5808e91e3 | 215 | |
mbed_official | 49:bee5808e91e3 | 216 | return desc; |
mbed_official | 49:bee5808e91e3 | 217 | } |
mbed_official | 49:bee5808e91e3 | 218 | |
mbed_official | 49:bee5808e91e3 | 219 | void USBHAL::EP0setup(uint8_t *buffer) |
mbed_official | 49:bee5808e91e3 | 220 | { |
enginerd | 67:6e1433359654 | 221 | // Setup packet is fixed at 8 bytes |
enginerd | 67:6e1433359654 | 222 | // Setup registers cannot be read in byte mode |
enginerd | 67:6e1433359654 | 223 | uint32_t *ptr32 = (uint32_t*)buffer; |
enginerd | 67:6e1433359654 | 224 | ptr32[0] = (uint32_t)MXC_USB->setup0; |
enginerd | 67:6e1433359654 | 225 | ptr32[1] = (uint32_t)MXC_USB->setup1; |
mbed_official | 49:bee5808e91e3 | 226 | } |
mbed_official | 49:bee5808e91e3 | 227 | |
mbed_official | 49:bee5808e91e3 | 228 | void USBHAL::EP0read(void) |
mbed_official | 49:bee5808e91e3 | 229 | { |
mbed_official | 49:bee5808e91e3 | 230 | if (control_state == CTRL_IN) { |
mbed_official | 49:bee5808e91e3 | 231 | // This is the status stage. ACK. |
mbed_official | 49:bee5808e91e3 | 232 | MXC_USB->ep[0] |= MXC_F_USB_EP_ST_ACK; |
mbed_official | 49:bee5808e91e3 | 233 | control_state = CTRL_NONE; |
mbed_official | 49:bee5808e91e3 | 234 | return; |
mbed_official | 49:bee5808e91e3 | 235 | } |
mbed_official | 49:bee5808e91e3 | 236 | |
mbed_official | 49:bee5808e91e3 | 237 | control_state = CTRL_OUT; |
mbed_official | 49:bee5808e91e3 | 238 | |
mbed_official | 49:bee5808e91e3 | 239 | endpointRead(EP0OUT, MAX_PACKET_SIZE_EP0); |
mbed_official | 49:bee5808e91e3 | 240 | } |
mbed_official | 49:bee5808e91e3 | 241 | |
mbed_official | 49:bee5808e91e3 | 242 | void USBHAL::EP0readStage(void) |
mbed_official | 49:bee5808e91e3 | 243 | { |
mbed_official | 49:bee5808e91e3 | 244 | // do nothing |
mbed_official | 49:bee5808e91e3 | 245 | } |
mbed_official | 49:bee5808e91e3 | 246 | |
mbed_official | 49:bee5808e91e3 | 247 | uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) |
mbed_official | 49:bee5808e91e3 | 248 | { |
mbed_official | 49:bee5808e91e3 | 249 | uint32_t size; |
mbed_official | 49:bee5808e91e3 | 250 | |
mbed_official | 49:bee5808e91e3 | 251 | if (MXC_USB->out_owner & 1) { |
mbed_official | 49:bee5808e91e3 | 252 | return 0; |
mbed_official | 49:bee5808e91e3 | 253 | } |
mbed_official | 49:bee5808e91e3 | 254 | |
mbed_official | 49:bee5808e91e3 | 255 | // get the packet length and contents |
mbed_official | 49:bee5808e91e3 | 256 | ep_buffer_t *desc = get_desc(EP0OUT); |
mbed_official | 49:bee5808e91e3 | 257 | size = desc->buf0_desc; |
mbed_official | 49:bee5808e91e3 | 258 | memcpy(buffer, aligned_buffer[0], size); |
mbed_official | 49:bee5808e91e3 | 259 | |
mbed_official | 49:bee5808e91e3 | 260 | return size; |
mbed_official | 49:bee5808e91e3 | 261 | } |
mbed_official | 49:bee5808e91e3 | 262 | |
mbed_official | 49:bee5808e91e3 | 263 | void USBHAL::EP0write(uint8_t *buffer, uint32_t size) |
mbed_official | 49:bee5808e91e3 | 264 | { |
mbed_official | 49:bee5808e91e3 | 265 | if ((size == 0) && (control_state != CTRL_IN)) { |
mbed_official | 49:bee5808e91e3 | 266 | // This is a status stage ACK. Handle in hardware. |
mbed_official | 49:bee5808e91e3 | 267 | MXC_USB->ep[0] |= MXC_F_USB_EP_ST_ACK; |
mbed_official | 49:bee5808e91e3 | 268 | control_state = CTRL_NONE; |
mbed_official | 49:bee5808e91e3 | 269 | return; |
mbed_official | 49:bee5808e91e3 | 270 | } |
mbed_official | 49:bee5808e91e3 | 271 | |
mbed_official | 49:bee5808e91e3 | 272 | control_state = CTRL_IN; |
mbed_official | 49:bee5808e91e3 | 273 | |
mbed_official | 49:bee5808e91e3 | 274 | endpointWrite(EP0IN, buffer, size); |
mbed_official | 49:bee5808e91e3 | 275 | } |
mbed_official | 49:bee5808e91e3 | 276 | |
mbed_official | 49:bee5808e91e3 | 277 | void USBHAL::EP0stall(void) |
mbed_official | 49:bee5808e91e3 | 278 | { |
mbed_official | 49:bee5808e91e3 | 279 | stallEndpoint(0); |
mbed_official | 49:bee5808e91e3 | 280 | } |
mbed_official | 49:bee5808e91e3 | 281 | |
mbed_official | 49:bee5808e91e3 | 282 | EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) |
mbed_official | 49:bee5808e91e3 | 283 | { |
mbed_official | 49:bee5808e91e3 | 284 | uint8_t epnum = EP_NUM(endpoint); |
mbed_official | 49:bee5808e91e3 | 285 | |
mbed_official | 49:bee5808e91e3 | 286 | if ((endpoint >= NUMBER_OF_PHYSICAL_ENDPOINTS) || IN_EP(endpoint)) { |
mbed_official | 49:bee5808e91e3 | 287 | return EP_INVALID; |
mbed_official | 49:bee5808e91e3 | 288 | } |
mbed_official | 49:bee5808e91e3 | 289 | |
mbed_official | 49:bee5808e91e3 | 290 | if (maximumSize > MXC_USB_MAX_PACKET) { |
mbed_official | 49:bee5808e91e3 | 291 | return EP_INVALID; |
mbed_official | 49:bee5808e91e3 | 292 | } |
mbed_official | 49:bee5808e91e3 | 293 | |
mbed_official | 49:bee5808e91e3 | 294 | uint32_t mask = (1 << epnum); |
mbed_official | 49:bee5808e91e3 | 295 | if (MXC_USB->out_owner & mask) { |
mbed_official | 49:bee5808e91e3 | 296 | return EP_INVALID; |
mbed_official | 49:bee5808e91e3 | 297 | } |
mbed_official | 49:bee5808e91e3 | 298 | |
mbed_official | 49:bee5808e91e3 | 299 | ep_buffer_t *desc = get_desc(endpoint); |
mbed_official | 49:bee5808e91e3 | 300 | desc->buf0_desc = maximumSize; |
mbed_official | 49:bee5808e91e3 | 301 | desc->buf0_address = (uint32_t)aligned_buffer[epnum]; |
mbed_official | 49:bee5808e91e3 | 302 | |
mbed_official | 49:bee5808e91e3 | 303 | MXC_USB->out_owner = mask; |
mbed_official | 49:bee5808e91e3 | 304 | |
mbed_official | 49:bee5808e91e3 | 305 | return EP_PENDING; |
mbed_official | 49:bee5808e91e3 | 306 | } |
mbed_official | 49:bee5808e91e3 | 307 | |
mbed_official | 49:bee5808e91e3 | 308 | EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t *data, uint32_t *bytesRead) |
mbed_official | 49:bee5808e91e3 | 309 | { |
mbed_official | 49:bee5808e91e3 | 310 | if ((endpoint >= NUMBER_OF_PHYSICAL_ENDPOINTS) || IN_EP(endpoint)) { |
mbed_official | 49:bee5808e91e3 | 311 | return EP_INVALID; |
mbed_official | 49:bee5808e91e3 | 312 | } |
mbed_official | 49:bee5808e91e3 | 313 | |
mbed_official | 49:bee5808e91e3 | 314 | uint32_t mask = (1 << EP_NUM(endpoint)); |
mbed_official | 49:bee5808e91e3 | 315 | if (MXC_USB->out_owner & mask) { |
mbed_official | 49:bee5808e91e3 | 316 | return EP_PENDING; |
mbed_official | 49:bee5808e91e3 | 317 | } |
mbed_official | 49:bee5808e91e3 | 318 | |
mbed_official | 49:bee5808e91e3 | 319 | // get the packet length and contents |
mbed_official | 49:bee5808e91e3 | 320 | ep_buffer_t *desc = get_desc(endpoint); |
mbed_official | 49:bee5808e91e3 | 321 | *bytesRead = desc->buf0_desc; |
mbed_official | 49:bee5808e91e3 | 322 | memcpy(data, aligned_buffer[EP_NUM(endpoint)], *bytesRead); |
mbed_official | 49:bee5808e91e3 | 323 | |
mbed_official | 49:bee5808e91e3 | 324 | return EP_COMPLETED; |
mbed_official | 49:bee5808e91e3 | 325 | } |
mbed_official | 49:bee5808e91e3 | 326 | |
mbed_official | 49:bee5808e91e3 | 327 | EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size) |
mbed_official | 49:bee5808e91e3 | 328 | { |
mbed_official | 49:bee5808e91e3 | 329 | uint8_t epnum = EP_NUM(endpoint); |
mbed_official | 49:bee5808e91e3 | 330 | |
mbed_official | 49:bee5808e91e3 | 331 | if ((endpoint >= NUMBER_OF_PHYSICAL_ENDPOINTS) || OUT_EP(endpoint)) { |
mbed_official | 49:bee5808e91e3 | 332 | return EP_INVALID; |
mbed_official | 49:bee5808e91e3 | 333 | } |
mbed_official | 49:bee5808e91e3 | 334 | |
mbed_official | 49:bee5808e91e3 | 335 | if (size > MXC_USB_MAX_PACKET) { |
mbed_official | 49:bee5808e91e3 | 336 | return EP_INVALID; |
mbed_official | 49:bee5808e91e3 | 337 | } |
mbed_official | 49:bee5808e91e3 | 338 | |
mbed_official | 49:bee5808e91e3 | 339 | uint32_t mask = (1 << epnum); |
mbed_official | 49:bee5808e91e3 | 340 | if (MXC_USB->in_owner & mask) { |
mbed_official | 49:bee5808e91e3 | 341 | return EP_INVALID; |
mbed_official | 49:bee5808e91e3 | 342 | } |
mbed_official | 49:bee5808e91e3 | 343 | |
mbed_official | 49:bee5808e91e3 | 344 | memcpy(aligned_buffer[epnum], data, size); |
mbed_official | 49:bee5808e91e3 | 345 | |
mbed_official | 49:bee5808e91e3 | 346 | ep_buffer_t *desc = get_desc(endpoint); |
mbed_official | 49:bee5808e91e3 | 347 | desc->buf0_desc = size; |
mbed_official | 49:bee5808e91e3 | 348 | desc->buf0_address = (uint32_t)aligned_buffer[epnum]; |
mbed_official | 49:bee5808e91e3 | 349 | |
mbed_official | 49:bee5808e91e3 | 350 | // start the DMA |
mbed_official | 49:bee5808e91e3 | 351 | MXC_USB->in_owner = mask; |
mbed_official | 49:bee5808e91e3 | 352 | |
mbed_official | 49:bee5808e91e3 | 353 | return EP_PENDING; |
mbed_official | 49:bee5808e91e3 | 354 | } |
mbed_official | 49:bee5808e91e3 | 355 | |
mbed_official | 49:bee5808e91e3 | 356 | EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) |
mbed_official | 49:bee5808e91e3 | 357 | { |
mbed_official | 49:bee5808e91e3 | 358 | uint32_t mask = (1 << EP_NUM(endpoint)); |
mbed_official | 49:bee5808e91e3 | 359 | if (MXC_USB->in_owner & mask) { |
mbed_official | 49:bee5808e91e3 | 360 | return EP_PENDING; |
mbed_official | 49:bee5808e91e3 | 361 | } |
mbed_official | 49:bee5808e91e3 | 362 | |
mbed_official | 49:bee5808e91e3 | 363 | return EP_COMPLETED; |
mbed_official | 49:bee5808e91e3 | 364 | } |
mbed_official | 49:bee5808e91e3 | 365 | |
mbed_official | 49:bee5808e91e3 | 366 | void USBHAL::stallEndpoint(uint8_t endpoint) |
mbed_official | 49:bee5808e91e3 | 367 | { |
mbed_official | 49:bee5808e91e3 | 368 | uint8_t epnum = EP_NUM(endpoint); |
mbed_official | 49:bee5808e91e3 | 369 | |
mbed_official | 49:bee5808e91e3 | 370 | if (epnum == 0) { |
mbed_official | 49:bee5808e91e3 | 371 | MXC_USB->ep[epnum] |= MXC_F_USB_EP_ST_STALL; |
mbed_official | 49:bee5808e91e3 | 372 | } |
mbed_official | 49:bee5808e91e3 | 373 | |
mbed_official | 49:bee5808e91e3 | 374 | MXC_USB->ep[epnum] |= MXC_F_USB_EP_STALL; |
mbed_official | 49:bee5808e91e3 | 375 | } |
mbed_official | 49:bee5808e91e3 | 376 | |
mbed_official | 49:bee5808e91e3 | 377 | void USBHAL::unstallEndpoint(uint8_t endpoint) |
mbed_official | 49:bee5808e91e3 | 378 | { |
mbed_official | 49:bee5808e91e3 | 379 | MXC_USB->ep[EP_NUM(endpoint)] &= ~MXC_F_USB_EP_STALL; |
mbed_official | 49:bee5808e91e3 | 380 | } |
mbed_official | 49:bee5808e91e3 | 381 | |
mbed_official | 49:bee5808e91e3 | 382 | bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t options) |
mbed_official | 49:bee5808e91e3 | 383 | { |
mbed_official | 49:bee5808e91e3 | 384 | uint8_t epnum = EP_NUM(endpoint); |
mbed_official | 49:bee5808e91e3 | 385 | uint32_t ep_ctrl; |
mbed_official | 49:bee5808e91e3 | 386 | |
mbed_official | 49:bee5808e91e3 | 387 | if (epnum >= NUMBER_OF_PHYSICAL_ENDPOINTS) { |
mbed_official | 49:bee5808e91e3 | 388 | return false; |
mbed_official | 49:bee5808e91e3 | 389 | } |
mbed_official | 49:bee5808e91e3 | 390 | |
mbed_official | 49:bee5808e91e3 | 391 | if (IN_EP(endpoint)) { |
mbed_official | 49:bee5808e91e3 | 392 | ep_ctrl = (MXC_V_USB_EP_DIR_IN << MXC_F_USB_EP_DIR_POS); |
mbed_official | 49:bee5808e91e3 | 393 | } else { |
mbed_official | 49:bee5808e91e3 | 394 | ep_ctrl = (MXC_S_USB_EP_DIR_OUT << MXC_F_USB_EP_DIR_POS); |
mbed_official | 49:bee5808e91e3 | 395 | } |
mbed_official | 49:bee5808e91e3 | 396 | |
mbed_official | 49:bee5808e91e3 | 397 | ep_ctrl |= (MXC_F_USB_EP_DT | MXC_F_USB_EP_INT_EN); |
mbed_official | 49:bee5808e91e3 | 398 | |
mbed_official | 49:bee5808e91e3 | 399 | MXC_USB->ep[epnum] = ep_ctrl; |
mbed_official | 49:bee5808e91e3 | 400 | |
mbed_official | 49:bee5808e91e3 | 401 | return true; |
mbed_official | 49:bee5808e91e3 | 402 | } |
mbed_official | 49:bee5808e91e3 | 403 | |
mbed_official | 49:bee5808e91e3 | 404 | bool USBHAL::getEndpointStallState(unsigned char endpoint) |
mbed_official | 49:bee5808e91e3 | 405 | { |
mbed_official | 49:bee5808e91e3 | 406 | return !!(MXC_USB->ep[endpoint] & MXC_F_USB_EP_STALL); |
mbed_official | 49:bee5808e91e3 | 407 | } |
mbed_official | 49:bee5808e91e3 | 408 | |
mbed_official | 49:bee5808e91e3 | 409 | void USBHAL::_usbisr(void) |
mbed_official | 49:bee5808e91e3 | 410 | { |
mbed_official | 49:bee5808e91e3 | 411 | instance->usbisr(); |
mbed_official | 49:bee5808e91e3 | 412 | } |
mbed_official | 49:bee5808e91e3 | 413 | |
mbed_official | 49:bee5808e91e3 | 414 | void USBHAL::usbisr(void) |
mbed_official | 49:bee5808e91e3 | 415 | { |
mbed_official | 49:bee5808e91e3 | 416 | // get and clear irqs |
mbed_official | 49:bee5808e91e3 | 417 | uint32_t irq_flags = MXC_USB->dev_intfl; |
mbed_official | 49:bee5808e91e3 | 418 | MXC_USB->dev_intfl = irq_flags; |
mbed_official | 49:bee5808e91e3 | 419 | |
mbed_official | 49:bee5808e91e3 | 420 | // process only enabled interrupts |
mbed_official | 49:bee5808e91e3 | 421 | irq_flags &= MXC_USB->dev_inten; |
mbed_official | 49:bee5808e91e3 | 422 | |
mbed_official | 49:bee5808e91e3 | 423 | // suspend |
mbed_official | 49:bee5808e91e3 | 424 | if (irq_flags & MXC_F_USB_DEV_INTFL_SUSP) { |
mbed_official | 49:bee5808e91e3 | 425 | suspendStateChanged(1); |
mbed_official | 49:bee5808e91e3 | 426 | } |
mbed_official | 49:bee5808e91e3 | 427 | |
mbed_official | 49:bee5808e91e3 | 428 | // bus reset |
mbed_official | 49:bee5808e91e3 | 429 | if (irq_flags & MXC_F_USB_DEV_INTFL_BRST) { |
mbed_official | 49:bee5808e91e3 | 430 | |
mbed_official | 49:bee5808e91e3 | 431 | // reset endpoints |
mbed_official | 49:bee5808e91e3 | 432 | for (int i = 0; i < MXC_USB_NUM_EP; i++) { |
mbed_official | 49:bee5808e91e3 | 433 | // Disable endpoint and clear the data toggle |
mbed_official | 49:bee5808e91e3 | 434 | MXC_USB->ep[i] &= ~MXC_F_USB_EP_DIR; |
mbed_official | 49:bee5808e91e3 | 435 | MXC_USB->ep[i] |= MXC_F_USB_EP_DT; |
mbed_official | 49:bee5808e91e3 | 436 | } |
mbed_official | 49:bee5808e91e3 | 437 | |
mbed_official | 49:bee5808e91e3 | 438 | // clear driver state |
mbed_official | 49:bee5808e91e3 | 439 | control_state = CTRL_NONE; |
mbed_official | 49:bee5808e91e3 | 440 | |
mbed_official | 49:bee5808e91e3 | 441 | busReset(); |
mbed_official | 49:bee5808e91e3 | 442 | |
mbed_official | 49:bee5808e91e3 | 443 | // no need to process events after reset |
mbed_official | 49:bee5808e91e3 | 444 | return; |
mbed_official | 49:bee5808e91e3 | 445 | } |
mbed_official | 49:bee5808e91e3 | 446 | |
mbed_official | 49:bee5808e91e3 | 447 | // Setup packet |
mbed_official | 49:bee5808e91e3 | 448 | if (irq_flags & MXC_F_USB_DEV_INTFL_SETUP) { |
mbed_official | 49:bee5808e91e3 | 449 | control_state = CTRL_SETUP; |
mbed_official | 49:bee5808e91e3 | 450 | EP0setupCallback(); |
mbed_official | 49:bee5808e91e3 | 451 | } |
mbed_official | 49:bee5808e91e3 | 452 | |
mbed_official | 49:bee5808e91e3 | 453 | // IN packets |
mbed_official | 49:bee5808e91e3 | 454 | if (irq_flags & MXC_F_USB_DEV_INTFL_EP_IN) { |
mbed_official | 49:bee5808e91e3 | 455 | // get and clear IN irqs |
mbed_official | 49:bee5808e91e3 | 456 | uint32_t in_irqs = MXC_USB->in_int; |
mbed_official | 49:bee5808e91e3 | 457 | MXC_USB->in_int = in_irqs; |
mbed_official | 49:bee5808e91e3 | 458 | |
mbed_official | 49:bee5808e91e3 | 459 | if (in_irqs & 1) { |
mbed_official | 49:bee5808e91e3 | 460 | EP0in(); |
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 (in_irqs & irq_mask) { |
mbed_official | 49:bee5808e91e3 | 466 | uint8_t endpoint = (epnum << 1) | DIR_IN; |
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 | // OUT packets |
mbed_official | 49:bee5808e91e3 | 473 | if (irq_flags & MXC_F_USB_DEV_INTFL_EP_OUT) { |
mbed_official | 49:bee5808e91e3 | 474 | // get and clear OUT irqs |
mbed_official | 49:bee5808e91e3 | 475 | uint32_t out_irqs = MXC_USB->out_int; |
mbed_official | 49:bee5808e91e3 | 476 | MXC_USB->out_int = out_irqs; |
mbed_official | 49:bee5808e91e3 | 477 | |
mbed_official | 49:bee5808e91e3 | 478 | if (out_irqs & 1) { |
mbed_official | 49:bee5808e91e3 | 479 | EP0out(); |
mbed_official | 49:bee5808e91e3 | 480 | } |
mbed_official | 49:bee5808e91e3 | 481 | |
mbed_official | 49:bee5808e91e3 | 482 | for (uint8_t epnum = 1; epnum < NUMBER_OF_LOGICAL_ENDPOINTS; epnum++) { |
mbed_official | 49:bee5808e91e3 | 483 | uint32_t irq_mask = (1 << epnum); |
mbed_official | 49:bee5808e91e3 | 484 | if (out_irqs & irq_mask) { |
mbed_official | 49:bee5808e91e3 | 485 | uint8_t endpoint = (epnum << 1) | DIR_OUT; |
mbed_official | 49:bee5808e91e3 | 486 | (instance->*(epCallback[endpoint]))(); |
mbed_official | 49:bee5808e91e3 | 487 | } |
mbed_official | 49:bee5808e91e3 | 488 | } |
mbed_official | 49:bee5808e91e3 | 489 | } |
mbed_official | 49:bee5808e91e3 | 490 | } |
mbed_official | 49:bee5808e91e3 | 491 | |
mbed_official | 49:bee5808e91e3 | 492 | #endif |