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