A web server for monitoring and controlling a MakerBot Replicator over the USB host and ethernet.

Dependencies:   IAP NTPClient RTC mbed-rtos mbed Socket lwip-sys lwip BurstSPI

Fork of LPC1768_Mini-DK by Frank Vannieuwkerke

Makerbot Server for LPC1768 Copyright (c) 2013, jake (at) allaboutjake (dot) com All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • The name of the author and/or copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, AUTHOR, OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Warnings:

This is not a commercial product or a hardened and secure network appliance. It is intended as a thought experiment or proof of concept and should not be relied upon in any way. Always operate your 3D printer in a safe and controlled manner.

Do not connect this directly to the exposed internet. It is intended to be behind a secure firewall (and NAT) such that it will only accept commands from the local network. Imagine how much fun a hacker could have instructing your 3D printer to continually print Standford bunnies. Well it could be much worse then that- a malicious user could send commands that could crash your machine (both in the software sense, as well as in the "smash your moving parts against the side of the machine repeatedly sense), overheat your extruders, cause your build plate to catch fire, and do severe damage to the machine, any surrounding building and propery. You have been warned.

Never print unattended and be ready to step in and stop the machine if something goes wrong. Keep in mind, a 3D printer has heaters that are operating at high temperatures, and if something starts to burn, it could cause damage to the machine, other property, and/or hurt yourself, pets, or others.

You should understand what you are doing. The source code here is not intended as a finished product or set of step by step instructions. You should engineer your own solution, which may wind up being better than mine.

Proceed at your own risk. You've been warned. (Several times) If you break your Makerbot, burn your house down, or injure yourself or others, I take no responsibility.

Introduction

I've been working on a side project to solve the "last mile" problem for people wanting to print from the network on their bots. I feel like the first half of the problem is solved with the FlashAir- getting the files to the card. The next step is a lightweight way of sending the "play back capture" command to the bot.

I looked around for a microcontroller platform that supports both networking and can function as a USB host. I happened to have an mbed (mbed) on hand that fit the bill. The mbed also has a working online toolchain (you need to own an mbed to gain access to the compiler). Some people don't like the online development environment, but I'm a fan of "working" and "Mac compatible." It was a good start, but cost wise, you would need an mbed LPC1768 module and some sort of carrier board that has both USB host and ethernet, or rig up your own connector solution. I happened to also have a Seedstudio mbed shield carrier board. This provides ethernet and USB connectors, but is another $25, putting the solution at around $75.

I also had an LPC1768 development board here called the "Mini-DK2". It has a USB host and a wired ethernet connector on board (search ebay if you're interested). It's a single-board solution that costs only $32 (and for $40 you can get one with a touchscreen) Its the cheapest development board I've seen with both USB host and an ethernet connector. I considered RasPi, but I'm not on that bandwagon. Since I had the Mini-DK2 on hand from another project that never went anywhere, I moved from the mbed module and carrier board to the DK2.

The mbed environment can compile binaries that work on the DK2 (again, you need to own at least one 1768 mbed already to get a license to use the compiler), and the mbed libraries provide some nice features. A USB Host library and and Ethernet library were readily available. The USBHost library didn't quite work out of the box. It took some time and more learning about the USB protocols than I would have liked, but I have the board communicating over the USB Host and the Makerbot.

Changes to stock mbed libraries

Many libraries are imported, but then converted to folders as to unlink them.

mbed provides a USHost library that includes a USBHostSerial object for connecting to CDC serial devices. Unfortunately, it did not work for me out of the box. I spent some time learning about USB protocols. One good reference is [Jan Axelson's Lakeview Research](http://www.lvr.com/usb_virtual_com_port.htm) discussion about CDC.

I found that the stock library was sending the control transfers to Interface 1. From what I understand, the control transfers needed to go to interface 0. I modified the USBHostSerial library to correct this, and the serial port interface came to life.

Next, I found that I wasn't able to get reliable communication. I traced it to what I think is an odd C++ inheritance and override problem. The USBHostSerial class implements the Stream interface, allowing printf/scanf operations. This is done by overriding the virtual _getc and _putc methods. Unfortunately, and for a reason I can't understand, these methods were not being called consistently. Sometimes they would work, but other times they would not. My solution was to implement transmit/receive methods with different names, and since the names were different, they seemed to get called consistently. I'd like to learn exactly what's going on here, but I don't feel like debugging it for academic purposes when it works just fine with the added methods.

Usage

Connect up your chosen dev board to power, ethernet and the USB host to the Makerbot's USB cable. The Mini-DK uses a USB-OTG adapter for the USB host. If you're using a Mini-DK board with an LCD, it will inform you of it's IP address on the display. This means it is now listening for a connection on port 7654.

If you are using an mbed dev board, or a Mini-DK without a display, the message will be directed to the serial console. Connect your computer to the appropriate port at a baud rate of 115200 to see the messages.

Use a telnet client to connect to the given IP address at port 7654. Telnet clients typically revert to "line mode" on ports other than 21. This means you get a local echo and the command isn't sent until you press enter.

Once connected, you can send the following commands:

A <username>:<password> : Set a username & password for the web interface and the telnet interface. Use the format shown with a colon separating the username from the password.

V : Print the version and Makerbot name, as well as the local firmware version (the Makerbot_Server firmware as discussed here).

B <filename.x3g> : Build from SD the given filename. According tot he protocol spec, this command is limited to 12 characters, so 8.3 filenames only.

P : Pause an active build

R : Resume active build

C : Cancel build- note that this immediately halts the build and does not clear the build area. You might want to pause the build first, and then cancel shortly after to make sure the nozzle isn't left hot and in contact with a printed part.

S : Print build status, tool and platform temps

Q : Quit and logout

The Mini-DK has two onboard buttons (besides the ISP and reset buttons). Currently one button will trigger a pause (if the Makerbot is printing) and the other will resume (if the Makerbot it paused)

Compiling

Edit "Target.h" to set whether you're building for an MBED module or the Mini-DK2

Installation

If you are using a mbed, then you can simply load the BIN file to the mbed using the mass storage bootloader. The mbed mounts as if it were a USB thumbdrive, and you copy the BIN file to the drive. After a reset, you're running the installed firmware.

The MiniDK has a serial bootloader. You connect to this bootloader from the "top" USB connector (not the USB host one). Hold down the ISP button and then tap the reset button and then release the ISP button to put it into programming mode. I use [lpc21isp](http://sourceforge.net/projects/lpc21isp/) to load the binary. The other option is FlashMagic, which uses HEX files, so you'll need to use some sort of bin2hex utility to convert the firmware file if you use this utility. I can't really say if/how this works, as I don't use this method. See this (http://mbed.org/users/frankvnk/notebook/lpc1768-mini-dk/) for more info.

Credits

Some credits, where credit is due.

EthernetInterface - modified to include PHY code for both the MiniDK2 and MBED based on selected #definitions

Mini-DK - Thanks for Frank and Erik for doing all the heavy lifting getting the MBED compiler and libraries and peripherals working on the Mini-DK2

NTP Client - Thanks to Donatien for this library to set the clock over the network

RTC - Thanks to Erik for the RTC library. I've got it in my project, but I don't think I'm using it for anything (yet).

SimpleSocket - Thanks to Yamaguchi-san. Modified slightly to take out references to EthernetInterface::init() and ::getIPAddress(). For some reason these don't like to be called in a thread.

JPEGCamera - Thanks again to Yamaguchi-san. Modified to output the JPEG binary over a socket rather than to a file descriptor.

USBHost - modified as noted above

IAP - Thanks to Okano-san. Pulled out of the Mini-DK folder so that I could link it back to the base repository at the root level.

Committer:
jakeb
Date:
Fri Aug 23 21:45:08 2013 +0000
Revision:
15:688b3e3958fd
Initial commit of software v0.2;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jakeb 15:688b3e3958fd 1 /* mbed USBHost Library
jakeb 15:688b3e3958fd 2 * Copyright (c) 2006-2013 ARM Limited
jakeb 15:688b3e3958fd 3 *
jakeb 15:688b3e3958fd 4 * Licensed under the Apache License, Version 2.0 (the "License");
jakeb 15:688b3e3958fd 5 * you may not use this file except in compliance with the License.
jakeb 15:688b3e3958fd 6 * You may obtain a copy of the License at
jakeb 15:688b3e3958fd 7 *
jakeb 15:688b3e3958fd 8 * http://www.apache.org/licenses/LICENSE-2.0
jakeb 15:688b3e3958fd 9 *
jakeb 15:688b3e3958fd 10 * Unless required by applicable law or agreed to in writing, software
jakeb 15:688b3e3958fd 11 * distributed under the License is distributed on an "AS IS" BASIS,
jakeb 15:688b3e3958fd 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jakeb 15:688b3e3958fd 13 * See the License for the specific language governing permissions and
jakeb 15:688b3e3958fd 14 * limitations under the License.
jakeb 15:688b3e3958fd 15 */
jakeb 15:688b3e3958fd 16
jakeb 15:688b3e3958fd 17
jakeb 15:688b3e3958fd 18 #include "USBHost.h"
jakeb 15:688b3e3958fd 19 #include "USBHostHub.h"
jakeb 15:688b3e3958fd 20
jakeb 15:688b3e3958fd 21 USBHost * USBHost::instHost = NULL;
jakeb 15:688b3e3958fd 22
jakeb 15:688b3e3958fd 23 #define DEVICE_CONNECTED_EVENT (1 << 0)
jakeb 15:688b3e3958fd 24 #define DEVICE_DISCONNECTED_EVENT (1 << 1)
jakeb 15:688b3e3958fd 25 #define TD_PROCESSED_EVENT (1 << 2)
jakeb 15:688b3e3958fd 26
jakeb 15:688b3e3958fd 27 #define MAX_TRY_ENUMERATE_HUB 3
jakeb 15:688b3e3958fd 28
jakeb 15:688b3e3958fd 29 #define MIN(a, b) ((a > b) ? b : a)
jakeb 15:688b3e3958fd 30
jakeb 15:688b3e3958fd 31 DigitalOut l4(LED4);
jakeb 15:688b3e3958fd 32
jakeb 15:688b3e3958fd 33 /**
jakeb 15:688b3e3958fd 34 * How interrupts are processed:
jakeb 15:688b3e3958fd 35 * - new device connected:
jakeb 15:688b3e3958fd 36 * - a message is queued in queue_usb_event with the id DEVICE_CONNECTED_EVENT
jakeb 15:688b3e3958fd 37 * - when the usb_thread receives the event, it:
jakeb 15:688b3e3958fd 38 * - resets the device
jakeb 15:688b3e3958fd 39 * - reads the device descriptor
jakeb 15:688b3e3958fd 40 * - sets the address of the device
jakeb 15:688b3e3958fd 41 * - if it is a hub, enumerates it
jakeb 15:688b3e3958fd 42 * - device disconnected:
jakeb 15:688b3e3958fd 43 * - a message is queued in queue_usb_event with the id DEVICE_DISCONNECTED_EVENT
jakeb 15:688b3e3958fd 44 * - when the usb_thread receives the event, it:
jakeb 15:688b3e3958fd 45 * - free the device and all its children (hub)
jakeb 15:688b3e3958fd 46 * - td processed
jakeb 15:688b3e3958fd 47 * - a message is queued in queue_usb_event with the id TD_PROCESSED_EVENT
jakeb 15:688b3e3958fd 48 * - when the usb_thread receives the event, it:
jakeb 15:688b3e3958fd 49 * - call the callback attached to the endpoint where the td is attached
jakeb 15:688b3e3958fd 50 */
jakeb 15:688b3e3958fd 51 void USBHost::usb_process() {
jakeb 15:688b3e3958fd 52
jakeb 15:688b3e3958fd 53 bool controlListState;
jakeb 15:688b3e3958fd 54 bool bulkListState;
jakeb 15:688b3e3958fd 55 bool interruptListState;
jakeb 15:688b3e3958fd 56 USBEndpoint * ep;
jakeb 15:688b3e3958fd 57 uint8_t i, j, res, timeout_set_addr = 10;
jakeb 15:688b3e3958fd 58 uint8_t buf[8];
jakeb 15:688b3e3958fd 59 bool too_many_hub;
jakeb 15:688b3e3958fd 60 int idx;
jakeb 15:688b3e3958fd 61
jakeb 15:688b3e3958fd 62 #if DEBUG_TRANSFER
jakeb 15:688b3e3958fd 63 uint8_t * buf_transfer;
jakeb 15:688b3e3958fd 64 #endif
jakeb 15:688b3e3958fd 65
jakeb 15:688b3e3958fd 66 #if MAX_HUB_NB
jakeb 15:688b3e3958fd 67 uint8_t k;
jakeb 15:688b3e3958fd 68 #endif
jakeb 15:688b3e3958fd 69
jakeb 15:688b3e3958fd 70 while(1) {
jakeb 15:688b3e3958fd 71 osEvent evt = mail_usb_event.get();
jakeb 15:688b3e3958fd 72
jakeb 15:688b3e3958fd 73 if (evt.status == osEventMail) {
jakeb 15:688b3e3958fd 74
jakeb 15:688b3e3958fd 75 l4 = !l4;
jakeb 15:688b3e3958fd 76 message_t * usb_msg = (message_t*)evt.value.p;
jakeb 15:688b3e3958fd 77
jakeb 15:688b3e3958fd 78 switch (usb_msg->event_id) {
jakeb 15:688b3e3958fd 79
jakeb 15:688b3e3958fd 80 // a new device has been connected
jakeb 15:688b3e3958fd 81 case DEVICE_CONNECTED_EVENT:
jakeb 15:688b3e3958fd 82 too_many_hub = false;
jakeb 15:688b3e3958fd 83 buf[4] = 0;
jakeb 15:688b3e3958fd 84
jakeb 15:688b3e3958fd 85 usb_mutex.lock();
jakeb 15:688b3e3958fd 86
jakeb 15:688b3e3958fd 87 for (i = 0; i < MAX_DEVICE_CONNECTED; i++) {
jakeb 15:688b3e3958fd 88 if (!deviceInUse[i]) {
jakeb 15:688b3e3958fd 89 USB_DBG_EVENT("new device connected: %p\r\n", &devices[i]);
jakeb 15:688b3e3958fd 90 devices[i].init(usb_msg->hub, usb_msg->port, usb_msg->lowSpeed);
jakeb 15:688b3e3958fd 91 deviceReset[i] = false;
jakeb 15:688b3e3958fd 92 deviceInited[i] = true;
jakeb 15:688b3e3958fd 93 break;
jakeb 15:688b3e3958fd 94 }
jakeb 15:688b3e3958fd 95 }
jakeb 15:688b3e3958fd 96
jakeb 15:688b3e3958fd 97 if (i == MAX_DEVICE_CONNECTED) {
jakeb 15:688b3e3958fd 98 USB_ERR("Too many device connected!!\r\n");
jakeb 15:688b3e3958fd 99 deviceInited[i] = false;
jakeb 15:688b3e3958fd 100 usb_mutex.unlock();
jakeb 15:688b3e3958fd 101 continue;
jakeb 15:688b3e3958fd 102 }
jakeb 15:688b3e3958fd 103
jakeb 15:688b3e3958fd 104 if (!controlEndpointAllocated) {
jakeb 15:688b3e3958fd 105 control = newEndpoint(CONTROL_ENDPOINT, OUT, 0x08, 0x00);
jakeb 15:688b3e3958fd 106 addEndpoint(NULL, 0, (USBEndpoint*)control);
jakeb 15:688b3e3958fd 107 controlEndpointAllocated = true;
jakeb 15:688b3e3958fd 108 }
jakeb 15:688b3e3958fd 109
jakeb 15:688b3e3958fd 110 #if MAX_HUB_NB
jakeb 15:688b3e3958fd 111 if (usb_msg->hub_parent)
jakeb 15:688b3e3958fd 112 devices[i].setHubParent((USBHostHub *)(usb_msg->hub_parent));
jakeb 15:688b3e3958fd 113 #endif
jakeb 15:688b3e3958fd 114
jakeb 15:688b3e3958fd 115 for (j = 0; j < timeout_set_addr; j++) {
jakeb 15:688b3e3958fd 116
jakeb 15:688b3e3958fd 117 resetDevice(&devices[i]);
jakeb 15:688b3e3958fd 118
jakeb 15:688b3e3958fd 119 // set size of control endpoint
jakeb 15:688b3e3958fd 120 devices[i].setSizeControlEndpoint(8);
jakeb 15:688b3e3958fd 121
jakeb 15:688b3e3958fd 122 devices[i].activeAddress(false);
jakeb 15:688b3e3958fd 123
jakeb 15:688b3e3958fd 124 // get first 8 bit of device descriptor
jakeb 15:688b3e3958fd 125 // and check if we deal with a hub
jakeb 15:688b3e3958fd 126 USB_DBG("usb_thread read device descriptor on dev: %p\r\n", &devices[i]);
jakeb 15:688b3e3958fd 127 res = getDeviceDescriptor(&devices[i], buf, 8);
jakeb 15:688b3e3958fd 128
jakeb 15:688b3e3958fd 129 if (res != USB_TYPE_OK) {
jakeb 15:688b3e3958fd 130 USB_ERR("usb_thread could not read dev descr");
jakeb 15:688b3e3958fd 131 continue;
jakeb 15:688b3e3958fd 132 }
jakeb 15:688b3e3958fd 133
jakeb 15:688b3e3958fd 134 // set size of control endpoint
jakeb 15:688b3e3958fd 135 devices[i].setSizeControlEndpoint(buf[7]);
jakeb 15:688b3e3958fd 136
jakeb 15:688b3e3958fd 137 // second step: set an address to the device
jakeb 15:688b3e3958fd 138 res = setAddress(&devices[i], devices[i].getAddress());
jakeb 15:688b3e3958fd 139
jakeb 15:688b3e3958fd 140 if (res != USB_TYPE_OK) {
jakeb 15:688b3e3958fd 141 USB_ERR("SET ADDR FAILED");
jakeb 15:688b3e3958fd 142 continue;
jakeb 15:688b3e3958fd 143 }
jakeb 15:688b3e3958fd 144 devices[i].activeAddress(true);
jakeb 15:688b3e3958fd 145 USB_DBG("Address of %p: %d", &devices[i], devices[i].getAddress());
jakeb 15:688b3e3958fd 146
jakeb 15:688b3e3958fd 147 // try to read again the device descriptor to check if the device
jakeb 15:688b3e3958fd 148 // answers to its new address
jakeb 15:688b3e3958fd 149 res = getDeviceDescriptor(&devices[i], buf, 8);
jakeb 15:688b3e3958fd 150
jakeb 15:688b3e3958fd 151 if (res == USB_TYPE_OK) {
jakeb 15:688b3e3958fd 152 break;
jakeb 15:688b3e3958fd 153 }
jakeb 15:688b3e3958fd 154
jakeb 15:688b3e3958fd 155 Thread::wait(100);
jakeb 15:688b3e3958fd 156 }
jakeb 15:688b3e3958fd 157
jakeb 15:688b3e3958fd 158 USB_INFO("New device connected: %p [hub: %d - port: %d]", &devices[i], usb_msg->hub, usb_msg->port);
jakeb 15:688b3e3958fd 159
jakeb 15:688b3e3958fd 160 #if MAX_HUB_NB
jakeb 15:688b3e3958fd 161 if (buf[4] == HUB_CLASS) {
jakeb 15:688b3e3958fd 162 for (k = 0; k < MAX_HUB_NB; k++) {
jakeb 15:688b3e3958fd 163 if (hub_in_use[k] == false) {
jakeb 15:688b3e3958fd 164 for (uint8_t j = 0; j < MAX_TRY_ENUMERATE_HUB; j++) {
jakeb 15:688b3e3958fd 165 if (hubs[k].connect(&devices[i])) {
jakeb 15:688b3e3958fd 166 devices[i].hub = &hubs[k];
jakeb 15:688b3e3958fd 167 hub_in_use[k] = true;
jakeb 15:688b3e3958fd 168 break;
jakeb 15:688b3e3958fd 169 }
jakeb 15:688b3e3958fd 170 }
jakeb 15:688b3e3958fd 171 if (hub_in_use[k] == true)
jakeb 15:688b3e3958fd 172 break;
jakeb 15:688b3e3958fd 173 }
jakeb 15:688b3e3958fd 174 }
jakeb 15:688b3e3958fd 175
jakeb 15:688b3e3958fd 176 if (k == MAX_HUB_NB) {
jakeb 15:688b3e3958fd 177 USB_ERR("Too many hubs connected!!\r\n");
jakeb 15:688b3e3958fd 178 too_many_hub = true;
jakeb 15:688b3e3958fd 179 }
jakeb 15:688b3e3958fd 180 }
jakeb 15:688b3e3958fd 181
jakeb 15:688b3e3958fd 182 if (usb_msg->hub_parent)
jakeb 15:688b3e3958fd 183 ((USBHostHub *)(usb_msg->hub_parent))->deviceConnected(&devices[i]);
jakeb 15:688b3e3958fd 184 #endif
jakeb 15:688b3e3958fd 185
jakeb 15:688b3e3958fd 186 if ((i < MAX_DEVICE_CONNECTED) && !too_many_hub) {
jakeb 15:688b3e3958fd 187 deviceInUse[i] = true;
jakeb 15:688b3e3958fd 188 }
jakeb 15:688b3e3958fd 189
jakeb 15:688b3e3958fd 190 usb_mutex.unlock();
jakeb 15:688b3e3958fd 191
jakeb 15:688b3e3958fd 192 break;
jakeb 15:688b3e3958fd 193
jakeb 15:688b3e3958fd 194 // a device has been disconnected
jakeb 15:688b3e3958fd 195 case DEVICE_DISCONNECTED_EVENT:
jakeb 15:688b3e3958fd 196
jakeb 15:688b3e3958fd 197 usb_mutex.lock();
jakeb 15:688b3e3958fd 198
jakeb 15:688b3e3958fd 199 controlListState = disableList(CONTROL_ENDPOINT);
jakeb 15:688b3e3958fd 200 bulkListState = disableList(BULK_ENDPOINT);
jakeb 15:688b3e3958fd 201 interruptListState = disableList(INTERRUPT_ENDPOINT);
jakeb 15:688b3e3958fd 202
jakeb 15:688b3e3958fd 203 idx = findDevice(usb_msg->hub, usb_msg->port, (USBHostHub *)(usb_msg->hub_parent));
jakeb 15:688b3e3958fd 204 if (idx != -1) {
jakeb 15:688b3e3958fd 205 freeDevice((USBDeviceConnected*)&devices[idx]);
jakeb 15:688b3e3958fd 206 }
jakeb 15:688b3e3958fd 207
jakeb 15:688b3e3958fd 208 if (controlListState) enableList(CONTROL_ENDPOINT);
jakeb 15:688b3e3958fd 209 if (bulkListState) enableList(BULK_ENDPOINT);
jakeb 15:688b3e3958fd 210 if (interruptListState) enableList(INTERRUPT_ENDPOINT);
jakeb 15:688b3e3958fd 211
jakeb 15:688b3e3958fd 212 usb_mutex.unlock();
jakeb 15:688b3e3958fd 213
jakeb 15:688b3e3958fd 214 break;
jakeb 15:688b3e3958fd 215
jakeb 15:688b3e3958fd 216 // a td has been processed
jakeb 15:688b3e3958fd 217 // call callback on the ed associated to the td
jakeb 15:688b3e3958fd 218 // we are not in ISR -> users can use printf in their callback method
jakeb 15:688b3e3958fd 219 case TD_PROCESSED_EVENT:
jakeb 15:688b3e3958fd 220 ep = (USBEndpoint *) ((HCTD *)usb_msg->td_addr)->ep;
jakeb 15:688b3e3958fd 221 if (usb_msg->td_state == USB_TYPE_IDLE) {
jakeb 15:688b3e3958fd 222 USB_DBG_EVENT("call callback on td %p [ep: %p state: %s - dev: %p - %s]", usb_msg->td_addr, ep, ep->getStateString(), ep->dev, ep->dev->getName(ep->getIntfNb()));
jakeb 15:688b3e3958fd 223
jakeb 15:688b3e3958fd 224 #if DEBUG_TRANSFER
jakeb 15:688b3e3958fd 225 if (ep->getDir() == IN) {
jakeb 15:688b3e3958fd 226 buf_transfer = ep->getBufStart();
jakeb 15:688b3e3958fd 227 printf("READ SUCCESS [%d bytes transferred - td: 0x%08X] on ep: [%p - addr: %02X]: ", ep->getLengthTransferred(), usb_msg->td_addr, ep, ep->getAddress());
jakeb 15:688b3e3958fd 228 for (int i = 0; i < ep->getLengthTransferred(); i++)
jakeb 15:688b3e3958fd 229 printf("%02X ", buf_transfer[i]);
jakeb 15:688b3e3958fd 230 printf("\r\n\r\n");
jakeb 15:688b3e3958fd 231 }
jakeb 15:688b3e3958fd 232 #endif
jakeb 15:688b3e3958fd 233 ep->call();
jakeb 15:688b3e3958fd 234 } else {
jakeb 15:688b3e3958fd 235 idx = findDevice(ep->dev);
jakeb 15:688b3e3958fd 236 if (idx != -1) {
jakeb 15:688b3e3958fd 237 if (deviceInUse[idx]) {
jakeb 15:688b3e3958fd 238 USB_WARN("td %p processed but not in idle state: %s [ep: %p - dev: %p - %s]", usb_msg->td_addr, ep->getStateString(), ep, ep->dev, ep->dev->getName(ep->getIntfNb()));
jakeb 15:688b3e3958fd 239 ep->setState(USB_TYPE_IDLE);
jakeb 15:688b3e3958fd 240 }
jakeb 15:688b3e3958fd 241 }
jakeb 15:688b3e3958fd 242 }
jakeb 15:688b3e3958fd 243 break;
jakeb 15:688b3e3958fd 244 }
jakeb 15:688b3e3958fd 245
jakeb 15:688b3e3958fd 246 mail_usb_event.free(usb_msg);
jakeb 15:688b3e3958fd 247 }
jakeb 15:688b3e3958fd 248 }
jakeb 15:688b3e3958fd 249 }
jakeb 15:688b3e3958fd 250
jakeb 15:688b3e3958fd 251 /* static */void USBHost::usb_process_static(void const * arg) {
jakeb 15:688b3e3958fd 252 ((USBHost *)arg)->usb_process();
jakeb 15:688b3e3958fd 253 }
jakeb 15:688b3e3958fd 254
jakeb 15:688b3e3958fd 255 USBHost::USBHost() : usbThread(USBHost::usb_process_static, (void *)this, osPriorityNormal, USB_THREAD_STACK)
jakeb 15:688b3e3958fd 256 {
jakeb 15:688b3e3958fd 257 headControlEndpoint = NULL;
jakeb 15:688b3e3958fd 258 headBulkEndpoint = NULL;
jakeb 15:688b3e3958fd 259 headInterruptEndpoint = NULL;
jakeb 15:688b3e3958fd 260 tailControlEndpoint = NULL;
jakeb 15:688b3e3958fd 261 tailBulkEndpoint = NULL;
jakeb 15:688b3e3958fd 262 tailInterruptEndpoint = NULL;
jakeb 15:688b3e3958fd 263
jakeb 15:688b3e3958fd 264 lenReportDescr = 0;
jakeb 15:688b3e3958fd 265
jakeb 15:688b3e3958fd 266 controlEndpointAllocated = false;
jakeb 15:688b3e3958fd 267
jakeb 15:688b3e3958fd 268 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
jakeb 15:688b3e3958fd 269 deviceInUse[i] = false;
jakeb 15:688b3e3958fd 270 devices[i].setAddress(i + 1);
jakeb 15:688b3e3958fd 271 deviceReset[i] = false;
jakeb 15:688b3e3958fd 272 deviceInited[i] = false;
jakeb 15:688b3e3958fd 273 for (uint8_t j = 0; j < MAX_INTF; j++)
jakeb 15:688b3e3958fd 274 deviceAttachedDriver[i][j] = false;
jakeb 15:688b3e3958fd 275 }
jakeb 15:688b3e3958fd 276
jakeb 15:688b3e3958fd 277 #if MAX_HUB_NB
jakeb 15:688b3e3958fd 278 for (uint8_t i = 0; i < MAX_HUB_NB; i++) {
jakeb 15:688b3e3958fd 279 hubs[i].setHost(this);
jakeb 15:688b3e3958fd 280 hub_in_use[i] = false;
jakeb 15:688b3e3958fd 281 }
jakeb 15:688b3e3958fd 282 #endif
jakeb 15:688b3e3958fd 283 }
jakeb 15:688b3e3958fd 284
jakeb 15:688b3e3958fd 285
jakeb 15:688b3e3958fd 286 void USBHost::transferCompleted(volatile uint32_t addr)
jakeb 15:688b3e3958fd 287 {
jakeb 15:688b3e3958fd 288 uint8_t state;
jakeb 15:688b3e3958fd 289
jakeb 15:688b3e3958fd 290 if(addr == NULL)
jakeb 15:688b3e3958fd 291 return;
jakeb 15:688b3e3958fd 292
jakeb 15:688b3e3958fd 293 volatile HCTD* tdList = NULL;
jakeb 15:688b3e3958fd 294
jakeb 15:688b3e3958fd 295 //First we must reverse the list order and dequeue each TD
jakeb 15:688b3e3958fd 296 do {
jakeb 15:688b3e3958fd 297 volatile HCTD* td = (volatile HCTD*)addr;
jakeb 15:688b3e3958fd 298 addr = (uint32_t)td->nextTD; //Dequeue from physical list
jakeb 15:688b3e3958fd 299 td->nextTD = tdList; //Enqueue into reversed list
jakeb 15:688b3e3958fd 300 tdList = td;
jakeb 15:688b3e3958fd 301 } while(addr);
jakeb 15:688b3e3958fd 302
jakeb 15:688b3e3958fd 303 while(tdList != NULL) {
jakeb 15:688b3e3958fd 304 volatile HCTD* td = tdList;
jakeb 15:688b3e3958fd 305 tdList = (volatile HCTD*)td->nextTD; //Dequeue element now as it could be modified below
jakeb 15:688b3e3958fd 306 if (td->ep != NULL) {
jakeb 15:688b3e3958fd 307 USBEndpoint * ep = (USBEndpoint *)(td->ep);
jakeb 15:688b3e3958fd 308
jakeb 15:688b3e3958fd 309 if (((HCTD *)td)->control >> 28) {
jakeb 15:688b3e3958fd 310 state = ((HCTD *)td)->control >> 28;
jakeb 15:688b3e3958fd 311 } else {
jakeb 15:688b3e3958fd 312 if (td->currBufPtr)
jakeb 15:688b3e3958fd 313 ep->setLengthTransferred((uint32_t)td->currBufPtr - (uint32_t)ep->getBufStart());
jakeb 15:688b3e3958fd 314 state = 16 /*USB_TYPE_IDLE*/;
jakeb 15:688b3e3958fd 315 }
jakeb 15:688b3e3958fd 316
jakeb 15:688b3e3958fd 317 ep->unqueueTransfer(td);
jakeb 15:688b3e3958fd 318
jakeb 15:688b3e3958fd 319 if (ep->getType() != CONTROL_ENDPOINT) {
jakeb 15:688b3e3958fd 320 // callback on the processed td will be called from the usb_thread (not in ISR)
jakeb 15:688b3e3958fd 321 message_t * usb_msg = mail_usb_event.alloc();
jakeb 15:688b3e3958fd 322 usb_msg->event_id = TD_PROCESSED_EVENT;
jakeb 15:688b3e3958fd 323 usb_msg->td_addr = (void *)td;
jakeb 15:688b3e3958fd 324 usb_msg->td_state = state;
jakeb 15:688b3e3958fd 325 mail_usb_event.put(usb_msg);
jakeb 15:688b3e3958fd 326 }
jakeb 15:688b3e3958fd 327 ep->setState(state);
jakeb 15:688b3e3958fd 328 ep->ep_queue.put((uint8_t*)1);
jakeb 15:688b3e3958fd 329 }
jakeb 15:688b3e3958fd 330 }
jakeb 15:688b3e3958fd 331 }
jakeb 15:688b3e3958fd 332
jakeb 15:688b3e3958fd 333 USBHost * USBHost::getHostInst()
jakeb 15:688b3e3958fd 334 {
jakeb 15:688b3e3958fd 335 if (instHost == NULL) {
jakeb 15:688b3e3958fd 336 instHost = new USBHost();
jakeb 15:688b3e3958fd 337 instHost->init();
jakeb 15:688b3e3958fd 338 }
jakeb 15:688b3e3958fd 339 return instHost;
jakeb 15:688b3e3958fd 340 }
jakeb 15:688b3e3958fd 341
jakeb 15:688b3e3958fd 342
jakeb 15:688b3e3958fd 343 /*
jakeb 15:688b3e3958fd 344 * Called when a device has been connected
jakeb 15:688b3e3958fd 345 * Called in ISR!!!! (no printf)
jakeb 15:688b3e3958fd 346 */
jakeb 15:688b3e3958fd 347 /* virtual */ void USBHost::deviceConnected(int hub, int port, bool lowSpeed, USBHostHub * hub_parent)
jakeb 15:688b3e3958fd 348 {
jakeb 15:688b3e3958fd 349 // be sure that the new device connected is not already connected...
jakeb 15:688b3e3958fd 350 int idx = findDevice(hub, port, hub_parent);
jakeb 15:688b3e3958fd 351 if (idx != -1) {
jakeb 15:688b3e3958fd 352 if (deviceInited[idx])
jakeb 15:688b3e3958fd 353 return;
jakeb 15:688b3e3958fd 354 }
jakeb 15:688b3e3958fd 355
jakeb 15:688b3e3958fd 356 message_t * usb_msg = mail_usb_event.alloc();
jakeb 15:688b3e3958fd 357 usb_msg->event_id = DEVICE_CONNECTED_EVENT;
jakeb 15:688b3e3958fd 358 usb_msg->hub = hub;
jakeb 15:688b3e3958fd 359 usb_msg->port = port;
jakeb 15:688b3e3958fd 360 usb_msg->lowSpeed = lowSpeed;
jakeb 15:688b3e3958fd 361 usb_msg->hub_parent = hub_parent;
jakeb 15:688b3e3958fd 362 mail_usb_event.put(usb_msg);
jakeb 15:688b3e3958fd 363 }
jakeb 15:688b3e3958fd 364
jakeb 15:688b3e3958fd 365 /*
jakeb 15:688b3e3958fd 366 * Called when a device has been disconnected
jakeb 15:688b3e3958fd 367 * Called in ISR!!!! (no printf)
jakeb 15:688b3e3958fd 368 */
jakeb 15:688b3e3958fd 369 /* virtual */ void USBHost::deviceDisconnected(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr)
jakeb 15:688b3e3958fd 370 {
jakeb 15:688b3e3958fd 371 // be sure that the device disconnected is connected...
jakeb 15:688b3e3958fd 372 int idx = findDevice(hub, port, hub_parent);
jakeb 15:688b3e3958fd 373 if (idx != -1) {
jakeb 15:688b3e3958fd 374 if (!deviceInUse[idx])
jakeb 15:688b3e3958fd 375 return;
jakeb 15:688b3e3958fd 376 } else {
jakeb 15:688b3e3958fd 377 return;
jakeb 15:688b3e3958fd 378 }
jakeb 15:688b3e3958fd 379
jakeb 15:688b3e3958fd 380 message_t * usb_msg = mail_usb_event.alloc();
jakeb 15:688b3e3958fd 381 usb_msg->event_id = DEVICE_DISCONNECTED_EVENT;
jakeb 15:688b3e3958fd 382 usb_msg->hub = hub;
jakeb 15:688b3e3958fd 383 usb_msg->port = port;
jakeb 15:688b3e3958fd 384 usb_msg->hub_parent = hub_parent;
jakeb 15:688b3e3958fd 385 mail_usb_event.put(usb_msg);
jakeb 15:688b3e3958fd 386 }
jakeb 15:688b3e3958fd 387
jakeb 15:688b3e3958fd 388 void USBHost::freeDevice(USBDeviceConnected * dev)
jakeb 15:688b3e3958fd 389 {
jakeb 15:688b3e3958fd 390 USBEndpoint * ep = NULL;
jakeb 15:688b3e3958fd 391 HCED * ed = NULL;
jakeb 15:688b3e3958fd 392
jakeb 15:688b3e3958fd 393 #if MAX_HUB_NB
jakeb 15:688b3e3958fd 394 if (dev->getClass() == HUB_CLASS) {
jakeb 15:688b3e3958fd 395 if (dev->hub == NULL) {
jakeb 15:688b3e3958fd 396 USB_ERR("HUB NULL!!!!!\r\n");
jakeb 15:688b3e3958fd 397 } else {
jakeb 15:688b3e3958fd 398 dev->hub->hubDisconnected();
jakeb 15:688b3e3958fd 399 for (uint8_t i = 0; i < MAX_HUB_NB; i++) {
jakeb 15:688b3e3958fd 400 if (dev->hub == &hubs[i]) {
jakeb 15:688b3e3958fd 401 hub_in_use[i] = false;
jakeb 15:688b3e3958fd 402 break;
jakeb 15:688b3e3958fd 403 }
jakeb 15:688b3e3958fd 404 }
jakeb 15:688b3e3958fd 405 }
jakeb 15:688b3e3958fd 406 }
jakeb 15:688b3e3958fd 407
jakeb 15:688b3e3958fd 408 // notify hub parent that this device has been disconnected
jakeb 15:688b3e3958fd 409 if (dev->getHubParent())
jakeb 15:688b3e3958fd 410 dev->getHubParent()->deviceDisconnected(dev);
jakeb 15:688b3e3958fd 411
jakeb 15:688b3e3958fd 412 #endif
jakeb 15:688b3e3958fd 413
jakeb 15:688b3e3958fd 414 int idx = findDevice(dev);
jakeb 15:688b3e3958fd 415 if (idx != -1) {
jakeb 15:688b3e3958fd 416 deviceInUse[idx] = false;
jakeb 15:688b3e3958fd 417 deviceReset[idx] = false;
jakeb 15:688b3e3958fd 418
jakeb 15:688b3e3958fd 419 for (uint8_t j = 0; j < MAX_INTF; j++) {
jakeb 15:688b3e3958fd 420 deviceAttachedDriver[idx][j] = false;
jakeb 15:688b3e3958fd 421 if (dev->getInterface(j) != NULL) {
jakeb 15:688b3e3958fd 422 USB_DBG("FREE INTF %d on dev: %p, %p, nb_endpot: %d, %s", j, (void *)dev->getInterface(j), dev, dev->getInterface(j)->nb_endpoint, dev->getName(j));
jakeb 15:688b3e3958fd 423 for (int i = 0; i < dev->getInterface(j)->nb_endpoint; i++) {
jakeb 15:688b3e3958fd 424 if ((ep = dev->getEndpoint(j, i)) != NULL) {
jakeb 15:688b3e3958fd 425 ed = (HCED *)ep->getHCED();
jakeb 15:688b3e3958fd 426 ed->control |= (1 << 14); //sKip bit
jakeb 15:688b3e3958fd 427 unqueueEndpoint(ep);
jakeb 15:688b3e3958fd 428
jakeb 15:688b3e3958fd 429 freeTD((volatile uint8_t*)ep->getTDList()[0]);
jakeb 15:688b3e3958fd 430 freeTD((volatile uint8_t*)ep->getTDList()[1]);
jakeb 15:688b3e3958fd 431
jakeb 15:688b3e3958fd 432 freeED((uint8_t *)ep->getHCED());
jakeb 15:688b3e3958fd 433 }
jakeb 15:688b3e3958fd 434 printList(BULK_ENDPOINT);
jakeb 15:688b3e3958fd 435 printList(INTERRUPT_ENDPOINT);
jakeb 15:688b3e3958fd 436 }
jakeb 15:688b3e3958fd 437 USB_INFO("Device disconnected [%p - %s - hub: %d - port: %d]", dev, dev->getName(j), dev->getHub(), dev->getPort());
jakeb 15:688b3e3958fd 438 }
jakeb 15:688b3e3958fd 439 }
jakeb 15:688b3e3958fd 440 dev->disconnect();
jakeb 15:688b3e3958fd 441 }
jakeb 15:688b3e3958fd 442 }
jakeb 15:688b3e3958fd 443
jakeb 15:688b3e3958fd 444
jakeb 15:688b3e3958fd 445 void USBHost::unqueueEndpoint(USBEndpoint * ep)
jakeb 15:688b3e3958fd 446 {
jakeb 15:688b3e3958fd 447 USBEndpoint * prec = NULL;
jakeb 15:688b3e3958fd 448 USBEndpoint * current = NULL;
jakeb 15:688b3e3958fd 449
jakeb 15:688b3e3958fd 450 for (int i = 0; i < 2; i++) {
jakeb 15:688b3e3958fd 451 current = (i == 0) ? (USBEndpoint*)headBulkEndpoint : (USBEndpoint*)headInterruptEndpoint;
jakeb 15:688b3e3958fd 452 prec = current;
jakeb 15:688b3e3958fd 453 while (current != NULL) {
jakeb 15:688b3e3958fd 454 if (current == ep) {
jakeb 15:688b3e3958fd 455 if (current->nextEndpoint() != NULL) {
jakeb 15:688b3e3958fd 456 prec->queueEndpoint(current->nextEndpoint());
jakeb 15:688b3e3958fd 457 if (current == headBulkEndpoint) {
jakeb 15:688b3e3958fd 458 updateBulkHeadED((uint32_t)current->nextEndpoint()->getHCED());
jakeb 15:688b3e3958fd 459 headBulkEndpoint = current->nextEndpoint();
jakeb 15:688b3e3958fd 460 } else if (current == headInterruptEndpoint) {
jakeb 15:688b3e3958fd 461 updateInterruptHeadED((uint32_t)current->nextEndpoint()->getHCED());
jakeb 15:688b3e3958fd 462 headInterruptEndpoint = current->nextEndpoint();
jakeb 15:688b3e3958fd 463 }
jakeb 15:688b3e3958fd 464 }
jakeb 15:688b3e3958fd 465 // here we are dequeuing the queue of ed
jakeb 15:688b3e3958fd 466 // we need to update the tail pointer
jakeb 15:688b3e3958fd 467 else {
jakeb 15:688b3e3958fd 468 prec->queueEndpoint(NULL);
jakeb 15:688b3e3958fd 469 if (current == headBulkEndpoint) {
jakeb 15:688b3e3958fd 470 updateBulkHeadED(0);
jakeb 15:688b3e3958fd 471 headBulkEndpoint = current->nextEndpoint();
jakeb 15:688b3e3958fd 472 } else if (current == headInterruptEndpoint) {
jakeb 15:688b3e3958fd 473 updateInterruptHeadED(0);
jakeb 15:688b3e3958fd 474 headInterruptEndpoint = current->nextEndpoint();
jakeb 15:688b3e3958fd 475 }
jakeb 15:688b3e3958fd 476
jakeb 15:688b3e3958fd 477 // modify tail
jakeb 15:688b3e3958fd 478 switch (current->getType()) {
jakeb 15:688b3e3958fd 479 case BULK_ENDPOINT:
jakeb 15:688b3e3958fd 480 tailBulkEndpoint = prec;
jakeb 15:688b3e3958fd 481 break;
jakeb 15:688b3e3958fd 482 case INTERRUPT_ENDPOINT:
jakeb 15:688b3e3958fd 483 tailInterruptEndpoint = prec;
jakeb 15:688b3e3958fd 484 break;
jakeb 15:688b3e3958fd 485 }
jakeb 15:688b3e3958fd 486 }
jakeb 15:688b3e3958fd 487 current->setState(USB_TYPE_FREE);
jakeb 15:688b3e3958fd 488 return;
jakeb 15:688b3e3958fd 489 }
jakeb 15:688b3e3958fd 490 prec = current;
jakeb 15:688b3e3958fd 491 current = current->nextEndpoint();
jakeb 15:688b3e3958fd 492 }
jakeb 15:688b3e3958fd 493 }
jakeb 15:688b3e3958fd 494 }
jakeb 15:688b3e3958fd 495
jakeb 15:688b3e3958fd 496
jakeb 15:688b3e3958fd 497 USBDeviceConnected * USBHost::getDevice(uint8_t index)
jakeb 15:688b3e3958fd 498 {
jakeb 15:688b3e3958fd 499 if ((index >= MAX_DEVICE_CONNECTED) || (!deviceInUse[index])) {
jakeb 15:688b3e3958fd 500 return NULL;
jakeb 15:688b3e3958fd 501 }
jakeb 15:688b3e3958fd 502 return (USBDeviceConnected*)&devices[index];
jakeb 15:688b3e3958fd 503 }
jakeb 15:688b3e3958fd 504
jakeb 15:688b3e3958fd 505 // create an USBEndpoint descriptor. the USBEndpoint is not linked
jakeb 15:688b3e3958fd 506 USBEndpoint * USBHost::newEndpoint(ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint32_t size, uint8_t addr)
jakeb 15:688b3e3958fd 507 {
jakeb 15:688b3e3958fd 508 int i = 0;
jakeb 15:688b3e3958fd 509 HCED * ed = (HCED *)getED();
jakeb 15:688b3e3958fd 510 HCTD* td_list[2] = { (HCTD*)getTD(), (HCTD*)getTD() };
jakeb 15:688b3e3958fd 511
jakeb 15:688b3e3958fd 512 memset((void *)td_list[0], 0x00, sizeof(HCTD));
jakeb 15:688b3e3958fd 513 memset((void *)td_list[1], 0x00, sizeof(HCTD));
jakeb 15:688b3e3958fd 514
jakeb 15:688b3e3958fd 515 // search a free USBEndpoint
jakeb 15:688b3e3958fd 516 for (i = 0; i < MAX_ENDPOINT; i++) {
jakeb 15:688b3e3958fd 517 if (endpoints[i].getState() == USB_TYPE_FREE) {
jakeb 15:688b3e3958fd 518 endpoints[i].init(ed, type, dir, size, addr, td_list);
jakeb 15:688b3e3958fd 519 USB_DBG("USBEndpoint created (%p): type: %d, dir: %d, size: %d, addr: %d, state: %s", &endpoints[i], type, dir, size, addr, endpoints[i].getStateString());
jakeb 15:688b3e3958fd 520 return &endpoints[i];
jakeb 15:688b3e3958fd 521 }
jakeb 15:688b3e3958fd 522 }
jakeb 15:688b3e3958fd 523 USB_ERR("could not allocate more endpoints!!!!");
jakeb 15:688b3e3958fd 524 return NULL;
jakeb 15:688b3e3958fd 525 }
jakeb 15:688b3e3958fd 526
jakeb 15:688b3e3958fd 527
jakeb 15:688b3e3958fd 528 USB_TYPE USBHost::resetDevice(USBDeviceConnected * dev)
jakeb 15:688b3e3958fd 529 {
jakeb 15:688b3e3958fd 530 int index = findDevice(dev);
jakeb 15:688b3e3958fd 531 if (index != -1) {
jakeb 15:688b3e3958fd 532 USB_DBG("Resetting hub %d, port %d\n", dev->getHub(), dev->getPort());
jakeb 15:688b3e3958fd 533 Thread::wait(100);
jakeb 15:688b3e3958fd 534 if (dev->getHub() == 0) {
jakeb 15:688b3e3958fd 535 resetRootHub();
jakeb 15:688b3e3958fd 536 }
jakeb 15:688b3e3958fd 537 #if MAX_HUB_NB
jakeb 15:688b3e3958fd 538 else {
jakeb 15:688b3e3958fd 539 dev->getHubParent()->portReset(dev->getPort());
jakeb 15:688b3e3958fd 540 }
jakeb 15:688b3e3958fd 541 #endif
jakeb 15:688b3e3958fd 542 Thread::wait(100);
jakeb 15:688b3e3958fd 543 deviceReset[index] = true;
jakeb 15:688b3e3958fd 544 return USB_TYPE_OK;
jakeb 15:688b3e3958fd 545 }
jakeb 15:688b3e3958fd 546
jakeb 15:688b3e3958fd 547 return USB_TYPE_ERROR;
jakeb 15:688b3e3958fd 548 }
jakeb 15:688b3e3958fd 549
jakeb 15:688b3e3958fd 550 // link the USBEndpoint to the linked list and attach an USBEndpoint to a device
jakeb 15:688b3e3958fd 551 bool USBHost::addEndpoint(USBDeviceConnected * dev, uint8_t intf_nb, USBEndpoint * ep)
jakeb 15:688b3e3958fd 552 {
jakeb 15:688b3e3958fd 553
jakeb 15:688b3e3958fd 554 if (ep == NULL) {
jakeb 15:688b3e3958fd 555 return false;
jakeb 15:688b3e3958fd 556 }
jakeb 15:688b3e3958fd 557
jakeb 15:688b3e3958fd 558 HCED * prevEd;
jakeb 15:688b3e3958fd 559
jakeb 15:688b3e3958fd 560 // set device address in the USBEndpoint descriptor
jakeb 15:688b3e3958fd 561 if (dev == NULL) {
jakeb 15:688b3e3958fd 562 ep->setDeviceAddress(0);
jakeb 15:688b3e3958fd 563 } else {
jakeb 15:688b3e3958fd 564 ep->setDeviceAddress(dev->getAddress());
jakeb 15:688b3e3958fd 565 }
jakeb 15:688b3e3958fd 566
jakeb 15:688b3e3958fd 567 if ((dev != NULL) && dev->getSpeed()) {
jakeb 15:688b3e3958fd 568 ep->setSpeed(dev->getSpeed());
jakeb 15:688b3e3958fd 569 }
jakeb 15:688b3e3958fd 570
jakeb 15:688b3e3958fd 571 ep->setIntfNb(intf_nb);
jakeb 15:688b3e3958fd 572
jakeb 15:688b3e3958fd 573 // queue the new USBEndpoint on the ED list
jakeb 15:688b3e3958fd 574 switch (ep->getType()) {
jakeb 15:688b3e3958fd 575
jakeb 15:688b3e3958fd 576 case CONTROL_ENDPOINT:
jakeb 15:688b3e3958fd 577 prevEd = ( HCED*) controlHeadED();
jakeb 15:688b3e3958fd 578 if (!prevEd) {
jakeb 15:688b3e3958fd 579 updateControlHeadED((uint32_t) ep->getHCED());
jakeb 15:688b3e3958fd 580 USB_DBG_TRANSFER("First control USBEndpoint: %08X", (uint32_t) ep->getHCED());
jakeb 15:688b3e3958fd 581 headControlEndpoint = ep;
jakeb 15:688b3e3958fd 582 tailControlEndpoint = ep;
jakeb 15:688b3e3958fd 583 return true;
jakeb 15:688b3e3958fd 584 }
jakeb 15:688b3e3958fd 585 tailControlEndpoint->queueEndpoint(ep);
jakeb 15:688b3e3958fd 586 tailControlEndpoint = ep;
jakeb 15:688b3e3958fd 587 return true;
jakeb 15:688b3e3958fd 588
jakeb 15:688b3e3958fd 589 case BULK_ENDPOINT:
jakeb 15:688b3e3958fd 590 prevEd = ( HCED*) bulkHeadED();
jakeb 15:688b3e3958fd 591 if (!prevEd) {
jakeb 15:688b3e3958fd 592 updateBulkHeadED((uint32_t) ep->getHCED());
jakeb 15:688b3e3958fd 593 USB_DBG_TRANSFER("First bulk USBEndpoint: %08X\r\n", (uint32_t) ep->getHCED());
jakeb 15:688b3e3958fd 594 headBulkEndpoint = ep;
jakeb 15:688b3e3958fd 595 tailBulkEndpoint = ep;
jakeb 15:688b3e3958fd 596 break;
jakeb 15:688b3e3958fd 597 }
jakeb 15:688b3e3958fd 598 USB_DBG_TRANSFER("Queue BULK Ed %p after %p\r\n",ep->getHCED(), prevEd);
jakeb 15:688b3e3958fd 599 tailBulkEndpoint->queueEndpoint(ep);
jakeb 15:688b3e3958fd 600 tailBulkEndpoint = ep;
jakeb 15:688b3e3958fd 601 break;
jakeb 15:688b3e3958fd 602
jakeb 15:688b3e3958fd 603 case INTERRUPT_ENDPOINT:
jakeb 15:688b3e3958fd 604 prevEd = ( HCED*) interruptHeadED();
jakeb 15:688b3e3958fd 605 if (!prevEd) {
jakeb 15:688b3e3958fd 606 updateInterruptHeadED((uint32_t) ep->getHCED());
jakeb 15:688b3e3958fd 607 USB_DBG_TRANSFER("First interrupt USBEndpoint: %08X\r\n", (uint32_t) ep->getHCED());
jakeb 15:688b3e3958fd 608 headInterruptEndpoint = ep;
jakeb 15:688b3e3958fd 609 tailInterruptEndpoint = ep;
jakeb 15:688b3e3958fd 610 break;
jakeb 15:688b3e3958fd 611 }
jakeb 15:688b3e3958fd 612 USB_DBG_TRANSFER("Queue INTERRUPT Ed %p after %p\r\n",ep->getHCED(), prevEd);
jakeb 15:688b3e3958fd 613 tailInterruptEndpoint->queueEndpoint(ep);
jakeb 15:688b3e3958fd 614 tailInterruptEndpoint = ep;
jakeb 15:688b3e3958fd 615 break;
jakeb 15:688b3e3958fd 616 default:
jakeb 15:688b3e3958fd 617 return false;
jakeb 15:688b3e3958fd 618 }
jakeb 15:688b3e3958fd 619
jakeb 15:688b3e3958fd 620 ep->dev = dev;
jakeb 15:688b3e3958fd 621 dev->addEndpoint(intf_nb, ep);
jakeb 15:688b3e3958fd 622
jakeb 15:688b3e3958fd 623 return true;
jakeb 15:688b3e3958fd 624 }
jakeb 15:688b3e3958fd 625
jakeb 15:688b3e3958fd 626
jakeb 15:688b3e3958fd 627 int USBHost::findDevice(USBDeviceConnected * dev)
jakeb 15:688b3e3958fd 628 {
jakeb 15:688b3e3958fd 629 for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) {
jakeb 15:688b3e3958fd 630 if (dev == &devices[i]) {
jakeb 15:688b3e3958fd 631 return i;
jakeb 15:688b3e3958fd 632 }
jakeb 15:688b3e3958fd 633 }
jakeb 15:688b3e3958fd 634 return -1;
jakeb 15:688b3e3958fd 635 }
jakeb 15:688b3e3958fd 636
jakeb 15:688b3e3958fd 637 int USBHost::findDevice(uint8_t hub, uint8_t port, USBHostHub * hub_parent)
jakeb 15:688b3e3958fd 638 {
jakeb 15:688b3e3958fd 639 for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) {
jakeb 15:688b3e3958fd 640 if (devices[i].getHub() == hub && devices[i].getPort() == port) {
jakeb 15:688b3e3958fd 641 if (hub_parent != NULL) {
jakeb 15:688b3e3958fd 642 if (hub_parent == devices[i].getHubParent())
jakeb 15:688b3e3958fd 643 return i;
jakeb 15:688b3e3958fd 644 } else {
jakeb 15:688b3e3958fd 645 return i;
jakeb 15:688b3e3958fd 646 }
jakeb 15:688b3e3958fd 647 }
jakeb 15:688b3e3958fd 648 }
jakeb 15:688b3e3958fd 649 return -1;
jakeb 15:688b3e3958fd 650 }
jakeb 15:688b3e3958fd 651
jakeb 15:688b3e3958fd 652 void USBHost::printList(ENDPOINT_TYPE type)
jakeb 15:688b3e3958fd 653 {
jakeb 15:688b3e3958fd 654 #if DEBUG_EP_STATE
jakeb 15:688b3e3958fd 655 volatile HCED * hced;
jakeb 15:688b3e3958fd 656 switch(type) {
jakeb 15:688b3e3958fd 657 case CONTROL_ENDPOINT:
jakeb 15:688b3e3958fd 658 hced = (HCED *)controlHeadED();
jakeb 15:688b3e3958fd 659 break;
jakeb 15:688b3e3958fd 660 case BULK_ENDPOINT:
jakeb 15:688b3e3958fd 661 hced = (HCED *)bulkHeadED();
jakeb 15:688b3e3958fd 662 break;
jakeb 15:688b3e3958fd 663 case INTERRUPT_ENDPOINT:
jakeb 15:688b3e3958fd 664 hced = (HCED *)interruptHeadED();
jakeb 15:688b3e3958fd 665 break;
jakeb 15:688b3e3958fd 666 }
jakeb 15:688b3e3958fd 667 volatile HCTD * hctd = NULL;
jakeb 15:688b3e3958fd 668 const char * type_str = (type == BULK_ENDPOINT) ? "BULK" :
jakeb 15:688b3e3958fd 669 ((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" :
jakeb 15:688b3e3958fd 670 ((type == CONTROL_ENDPOINT) ? "CONTROL" : "ISOCHRONOUS"));
jakeb 15:688b3e3958fd 671 printf("State of %s:\r\n", type_str);
jakeb 15:688b3e3958fd 672 while (hced != NULL) {
jakeb 15:688b3e3958fd 673 uint8_t dir = ((hced->control & (3 << 11)) >> 11);
jakeb 15:688b3e3958fd 674 printf("hced: %p [ADDR: %d, DIR: %s, EP_NB: 0x%X]\r\n", hced,
jakeb 15:688b3e3958fd 675 hced->control & 0x7f,
jakeb 15:688b3e3958fd 676 (dir == 1) ? "OUT" : ((dir == 0) ? "FROM_TD":"IN"),
jakeb 15:688b3e3958fd 677 (hced->control & (0xf << 7)) >> 7);
jakeb 15:688b3e3958fd 678 hctd = (HCTD *)((uint32_t)(hced->headTD) & ~(0xf));
jakeb 15:688b3e3958fd 679 while (hctd != hced->tailTD) {
jakeb 15:688b3e3958fd 680 printf("\thctd: %p [DIR: %s]\r\n", hctd, ((hctd->control & (3 << 19)) >> 19) == 1 ? "OUT" : "IN");
jakeb 15:688b3e3958fd 681 hctd = hctd->nextTD;
jakeb 15:688b3e3958fd 682 }
jakeb 15:688b3e3958fd 683 printf("\thctd: %p\r\n", hctd);
jakeb 15:688b3e3958fd 684 hced = hced->nextED;
jakeb 15:688b3e3958fd 685 }
jakeb 15:688b3e3958fd 686 printf("\r\n\r\n");
jakeb 15:688b3e3958fd 687 #endif
jakeb 15:688b3e3958fd 688 }
jakeb 15:688b3e3958fd 689
jakeb 15:688b3e3958fd 690
jakeb 15:688b3e3958fd 691 // add a transfer on the TD linked list
jakeb 15:688b3e3958fd 692 USB_TYPE USBHost::addTransfer(USBEndpoint * ed, uint8_t * buf, uint32_t len)
jakeb 15:688b3e3958fd 693 {
jakeb 15:688b3e3958fd 694 td_mutex.lock();
jakeb 15:688b3e3958fd 695
jakeb 15:688b3e3958fd 696 // allocate a TD which will be freed in TDcompletion
jakeb 15:688b3e3958fd 697 volatile HCTD * td = ed->getNextTD();
jakeb 15:688b3e3958fd 698 if (td == NULL) {
jakeb 15:688b3e3958fd 699 return USB_TYPE_ERROR;
jakeb 15:688b3e3958fd 700 }
jakeb 15:688b3e3958fd 701
jakeb 15:688b3e3958fd 702 uint32_t token = (ed->isSetup() ? TD_SETUP : ( (ed->getDir() == IN) ? TD_IN : TD_OUT ));
jakeb 15:688b3e3958fd 703
jakeb 15:688b3e3958fd 704 uint32_t td_toggle;
jakeb 15:688b3e3958fd 705
jakeb 15:688b3e3958fd 706 if (ed->getType() == CONTROL_ENDPOINT) {
jakeb 15:688b3e3958fd 707 if (ed->isSetup()) {
jakeb 15:688b3e3958fd 708 td_toggle = TD_TOGGLE_0;
jakeb 15:688b3e3958fd 709 } else {
jakeb 15:688b3e3958fd 710 td_toggle = TD_TOGGLE_1;
jakeb 15:688b3e3958fd 711 }
jakeb 15:688b3e3958fd 712 } else {
jakeb 15:688b3e3958fd 713 td_toggle = 0;
jakeb 15:688b3e3958fd 714 }
jakeb 15:688b3e3958fd 715
jakeb 15:688b3e3958fd 716 td->control = (TD_ROUNDING | token | TD_DELAY_INT(0) | td_toggle | TD_CC);
jakeb 15:688b3e3958fd 717 td->currBufPtr = buf;
jakeb 15:688b3e3958fd 718 td->bufEnd = (buf + (len - 1));
jakeb 15:688b3e3958fd 719
jakeb 15:688b3e3958fd 720 ENDPOINT_TYPE type = ed->getType();
jakeb 15:688b3e3958fd 721
jakeb 15:688b3e3958fd 722 disableList(type);
jakeb 15:688b3e3958fd 723 ed->queueTransfer();
jakeb 15:688b3e3958fd 724 printList(type);
jakeb 15:688b3e3958fd 725 enableList(type);
jakeb 15:688b3e3958fd 726
jakeb 15:688b3e3958fd 727 td_mutex.unlock();
jakeb 15:688b3e3958fd 728
jakeb 15:688b3e3958fd 729 return USB_TYPE_PROCESSING;
jakeb 15:688b3e3958fd 730 }
jakeb 15:688b3e3958fd 731
jakeb 15:688b3e3958fd 732
jakeb 15:688b3e3958fd 733
jakeb 15:688b3e3958fd 734 USB_TYPE USBHost::getDeviceDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_dev_descr)
jakeb 15:688b3e3958fd 735 {
jakeb 15:688b3e3958fd 736 USB_TYPE t = controlRead( dev,
jakeb 15:688b3e3958fd 737 USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE,
jakeb 15:688b3e3958fd 738 GET_DESCRIPTOR,
jakeb 15:688b3e3958fd 739 (DEVICE_DESCRIPTOR << 8) | (0),
jakeb 15:688b3e3958fd 740 0, buf, MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf));
jakeb 15:688b3e3958fd 741 if (len_dev_descr)
jakeb 15:688b3e3958fd 742 *len_dev_descr = MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf);
jakeb 15:688b3e3958fd 743
jakeb 15:688b3e3958fd 744 return t;
jakeb 15:688b3e3958fd 745 }
jakeb 15:688b3e3958fd 746
jakeb 15:688b3e3958fd 747 USB_TYPE USBHost::getConfigurationDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_conf_descr)
jakeb 15:688b3e3958fd 748 {
jakeb 15:688b3e3958fd 749 USB_TYPE res;
jakeb 15:688b3e3958fd 750 uint16_t total_conf_descr_length = 0;
jakeb 15:688b3e3958fd 751
jakeb 15:688b3e3958fd 752 // fourth step: get the beginning of the configuration descriptor to have the total length of the conf descr
jakeb 15:688b3e3958fd 753 res = controlRead( dev,
jakeb 15:688b3e3958fd 754 USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE,
jakeb 15:688b3e3958fd 755 GET_DESCRIPTOR,
jakeb 15:688b3e3958fd 756 (CONFIGURATION_DESCRIPTOR << 8) | (0),
jakeb 15:688b3e3958fd 757 0, buf, CONFIGURATION_DESCRIPTOR_LENGTH);
jakeb 15:688b3e3958fd 758
jakeb 15:688b3e3958fd 759 if (res != USB_TYPE_OK) {
jakeb 15:688b3e3958fd 760 USB_ERR("GET CONF 1 DESCR FAILED");
jakeb 15:688b3e3958fd 761 return res;
jakeb 15:688b3e3958fd 762 }
jakeb 15:688b3e3958fd 763 total_conf_descr_length = buf[2] | (buf[3] << 8);
jakeb 15:688b3e3958fd 764 total_conf_descr_length = MIN(max_len_buf, total_conf_descr_length);
jakeb 15:688b3e3958fd 765
jakeb 15:688b3e3958fd 766 if (len_conf_descr)
jakeb 15:688b3e3958fd 767 *len_conf_descr = total_conf_descr_length;
jakeb 15:688b3e3958fd 768
jakeb 15:688b3e3958fd 769 USB_DBG("TOTAL_LENGTH: %d \t NUM_INTERF: %d", total_conf_descr_length, buf[4]);
jakeb 15:688b3e3958fd 770
jakeb 15:688b3e3958fd 771 return controlRead( dev,
jakeb 15:688b3e3958fd 772 USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE,
jakeb 15:688b3e3958fd 773 GET_DESCRIPTOR,
jakeb 15:688b3e3958fd 774 (CONFIGURATION_DESCRIPTOR << 8) | (0),
jakeb 15:688b3e3958fd 775 0, buf, total_conf_descr_length);
jakeb 15:688b3e3958fd 776 }
jakeb 15:688b3e3958fd 777
jakeb 15:688b3e3958fd 778
jakeb 15:688b3e3958fd 779 USB_TYPE USBHost::setAddress(USBDeviceConnected * dev, uint8_t address) {
jakeb 15:688b3e3958fd 780 return controlWrite( dev,
jakeb 15:688b3e3958fd 781 USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE,
jakeb 15:688b3e3958fd 782 SET_ADDRESS,
jakeb 15:688b3e3958fd 783 address,
jakeb 15:688b3e3958fd 784 0, NULL, 0);
jakeb 15:688b3e3958fd 785
jakeb 15:688b3e3958fd 786 }
jakeb 15:688b3e3958fd 787
jakeb 15:688b3e3958fd 788 USB_TYPE USBHost::setConfiguration(USBDeviceConnected * dev, uint8_t conf)
jakeb 15:688b3e3958fd 789 {
jakeb 15:688b3e3958fd 790 return controlWrite( dev,
jakeb 15:688b3e3958fd 791 USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE,
jakeb 15:688b3e3958fd 792 SET_CONFIGURATION,
jakeb 15:688b3e3958fd 793 conf,
jakeb 15:688b3e3958fd 794 0, NULL, 0);
jakeb 15:688b3e3958fd 795 }
jakeb 15:688b3e3958fd 796
jakeb 15:688b3e3958fd 797 uint8_t USBHost::numberDriverAttached(USBDeviceConnected * dev) {
jakeb 15:688b3e3958fd 798 int index = findDevice(dev);
jakeb 15:688b3e3958fd 799 uint8_t cnt = 0;
jakeb 15:688b3e3958fd 800 if (index == -1)
jakeb 15:688b3e3958fd 801 return 0;
jakeb 15:688b3e3958fd 802 for (uint8_t i = 0; i < MAX_INTF; i++) {
jakeb 15:688b3e3958fd 803 if (deviceAttachedDriver[index][i])
jakeb 15:688b3e3958fd 804 cnt++;
jakeb 15:688b3e3958fd 805 }
jakeb 15:688b3e3958fd 806 return cnt;
jakeb 15:688b3e3958fd 807 }
jakeb 15:688b3e3958fd 808
jakeb 15:688b3e3958fd 809 // enumerate a device with the control USBEndpoint
jakeb 15:688b3e3958fd 810 USB_TYPE USBHost::enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator)
jakeb 15:688b3e3958fd 811 {
jakeb 15:688b3e3958fd 812 uint16_t total_conf_descr_length = 0;
jakeb 15:688b3e3958fd 813 USB_TYPE res;
jakeb 15:688b3e3958fd 814
jakeb 15:688b3e3958fd 815 usb_mutex.lock();
jakeb 15:688b3e3958fd 816
jakeb 15:688b3e3958fd 817 // don't enumerate a device which all interfaces are registered to a specific driver
jakeb 15:688b3e3958fd 818 int index = findDevice(dev);
jakeb 15:688b3e3958fd 819
jakeb 15:688b3e3958fd 820 if (index == -1) {
jakeb 15:688b3e3958fd 821 usb_mutex.unlock();
jakeb 15:688b3e3958fd 822 return USB_TYPE_ERROR;
jakeb 15:688b3e3958fd 823 }
jakeb 15:688b3e3958fd 824
jakeb 15:688b3e3958fd 825 uint8_t nb_intf_attached = numberDriverAttached(dev);
jakeb 15:688b3e3958fd 826 USB_DBG("dev: %p nb_intf: %d", dev, dev->getNbIntf());
jakeb 15:688b3e3958fd 827 USB_DBG("dev: %p nb_intf_attached: %d", dev, nb_intf_attached);
jakeb 15:688b3e3958fd 828 if ((nb_intf_attached != 0) && (dev->getNbIntf() == nb_intf_attached)) {
jakeb 15:688b3e3958fd 829 USB_DBG("Don't enumerate dev: %p because all intf are registered with a driver", dev);
jakeb 15:688b3e3958fd 830 usb_mutex.unlock();
jakeb 15:688b3e3958fd 831 return USB_TYPE_OK;
jakeb 15:688b3e3958fd 832 }
jakeb 15:688b3e3958fd 833
jakeb 15:688b3e3958fd 834 USB_DBG("Enumerate dev: %p", dev);
jakeb 15:688b3e3958fd 835
jakeb 15:688b3e3958fd 836 // third step: get the whole device descriptor to see vid, pid
jakeb 15:688b3e3958fd 837 res = getDeviceDescriptor(dev, data, DEVICE_DESCRIPTOR_LENGTH);
jakeb 15:688b3e3958fd 838
jakeb 15:688b3e3958fd 839 if (res != USB_TYPE_OK) {
jakeb 15:688b3e3958fd 840 USB_DBG("GET DEV DESCR FAILED");
jakeb 15:688b3e3958fd 841 usb_mutex.unlock();
jakeb 15:688b3e3958fd 842 return res;
jakeb 15:688b3e3958fd 843 }
jakeb 15:688b3e3958fd 844
jakeb 15:688b3e3958fd 845 dev->setClass(data[4]);
jakeb 15:688b3e3958fd 846 dev->setSubClass(data[5]);
jakeb 15:688b3e3958fd 847 dev->setProtocol(data[6]);
jakeb 15:688b3e3958fd 848 dev->setVid(data[8] | (data[9] << 8));
jakeb 15:688b3e3958fd 849 dev->setPid(data[10] | (data[11] << 8));
jakeb 15:688b3e3958fd 850 USB_DBG("CLASS: %02X \t VID: %04X \t PID: %04X", data[4], data[8] | (data[9] << 8), data[10] | (data[11] << 8));
jakeb 15:688b3e3958fd 851
jakeb 15:688b3e3958fd 852 pEnumerator->setVidPid( data[8] | (data[9] << 8), data[10] | (data[11] << 8) );
jakeb 15:688b3e3958fd 853
jakeb 15:688b3e3958fd 854 res = getConfigurationDescriptor(dev, data, 300, &total_conf_descr_length);
jakeb 15:688b3e3958fd 855 if (res != USB_TYPE_OK) {
jakeb 15:688b3e3958fd 856 usb_mutex.unlock();
jakeb 15:688b3e3958fd 857 return res;
jakeb 15:688b3e3958fd 858 }
jakeb 15:688b3e3958fd 859
jakeb 15:688b3e3958fd 860 #if DEBUG
jakeb 15:688b3e3958fd 861 USB_DBG("CONFIGURATION DESCRIPTOR:\r\n");
jakeb 15:688b3e3958fd 862 for (int i = 0; i < total_conf_descr_length; i++)
jakeb 15:688b3e3958fd 863 printf("%02X ", data[i]);
jakeb 15:688b3e3958fd 864 printf("\r\n\r\n");
jakeb 15:688b3e3958fd 865 #endif
jakeb 15:688b3e3958fd 866
jakeb 15:688b3e3958fd 867 // Parse the configuration descriptor
jakeb 15:688b3e3958fd 868 parseConfDescr(dev, data, total_conf_descr_length, pEnumerator);
jakeb 15:688b3e3958fd 869
jakeb 15:688b3e3958fd 870 // only set configuration if not enumerated before
jakeb 15:688b3e3958fd 871 if (!dev->isEnumerated()) {
jakeb 15:688b3e3958fd 872
jakeb 15:688b3e3958fd 873 USB_DBG("Set configuration 1 on dev: %p", dev);
jakeb 15:688b3e3958fd 874 // sixth step: set configuration (only 1 supported)
jakeb 15:688b3e3958fd 875 res = setConfiguration(dev, 1);
jakeb 15:688b3e3958fd 876
jakeb 15:688b3e3958fd 877 if (res != USB_TYPE_OK) {
jakeb 15:688b3e3958fd 878 USB_DBG("SET CONF FAILED");
jakeb 15:688b3e3958fd 879 usb_mutex.unlock();
jakeb 15:688b3e3958fd 880 return res;
jakeb 15:688b3e3958fd 881 }
jakeb 15:688b3e3958fd 882 }
jakeb 15:688b3e3958fd 883
jakeb 15:688b3e3958fd 884 dev->setEnumerated();
jakeb 15:688b3e3958fd 885
jakeb 15:688b3e3958fd 886 // Now the device is enumerated!
jakeb 15:688b3e3958fd 887 USB_DBG("dev %p is enumerated\r\n", dev);
jakeb 15:688b3e3958fd 888 usb_mutex.unlock();
jakeb 15:688b3e3958fd 889
jakeb 15:688b3e3958fd 890 // Some devices may require this delay
jakeb 15:688b3e3958fd 891 wait_ms(100);
jakeb 15:688b3e3958fd 892
jakeb 15:688b3e3958fd 893 return USB_TYPE_OK;
jakeb 15:688b3e3958fd 894 }
jakeb 15:688b3e3958fd 895 // this method fills the USBDeviceConnected object: class,.... . It also add endpoints found in the descriptor.
jakeb 15:688b3e3958fd 896 void USBHost::parseConfDescr(USBDeviceConnected * dev, uint8_t * conf_descr, uint32_t len, IUSBEnumerator* pEnumerator)
jakeb 15:688b3e3958fd 897 {
jakeb 15:688b3e3958fd 898 uint32_t index = 0;
jakeb 15:688b3e3958fd 899 uint32_t len_desc = 0;
jakeb 15:688b3e3958fd 900 uint8_t id = 0;
jakeb 15:688b3e3958fd 901 int nb_endpoints_used = 0;
jakeb 15:688b3e3958fd 902 USBEndpoint * ep = NULL;
jakeb 15:688b3e3958fd 903 uint8_t intf_nb = 0;
jakeb 15:688b3e3958fd 904 bool parsing_intf = false;
jakeb 15:688b3e3958fd 905 uint8_t current_intf = 0;
jakeb 15:688b3e3958fd 906
jakeb 15:688b3e3958fd 907 while (index < len) {
jakeb 15:688b3e3958fd 908 len_desc = conf_descr[index];
jakeb 15:688b3e3958fd 909 id = conf_descr[index+1];
jakeb 15:688b3e3958fd 910 switch (id) {
jakeb 15:688b3e3958fd 911 case CONFIGURATION_DESCRIPTOR:
jakeb 15:688b3e3958fd 912 USB_DBG("dev: %p has %d intf", dev, conf_descr[4]);
jakeb 15:688b3e3958fd 913 dev->setNbIntf(conf_descr[4]);
jakeb 15:688b3e3958fd 914 break;
jakeb 15:688b3e3958fd 915 case INTERFACE_DESCRIPTOR:
jakeb 15:688b3e3958fd 916 if(pEnumerator->parseInterface(conf_descr[index + 2], conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7])) {
jakeb 15:688b3e3958fd 917 if (intf_nb++ <= MAX_INTF) {
jakeb 15:688b3e3958fd 918 current_intf = conf_descr[index + 2];
jakeb 15:688b3e3958fd 919 dev->addInterface(current_intf, conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7]);
jakeb 15:688b3e3958fd 920 nb_endpoints_used = 0;
jakeb 15:688b3e3958fd 921 USB_DBG("ADD INTF %d on device %p: class: %d, subclass: %d, proto: %d", current_intf, dev, conf_descr[index + 5],conf_descr[index + 6],conf_descr[index + 7]);
jakeb 15:688b3e3958fd 922 } else {
jakeb 15:688b3e3958fd 923 USB_DBG("Drop intf...");
jakeb 15:688b3e3958fd 924 }
jakeb 15:688b3e3958fd 925 parsing_intf = true;
jakeb 15:688b3e3958fd 926 } else {
jakeb 15:688b3e3958fd 927 parsing_intf = false;
jakeb 15:688b3e3958fd 928 }
jakeb 15:688b3e3958fd 929 break;
jakeb 15:688b3e3958fd 930 case ENDPOINT_DESCRIPTOR:
jakeb 15:688b3e3958fd 931 if (parsing_intf && (intf_nb <= MAX_INTF) ) {
jakeb 15:688b3e3958fd 932 if (nb_endpoints_used < MAX_ENDPOINT_PER_INTERFACE) {
jakeb 15:688b3e3958fd 933 if( pEnumerator->useEndpoint(current_intf, (ENDPOINT_TYPE)(conf_descr[index + 3] & 0x03), (ENDPOINT_DIRECTION)((conf_descr[index + 2] >> 7) + 1)) ) {
jakeb 15:688b3e3958fd 934 // if the USBEndpoint is isochronous -> skip it (TODO: fix this)
jakeb 15:688b3e3958fd 935 if ((conf_descr[index + 3] & 0x03) != ISOCHRONOUS_ENDPOINT) {
jakeb 15:688b3e3958fd 936 ep = newEndpoint((ENDPOINT_TYPE)(conf_descr[index+3] & 0x03),
jakeb 15:688b3e3958fd 937 (ENDPOINT_DIRECTION)((conf_descr[index + 2] >> 7) + 1),
jakeb 15:688b3e3958fd 938 conf_descr[index + 4] | (conf_descr[index + 5] << 8),
jakeb 15:688b3e3958fd 939 conf_descr[index + 2] & 0x0f);
jakeb 15:688b3e3958fd 940 USB_DBG("ADD USBEndpoint %p, on interf %d on device %p", ep, current_intf, dev);
jakeb 15:688b3e3958fd 941 if (ep != NULL && dev != NULL) {
jakeb 15:688b3e3958fd 942 addEndpoint(dev, current_intf, ep);
jakeb 15:688b3e3958fd 943 } else {
jakeb 15:688b3e3958fd 944 USB_DBG("EP NULL");
jakeb 15:688b3e3958fd 945 }
jakeb 15:688b3e3958fd 946 nb_endpoints_used++;
jakeb 15:688b3e3958fd 947 } else {
jakeb 15:688b3e3958fd 948 USB_DBG("ISO USBEndpoint NOT SUPPORTED");
jakeb 15:688b3e3958fd 949 }
jakeb 15:688b3e3958fd 950 }
jakeb 15:688b3e3958fd 951 }
jakeb 15:688b3e3958fd 952 }
jakeb 15:688b3e3958fd 953 break;
jakeb 15:688b3e3958fd 954 case HID_DESCRIPTOR:
jakeb 15:688b3e3958fd 955 lenReportDescr = conf_descr[index + 7] | (conf_descr[index + 8] << 8);
jakeb 15:688b3e3958fd 956 break;
jakeb 15:688b3e3958fd 957 default:
jakeb 15:688b3e3958fd 958 break;
jakeb 15:688b3e3958fd 959 }
jakeb 15:688b3e3958fd 960 index += len_desc;
jakeb 15:688b3e3958fd 961 }
jakeb 15:688b3e3958fd 962 }
jakeb 15:688b3e3958fd 963
jakeb 15:688b3e3958fd 964
jakeb 15:688b3e3958fd 965 USB_TYPE USBHost::bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking)
jakeb 15:688b3e3958fd 966 {
jakeb 15:688b3e3958fd 967 return generalTransfer(dev, ep, buf, len, blocking, BULK_ENDPOINT, true);
jakeb 15:688b3e3958fd 968 }
jakeb 15:688b3e3958fd 969
jakeb 15:688b3e3958fd 970 USB_TYPE USBHost::bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking)
jakeb 15:688b3e3958fd 971 {
jakeb 15:688b3e3958fd 972 return generalTransfer(dev, ep, buf, len, blocking, BULK_ENDPOINT, false);
jakeb 15:688b3e3958fd 973 }
jakeb 15:688b3e3958fd 974
jakeb 15:688b3e3958fd 975 USB_TYPE USBHost::interruptWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking)
jakeb 15:688b3e3958fd 976 {
jakeb 15:688b3e3958fd 977 return generalTransfer(dev, ep, buf, len, blocking, INTERRUPT_ENDPOINT, true);
jakeb 15:688b3e3958fd 978 }
jakeb 15:688b3e3958fd 979
jakeb 15:688b3e3958fd 980 USB_TYPE USBHost::interruptRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking)
jakeb 15:688b3e3958fd 981 {
jakeb 15:688b3e3958fd 982 return generalTransfer(dev, ep, buf, len, blocking, INTERRUPT_ENDPOINT, false);
jakeb 15:688b3e3958fd 983 }
jakeb 15:688b3e3958fd 984
jakeb 15:688b3e3958fd 985 USB_TYPE USBHost::generalTransfer(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking, ENDPOINT_TYPE type, bool write) {
jakeb 15:688b3e3958fd 986
jakeb 15:688b3e3958fd 987 #if DEBUG_TRANSFER
jakeb 15:688b3e3958fd 988 const char * type_str = (type == BULK_ENDPOINT) ? "BULK" : ((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" : "ISOCHRONOUS");
jakeb 15:688b3e3958fd 989 USB_DBG_TRANSFER("----- %s %s [dev: %p - %s - hub: %d - port: %d - addr: %d - ep: %02X]------", type_str, (write) ? "WRITE" : "READ", dev, dev->getName(ep->getIntfNb()), dev->getHub(), dev->getPort(), dev->getAddress(), ep->getAddress());
jakeb 15:688b3e3958fd 990 #endif
jakeb 15:688b3e3958fd 991
jakeb 15:688b3e3958fd 992 usb_mutex.lock();
jakeb 15:688b3e3958fd 993
jakeb 15:688b3e3958fd 994 USB_TYPE res;
jakeb 15:688b3e3958fd 995 ENDPOINT_DIRECTION dir = (write) ? OUT : IN;
jakeb 15:688b3e3958fd 996
jakeb 15:688b3e3958fd 997 if (dev == NULL) {
jakeb 15:688b3e3958fd 998 USB_ERR("dev NULL");
jakeb 15:688b3e3958fd 999 usb_mutex.unlock();
jakeb 15:688b3e3958fd 1000 return USB_TYPE_ERROR;
jakeb 15:688b3e3958fd 1001 }
jakeb 15:688b3e3958fd 1002
jakeb 15:688b3e3958fd 1003 if (ep == NULL) {
jakeb 15:688b3e3958fd 1004 USB_ERR("ep NULL");
jakeb 15:688b3e3958fd 1005 usb_mutex.unlock();
jakeb 15:688b3e3958fd 1006 return USB_TYPE_ERROR;
jakeb 15:688b3e3958fd 1007 }
jakeb 15:688b3e3958fd 1008
jakeb 15:688b3e3958fd 1009 if (ep->getState() != USB_TYPE_IDLE) {
jakeb 15:688b3e3958fd 1010 USB_WARN("[ep: %p - dev: %p - %s] NOT IDLE: %s", ep, ep->dev, ep->dev->getName(ep->getIntfNb()), ep->getStateString());
jakeb 15:688b3e3958fd 1011 usb_mutex.unlock();
jakeb 15:688b3e3958fd 1012 return ep->getState();
jakeb 15:688b3e3958fd 1013 }
jakeb 15:688b3e3958fd 1014
jakeb 15:688b3e3958fd 1015 if ((ep->getDir() != dir) || (ep->getType() != type)) {
jakeb 15:688b3e3958fd 1016 USB_ERR("[ep: %p - dev: %p] wrong dir or bad USBEndpoint type", ep, ep->dev);
jakeb 15:688b3e3958fd 1017 usb_mutex.unlock();
jakeb 15:688b3e3958fd 1018 return USB_TYPE_ERROR;
jakeb 15:688b3e3958fd 1019 }
jakeb 15:688b3e3958fd 1020
jakeb 15:688b3e3958fd 1021 if (dev->getAddress() != ep->getDeviceAddress()) {
jakeb 15:688b3e3958fd 1022 USB_ERR("[ep: %p - dev: %p] USBEndpoint addr and device addr don't match", ep, ep->dev);
jakeb 15:688b3e3958fd 1023 usb_mutex.unlock();
jakeb 15:688b3e3958fd 1024 return USB_TYPE_ERROR;
jakeb 15:688b3e3958fd 1025 }
jakeb 15:688b3e3958fd 1026
jakeb 15:688b3e3958fd 1027 #if DEBUG_TRANSFER
jakeb 15:688b3e3958fd 1028 if (write) {
jakeb 15:688b3e3958fd 1029 USB_DBG_TRANSFER("%s WRITE buffer", type_str);
jakeb 15:688b3e3958fd 1030 for (int i = 0; i < ep->getLengthTransferred(); i++)
jakeb 15:688b3e3958fd 1031 printf("%02X ", buf[i]);
jakeb 15:688b3e3958fd 1032 printf("\r\n\r\n");
jakeb 15:688b3e3958fd 1033 }
jakeb 15:688b3e3958fd 1034 #endif
jakeb 15:688b3e3958fd 1035 addTransfer(ep, buf, len);
jakeb 15:688b3e3958fd 1036
jakeb 15:688b3e3958fd 1037 if (blocking) {
jakeb 15:688b3e3958fd 1038
jakeb 15:688b3e3958fd 1039 ep->ep_queue.get();
jakeb 15:688b3e3958fd 1040 res = ep->getState();
jakeb 15:688b3e3958fd 1041
jakeb 15:688b3e3958fd 1042 USB_DBG_TRANSFER("%s TRANSFER res: %s on ep: %p\r\n", type_str, ep->getStateString(), ep);
jakeb 15:688b3e3958fd 1043
jakeb 15:688b3e3958fd 1044 if (res != USB_TYPE_IDLE) {
jakeb 15:688b3e3958fd 1045 usb_mutex.unlock();
jakeb 15:688b3e3958fd 1046 return res;
jakeb 15:688b3e3958fd 1047 }
jakeb 15:688b3e3958fd 1048
jakeb 15:688b3e3958fd 1049 usb_mutex.unlock();
jakeb 15:688b3e3958fd 1050 return USB_TYPE_OK;
jakeb 15:688b3e3958fd 1051 }
jakeb 15:688b3e3958fd 1052
jakeb 15:688b3e3958fd 1053 usb_mutex.unlock();
jakeb 15:688b3e3958fd 1054 return USB_TYPE_PROCESSING;
jakeb 15:688b3e3958fd 1055
jakeb 15:688b3e3958fd 1056 }
jakeb 15:688b3e3958fd 1057
jakeb 15:688b3e3958fd 1058
jakeb 15:688b3e3958fd 1059 USB_TYPE USBHost::controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) {
jakeb 15:688b3e3958fd 1060 return controlTransfer(dev, requestType, request, value, index, buf, len, false);
jakeb 15:688b3e3958fd 1061 }
jakeb 15:688b3e3958fd 1062
jakeb 15:688b3e3958fd 1063 USB_TYPE USBHost::controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) {
jakeb 15:688b3e3958fd 1064 return controlTransfer(dev, requestType, request, value, index, buf, len, true);
jakeb 15:688b3e3958fd 1065 }
jakeb 15:688b3e3958fd 1066
jakeb 15:688b3e3958fd 1067 USB_TYPE USBHost::controlTransfer(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len, bool write)
jakeb 15:688b3e3958fd 1068 {
jakeb 15:688b3e3958fd 1069 usb_mutex.lock();
jakeb 15:688b3e3958fd 1070 USB_DBG_TRANSFER("----- CONTROL %s [dev: %p - hub: %d - port: %d] ------", (write) ? "WRITE" : "READ", dev, dev->getHub(), dev->getPort());
jakeb 15:688b3e3958fd 1071
jakeb 15:688b3e3958fd 1072 int length_transfer = len;
jakeb 15:688b3e3958fd 1073 USB_TYPE res;
jakeb 15:688b3e3958fd 1074 uint32_t token;
jakeb 15:688b3e3958fd 1075
jakeb 15:688b3e3958fd 1076 control->setSpeed(dev->getSpeed());
jakeb 15:688b3e3958fd 1077 control->setSize(dev->getSizeControlEndpoint());
jakeb 15:688b3e3958fd 1078 if (dev->isActiveAddress()) {
jakeb 15:688b3e3958fd 1079 control->setDeviceAddress(dev->getAddress());
jakeb 15:688b3e3958fd 1080 } else {
jakeb 15:688b3e3958fd 1081 control->setDeviceAddress(0);
jakeb 15:688b3e3958fd 1082 }
jakeb 15:688b3e3958fd 1083
jakeb 15:688b3e3958fd 1084 USB_DBG_TRANSFER("Control transfer on device: %d\r\n", control->getDeviceAddress());
jakeb 15:688b3e3958fd 1085 fillControlBuf(requestType, request, value, index, len);
jakeb 15:688b3e3958fd 1086
jakeb 15:688b3e3958fd 1087 #if DEBUG_TRANSFER
jakeb 15:688b3e3958fd 1088 USB_DBG_TRANSFER("SETUP PACKET: ");
jakeb 15:688b3e3958fd 1089 for (int i = 0; i < 8; i++)
jakeb 15:688b3e3958fd 1090 printf("%01X ", setupPacket[i]);
jakeb 15:688b3e3958fd 1091 printf("\r\n");
jakeb 15:688b3e3958fd 1092 #endif
jakeb 15:688b3e3958fd 1093
jakeb 15:688b3e3958fd 1094 control->setNextToken(TD_SETUP);
jakeb 15:688b3e3958fd 1095 addTransfer(control, (uint8_t*)setupPacket, 8);
jakeb 15:688b3e3958fd 1096
jakeb 15:688b3e3958fd 1097 control->ep_queue.get();
jakeb 15:688b3e3958fd 1098 res = control->getState();
jakeb 15:688b3e3958fd 1099
jakeb 15:688b3e3958fd 1100 USB_DBG_TRANSFER("CONTROL setup stage %s", control->getStateString());
jakeb 15:688b3e3958fd 1101
jakeb 15:688b3e3958fd 1102 if (res != USB_TYPE_IDLE) {
jakeb 15:688b3e3958fd 1103 usb_mutex.unlock();
jakeb 15:688b3e3958fd 1104 return res;
jakeb 15:688b3e3958fd 1105 }
jakeb 15:688b3e3958fd 1106
jakeb 15:688b3e3958fd 1107 if (length_transfer) {
jakeb 15:688b3e3958fd 1108 token = (write) ? TD_OUT : TD_IN;
jakeb 15:688b3e3958fd 1109 control->setNextToken(token);
jakeb 15:688b3e3958fd 1110 addTransfer(control, (uint8_t *)buf, length_transfer);
jakeb 15:688b3e3958fd 1111
jakeb 15:688b3e3958fd 1112 control->ep_queue.get();
jakeb 15:688b3e3958fd 1113 res = control->getState();
jakeb 15:688b3e3958fd 1114
jakeb 15:688b3e3958fd 1115 #if DEBUG_TRANSFER
jakeb 15:688b3e3958fd 1116 USB_DBG_TRANSFER("CONTROL %s stage %s", (write) ? "WRITE" : "READ", control->getStateString());
jakeb 15:688b3e3958fd 1117 if (write) {
jakeb 15:688b3e3958fd 1118 USB_DBG_TRANSFER("CONTROL WRITE buffer");
jakeb 15:688b3e3958fd 1119 for (int i = 0; i < control->getLengthTransferred(); i++)
jakeb 15:688b3e3958fd 1120 printf("%02X ", buf[i]);
jakeb 15:688b3e3958fd 1121 printf("\r\n\r\n");
jakeb 15:688b3e3958fd 1122 } else {
jakeb 15:688b3e3958fd 1123 USB_DBG_TRANSFER("CONTROL READ SUCCESS [%d bytes transferred]", control->getLengthTransferred());
jakeb 15:688b3e3958fd 1124 for (int i = 0; i < control->getLengthTransferred(); i++)
jakeb 15:688b3e3958fd 1125 printf("%02X ", buf[i]);
jakeb 15:688b3e3958fd 1126 printf("\r\n\r\n");
jakeb 15:688b3e3958fd 1127 }
jakeb 15:688b3e3958fd 1128 #endif
jakeb 15:688b3e3958fd 1129
jakeb 15:688b3e3958fd 1130 if (res != USB_TYPE_IDLE) {
jakeb 15:688b3e3958fd 1131 usb_mutex.unlock();
jakeb 15:688b3e3958fd 1132 return res;
jakeb 15:688b3e3958fd 1133 }
jakeb 15:688b3e3958fd 1134 }
jakeb 15:688b3e3958fd 1135
jakeb 15:688b3e3958fd 1136 token = (write) ? TD_IN : TD_OUT;
jakeb 15:688b3e3958fd 1137 control->setNextToken(token);
jakeb 15:688b3e3958fd 1138 addTransfer(control, NULL, 0);
jakeb 15:688b3e3958fd 1139
jakeb 15:688b3e3958fd 1140 control->ep_queue.get();
jakeb 15:688b3e3958fd 1141 res = control->getState();
jakeb 15:688b3e3958fd 1142
jakeb 15:688b3e3958fd 1143 USB_DBG_TRANSFER("CONTROL ack stage %s", control->getStateString());
jakeb 15:688b3e3958fd 1144 usb_mutex.unlock();
jakeb 15:688b3e3958fd 1145
jakeb 15:688b3e3958fd 1146 if (res != USB_TYPE_IDLE)
jakeb 15:688b3e3958fd 1147 return res;
jakeb 15:688b3e3958fd 1148
jakeb 15:688b3e3958fd 1149 return USB_TYPE_OK;
jakeb 15:688b3e3958fd 1150 }
jakeb 15:688b3e3958fd 1151
jakeb 15:688b3e3958fd 1152
jakeb 15:688b3e3958fd 1153 void USBHost::fillControlBuf(uint8_t requestType, uint8_t request, uint16_t value, uint16_t index, int len)
jakeb 15:688b3e3958fd 1154 {
jakeb 15:688b3e3958fd 1155 #ifdef __BIG_ENDIAN
jakeb 15:688b3e3958fd 1156 #error "Must implement BE to LE conv here"
jakeb 15:688b3e3958fd 1157 #endif
jakeb 15:688b3e3958fd 1158 setupPacket[0] = requestType;
jakeb 15:688b3e3958fd 1159 setupPacket[1] = request;
jakeb 15:688b3e3958fd 1160 //We are in LE so it's fine
jakeb 15:688b3e3958fd 1161 *((uint16_t*)&setupPacket[2]) = value;
jakeb 15:688b3e3958fd 1162 *((uint16_t*)&setupPacket[4]) = index;
jakeb 15:688b3e3958fd 1163 *((uint16_t*)&setupPacket[6]) = (uint32_t) len;
jakeb 15:688b3e3958fd 1164 }