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