![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Kalibriersoftware Stromwerte
USBDevice/USBHAL_LPC11U.cpp@1:ea5f520dcdc1, 2015-11-28 (annotated)
- Committer:
- Racer01014
- Date:
- Sat Nov 28 13:50:31 2015 +0000
- Revision:
- 1:ea5f520dcdc1
- Parent:
- 0:5e35c180ed4a
-
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Racer01014 | 0:5e35c180ed4a | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
Racer01014 | 0:5e35c180ed4a | 2 | * |
Racer01014 | 0:5e35c180ed4a | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Racer01014 | 0:5e35c180ed4a | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
Racer01014 | 0:5e35c180ed4a | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
Racer01014 | 0:5e35c180ed4a | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
Racer01014 | 0:5e35c180ed4a | 7 | * Software is furnished to do so, subject to the following conditions: |
Racer01014 | 0:5e35c180ed4a | 8 | * |
Racer01014 | 0:5e35c180ed4a | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Racer01014 | 0:5e35c180ed4a | 10 | * substantial portions of the Software. |
Racer01014 | 0:5e35c180ed4a | 11 | * |
Racer01014 | 0:5e35c180ed4a | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Racer01014 | 0:5e35c180ed4a | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Racer01014 | 0:5e35c180ed4a | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Racer01014 | 0:5e35c180ed4a | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Racer01014 | 0:5e35c180ed4a | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Racer01014 | 0:5e35c180ed4a | 17 | */ |
Racer01014 | 0:5e35c180ed4a | 18 | |
Racer01014 | 0:5e35c180ed4a | 19 | #ifdef TARGET_LPC11U24 |
Racer01014 | 0:5e35c180ed4a | 20 | |
Racer01014 | 0:5e35c180ed4a | 21 | #include "USBHAL.h" |
Racer01014 | 0:5e35c180ed4a | 22 | |
Racer01014 | 0:5e35c180ed4a | 23 | USBHAL * USBHAL::instance; |
Racer01014 | 0:5e35c180ed4a | 24 | |
Racer01014 | 0:5e35c180ed4a | 25 | // Valid physical endpoint numbers are 0 to (NUMBER_OF_PHYSICAL_ENDPOINTS-1) |
Racer01014 | 0:5e35c180ed4a | 26 | #define LAST_PHYSICAL_ENDPOINT (NUMBER_OF_PHYSICAL_ENDPOINTS-1) |
Racer01014 | 0:5e35c180ed4a | 27 | |
Racer01014 | 0:5e35c180ed4a | 28 | // Convert physical endpoint number to register bit |
Racer01014 | 0:5e35c180ed4a | 29 | #define EP(endpoint) (1UL<<endpoint) |
Racer01014 | 0:5e35c180ed4a | 30 | |
Racer01014 | 0:5e35c180ed4a | 31 | // Convert physical to logical |
Racer01014 | 0:5e35c180ed4a | 32 | #define PHY_TO_LOG(endpoint) ((endpoint)>>1) |
Racer01014 | 0:5e35c180ed4a | 33 | |
Racer01014 | 0:5e35c180ed4a | 34 | // Get endpoint direction |
Racer01014 | 0:5e35c180ed4a | 35 | #define IN_EP(endpoint) ((endpoint) & 1U ? true : false) |
Racer01014 | 0:5e35c180ed4a | 36 | #define OUT_EP(endpoint) ((endpoint) & 1U ? false : true) |
Racer01014 | 0:5e35c180ed4a | 37 | |
Racer01014 | 0:5e35c180ed4a | 38 | // USB RAM |
Racer01014 | 0:5e35c180ed4a | 39 | #define USB_RAM_START (0x20004000) |
Racer01014 | 0:5e35c180ed4a | 40 | #define USB_RAM_SIZE (0x00000800) |
Racer01014 | 0:5e35c180ed4a | 41 | |
Racer01014 | 0:5e35c180ed4a | 42 | // SYSAHBCLKCTRL |
Racer01014 | 0:5e35c180ed4a | 43 | #define CLK_USB (1UL<<14) |
Racer01014 | 0:5e35c180ed4a | 44 | #define CLK_USBRAM (1UL<<27) |
Racer01014 | 0:5e35c180ed4a | 45 | |
Racer01014 | 0:5e35c180ed4a | 46 | // USB Information register |
Racer01014 | 0:5e35c180ed4a | 47 | #define FRAME_NR(a) ((a) & 0x7ff) // Frame number |
Racer01014 | 0:5e35c180ed4a | 48 | |
Racer01014 | 0:5e35c180ed4a | 49 | // USB Device Command/Status register |
Racer01014 | 0:5e35c180ed4a | 50 | #define DEV_ADDR_MASK (0x7f) // Device address |
Racer01014 | 0:5e35c180ed4a | 51 | #define DEV_ADDR(a) ((a) & DEV_ADDR_MASK) |
Racer01014 | 0:5e35c180ed4a | 52 | #define DEV_EN (1UL<<7) // Device enable |
Racer01014 | 0:5e35c180ed4a | 53 | #define SETUP (1UL<<8) // SETUP token received |
Racer01014 | 0:5e35c180ed4a | 54 | #define PLL_ON (1UL<<9) // PLL enabled in suspend |
Racer01014 | 0:5e35c180ed4a | 55 | #define DCON (1UL<<16) // Device status - connect |
Racer01014 | 0:5e35c180ed4a | 56 | #define DSUS (1UL<<17) // Device status - suspend |
Racer01014 | 0:5e35c180ed4a | 57 | #define DCON_C (1UL<<24) // Connect change |
Racer01014 | 0:5e35c180ed4a | 58 | #define DSUS_C (1UL<<25) // Suspend change |
Racer01014 | 0:5e35c180ed4a | 59 | #define DRES_C (1UL<<26) // Reset change |
Racer01014 | 0:5e35c180ed4a | 60 | #define VBUSDEBOUNCED (1UL<<28) // Vbus detected |
Racer01014 | 0:5e35c180ed4a | 61 | |
Racer01014 | 0:5e35c180ed4a | 62 | // Endpoint Command/Status list |
Racer01014 | 0:5e35c180ed4a | 63 | #define CMDSTS_A (1UL<<31) // Active |
Racer01014 | 0:5e35c180ed4a | 64 | #define CMDSTS_D (1UL<<30) // Disable |
Racer01014 | 0:5e35c180ed4a | 65 | #define CMDSTS_S (1UL<<29) // Stall |
Racer01014 | 0:5e35c180ed4a | 66 | #define CMDSTS_TR (1UL<<28) // Toggle Reset |
Racer01014 | 0:5e35c180ed4a | 67 | #define CMDSTS_RF (1UL<<27) // Rate Feedback mode |
Racer01014 | 0:5e35c180ed4a | 68 | #define CMDSTS_TV (1UL<<27) // Toggle Value |
Racer01014 | 0:5e35c180ed4a | 69 | #define CMDSTS_T (1UL<<26) // Endpoint Type |
Racer01014 | 0:5e35c180ed4a | 70 | #define CMDSTS_NBYTES(n) (((n)&0x3ff)<<16) // Number of bytes |
Racer01014 | 0:5e35c180ed4a | 71 | #define CMDSTS_ADDRESS_OFFSET(a) (((a)>>6)&0xffff) // Buffer start address |
Racer01014 | 0:5e35c180ed4a | 72 | |
Racer01014 | 0:5e35c180ed4a | 73 | #define BYTES_REMAINING(s) (((s)>>16)&0x3ff) // Bytes remaining after transfer |
Racer01014 | 0:5e35c180ed4a | 74 | |
Racer01014 | 0:5e35c180ed4a | 75 | // USB Non-endpoint interrupt sources |
Racer01014 | 0:5e35c180ed4a | 76 | #define FRAME_INT (1UL<<30) |
Racer01014 | 0:5e35c180ed4a | 77 | #define DEV_INT (1UL<<31) |
Racer01014 | 0:5e35c180ed4a | 78 | |
Racer01014 | 0:5e35c180ed4a | 79 | static volatile int epComplete = 0; |
Racer01014 | 0:5e35c180ed4a | 80 | |
Racer01014 | 0:5e35c180ed4a | 81 | // One entry for a double-buffered logical endpoint in the endpoint |
Racer01014 | 0:5e35c180ed4a | 82 | // command/status list. Endpoint 0 is single buffered, out[1] is used |
Racer01014 | 0:5e35c180ed4a | 83 | // for the SETUP packet and in[1] is not used |
Racer01014 | 0:5e35c180ed4a | 84 | typedef __packed struct { |
Racer01014 | 0:5e35c180ed4a | 85 | uint32_t out[2]; |
Racer01014 | 0:5e35c180ed4a | 86 | uint32_t in[2]; |
Racer01014 | 0:5e35c180ed4a | 87 | } EP_COMMAND_STATUS; |
Racer01014 | 0:5e35c180ed4a | 88 | |
Racer01014 | 0:5e35c180ed4a | 89 | typedef __packed struct { |
Racer01014 | 0:5e35c180ed4a | 90 | uint8_t out[MAX_PACKET_SIZE_EP0]; |
Racer01014 | 0:5e35c180ed4a | 91 | uint8_t in[MAX_PACKET_SIZE_EP0]; |
Racer01014 | 0:5e35c180ed4a | 92 | uint8_t setup[SETUP_PACKET_SIZE]; |
Racer01014 | 0:5e35c180ed4a | 93 | } CONTROL_TRANSFER; |
Racer01014 | 0:5e35c180ed4a | 94 | |
Racer01014 | 0:5e35c180ed4a | 95 | typedef __packed struct { |
Racer01014 | 0:5e35c180ed4a | 96 | uint32_t maxPacket; |
Racer01014 | 0:5e35c180ed4a | 97 | uint32_t buffer[2]; |
Racer01014 | 0:5e35c180ed4a | 98 | uint32_t options; |
Racer01014 | 0:5e35c180ed4a | 99 | } EP_STATE; |
Racer01014 | 0:5e35c180ed4a | 100 | |
Racer01014 | 0:5e35c180ed4a | 101 | static volatile EP_STATE endpointState[NUMBER_OF_PHYSICAL_ENDPOINTS]; |
Racer01014 | 0:5e35c180ed4a | 102 | |
Racer01014 | 0:5e35c180ed4a | 103 | // Pointer to the endpoint command/status list |
Racer01014 | 0:5e35c180ed4a | 104 | static EP_COMMAND_STATUS *ep = NULL; |
Racer01014 | 0:5e35c180ed4a | 105 | |
Racer01014 | 0:5e35c180ed4a | 106 | // Pointer to endpoint 0 data (IN/OUT and SETUP) |
Racer01014 | 0:5e35c180ed4a | 107 | static CONTROL_TRANSFER *ct = NULL; |
Racer01014 | 0:5e35c180ed4a | 108 | |
Racer01014 | 0:5e35c180ed4a | 109 | // Shadow DEVCMDSTAT register to avoid accidentally clearing flags or |
Racer01014 | 0:5e35c180ed4a | 110 | // initiating a remote wakeup event. |
Racer01014 | 0:5e35c180ed4a | 111 | static volatile uint32_t devCmdStat; |
Racer01014 | 0:5e35c180ed4a | 112 | |
Racer01014 | 0:5e35c180ed4a | 113 | // Pointers used to allocate USB RAM |
Racer01014 | 0:5e35c180ed4a | 114 | static uint32_t usbRamPtr = USB_RAM_START; |
Racer01014 | 0:5e35c180ed4a | 115 | static uint32_t epRamPtr = 0; // Buffers for endpoints > 0 start here |
Racer01014 | 0:5e35c180ed4a | 116 | |
Racer01014 | 0:5e35c180ed4a | 117 | #define ROUND_UP_TO_MULTIPLE(x, m) ((((x)+((m)-1))/(m))*(m)) |
Racer01014 | 0:5e35c180ed4a | 118 | |
Racer01014 | 0:5e35c180ed4a | 119 | void USBMemCopy(uint8_t *dst, uint8_t *src, uint32_t size); |
Racer01014 | 0:5e35c180ed4a | 120 | void USBMemCopy(uint8_t *dst, uint8_t *src, uint32_t size) { |
Racer01014 | 0:5e35c180ed4a | 121 | if (size > 0) { |
Racer01014 | 0:5e35c180ed4a | 122 | do { |
Racer01014 | 0:5e35c180ed4a | 123 | *dst++ = *src++; |
Racer01014 | 0:5e35c180ed4a | 124 | } while (--size > 0); |
Racer01014 | 0:5e35c180ed4a | 125 | } |
Racer01014 | 0:5e35c180ed4a | 126 | } |
Racer01014 | 0:5e35c180ed4a | 127 | |
Racer01014 | 0:5e35c180ed4a | 128 | |
Racer01014 | 0:5e35c180ed4a | 129 | USBHAL::USBHAL(void) { |
Racer01014 | 0:5e35c180ed4a | 130 | NVIC_DisableIRQ(USB_IRQn); |
Racer01014 | 0:5e35c180ed4a | 131 | |
Racer01014 | 0:5e35c180ed4a | 132 | // fill in callback array |
Racer01014 | 0:5e35c180ed4a | 133 | epCallback[0] = &USBHAL::EP1_OUT_callback; |
Racer01014 | 0:5e35c180ed4a | 134 | epCallback[1] = &USBHAL::EP1_IN_callback; |
Racer01014 | 0:5e35c180ed4a | 135 | epCallback[2] = &USBHAL::EP2_OUT_callback; |
Racer01014 | 0:5e35c180ed4a | 136 | epCallback[3] = &USBHAL::EP2_IN_callback; |
Racer01014 | 0:5e35c180ed4a | 137 | epCallback[4] = &USBHAL::EP3_OUT_callback; |
Racer01014 | 0:5e35c180ed4a | 138 | epCallback[5] = &USBHAL::EP3_IN_callback; |
Racer01014 | 0:5e35c180ed4a | 139 | epCallback[6] = &USBHAL::EP4_OUT_callback; |
Racer01014 | 0:5e35c180ed4a | 140 | epCallback[7] = &USBHAL::EP4_IN_callback; |
Racer01014 | 0:5e35c180ed4a | 141 | |
Racer01014 | 0:5e35c180ed4a | 142 | // nUSB_CONNECT output |
Racer01014 | 0:5e35c180ed4a | 143 | LPC_IOCON->PIO0_6 = 0x00000001; |
Racer01014 | 0:5e35c180ed4a | 144 | |
Racer01014 | 0:5e35c180ed4a | 145 | // Enable clocks (USB registers, USB RAM) |
Racer01014 | 0:5e35c180ed4a | 146 | LPC_SYSCON->SYSAHBCLKCTRL |= CLK_USB | CLK_USBRAM; |
Racer01014 | 0:5e35c180ed4a | 147 | |
Racer01014 | 0:5e35c180ed4a | 148 | // Ensure device disconnected (DCON not set) |
Racer01014 | 0:5e35c180ed4a | 149 | LPC_USB->DEVCMDSTAT = 0; |
Racer01014 | 0:5e35c180ed4a | 150 | |
Racer01014 | 0:5e35c180ed4a | 151 | // to ensure that the USB host sees the device as |
Racer01014 | 0:5e35c180ed4a | 152 | // disconnected if the target CPU is reset. |
Racer01014 | 0:5e35c180ed4a | 153 | wait(0.3); |
Racer01014 | 0:5e35c180ed4a | 154 | |
Racer01014 | 0:5e35c180ed4a | 155 | // Reserve space in USB RAM for endpoint command/status list |
Racer01014 | 0:5e35c180ed4a | 156 | // Must be 256 byte aligned |
Racer01014 | 0:5e35c180ed4a | 157 | usbRamPtr = ROUND_UP_TO_MULTIPLE(usbRamPtr, 256); |
Racer01014 | 0:5e35c180ed4a | 158 | ep = (EP_COMMAND_STATUS *)usbRamPtr; |
Racer01014 | 0:5e35c180ed4a | 159 | usbRamPtr += (sizeof(EP_COMMAND_STATUS) * NUMBER_OF_LOGICAL_ENDPOINTS); |
Racer01014 | 0:5e35c180ed4a | 160 | LPC_USB->EPLISTSTART = (uint32_t)(ep) & 0xffffff00; |
Racer01014 | 0:5e35c180ed4a | 161 | |
Racer01014 | 0:5e35c180ed4a | 162 | // Reserve space in USB RAM for Endpoint 0 |
Racer01014 | 0:5e35c180ed4a | 163 | // Must be 64 byte aligned |
Racer01014 | 0:5e35c180ed4a | 164 | usbRamPtr = ROUND_UP_TO_MULTIPLE(usbRamPtr, 64); |
Racer01014 | 0:5e35c180ed4a | 165 | ct = (CONTROL_TRANSFER *)usbRamPtr; |
Racer01014 | 0:5e35c180ed4a | 166 | usbRamPtr += sizeof(CONTROL_TRANSFER); |
Racer01014 | 0:5e35c180ed4a | 167 | LPC_USB->DATABUFSTART =(uint32_t)(ct) & 0xffc00000; |
Racer01014 | 0:5e35c180ed4a | 168 | |
Racer01014 | 0:5e35c180ed4a | 169 | // Setup command/status list for EP0 |
Racer01014 | 0:5e35c180ed4a | 170 | ep[0].out[0] = 0; |
Racer01014 | 0:5e35c180ed4a | 171 | ep[0].in[0] = 0; |
Racer01014 | 0:5e35c180ed4a | 172 | ep[0].out[1] = CMDSTS_ADDRESS_OFFSET((uint32_t)ct->setup); |
Racer01014 | 0:5e35c180ed4a | 173 | |
Racer01014 | 0:5e35c180ed4a | 174 | // Route all interrupts to IRQ, some can be routed to |
Racer01014 | 0:5e35c180ed4a | 175 | // USB_FIQ if you wish. |
Racer01014 | 0:5e35c180ed4a | 176 | LPC_USB->INTROUTING = 0; |
Racer01014 | 0:5e35c180ed4a | 177 | |
Racer01014 | 0:5e35c180ed4a | 178 | // Set device address 0, enable USB device, no remote wakeup |
Racer01014 | 0:5e35c180ed4a | 179 | devCmdStat = DEV_ADDR(0) | DEV_EN | DSUS; |
Racer01014 | 0:5e35c180ed4a | 180 | LPC_USB->DEVCMDSTAT = devCmdStat; |
Racer01014 | 0:5e35c180ed4a | 181 | |
Racer01014 | 0:5e35c180ed4a | 182 | // Enable interrupts for device events and EP0 |
Racer01014 | 0:5e35c180ed4a | 183 | LPC_USB->INTEN = DEV_INT | EP(EP0IN) | EP(EP0OUT) | FRAME_INT; |
Racer01014 | 0:5e35c180ed4a | 184 | instance = this; |
Racer01014 | 0:5e35c180ed4a | 185 | |
Racer01014 | 0:5e35c180ed4a | 186 | //attach IRQ handler and enable interrupts |
Racer01014 | 0:5e35c180ed4a | 187 | NVIC_SetVector(USB_IRQn, (uint32_t)&_usbisr); |
Racer01014 | 0:5e35c180ed4a | 188 | } |
Racer01014 | 0:5e35c180ed4a | 189 | |
Racer01014 | 0:5e35c180ed4a | 190 | USBHAL::~USBHAL(void) { |
Racer01014 | 0:5e35c180ed4a | 191 | // Ensure device disconnected (DCON not set) |
Racer01014 | 0:5e35c180ed4a | 192 | LPC_USB->DEVCMDSTAT = 0; |
Racer01014 | 0:5e35c180ed4a | 193 | // Disable USB interrupts |
Racer01014 | 0:5e35c180ed4a | 194 | NVIC_DisableIRQ(USB_IRQn); |
Racer01014 | 0:5e35c180ed4a | 195 | } |
Racer01014 | 0:5e35c180ed4a | 196 | |
Racer01014 | 0:5e35c180ed4a | 197 | void USBHAL::connect(void) { |
Racer01014 | 0:5e35c180ed4a | 198 | NVIC_EnableIRQ(USB_IRQn); |
Racer01014 | 0:5e35c180ed4a | 199 | devCmdStat |= DCON; |
Racer01014 | 0:5e35c180ed4a | 200 | LPC_USB->DEVCMDSTAT = devCmdStat; |
Racer01014 | 0:5e35c180ed4a | 201 | } |
Racer01014 | 0:5e35c180ed4a | 202 | |
Racer01014 | 0:5e35c180ed4a | 203 | void USBHAL::disconnect(void) { |
Racer01014 | 0:5e35c180ed4a | 204 | NVIC_DisableIRQ(USB_IRQn); |
Racer01014 | 0:5e35c180ed4a | 205 | devCmdStat &= ~DCON; |
Racer01014 | 0:5e35c180ed4a | 206 | LPC_USB->DEVCMDSTAT = devCmdStat; |
Racer01014 | 0:5e35c180ed4a | 207 | } |
Racer01014 | 0:5e35c180ed4a | 208 | |
Racer01014 | 0:5e35c180ed4a | 209 | void USBHAL::configureDevice(void) { |
Racer01014 | 0:5e35c180ed4a | 210 | // Not required |
Racer01014 | 0:5e35c180ed4a | 211 | } |
Racer01014 | 0:5e35c180ed4a | 212 | |
Racer01014 | 0:5e35c180ed4a | 213 | void USBHAL::unconfigureDevice(void) { |
Racer01014 | 0:5e35c180ed4a | 214 | // Not required |
Racer01014 | 0:5e35c180ed4a | 215 | } |
Racer01014 | 0:5e35c180ed4a | 216 | |
Racer01014 | 0:5e35c180ed4a | 217 | void USBHAL::EP0setup(uint8_t *buffer) { |
Racer01014 | 0:5e35c180ed4a | 218 | // Copy setup packet data |
Racer01014 | 0:5e35c180ed4a | 219 | USBMemCopy(buffer, ct->setup, SETUP_PACKET_SIZE); |
Racer01014 | 0:5e35c180ed4a | 220 | } |
Racer01014 | 0:5e35c180ed4a | 221 | |
Racer01014 | 0:5e35c180ed4a | 222 | void USBHAL::EP0read(void) { |
Racer01014 | 0:5e35c180ed4a | 223 | // Start an endpoint 0 read |
Racer01014 | 0:5e35c180ed4a | 224 | |
Racer01014 | 0:5e35c180ed4a | 225 | // The USB ISR will call USBDevice_EP0out() when a packet has been read, |
Racer01014 | 0:5e35c180ed4a | 226 | // the USBDevice layer then calls USBBusInterface_EP0getReadResult() to |
Racer01014 | 0:5e35c180ed4a | 227 | // read the data. |
Racer01014 | 0:5e35c180ed4a | 228 | |
Racer01014 | 0:5e35c180ed4a | 229 | ep[0].out[0] = CMDSTS_A |CMDSTS_NBYTES(MAX_PACKET_SIZE_EP0) \ |
Racer01014 | 0:5e35c180ed4a | 230 | | CMDSTS_ADDRESS_OFFSET((uint32_t)ct->out); |
Racer01014 | 0:5e35c180ed4a | 231 | } |
Racer01014 | 0:5e35c180ed4a | 232 | |
Racer01014 | 0:5e35c180ed4a | 233 | uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) { |
Racer01014 | 0:5e35c180ed4a | 234 | // Complete an endpoint 0 read |
Racer01014 | 0:5e35c180ed4a | 235 | uint32_t bytesRead; |
Racer01014 | 0:5e35c180ed4a | 236 | |
Racer01014 | 0:5e35c180ed4a | 237 | // Find how many bytes were read |
Racer01014 | 0:5e35c180ed4a | 238 | bytesRead = MAX_PACKET_SIZE_EP0 - BYTES_REMAINING(ep[0].out[0]); |
Racer01014 | 0:5e35c180ed4a | 239 | |
Racer01014 | 0:5e35c180ed4a | 240 | // Copy data |
Racer01014 | 0:5e35c180ed4a | 241 | USBMemCopy(buffer, ct->out, bytesRead); |
Racer01014 | 0:5e35c180ed4a | 242 | return bytesRead; |
Racer01014 | 0:5e35c180ed4a | 243 | } |
Racer01014 | 0:5e35c180ed4a | 244 | |
Racer01014 | 0:5e35c180ed4a | 245 | |
Racer01014 | 0:5e35c180ed4a | 246 | void USBHAL::EP0readStage(void) { |
Racer01014 | 0:5e35c180ed4a | 247 | // Not required |
Racer01014 | 0:5e35c180ed4a | 248 | } |
Racer01014 | 0:5e35c180ed4a | 249 | |
Racer01014 | 0:5e35c180ed4a | 250 | void USBHAL::EP0write(uint8_t *buffer, uint32_t size) { |
Racer01014 | 0:5e35c180ed4a | 251 | // Start and endpoint 0 write |
Racer01014 | 0:5e35c180ed4a | 252 | |
Racer01014 | 0:5e35c180ed4a | 253 | // The USB ISR will call USBDevice_EP0in() when the data has |
Racer01014 | 0:5e35c180ed4a | 254 | // been written, the USBDevice layer then calls |
Racer01014 | 0:5e35c180ed4a | 255 | // USBBusInterface_EP0getWriteResult() to complete the transaction. |
Racer01014 | 0:5e35c180ed4a | 256 | |
Racer01014 | 0:5e35c180ed4a | 257 | // Copy data |
Racer01014 | 0:5e35c180ed4a | 258 | USBMemCopy(ct->in, buffer, size); |
Racer01014 | 0:5e35c180ed4a | 259 | |
Racer01014 | 0:5e35c180ed4a | 260 | // Start transfer |
Racer01014 | 0:5e35c180ed4a | 261 | ep[0].in[0] = CMDSTS_A | CMDSTS_NBYTES(size) \ |
Racer01014 | 0:5e35c180ed4a | 262 | | CMDSTS_ADDRESS_OFFSET((uint32_t)ct->in); |
Racer01014 | 0:5e35c180ed4a | 263 | } |
Racer01014 | 0:5e35c180ed4a | 264 | |
Racer01014 | 0:5e35c180ed4a | 265 | |
Racer01014 | 0:5e35c180ed4a | 266 | EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) { |
Racer01014 | 0:5e35c180ed4a | 267 | uint8_t bf = 0; |
Racer01014 | 0:5e35c180ed4a | 268 | uint32_t flags = 0; |
Racer01014 | 0:5e35c180ed4a | 269 | |
Racer01014 | 0:5e35c180ed4a | 270 | //check which buffer must be filled |
Racer01014 | 0:5e35c180ed4a | 271 | if (LPC_USB->EPBUFCFG & EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 272 | // Double buffered |
Racer01014 | 0:5e35c180ed4a | 273 | if (LPC_USB->EPINUSE & EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 274 | bf = 1; |
Racer01014 | 0:5e35c180ed4a | 275 | } else { |
Racer01014 | 0:5e35c180ed4a | 276 | bf = 0; |
Racer01014 | 0:5e35c180ed4a | 277 | } |
Racer01014 | 0:5e35c180ed4a | 278 | } |
Racer01014 | 0:5e35c180ed4a | 279 | |
Racer01014 | 0:5e35c180ed4a | 280 | // if isochronous endpoint, T = 1 |
Racer01014 | 0:5e35c180ed4a | 281 | if(endpointState[endpoint].options & ISOCHRONOUS) |
Racer01014 | 0:5e35c180ed4a | 282 | { |
Racer01014 | 0:5e35c180ed4a | 283 | flags |= CMDSTS_T; |
Racer01014 | 0:5e35c180ed4a | 284 | } |
Racer01014 | 0:5e35c180ed4a | 285 | |
Racer01014 | 0:5e35c180ed4a | 286 | //Active the endpoint for reading |
Racer01014 | 0:5e35c180ed4a | 287 | ep[PHY_TO_LOG(endpoint)].out[bf] = CMDSTS_A | CMDSTS_NBYTES(maximumSize) \ |
Racer01014 | 0:5e35c180ed4a | 288 | | CMDSTS_ADDRESS_OFFSET((uint32_t)ct->out) | flags; |
Racer01014 | 0:5e35c180ed4a | 289 | return EP_PENDING; |
Racer01014 | 0:5e35c180ed4a | 290 | } |
Racer01014 | 0:5e35c180ed4a | 291 | |
Racer01014 | 0:5e35c180ed4a | 292 | EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t *data, uint32_t *bytesRead) { |
Racer01014 | 0:5e35c180ed4a | 293 | |
Racer01014 | 0:5e35c180ed4a | 294 | uint8_t bf = 0; |
Racer01014 | 0:5e35c180ed4a | 295 | |
Racer01014 | 0:5e35c180ed4a | 296 | if (!(epComplete & EP(endpoint))) |
Racer01014 | 0:5e35c180ed4a | 297 | return EP_PENDING; |
Racer01014 | 0:5e35c180ed4a | 298 | else { |
Racer01014 | 0:5e35c180ed4a | 299 | epComplete &= ~EP(endpoint); |
Racer01014 | 0:5e35c180ed4a | 300 | |
Racer01014 | 0:5e35c180ed4a | 301 | //check which buffer has been filled |
Racer01014 | 0:5e35c180ed4a | 302 | if (LPC_USB->EPBUFCFG & EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 303 | // Double buffered (here we read the previous buffer which was used) |
Racer01014 | 0:5e35c180ed4a | 304 | if (LPC_USB->EPINUSE & EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 305 | bf = 0; |
Racer01014 | 0:5e35c180ed4a | 306 | } else { |
Racer01014 | 0:5e35c180ed4a | 307 | bf = 1; |
Racer01014 | 0:5e35c180ed4a | 308 | } |
Racer01014 | 0:5e35c180ed4a | 309 | } |
Racer01014 | 0:5e35c180ed4a | 310 | |
Racer01014 | 0:5e35c180ed4a | 311 | // Find how many bytes were read |
Racer01014 | 0:5e35c180ed4a | 312 | *bytesRead = (uint32_t) (endpointState[endpoint].maxPacket - BYTES_REMAINING(ep[PHY_TO_LOG(endpoint)].out[bf])); |
Racer01014 | 0:5e35c180ed4a | 313 | |
Racer01014 | 0:5e35c180ed4a | 314 | // Copy data |
Racer01014 | 0:5e35c180ed4a | 315 | USBMemCopy(data, ct->out, *bytesRead); |
Racer01014 | 0:5e35c180ed4a | 316 | return EP_COMPLETED; |
Racer01014 | 0:5e35c180ed4a | 317 | } |
Racer01014 | 0:5e35c180ed4a | 318 | } |
Racer01014 | 0:5e35c180ed4a | 319 | |
Racer01014 | 0:5e35c180ed4a | 320 | void USBHAL::EP0getWriteResult(void) { |
Racer01014 | 0:5e35c180ed4a | 321 | // Not required |
Racer01014 | 0:5e35c180ed4a | 322 | } |
Racer01014 | 0:5e35c180ed4a | 323 | |
Racer01014 | 0:5e35c180ed4a | 324 | void USBHAL::EP0stall(void) { |
Racer01014 | 0:5e35c180ed4a | 325 | ep[0].in[0] = CMDSTS_S; |
Racer01014 | 0:5e35c180ed4a | 326 | ep[0].out[0] = CMDSTS_S; |
Racer01014 | 0:5e35c180ed4a | 327 | } |
Racer01014 | 0:5e35c180ed4a | 328 | |
Racer01014 | 0:5e35c180ed4a | 329 | void USBHAL::setAddress(uint8_t address) { |
Racer01014 | 0:5e35c180ed4a | 330 | devCmdStat &= ~DEV_ADDR_MASK; |
Racer01014 | 0:5e35c180ed4a | 331 | devCmdStat |= DEV_ADDR(address); |
Racer01014 | 0:5e35c180ed4a | 332 | LPC_USB->DEVCMDSTAT = devCmdStat; |
Racer01014 | 0:5e35c180ed4a | 333 | } |
Racer01014 | 0:5e35c180ed4a | 334 | |
Racer01014 | 0:5e35c180ed4a | 335 | EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size) { |
Racer01014 | 0:5e35c180ed4a | 336 | uint32_t flags = 0; |
Racer01014 | 0:5e35c180ed4a | 337 | uint32_t bf; |
Racer01014 | 0:5e35c180ed4a | 338 | |
Racer01014 | 0:5e35c180ed4a | 339 | // Validate parameters |
Racer01014 | 0:5e35c180ed4a | 340 | if (data == NULL) { |
Racer01014 | 0:5e35c180ed4a | 341 | return EP_INVALID; |
Racer01014 | 0:5e35c180ed4a | 342 | } |
Racer01014 | 0:5e35c180ed4a | 343 | |
Racer01014 | 0:5e35c180ed4a | 344 | if (endpoint > LAST_PHYSICAL_ENDPOINT) { |
Racer01014 | 0:5e35c180ed4a | 345 | return EP_INVALID; |
Racer01014 | 0:5e35c180ed4a | 346 | } |
Racer01014 | 0:5e35c180ed4a | 347 | |
Racer01014 | 0:5e35c180ed4a | 348 | if ((endpoint==EP0IN) || (endpoint==EP0OUT)) { |
Racer01014 | 0:5e35c180ed4a | 349 | return EP_INVALID; |
Racer01014 | 0:5e35c180ed4a | 350 | } |
Racer01014 | 0:5e35c180ed4a | 351 | |
Racer01014 | 0:5e35c180ed4a | 352 | if (size > endpointState[endpoint].maxPacket) { |
Racer01014 | 0:5e35c180ed4a | 353 | return EP_INVALID; |
Racer01014 | 0:5e35c180ed4a | 354 | } |
Racer01014 | 0:5e35c180ed4a | 355 | |
Racer01014 | 0:5e35c180ed4a | 356 | if (LPC_USB->EPBUFCFG & EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 357 | // Double buffered |
Racer01014 | 0:5e35c180ed4a | 358 | if (LPC_USB->EPINUSE & EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 359 | bf = 1; |
Racer01014 | 0:5e35c180ed4a | 360 | } else { |
Racer01014 | 0:5e35c180ed4a | 361 | bf = 0; |
Racer01014 | 0:5e35c180ed4a | 362 | } |
Racer01014 | 0:5e35c180ed4a | 363 | } else { |
Racer01014 | 0:5e35c180ed4a | 364 | // Single buffered |
Racer01014 | 0:5e35c180ed4a | 365 | bf = 0; |
Racer01014 | 0:5e35c180ed4a | 366 | } |
Racer01014 | 0:5e35c180ed4a | 367 | |
Racer01014 | 0:5e35c180ed4a | 368 | // Check if already active |
Racer01014 | 0:5e35c180ed4a | 369 | if (ep[PHY_TO_LOG(endpoint)].in[bf] & CMDSTS_A) { |
Racer01014 | 0:5e35c180ed4a | 370 | return EP_INVALID; |
Racer01014 | 0:5e35c180ed4a | 371 | } |
Racer01014 | 0:5e35c180ed4a | 372 | |
Racer01014 | 0:5e35c180ed4a | 373 | // Check if stalled |
Racer01014 | 0:5e35c180ed4a | 374 | if (ep[PHY_TO_LOG(endpoint)].in[bf] & CMDSTS_S) { |
Racer01014 | 0:5e35c180ed4a | 375 | return EP_STALLED; |
Racer01014 | 0:5e35c180ed4a | 376 | } |
Racer01014 | 0:5e35c180ed4a | 377 | |
Racer01014 | 0:5e35c180ed4a | 378 | // Copy data to USB RAM |
Racer01014 | 0:5e35c180ed4a | 379 | USBMemCopy((uint8_t *)endpointState[endpoint].buffer[bf], data, size); |
Racer01014 | 0:5e35c180ed4a | 380 | |
Racer01014 | 0:5e35c180ed4a | 381 | // Add options |
Racer01014 | 0:5e35c180ed4a | 382 | if (endpointState[endpoint].options & RATE_FEEDBACK_MODE) { |
Racer01014 | 0:5e35c180ed4a | 383 | flags |= CMDSTS_RF; |
Racer01014 | 0:5e35c180ed4a | 384 | } |
Racer01014 | 0:5e35c180ed4a | 385 | |
Racer01014 | 0:5e35c180ed4a | 386 | if (endpointState[endpoint].options & ISOCHRONOUS) { |
Racer01014 | 0:5e35c180ed4a | 387 | flags |= CMDSTS_T; |
Racer01014 | 0:5e35c180ed4a | 388 | } |
Racer01014 | 0:5e35c180ed4a | 389 | |
Racer01014 | 0:5e35c180ed4a | 390 | // Add transfer |
Racer01014 | 0:5e35c180ed4a | 391 | ep[PHY_TO_LOG(endpoint)].in[bf] = CMDSTS_ADDRESS_OFFSET( \ |
Racer01014 | 0:5e35c180ed4a | 392 | endpointState[endpoint].buffer[bf]) \ |
Racer01014 | 0:5e35c180ed4a | 393 | | CMDSTS_NBYTES(size) | CMDSTS_A | flags; |
Racer01014 | 0:5e35c180ed4a | 394 | |
Racer01014 | 0:5e35c180ed4a | 395 | return EP_PENDING; |
Racer01014 | 0:5e35c180ed4a | 396 | } |
Racer01014 | 0:5e35c180ed4a | 397 | |
Racer01014 | 0:5e35c180ed4a | 398 | EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) { |
Racer01014 | 0:5e35c180ed4a | 399 | uint32_t bf; |
Racer01014 | 0:5e35c180ed4a | 400 | |
Racer01014 | 0:5e35c180ed4a | 401 | // Validate parameters |
Racer01014 | 0:5e35c180ed4a | 402 | if (endpoint > LAST_PHYSICAL_ENDPOINT) { |
Racer01014 | 0:5e35c180ed4a | 403 | return EP_INVALID; |
Racer01014 | 0:5e35c180ed4a | 404 | } |
Racer01014 | 0:5e35c180ed4a | 405 | |
Racer01014 | 0:5e35c180ed4a | 406 | if (OUT_EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 407 | return EP_INVALID; |
Racer01014 | 0:5e35c180ed4a | 408 | } |
Racer01014 | 0:5e35c180ed4a | 409 | |
Racer01014 | 0:5e35c180ed4a | 410 | if (LPC_USB->EPBUFCFG & EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 411 | // Double buffered // TODO: FIX THIS |
Racer01014 | 0:5e35c180ed4a | 412 | if (LPC_USB->EPINUSE & EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 413 | bf = 1; |
Racer01014 | 0:5e35c180ed4a | 414 | } else { |
Racer01014 | 0:5e35c180ed4a | 415 | bf = 0; |
Racer01014 | 0:5e35c180ed4a | 416 | } |
Racer01014 | 0:5e35c180ed4a | 417 | } else { |
Racer01014 | 0:5e35c180ed4a | 418 | // Single buffered |
Racer01014 | 0:5e35c180ed4a | 419 | bf = 0; |
Racer01014 | 0:5e35c180ed4a | 420 | } |
Racer01014 | 0:5e35c180ed4a | 421 | |
Racer01014 | 0:5e35c180ed4a | 422 | // Check if endpoint still active |
Racer01014 | 0:5e35c180ed4a | 423 | if (ep[PHY_TO_LOG(endpoint)].in[bf] & CMDSTS_A) { |
Racer01014 | 0:5e35c180ed4a | 424 | return EP_PENDING; |
Racer01014 | 0:5e35c180ed4a | 425 | } |
Racer01014 | 0:5e35c180ed4a | 426 | |
Racer01014 | 0:5e35c180ed4a | 427 | // Check if stalled |
Racer01014 | 0:5e35c180ed4a | 428 | if (ep[PHY_TO_LOG(endpoint)].in[bf] & CMDSTS_S) { |
Racer01014 | 0:5e35c180ed4a | 429 | return EP_STALLED; |
Racer01014 | 0:5e35c180ed4a | 430 | } |
Racer01014 | 0:5e35c180ed4a | 431 | |
Racer01014 | 0:5e35c180ed4a | 432 | return EP_COMPLETED; |
Racer01014 | 0:5e35c180ed4a | 433 | } |
Racer01014 | 0:5e35c180ed4a | 434 | |
Racer01014 | 0:5e35c180ed4a | 435 | void USBHAL::stallEndpoint(uint8_t endpoint) { |
Racer01014 | 0:5e35c180ed4a | 436 | |
Racer01014 | 0:5e35c180ed4a | 437 | // FIX: should this clear active bit? |
Racer01014 | 0:5e35c180ed4a | 438 | if (IN_EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 439 | ep[PHY_TO_LOG(endpoint)].in[0] |= CMDSTS_S; |
Racer01014 | 0:5e35c180ed4a | 440 | ep[PHY_TO_LOG(endpoint)].in[1] |= CMDSTS_S; |
Racer01014 | 0:5e35c180ed4a | 441 | } else { |
Racer01014 | 0:5e35c180ed4a | 442 | ep[PHY_TO_LOG(endpoint)].out[0] |= CMDSTS_S; |
Racer01014 | 0:5e35c180ed4a | 443 | ep[PHY_TO_LOG(endpoint)].out[1] |= CMDSTS_S; |
Racer01014 | 0:5e35c180ed4a | 444 | } |
Racer01014 | 0:5e35c180ed4a | 445 | } |
Racer01014 | 0:5e35c180ed4a | 446 | |
Racer01014 | 0:5e35c180ed4a | 447 | void USBHAL::unstallEndpoint(uint8_t endpoint) { |
Racer01014 | 0:5e35c180ed4a | 448 | if (LPC_USB->EPBUFCFG & EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 449 | // Double buffered |
Racer01014 | 0:5e35c180ed4a | 450 | if (IN_EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 451 | ep[PHY_TO_LOG(endpoint)].in[0] = 0; // S = 0 |
Racer01014 | 0:5e35c180ed4a | 452 | ep[PHY_TO_LOG(endpoint)].in[1] = 0; // S = 0 |
Racer01014 | 0:5e35c180ed4a | 453 | |
Racer01014 | 0:5e35c180ed4a | 454 | if (LPC_USB->EPINUSE & EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 455 | ep[PHY_TO_LOG(endpoint)].in[1] = CMDSTS_TR; // S = 0, TR = 1, TV = 0 |
Racer01014 | 0:5e35c180ed4a | 456 | } else { |
Racer01014 | 0:5e35c180ed4a | 457 | ep[PHY_TO_LOG(endpoint)].in[0] = CMDSTS_TR; // S = 0, TR = 1, TV = 0 |
Racer01014 | 0:5e35c180ed4a | 458 | } |
Racer01014 | 0:5e35c180ed4a | 459 | } else { |
Racer01014 | 0:5e35c180ed4a | 460 | ep[PHY_TO_LOG(endpoint)].out[0] = 0; // S = 0 |
Racer01014 | 0:5e35c180ed4a | 461 | ep[PHY_TO_LOG(endpoint)].out[1] = 0; // S = 0 |
Racer01014 | 0:5e35c180ed4a | 462 | |
Racer01014 | 0:5e35c180ed4a | 463 | if (LPC_USB->EPINUSE & EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 464 | ep[PHY_TO_LOG(endpoint)].out[1] = CMDSTS_TR; // S = 0, TR = 1, TV = 0 |
Racer01014 | 0:5e35c180ed4a | 465 | } else { |
Racer01014 | 0:5e35c180ed4a | 466 | ep[PHY_TO_LOG(endpoint)].out[0] = CMDSTS_TR; // S = 0, TR = 1, TV = 0 |
Racer01014 | 0:5e35c180ed4a | 467 | } |
Racer01014 | 0:5e35c180ed4a | 468 | } |
Racer01014 | 0:5e35c180ed4a | 469 | } else { |
Racer01014 | 0:5e35c180ed4a | 470 | // Single buffered |
Racer01014 | 0:5e35c180ed4a | 471 | if (IN_EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 472 | ep[PHY_TO_LOG(endpoint)].in[0] = CMDSTS_TR; // S = 0, TR = 1, TV = 0 |
Racer01014 | 0:5e35c180ed4a | 473 | } else { |
Racer01014 | 0:5e35c180ed4a | 474 | ep[PHY_TO_LOG(endpoint)].out[0] = CMDSTS_TR; // S = 0, TR = 1, TV = 0 |
Racer01014 | 0:5e35c180ed4a | 475 | } |
Racer01014 | 0:5e35c180ed4a | 476 | } |
Racer01014 | 0:5e35c180ed4a | 477 | } |
Racer01014 | 0:5e35c180ed4a | 478 | |
Racer01014 | 0:5e35c180ed4a | 479 | bool USBHAL::getEndpointStallState(unsigned char endpoint) { |
Racer01014 | 0:5e35c180ed4a | 480 | if (IN_EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 481 | if (LPC_USB->EPINUSE & EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 482 | if (ep[PHY_TO_LOG(endpoint)].in[1] & CMDSTS_S) { |
Racer01014 | 0:5e35c180ed4a | 483 | return true; |
Racer01014 | 0:5e35c180ed4a | 484 | } |
Racer01014 | 0:5e35c180ed4a | 485 | } else { |
Racer01014 | 0:5e35c180ed4a | 486 | if (ep[PHY_TO_LOG(endpoint)].in[0] & CMDSTS_S) { |
Racer01014 | 0:5e35c180ed4a | 487 | return true; |
Racer01014 | 0:5e35c180ed4a | 488 | } |
Racer01014 | 0:5e35c180ed4a | 489 | } |
Racer01014 | 0:5e35c180ed4a | 490 | } else { |
Racer01014 | 0:5e35c180ed4a | 491 | if (LPC_USB->EPINUSE & EP(endpoint)) { |
Racer01014 | 0:5e35c180ed4a | 492 | if (ep[PHY_TO_LOG(endpoint)].out[1] & CMDSTS_S) { |
Racer01014 | 0:5e35c180ed4a | 493 | return true; |
Racer01014 | 0:5e35c180ed4a | 494 | } |
Racer01014 | 0:5e35c180ed4a | 495 | } else { |
Racer01014 | 0:5e35c180ed4a | 496 | if (ep[PHY_TO_LOG(endpoint)].out[0] & CMDSTS_S) { |
Racer01014 | 0:5e35c180ed4a | 497 | return true; |
Racer01014 | 0:5e35c180ed4a | 498 | } |
Racer01014 | 0:5e35c180ed4a | 499 | } |
Racer01014 | 0:5e35c180ed4a | 500 | } |
Racer01014 | 0:5e35c180ed4a | 501 | |
Racer01014 | 0:5e35c180ed4a | 502 | return false; |
Racer01014 | 0:5e35c180ed4a | 503 | } |
Racer01014 | 0:5e35c180ed4a | 504 | |
Racer01014 | 0:5e35c180ed4a | 505 | bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t options) { |
Racer01014 | 0:5e35c180ed4a | 506 | uint32_t tmpEpRamPtr; |
Racer01014 | 0:5e35c180ed4a | 507 | |
Racer01014 | 0:5e35c180ed4a | 508 | if (endpoint > LAST_PHYSICAL_ENDPOINT) { |
Racer01014 | 0:5e35c180ed4a | 509 | return false; |
Racer01014 | 0:5e35c180ed4a | 510 | } |
Racer01014 | 0:5e35c180ed4a | 511 | |
Racer01014 | 0:5e35c180ed4a | 512 | // Not applicable to the control endpoints |
Racer01014 | 0:5e35c180ed4a | 513 | if ((endpoint==EP0IN) || (endpoint==EP0OUT)) { |
Racer01014 | 0:5e35c180ed4a | 514 | return false; |
Racer01014 | 0:5e35c180ed4a | 515 | } |
Racer01014 | 0:5e35c180ed4a | 516 | |
Racer01014 | 0:5e35c180ed4a | 517 | // Allocate buffers in USB RAM |
Racer01014 | 0:5e35c180ed4a | 518 | tmpEpRamPtr = epRamPtr; |
Racer01014 | 0:5e35c180ed4a | 519 | |
Racer01014 | 0:5e35c180ed4a | 520 | // Must be 64 byte aligned |
Racer01014 | 0:5e35c180ed4a | 521 | tmpEpRamPtr = ROUND_UP_TO_MULTIPLE(tmpEpRamPtr, 64); |
Racer01014 | 0:5e35c180ed4a | 522 | |
Racer01014 | 0:5e35c180ed4a | 523 | if ((tmpEpRamPtr + maxPacket) > (USB_RAM_START + USB_RAM_SIZE)) { |
Racer01014 | 0:5e35c180ed4a | 524 | // Out of memory |
Racer01014 | 0:5e35c180ed4a | 525 | return false; |
Racer01014 | 0:5e35c180ed4a | 526 | } |
Racer01014 | 0:5e35c180ed4a | 527 | |
Racer01014 | 0:5e35c180ed4a | 528 | // Allocate first buffer |
Racer01014 | 0:5e35c180ed4a | 529 | endpointState[endpoint].buffer[0] = tmpEpRamPtr; |
Racer01014 | 0:5e35c180ed4a | 530 | tmpEpRamPtr += maxPacket; |
Racer01014 | 0:5e35c180ed4a | 531 | |
Racer01014 | 0:5e35c180ed4a | 532 | if (!(options & SINGLE_BUFFERED)) { |
Racer01014 | 0:5e35c180ed4a | 533 | // Must be 64 byte aligned |
Racer01014 | 0:5e35c180ed4a | 534 | tmpEpRamPtr = ROUND_UP_TO_MULTIPLE(tmpEpRamPtr, 64); |
Racer01014 | 0:5e35c180ed4a | 535 | |
Racer01014 | 0:5e35c180ed4a | 536 | if ((tmpEpRamPtr + maxPacket) > (USB_RAM_START + USB_RAM_SIZE)) { |
Racer01014 | 0:5e35c180ed4a | 537 | // Out of memory |
Racer01014 | 0:5e35c180ed4a | 538 | return false; |
Racer01014 | 0:5e35c180ed4a | 539 | } |
Racer01014 | 0:5e35c180ed4a | 540 | |
Racer01014 | 0:5e35c180ed4a | 541 | // Allocate second buffer |
Racer01014 | 0:5e35c180ed4a | 542 | endpointState[endpoint].buffer[1] = tmpEpRamPtr; |
Racer01014 | 0:5e35c180ed4a | 543 | tmpEpRamPtr += maxPacket; |
Racer01014 | 0:5e35c180ed4a | 544 | } |
Racer01014 | 0:5e35c180ed4a | 545 | |
Racer01014 | 0:5e35c180ed4a | 546 | // Commit to this USB RAM allocation |
Racer01014 | 0:5e35c180ed4a | 547 | epRamPtr = tmpEpRamPtr; |
Racer01014 | 0:5e35c180ed4a | 548 | |
Racer01014 | 0:5e35c180ed4a | 549 | // Remaining endpoint state values |
Racer01014 | 0:5e35c180ed4a | 550 | endpointState[endpoint].maxPacket = maxPacket; |
Racer01014 | 0:5e35c180ed4a | 551 | endpointState[endpoint].options = options; |
Racer01014 | 0:5e35c180ed4a | 552 | |
Racer01014 | 0:5e35c180ed4a | 553 | // Enable double buffering if required |
Racer01014 | 0:5e35c180ed4a | 554 | if (options & SINGLE_BUFFERED) { |
Racer01014 | 0:5e35c180ed4a | 555 | LPC_USB->EPBUFCFG &= ~EP(endpoint); |
Racer01014 | 0:5e35c180ed4a | 556 | } else { |
Racer01014 | 0:5e35c180ed4a | 557 | // Double buffered |
Racer01014 | 0:5e35c180ed4a | 558 | LPC_USB->EPBUFCFG |= EP(endpoint); |
Racer01014 | 0:5e35c180ed4a | 559 | } |
Racer01014 | 0:5e35c180ed4a | 560 | |
Racer01014 | 0:5e35c180ed4a | 561 | // Enable interrupt |
Racer01014 | 0:5e35c180ed4a | 562 | LPC_USB->INTEN |= EP(endpoint); |
Racer01014 | 0:5e35c180ed4a | 563 | |
Racer01014 | 0:5e35c180ed4a | 564 | // Enable endpoint |
Racer01014 | 0:5e35c180ed4a | 565 | unstallEndpoint(endpoint); |
Racer01014 | 0:5e35c180ed4a | 566 | return true; |
Racer01014 | 0:5e35c180ed4a | 567 | } |
Racer01014 | 0:5e35c180ed4a | 568 | |
Racer01014 | 0:5e35c180ed4a | 569 | void USBHAL::remoteWakeup(void) { |
Racer01014 | 0:5e35c180ed4a | 570 | // Clearing DSUS bit initiates a remote wakeup if the |
Racer01014 | 0:5e35c180ed4a | 571 | // device is currently enabled and suspended - otherwise |
Racer01014 | 0:5e35c180ed4a | 572 | // it has no effect. |
Racer01014 | 0:5e35c180ed4a | 573 | LPC_USB->DEVCMDSTAT = devCmdStat & ~DSUS; |
Racer01014 | 0:5e35c180ed4a | 574 | } |
Racer01014 | 0:5e35c180ed4a | 575 | |
Racer01014 | 0:5e35c180ed4a | 576 | |
Racer01014 | 0:5e35c180ed4a | 577 | static void disableEndpoints(void) { |
Racer01014 | 0:5e35c180ed4a | 578 | uint32_t logEp; |
Racer01014 | 0:5e35c180ed4a | 579 | |
Racer01014 | 0:5e35c180ed4a | 580 | // Ref. Table 158 "When a bus reset is received, software |
Racer01014 | 0:5e35c180ed4a | 581 | // must set the disable bit of all endpoints to 1". |
Racer01014 | 0:5e35c180ed4a | 582 | |
Racer01014 | 0:5e35c180ed4a | 583 | for (logEp = 1; logEp < NUMBER_OF_LOGICAL_ENDPOINTS; logEp++) { |
Racer01014 | 0:5e35c180ed4a | 584 | ep[logEp].out[0] = CMDSTS_D; |
Racer01014 | 0:5e35c180ed4a | 585 | ep[logEp].out[1] = CMDSTS_D; |
Racer01014 | 0:5e35c180ed4a | 586 | ep[logEp].in[0] = CMDSTS_D; |
Racer01014 | 0:5e35c180ed4a | 587 | ep[logEp].in[1] = CMDSTS_D; |
Racer01014 | 0:5e35c180ed4a | 588 | } |
Racer01014 | 0:5e35c180ed4a | 589 | |
Racer01014 | 0:5e35c180ed4a | 590 | // Start of USB RAM for endpoints > 0 |
Racer01014 | 0:5e35c180ed4a | 591 | epRamPtr = usbRamPtr; |
Racer01014 | 0:5e35c180ed4a | 592 | } |
Racer01014 | 0:5e35c180ed4a | 593 | |
Racer01014 | 0:5e35c180ed4a | 594 | |
Racer01014 | 0:5e35c180ed4a | 595 | |
Racer01014 | 0:5e35c180ed4a | 596 | void USBHAL::_usbisr(void) { |
Racer01014 | 0:5e35c180ed4a | 597 | instance->usbisr(); |
Racer01014 | 0:5e35c180ed4a | 598 | } |
Racer01014 | 0:5e35c180ed4a | 599 | |
Racer01014 | 0:5e35c180ed4a | 600 | void USBHAL::usbisr(void) { |
Racer01014 | 0:5e35c180ed4a | 601 | // Start of frame |
Racer01014 | 0:5e35c180ed4a | 602 | if (LPC_USB->INTSTAT & FRAME_INT) { |
Racer01014 | 0:5e35c180ed4a | 603 | // Clear SOF interrupt |
Racer01014 | 0:5e35c180ed4a | 604 | LPC_USB->INTSTAT = FRAME_INT; |
Racer01014 | 0:5e35c180ed4a | 605 | |
Racer01014 | 0:5e35c180ed4a | 606 | // SOF event, read frame number |
Racer01014 | 0:5e35c180ed4a | 607 | SOF(FRAME_NR(LPC_USB->INFO)); |
Racer01014 | 0:5e35c180ed4a | 608 | } |
Racer01014 | 0:5e35c180ed4a | 609 | |
Racer01014 | 0:5e35c180ed4a | 610 | // Device state |
Racer01014 | 0:5e35c180ed4a | 611 | if (LPC_USB->INTSTAT & DEV_INT) { |
Racer01014 | 0:5e35c180ed4a | 612 | LPC_USB->INTSTAT = DEV_INT; |
Racer01014 | 0:5e35c180ed4a | 613 | |
Racer01014 | 0:5e35c180ed4a | 614 | if (LPC_USB->DEVCMDSTAT & DSUS_C) { |
Racer01014 | 0:5e35c180ed4a | 615 | // Suspend status changed |
Racer01014 | 0:5e35c180ed4a | 616 | LPC_USB->DEVCMDSTAT = devCmdStat | DSUS_C; |
Racer01014 | 0:5e35c180ed4a | 617 | if((LPC_USB->DEVCMDSTAT & DSUS) != 0) { |
Racer01014 | 0:5e35c180ed4a | 618 | suspendStateChanged(1); |
Racer01014 | 0:5e35c180ed4a | 619 | } |
Racer01014 | 0:5e35c180ed4a | 620 | } |
Racer01014 | 0:5e35c180ed4a | 621 | |
Racer01014 | 0:5e35c180ed4a | 622 | if (LPC_USB->DEVCMDSTAT & DRES_C) { |
Racer01014 | 0:5e35c180ed4a | 623 | // Bus reset |
Racer01014 | 0:5e35c180ed4a | 624 | LPC_USB->DEVCMDSTAT = devCmdStat | DRES_C; |
Racer01014 | 0:5e35c180ed4a | 625 | |
Racer01014 | 0:5e35c180ed4a | 626 | suspendStateChanged(0); |
Racer01014 | 0:5e35c180ed4a | 627 | |
Racer01014 | 0:5e35c180ed4a | 628 | // Disable endpoints > 0 |
Racer01014 | 0:5e35c180ed4a | 629 | disableEndpoints(); |
Racer01014 | 0:5e35c180ed4a | 630 | |
Racer01014 | 0:5e35c180ed4a | 631 | // Bus reset event |
Racer01014 | 0:5e35c180ed4a | 632 | busReset(); |
Racer01014 | 0:5e35c180ed4a | 633 | } |
Racer01014 | 0:5e35c180ed4a | 634 | } |
Racer01014 | 0:5e35c180ed4a | 635 | |
Racer01014 | 0:5e35c180ed4a | 636 | // Endpoint 0 |
Racer01014 | 0:5e35c180ed4a | 637 | if (LPC_USB->INTSTAT & EP(EP0OUT)) { |
Racer01014 | 0:5e35c180ed4a | 638 | // Clear EP0OUT/SETUP interrupt |
Racer01014 | 0:5e35c180ed4a | 639 | LPC_USB->INTSTAT = EP(EP0OUT); |
Racer01014 | 0:5e35c180ed4a | 640 | |
Racer01014 | 0:5e35c180ed4a | 641 | // Check if SETUP |
Racer01014 | 0:5e35c180ed4a | 642 | if (LPC_USB->DEVCMDSTAT & SETUP) { |
Racer01014 | 0:5e35c180ed4a | 643 | // Clear Active and Stall bits for EP0 |
Racer01014 | 0:5e35c180ed4a | 644 | // Documentation does not make it clear if we must use the |
Racer01014 | 0:5e35c180ed4a | 645 | // EPSKIP register to achieve this, Fig. 16 and NXP reference |
Racer01014 | 0:5e35c180ed4a | 646 | // code suggests we can just clear the Active bits - check with |
Racer01014 | 0:5e35c180ed4a | 647 | // NXP to be sure. |
Racer01014 | 0:5e35c180ed4a | 648 | ep[0].in[0] = 0; |
Racer01014 | 0:5e35c180ed4a | 649 | ep[0].out[0] = 0; |
Racer01014 | 0:5e35c180ed4a | 650 | |
Racer01014 | 0:5e35c180ed4a | 651 | // Clear EP0IN interrupt |
Racer01014 | 0:5e35c180ed4a | 652 | LPC_USB->INTSTAT = EP(EP0IN); |
Racer01014 | 0:5e35c180ed4a | 653 | |
Racer01014 | 0:5e35c180ed4a | 654 | // Clear SETUP (and INTONNAK_CI/O) in device status register |
Racer01014 | 0:5e35c180ed4a | 655 | LPC_USB->DEVCMDSTAT = devCmdStat | SETUP; |
Racer01014 | 0:5e35c180ed4a | 656 | |
Racer01014 | 0:5e35c180ed4a | 657 | // EP0 SETUP event (SETUP data received) |
Racer01014 | 0:5e35c180ed4a | 658 | EP0setupCallback(); |
Racer01014 | 0:5e35c180ed4a | 659 | } else { |
Racer01014 | 0:5e35c180ed4a | 660 | // EP0OUT ACK event (OUT data received) |
Racer01014 | 0:5e35c180ed4a | 661 | EP0out(); |
Racer01014 | 0:5e35c180ed4a | 662 | } |
Racer01014 | 0:5e35c180ed4a | 663 | } |
Racer01014 | 0:5e35c180ed4a | 664 | |
Racer01014 | 0:5e35c180ed4a | 665 | if (LPC_USB->INTSTAT & EP(EP0IN)) { |
Racer01014 | 0:5e35c180ed4a | 666 | // Clear EP0IN interrupt |
Racer01014 | 0:5e35c180ed4a | 667 | LPC_USB->INTSTAT = EP(EP0IN); |
Racer01014 | 0:5e35c180ed4a | 668 | |
Racer01014 | 0:5e35c180ed4a | 669 | // EP0IN ACK event (IN data sent) |
Racer01014 | 0:5e35c180ed4a | 670 | EP0in(); |
Racer01014 | 0:5e35c180ed4a | 671 | } |
Racer01014 | 0:5e35c180ed4a | 672 | |
Racer01014 | 0:5e35c180ed4a | 673 | for (uint8_t num = 2; num < 5*2; num++) { |
Racer01014 | 0:5e35c180ed4a | 674 | if (LPC_USB->INTSTAT & EP(num)) { |
Racer01014 | 0:5e35c180ed4a | 675 | LPC_USB->INTSTAT = EP(num); |
Racer01014 | 0:5e35c180ed4a | 676 | epComplete |= EP(num); |
Racer01014 | 0:5e35c180ed4a | 677 | if ((instance->*(epCallback[num - 2]))()) { |
Racer01014 | 0:5e35c180ed4a | 678 | epComplete &= ~EP(num); |
Racer01014 | 0:5e35c180ed4a | 679 | } |
Racer01014 | 0:5e35c180ed4a | 680 | } |
Racer01014 | 0:5e35c180ed4a | 681 | } |
Racer01014 | 0:5e35c180ed4a | 682 | } |
Racer01014 | 0:5e35c180ed4a | 683 | |
Racer01014 | 0:5e35c180ed4a | 684 | #endif |