Base station firmware

Committer:
Perijah
Date:
Tue May 24 13:16:55 2016 +0000
Revision:
0:ecc3925d3f8c
Base station firmware

Who changed what in which revision?

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