Simple USBHost library for Nucleo F446RE/F411RE/F401RE FRDM-KL46Z/KL25Z/F64F LPC4088/LPC1768
Dependents: F401RE-BTstack_example F401RE-USBHostMSD_HelloWorld
Fork of KL46Z-USBHost by
簡易USBホストライブラリです。
official-USBHostの下位互換で対応プログラムを僅かな修正で動かすことが出来ます。
Platforms
- Nucleo F446RE
- Nucleo F411RE
- Nucleo F401RE
- FRDM-K64F
- FRDM-KL46Z
- FRDM-KL25Z
- LPC4088
- LPC1768
Nucleo F446RE/F411RE/F401REのUSB接続方法
ST morpho | USB |
---|---|
U5V (CN10-8) | VBUS (1 RED) |
PA11 (CN10-14) | DM (2 WHITE) |
PA12 (CN10-12) | DP (3 GREEN) |
GND (CN10-20) | GND (4 BLACK) |
Examples
Import programF446RE-USBHostMouse_HelloWorld
USBHostMouse Hello World for ST-Nucleo-F446RE
Import programF401RE-USBHostMSD_HelloWorld
Simple USBHost MSD(USB flash drive) for Nucleo F401RE/FRDM-KL46Z test program
Import programF401RE-USBHostC270_example
Simple USBHost WebCam test program
Import programK64F_USBHostC270_example
Simple USBHost C270 example
Import programF401RE-BTstack_example
BTstack for Nucleo F401RE/FRDM-KL46Z example program
Import programUSBHostRSSI_example
Bluetooth device discovery example program.
Import programKL46Z-USBHostGPS_HelloWorld
Simple USBHost GPS Dongle Receiver for FRDM-KL46Z test program
USBHostHID/USBHostMouse.cpp@23:4ab8bc835303, 2016-05-01 (annotated)
- Committer:
- va009039
- Date:
- Sun May 01 03:18:11 2016 +0000
- Revision:
- 23:4ab8bc835303
- Parent:
- 13:8774c07f12a5
add ST-Nucleo-F446RE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 |
13:8774c07f12a5 | 1 | /* mbed USBHost Library |
va009039 |
13:8774c07f12a5 | 2 | * Copyright (c) 2006-2013 ARM Limited |
va009039 |
13:8774c07f12a5 | 3 | * |
va009039 |
13:8774c07f12a5 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
va009039 |
13:8774c07f12a5 | 5 | * you may not use this file except in compliance with the License. |
va009039 |
13:8774c07f12a5 | 6 | * You may obtain a copy of the License at |
va009039 |
13:8774c07f12a5 | 7 | * |
va009039 |
13:8774c07f12a5 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
va009039 |
13:8774c07f12a5 | 9 | * |
va009039 |
13:8774c07f12a5 | 10 | * Unless required by applicable law or agreed to in writing, software |
va009039 |
13:8774c07f12a5 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
va009039 |
13:8774c07f12a5 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
va009039 |
13:8774c07f12a5 | 13 | * See the License for the specific language governing permissions and |
va009039 |
13:8774c07f12a5 | 14 | * limitations under the License. |
va009039 |
13:8774c07f12a5 | 15 | */ |
va009039 |
13:8774c07f12a5 | 16 | |
va009039 |
13:8774c07f12a5 | 17 | #include "USBHostMouse.h" |
va009039 |
13:8774c07f12a5 | 18 | |
va009039 |
13:8774c07f12a5 | 19 | #if USBHOST_MOUSE |
va009039 |
13:8774c07f12a5 | 20 | |
va009039 |
13:8774c07f12a5 | 21 | USBHostMouse::USBHostMouse() { |
va009039 |
13:8774c07f12a5 | 22 | host = USBHost::getHostInst(); |
va009039 |
13:8774c07f12a5 | 23 | init(); |
va009039 |
13:8774c07f12a5 | 24 | } |
va009039 |
13:8774c07f12a5 | 25 | |
va009039 |
13:8774c07f12a5 | 26 | void USBHostMouse::init() { |
va009039 |
13:8774c07f12a5 | 27 | dev = NULL; |
va009039 |
13:8774c07f12a5 | 28 | int_in = NULL; |
va009039 |
13:8774c07f12a5 | 29 | onUpdate = NULL; |
va009039 |
13:8774c07f12a5 | 30 | onButtonUpdate = NULL; |
va009039 |
13:8774c07f12a5 | 31 | onXUpdate = NULL; |
va009039 |
13:8774c07f12a5 | 32 | onYUpdate = NULL; |
va009039 |
13:8774c07f12a5 | 33 | onZUpdate = NULL; |
va009039 |
13:8774c07f12a5 | 34 | report_id = 0; |
va009039 |
13:8774c07f12a5 | 35 | dev_connected = false; |
va009039 |
13:8774c07f12a5 | 36 | mouse_device_found = false; |
va009039 |
13:8774c07f12a5 | 37 | mouse_intf = -1; |
va009039 |
13:8774c07f12a5 | 38 | |
va009039 |
13:8774c07f12a5 | 39 | buttons = 0; |
va009039 |
13:8774c07f12a5 | 40 | x = 0; |
va009039 |
13:8774c07f12a5 | 41 | y = 0; |
va009039 |
13:8774c07f12a5 | 42 | z = 0; |
va009039 |
13:8774c07f12a5 | 43 | } |
va009039 |
13:8774c07f12a5 | 44 | |
va009039 |
13:8774c07f12a5 | 45 | bool USBHostMouse::connected() { |
va009039 |
13:8774c07f12a5 | 46 | return dev_connected; |
va009039 |
13:8774c07f12a5 | 47 | } |
va009039 |
13:8774c07f12a5 | 48 | |
va009039 |
13:8774c07f12a5 | 49 | bool USBHostMouse::connect() { |
va009039 |
13:8774c07f12a5 | 50 | |
va009039 |
13:8774c07f12a5 | 51 | if (dev_connected) { |
va009039 |
13:8774c07f12a5 | 52 | return true; |
va009039 |
13:8774c07f12a5 | 53 | } |
va009039 |
13:8774c07f12a5 | 54 | |
va009039 |
13:8774c07f12a5 | 55 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
va009039 |
13:8774c07f12a5 | 56 | if ((dev = host->getDevice(i)) != NULL) { |
va009039 |
13:8774c07f12a5 | 57 | |
va009039 |
13:8774c07f12a5 | 58 | if(host->enumerate(dev, this)) |
va009039 |
13:8774c07f12a5 | 59 | break; |
va009039 |
13:8774c07f12a5 | 60 | |
va009039 |
13:8774c07f12a5 | 61 | if (mouse_device_found) { |
va009039 |
13:8774c07f12a5 | 62 | |
va009039 |
13:8774c07f12a5 | 63 | int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN); |
va009039 |
13:8774c07f12a5 | 64 | if (!int_in) |
va009039 |
13:8774c07f12a5 | 65 | break; |
va009039 |
13:8774c07f12a5 | 66 | |
va009039 |
13:8774c07f12a5 | 67 | USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf); |
va009039 |
13:8774c07f12a5 | 68 | dev->setName("Mouse", mouse_intf); |
va009039 |
13:8774c07f12a5 | 69 | host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init); |
va009039 |
13:8774c07f12a5 | 70 | |
va009039 |
13:8774c07f12a5 | 71 | int_in->attach(this, &USBHostMouse::rxHandler); |
va009039 |
13:8774c07f12a5 | 72 | host->interruptRead(dev, int_in, report, int_in->getSize(), false); |
va009039 |
13:8774c07f12a5 | 73 | |
va009039 |
13:8774c07f12a5 | 74 | dev_connected = true; |
va009039 |
13:8774c07f12a5 | 75 | return true; |
va009039 |
13:8774c07f12a5 | 76 | } |
va009039 |
13:8774c07f12a5 | 77 | } |
va009039 |
13:8774c07f12a5 | 78 | } |
va009039 |
13:8774c07f12a5 | 79 | init(); |
va009039 |
13:8774c07f12a5 | 80 | return false; |
va009039 |
13:8774c07f12a5 | 81 | } |
va009039 |
13:8774c07f12a5 | 82 | |
va009039 |
13:8774c07f12a5 | 83 | void USBHostMouse::rxHandler() { |
va009039 |
13:8774c07f12a5 | 84 | int len_listen = int_in->getSize(); |
va009039 |
13:8774c07f12a5 | 85 | |
va009039 |
13:8774c07f12a5 | 86 | if (onUpdate) { |
va009039 |
13:8774c07f12a5 | 87 | (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]); |
va009039 |
13:8774c07f12a5 | 88 | } |
va009039 |
13:8774c07f12a5 | 89 | |
va009039 |
13:8774c07f12a5 | 90 | if (onButtonUpdate && (buttons != (report[0] & 0x07))) { |
va009039 |
13:8774c07f12a5 | 91 | (*onButtonUpdate)(report[0] & 0x07); |
va009039 |
13:8774c07f12a5 | 92 | } |
va009039 |
13:8774c07f12a5 | 93 | |
va009039 |
13:8774c07f12a5 | 94 | if (onXUpdate && (x != report[1])) { |
va009039 |
13:8774c07f12a5 | 95 | (*onXUpdate)(report[1]); |
va009039 |
13:8774c07f12a5 | 96 | } |
va009039 |
13:8774c07f12a5 | 97 | |
va009039 |
13:8774c07f12a5 | 98 | if (onYUpdate && (y != report[2])) { |
va009039 |
13:8774c07f12a5 | 99 | (*onYUpdate)(report[2]); |
va009039 |
13:8774c07f12a5 | 100 | } |
va009039 |
13:8774c07f12a5 | 101 | |
va009039 |
13:8774c07f12a5 | 102 | if (onZUpdate && (z != report[3])) { |
va009039 |
13:8774c07f12a5 | 103 | (*onZUpdate)(report[3]); |
va009039 |
13:8774c07f12a5 | 104 | } |
va009039 |
13:8774c07f12a5 | 105 | |
va009039 |
13:8774c07f12a5 | 106 | // update mouse state |
va009039 |
13:8774c07f12a5 | 107 | buttons = report[0] & 0x07; |
va009039 |
13:8774c07f12a5 | 108 | x = report[1]; |
va009039 |
13:8774c07f12a5 | 109 | y = report[2]; |
va009039 |
13:8774c07f12a5 | 110 | z = report[3]; |
va009039 |
13:8774c07f12a5 | 111 | |
va009039 |
13:8774c07f12a5 | 112 | if (dev) |
va009039 |
13:8774c07f12a5 | 113 | host->interruptRead(dev, int_in, report, len_listen, false); |
va009039 |
13:8774c07f12a5 | 114 | } |
va009039 |
13:8774c07f12a5 | 115 | |
va009039 |
13:8774c07f12a5 | 116 | /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid) |
va009039 |
13:8774c07f12a5 | 117 | { |
va009039 |
13:8774c07f12a5 | 118 | // we don't check VID/PID for mouse driver |
va009039 |
13:8774c07f12a5 | 119 | } |
va009039 |
13:8774c07f12a5 | 120 | |
va009039 |
13:8774c07f12a5 | 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 |
13:8774c07f12a5 | 122 | { |
va009039 |
13:8774c07f12a5 | 123 | if ((mouse_intf == -1) && |
va009039 |
13:8774c07f12a5 | 124 | (intf_class == HID_CLASS) && |
va009039 |
13:8774c07f12a5 | 125 | (intf_subclass == 0x01) && |
va009039 |
13:8774c07f12a5 | 126 | (intf_protocol == 0x02)) { |
va009039 |
13:8774c07f12a5 | 127 | mouse_intf = intf_nb; |
va009039 |
13:8774c07f12a5 | 128 | return true; |
va009039 |
13:8774c07f12a5 | 129 | } |
va009039 |
13:8774c07f12a5 | 130 | return false; |
va009039 |
13:8774c07f12a5 | 131 | } |
va009039 |
13:8774c07f12a5 | 132 | |
va009039 |
13:8774c07f12a5 | 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 |
13:8774c07f12a5 | 134 | { |
va009039 |
13:8774c07f12a5 | 135 | if (intf_nb == mouse_intf) { |
va009039 |
13:8774c07f12a5 | 136 | if (type == INTERRUPT_ENDPOINT && dir == IN) { |
va009039 |
13:8774c07f12a5 | 137 | mouse_device_found = true; |
va009039 |
13:8774c07f12a5 | 138 | return true; |
va009039 |
13:8774c07f12a5 | 139 | } |
va009039 |
13:8774c07f12a5 | 140 | } |
va009039 |
13:8774c07f12a5 | 141 | return false; |
va009039 |
13:8774c07f12a5 | 142 | } |
va009039 |
13:8774c07f12a5 | 143 | |
va009039 |
13:8774c07f12a5 | 144 | #endif |