works!

Dependencies:   mbed

Committer:
mjoun
Date:
Fri Feb 21 20:40:56 2020 +0000
Revision:
1:6e512faaa17c
works

Who changed what in which revision?

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