blablabla
Dependencies: MAG3110 MMA8451Q SLCD- TSI USBDevice mbed
USBDevice/USBDevice/USBHAL_LPC40.cpp@0:339b7abfa147, 2014-04-16 (annotated)
- Committer:
- Osator
- Date:
- Wed Apr 16 12:20:00 2014 +0000
- Revision:
- 0:339b7abfa147
blablabla
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Osator | 0:339b7abfa147 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
Osator | 0:339b7abfa147 | 2 | * |
Osator | 0:339b7abfa147 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Osator | 0:339b7abfa147 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
Osator | 0:339b7abfa147 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
Osator | 0:339b7abfa147 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
Osator | 0:339b7abfa147 | 7 | * Software is furnished to do so, subject to the following conditions: |
Osator | 0:339b7abfa147 | 8 | * |
Osator | 0:339b7abfa147 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Osator | 0:339b7abfa147 | 10 | * substantial portions of the Software. |
Osator | 0:339b7abfa147 | 11 | * |
Osator | 0:339b7abfa147 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Osator | 0:339b7abfa147 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Osator | 0:339b7abfa147 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Osator | 0:339b7abfa147 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Osator | 0:339b7abfa147 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Osator | 0:339b7abfa147 | 17 | */ |
Osator | 0:339b7abfa147 | 18 | |
Osator | 0:339b7abfa147 | 19 | #if defined(TARGET_LPC4088) |
Osator | 0:339b7abfa147 | 20 | |
Osator | 0:339b7abfa147 | 21 | #include "USBHAL.h" |
Osator | 0:339b7abfa147 | 22 | |
Osator | 0:339b7abfa147 | 23 | |
Osator | 0:339b7abfa147 | 24 | // Get endpoint direction |
Osator | 0:339b7abfa147 | 25 | #define IN_EP(endpoint) ((endpoint) & 1U ? true : false) |
Osator | 0:339b7abfa147 | 26 | #define OUT_EP(endpoint) ((endpoint) & 1U ? false : true) |
Osator | 0:339b7abfa147 | 27 | |
Osator | 0:339b7abfa147 | 28 | // Convert physical endpoint number to register bit |
Osator | 0:339b7abfa147 | 29 | #define EP(endpoint) (1UL<<endpoint) |
Osator | 0:339b7abfa147 | 30 | |
Osator | 0:339b7abfa147 | 31 | // Power Control for Peripherals register |
Osator | 0:339b7abfa147 | 32 | #define PCUSB (1UL<<31) |
Osator | 0:339b7abfa147 | 33 | |
Osator | 0:339b7abfa147 | 34 | // USB Clock Control register |
Osator | 0:339b7abfa147 | 35 | #define DEV_CLK_EN (1UL<<1) |
Osator | 0:339b7abfa147 | 36 | #define PORT_CLK_EN (1UL<<3) |
Osator | 0:339b7abfa147 | 37 | #define AHB_CLK_EN (1UL<<4) |
Osator | 0:339b7abfa147 | 38 | |
Osator | 0:339b7abfa147 | 39 | // USB Clock Status register |
Osator | 0:339b7abfa147 | 40 | #define DEV_CLK_ON (1UL<<1) |
Osator | 0:339b7abfa147 | 41 | #define AHB_CLK_ON (1UL<<4) |
Osator | 0:339b7abfa147 | 42 | |
Osator | 0:339b7abfa147 | 43 | // USB Device Interupt registers |
Osator | 0:339b7abfa147 | 44 | #define FRAME (1UL<<0) |
Osator | 0:339b7abfa147 | 45 | #define EP_FAST (1UL<<1) |
Osator | 0:339b7abfa147 | 46 | #define EP_SLOW (1UL<<2) |
Osator | 0:339b7abfa147 | 47 | #define DEV_STAT (1UL<<3) |
Osator | 0:339b7abfa147 | 48 | #define CCEMPTY (1UL<<4) |
Osator | 0:339b7abfa147 | 49 | #define CDFULL (1UL<<5) |
Osator | 0:339b7abfa147 | 50 | #define RxENDPKT (1UL<<6) |
Osator | 0:339b7abfa147 | 51 | #define TxENDPKT (1UL<<7) |
Osator | 0:339b7abfa147 | 52 | #define EP_RLZED (1UL<<8) |
Osator | 0:339b7abfa147 | 53 | #define ERR_INT (1UL<<9) |
Osator | 0:339b7abfa147 | 54 | |
Osator | 0:339b7abfa147 | 55 | // USB Control register |
Osator | 0:339b7abfa147 | 56 | #define RD_EN (1<<0) |
Osator | 0:339b7abfa147 | 57 | #define WR_EN (1<<1) |
Osator | 0:339b7abfa147 | 58 | #define LOG_ENDPOINT(endpoint) ((endpoint>>1)<<2) |
Osator | 0:339b7abfa147 | 59 | |
Osator | 0:339b7abfa147 | 60 | // USB Receive Packet Length register |
Osator | 0:339b7abfa147 | 61 | #define DV (1UL<<10) |
Osator | 0:339b7abfa147 | 62 | #define PKT_RDY (1UL<<11) |
Osator | 0:339b7abfa147 | 63 | #define PKT_LNGTH_MASK (0x3ff) |
Osator | 0:339b7abfa147 | 64 | |
Osator | 0:339b7abfa147 | 65 | // Serial Interface Engine (SIE) |
Osator | 0:339b7abfa147 | 66 | #define SIE_WRITE (0x01) |
Osator | 0:339b7abfa147 | 67 | #define SIE_READ (0x02) |
Osator | 0:339b7abfa147 | 68 | #define SIE_COMMAND (0x05) |
Osator | 0:339b7abfa147 | 69 | #define SIE_CMD_CODE(phase, data) ((phase<<8)|(data<<16)) |
Osator | 0:339b7abfa147 | 70 | |
Osator | 0:339b7abfa147 | 71 | // SIE Command codes |
Osator | 0:339b7abfa147 | 72 | #define SIE_CMD_SET_ADDRESS (0xD0) |
Osator | 0:339b7abfa147 | 73 | #define SIE_CMD_CONFIGURE_DEVICE (0xD8) |
Osator | 0:339b7abfa147 | 74 | #define SIE_CMD_SET_MODE (0xF3) |
Osator | 0:339b7abfa147 | 75 | #define SIE_CMD_READ_FRAME_NUMBER (0xF5) |
Osator | 0:339b7abfa147 | 76 | #define SIE_CMD_READ_TEST_REGISTER (0xFD) |
Osator | 0:339b7abfa147 | 77 | #define SIE_CMD_SET_DEVICE_STATUS (0xFE) |
Osator | 0:339b7abfa147 | 78 | #define SIE_CMD_GET_DEVICE_STATUS (0xFE) |
Osator | 0:339b7abfa147 | 79 | #define SIE_CMD_GET_ERROR_CODE (0xFF) |
Osator | 0:339b7abfa147 | 80 | #define SIE_CMD_READ_ERROR_STATUS (0xFB) |
Osator | 0:339b7abfa147 | 81 | |
Osator | 0:339b7abfa147 | 82 | #define SIE_CMD_SELECT_ENDPOINT(endpoint) (0x00+endpoint) |
Osator | 0:339b7abfa147 | 83 | #define SIE_CMD_SELECT_ENDPOINT_CLEAR_INTERRUPT(endpoint) (0x40+endpoint) |
Osator | 0:339b7abfa147 | 84 | #define SIE_CMD_SET_ENDPOINT_STATUS(endpoint) (0x40+endpoint) |
Osator | 0:339b7abfa147 | 85 | |
Osator | 0:339b7abfa147 | 86 | #define SIE_CMD_CLEAR_BUFFER (0xF2) |
Osator | 0:339b7abfa147 | 87 | #define SIE_CMD_VALIDATE_BUFFER (0xFA) |
Osator | 0:339b7abfa147 | 88 | |
Osator | 0:339b7abfa147 | 89 | // SIE Device Status register |
Osator | 0:339b7abfa147 | 90 | #define SIE_DS_CON (1<<0) |
Osator | 0:339b7abfa147 | 91 | #define SIE_DS_CON_CH (1<<1) |
Osator | 0:339b7abfa147 | 92 | #define SIE_DS_SUS (1<<2) |
Osator | 0:339b7abfa147 | 93 | #define SIE_DS_SUS_CH (1<<3) |
Osator | 0:339b7abfa147 | 94 | #define SIE_DS_RST (1<<4) |
Osator | 0:339b7abfa147 | 95 | |
Osator | 0:339b7abfa147 | 96 | // SIE Device Set Address register |
Osator | 0:339b7abfa147 | 97 | #define SIE_DSA_DEV_EN (1<<7) |
Osator | 0:339b7abfa147 | 98 | |
Osator | 0:339b7abfa147 | 99 | // SIE Configue Device register |
Osator | 0:339b7abfa147 | 100 | #define SIE_CONF_DEVICE (1<<0) |
Osator | 0:339b7abfa147 | 101 | |
Osator | 0:339b7abfa147 | 102 | // Select Endpoint register |
Osator | 0:339b7abfa147 | 103 | #define SIE_SE_FE (1<<0) |
Osator | 0:339b7abfa147 | 104 | #define SIE_SE_ST (1<<1) |
Osator | 0:339b7abfa147 | 105 | #define SIE_SE_STP (1<<2) |
Osator | 0:339b7abfa147 | 106 | #define SIE_SE_PO (1<<3) |
Osator | 0:339b7abfa147 | 107 | #define SIE_SE_EPN (1<<4) |
Osator | 0:339b7abfa147 | 108 | #define SIE_SE_B_1_FULL (1<<5) |
Osator | 0:339b7abfa147 | 109 | #define SIE_SE_B_2_FULL (1<<6) |
Osator | 0:339b7abfa147 | 110 | |
Osator | 0:339b7abfa147 | 111 | // Set Endpoint Status command |
Osator | 0:339b7abfa147 | 112 | #define SIE_SES_ST (1<<0) |
Osator | 0:339b7abfa147 | 113 | #define SIE_SES_DA (1<<5) |
Osator | 0:339b7abfa147 | 114 | #define SIE_SES_RF_MO (1<<6) |
Osator | 0:339b7abfa147 | 115 | #define SIE_SES_CND_ST (1<<7) |
Osator | 0:339b7abfa147 | 116 | |
Osator | 0:339b7abfa147 | 117 | |
Osator | 0:339b7abfa147 | 118 | USBHAL * USBHAL::instance; |
Osator | 0:339b7abfa147 | 119 | |
Osator | 0:339b7abfa147 | 120 | static volatile int epComplete; |
Osator | 0:339b7abfa147 | 121 | static uint32_t endpointStallState; |
Osator | 0:339b7abfa147 | 122 | |
Osator | 0:339b7abfa147 | 123 | static void SIECommand(uint32_t command) { |
Osator | 0:339b7abfa147 | 124 | // The command phase of a SIE transaction |
Osator | 0:339b7abfa147 | 125 | LPC_USB->DevIntClr = CCEMPTY; |
Osator | 0:339b7abfa147 | 126 | LPC_USB->CmdCode = SIE_CMD_CODE(SIE_COMMAND, command); |
Osator | 0:339b7abfa147 | 127 | while (!(LPC_USB->DevIntSt & CCEMPTY)); |
Osator | 0:339b7abfa147 | 128 | } |
Osator | 0:339b7abfa147 | 129 | |
Osator | 0:339b7abfa147 | 130 | static void SIEWriteData(uint8_t data) { |
Osator | 0:339b7abfa147 | 131 | // The data write phase of a SIE transaction |
Osator | 0:339b7abfa147 | 132 | LPC_USB->DevIntClr = CCEMPTY; |
Osator | 0:339b7abfa147 | 133 | LPC_USB->CmdCode = SIE_CMD_CODE(SIE_WRITE, data); |
Osator | 0:339b7abfa147 | 134 | while (!(LPC_USB->DevIntSt & CCEMPTY)); |
Osator | 0:339b7abfa147 | 135 | } |
Osator | 0:339b7abfa147 | 136 | |
Osator | 0:339b7abfa147 | 137 | static uint8_t SIEReadData(uint32_t command) { |
Osator | 0:339b7abfa147 | 138 | // The data read phase of a SIE transaction |
Osator | 0:339b7abfa147 | 139 | LPC_USB->DevIntClr = CDFULL; |
Osator | 0:339b7abfa147 | 140 | LPC_USB->CmdCode = SIE_CMD_CODE(SIE_READ, command); |
Osator | 0:339b7abfa147 | 141 | while (!(LPC_USB->DevIntSt & CDFULL)); |
Osator | 0:339b7abfa147 | 142 | return (uint8_t)LPC_USB->CmdData; |
Osator | 0:339b7abfa147 | 143 | } |
Osator | 0:339b7abfa147 | 144 | |
Osator | 0:339b7abfa147 | 145 | static void SIEsetDeviceStatus(uint8_t status) { |
Osator | 0:339b7abfa147 | 146 | // Write SIE device status register |
Osator | 0:339b7abfa147 | 147 | SIECommand(SIE_CMD_SET_DEVICE_STATUS); |
Osator | 0:339b7abfa147 | 148 | SIEWriteData(status); |
Osator | 0:339b7abfa147 | 149 | } |
Osator | 0:339b7abfa147 | 150 | |
Osator | 0:339b7abfa147 | 151 | static uint8_t SIEgetDeviceStatus(void) { |
Osator | 0:339b7abfa147 | 152 | // Read SIE device status register |
Osator | 0:339b7abfa147 | 153 | SIECommand(SIE_CMD_GET_DEVICE_STATUS); |
Osator | 0:339b7abfa147 | 154 | return SIEReadData(SIE_CMD_GET_DEVICE_STATUS); |
Osator | 0:339b7abfa147 | 155 | } |
Osator | 0:339b7abfa147 | 156 | |
Osator | 0:339b7abfa147 | 157 | void SIEsetAddress(uint8_t address) { |
Osator | 0:339b7abfa147 | 158 | // Write SIE device address register |
Osator | 0:339b7abfa147 | 159 | SIECommand(SIE_CMD_SET_ADDRESS); |
Osator | 0:339b7abfa147 | 160 | SIEWriteData((address & 0x7f) | SIE_DSA_DEV_EN); |
Osator | 0:339b7abfa147 | 161 | } |
Osator | 0:339b7abfa147 | 162 | |
Osator | 0:339b7abfa147 | 163 | static uint8_t SIEselectEndpoint(uint8_t endpoint) { |
Osator | 0:339b7abfa147 | 164 | // SIE select endpoint command |
Osator | 0:339b7abfa147 | 165 | SIECommand(SIE_CMD_SELECT_ENDPOINT(endpoint)); |
Osator | 0:339b7abfa147 | 166 | return SIEReadData(SIE_CMD_SELECT_ENDPOINT(endpoint)); |
Osator | 0:339b7abfa147 | 167 | } |
Osator | 0:339b7abfa147 | 168 | |
Osator | 0:339b7abfa147 | 169 | static uint8_t SIEclearBuffer(void) { |
Osator | 0:339b7abfa147 | 170 | // SIE clear buffer command |
Osator | 0:339b7abfa147 | 171 | SIECommand(SIE_CMD_CLEAR_BUFFER); |
Osator | 0:339b7abfa147 | 172 | return SIEReadData(SIE_CMD_CLEAR_BUFFER); |
Osator | 0:339b7abfa147 | 173 | } |
Osator | 0:339b7abfa147 | 174 | |
Osator | 0:339b7abfa147 | 175 | static void SIEvalidateBuffer(void) { |
Osator | 0:339b7abfa147 | 176 | // SIE validate buffer command |
Osator | 0:339b7abfa147 | 177 | SIECommand(SIE_CMD_VALIDATE_BUFFER); |
Osator | 0:339b7abfa147 | 178 | } |
Osator | 0:339b7abfa147 | 179 | |
Osator | 0:339b7abfa147 | 180 | static void SIEsetEndpointStatus(uint8_t endpoint, uint8_t status) { |
Osator | 0:339b7abfa147 | 181 | // SIE set endpoint status command |
Osator | 0:339b7abfa147 | 182 | SIECommand(SIE_CMD_SET_ENDPOINT_STATUS(endpoint)); |
Osator | 0:339b7abfa147 | 183 | SIEWriteData(status); |
Osator | 0:339b7abfa147 | 184 | } |
Osator | 0:339b7abfa147 | 185 | |
Osator | 0:339b7abfa147 | 186 | static uint16_t SIEgetFrameNumber(void) __attribute__ ((unused)); |
Osator | 0:339b7abfa147 | 187 | static uint16_t SIEgetFrameNumber(void) { |
Osator | 0:339b7abfa147 | 188 | // Read current frame number |
Osator | 0:339b7abfa147 | 189 | uint16_t lowByte; |
Osator | 0:339b7abfa147 | 190 | uint16_t highByte; |
Osator | 0:339b7abfa147 | 191 | |
Osator | 0:339b7abfa147 | 192 | SIECommand(SIE_CMD_READ_FRAME_NUMBER); |
Osator | 0:339b7abfa147 | 193 | lowByte = SIEReadData(SIE_CMD_READ_FRAME_NUMBER); |
Osator | 0:339b7abfa147 | 194 | highByte = SIEReadData(SIE_CMD_READ_FRAME_NUMBER); |
Osator | 0:339b7abfa147 | 195 | |
Osator | 0:339b7abfa147 | 196 | return (highByte << 8) | lowByte; |
Osator | 0:339b7abfa147 | 197 | } |
Osator | 0:339b7abfa147 | 198 | |
Osator | 0:339b7abfa147 | 199 | static void SIEconfigureDevice(void) { |
Osator | 0:339b7abfa147 | 200 | // SIE Configure device command |
Osator | 0:339b7abfa147 | 201 | SIECommand(SIE_CMD_CONFIGURE_DEVICE); |
Osator | 0:339b7abfa147 | 202 | SIEWriteData(SIE_CONF_DEVICE); |
Osator | 0:339b7abfa147 | 203 | } |
Osator | 0:339b7abfa147 | 204 | |
Osator | 0:339b7abfa147 | 205 | static void SIEunconfigureDevice(void) { |
Osator | 0:339b7abfa147 | 206 | // SIE Configure device command |
Osator | 0:339b7abfa147 | 207 | SIECommand(SIE_CMD_CONFIGURE_DEVICE); |
Osator | 0:339b7abfa147 | 208 | SIEWriteData(0); |
Osator | 0:339b7abfa147 | 209 | } |
Osator | 0:339b7abfa147 | 210 | |
Osator | 0:339b7abfa147 | 211 | static void SIEconnect(void) { |
Osator | 0:339b7abfa147 | 212 | // Connect USB device |
Osator | 0:339b7abfa147 | 213 | uint8_t status = SIEgetDeviceStatus(); |
Osator | 0:339b7abfa147 | 214 | SIEsetDeviceStatus(status | SIE_DS_CON); |
Osator | 0:339b7abfa147 | 215 | } |
Osator | 0:339b7abfa147 | 216 | |
Osator | 0:339b7abfa147 | 217 | |
Osator | 0:339b7abfa147 | 218 | static void SIEdisconnect(void) { |
Osator | 0:339b7abfa147 | 219 | // Disconnect USB device |
Osator | 0:339b7abfa147 | 220 | uint8_t status = SIEgetDeviceStatus(); |
Osator | 0:339b7abfa147 | 221 | SIEsetDeviceStatus(status & ~SIE_DS_CON); |
Osator | 0:339b7abfa147 | 222 | } |
Osator | 0:339b7abfa147 | 223 | |
Osator | 0:339b7abfa147 | 224 | |
Osator | 0:339b7abfa147 | 225 | static uint8_t selectEndpointClearInterrupt(uint8_t endpoint) { |
Osator | 0:339b7abfa147 | 226 | // Implemented using using EP_INT_CLR. |
Osator | 0:339b7abfa147 | 227 | LPC_USB->EpIntClr = EP(endpoint); |
Osator | 0:339b7abfa147 | 228 | while (!(LPC_USB->DevIntSt & CDFULL)); |
Osator | 0:339b7abfa147 | 229 | return (uint8_t)LPC_USB->CmdData; |
Osator | 0:339b7abfa147 | 230 | } |
Osator | 0:339b7abfa147 | 231 | |
Osator | 0:339b7abfa147 | 232 | |
Osator | 0:339b7abfa147 | 233 | static void enableEndpointEvent(uint8_t endpoint) { |
Osator | 0:339b7abfa147 | 234 | // Enable an endpoint interrupt |
Osator | 0:339b7abfa147 | 235 | LPC_USB->EpIntEn |= EP(endpoint); |
Osator | 0:339b7abfa147 | 236 | } |
Osator | 0:339b7abfa147 | 237 | |
Osator | 0:339b7abfa147 | 238 | static void disableEndpointEvent(uint8_t endpoint) __attribute__ ((unused)); |
Osator | 0:339b7abfa147 | 239 | static void disableEndpointEvent(uint8_t endpoint) { |
Osator | 0:339b7abfa147 | 240 | // Disable an endpoint interrupt |
Osator | 0:339b7abfa147 | 241 | LPC_USB->EpIntEn &= ~EP(endpoint); |
Osator | 0:339b7abfa147 | 242 | } |
Osator | 0:339b7abfa147 | 243 | |
Osator | 0:339b7abfa147 | 244 | static volatile uint32_t __attribute__((used)) dummyRead; |
Osator | 0:339b7abfa147 | 245 | uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer) { |
Osator | 0:339b7abfa147 | 246 | // Read from an OUT endpoint |
Osator | 0:339b7abfa147 | 247 | uint32_t size; |
Osator | 0:339b7abfa147 | 248 | uint32_t i; |
Osator | 0:339b7abfa147 | 249 | uint32_t data = 0; |
Osator | 0:339b7abfa147 | 250 | uint8_t offset; |
Osator | 0:339b7abfa147 | 251 | |
Osator | 0:339b7abfa147 | 252 | LPC_USB->Ctrl = LOG_ENDPOINT(endpoint) | RD_EN; |
Osator | 0:339b7abfa147 | 253 | while (!(LPC_USB->RxPLen & PKT_RDY)); |
Osator | 0:339b7abfa147 | 254 | |
Osator | 0:339b7abfa147 | 255 | size = LPC_USB->RxPLen & PKT_LNGTH_MASK; |
Osator | 0:339b7abfa147 | 256 | |
Osator | 0:339b7abfa147 | 257 | offset = 0; |
Osator | 0:339b7abfa147 | 258 | |
Osator | 0:339b7abfa147 | 259 | if (size > 0) { |
Osator | 0:339b7abfa147 | 260 | for (i=0; i<size; i++) { |
Osator | 0:339b7abfa147 | 261 | if (offset==0) { |
Osator | 0:339b7abfa147 | 262 | // Fetch up to four bytes of data as a word |
Osator | 0:339b7abfa147 | 263 | data = LPC_USB->RxData; |
Osator | 0:339b7abfa147 | 264 | } |
Osator | 0:339b7abfa147 | 265 | |
Osator | 0:339b7abfa147 | 266 | // extract a byte |
Osator | 0:339b7abfa147 | 267 | *buffer = (data>>offset) & 0xff; |
Osator | 0:339b7abfa147 | 268 | buffer++; |
Osator | 0:339b7abfa147 | 269 | |
Osator | 0:339b7abfa147 | 270 | // move on to the next byte |
Osator | 0:339b7abfa147 | 271 | offset = (offset + 8) % 32; |
Osator | 0:339b7abfa147 | 272 | } |
Osator | 0:339b7abfa147 | 273 | } else { |
Osator | 0:339b7abfa147 | 274 | dummyRead = LPC_USB->RxData; |
Osator | 0:339b7abfa147 | 275 | } |
Osator | 0:339b7abfa147 | 276 | |
Osator | 0:339b7abfa147 | 277 | LPC_USB->Ctrl = 0; |
Osator | 0:339b7abfa147 | 278 | |
Osator | 0:339b7abfa147 | 279 | if ((endpoint >> 1) % 3 || (endpoint >> 1) == 0) { |
Osator | 0:339b7abfa147 | 280 | SIEselectEndpoint(endpoint); |
Osator | 0:339b7abfa147 | 281 | SIEclearBuffer(); |
Osator | 0:339b7abfa147 | 282 | } |
Osator | 0:339b7abfa147 | 283 | |
Osator | 0:339b7abfa147 | 284 | return size; |
Osator | 0:339b7abfa147 | 285 | } |
Osator | 0:339b7abfa147 | 286 | |
Osator | 0:339b7abfa147 | 287 | static void endpointWritecore(uint8_t endpoint, uint8_t *buffer, uint32_t size) { |
Osator | 0:339b7abfa147 | 288 | // Write to an IN endpoint |
Osator | 0:339b7abfa147 | 289 | uint32_t temp, data; |
Osator | 0:339b7abfa147 | 290 | uint8_t offset; |
Osator | 0:339b7abfa147 | 291 | |
Osator | 0:339b7abfa147 | 292 | LPC_USB->Ctrl = LOG_ENDPOINT(endpoint) | WR_EN; |
Osator | 0:339b7abfa147 | 293 | |
Osator | 0:339b7abfa147 | 294 | LPC_USB->TxPLen = size; |
Osator | 0:339b7abfa147 | 295 | offset = 0; |
Osator | 0:339b7abfa147 | 296 | data = 0; |
Osator | 0:339b7abfa147 | 297 | |
Osator | 0:339b7abfa147 | 298 | if (size>0) { |
Osator | 0:339b7abfa147 | 299 | do { |
Osator | 0:339b7abfa147 | 300 | // Fetch next data byte into a word-sized temporary variable |
Osator | 0:339b7abfa147 | 301 | temp = *buffer++; |
Osator | 0:339b7abfa147 | 302 | |
Osator | 0:339b7abfa147 | 303 | // Add to current data word |
Osator | 0:339b7abfa147 | 304 | temp = temp << offset; |
Osator | 0:339b7abfa147 | 305 | data = data | temp; |
Osator | 0:339b7abfa147 | 306 | |
Osator | 0:339b7abfa147 | 307 | // move on to the next byte |
Osator | 0:339b7abfa147 | 308 | offset = (offset + 8) % 32; |
Osator | 0:339b7abfa147 | 309 | size--; |
Osator | 0:339b7abfa147 | 310 | |
Osator | 0:339b7abfa147 | 311 | if ((offset==0) || (size==0)) { |
Osator | 0:339b7abfa147 | 312 | // Write the word to the endpoint |
Osator | 0:339b7abfa147 | 313 | LPC_USB->TxData = data; |
Osator | 0:339b7abfa147 | 314 | data = 0; |
Osator | 0:339b7abfa147 | 315 | } |
Osator | 0:339b7abfa147 | 316 | } while (size>0); |
Osator | 0:339b7abfa147 | 317 | } else { |
Osator | 0:339b7abfa147 | 318 | LPC_USB->TxData = 0; |
Osator | 0:339b7abfa147 | 319 | } |
Osator | 0:339b7abfa147 | 320 | |
Osator | 0:339b7abfa147 | 321 | // Clear WR_EN to cover zero length packet case |
Osator | 0:339b7abfa147 | 322 | LPC_USB->Ctrl=0; |
Osator | 0:339b7abfa147 | 323 | |
Osator | 0:339b7abfa147 | 324 | SIEselectEndpoint(endpoint); |
Osator | 0:339b7abfa147 | 325 | SIEvalidateBuffer(); |
Osator | 0:339b7abfa147 | 326 | } |
Osator | 0:339b7abfa147 | 327 | |
Osator | 0:339b7abfa147 | 328 | USBHAL::USBHAL(void) { |
Osator | 0:339b7abfa147 | 329 | // Disable IRQ |
Osator | 0:339b7abfa147 | 330 | NVIC_DisableIRQ(USB_IRQn); |
Osator | 0:339b7abfa147 | 331 | |
Osator | 0:339b7abfa147 | 332 | // fill in callback array |
Osator | 0:339b7abfa147 | 333 | epCallback[0] = &USBHAL::EP1_OUT_callback; |
Osator | 0:339b7abfa147 | 334 | epCallback[1] = &USBHAL::EP1_IN_callback; |
Osator | 0:339b7abfa147 | 335 | epCallback[2] = &USBHAL::EP2_OUT_callback; |
Osator | 0:339b7abfa147 | 336 | epCallback[3] = &USBHAL::EP2_IN_callback; |
Osator | 0:339b7abfa147 | 337 | epCallback[4] = &USBHAL::EP3_OUT_callback; |
Osator | 0:339b7abfa147 | 338 | epCallback[5] = &USBHAL::EP3_IN_callback; |
Osator | 0:339b7abfa147 | 339 | epCallback[6] = &USBHAL::EP4_OUT_callback; |
Osator | 0:339b7abfa147 | 340 | epCallback[7] = &USBHAL::EP4_IN_callback; |
Osator | 0:339b7abfa147 | 341 | epCallback[8] = &USBHAL::EP5_OUT_callback; |
Osator | 0:339b7abfa147 | 342 | epCallback[9] = &USBHAL::EP5_IN_callback; |
Osator | 0:339b7abfa147 | 343 | epCallback[10] = &USBHAL::EP6_OUT_callback; |
Osator | 0:339b7abfa147 | 344 | epCallback[11] = &USBHAL::EP6_IN_callback; |
Osator | 0:339b7abfa147 | 345 | epCallback[12] = &USBHAL::EP7_OUT_callback; |
Osator | 0:339b7abfa147 | 346 | epCallback[13] = &USBHAL::EP7_IN_callback; |
Osator | 0:339b7abfa147 | 347 | epCallback[14] = &USBHAL::EP8_OUT_callback; |
Osator | 0:339b7abfa147 | 348 | epCallback[15] = &USBHAL::EP8_IN_callback; |
Osator | 0:339b7abfa147 | 349 | epCallback[16] = &USBHAL::EP9_OUT_callback; |
Osator | 0:339b7abfa147 | 350 | epCallback[17] = &USBHAL::EP9_IN_callback; |
Osator | 0:339b7abfa147 | 351 | epCallback[18] = &USBHAL::EP10_OUT_callback; |
Osator | 0:339b7abfa147 | 352 | epCallback[19] = &USBHAL::EP10_IN_callback; |
Osator | 0:339b7abfa147 | 353 | epCallback[20] = &USBHAL::EP11_OUT_callback; |
Osator | 0:339b7abfa147 | 354 | epCallback[21] = &USBHAL::EP11_IN_callback; |
Osator | 0:339b7abfa147 | 355 | epCallback[22] = &USBHAL::EP12_OUT_callback; |
Osator | 0:339b7abfa147 | 356 | epCallback[23] = &USBHAL::EP12_IN_callback; |
Osator | 0:339b7abfa147 | 357 | epCallback[24] = &USBHAL::EP13_OUT_callback; |
Osator | 0:339b7abfa147 | 358 | epCallback[25] = &USBHAL::EP13_IN_callback; |
Osator | 0:339b7abfa147 | 359 | epCallback[26] = &USBHAL::EP14_OUT_callback; |
Osator | 0:339b7abfa147 | 360 | epCallback[27] = &USBHAL::EP14_IN_callback; |
Osator | 0:339b7abfa147 | 361 | epCallback[28] = &USBHAL::EP15_OUT_callback; |
Osator | 0:339b7abfa147 | 362 | epCallback[29] = &USBHAL::EP15_IN_callback; |
Osator | 0:339b7abfa147 | 363 | |
Osator | 0:339b7abfa147 | 364 | // Enable power to USB device controller |
Osator | 0:339b7abfa147 | 365 | LPC_SC->PCONP |= PCUSB; |
Osator | 0:339b7abfa147 | 366 | |
Osator | 0:339b7abfa147 | 367 | // Enable USB clocks |
Osator | 0:339b7abfa147 | 368 | LPC_USB->USBClkCtrl |= DEV_CLK_EN | AHB_CLK_EN | PORT_CLK_EN; |
Osator | 0:339b7abfa147 | 369 | while ((LPC_USB->USBClkSt & (DEV_CLK_EN | AHB_CLK_EN | PORT_CLK_EN)) != (DEV_CLK_ON | AHB_CLK_ON | PORT_CLK_EN)); |
Osator | 0:339b7abfa147 | 370 | |
Osator | 0:339b7abfa147 | 371 | // Select port USB2 |
Osator | 0:339b7abfa147 | 372 | LPC_USB->StCtrl |= 3; |
Osator | 0:339b7abfa147 | 373 | |
Osator | 0:339b7abfa147 | 374 | |
Osator | 0:339b7abfa147 | 375 | // Configure pin P0.31 to be USB2 |
Osator | 0:339b7abfa147 | 376 | LPC_IOCON->P0_31 &= ~0x07; |
Osator | 0:339b7abfa147 | 377 | LPC_IOCON->P0_31 |= 0x01; |
Osator | 0:339b7abfa147 | 378 | |
Osator | 0:339b7abfa147 | 379 | // Disconnect USB device |
Osator | 0:339b7abfa147 | 380 | SIEdisconnect(); |
Osator | 0:339b7abfa147 | 381 | |
Osator | 0:339b7abfa147 | 382 | // Configure pin P0.14 to be Connect |
Osator | 0:339b7abfa147 | 383 | LPC_IOCON->P0_14 &= ~0x07; |
Osator | 0:339b7abfa147 | 384 | LPC_IOCON->P0_14 |= 0x03; |
Osator | 0:339b7abfa147 | 385 | |
Osator | 0:339b7abfa147 | 386 | // Connect must be low for at least 2.5uS |
Osator | 0:339b7abfa147 | 387 | wait(0.3); |
Osator | 0:339b7abfa147 | 388 | |
Osator | 0:339b7abfa147 | 389 | // Set the maximum packet size for the control endpoints |
Osator | 0:339b7abfa147 | 390 | realiseEndpoint(EP0IN, MAX_PACKET_SIZE_EP0, 0); |
Osator | 0:339b7abfa147 | 391 | realiseEndpoint(EP0OUT, MAX_PACKET_SIZE_EP0, 0); |
Osator | 0:339b7abfa147 | 392 | |
Osator | 0:339b7abfa147 | 393 | // Attach IRQ |
Osator | 0:339b7abfa147 | 394 | instance = this; |
Osator | 0:339b7abfa147 | 395 | NVIC_SetVector(USB_IRQn, (uint32_t)&_usbisr); |
Osator | 0:339b7abfa147 | 396 | |
Osator | 0:339b7abfa147 | 397 | // Enable interrupts for device events and EP0 |
Osator | 0:339b7abfa147 | 398 | LPC_USB->DevIntEn = EP_SLOW | DEV_STAT | FRAME; |
Osator | 0:339b7abfa147 | 399 | enableEndpointEvent(EP0IN); |
Osator | 0:339b7abfa147 | 400 | enableEndpointEvent(EP0OUT); |
Osator | 0:339b7abfa147 | 401 | } |
Osator | 0:339b7abfa147 | 402 | |
Osator | 0:339b7abfa147 | 403 | USBHAL::~USBHAL(void) { |
Osator | 0:339b7abfa147 | 404 | // Ensure device disconnected |
Osator | 0:339b7abfa147 | 405 | SIEdisconnect(); |
Osator | 0:339b7abfa147 | 406 | // Disable USB interrupts |
Osator | 0:339b7abfa147 | 407 | NVIC_DisableIRQ(USB_IRQn); |
Osator | 0:339b7abfa147 | 408 | } |
Osator | 0:339b7abfa147 | 409 | |
Osator | 0:339b7abfa147 | 410 | void USBHAL::connect(void) { |
Osator | 0:339b7abfa147 | 411 | NVIC_EnableIRQ(USB_IRQn); |
Osator | 0:339b7abfa147 | 412 | // Connect USB device |
Osator | 0:339b7abfa147 | 413 | SIEconnect(); |
Osator | 0:339b7abfa147 | 414 | } |
Osator | 0:339b7abfa147 | 415 | |
Osator | 0:339b7abfa147 | 416 | void USBHAL::disconnect(void) { |
Osator | 0:339b7abfa147 | 417 | NVIC_DisableIRQ(USB_IRQn); |
Osator | 0:339b7abfa147 | 418 | // Disconnect USB device |
Osator | 0:339b7abfa147 | 419 | SIEdisconnect(); |
Osator | 0:339b7abfa147 | 420 | } |
Osator | 0:339b7abfa147 | 421 | |
Osator | 0:339b7abfa147 | 422 | void USBHAL::configureDevice(void) { |
Osator | 0:339b7abfa147 | 423 | SIEconfigureDevice(); |
Osator | 0:339b7abfa147 | 424 | } |
Osator | 0:339b7abfa147 | 425 | |
Osator | 0:339b7abfa147 | 426 | void USBHAL::unconfigureDevice(void) { |
Osator | 0:339b7abfa147 | 427 | SIEunconfigureDevice(); |
Osator | 0:339b7abfa147 | 428 | } |
Osator | 0:339b7abfa147 | 429 | |
Osator | 0:339b7abfa147 | 430 | void USBHAL::setAddress(uint8_t address) { |
Osator | 0:339b7abfa147 | 431 | SIEsetAddress(address); |
Osator | 0:339b7abfa147 | 432 | } |
Osator | 0:339b7abfa147 | 433 | |
Osator | 0:339b7abfa147 | 434 | void USBHAL::EP0setup(uint8_t *buffer) { |
Osator | 0:339b7abfa147 | 435 | endpointReadcore(EP0OUT, buffer); |
Osator | 0:339b7abfa147 | 436 | } |
Osator | 0:339b7abfa147 | 437 | |
Osator | 0:339b7abfa147 | 438 | void USBHAL::EP0read(void) { |
Osator | 0:339b7abfa147 | 439 | // Not required |
Osator | 0:339b7abfa147 | 440 | } |
Osator | 0:339b7abfa147 | 441 | |
Osator | 0:339b7abfa147 | 442 | void USBHAL::EP0readStage(void) { |
Osator | 0:339b7abfa147 | 443 | // Not required |
Osator | 0:339b7abfa147 | 444 | } |
Osator | 0:339b7abfa147 | 445 | |
Osator | 0:339b7abfa147 | 446 | uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) { |
Osator | 0:339b7abfa147 | 447 | return endpointReadcore(EP0OUT, buffer); |
Osator | 0:339b7abfa147 | 448 | } |
Osator | 0:339b7abfa147 | 449 | |
Osator | 0:339b7abfa147 | 450 | void USBHAL::EP0write(uint8_t *buffer, uint32_t size) { |
Osator | 0:339b7abfa147 | 451 | endpointWritecore(EP0IN, buffer, size); |
Osator | 0:339b7abfa147 | 452 | } |
Osator | 0:339b7abfa147 | 453 | |
Osator | 0:339b7abfa147 | 454 | void USBHAL::EP0getWriteResult(void) { |
Osator | 0:339b7abfa147 | 455 | // Not required |
Osator | 0:339b7abfa147 | 456 | } |
Osator | 0:339b7abfa147 | 457 | |
Osator | 0:339b7abfa147 | 458 | void USBHAL::EP0stall(void) { |
Osator | 0:339b7abfa147 | 459 | // This will stall both control endpoints |
Osator | 0:339b7abfa147 | 460 | stallEndpoint(EP0OUT); |
Osator | 0:339b7abfa147 | 461 | } |
Osator | 0:339b7abfa147 | 462 | |
Osator | 0:339b7abfa147 | 463 | EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) { |
Osator | 0:339b7abfa147 | 464 | return EP_PENDING; |
Osator | 0:339b7abfa147 | 465 | } |
Osator | 0:339b7abfa147 | 466 | |
Osator | 0:339b7abfa147 | 467 | EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) { |
Osator | 0:339b7abfa147 | 468 | |
Osator | 0:339b7abfa147 | 469 | //for isochronous endpoint, we don't wait an interrupt |
Osator | 0:339b7abfa147 | 470 | if ((endpoint >> 1) % 3 || (endpoint >> 1) == 0) { |
Osator | 0:339b7abfa147 | 471 | if (!(epComplete & EP(endpoint))) |
Osator | 0:339b7abfa147 | 472 | return EP_PENDING; |
Osator | 0:339b7abfa147 | 473 | } |
Osator | 0:339b7abfa147 | 474 | |
Osator | 0:339b7abfa147 | 475 | *bytesRead = endpointReadcore(endpoint, buffer); |
Osator | 0:339b7abfa147 | 476 | epComplete &= ~EP(endpoint); |
Osator | 0:339b7abfa147 | 477 | return EP_COMPLETED; |
Osator | 0:339b7abfa147 | 478 | } |
Osator | 0:339b7abfa147 | 479 | |
Osator | 0:339b7abfa147 | 480 | EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size) { |
Osator | 0:339b7abfa147 | 481 | if (getEndpointStallState(endpoint)) { |
Osator | 0:339b7abfa147 | 482 | return EP_STALLED; |
Osator | 0:339b7abfa147 | 483 | } |
Osator | 0:339b7abfa147 | 484 | |
Osator | 0:339b7abfa147 | 485 | epComplete &= ~EP(endpoint); |
Osator | 0:339b7abfa147 | 486 | |
Osator | 0:339b7abfa147 | 487 | endpointWritecore(endpoint, data, size); |
Osator | 0:339b7abfa147 | 488 | return EP_PENDING; |
Osator | 0:339b7abfa147 | 489 | } |
Osator | 0:339b7abfa147 | 490 | |
Osator | 0:339b7abfa147 | 491 | EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) { |
Osator | 0:339b7abfa147 | 492 | if (epComplete & EP(endpoint)) { |
Osator | 0:339b7abfa147 | 493 | epComplete &= ~EP(endpoint); |
Osator | 0:339b7abfa147 | 494 | return EP_COMPLETED; |
Osator | 0:339b7abfa147 | 495 | } |
Osator | 0:339b7abfa147 | 496 | |
Osator | 0:339b7abfa147 | 497 | return EP_PENDING; |
Osator | 0:339b7abfa147 | 498 | } |
Osator | 0:339b7abfa147 | 499 | |
Osator | 0:339b7abfa147 | 500 | bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flags) { |
Osator | 0:339b7abfa147 | 501 | // Realise an endpoint |
Osator | 0:339b7abfa147 | 502 | LPC_USB->DevIntClr = EP_RLZED; |
Osator | 0:339b7abfa147 | 503 | LPC_USB->ReEp |= EP(endpoint); |
Osator | 0:339b7abfa147 | 504 | LPC_USB->EpInd = endpoint; |
Osator | 0:339b7abfa147 | 505 | LPC_USB->MaxPSize = maxPacket; |
Osator | 0:339b7abfa147 | 506 | |
Osator | 0:339b7abfa147 | 507 | while (!(LPC_USB->DevIntSt & EP_RLZED)); |
Osator | 0:339b7abfa147 | 508 | LPC_USB->DevIntClr = EP_RLZED; |
Osator | 0:339b7abfa147 | 509 | |
Osator | 0:339b7abfa147 | 510 | // Clear stall state |
Osator | 0:339b7abfa147 | 511 | endpointStallState &= ~EP(endpoint); |
Osator | 0:339b7abfa147 | 512 | |
Osator | 0:339b7abfa147 | 513 | enableEndpointEvent(endpoint); |
Osator | 0:339b7abfa147 | 514 | return true; |
Osator | 0:339b7abfa147 | 515 | } |
Osator | 0:339b7abfa147 | 516 | |
Osator | 0:339b7abfa147 | 517 | void USBHAL::stallEndpoint(uint8_t endpoint) { |
Osator | 0:339b7abfa147 | 518 | // Stall an endpoint |
Osator | 0:339b7abfa147 | 519 | if ( (endpoint==EP0IN) || (endpoint==EP0OUT) ) { |
Osator | 0:339b7abfa147 | 520 | // Conditionally stall both control endpoints |
Osator | 0:339b7abfa147 | 521 | SIEsetEndpointStatus(EP0OUT, SIE_SES_CND_ST); |
Osator | 0:339b7abfa147 | 522 | } else { |
Osator | 0:339b7abfa147 | 523 | SIEsetEndpointStatus(endpoint, SIE_SES_ST); |
Osator | 0:339b7abfa147 | 524 | |
Osator | 0:339b7abfa147 | 525 | // Update stall state |
Osator | 0:339b7abfa147 | 526 | endpointStallState |= EP(endpoint); |
Osator | 0:339b7abfa147 | 527 | } |
Osator | 0:339b7abfa147 | 528 | } |
Osator | 0:339b7abfa147 | 529 | |
Osator | 0:339b7abfa147 | 530 | void USBHAL::unstallEndpoint(uint8_t endpoint) { |
Osator | 0:339b7abfa147 | 531 | // Unstall an endpoint. The endpoint will also be reinitialised |
Osator | 0:339b7abfa147 | 532 | SIEsetEndpointStatus(endpoint, 0); |
Osator | 0:339b7abfa147 | 533 | |
Osator | 0:339b7abfa147 | 534 | // Update stall state |
Osator | 0:339b7abfa147 | 535 | endpointStallState &= ~EP(endpoint); |
Osator | 0:339b7abfa147 | 536 | } |
Osator | 0:339b7abfa147 | 537 | |
Osator | 0:339b7abfa147 | 538 | bool USBHAL::getEndpointStallState(uint8_t endpoint) { |
Osator | 0:339b7abfa147 | 539 | // Returns true if endpoint stalled |
Osator | 0:339b7abfa147 | 540 | return endpointStallState & EP(endpoint); |
Osator | 0:339b7abfa147 | 541 | } |
Osator | 0:339b7abfa147 | 542 | |
Osator | 0:339b7abfa147 | 543 | void USBHAL::remoteWakeup(void) { |
Osator | 0:339b7abfa147 | 544 | // Remote wakeup |
Osator | 0:339b7abfa147 | 545 | uint8_t status; |
Osator | 0:339b7abfa147 | 546 | |
Osator | 0:339b7abfa147 | 547 | // Enable USB clocks |
Osator | 0:339b7abfa147 | 548 | LPC_USB->USBClkCtrl |= DEV_CLK_EN | AHB_CLK_EN; |
Osator | 0:339b7abfa147 | 549 | while (LPC_USB->USBClkSt != (DEV_CLK_ON | AHB_CLK_ON)); |
Osator | 0:339b7abfa147 | 550 | |
Osator | 0:339b7abfa147 | 551 | status = SIEgetDeviceStatus(); |
Osator | 0:339b7abfa147 | 552 | SIEsetDeviceStatus(status & ~SIE_DS_SUS); |
Osator | 0:339b7abfa147 | 553 | } |
Osator | 0:339b7abfa147 | 554 | |
Osator | 0:339b7abfa147 | 555 | void USBHAL::_usbisr(void) { |
Osator | 0:339b7abfa147 | 556 | instance->usbisr(); |
Osator | 0:339b7abfa147 | 557 | } |
Osator | 0:339b7abfa147 | 558 | |
Osator | 0:339b7abfa147 | 559 | |
Osator | 0:339b7abfa147 | 560 | void USBHAL::usbisr(void) { |
Osator | 0:339b7abfa147 | 561 | uint8_t devStat; |
Osator | 0:339b7abfa147 | 562 | |
Osator | 0:339b7abfa147 | 563 | if (LPC_USB->DevIntSt & FRAME) { |
Osator | 0:339b7abfa147 | 564 | // Start of frame event |
Osator | 0:339b7abfa147 | 565 | SOF(SIEgetFrameNumber()); |
Osator | 0:339b7abfa147 | 566 | // Clear interrupt status flag |
Osator | 0:339b7abfa147 | 567 | LPC_USB->DevIntClr = FRAME; |
Osator | 0:339b7abfa147 | 568 | } |
Osator | 0:339b7abfa147 | 569 | |
Osator | 0:339b7abfa147 | 570 | if (LPC_USB->DevIntSt & DEV_STAT) { |
Osator | 0:339b7abfa147 | 571 | // Device Status interrupt |
Osator | 0:339b7abfa147 | 572 | // Must clear the interrupt status flag before reading the device status from the SIE |
Osator | 0:339b7abfa147 | 573 | LPC_USB->DevIntClr = DEV_STAT; |
Osator | 0:339b7abfa147 | 574 | |
Osator | 0:339b7abfa147 | 575 | // Read device status from SIE |
Osator | 0:339b7abfa147 | 576 | devStat = SIEgetDeviceStatus(); |
Osator | 0:339b7abfa147 | 577 | //printf("devStat: %d\r\n", devStat); |
Osator | 0:339b7abfa147 | 578 | |
Osator | 0:339b7abfa147 | 579 | if (devStat & SIE_DS_SUS_CH) { |
Osator | 0:339b7abfa147 | 580 | // Suspend status changed |
Osator | 0:339b7abfa147 | 581 | if((devStat & SIE_DS_SUS) != 0) { |
Osator | 0:339b7abfa147 | 582 | suspendStateChanged(0); |
Osator | 0:339b7abfa147 | 583 | } |
Osator | 0:339b7abfa147 | 584 | } |
Osator | 0:339b7abfa147 | 585 | |
Osator | 0:339b7abfa147 | 586 | if (devStat & SIE_DS_RST) { |
Osator | 0:339b7abfa147 | 587 | // Bus reset |
Osator | 0:339b7abfa147 | 588 | if((devStat & SIE_DS_SUS) == 0) { |
Osator | 0:339b7abfa147 | 589 | suspendStateChanged(1); |
Osator | 0:339b7abfa147 | 590 | } |
Osator | 0:339b7abfa147 | 591 | busReset(); |
Osator | 0:339b7abfa147 | 592 | } |
Osator | 0:339b7abfa147 | 593 | } |
Osator | 0:339b7abfa147 | 594 | |
Osator | 0:339b7abfa147 | 595 | if (LPC_USB->DevIntSt & EP_SLOW) { |
Osator | 0:339b7abfa147 | 596 | // (Slow) Endpoint Interrupt |
Osator | 0:339b7abfa147 | 597 | |
Osator | 0:339b7abfa147 | 598 | // Process each endpoint interrupt |
Osator | 0:339b7abfa147 | 599 | if (LPC_USB->EpIntSt & EP(EP0OUT)) { |
Osator | 0:339b7abfa147 | 600 | if (selectEndpointClearInterrupt(EP0OUT) & SIE_SE_STP) { |
Osator | 0:339b7abfa147 | 601 | // this is a setup packet |
Osator | 0:339b7abfa147 | 602 | EP0setupCallback(); |
Osator | 0:339b7abfa147 | 603 | } else { |
Osator | 0:339b7abfa147 | 604 | EP0out(); |
Osator | 0:339b7abfa147 | 605 | } |
Osator | 0:339b7abfa147 | 606 | LPC_USB->DevIntClr = EP_SLOW; |
Osator | 0:339b7abfa147 | 607 | } |
Osator | 0:339b7abfa147 | 608 | |
Osator | 0:339b7abfa147 | 609 | if (LPC_USB->EpIntSt & EP(EP0IN)) { |
Osator | 0:339b7abfa147 | 610 | selectEndpointClearInterrupt(EP0IN); |
Osator | 0:339b7abfa147 | 611 | LPC_USB->DevIntClr = EP_SLOW; |
Osator | 0:339b7abfa147 | 612 | EP0in(); |
Osator | 0:339b7abfa147 | 613 | } |
Osator | 0:339b7abfa147 | 614 | |
Osator | 0:339b7abfa147 | 615 | for (uint8_t num = 2; num < 16*2; num++) { |
Osator | 0:339b7abfa147 | 616 | if (LPC_USB->EpIntSt & EP(num)) { |
Osator | 0:339b7abfa147 | 617 | selectEndpointClearInterrupt(num); |
Osator | 0:339b7abfa147 | 618 | epComplete |= EP(num); |
Osator | 0:339b7abfa147 | 619 | LPC_USB->DevIntClr = EP_SLOW; |
Osator | 0:339b7abfa147 | 620 | if ((instance->*(epCallback[num - 2]))()) { |
Osator | 0:339b7abfa147 | 621 | epComplete &= ~EP(num); |
Osator | 0:339b7abfa147 | 622 | } |
Osator | 0:339b7abfa147 | 623 | } |
Osator | 0:339b7abfa147 | 624 | } |
Osator | 0:339b7abfa147 | 625 | } |
Osator | 0:339b7abfa147 | 626 | } |
Osator | 0:339b7abfa147 | 627 | |
Osator | 0:339b7abfa147 | 628 | #endif |