USB device stack

Dependents:   mbed-mX-USB-TEST1 USBMSD_SD_HID_HelloWorld HidTest MIDI_usb_bridge ... more

Legacy Warning

This is an mbed 2 library. To learn more about mbed OS 5, visit the docs.

Pull requests against this repository are no longer supported. Please raise against mbed OS 5 as documented above.

Committer:
Kojto
Date:
Thu Jul 20 10:14:36 2017 +0100
Revision:
70:2c525a50f1b6
Parent:
60:0e6b3f44926e
Update libraries (ed9d1da)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 8:335f2506f422 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
samux 8:335f2506f422 2 *
samux 8:335f2506f422 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 8:335f2506f422 4 * and associated documentation files (the "Software"), to deal in the Software without
samux 8:335f2506f422 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
samux 8:335f2506f422 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
samux 8:335f2506f422 7 * Software is furnished to do so, subject to the following conditions:
samux 8:335f2506f422 8 *
samux 8:335f2506f422 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 8:335f2506f422 10 * substantial portions of the Software.
samux 8:335f2506f422 11 *
samux 8:335f2506f422 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 8:335f2506f422 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 8:335f2506f422 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 8:335f2506f422 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 8:335f2506f422 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 8:335f2506f422 17 */
samux 8:335f2506f422 18
mbed_official 43:c0605f23f916 19 #if defined(TARGET_KL25Z) | defined(TARGET_KL43Z) | defined(TARGET_KL46Z) | defined(TARGET_K20D50M) | defined(TARGET_K64F) | defined(TARGET_K22F) | defined(TARGET_TEENSY3_1)
samux 8:335f2506f422 20
Kojto 70:2c525a50f1b6 21 #if defined(TARGET_KSDK2_MCUS)
Kojto 70:2c525a50f1b6 22 #include "fsl_common.h"
Kojto 70:2c525a50f1b6 23 #endif
samux 8:335f2506f422 24 #include "USBHAL.h"
samux 8:335f2506f422 25
samux 8:335f2506f422 26 USBHAL * USBHAL::instance;
samux 8:335f2506f422 27
samux 8:335f2506f422 28 static volatile int epComplete = 0;
samux 8:335f2506f422 29
samux 8:335f2506f422 30 // Convert physical endpoint number to register bit
samux 8:335f2506f422 31 #define EP(endpoint) (1<<(endpoint))
samux 8:335f2506f422 32
samux 8:335f2506f422 33 // Convert physical to logical
samux 8:335f2506f422 34 #define PHY_TO_LOG(endpoint) ((endpoint)>>1)
samux 8:335f2506f422 35
samux 8:335f2506f422 36 // Get endpoint direction
samux 8:335f2506f422 37 #define IN_EP(endpoint) ((endpoint) & 1U ? true : false)
samux 8:335f2506f422 38 #define OUT_EP(endpoint) ((endpoint) & 1U ? false : true)
samux 8:335f2506f422 39
samux 8:335f2506f422 40 #define BD_OWN_MASK (1<<7)
samux 8:335f2506f422 41 #define BD_DATA01_MASK (1<<6)
samux 8:335f2506f422 42 #define BD_KEEP_MASK (1<<5)
samux 8:335f2506f422 43 #define BD_NINC_MASK (1<<4)
samux 8:335f2506f422 44 #define BD_DTS_MASK (1<<3)
samux 8:335f2506f422 45 #define BD_STALL_MASK (1<<2)
samux 8:335f2506f422 46
samux 8:335f2506f422 47 #define TX 1
samux 8:335f2506f422 48 #define RX 0
samux 8:335f2506f422 49 #define ODD 0
samux 8:335f2506f422 50 #define EVEN 1
samux 8:335f2506f422 51 // this macro waits a physical endpoint number
samux 8:335f2506f422 52 #define EP_BDT_IDX(ep, dir, odd) (((ep * 4) + (2 * dir) + (1 * odd)))
samux 8:335f2506f422 53
samux 8:335f2506f422 54 #define SETUP_TOKEN 0x0D
samux 8:335f2506f422 55 #define IN_TOKEN 0x09
samux 8:335f2506f422 56 #define OUT_TOKEN 0x01
samux 8:335f2506f422 57 #define TOK_PID(idx) ((bdt[idx].info >> 2) & 0x0F)
samux 8:335f2506f422 58
samux 8:335f2506f422 59 // for each endpt: 8 bytes
samux 8:335f2506f422 60 typedef struct BDT {
samux 8:335f2506f422 61 uint8_t info; // BD[0:7]
samux 8:335f2506f422 62 uint8_t dummy; // RSVD: BD[8:15]
samux 8:335f2506f422 63 uint16_t byte_count; // BD[16:32]
samux 8:335f2506f422 64 uint32_t address; // Addr
mbed_official 20:d38b72fed893 65 } BDT;
samux 8:335f2506f422 66
Kojto 70:2c525a50f1b6 67 // there are:
Kojto 70:2c525a50f1b6 68 // * 4 bidirectionnal endpt -> 8 physical endpt
Kojto 70:2c525a50f1b6 69 // * as there are ODD and EVEN buffer -> 8*2 bdt
Kojto 70:2c525a50f1b6 70 MBED_ALIGN(512) BDT bdt[NUMBER_OF_PHYSICAL_ENDPOINTS * 2]; // 512 bytes aligned!
samux 8:335f2506f422 71
Kojto 70:2c525a50f1b6 72 uint8_t * endpoint_buffer[NUMBER_OF_PHYSICAL_ENDPOINTS * 2];
samux 8:335f2506f422 73
samux 8:335f2506f422 74 static uint8_t set_addr = 0;
samux 8:335f2506f422 75 static uint8_t addr = 0;
samux 8:335f2506f422 76
samux 8:335f2506f422 77 static uint32_t Data1 = 0x55555555;
samux 8:335f2506f422 78
samux 8:335f2506f422 79 static uint32_t frameNumber() {
bogdanm 13:16731886c049 80 return((USB0->FRMNUML | (USB0->FRMNUMH << 8)) & 0x07FF);
samux 8:335f2506f422 81 }
samux 8:335f2506f422 82
samux 8:335f2506f422 83 uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer) {
samux 8:335f2506f422 84 return 0;
samux 8:335f2506f422 85 }
samux 8:335f2506f422 86
mbed_official 20:d38b72fed893 87 USBHAL::USBHAL(void) {
samux 8:335f2506f422 88 // Disable IRQ
samux 8:335f2506f422 89 NVIC_DisableIRQ(USB0_IRQn);
mbed_official 20:d38b72fed893 90
Kojto 70:2c525a50f1b6 91 #if (defined(FSL_FEATURE_SOC_MPU_COUNT) && (FSL_FEATURE_SOC_MPU_COUNT > 0U))
mbed_official 22:5b7d31d9d3f3 92 MPU->CESR=0;
mbed_official 22:5b7d31d9d3f3 93 #endif
samux 8:335f2506f422 94 // fill in callback array
samux 8:335f2506f422 95 epCallback[0] = &USBHAL::EP1_OUT_callback;
samux 8:335f2506f422 96 epCallback[1] = &USBHAL::EP1_IN_callback;
samux 8:335f2506f422 97 epCallback[2] = &USBHAL::EP2_OUT_callback;
samux 8:335f2506f422 98 epCallback[3] = &USBHAL::EP2_IN_callback;
samux 8:335f2506f422 99 epCallback[4] = &USBHAL::EP3_OUT_callback;
samux 8:335f2506f422 100 epCallback[5] = &USBHAL::EP3_IN_callback;
samux 8:335f2506f422 101 epCallback[6] = &USBHAL::EP4_OUT_callback;
samux 8:335f2506f422 102 epCallback[7] = &USBHAL::EP4_IN_callback;
mbed_official 20:d38b72fed893 103
Kojto 70:2c525a50f1b6 104 #if defined(TARGET_KL43Z) || defined(TARGET_K22F) || defined(TARGET_K64F)
mbed_official 34:a4e9ddc2e2af 105 // enable USBFS clock
Kojto 70:2c525a50f1b6 106 CLOCK_EnableUsbfs0Clock(kCLOCK_UsbSrcIrc48M, 48000000U);
mbed_official 34:a4e9ddc2e2af 107 #else
samux 8:335f2506f422 108 // choose usb src as PLL
mbed_official 38:dfe51ad5cacf 109 SIM->SOPT2 &= ~SIM_SOPT2_PLLFLLSEL_MASK;
mbed_official 38:dfe51ad5cacf 110 SIM->SOPT2 |= (SIM_SOPT2_USBSRC_MASK | (1 << SIM_SOPT2_PLLFLLSEL_SHIFT));
mbed_official 20:d38b72fed893 111
samux 8:335f2506f422 112 // enable OTG clock
samux 8:335f2506f422 113 SIM->SCGC4 |= SIM_SCGC4_USBOTG_MASK;
mbed_official 34:a4e9ddc2e2af 114 #endif
samux 8:335f2506f422 115
samux 8:335f2506f422 116 // Attach IRQ
samux 8:335f2506f422 117 instance = this;
samux 8:335f2506f422 118 NVIC_SetVector(USB0_IRQn, (uint32_t)&_usbisr);
samux 8:335f2506f422 119 NVIC_EnableIRQ(USB0_IRQn);
mbed_official 20:d38b72fed893 120
samux 8:335f2506f422 121 // USB Module Configuration
samux 8:335f2506f422 122 // Set BDT Base Register
mbed_official 22:5b7d31d9d3f3 123 USB0->BDTPAGE1 = (uint8_t)((uint32_t)bdt>>8);
mbed_official 22:5b7d31d9d3f3 124 USB0->BDTPAGE2 = (uint8_t)((uint32_t)bdt>>16);
mbed_official 22:5b7d31d9d3f3 125 USB0->BDTPAGE3 = (uint8_t)((uint32_t)bdt>>24);
samux 8:335f2506f422 126
samux 8:335f2506f422 127 // Clear interrupt flag
samux 8:335f2506f422 128 USB0->ISTAT = 0xff;
samux 8:335f2506f422 129
samux 8:335f2506f422 130 // USB Interrupt Enablers
mbed_official 20:d38b72fed893 131 USB0->INTEN |= USB_INTEN_TOKDNEEN_MASK |
mbed_official 20:d38b72fed893 132 USB_INTEN_SOFTOKEN_MASK |
samux 8:335f2506f422 133 USB_INTEN_ERROREN_MASK |
samux 8:335f2506f422 134 USB_INTEN_USBRSTEN_MASK;
mbed_official 20:d38b72fed893 135
mbed_official 20:d38b72fed893 136 // Disable weak pull downs
mbed_official 20:d38b72fed893 137 USB0->USBCTRL &= ~(USB_USBCTRL_PDE_MASK | USB_USBCTRL_SUSP_MASK);
mbed_official 20:d38b72fed893 138
samux 8:335f2506f422 139 USB0->USBTRC0 |= 0x40;
Kojto 70:2c525a50f1b6 140
Kojto 70:2c525a50f1b6 141 /* Allocate control endpoint buffers */
Kojto 70:2c525a50f1b6 142 endpoint_buffer[EP_BDT_IDX(0, TX, ODD)] = (uint8_t *)malloc(MAX_PACKET_SIZE_EP0);
Kojto 70:2c525a50f1b6 143 endpoint_buffer[EP_BDT_IDX(0, RX, ODD)] = (uint8_t *)malloc(MAX_PACKET_SIZE_EP0);
samux 8:335f2506f422 144 }
samux 8:335f2506f422 145
samux 8:335f2506f422 146 USBHAL::~USBHAL(void) { }
samux 8:335f2506f422 147
samux 8:335f2506f422 148 void USBHAL::connect(void) {
samux 8:335f2506f422 149 // enable USB
samux 8:335f2506f422 150 USB0->CTL |= USB_CTL_USBENSOFEN_MASK;
samux 8:335f2506f422 151 // Pull up enable
samux 8:335f2506f422 152 USB0->CONTROL |= USB_CONTROL_DPPULLUPNONOTG_MASK;
Kojto 70:2c525a50f1b6 153
Kojto 70:2c525a50f1b6 154 // Allocate endpoint buffers; do allocate control endpoint buffers
Kojto 70:2c525a50f1b6 155 for (int i = 4; i < (NUMBER_OF_PHYSICAL_ENDPOINTS * 2); i++) {
Kojto 70:2c525a50f1b6 156 if ((i == EPISO_OUT) || (i == EPISO_IN)) {
Kojto 70:2c525a50f1b6 157 endpoint_buffer[i] = (uint8_t *)malloc(MAX_PACKET_SIZE_EPISO);
Kojto 70:2c525a50f1b6 158 } else {
Kojto 70:2c525a50f1b6 159 endpoint_buffer[i] = (uint8_t *)malloc(MAX_PACKET_SIZE_EPBULK);
Kojto 70:2c525a50f1b6 160 }
Kojto 70:2c525a50f1b6 161 }
samux 8:335f2506f422 162 }
samux 8:335f2506f422 163
samux 8:335f2506f422 164 void USBHAL::disconnect(void) {
samux 8:335f2506f422 165 // disable USB
samux 8:335f2506f422 166 USB0->CTL &= ~USB_CTL_USBENSOFEN_MASK;
samux 8:335f2506f422 167 // Pull up disable
samux 8:335f2506f422 168 USB0->CONTROL &= ~USB_CONTROL_DPPULLUPNONOTG_MASK;
bogdanm 14:d495202c90f4 169
Kojto 70:2c525a50f1b6 170 //Free buffers if required; do not free the control endpoint buffers
Kojto 70:2c525a50f1b6 171 for (int i = 4; i < (NUMBER_OF_PHYSICAL_ENDPOINTS * 2); i++) {
bogdanm 14:d495202c90f4 172 free(endpoint_buffer[i]);
bogdanm 14:d495202c90f4 173 endpoint_buffer[i] = NULL;
bogdanm 14:d495202c90f4 174 }
samux 8:335f2506f422 175 }
samux 8:335f2506f422 176
samux 8:335f2506f422 177 void USBHAL::configureDevice(void) {
samux 8:335f2506f422 178 // not needed
samux 8:335f2506f422 179 }
samux 8:335f2506f422 180
samux 8:335f2506f422 181 void USBHAL::unconfigureDevice(void) {
samux 8:335f2506f422 182 // not needed
samux 8:335f2506f422 183 }
samux 8:335f2506f422 184
samux 8:335f2506f422 185 void USBHAL::setAddress(uint8_t address) {
samux 8:335f2506f422 186 // we don't set the address now otherwise the usb controller does not ack
samux 8:335f2506f422 187 // we set a flag instead
samux 8:335f2506f422 188 // see usbisr when an IN token is received
samux 8:335f2506f422 189 set_addr = 1;
samux 8:335f2506f422 190 addr = address;
samux 8:335f2506f422 191 }
samux 8:335f2506f422 192
samux 8:335f2506f422 193 bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flags) {
samux 8:335f2506f422 194 uint32_t handshake_flag = 0;
samux 8:335f2506f422 195 uint8_t * buf;
samux 8:335f2506f422 196
samux 8:335f2506f422 197 if (endpoint > NUMBER_OF_PHYSICAL_ENDPOINTS - 1) {
samux 8:335f2506f422 198 return false;
samux 8:335f2506f422 199 }
samux 8:335f2506f422 200
samux 8:335f2506f422 201 uint32_t log_endpoint = PHY_TO_LOG(endpoint);
samux 8:335f2506f422 202
samux 8:335f2506f422 203 if ((flags & ISOCHRONOUS) == 0) {
samux 8:335f2506f422 204 handshake_flag = USB_ENDPT_EPHSHK_MASK;
Kojto 70:2c525a50f1b6 205 }
Kojto 70:2c525a50f1b6 206
Kojto 70:2c525a50f1b6 207 if (IN_EP(endpoint)) {
Kojto 70:2c525a50f1b6 208 buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)][0];
samux 8:335f2506f422 209 } else {
Kojto 70:2c525a50f1b6 210 buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)][0];
samux 8:335f2506f422 211 }
samux 8:335f2506f422 212
samux 8:335f2506f422 213 // IN endpt -> device to host (TX)
samux 8:335f2506f422 214 if (IN_EP(endpoint)) {
samux 8:335f2506f422 215 USB0->ENDPOINT[log_endpoint].ENDPT |= handshake_flag | // ep handshaking (not if iso endpoint)
samux 8:335f2506f422 216 USB_ENDPT_EPTXEN_MASK; // en TX (IN) tran
samux 8:335f2506f422 217 bdt[EP_BDT_IDX(log_endpoint, TX, ODD )].address = (uint32_t) buf;
samux 8:335f2506f422 218 bdt[EP_BDT_IDX(log_endpoint, TX, EVEN)].address = 0;
samux 8:335f2506f422 219 }
samux 8:335f2506f422 220 // OUT endpt -> host to device (RX)
samux 8:335f2506f422 221 else {
samux 8:335f2506f422 222 USB0->ENDPOINT[log_endpoint].ENDPT |= handshake_flag | // ep handshaking (not if iso endpoint)
samux 8:335f2506f422 223 USB_ENDPT_EPRXEN_MASK; // en RX (OUT) tran.
samux 8:335f2506f422 224 bdt[EP_BDT_IDX(log_endpoint, RX, ODD )].byte_count = maxPacket;
samux 8:335f2506f422 225 bdt[EP_BDT_IDX(log_endpoint, RX, ODD )].address = (uint32_t) buf;
samux 8:335f2506f422 226 bdt[EP_BDT_IDX(log_endpoint, RX, ODD )].info = BD_OWN_MASK | BD_DTS_MASK;
samux 8:335f2506f422 227 bdt[EP_BDT_IDX(log_endpoint, RX, EVEN)].info = 0;
samux 8:335f2506f422 228 }
samux 8:335f2506f422 229
samux 8:335f2506f422 230 Data1 |= (1 << endpoint);
samux 8:335f2506f422 231
samux 8:335f2506f422 232 return true;
samux 8:335f2506f422 233 }
samux 8:335f2506f422 234
samux 8:335f2506f422 235 // read setup packet
samux 8:335f2506f422 236 void USBHAL::EP0setup(uint8_t *buffer) {
samux 8:335f2506f422 237 uint32_t sz;
samux 8:335f2506f422 238 endpointReadResult(EP0OUT, buffer, &sz);
samux 8:335f2506f422 239 }
samux 8:335f2506f422 240
samux 8:335f2506f422 241 void USBHAL::EP0readStage(void) {
samux 8:335f2506f422 242 Data1 &= ~1UL; // set DATA0
samux 8:335f2506f422 243 bdt[0].info = (BD_DTS_MASK | BD_OWN_MASK);
samux 8:335f2506f422 244 }
samux 8:335f2506f422 245
samux 8:335f2506f422 246 void USBHAL::EP0read(void) {
samux 8:335f2506f422 247 uint32_t idx = EP_BDT_IDX(PHY_TO_LOG(EP0OUT), RX, 0);
samux 8:335f2506f422 248 bdt[idx].byte_count = MAX_PACKET_SIZE_EP0;
samux 8:335f2506f422 249 }
samux 8:335f2506f422 250
samux 8:335f2506f422 251 uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) {
samux 8:335f2506f422 252 uint32_t sz;
samux 8:335f2506f422 253 endpointReadResult(EP0OUT, buffer, &sz);
samux 8:335f2506f422 254 return sz;
samux 8:335f2506f422 255 }
samux 8:335f2506f422 256
samux 8:335f2506f422 257 void USBHAL::EP0write(uint8_t *buffer, uint32_t size) {
samux 8:335f2506f422 258 endpointWrite(EP0IN, buffer, size);
samux 8:335f2506f422 259 }
samux 8:335f2506f422 260
samux 8:335f2506f422 261 void USBHAL::EP0getWriteResult(void) {
samux 8:335f2506f422 262 }
samux 8:335f2506f422 263
samux 8:335f2506f422 264 void USBHAL::EP0stall(void) {
samux 8:335f2506f422 265 stallEndpoint(EP0OUT);
samux 8:335f2506f422 266 }
samux 8:335f2506f422 267
samux 8:335f2506f422 268 EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) {
samux 8:335f2506f422 269 endpoint = PHY_TO_LOG(endpoint);
samux 8:335f2506f422 270 uint32_t idx = EP_BDT_IDX(endpoint, RX, 0);
samux 8:335f2506f422 271 bdt[idx].byte_count = maximumSize;
samux 8:335f2506f422 272 return EP_PENDING;
samux 8:335f2506f422 273 }
samux 8:335f2506f422 274
samux 8:335f2506f422 275 EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) {
samux 8:335f2506f422 276 uint32_t n, sz, idx, setup = 0;
samux 8:335f2506f422 277 uint8_t not_iso;
samux 8:335f2506f422 278 uint8_t * ep_buf;
mbed_official 20:d38b72fed893 279
samux 8:335f2506f422 280 uint32_t log_endpoint = PHY_TO_LOG(endpoint);
mbed_official 20:d38b72fed893 281
samux 8:335f2506f422 282 if (endpoint > NUMBER_OF_PHYSICAL_ENDPOINTS - 1) {
samux 8:335f2506f422 283 return EP_INVALID;
samux 8:335f2506f422 284 }
samux 8:335f2506f422 285
samux 8:335f2506f422 286 // if read on a IN endpoint -> error
samux 8:335f2506f422 287 if (IN_EP(endpoint)) {
samux 8:335f2506f422 288 return EP_INVALID;
samux 8:335f2506f422 289 }
samux 8:335f2506f422 290
samux 8:335f2506f422 291 idx = EP_BDT_IDX(log_endpoint, RX, 0);
samux 8:335f2506f422 292 sz = bdt[idx].byte_count;
samux 8:335f2506f422 293 not_iso = USB0->ENDPOINT[log_endpoint].ENDPT & USB_ENDPT_EPHSHK_MASK;
samux 8:335f2506f422 294
samux 8:335f2506f422 295 //for isochronous endpoint, we don't wait an interrupt
samux 8:335f2506f422 296 if ((log_endpoint != 0) && not_iso && !(epComplete & EP(endpoint))) {
samux 8:335f2506f422 297 return EP_PENDING;
samux 8:335f2506f422 298 }
samux 8:335f2506f422 299
samux 8:335f2506f422 300 if ((log_endpoint == 0) && (TOK_PID(idx) == SETUP_TOKEN)) {
samux 8:335f2506f422 301 setup = 1;
samux 8:335f2506f422 302 }
samux 8:335f2506f422 303
Kojto 70:2c525a50f1b6 304 ep_buf = endpoint_buffer[idx];
samux 8:335f2506f422 305
samux 8:335f2506f422 306 for (n = 0; n < sz; n++) {
samux 8:335f2506f422 307 buffer[n] = ep_buf[n];
samux 8:335f2506f422 308 }
samux 8:335f2506f422 309
samux 8:335f2506f422 310 if (((Data1 >> endpoint) & 1) == ((bdt[idx].info >> 6) & 1)) {
samux 8:335f2506f422 311 if (setup && (buffer[6] == 0)) // if no setup data stage,
samux 8:335f2506f422 312 Data1 &= ~1UL; // set DATA0
mbed_official 20:d38b72fed893 313 else
samux 8:335f2506f422 314 Data1 ^= (1 << endpoint);
samux 8:335f2506f422 315 }
samux 8:335f2506f422 316
samux 8:335f2506f422 317 if (((Data1 >> endpoint) & 1)) {
samux 8:335f2506f422 318 bdt[idx].info = BD_DTS_MASK | BD_DATA01_MASK | BD_OWN_MASK;
samux 8:335f2506f422 319 }
samux 8:335f2506f422 320 else {
samux 8:335f2506f422 321 bdt[idx].info = BD_DTS_MASK | BD_OWN_MASK;
samux 8:335f2506f422 322 }
mbed_official 20:d38b72fed893 323
samux 8:335f2506f422 324 USB0->CTL &= ~USB_CTL_TXSUSPENDTOKENBUSY_MASK;
samux 8:335f2506f422 325 *bytesRead = sz;
samux 8:335f2506f422 326
samux 8:335f2506f422 327 epComplete &= ~EP(endpoint);
samux 8:335f2506f422 328 return EP_COMPLETED;
samux 8:335f2506f422 329 }
samux 8:335f2506f422 330
samux 8:335f2506f422 331 EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size) {
samux 8:335f2506f422 332 uint32_t idx, n;
samux 8:335f2506f422 333 uint8_t * ep_buf;
samux 8:335f2506f422 334
samux 8:335f2506f422 335 if (endpoint > NUMBER_OF_PHYSICAL_ENDPOINTS - 1) {
samux 8:335f2506f422 336 return EP_INVALID;
samux 8:335f2506f422 337 }
samux 8:335f2506f422 338
samux 8:335f2506f422 339 // if write on a OUT endpoint -> error
samux 8:335f2506f422 340 if (OUT_EP(endpoint)) {
samux 8:335f2506f422 341 return EP_INVALID;
samux 8:335f2506f422 342 }
samux 8:335f2506f422 343
samux 8:335f2506f422 344 idx = EP_BDT_IDX(PHY_TO_LOG(endpoint), TX, 0);
samux 8:335f2506f422 345 bdt[idx].byte_count = size;
mbed_official 20:d38b72fed893 346
Kojto 70:2c525a50f1b6 347 ep_buf = endpoint_buffer[idx];
mbed_official 20:d38b72fed893 348
samux 8:335f2506f422 349 for (n = 0; n < size; n++) {
samux 8:335f2506f422 350 ep_buf[n] = data[n];
samux 8:335f2506f422 351 }
mbed_official 20:d38b72fed893 352
samux 8:335f2506f422 353 if ((Data1 >> endpoint) & 1) {
samux 8:335f2506f422 354 bdt[idx].info = BD_OWN_MASK | BD_DTS_MASK;
samux 8:335f2506f422 355 } else {
samux 8:335f2506f422 356 bdt[idx].info = BD_OWN_MASK | BD_DTS_MASK | BD_DATA01_MASK;
samux 8:335f2506f422 357 }
mbed_official 20:d38b72fed893 358
samux 8:335f2506f422 359 Data1 ^= (1 << endpoint);
mbed_official 20:d38b72fed893 360
samux 8:335f2506f422 361 return EP_PENDING;
samux 8:335f2506f422 362 }
samux 8:335f2506f422 363
samux 8:335f2506f422 364 EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) {
samux 8:335f2506f422 365 if (epComplete & EP(endpoint)) {
samux 8:335f2506f422 366 epComplete &= ~EP(endpoint);
samux 8:335f2506f422 367 return EP_COMPLETED;
samux 8:335f2506f422 368 }
samux 8:335f2506f422 369
samux 8:335f2506f422 370 return EP_PENDING;
samux 8:335f2506f422 371 }
samux 8:335f2506f422 372
samux 8:335f2506f422 373 void USBHAL::stallEndpoint(uint8_t endpoint) {
samux 8:335f2506f422 374 USB0->ENDPOINT[PHY_TO_LOG(endpoint)].ENDPT |= USB_ENDPT_EPSTALL_MASK;
samux 8:335f2506f422 375 }
samux 8:335f2506f422 376
samux 8:335f2506f422 377 void USBHAL::unstallEndpoint(uint8_t endpoint) {
samux 8:335f2506f422 378 USB0->ENDPOINT[PHY_TO_LOG(endpoint)].ENDPT &= ~USB_ENDPT_EPSTALL_MASK;
samux 8:335f2506f422 379 }
samux 8:335f2506f422 380
samux 8:335f2506f422 381 bool USBHAL::getEndpointStallState(uint8_t endpoint) {
samux 8:335f2506f422 382 uint8_t stall = (USB0->ENDPOINT[PHY_TO_LOG(endpoint)].ENDPT & USB_ENDPT_EPSTALL_MASK);
samux 8:335f2506f422 383 return (stall) ? true : false;
samux 8:335f2506f422 384 }
samux 8:335f2506f422 385
samux 8:335f2506f422 386 void USBHAL::remoteWakeup(void) {
samux 8:335f2506f422 387 // [TODO]
samux 8:335f2506f422 388 }
samux 8:335f2506f422 389
samux 8:335f2506f422 390
samux 8:335f2506f422 391 void USBHAL::_usbisr(void) {
samux 8:335f2506f422 392 instance->usbisr();
samux 8:335f2506f422 393 }
samux 8:335f2506f422 394
samux 8:335f2506f422 395
samux 8:335f2506f422 396 void USBHAL::usbisr(void) {
samux 8:335f2506f422 397 uint8_t i;
samux 8:335f2506f422 398 uint8_t istat = USB0->ISTAT;
samux 8:335f2506f422 399
samux 8:335f2506f422 400 // reset interrupt
mbed_official 20:d38b72fed893 401 if (istat & USB_ISTAT_USBRST_MASK) {
samux 8:335f2506f422 402 // disable all endpt
samux 8:335f2506f422 403 for(i = 0; i < 16; i++) {
samux 8:335f2506f422 404 USB0->ENDPOINT[i].ENDPT = 0x00;
samux 8:335f2506f422 405 }
samux 8:335f2506f422 406
samux 8:335f2506f422 407 // enable control endpoint
samux 8:335f2506f422 408 realiseEndpoint(EP0OUT, MAX_PACKET_SIZE_EP0, 0);
samux 8:335f2506f422 409 realiseEndpoint(EP0IN, MAX_PACKET_SIZE_EP0, 0);
samux 8:335f2506f422 410
samux 8:335f2506f422 411 Data1 = 0x55555555;
samux 8:335f2506f422 412 USB0->CTL |= USB_CTL_ODDRST_MASK;
samux 8:335f2506f422 413
samux 8:335f2506f422 414 USB0->ISTAT = 0xFF; // clear all interrupt status flags
samux 8:335f2506f422 415 USB0->ERRSTAT = 0xFF; // clear all error flags
samux 8:335f2506f422 416 USB0->ERREN = 0xFF; // enable error interrupt sources
samux 8:335f2506f422 417 USB0->ADDR = 0x00; // set default address
samux 8:335f2506f422 418
Kojto 70:2c525a50f1b6 419 // reset bus for USBDevice layer
Kojto 70:2c525a50f1b6 420 busReset();
Kojto 70:2c525a50f1b6 421
samux 8:335f2506f422 422 return;
samux 8:335f2506f422 423 }
samux 8:335f2506f422 424
samux 8:335f2506f422 425 // resume interrupt
samux 8:335f2506f422 426 if (istat & USB_ISTAT_RESUME_MASK) {
samux 8:335f2506f422 427 USB0->ISTAT = USB_ISTAT_RESUME_MASK;
samux 8:335f2506f422 428 }
samux 8:335f2506f422 429
samux 8:335f2506f422 430 // SOF interrupt
samux 8:335f2506f422 431 if (istat & USB_ISTAT_SOFTOK_MASK) {
mbed_official 20:d38b72fed893 432 USB0->ISTAT = USB_ISTAT_SOFTOK_MASK;
samux 8:335f2506f422 433 // SOF event, read frame number
samux 8:335f2506f422 434 SOF(frameNumber());
samux 8:335f2506f422 435 }
mbed_official 20:d38b72fed893 436
samux 8:335f2506f422 437 // stall interrupt
samux 8:335f2506f422 438 if (istat & 1<<7) {
samux 8:335f2506f422 439 if (USB0->ENDPOINT[0].ENDPT & USB_ENDPT_EPSTALL_MASK)
samux 8:335f2506f422 440 USB0->ENDPOINT[0].ENDPT &= ~USB_ENDPT_EPSTALL_MASK;
samux 8:335f2506f422 441 USB0->ISTAT |= USB_ISTAT_STALL_MASK;
samux 8:335f2506f422 442 }
samux 8:335f2506f422 443
samux 8:335f2506f422 444 // token interrupt
samux 8:335f2506f422 445 if (istat & 1<<3) {
samux 8:335f2506f422 446 uint32_t num = (USB0->STAT >> 4) & 0x0F;
samux 8:335f2506f422 447 uint32_t dir = (USB0->STAT >> 3) & 0x01;
samux 8:335f2506f422 448 uint32_t ev_odd = (USB0->STAT >> 2) & 0x01;
mbed_official 60:0e6b3f44926e 449 int endpoint = (num << 1) | dir;
samux 8:335f2506f422 450
samux 8:335f2506f422 451 // setup packet
samux 8:335f2506f422 452 if ((num == 0) && (TOK_PID((EP_BDT_IDX(num, dir, ev_odd))) == SETUP_TOKEN)) {
samux 8:335f2506f422 453 Data1 &= ~0x02;
samux 8:335f2506f422 454 bdt[EP_BDT_IDX(0, TX, EVEN)].info &= ~BD_OWN_MASK;
samux 8:335f2506f422 455 bdt[EP_BDT_IDX(0, TX, ODD)].info &= ~BD_OWN_MASK;
samux 8:335f2506f422 456
samux 8:335f2506f422 457 // EP0 SETUP event (SETUP data received)
samux 8:335f2506f422 458 EP0setupCallback();
mbed_official 20:d38b72fed893 459
samux 8:335f2506f422 460 } else {
samux 8:335f2506f422 461 // OUT packet
samux 8:335f2506f422 462 if (TOK_PID((EP_BDT_IDX(num, dir, ev_odd))) == OUT_TOKEN) {
samux 8:335f2506f422 463 if (num == 0)
samux 8:335f2506f422 464 EP0out();
samux 8:335f2506f422 465 else {
mbed_official 60:0e6b3f44926e 466 epComplete |= EP(endpoint);
mbed_official 60:0e6b3f44926e 467 if ((instance->*(epCallback[endpoint - 2]))()) {
mbed_official 60:0e6b3f44926e 468 epComplete &= ~EP(endpoint);
samux 8:335f2506f422 469 }
samux 8:335f2506f422 470 }
samux 8:335f2506f422 471 }
samux 8:335f2506f422 472
samux 8:335f2506f422 473 // IN packet
samux 8:335f2506f422 474 if (TOK_PID((EP_BDT_IDX(num, dir, ev_odd))) == IN_TOKEN) {
samux 8:335f2506f422 475 if (num == 0) {
samux 8:335f2506f422 476 EP0in();
samux 8:335f2506f422 477 if (set_addr == 1) {
samux 8:335f2506f422 478 USB0->ADDR = addr & 0x7F;
samux 8:335f2506f422 479 set_addr = 0;
samux 8:335f2506f422 480 }
samux 8:335f2506f422 481 }
samux 8:335f2506f422 482 else {
mbed_official 60:0e6b3f44926e 483 epComplete |= EP(endpoint);
mbed_official 60:0e6b3f44926e 484 if ((instance->*(epCallback[endpoint - 2]))()) {
mbed_official 60:0e6b3f44926e 485 epComplete &= ~EP(endpoint);
samux 8:335f2506f422 486 }
samux 8:335f2506f422 487 }
samux 8:335f2506f422 488 }
samux 8:335f2506f422 489 }
samux 8:335f2506f422 490
samux 8:335f2506f422 491 USB0->ISTAT = USB_ISTAT_TOKDNE_MASK;
samux 8:335f2506f422 492 }
mbed_official 20:d38b72fed893 493
samux 8:335f2506f422 494 // sleep interrupt
samux 8:335f2506f422 495 if (istat & 1<<4) {
samux 8:335f2506f422 496 USB0->ISTAT |= USB_ISTAT_SLEEP_MASK;
mbed_official 20:d38b72fed893 497 }
samux 8:335f2506f422 498
samux 8:335f2506f422 499 // error interrupt
samux 8:335f2506f422 500 if (istat & USB_ISTAT_ERROR_MASK) {
samux 8:335f2506f422 501 USB0->ERRSTAT = 0xFF;
samux 8:335f2506f422 502 USB0->ISTAT |= USB_ISTAT_ERROR_MASK;
samux 8:335f2506f422 503 }
samux 8:335f2506f422 504 }
samux 8:335f2506f422 505
samux 8:335f2506f422 506
samux 8:335f2506f422 507 #endif