Forked to make modifications to bring the USBHID into USB compliance and add additional features.

Dependents:   HW4_AudioControl

Fork of USBDevice by mbed official

As of Revision 18 everything in the USBHID specification is now implemented, except Multi-reports.

Revision comments for changelist 18

USBHID.cpp

  • Added SET_PROTOCOL support
  • Added GET_PROTOCOL support
  • protocolSate is set to 1 by default to match USB HID specification. This variable should be checked to determine which format theinput report should have. 1 - Use the user specified report format. 0 - Use the BOOT protocol report format.

Revision comments for changelist 16

  • HID_REPORT transformed from structure to class. This was done for several reasons.
  1. When multiple reports are used the 64 byte size for every report becomes a problem.
  2. The length value should always remain the same for a report, Make the constructor set the vale at the same time it allocates memory for the DATA area.
  • By default the data will be an array of MAX_HID_REPORT_SIZE like the structure,
  • When given a length argument, the hid_report.length will be set, and hid_report.data will be an array of the size given.
  • Length zero causes data to be NULL
  • Mostly backwards compatible. The definition of a destructor caused a compiler error in USBMouse::update and USBMousekeyboard::update. This error was caused by instatiation of HID_REPORT in the middle of an IF logic statement. These files have been modified. The error complained that the logic skipped object initialization. The HID_REPORT has been moved to the class definition. Since both ABSOLUTE and RELATIVE modes used the HID_REPORT, this seems to make more sense. Previously the hid_report would be instatiated in <class>::mousesend and <class>::update.

Revision comments for changelist 14

USBdevice.cpp

  • Modified USB device state to change from Configure when disconnect is called.
  • Modified the call back function for when the suspend state changes. This should be used to turn off peripherals to conserve power.

Revision comments for changelist 13

USBdevice.cpp

  • ) Changed DEBUG messages to be more descriptive for string descriptor
  • ) Bug fix: Control Transfers did not actually transfer the data from Buffer to transfer->ptr

USBHIDTypes.h

  • ) Added ALL CLASS request to KEYWORD list
  • ) Added KEYWORDS for report type

USBHID.h

  • ) Added a new constructor to specify size of feature report
  • ) Added HID_REPORT inputReport and featureReport
  • ) Added data structures to support IDLE rate
  • ) Added data structures to support callback functions

USBHID.cpp

  • ) Changed constructor to initialize new feature data structures
  • ) Implemented Set_IDLE/GET_IDLE and the periodic resend of non-changed data
  • ) Implemented HID specification required control transfer GET_REPORT
  • ) Fixed issue where Intreput transfers and control transfers did not access the same data structures.
  • ) Implemented Feature reports
  • ) Implemented Callback Hooks for get_report/set_report actions.
  • ) Added callback hooks for interupt actions in the new functions.
  • ) interupt transfer can now write to outputReport
  • ) Modified SET_REPORT code to function for multiple types.
  • ) Refactored some code in preperation to add multi report support
Test NumberTest DescriptionTest ResultNotes
1Use USBmouse to verify backward compatibility of constructor and methodsPass
2Test SET_REPORT can set a feature reportPass
3Test GET_REPORT can retrieve a feature reportPass
4Test SET_IDLE sets up a reoccuring triggerPassIOCTL_SET_POLL_FREQUENCY_MSEC does not function for the windows HID driver. A Special test program is used to rearm the IDLE rate after windows sets it to zero
5Test SET_IDLE disables a triggerPassWindows automatically sends this command to a HID device when it is inserted.
6Enabled DEBUG in USBDevice.cpp and generated str descriptor requests.Pass
7Test SET_REPORT can set an output reportPass
8Test GET_REPORT can retrieve an output reportPass
9ReadFile, accesses the input_reportPass
10WriteFile accesses the output_report, via interupt transfer when ep1_out is used.Pass
11WriteFile accesses the output_report, via control transfer when ep1_out is NOT used.Not Tested
12Callback hooks trigger independently for each type of set_report/get_reportPass
13New constructor sets feature_report sizePass
14Control transfer SET_REPORT and writeFile access the same data structureBUGThe same data structure is accessed, but the data transfer size is different. The writeFile strips the leading byte which is the report ID, The Control transfer keeps the byte.
15Control transfer GET_REPORT and readFile access the same data structureBUGThe same dtat structure is accessed, but the data transfer size is different. The readFile strips the leading byte which is the report ID, The Control transfer keeps the byte.
16Test GET_IDLE retrieves the IDLE rateUnknownWindows HID driver does not implement IOCTL_HID_GET_POLL_FREQUENCY_MSEC
Committer:
jakowisp
Date:
Thu Aug 08 08:02:46 2013 +0000
Revision:
18:cb3afa532fcd
Parent:
14:00cd29199e0e
Default ProtocolState was incorrectly being set to 0:Boot when the HID specification states that the default should be 1:Report

Who changed what in which revision?

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