USBHost library. NOTE: This library is only officially supported on the LPC1768 platform. For more information, please see the handbook page.

Dependencies:   FATFileSystem mbed-rtos

Dependents:   BTstack WallbotWii SD to Flash Data Transfer USBHost-MSD_HelloWorld ... 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:
Anna Bridge
Date:
Thu Aug 17 18:12:22 2017 +0100
Revision:
40:7c3b59bb364e
Parent:
37:f1e388e7b752
DISCO_L475VG_IOT01A: Add support of USBHost

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 8:93da8ea2708b 1 /* mbed USBHost Library
samux 8:93da8ea2708b 2 * Copyright (c) 2006-2013 ARM Limited
samux 8:93da8ea2708b 3 *
samux 8:93da8ea2708b 4 * Licensed under the Apache License, Version 2.0 (the "License");
samux 8:93da8ea2708b 5 * you may not use this file except in compliance with the License.
samux 8:93da8ea2708b 6 * You may obtain a copy of the License at
samux 8:93da8ea2708b 7 *
samux 8:93da8ea2708b 8 * http://www.apache.org/licenses/LICENSE-2.0
samux 8:93da8ea2708b 9 *
samux 8:93da8ea2708b 10 * Unless required by applicable law or agreed to in writing, software
samux 8:93da8ea2708b 11 * distributed under the License is distributed on an "AS IS" BASIS,
samux 8:93da8ea2708b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
samux 8:93da8ea2708b 13 * See the License for the specific language governing permissions and
samux 8:93da8ea2708b 14 * limitations under the License.
samux 8:93da8ea2708b 15 */
mbed_official 0:a554658735bf 16
mbed_official 0:a554658735bf 17 #include "USBHostKeyboard.h"
mbed_official 0:a554658735bf 18
mbed_official 0:a554658735bf 19 #if USBHOST_KEYBOARD
mbed_official 0:a554658735bf 20
mbed_official 24:868cbfe611a7 21 static uint8_t keymap[4][0x39] = {
Kojto 37:f1e388e7b752 22 {
Kojto 37:f1e388e7b752 23 0, 0, 0, 0, 'a', 'b' /*0x05*/,
Kojto 37:f1e388e7b752 24 'c', 'd', 'e', 'f', 'g' /*0x0a*/,
Kojto 37:f1e388e7b752 25 'h', 'i', 'j', 'k', 'l'/*0x0f*/,
Kojto 37:f1e388e7b752 26 'm', 'n', 'o', 'p', 'q'/*0x14*/,
Kojto 37:f1e388e7b752 27 'r', 's', 't', 'u', 'v'/*0x19*/,
Kojto 37:f1e388e7b752 28 'w', 'x', 'y', 'z', '1'/*0x1E*/,
Kojto 37:f1e388e7b752 29 '2', '3', '4', '5', '6'/*0x23*/,
Kojto 37:f1e388e7b752 30 '7', '8', '9', '0', 0x0A /*enter*/, /*0x28*/
Kojto 37:f1e388e7b752 31 0x1B /*escape*/, 0x08 /*backspace*/, 0x09/*tab*/, 0x20/*space*/, '-', /*0x2d*/
Kojto 37:f1e388e7b752 32 '=', '[', ']', '\\', '#', /*0x32*/
Kojto 37:f1e388e7b752 33 ';', '\'', 0, ',', '.', /*0x37*/
Kojto 37:f1e388e7b752 34 '/'
Kojto 37:f1e388e7b752 35 },
mbed_official 0:a554658735bf 36
mbed_official 0:a554658735bf 37 /* CTRL MODIFIER */
Kojto 37:f1e388e7b752 38 {
Kojto 37:f1e388e7b752 39 0, 0, 0, 0, 0, 0 /*0x05*/,
Kojto 37:f1e388e7b752 40 0, 0, 0, 0, 0 /*0x0a*/,
Kojto 37:f1e388e7b752 41 0, 0, 0, 0, 0/*0x0f*/,
Kojto 37:f1e388e7b752 42 0, 0, 0, 0, 0/*0x14*/,
Kojto 37:f1e388e7b752 43 0, 0, 0, 0, 0/*0x19*/,
Kojto 37:f1e388e7b752 44 0, 0, 0, 0, 0/*0x1E*/,
Kojto 37:f1e388e7b752 45 0, 0, 0, 0, 0/*0x23*/,
Kojto 37:f1e388e7b752 46 0, 0, 0, 0, 0 /*enter*/, /*0x28*/
Kojto 37:f1e388e7b752 47 0, 0, 0, 0, 0, /*0x2d*/
Kojto 37:f1e388e7b752 48 0, 0, 0, 0, 0, /*0x32*/
Kojto 37:f1e388e7b752 49 0, 0, 0, 0, 0, /*0x37*/
Kojto 37:f1e388e7b752 50 0
Kojto 37:f1e388e7b752 51 },
mbed_official 0:a554658735bf 52
mbed_official 0:a554658735bf 53 /* SHIFT MODIFIER */
Kojto 37:f1e388e7b752 54 {
Kojto 37:f1e388e7b752 55 0, 0, 0, 0, 'A', 'B' /*0x05*/,
Kojto 37:f1e388e7b752 56 'C', 'D', 'E', 'F', 'G' /*0x0a*/,
Kojto 37:f1e388e7b752 57 'H', 'I', 'J', 'K', 'L'/*0x0f*/,
Kojto 37:f1e388e7b752 58 'M', 'N', 'O', 'P', 'Q'/*0x14*/,
Kojto 37:f1e388e7b752 59 'R', 'S', 'T', 'U', 'V'/*0x19*/,
Kojto 37:f1e388e7b752 60 'W', 'X', 'Y', 'Z', '!'/*0x1E*/,
Kojto 37:f1e388e7b752 61 '@', '#', '$', '%', '^'/*0x23*/,
Kojto 37:f1e388e7b752 62 '&', '*', '(', ')', 0, /*0x28*/
Kojto 37:f1e388e7b752 63 0, 0, 0, 0, 0, /*0x2d*/
Kojto 37:f1e388e7b752 64 '+', '{', '}', '|', '~', /*0x32*/
Kojto 37:f1e388e7b752 65 ':', '"', 0, '<', '>', /*0x37*/
Kojto 37:f1e388e7b752 66 '?'
Kojto 37:f1e388e7b752 67 },
mbed_official 0:a554658735bf 68
mbed_official 0:a554658735bf 69 /* ALT MODIFIER */
Kojto 37:f1e388e7b752 70 {
Kojto 37:f1e388e7b752 71 0, 0, 0, 0, 0, 0 /*0x05*/,
Kojto 37:f1e388e7b752 72 0, 0, 0, 0, 0 /*0x0a*/,
Kojto 37:f1e388e7b752 73 0, 0, 0, 0, 0/*0x0f*/,
Kojto 37:f1e388e7b752 74 0, 0, 0, 0, 0/*0x14*/,
Kojto 37:f1e388e7b752 75 0, 0, 0, 0, 0/*0x19*/,
Kojto 37:f1e388e7b752 76 0, 0, 0, 0, 0/*0x1E*/,
Kojto 37:f1e388e7b752 77 0, 0, 0, 0, 0/*0x23*/,
Kojto 37:f1e388e7b752 78 0, 0, 0, 0, 0 /*enter*/, /*0x28*/
Kojto 37:f1e388e7b752 79 0, 0, 0, 0, 0, /*0x2d*/
Kojto 37:f1e388e7b752 80 0, 0, 0, 0, 0, /*0x32*/
Kojto 37:f1e388e7b752 81 0, 0, 0, 0, 0, /*0x37*/
Kojto 37:f1e388e7b752 82 0
Kojto 37:f1e388e7b752 83 }
mbed_official 0:a554658735bf 84
mbed_official 0:a554658735bf 85 };
mbed_official 0:a554658735bf 86
mbed_official 0:a554658735bf 87
Kojto 37:f1e388e7b752 88 USBHostKeyboard::USBHostKeyboard()
Kojto 37:f1e388e7b752 89 {
mbed_official 0:a554658735bf 90 host = USBHost::getHostInst();
mbed_official 0:a554658735bf 91 init();
mbed_official 0:a554658735bf 92 }
mbed_official 0:a554658735bf 93
mbed_official 0:a554658735bf 94
Kojto 37:f1e388e7b752 95 void USBHostKeyboard::init()
Kojto 37:f1e388e7b752 96 {
mbed_official 0:a554658735bf 97 dev = NULL;
mbed_official 0:a554658735bf 98 int_in = NULL;
mbed_official 0:a554658735bf 99 report_id = 0;
mbed_official 0:a554658735bf 100 onKey = NULL;
mbed_official 0:a554658735bf 101 onKeyCode = NULL;
mbed_official 0:a554658735bf 102 dev_connected = false;
mbed_official 0:a554658735bf 103 keyboard_intf = -1;
mbed_official 0:a554658735bf 104 keyboard_device_found = false;
mbed_official 0:a554658735bf 105 }
mbed_official 0:a554658735bf 106
Kojto 37:f1e388e7b752 107 bool USBHostKeyboard::connected()
Kojto 37:f1e388e7b752 108 {
mbed_official 0:a554658735bf 109 return dev_connected;
mbed_official 0:a554658735bf 110 }
mbed_official 0:a554658735bf 111
mbed_official 0:a554658735bf 112
Kojto 37:f1e388e7b752 113 bool USBHostKeyboard::connect()
Kojto 37:f1e388e7b752 114 {
mbed_official 24:868cbfe611a7 115
mbed_official 0:a554658735bf 116 if (dev_connected) {
mbed_official 0:a554658735bf 117 return true;
mbed_official 0:a554658735bf 118 }
mbed_official 24:868cbfe611a7 119
samux 5:e48791a1ef18 120 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
mbed_official 0:a554658735bf 121 if ((dev = host->getDevice(i)) != NULL) {
mbed_official 0:a554658735bf 122
Kojto 37:f1e388e7b752 123 if (host->enumerate(dev, this)) {
mbed_official 0:a554658735bf 124 break;
Kojto 37:f1e388e7b752 125 }
mbed_official 24:868cbfe611a7 126
mbed_official 0:a554658735bf 127 if (keyboard_device_found) {
Kojto 37:f1e388e7b752 128 {
Kojto 37:f1e388e7b752 129 /* As this is done in a specific thread
Kojto 37:f1e388e7b752 130 * this lock is taken to avoid to process the device
Kojto 37:f1e388e7b752 131 * disconnect in usb process during the device registering */
Kojto 37:f1e388e7b752 132 USBHost::Lock Lock(host);
mbed_official 24:868cbfe611a7 133
Kojto 37:f1e388e7b752 134 int_in = dev->getEndpoint(keyboard_intf, INTERRUPT_ENDPOINT, IN);
mbed_official 24:868cbfe611a7 135
Kojto 37:f1e388e7b752 136 if (!int_in) {
Kojto 37:f1e388e7b752 137 break;
Kojto 37:f1e388e7b752 138 }
mbed_official 24:868cbfe611a7 139
Kojto 37:f1e388e7b752 140 USB_INFO("New Keyboard device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, keyboard_intf);
Kojto 37:f1e388e7b752 141 dev->setName("Keyboard", keyboard_intf);
Kojto 37:f1e388e7b752 142 host->registerDriver(dev, keyboard_intf, this, &USBHostKeyboard::init);
Kojto 37:f1e388e7b752 143
Kojto 37:f1e388e7b752 144 int_in->attach(this, &USBHostKeyboard::rxHandler);
Kojto 37:f1e388e7b752 145 }
mbed_official 0:a554658735bf 146 host->interruptRead(dev, int_in, report, int_in->getSize(), false);
mbed_official 24:868cbfe611a7 147
mbed_official 0:a554658735bf 148 dev_connected = true;
mbed_official 0:a554658735bf 149 return true;
mbed_official 0:a554658735bf 150 }
mbed_official 0:a554658735bf 151 }
mbed_official 0:a554658735bf 152 }
mbed_official 0:a554658735bf 153 init();
mbed_official 0:a554658735bf 154 return false;
mbed_official 0:a554658735bf 155 }
mbed_official 0:a554658735bf 156
Kojto 37:f1e388e7b752 157 void USBHostKeyboard::rxHandler()
Kojto 37:f1e388e7b752 158 {
mbed_official 0:a554658735bf 159 int len = int_in->getLengthTransferred();
mbed_official 0:a554658735bf 160 int index = (len == 9) ? 1 : 0;
mbed_official 0:a554658735bf 161 int len_listen = int_in->getSize();
mbed_official 0:a554658735bf 162 uint8_t key = 0;
mbed_official 0:a554658735bf 163 if (len == 8 || len == 9) {
mbed_official 0:a554658735bf 164 uint8_t modifier = (report[index] == 4) ? 3 : report[index];
mbed_official 0:a554658735bf 165 len_listen = len;
mbed_official 0:a554658735bf 166 key = keymap[modifier][report[index + 2]];
mbed_official 0:a554658735bf 167 if (key && onKey) {
mbed_official 0:a554658735bf 168 (*onKey)(key);
mbed_official 0:a554658735bf 169 }
samux 5:e48791a1ef18 170 if ((report[index + 2] || modifier) && onKeyCode) {
mbed_official 0:a554658735bf 171 (*onKeyCode)(report[index + 2], modifier);
mbed_official 0:a554658735bf 172 }
mbed_official 0:a554658735bf 173 }
Kojto 37:f1e388e7b752 174 if (dev && int_in) {
mbed_official 0:a554658735bf 175 host->interruptRead(dev, int_in, report, len_listen, false);
Kojto 37:f1e388e7b752 176 }
mbed_official 0:a554658735bf 177 }
mbed_official 0:a554658735bf 178
mbed_official 0:a554658735bf 179 /*virtual*/ void USBHostKeyboard::setVidPid(uint16_t vid, uint16_t pid)
mbed_official 0:a554658735bf 180 {
mbed_official 0:a554658735bf 181 // we don't check VID/PID for keyboard driver
mbed_official 0:a554658735bf 182 }
mbed_official 0:a554658735bf 183
mbed_official 0:a554658735bf 184 /*virtual*/ bool USBHostKeyboard::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
mbed_official 0:a554658735bf 185 {
mbed_official 0:a554658735bf 186 if ((keyboard_intf == -1) &&
Kojto 37:f1e388e7b752 187 (intf_class == HID_CLASS) &&
Kojto 37:f1e388e7b752 188 (intf_subclass == 0x01) &&
Kojto 37:f1e388e7b752 189 (intf_protocol == 0x01)) {
mbed_official 0:a554658735bf 190 keyboard_intf = intf_nb;
mbed_official 0:a554658735bf 191 return true;
mbed_official 0:a554658735bf 192 }
mbed_official 0:a554658735bf 193 return false;
mbed_official 0:a554658735bf 194 }
mbed_official 0:a554658735bf 195
mbed_official 0:a554658735bf 196 /*virtual*/ bool USBHostKeyboard::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
mbed_official 0:a554658735bf 197 {
mbed_official 0:a554658735bf 198 if (intf_nb == keyboard_intf) {
mbed_official 0:a554658735bf 199 if (type == INTERRUPT_ENDPOINT && dir == IN) {
mbed_official 0:a554658735bf 200 keyboard_device_found = true;
mbed_official 0:a554658735bf 201 return true;
mbed_official 0:a554658735bf 202 }
mbed_official 0:a554658735bf 203 }
mbed_official 0:a554658735bf 204 return false;
mbed_official 0:a554658735bf 205 }
mbed_official 0:a554658735bf 206
mbed_official 0:a554658735bf 207 #endif