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
Parent:
8:6463cd1964c0
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 "USBHostMouse.h"
va009039 9:7f9f64cf5ded 18
va009039 9:7f9f64cf5ded 19 #if USBHOST_MOUSE
va009039 9:7f9f64cf5ded 20
va009039 9:7f9f64cf5ded 21 USBHostMouse::USBHostMouse() {
va009039 9:7f9f64cf5ded 22 host = USBHost::getHostInst();
va009039 9:7f9f64cf5ded 23 init();
va009039 9:7f9f64cf5ded 24 }
va009039 9:7f9f64cf5ded 25
va009039 9:7f9f64cf5ded 26 void USBHostMouse::init() {
va009039 9:7f9f64cf5ded 27 dev = NULL;
va009039 9:7f9f64cf5ded 28 int_in = NULL;
va009039 9:7f9f64cf5ded 29 onUpdate = NULL;
va009039 9:7f9f64cf5ded 30 onButtonUpdate = NULL;
va009039 9:7f9f64cf5ded 31 onXUpdate = NULL;
va009039 9:7f9f64cf5ded 32 onYUpdate = NULL;
va009039 9:7f9f64cf5ded 33 onZUpdate = NULL;
va009039 9:7f9f64cf5ded 34 report_id = 0;
va009039 9:7f9f64cf5ded 35 dev_connected = false;
va009039 9:7f9f64cf5ded 36 mouse_device_found = false;
va009039 9:7f9f64cf5ded 37 mouse_intf = -1;
va009039 9:7f9f64cf5ded 38
va009039 9:7f9f64cf5ded 39 buttons = 0;
va009039 9:7f9f64cf5ded 40 x = 0;
va009039 9:7f9f64cf5ded 41 y = 0;
va009039 9:7f9f64cf5ded 42 z = 0;
va009039 9:7f9f64cf5ded 43 }
va009039 9:7f9f64cf5ded 44
va009039 9:7f9f64cf5ded 45 bool USBHostMouse::connected() {
va009039 9:7f9f64cf5ded 46 return dev_connected;
va009039 9:7f9f64cf5ded 47 }
va009039 9:7f9f64cf5ded 48
va009039 9:7f9f64cf5ded 49 bool USBHostMouse::connect() {
va009039 9:7f9f64cf5ded 50
va009039 9:7f9f64cf5ded 51 if (dev_connected) {
va009039 9:7f9f64cf5ded 52 return true;
va009039 9:7f9f64cf5ded 53 }
va009039 9:7f9f64cf5ded 54
va009039 9:7f9f64cf5ded 55 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
va009039 9:7f9f64cf5ded 56 if ((dev = host->getDevice(i)) != NULL) {
va009039 9:7f9f64cf5ded 57
va009039 9:7f9f64cf5ded 58 if(host->enumerate(dev, this))
va009039 9:7f9f64cf5ded 59 break;
va009039 9:7f9f64cf5ded 60
va009039 9:7f9f64cf5ded 61 if (mouse_device_found) {
va009039 9:7f9f64cf5ded 62
va009039 9:7f9f64cf5ded 63 int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
va009039 9:7f9f64cf5ded 64 if (!int_in)
va009039 9:7f9f64cf5ded 65 break;
va009039 9:7f9f64cf5ded 66
va009039 9:7f9f64cf5ded 67 USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf);
va009039 9:7f9f64cf5ded 68 dev->setName("Mouse", mouse_intf);
va009039 9:7f9f64cf5ded 69 host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init);
va009039 9:7f9f64cf5ded 70
va009039 9:7f9f64cf5ded 71 int_in->attach(this, &USBHostMouse::rxHandler);
va009039 9:7f9f64cf5ded 72 host->interruptRead(dev, int_in, report, int_in->getSize(), false);
va009039 9:7f9f64cf5ded 73
va009039 9:7f9f64cf5ded 74 dev_connected = true;
va009039 9:7f9f64cf5ded 75 return true;
va009039 9:7f9f64cf5ded 76 }
va009039 9:7f9f64cf5ded 77 }
va009039 9:7f9f64cf5ded 78 }
va009039 9:7f9f64cf5ded 79 init();
va009039 9:7f9f64cf5ded 80 return false;
va009039 9:7f9f64cf5ded 81 }
va009039 9:7f9f64cf5ded 82
va009039 9:7f9f64cf5ded 83 void USBHostMouse::rxHandler() {
va009039 9:7f9f64cf5ded 84 int len_listen = int_in->getSize();
va009039 9:7f9f64cf5ded 85
va009039 9:7f9f64cf5ded 86 if (onUpdate) {
va009039 9:7f9f64cf5ded 87 (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
va009039 9:7f9f64cf5ded 88 }
va009039 9:7f9f64cf5ded 89
va009039 9:7f9f64cf5ded 90 if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
va009039 9:7f9f64cf5ded 91 (*onButtonUpdate)(report[0] & 0x07);
va009039 9:7f9f64cf5ded 92 }
va009039 9:7f9f64cf5ded 93
va009039 9:7f9f64cf5ded 94 if (onXUpdate && (x != report[1])) {
va009039 9:7f9f64cf5ded 95 (*onXUpdate)(report[1]);
va009039 9:7f9f64cf5ded 96 }
va009039 9:7f9f64cf5ded 97
va009039 9:7f9f64cf5ded 98 if (onYUpdate && (y != report[2])) {
va009039 9:7f9f64cf5ded 99 (*onYUpdate)(report[2]);
va009039 9:7f9f64cf5ded 100 }
va009039 9:7f9f64cf5ded 101
va009039 9:7f9f64cf5ded 102 if (onZUpdate && (z != report[3])) {
va009039 9:7f9f64cf5ded 103 (*onZUpdate)(report[3]);
va009039 9:7f9f64cf5ded 104 }
va009039 9:7f9f64cf5ded 105
va009039 9:7f9f64cf5ded 106 // update mouse state
va009039 9:7f9f64cf5ded 107 buttons = report[0] & 0x07;
va009039 9:7f9f64cf5ded 108 x = report[1];
va009039 9:7f9f64cf5ded 109 y = report[2];
va009039 9:7f9f64cf5ded 110 z = report[3];
va009039 9:7f9f64cf5ded 111
va009039 9:7f9f64cf5ded 112 if (dev)
va009039 9:7f9f64cf5ded 113 host->interruptRead(dev, int_in, report, len_listen, false);
va009039 9:7f9f64cf5ded 114 }
va009039 9:7f9f64cf5ded 115
va009039 9:7f9f64cf5ded 116 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
va009039 9:7f9f64cf5ded 117 {
va009039 9:7f9f64cf5ded 118 // we don't check VID/PID for mouse driver
va009039 9:7f9f64cf5ded 119 }
va009039 9:7f9f64cf5ded 120
va009039 9:7f9f64cf5ded 121 /*virtual*/ bool USBHostMouse::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 122 {
va009039 9:7f9f64cf5ded 123 if ((mouse_intf == -1) &&
va009039 9:7f9f64cf5ded 124 (intf_class == HID_CLASS) &&
va009039 9:7f9f64cf5ded 125 (intf_subclass == 0x01) &&
va009039 9:7f9f64cf5ded 126 (intf_protocol == 0x02)) {
va009039 9:7f9f64cf5ded 127 mouse_intf = intf_nb;
va009039 9:7f9f64cf5ded 128 return true;
va009039 9:7f9f64cf5ded 129 }
va009039 9:7f9f64cf5ded 130 return false;
va009039 9:7f9f64cf5ded 131 }
va009039 9:7f9f64cf5ded 132
va009039 9:7f9f64cf5ded 133 /*virtual*/ bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
va009039 9:7f9f64cf5ded 134 {
va009039 9:7f9f64cf5ded 135 if (intf_nb == mouse_intf) {
va009039 9:7f9f64cf5ded 136 if (type == INTERRUPT_ENDPOINT && dir == IN) {
va009039 9:7f9f64cf5ded 137 mouse_device_found = true;
va009039 9:7f9f64cf5ded 138 return true;
va009039 9:7f9f64cf5ded 139 }
va009039 9:7f9f64cf5ded 140 }
va009039 9:7f9f64cf5ded 141 return false;
va009039 9:7f9f64cf5ded 142 }
va009039 9:7f9f64cf5ded 143
va009039 9:7f9f64cf5ded 144 #endif