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