Library to use Arduino USB host shield on mbed
ArduinoのUSB Host Shield 2.0をmbedで使えるようにしたライブラリです。
大体のコードがArduinoからそのまま移植可能です。
Arduino UNOやMega用のホストシールド以外にもミニサイズのホストシールドでも使用可能です
シールドについて
3.3VのI/O用にシールドの改造が必要になりますがネット上に記事がたくさんあるのでそちらを参考にしてください
接続例
使い方
Arduinoのコードと違うのはUSBのインスタンスの宣言部分のみです。
ピンを自分で指定できるようにしたので使いやすくなりました。
仕様
- Arduinoのmillis関数、micros関数の移植のために内部でTimerクラスを使用しています。
main.cpp
#include "mbed.h" #include <PS3BT.h> #include <usbhub.h> Serial pc(USBTX, USBRX, 115200); //Nucleo f303k8用 USB Usb(A6, A5, A4, A3, A2); // mosi, miso, sclk, ssel, intr BTD Btd(&Usb); PS3BT PS3(&Btd); int main() { bool printAngle = false; if (Usb.Init() == -1) { pc.printf("\r\nOSC did not start"); while (1); // Halt } pc.printf("\r\nPS3 USB Library Started"); while (1) { Usb.Task(); if (PS3.PS3Connected || PS3.PS3NavigationConnected) { if (PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) { pc.printf("\r\nLeftHatX: %d", PS3.getAnalogHat(LeftHatX)); pc.printf("\tLeftHatY: %d", PS3.getAnalogHat(LeftHatY)); if (PS3.PS3Connected) { // The Navigation controller only have one joystick pc.printf("\tRightHatX: %d", PS3.getAnalogHat(RightHatX)); pc.printf("\tRightHatY: %d", PS3.getAnalogHat(RightHatY)); } } // Analog button values can be read from almost all buttons if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) { pc.printf("\r\nL2: %d", PS3.getAnalogButton(L2)); if (!PS3.PS3NavigationConnected) { pc.printf("\tR2: %d", PS3.getAnalogButton(R2)); } } if (PS3.getButtonClick(PS)) { PS3.disconnect(); pc.printf("\r\nPS"); } if (PS3.getButtonClick(TRIANGLE)) pc.printf("\r\nTriangle"); if (PS3.getButtonClick(CIRCLE)) pc.printf("\r\nCircle"); if (PS3.getButtonClick(CROSS)) pc.printf("\r\nCross"); if (PS3.getButtonClick(SQUARE)) pc.printf("\r\nSquare"); if (PS3.getButtonClick(UP)) { pc.printf("\r\nUp"); PS3.setLedOff(); PS3.setLedOn(CONTROLLER_LED4); } if (PS3.getButtonClick(RIGHT)) { pc.printf("\r\nRight"); PS3.setLedOff(); PS3.setLedOn(CONTROLLER_LED1); } if (PS3.getButtonClick(DOWN)) { pc.printf("\r\nDown"); PS3.setLedOff(); PS3.setLedOn(CONTROLLER_LED2); } if (PS3.getButtonClick(LEFT)) { pc.printf("\r\nLeft"); PS3.setLedOff(); PS3.setLedOn(CONTROLLER_LED3); } if (PS3.getButtonClick(L1)) pc.printf("\r\nL1"); if (PS3.getButtonClick(L3)) pc.printf("\r\nL3"); if (PS3.getButtonClick(R1)) pc.printf("\r\nR1"); if (PS3.getButtonClick(R3)) pc.printf("\r\nR3"); if (PS3.getButtonClick(SELECT)) { pc.printf("\r\nSelect - "); PS3.printStatusString(); } if (PS3.getButtonClick(START)) { pc.printf("\r\nStart"); printAngle = !printAngle; } if (printAngle) { pc.printf("\r\nPitch: %.3lf", PS3.getAngle(Pitch)); pc.printf("\tRoll: %.3lf", PS3.getAngle(Roll)); } } else { pc.printf("not connect\n"); } } }
USB_Host/hidcomposite.cpp@1:da31140f2a1c, 2020-05-02 (annotated)
- Committer:
- robo_ichinoseki_a
- Date:
- Sat May 02 05:56:48 2020 +0000
- Revision:
- 1:da31140f2a1c
- Parent:
- 0:b1ce54272580
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kotakku | 0:b1ce54272580 | 1 | /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved. |
kotakku | 0:b1ce54272580 | 2 | |
kotakku | 0:b1ce54272580 | 3 | This software may be distributed and modified under the terms of the GNU |
kotakku | 0:b1ce54272580 | 4 | General Public License version 2 (GPL2) as published by the Free Software |
kotakku | 0:b1ce54272580 | 5 | Foundation and appearing in the file GPL2.TXT included in the packaging of |
kotakku | 0:b1ce54272580 | 6 | this file. Please note that GPL2 Section 2[b] requires that all works based |
kotakku | 0:b1ce54272580 | 7 | on this software must also be made publicly available under the terms of |
kotakku | 0:b1ce54272580 | 8 | the GPL2 ("Copyleft"). |
kotakku | 0:b1ce54272580 | 9 | |
kotakku | 0:b1ce54272580 | 10 | Contact information |
kotakku | 0:b1ce54272580 | 11 | ------------------- |
kotakku | 0:b1ce54272580 | 12 | |
kotakku | 0:b1ce54272580 | 13 | Circuits At Home, LTD |
kotakku | 0:b1ce54272580 | 14 | Web : http://www.circuitsathome.com |
kotakku | 0:b1ce54272580 | 15 | e-mail : support@circuitsathome.com |
kotakku | 0:b1ce54272580 | 16 | */ |
kotakku | 0:b1ce54272580 | 17 | |
kotakku | 0:b1ce54272580 | 18 | #include "hidcomposite.h" |
kotakku | 0:b1ce54272580 | 19 | |
kotakku | 0:b1ce54272580 | 20 | HIDComposite::HIDComposite(USB *p) : |
kotakku | 0:b1ce54272580 | 21 | USBHID(p), |
kotakku | 0:b1ce54272580 | 22 | qNextPollTime(0), |
kotakku | 0:b1ce54272580 | 23 | pollInterval(0), |
kotakku | 0:b1ce54272580 | 24 | bPollEnable(false), |
kotakku | 0:b1ce54272580 | 25 | bHasReportId(false) { |
kotakku | 0:b1ce54272580 | 26 | Initialize(); |
kotakku | 0:b1ce54272580 | 27 | |
kotakku | 0:b1ce54272580 | 28 | if(pUsb) |
kotakku | 0:b1ce54272580 | 29 | pUsb->RegisterDeviceClass(this); |
kotakku | 0:b1ce54272580 | 30 | } |
kotakku | 0:b1ce54272580 | 31 | |
kotakku | 0:b1ce54272580 | 32 | uint16_t HIDComposite::GetHidClassDescrLen(uint8_t type, uint8_t num) { |
kotakku | 0:b1ce54272580 | 33 | for(uint8_t i = 0, n = 0; i < HID_MAX_HID_CLASS_DESCRIPTORS; i++) { |
kotakku | 0:b1ce54272580 | 34 | if(descrInfo[i].bDescrType == type) { |
kotakku | 0:b1ce54272580 | 35 | if(n == num) |
kotakku | 0:b1ce54272580 | 36 | return descrInfo[i].wDescriptorLength; |
kotakku | 0:b1ce54272580 | 37 | n++; |
kotakku | 0:b1ce54272580 | 38 | } |
kotakku | 0:b1ce54272580 | 39 | } |
kotakku | 0:b1ce54272580 | 40 | return 0; |
kotakku | 0:b1ce54272580 | 41 | } |
kotakku | 0:b1ce54272580 | 42 | |
kotakku | 0:b1ce54272580 | 43 | void HIDComposite::Initialize() { |
kotakku | 0:b1ce54272580 | 44 | for(uint8_t i = 0; i < MAX_REPORT_PARSERS; i++) { |
kotakku | 0:b1ce54272580 | 45 | rptParsers[i].rptId = 0; |
kotakku | 0:b1ce54272580 | 46 | rptParsers[i].rptParser = NULL; |
kotakku | 0:b1ce54272580 | 47 | } |
kotakku | 0:b1ce54272580 | 48 | for(uint8_t i = 0; i < HID_MAX_HID_CLASS_DESCRIPTORS; i++) { |
kotakku | 0:b1ce54272580 | 49 | descrInfo[i].bDescrType = 0; |
kotakku | 0:b1ce54272580 | 50 | descrInfo[i].wDescriptorLength = 0; |
kotakku | 0:b1ce54272580 | 51 | } |
kotakku | 0:b1ce54272580 | 52 | for(uint8_t i = 0; i < maxHidInterfaces; i++) { |
kotakku | 0:b1ce54272580 | 53 | hidInterfaces[i].bmInterface = 0; |
kotakku | 0:b1ce54272580 | 54 | hidInterfaces[i].bmProtocol = 0; |
kotakku | 0:b1ce54272580 | 55 | |
kotakku | 0:b1ce54272580 | 56 | for(uint8_t j = 0; j < maxEpPerInterface; j++) |
kotakku | 0:b1ce54272580 | 57 | hidInterfaces[i].epIndex[j] = 0; |
kotakku | 0:b1ce54272580 | 58 | } |
kotakku | 0:b1ce54272580 | 59 | for(uint8_t i = 0; i < totalEndpoints; i++) { |
kotakku | 0:b1ce54272580 | 60 | epInfo[i].epAddr = 0; |
kotakku | 0:b1ce54272580 | 61 | epInfo[i].maxPktSize = (i) ? 0 : 8; |
kotakku | 0:b1ce54272580 | 62 | epInfo[i].bmSndToggle = 0; |
kotakku | 0:b1ce54272580 | 63 | epInfo[i].bmRcvToggle = 0; |
kotakku | 0:b1ce54272580 | 64 | epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER; |
kotakku | 0:b1ce54272580 | 65 | } |
kotakku | 0:b1ce54272580 | 66 | bNumEP = 1; |
kotakku | 0:b1ce54272580 | 67 | bNumIface = 0; |
kotakku | 0:b1ce54272580 | 68 | bConfNum = 0; |
kotakku | 0:b1ce54272580 | 69 | pollInterval = 0; |
kotakku | 0:b1ce54272580 | 70 | } |
kotakku | 0:b1ce54272580 | 71 | |
kotakku | 0:b1ce54272580 | 72 | bool HIDComposite::SetReportParser(uint8_t id, HIDReportParser *prs) { |
kotakku | 0:b1ce54272580 | 73 | for(uint8_t i = 0; i < MAX_REPORT_PARSERS; i++) { |
kotakku | 0:b1ce54272580 | 74 | if(rptParsers[i].rptId == 0 && rptParsers[i].rptParser == NULL) { |
kotakku | 0:b1ce54272580 | 75 | rptParsers[i].rptId = id; |
kotakku | 0:b1ce54272580 | 76 | rptParsers[i].rptParser = prs; |
kotakku | 0:b1ce54272580 | 77 | return true; |
kotakku | 0:b1ce54272580 | 78 | } |
kotakku | 0:b1ce54272580 | 79 | } |
kotakku | 0:b1ce54272580 | 80 | return false; |
kotakku | 0:b1ce54272580 | 81 | } |
kotakku | 0:b1ce54272580 | 82 | |
kotakku | 0:b1ce54272580 | 83 | HIDReportParser* HIDComposite::GetReportParser(uint8_t id) { |
kotakku | 0:b1ce54272580 | 84 | if(!bHasReportId) |
kotakku | 0:b1ce54272580 | 85 | return ((rptParsers[0].rptParser) ? rptParsers[0].rptParser : NULL); |
kotakku | 0:b1ce54272580 | 86 | |
kotakku | 0:b1ce54272580 | 87 | for(uint8_t i = 0; i < MAX_REPORT_PARSERS; i++) { |
kotakku | 0:b1ce54272580 | 88 | if(rptParsers[i].rptId == id) |
kotakku | 0:b1ce54272580 | 89 | return rptParsers[i].rptParser; |
kotakku | 0:b1ce54272580 | 90 | } |
kotakku | 0:b1ce54272580 | 91 | return NULL; |
kotakku | 0:b1ce54272580 | 92 | } |
kotakku | 0:b1ce54272580 | 93 | |
kotakku | 0:b1ce54272580 | 94 | uint8_t HIDComposite::Init(uint8_t parent, uint8_t port, bool lowspeed) { |
kotakku | 0:b1ce54272580 | 95 | const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR); |
kotakku | 0:b1ce54272580 | 96 | |
kotakku | 0:b1ce54272580 | 97 | uint8_t buf[constBufSize]; |
kotakku | 0:b1ce54272580 | 98 | USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf); |
kotakku | 0:b1ce54272580 | 99 | uint8_t rcode; |
kotakku | 0:b1ce54272580 | 100 | UsbDevice *p = NULL; |
kotakku | 0:b1ce54272580 | 101 | EpInfo *oldep_ptr = NULL; |
kotakku | 0:b1ce54272580 | 102 | uint8_t len = 0; |
kotakku | 0:b1ce54272580 | 103 | |
kotakku | 0:b1ce54272580 | 104 | uint8_t num_of_conf; // number of configurations |
kotakku | 0:b1ce54272580 | 105 | //uint8_t num_of_intf; // number of interfaces |
kotakku | 0:b1ce54272580 | 106 | |
kotakku | 0:b1ce54272580 | 107 | AddressPool &addrPool = pUsb->GetAddressPool(); |
kotakku | 0:b1ce54272580 | 108 | |
kotakku | 0:b1ce54272580 | 109 | USBTRACE("HU Init\r\n"); |
kotakku | 0:b1ce54272580 | 110 | |
kotakku | 0:b1ce54272580 | 111 | if(bAddress) |
kotakku | 0:b1ce54272580 | 112 | return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE; |
kotakku | 0:b1ce54272580 | 113 | |
kotakku | 0:b1ce54272580 | 114 | // Get pointer to pseudo device with address 0 assigned |
kotakku | 0:b1ce54272580 | 115 | p = addrPool.GetUsbDevicePtr(0); |
kotakku | 0:b1ce54272580 | 116 | |
kotakku | 0:b1ce54272580 | 117 | if(!p) |
kotakku | 0:b1ce54272580 | 118 | return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL; |
kotakku | 0:b1ce54272580 | 119 | |
kotakku | 0:b1ce54272580 | 120 | if(!p->epinfo) { |
kotakku | 0:b1ce54272580 | 121 | USBTRACE("epinfo\r\n"); |
kotakku | 0:b1ce54272580 | 122 | return USB_ERROR_EPINFO_IS_NULL; |
kotakku | 0:b1ce54272580 | 123 | } |
kotakku | 0:b1ce54272580 | 124 | |
kotakku | 0:b1ce54272580 | 125 | // Save old pointer to EP_RECORD of address 0 |
kotakku | 0:b1ce54272580 | 126 | oldep_ptr = p->epinfo; |
kotakku | 0:b1ce54272580 | 127 | |
kotakku | 0:b1ce54272580 | 128 | // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence |
kotakku | 0:b1ce54272580 | 129 | p->epinfo = epInfo; |
kotakku | 0:b1ce54272580 | 130 | |
kotakku | 0:b1ce54272580 | 131 | p->lowspeed = lowspeed; |
kotakku | 0:b1ce54272580 | 132 | |
kotakku | 0:b1ce54272580 | 133 | // Get device descriptor |
kotakku | 0:b1ce54272580 | 134 | rcode = pUsb->getDevDescr(0, 0, 8, (uint8_t*)buf); |
kotakku | 0:b1ce54272580 | 135 | |
kotakku | 0:b1ce54272580 | 136 | if(!rcode) |
kotakku | 0:b1ce54272580 | 137 | len = (buf[0] > constBufSize) ? constBufSize : buf[0]; |
kotakku | 0:b1ce54272580 | 138 | |
kotakku | 0:b1ce54272580 | 139 | if(rcode) { |
kotakku | 0:b1ce54272580 | 140 | // Restore p->epinfo |
kotakku | 0:b1ce54272580 | 141 | p->epinfo = oldep_ptr; |
kotakku | 0:b1ce54272580 | 142 | |
kotakku | 0:b1ce54272580 | 143 | goto FailGetDevDescr; |
kotakku | 0:b1ce54272580 | 144 | } |
kotakku | 0:b1ce54272580 | 145 | |
kotakku | 0:b1ce54272580 | 146 | // Restore p->epinfo |
kotakku | 0:b1ce54272580 | 147 | p->epinfo = oldep_ptr; |
kotakku | 0:b1ce54272580 | 148 | |
kotakku | 0:b1ce54272580 | 149 | // Allocate new address according to device class |
kotakku | 0:b1ce54272580 | 150 | bAddress = addrPool.AllocAddress(parent, false, port); |
kotakku | 0:b1ce54272580 | 151 | |
kotakku | 0:b1ce54272580 | 152 | if(!bAddress) |
kotakku | 0:b1ce54272580 | 153 | return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL; |
kotakku | 0:b1ce54272580 | 154 | |
kotakku | 0:b1ce54272580 | 155 | // Extract Max Packet Size from the device descriptor |
kotakku | 0:b1ce54272580 | 156 | epInfo[0].maxPktSize = udd->bMaxPacketSize0; |
kotakku | 0:b1ce54272580 | 157 | |
kotakku | 0:b1ce54272580 | 158 | // Assign new address to the device |
kotakku | 0:b1ce54272580 | 159 | rcode = pUsb->setAddr(0, 0, bAddress); |
kotakku | 0:b1ce54272580 | 160 | |
kotakku | 0:b1ce54272580 | 161 | if(rcode) { |
kotakku | 0:b1ce54272580 | 162 | p->lowspeed = false; |
kotakku | 0:b1ce54272580 | 163 | addrPool.FreeAddress(bAddress); |
kotakku | 0:b1ce54272580 | 164 | bAddress = 0; |
kotakku | 0:b1ce54272580 | 165 | USBTRACE2("setAddr:", rcode); |
kotakku | 0:b1ce54272580 | 166 | return rcode; |
kotakku | 0:b1ce54272580 | 167 | } |
kotakku | 0:b1ce54272580 | 168 | |
kotakku | 0:b1ce54272580 | 169 | //delay(2); //per USB 2.0 sect.9.2.6.3 |
kotakku | 0:b1ce54272580 | 170 | |
kotakku | 0:b1ce54272580 | 171 | USBTRACE2("Addr:", bAddress); |
kotakku | 0:b1ce54272580 | 172 | |
kotakku | 0:b1ce54272580 | 173 | p->lowspeed = false; |
kotakku | 0:b1ce54272580 | 174 | |
kotakku | 0:b1ce54272580 | 175 | p = addrPool.GetUsbDevicePtr(bAddress); |
kotakku | 0:b1ce54272580 | 176 | |
kotakku | 0:b1ce54272580 | 177 | if(!p) |
kotakku | 0:b1ce54272580 | 178 | return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL; |
kotakku | 0:b1ce54272580 | 179 | |
kotakku | 0:b1ce54272580 | 180 | p->lowspeed = lowspeed; |
kotakku | 0:b1ce54272580 | 181 | |
kotakku | 0:b1ce54272580 | 182 | if(len) |
kotakku | 0:b1ce54272580 | 183 | rcode = pUsb->getDevDescr(bAddress, 0, len, (uint8_t*)buf); |
kotakku | 0:b1ce54272580 | 184 | |
kotakku | 0:b1ce54272580 | 185 | if(rcode) |
kotakku | 0:b1ce54272580 | 186 | goto FailGetDevDescr; |
kotakku | 0:b1ce54272580 | 187 | |
kotakku | 0:b1ce54272580 | 188 | VID = udd->idVendor; // Can be used by classes that inherits this class to check the VID and PID of the connected device |
kotakku | 0:b1ce54272580 | 189 | PID = udd->idProduct; |
kotakku | 0:b1ce54272580 | 190 | |
kotakku | 0:b1ce54272580 | 191 | num_of_conf = udd->bNumConfigurations; |
kotakku | 0:b1ce54272580 | 192 | |
kotakku | 0:b1ce54272580 | 193 | // Assign epInfo to epinfo pointer |
kotakku | 0:b1ce54272580 | 194 | rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo); |
kotakku | 0:b1ce54272580 | 195 | |
kotakku | 0:b1ce54272580 | 196 | if(rcode) |
kotakku | 0:b1ce54272580 | 197 | goto FailSetDevTblEntry; |
kotakku | 0:b1ce54272580 | 198 | |
kotakku | 0:b1ce54272580 | 199 | USBTRACE2("NC:", num_of_conf); |
kotakku | 0:b1ce54272580 | 200 | |
kotakku | 0:b1ce54272580 | 201 | for(uint8_t i = 0; i < num_of_conf; i++) { |
kotakku | 0:b1ce54272580 | 202 | //HexDumper<USBReadParser, uint16_t, uint16_t> HexDump; |
kotakku | 0:b1ce54272580 | 203 | ConfigDescParser<USB_CLASS_HID, 0, 0, |
kotakku | 0:b1ce54272580 | 204 | CP_MASK_COMPARE_CLASS> confDescrParser(this); |
kotakku | 0:b1ce54272580 | 205 | |
kotakku | 0:b1ce54272580 | 206 | //rcode = pUsb->getConfDescr(bAddress, 0, i, &HexDump); |
kotakku | 0:b1ce54272580 | 207 | rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser); |
kotakku | 0:b1ce54272580 | 208 | |
kotakku | 0:b1ce54272580 | 209 | if(rcode) |
kotakku | 0:b1ce54272580 | 210 | goto FailGetConfDescr; |
kotakku | 0:b1ce54272580 | 211 | |
kotakku | 0:b1ce54272580 | 212 | if(bNumEP > 1) |
kotakku | 0:b1ce54272580 | 213 | break; |
kotakku | 0:b1ce54272580 | 214 | } // for |
kotakku | 0:b1ce54272580 | 215 | |
kotakku | 0:b1ce54272580 | 216 | if(bNumEP < 2) |
kotakku | 0:b1ce54272580 | 217 | return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED; |
kotakku | 0:b1ce54272580 | 218 | |
kotakku | 0:b1ce54272580 | 219 | // Assign epInfo to epinfo pointer |
kotakku | 0:b1ce54272580 | 220 | rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo); |
kotakku | 0:b1ce54272580 | 221 | |
kotakku | 0:b1ce54272580 | 222 | USBTRACE2("Cnf:", bConfNum); |
kotakku | 0:b1ce54272580 | 223 | |
kotakku | 0:b1ce54272580 | 224 | // Set Configuration Value |
kotakku | 0:b1ce54272580 | 225 | rcode = pUsb->setConf(bAddress, 0, bConfNum); |
kotakku | 0:b1ce54272580 | 226 | |
kotakku | 0:b1ce54272580 | 227 | if(rcode) |
kotakku | 0:b1ce54272580 | 228 | goto FailSetConfDescr; |
kotakku | 0:b1ce54272580 | 229 | |
kotakku | 0:b1ce54272580 | 230 | USBTRACE2("NumIface:", bNumIface); |
kotakku | 0:b1ce54272580 | 231 | |
kotakku | 0:b1ce54272580 | 232 | for(uint8_t i = 0; i < bNumIface; i++) { |
kotakku | 0:b1ce54272580 | 233 | if(hidInterfaces[i].epIndex[epInterruptInIndex] == 0) |
kotakku | 0:b1ce54272580 | 234 | continue; |
kotakku | 0:b1ce54272580 | 235 | |
kotakku | 0:b1ce54272580 | 236 | USBTRACE2("SetIdle:", hidInterfaces[i].bmInterface); |
kotakku | 0:b1ce54272580 | 237 | |
kotakku | 0:b1ce54272580 | 238 | rcode = SetIdle(hidInterfaces[i].bmInterface, 0, 0); |
kotakku | 0:b1ce54272580 | 239 | |
kotakku | 0:b1ce54272580 | 240 | if(rcode && rcode != hrSTALL) |
kotakku | 0:b1ce54272580 | 241 | goto FailSetIdle; |
kotakku | 0:b1ce54272580 | 242 | } |
kotakku | 0:b1ce54272580 | 243 | |
kotakku | 0:b1ce54272580 | 244 | USBTRACE("HU configured\r\n"); |
kotakku | 0:b1ce54272580 | 245 | |
kotakku | 0:b1ce54272580 | 246 | OnInitSuccessful(); |
kotakku | 0:b1ce54272580 | 247 | |
kotakku | 0:b1ce54272580 | 248 | bPollEnable = true; |
kotakku | 0:b1ce54272580 | 249 | return 0; |
kotakku | 0:b1ce54272580 | 250 | |
kotakku | 0:b1ce54272580 | 251 | FailGetDevDescr: |
kotakku | 0:b1ce54272580 | 252 | #ifdef DEBUG_USB_HOST |
kotakku | 0:b1ce54272580 | 253 | NotifyFailGetDevDescr(); |
kotakku | 0:b1ce54272580 | 254 | goto Fail; |
kotakku | 0:b1ce54272580 | 255 | #endif |
kotakku | 0:b1ce54272580 | 256 | |
kotakku | 0:b1ce54272580 | 257 | FailSetDevTblEntry: |
kotakku | 0:b1ce54272580 | 258 | #ifdef DEBUG_USB_HOST |
kotakku | 0:b1ce54272580 | 259 | NotifyFailSetDevTblEntry(); |
kotakku | 0:b1ce54272580 | 260 | goto Fail; |
kotakku | 0:b1ce54272580 | 261 | #endif |
kotakku | 0:b1ce54272580 | 262 | |
kotakku | 0:b1ce54272580 | 263 | FailGetConfDescr: |
kotakku | 0:b1ce54272580 | 264 | #ifdef DEBUG_USB_HOST |
kotakku | 0:b1ce54272580 | 265 | NotifyFailGetConfDescr(); |
kotakku | 0:b1ce54272580 | 266 | goto Fail; |
kotakku | 0:b1ce54272580 | 267 | #endif |
kotakku | 0:b1ce54272580 | 268 | |
kotakku | 0:b1ce54272580 | 269 | FailSetConfDescr: |
kotakku | 0:b1ce54272580 | 270 | #ifdef DEBUG_USB_HOST |
kotakku | 0:b1ce54272580 | 271 | NotifyFailSetConfDescr(); |
kotakku | 0:b1ce54272580 | 272 | goto Fail; |
kotakku | 0:b1ce54272580 | 273 | #endif |
kotakku | 0:b1ce54272580 | 274 | |
kotakku | 0:b1ce54272580 | 275 | |
kotakku | 0:b1ce54272580 | 276 | FailSetIdle: |
kotakku | 0:b1ce54272580 | 277 | #ifdef DEBUG_USB_HOST |
kotakku | 0:b1ce54272580 | 278 | USBTRACE("SetIdle:"); |
kotakku | 0:b1ce54272580 | 279 | #endif |
kotakku | 0:b1ce54272580 | 280 | |
kotakku | 0:b1ce54272580 | 281 | #ifdef DEBUG_USB_HOST |
kotakku | 0:b1ce54272580 | 282 | Fail: |
kotakku | 0:b1ce54272580 | 283 | NotifyFail(rcode); |
kotakku | 0:b1ce54272580 | 284 | #endif |
kotakku | 0:b1ce54272580 | 285 | Release(); |
kotakku | 0:b1ce54272580 | 286 | return rcode; |
kotakku | 0:b1ce54272580 | 287 | } |
kotakku | 0:b1ce54272580 | 288 | |
kotakku | 0:b1ce54272580 | 289 | HIDComposite::HIDInterface* HIDComposite::FindInterface(uint8_t iface, uint8_t alt, uint8_t proto) { |
kotakku | 0:b1ce54272580 | 290 | for(uint8_t i = 0; i < bNumIface && i < maxHidInterfaces; i++) |
kotakku | 0:b1ce54272580 | 291 | if(hidInterfaces[i].bmInterface == iface && hidInterfaces[i].bmAltSet == alt |
kotakku | 0:b1ce54272580 | 292 | && hidInterfaces[i].bmProtocol == proto) |
kotakku | 0:b1ce54272580 | 293 | return hidInterfaces + i; |
kotakku | 0:b1ce54272580 | 294 | return NULL; |
kotakku | 0:b1ce54272580 | 295 | } |
kotakku | 0:b1ce54272580 | 296 | |
kotakku | 0:b1ce54272580 | 297 | void HIDComposite::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *pep) { |
kotakku | 0:b1ce54272580 | 298 | //ErrorMessage<uint8_t>(PSTR("\r\nConf.Val"), conf); |
kotakku | 0:b1ce54272580 | 299 | //ErrorMessage<uint8_t>(PSTR("Iface Num"), iface); |
kotakku | 0:b1ce54272580 | 300 | //ErrorMessage<uint8_t>(PSTR("Alt.Set"), alt); |
kotakku | 0:b1ce54272580 | 301 | |
kotakku | 0:b1ce54272580 | 302 | bConfNum = conf; |
kotakku | 0:b1ce54272580 | 303 | |
kotakku | 0:b1ce54272580 | 304 | uint8_t index = 0; |
kotakku | 0:b1ce54272580 | 305 | HIDInterface *piface = FindInterface(iface, alt, proto); |
kotakku | 0:b1ce54272580 | 306 | |
kotakku | 0:b1ce54272580 | 307 | // Fill in interface structure in case of new interface |
kotakku | 0:b1ce54272580 | 308 | if(!piface) { |
kotakku | 0:b1ce54272580 | 309 | piface = hidInterfaces + bNumIface; |
kotakku | 0:b1ce54272580 | 310 | piface->bmInterface = iface; |
kotakku | 0:b1ce54272580 | 311 | piface->bmAltSet = alt; |
kotakku | 0:b1ce54272580 | 312 | piface->bmProtocol = proto; |
kotakku | 0:b1ce54272580 | 313 | bNumIface++; |
kotakku | 0:b1ce54272580 | 314 | } |
kotakku | 0:b1ce54272580 | 315 | |
kotakku | 0:b1ce54272580 | 316 | if((pep->bmAttributes & bmUSB_TRANSFER_TYPE) == USB_TRANSFER_TYPE_INTERRUPT) |
kotakku | 0:b1ce54272580 | 317 | index = (pep->bEndpointAddress & 0x80) == 0x80 ? epInterruptInIndex : epInterruptOutIndex; |
kotakku | 0:b1ce54272580 | 318 | |
kotakku | 0:b1ce54272580 | 319 | if(!SelectInterface(iface, proto)) |
kotakku | 0:b1ce54272580 | 320 | index = 0; |
kotakku | 0:b1ce54272580 | 321 | |
kotakku | 0:b1ce54272580 | 322 | if(index) { |
kotakku | 0:b1ce54272580 | 323 | // Fill in the endpoint info structure |
kotakku | 0:b1ce54272580 | 324 | epInfo[bNumEP].epAddr = (pep->bEndpointAddress & 0x0F); |
kotakku | 0:b1ce54272580 | 325 | epInfo[bNumEP].maxPktSize = (uint8_t)pep->wMaxPacketSize; |
kotakku | 0:b1ce54272580 | 326 | epInfo[bNumEP].bmSndToggle = 0; |
kotakku | 0:b1ce54272580 | 327 | epInfo[bNumEP].bmRcvToggle = 0; |
kotakku | 0:b1ce54272580 | 328 | epInfo[bNumEP].bmNakPower = USB_NAK_NOWAIT; |
kotakku | 0:b1ce54272580 | 329 | |
kotakku | 0:b1ce54272580 | 330 | // Fill in the endpoint index list |
kotakku | 0:b1ce54272580 | 331 | piface->epIndex[index] = bNumEP; //(pep->bEndpointAddress & 0x0F); |
kotakku | 0:b1ce54272580 | 332 | |
kotakku | 0:b1ce54272580 | 333 | if(pollInterval < pep->bInterval) // Set the polling interval as the largest polling interval obtained from endpoints |
kotakku | 0:b1ce54272580 | 334 | pollInterval = pep->bInterval; |
kotakku | 0:b1ce54272580 | 335 | |
kotakku | 0:b1ce54272580 | 336 | bNumEP++; |
kotakku | 0:b1ce54272580 | 337 | } |
kotakku | 0:b1ce54272580 | 338 | } |
kotakku | 0:b1ce54272580 | 339 | |
kotakku | 0:b1ce54272580 | 340 | uint8_t HIDComposite::Release() { |
kotakku | 0:b1ce54272580 | 341 | pUsb->GetAddressPool().FreeAddress(bAddress); |
kotakku | 0:b1ce54272580 | 342 | |
kotakku | 0:b1ce54272580 | 343 | bNumEP = 1; |
kotakku | 0:b1ce54272580 | 344 | bAddress = 0; |
kotakku | 0:b1ce54272580 | 345 | qNextPollTime = 0; |
kotakku | 0:b1ce54272580 | 346 | bPollEnable = false; |
kotakku | 0:b1ce54272580 | 347 | return 0; |
kotakku | 0:b1ce54272580 | 348 | } |
kotakku | 0:b1ce54272580 | 349 | |
kotakku | 0:b1ce54272580 | 350 | void HIDComposite::ZeroMemory(uint8_t len, uint8_t *buf) { |
kotakku | 0:b1ce54272580 | 351 | for(uint8_t i = 0; i < len; i++) |
kotakku | 0:b1ce54272580 | 352 | buf[i] = 0; |
kotakku | 0:b1ce54272580 | 353 | } |
kotakku | 0:b1ce54272580 | 354 | |
kotakku | 0:b1ce54272580 | 355 | uint8_t HIDComposite::Poll() { |
kotakku | 0:b1ce54272580 | 356 | uint8_t rcode = 0; |
kotakku | 0:b1ce54272580 | 357 | |
kotakku | 0:b1ce54272580 | 358 | if(!bPollEnable) |
kotakku | 0:b1ce54272580 | 359 | return 0; |
kotakku | 0:b1ce54272580 | 360 | |
kotakku | 0:b1ce54272580 | 361 | if((int32_t)((uint32_t)millis() - qNextPollTime) >= 0L) { |
kotakku | 0:b1ce54272580 | 362 | qNextPollTime = (uint32_t)millis() + pollInterval; |
kotakku | 0:b1ce54272580 | 363 | |
kotakku | 0:b1ce54272580 | 364 | uint8_t buf[constBuffLen]; |
kotakku | 0:b1ce54272580 | 365 | |
kotakku | 0:b1ce54272580 | 366 | for(uint8_t i = 0; i < bNumIface; i++) { |
kotakku | 0:b1ce54272580 | 367 | uint8_t index = hidInterfaces[i].epIndex[epInterruptInIndex]; |
kotakku | 0:b1ce54272580 | 368 | |
kotakku | 0:b1ce54272580 | 369 | if (index == 0) |
kotakku | 0:b1ce54272580 | 370 | continue; |
kotakku | 0:b1ce54272580 | 371 | |
kotakku | 0:b1ce54272580 | 372 | uint16_t read = (uint16_t)epInfo[index].maxPktSize; |
kotakku | 0:b1ce54272580 | 373 | |
kotakku | 0:b1ce54272580 | 374 | ZeroMemory(constBuffLen, buf); |
kotakku | 0:b1ce54272580 | 375 | |
kotakku | 0:b1ce54272580 | 376 | uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[index].epAddr, &read, buf); |
kotakku | 0:b1ce54272580 | 377 | |
kotakku | 0:b1ce54272580 | 378 | if(rcode) { |
kotakku | 0:b1ce54272580 | 379 | if(rcode != hrNAK) |
kotakku | 0:b1ce54272580 | 380 | USBTRACE3("(hidcomposite.h) Poll:", rcode, 0x81); |
kotakku | 0:b1ce54272580 | 381 | continue; |
kotakku | 0:b1ce54272580 | 382 | } |
kotakku | 0:b1ce54272580 | 383 | |
kotakku | 0:b1ce54272580 | 384 | if(read == 0) |
kotakku | 0:b1ce54272580 | 385 | continue; |
kotakku | 0:b1ce54272580 | 386 | |
kotakku | 0:b1ce54272580 | 387 | if(read > constBuffLen) |
kotakku | 0:b1ce54272580 | 388 | read = constBuffLen; |
kotakku | 0:b1ce54272580 | 389 | |
kotakku | 0:b1ce54272580 | 390 | #if 0 |
kotakku | 0:b1ce54272580 | 391 | Notify(PSTR("\r\nBuf: "), 0x80); |
kotakku | 0:b1ce54272580 | 392 | |
kotakku | 0:b1ce54272580 | 393 | for(uint8_t i = 0; i < read; i++) { |
kotakku | 0:b1ce54272580 | 394 | D_PrintHex<uint8_t > (buf[i], 0x80); |
kotakku | 0:b1ce54272580 | 395 | Notify(PSTR(" "), 0x80); |
kotakku | 0:b1ce54272580 | 396 | } |
kotakku | 0:b1ce54272580 | 397 | |
kotakku | 0:b1ce54272580 | 398 | Notify(PSTR("\r\n"), 0x80); |
kotakku | 0:b1ce54272580 | 399 | #endif |
kotakku | 0:b1ce54272580 | 400 | ParseHIDData(this, epInfo[index].epAddr, bHasReportId, (uint8_t)read, buf); |
kotakku | 0:b1ce54272580 | 401 | |
kotakku | 0:b1ce54272580 | 402 | HIDReportParser *prs = GetReportParser(((bHasReportId) ? *buf : 0)); |
kotakku | 0:b1ce54272580 | 403 | |
kotakku | 0:b1ce54272580 | 404 | if(prs) |
kotakku | 0:b1ce54272580 | 405 | prs->Parse(this, bHasReportId, (uint8_t)read, buf); |
kotakku | 0:b1ce54272580 | 406 | } |
kotakku | 0:b1ce54272580 | 407 | |
kotakku | 0:b1ce54272580 | 408 | } |
kotakku | 0:b1ce54272580 | 409 | return rcode; |
kotakku | 0:b1ce54272580 | 410 | } |
kotakku | 0:b1ce54272580 | 411 | |
kotakku | 0:b1ce54272580 | 412 | // Send a report to interrupt out endpoint. This is NOT SetReport() request! |
kotakku | 0:b1ce54272580 | 413 | uint8_t HIDComposite::SndRpt(uint16_t nbytes, uint8_t *dataptr) { |
kotakku | 0:b1ce54272580 | 414 | return pUsb->outTransfer(bAddress, epInfo[epInterruptOutIndex].epAddr, nbytes, dataptr); |
kotakku | 0:b1ce54272580 | 415 | } |
kotakku | 0:b1ce54272580 | 416 |