Usb read

Dependencies:   FATFileSystem

Fork of F401RE-USBHost by Norimasa Okamoto

Committer:
va009039
Date:
Mon Feb 03 13:00:16 2014 +0000
Revision:
9:7f9f64cf5ded
add interrupt write transfer.

Who changed what in which revision?

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