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/address.h@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 program is free software; you can redistribute it and/or modify |
kotakku | 0:b1ce54272580 | 4 | it under the terms of the GNU General Public License as published by |
kotakku | 0:b1ce54272580 | 5 | the Free Software Foundation; either version 2 of the License, or |
kotakku | 0:b1ce54272580 | 6 | (at your option) any later version. |
kotakku | 0:b1ce54272580 | 7 | |
kotakku | 0:b1ce54272580 | 8 | This program is distributed in the hope that it will be useful, |
kotakku | 0:b1ce54272580 | 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
kotakku | 0:b1ce54272580 | 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
kotakku | 0:b1ce54272580 | 11 | GNU General Public License for more details. |
kotakku | 0:b1ce54272580 | 12 | |
kotakku | 0:b1ce54272580 | 13 | You should have received a copy of the GNU General Public License |
kotakku | 0:b1ce54272580 | 14 | along with this program; if not, write to the Free Software |
kotakku | 0:b1ce54272580 | 15 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
kotakku | 0:b1ce54272580 | 16 | |
kotakku | 0:b1ce54272580 | 17 | Contact information |
kotakku | 0:b1ce54272580 | 18 | ------------------- |
kotakku | 0:b1ce54272580 | 19 | |
kotakku | 0:b1ce54272580 | 20 | Circuits At Home, LTD |
kotakku | 0:b1ce54272580 | 21 | Web : http://www.circuitsathome.com |
kotakku | 0:b1ce54272580 | 22 | e-mail : support@circuitsathome.com |
kotakku | 0:b1ce54272580 | 23 | */ |
kotakku | 0:b1ce54272580 | 24 | |
kotakku | 0:b1ce54272580 | 25 | #if !defined(_usb_h_) || defined(__ADDRESS_H__) |
kotakku | 0:b1ce54272580 | 26 | #error "Never include address.h directly; include Usb.h instead" |
kotakku | 0:b1ce54272580 | 27 | #else |
kotakku | 0:b1ce54272580 | 28 | #define __ADDRESS_H__ |
kotakku | 0:b1ce54272580 | 29 | |
kotakku | 0:b1ce54272580 | 30 | |
kotakku | 0:b1ce54272580 | 31 | |
kotakku | 0:b1ce54272580 | 32 | /* NAK powers. To save space in endpoint data structure, amount of retries before giving up and returning 0x4 is stored in */ |
kotakku | 0:b1ce54272580 | 33 | /* bmNakPower as a power of 2. The actual nak_limit is then calculated as nak_limit = ( 2^bmNakPower - 1) */ |
kotakku | 0:b1ce54272580 | 34 | #define USB_NAK_MAX_POWER 15 //NAK binary order maximum value |
kotakku | 0:b1ce54272580 | 35 | #define USB_NAK_DEFAULT 14 //default 32K-1 NAKs before giving up |
kotakku | 0:b1ce54272580 | 36 | #define USB_NAK_NOWAIT 1 //Single NAK stops transfer |
kotakku | 0:b1ce54272580 | 37 | #define USB_NAK_NONAK 0 //Do not count NAKs, stop retrying after USB Timeout |
kotakku | 0:b1ce54272580 | 38 | |
kotakku | 0:b1ce54272580 | 39 | struct EpInfo { |
kotakku | 0:b1ce54272580 | 40 | uint8_t epAddr; // Endpoint address |
kotakku | 0:b1ce54272580 | 41 | uint8_t maxPktSize; // Maximum packet size |
kotakku | 0:b1ce54272580 | 42 | |
kotakku | 0:b1ce54272580 | 43 | union { |
kotakku | 0:b1ce54272580 | 44 | uint8_t epAttribs; |
kotakku | 0:b1ce54272580 | 45 | |
kotakku | 0:b1ce54272580 | 46 | struct { |
kotakku | 0:b1ce54272580 | 47 | uint8_t bmSndToggle : 1; // Send toggle, when zero bmSNDTOG0, bmSNDTOG1 otherwise |
kotakku | 0:b1ce54272580 | 48 | uint8_t bmRcvToggle : 1; // Send toggle, when zero bmRCVTOG0, bmRCVTOG1 otherwise |
kotakku | 0:b1ce54272580 | 49 | uint8_t bmNakPower : 6; // Binary order for NAK_LIMIT value |
kotakku | 0:b1ce54272580 | 50 | } __attribute__((packed)); |
kotakku | 0:b1ce54272580 | 51 | }; |
kotakku | 0:b1ce54272580 | 52 | } __attribute__((packed)); |
kotakku | 0:b1ce54272580 | 53 | |
kotakku | 0:b1ce54272580 | 54 | // 7 6 5 4 3 2 1 0 |
kotakku | 0:b1ce54272580 | 55 | // --------------------------------- |
kotakku | 0:b1ce54272580 | 56 | // | | H | P | P | P | A | A | A | |
kotakku | 0:b1ce54272580 | 57 | // --------------------------------- |
kotakku | 0:b1ce54272580 | 58 | // |
kotakku | 0:b1ce54272580 | 59 | // H - if 1 the address is a hub address |
kotakku | 0:b1ce54272580 | 60 | // P - parent hub address |
kotakku | 0:b1ce54272580 | 61 | // A - device address / port number in case of hub |
kotakku | 0:b1ce54272580 | 62 | // |
kotakku | 0:b1ce54272580 | 63 | |
kotakku | 0:b1ce54272580 | 64 | struct UsbDeviceAddress { |
kotakku | 0:b1ce54272580 | 65 | |
kotakku | 0:b1ce54272580 | 66 | union { |
kotakku | 0:b1ce54272580 | 67 | |
kotakku | 0:b1ce54272580 | 68 | struct { |
kotakku | 0:b1ce54272580 | 69 | uint8_t bmAddress : 3; // device address/port number |
kotakku | 0:b1ce54272580 | 70 | uint8_t bmParent : 3; // parent hub address |
kotakku | 0:b1ce54272580 | 71 | uint8_t bmHub : 1; // hub flag |
kotakku | 0:b1ce54272580 | 72 | uint8_t bmReserved : 1; // reserved, must be zero |
kotakku | 0:b1ce54272580 | 73 | } __attribute__((packed)); |
kotakku | 0:b1ce54272580 | 74 | uint8_t devAddress; |
kotakku | 0:b1ce54272580 | 75 | }; |
kotakku | 0:b1ce54272580 | 76 | } __attribute__((packed)); |
kotakku | 0:b1ce54272580 | 77 | |
kotakku | 0:b1ce54272580 | 78 | #define bmUSB_DEV_ADDR_ADDRESS 0x07 |
kotakku | 0:b1ce54272580 | 79 | #define bmUSB_DEV_ADDR_PARENT 0x38 |
kotakku | 0:b1ce54272580 | 80 | #define bmUSB_DEV_ADDR_HUB 0x40 |
kotakku | 0:b1ce54272580 | 81 | |
kotakku | 0:b1ce54272580 | 82 | struct UsbDevice { |
kotakku | 0:b1ce54272580 | 83 | EpInfo *epinfo; // endpoint info pointer |
kotakku | 0:b1ce54272580 | 84 | UsbDeviceAddress address; |
kotakku | 0:b1ce54272580 | 85 | uint8_t epcount; // number of endpoints |
kotakku | 0:b1ce54272580 | 86 | bool lowspeed; // indicates if a device is the low speed one |
kotakku | 0:b1ce54272580 | 87 | // uint8_t devclass; // device class |
kotakku | 0:b1ce54272580 | 88 | } __attribute__((packed)); |
kotakku | 0:b1ce54272580 | 89 | |
kotakku | 0:b1ce54272580 | 90 | class AddressPool { |
kotakku | 0:b1ce54272580 | 91 | public: |
kotakku | 0:b1ce54272580 | 92 | virtual UsbDevice* GetUsbDevicePtr(uint8_t addr) = 0; |
kotakku | 0:b1ce54272580 | 93 | virtual uint8_t AllocAddress(uint8_t parent, bool is_hub = false, uint8_t port = 0) = 0; |
kotakku | 0:b1ce54272580 | 94 | virtual void FreeAddress(uint8_t addr) = 0; |
kotakku | 0:b1ce54272580 | 95 | }; |
kotakku | 0:b1ce54272580 | 96 | |
kotakku | 0:b1ce54272580 | 97 | typedef void (*UsbDeviceHandleFunc)(UsbDevice *pdev); |
kotakku | 0:b1ce54272580 | 98 | |
kotakku | 0:b1ce54272580 | 99 | #define ADDR_ERROR_INVALID_INDEX 0xFF |
kotakku | 0:b1ce54272580 | 100 | #define ADDR_ERROR_INVALID_ADDRESS 0xFF |
kotakku | 0:b1ce54272580 | 101 | |
kotakku | 0:b1ce54272580 | 102 | template <const uint8_t MAX_DEVICES_ALLOWED> |
kotakku | 0:b1ce54272580 | 103 | class AddressPoolImpl : public AddressPool { |
kotakku | 0:b1ce54272580 | 104 | EpInfo dev0ep; //Endpoint data structure used during enumeration for uninitialized device |
kotakku | 0:b1ce54272580 | 105 | |
kotakku | 0:b1ce54272580 | 106 | uint8_t hubCounter; // hub counter is kept |
kotakku | 0:b1ce54272580 | 107 | // in order to avoid hub address duplication |
kotakku | 0:b1ce54272580 | 108 | |
kotakku | 0:b1ce54272580 | 109 | UsbDevice thePool[MAX_DEVICES_ALLOWED]; |
kotakku | 0:b1ce54272580 | 110 | |
kotakku | 0:b1ce54272580 | 111 | // Initializes address pool entry |
kotakku | 0:b1ce54272580 | 112 | |
kotakku | 0:b1ce54272580 | 113 | void InitEntry(uint8_t index) { |
kotakku | 0:b1ce54272580 | 114 | thePool[index].address.devAddress = 0; |
kotakku | 0:b1ce54272580 | 115 | thePool[index].epcount = 1; |
kotakku | 0:b1ce54272580 | 116 | thePool[index].lowspeed = 0; |
kotakku | 0:b1ce54272580 | 117 | thePool[index].epinfo = &dev0ep; |
kotakku | 0:b1ce54272580 | 118 | }; |
kotakku | 0:b1ce54272580 | 119 | |
kotakku | 0:b1ce54272580 | 120 | // Returns thePool index for a given address |
kotakku | 0:b1ce54272580 | 121 | |
kotakku | 0:b1ce54272580 | 122 | uint8_t FindAddressIndex(uint8_t address = 0) { |
kotakku | 0:b1ce54272580 | 123 | for(uint8_t i = 1; i < MAX_DEVICES_ALLOWED; i++) { |
kotakku | 0:b1ce54272580 | 124 | if(thePool[i].address.devAddress == address) |
kotakku | 0:b1ce54272580 | 125 | return i; |
kotakku | 0:b1ce54272580 | 126 | } |
kotakku | 0:b1ce54272580 | 127 | return 0; |
kotakku | 0:b1ce54272580 | 128 | }; |
kotakku | 0:b1ce54272580 | 129 | |
kotakku | 0:b1ce54272580 | 130 | // Returns thePool child index for a given parent |
kotakku | 0:b1ce54272580 | 131 | |
kotakku | 0:b1ce54272580 | 132 | uint8_t FindChildIndex(UsbDeviceAddress addr, uint8_t start = 1) { |
kotakku | 0:b1ce54272580 | 133 | for(uint8_t i = (start < 1 || start >= MAX_DEVICES_ALLOWED) ? 1 : start; i < MAX_DEVICES_ALLOWED; i++) { |
kotakku | 0:b1ce54272580 | 134 | if(thePool[i].address.bmParent == addr.bmAddress) |
kotakku | 0:b1ce54272580 | 135 | return i; |
kotakku | 0:b1ce54272580 | 136 | } |
kotakku | 0:b1ce54272580 | 137 | return 0; |
kotakku | 0:b1ce54272580 | 138 | }; |
kotakku | 0:b1ce54272580 | 139 | |
kotakku | 0:b1ce54272580 | 140 | // Frees address entry specified by index parameter |
kotakku | 0:b1ce54272580 | 141 | |
kotakku | 0:b1ce54272580 | 142 | void FreeAddressByIndex(uint8_t index) { |
kotakku | 0:b1ce54272580 | 143 | // Zero field is reserved and should not be affected |
kotakku | 0:b1ce54272580 | 144 | if(index == 0) |
kotakku | 0:b1ce54272580 | 145 | return; |
kotakku | 0:b1ce54272580 | 146 | |
kotakku | 0:b1ce54272580 | 147 | UsbDeviceAddress uda = thePool[index].address; |
kotakku | 0:b1ce54272580 | 148 | // If a hub was switched off all port addresses should be freed |
kotakku | 0:b1ce54272580 | 149 | if(uda.bmHub == 1) { |
kotakku | 0:b1ce54272580 | 150 | for(uint8_t i = 1; (i = FindChildIndex(uda, i));) |
kotakku | 0:b1ce54272580 | 151 | FreeAddressByIndex(i); |
kotakku | 0:b1ce54272580 | 152 | |
kotakku | 0:b1ce54272580 | 153 | // If the hub had the last allocated address, hubCounter should be decremented |
kotakku | 0:b1ce54272580 | 154 | if(hubCounter == uda.bmAddress) |
kotakku | 0:b1ce54272580 | 155 | hubCounter--; |
kotakku | 0:b1ce54272580 | 156 | } |
kotakku | 0:b1ce54272580 | 157 | InitEntry(index); |
kotakku | 0:b1ce54272580 | 158 | } |
kotakku | 0:b1ce54272580 | 159 | |
kotakku | 0:b1ce54272580 | 160 | // Initializes the whole address pool at once |
kotakku | 0:b1ce54272580 | 161 | |
kotakku | 0:b1ce54272580 | 162 | void InitAllAddresses() { |
kotakku | 0:b1ce54272580 | 163 | for(uint8_t i = 1; i < MAX_DEVICES_ALLOWED; i++) |
kotakku | 0:b1ce54272580 | 164 | InitEntry(i); |
kotakku | 0:b1ce54272580 | 165 | |
kotakku | 0:b1ce54272580 | 166 | hubCounter = 0; |
kotakku | 0:b1ce54272580 | 167 | }; |
kotakku | 0:b1ce54272580 | 168 | |
kotakku | 0:b1ce54272580 | 169 | public: |
kotakku | 0:b1ce54272580 | 170 | |
kotakku | 0:b1ce54272580 | 171 | AddressPoolImpl() : hubCounter(0) { |
kotakku | 0:b1ce54272580 | 172 | // Zero address is reserved |
kotakku | 0:b1ce54272580 | 173 | InitEntry(0); |
kotakku | 0:b1ce54272580 | 174 | |
kotakku | 0:b1ce54272580 | 175 | thePool[0].address.devAddress = 0; |
kotakku | 0:b1ce54272580 | 176 | thePool[0].epinfo = &dev0ep; |
kotakku | 0:b1ce54272580 | 177 | dev0ep.epAddr = 0; |
kotakku | 0:b1ce54272580 | 178 | dev0ep.maxPktSize = 8; |
kotakku | 0:b1ce54272580 | 179 | dev0ep.bmSndToggle = 0; // Set DATA0/1 toggles to 0 |
kotakku | 0:b1ce54272580 | 180 | dev0ep.bmRcvToggle = 0; |
kotakku | 0:b1ce54272580 | 181 | dev0ep.bmNakPower = USB_NAK_MAX_POWER; |
kotakku | 0:b1ce54272580 | 182 | |
kotakku | 0:b1ce54272580 | 183 | InitAllAddresses(); |
kotakku | 0:b1ce54272580 | 184 | }; |
kotakku | 0:b1ce54272580 | 185 | |
kotakku | 0:b1ce54272580 | 186 | // Returns a pointer to a specified address entry |
kotakku | 0:b1ce54272580 | 187 | |
kotakku | 0:b1ce54272580 | 188 | virtual UsbDevice* GetUsbDevicePtr(uint8_t addr) { |
kotakku | 0:b1ce54272580 | 189 | if(!addr) |
kotakku | 0:b1ce54272580 | 190 | return thePool; |
kotakku | 0:b1ce54272580 | 191 | |
kotakku | 0:b1ce54272580 | 192 | uint8_t index = FindAddressIndex(addr); |
kotakku | 0:b1ce54272580 | 193 | |
kotakku | 0:b1ce54272580 | 194 | return (!index) ? NULL : thePool + index; |
kotakku | 0:b1ce54272580 | 195 | }; |
kotakku | 0:b1ce54272580 | 196 | |
kotakku | 0:b1ce54272580 | 197 | // Performs an operation specified by pfunc for each addressed device |
kotakku | 0:b1ce54272580 | 198 | |
kotakku | 0:b1ce54272580 | 199 | void ForEachUsbDevice(UsbDeviceHandleFunc pfunc) { |
kotakku | 0:b1ce54272580 | 200 | if(!pfunc) |
kotakku | 0:b1ce54272580 | 201 | return; |
kotakku | 0:b1ce54272580 | 202 | |
kotakku | 0:b1ce54272580 | 203 | for(uint8_t i = 1; i < MAX_DEVICES_ALLOWED; i++) |
kotakku | 0:b1ce54272580 | 204 | if(thePool[i].address.devAddress) |
kotakku | 0:b1ce54272580 | 205 | pfunc(thePool + i); |
kotakku | 0:b1ce54272580 | 206 | }; |
kotakku | 0:b1ce54272580 | 207 | |
kotakku | 0:b1ce54272580 | 208 | // Allocates new address |
kotakku | 0:b1ce54272580 | 209 | |
kotakku | 0:b1ce54272580 | 210 | virtual uint8_t AllocAddress(uint8_t parent, bool is_hub = false, uint8_t port = 0) { |
kotakku | 0:b1ce54272580 | 211 | /* if (parent != 0 && port == 0) |
kotakku | 0:b1ce54272580 | 212 | USB_HOST_SERIAL.println("PRT:0"); */ |
kotakku | 0:b1ce54272580 | 213 | UsbDeviceAddress _parent; |
kotakku | 0:b1ce54272580 | 214 | _parent.devAddress = parent; |
kotakku | 0:b1ce54272580 | 215 | if(_parent.bmReserved || port > 7) |
kotakku | 0:b1ce54272580 | 216 | //if(parent > 127 || port > 7) |
kotakku | 0:b1ce54272580 | 217 | return 0; |
kotakku | 0:b1ce54272580 | 218 | |
kotakku | 0:b1ce54272580 | 219 | if(is_hub && hubCounter == 7) |
kotakku | 0:b1ce54272580 | 220 | return 0; |
kotakku | 0:b1ce54272580 | 221 | |
kotakku | 0:b1ce54272580 | 222 | // finds first empty address entry starting from one |
kotakku | 0:b1ce54272580 | 223 | uint8_t index = FindAddressIndex(0); |
kotakku | 0:b1ce54272580 | 224 | |
kotakku | 0:b1ce54272580 | 225 | if(!index) // if empty entry is not found |
kotakku | 0:b1ce54272580 | 226 | return 0; |
kotakku | 0:b1ce54272580 | 227 | |
kotakku | 0:b1ce54272580 | 228 | if(_parent.devAddress == 0) { |
kotakku | 0:b1ce54272580 | 229 | if(is_hub) { |
kotakku | 0:b1ce54272580 | 230 | thePool[index].address.devAddress = 0x41; |
kotakku | 0:b1ce54272580 | 231 | hubCounter++; |
kotakku | 0:b1ce54272580 | 232 | } else |
kotakku | 0:b1ce54272580 | 233 | thePool[index].address.devAddress = 1; |
kotakku | 0:b1ce54272580 | 234 | |
kotakku | 0:b1ce54272580 | 235 | return thePool[index].address.devAddress; |
kotakku | 0:b1ce54272580 | 236 | } |
kotakku | 0:b1ce54272580 | 237 | |
kotakku | 0:b1ce54272580 | 238 | UsbDeviceAddress addr; |
kotakku | 0:b1ce54272580 | 239 | addr.devAddress = 0; // Ensure all bits are zero |
kotakku | 0:b1ce54272580 | 240 | addr.bmParent = _parent.bmAddress; |
kotakku | 0:b1ce54272580 | 241 | if(is_hub) { |
kotakku | 0:b1ce54272580 | 242 | addr.bmHub = 1; |
kotakku | 0:b1ce54272580 | 243 | addr.bmAddress = ++hubCounter; |
kotakku | 0:b1ce54272580 | 244 | } else { |
kotakku | 0:b1ce54272580 | 245 | addr.bmHub = 0; |
kotakku | 0:b1ce54272580 | 246 | addr.bmAddress = port; |
kotakku | 0:b1ce54272580 | 247 | } |
kotakku | 0:b1ce54272580 | 248 | thePool[index].address = addr; |
kotakku | 0:b1ce54272580 | 249 | /* |
kotakku | 0:b1ce54272580 | 250 | USB_HOST_SERIAL.print("Addr:"); |
kotakku | 0:b1ce54272580 | 251 | USB_HOST_SERIAL.print(addr.bmHub, HEX); |
kotakku | 0:b1ce54272580 | 252 | USB_HOST_SERIAL.print("."); |
kotakku | 0:b1ce54272580 | 253 | USB_HOST_SERIAL.print(addr.bmParent, HEX); |
kotakku | 0:b1ce54272580 | 254 | USB_HOST_SERIAL.print("."); |
kotakku | 0:b1ce54272580 | 255 | USB_HOST_SERIAL.println(addr.bmAddress, HEX); |
kotakku | 0:b1ce54272580 | 256 | */ |
kotakku | 0:b1ce54272580 | 257 | return thePool[index].address.devAddress; |
kotakku | 0:b1ce54272580 | 258 | }; |
kotakku | 0:b1ce54272580 | 259 | |
kotakku | 0:b1ce54272580 | 260 | // Empties pool entry |
kotakku | 0:b1ce54272580 | 261 | |
kotakku | 0:b1ce54272580 | 262 | virtual void FreeAddress(uint8_t addr) { |
kotakku | 0:b1ce54272580 | 263 | // if the root hub is disconnected all the addresses should be initialized |
kotakku | 0:b1ce54272580 | 264 | if(addr == 0x41) { |
kotakku | 0:b1ce54272580 | 265 | InitAllAddresses(); |
kotakku | 0:b1ce54272580 | 266 | return; |
kotakku | 0:b1ce54272580 | 267 | } |
kotakku | 0:b1ce54272580 | 268 | uint8_t index = FindAddressIndex(addr); |
kotakku | 0:b1ce54272580 | 269 | FreeAddressByIndex(index); |
kotakku | 0:b1ce54272580 | 270 | }; |
kotakku | 0:b1ce54272580 | 271 | |
kotakku | 0:b1ce54272580 | 272 | // Returns number of hubs attached |
kotakku | 0:b1ce54272580 | 273 | // It can be rather helpfull to find out if there are hubs attached than getting the exact number of hubs. |
kotakku | 0:b1ce54272580 | 274 | //uint8_t GetNumHubs() |
kotakku | 0:b1ce54272580 | 275 | //{ |
kotakku | 0:b1ce54272580 | 276 | // return hubCounter; |
kotakku | 0:b1ce54272580 | 277 | //}; |
kotakku | 0:b1ce54272580 | 278 | //uint8_t GetNumDevices() |
kotakku | 0:b1ce54272580 | 279 | //{ |
kotakku | 0:b1ce54272580 | 280 | // uint8_t counter = 0; |
kotakku | 0:b1ce54272580 | 281 | |
kotakku | 0:b1ce54272580 | 282 | // for (uint8_t i=1; i<MAX_DEVICES_ALLOWED; i++) |
kotakku | 0:b1ce54272580 | 283 | // if (thePool[i].address != 0); |
kotakku | 0:b1ce54272580 | 284 | // counter ++; |
kotakku | 0:b1ce54272580 | 285 | |
kotakku | 0:b1ce54272580 | 286 | // return counter; |
kotakku | 0:b1ce54272580 | 287 | //}; |
kotakku | 0:b1ce54272580 | 288 | }; |
kotakku | 0:b1ce54272580 | 289 | |
kotakku | 0:b1ce54272580 | 290 | #endif // __ADDRESS_H__ |
kotakku | 0:b1ce54272580 | 291 |