Sensor reporting over USB CDC

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
wue
Date:
Wed Apr 16 12:20:12 2014 +0000
Revision:
0:7b58cdacf811
Sensor reporting over USB CDC

Who changed what in which revision?

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