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/UsbCore.h@0:b1ce54272580, 2020-01-18 (annotated)
- Committer:
- kotakku
- Date:
- Sat Jan 18 15:06:35 2020 +0000
- Revision:
- 0:b1ce54272580
- Child:
- 1:da31140f2a1c
1.0.0 first commit
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 | // warning |
kotakku | 0:b1ce54272580 | 26 | // #define _usb_h_ |
kotakku | 0:b1ce54272580 | 27 | // #define MBED_H |
kotakku | 0:b1ce54272580 | 28 | |
kotakku | 0:b1ce54272580 | 29 | #if !defined(_usb_h_) || defined(USBCORE_H) |
kotakku | 0:b1ce54272580 | 30 | #error "Never include UsbCore.h directly; include Usb.h instead" |
kotakku | 0:b1ce54272580 | 31 | #else |
kotakku | 0:b1ce54272580 | 32 | #define USBCORE_H |
kotakku | 0:b1ce54272580 | 33 | |
kotakku | 0:b1ce54272580 | 34 | // Not used anymore? If anyone uses this, please let us know so that this may be |
kotakku | 0:b1ce54272580 | 35 | // moved to the proper place, settings.h. |
kotakku | 0:b1ce54272580 | 36 | //#define USB_METHODS_INLINE |
kotakku | 0:b1ce54272580 | 37 | |
kotakku | 0:b1ce54272580 | 38 | |
kotakku | 0:b1ce54272580 | 39 | /* Common setup data constant combinations */ |
kotakku | 0:b1ce54272580 | 40 | #define bmREQ_GET_DESCR USB_SETUP_DEVICE_TO_HOST | USB_SETUP_TYPE_STANDARD | USB_SETUP_RECIPIENT_DEVICE //get descriptor request type |
kotakku | 0:b1ce54272580 | 41 | #define bmREQ_SET USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_STANDARD | USB_SETUP_RECIPIENT_DEVICE //set request type for all but 'set feature' and 'set interface' |
kotakku | 0:b1ce54272580 | 42 | #define bmREQ_CL_GET_INTF USB_SETUP_DEVICE_TO_HOST | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE //get interface request type |
kotakku | 0:b1ce54272580 | 43 | |
kotakku | 0:b1ce54272580 | 44 | // D7 data transfer direction (0 - host-to-device, 1 - device-to-host) |
kotakku | 0:b1ce54272580 | 45 | // D6-5 Type (0- standard, 1 - class, 2 - vendor, 3 - reserved) |
kotakku | 0:b1ce54272580 | 46 | // D4-0 Recipient (0 - device, 1 - interface, 2 - endpoint, 3 - other, 4..31 - reserved) |
kotakku | 0:b1ce54272580 | 47 | |
kotakku | 0:b1ce54272580 | 48 | // USB Device Classes |
kotakku | 0:b1ce54272580 | 49 | #define USB_CLASS_USE_CLASS_INFO 0x00 // Use Class Info in the Interface Descriptors |
kotakku | 0:b1ce54272580 | 50 | #define USB_CLASS_AUDIO 0x01 // Audio |
kotakku | 0:b1ce54272580 | 51 | #define USB_CLASS_COM_AND_CDC_CTRL 0x02 // Communications and CDC Control |
kotakku | 0:b1ce54272580 | 52 | #define USB_CLASS_HID 0x03 // HID |
kotakku | 0:b1ce54272580 | 53 | #define USB_CLASS_PHYSICAL 0x05 // Physical |
kotakku | 0:b1ce54272580 | 54 | #define USB_CLASS_IMAGE 0x06 // Image |
kotakku | 0:b1ce54272580 | 55 | #define USB_CLASS_PRINTER 0x07 // Printer |
kotakku | 0:b1ce54272580 | 56 | #define USB_CLASS_MASS_STORAGE 0x08 // Mass Storage |
kotakku | 0:b1ce54272580 | 57 | #define USB_CLASS_HUB 0x09 // Hub |
kotakku | 0:b1ce54272580 | 58 | #define USB_CLASS_CDC_DATA 0x0a // CDC-Data |
kotakku | 0:b1ce54272580 | 59 | #define USB_CLASS_SMART_CARD 0x0b // Smart-Card |
kotakku | 0:b1ce54272580 | 60 | #define USB_CLASS_CONTENT_SECURITY 0x0d // Content Security |
kotakku | 0:b1ce54272580 | 61 | #define USB_CLASS_VIDEO 0x0e // Video |
kotakku | 0:b1ce54272580 | 62 | #define USB_CLASS_PERSONAL_HEALTH 0x0f // Personal Healthcare |
kotakku | 0:b1ce54272580 | 63 | #define USB_CLASS_DIAGNOSTIC_DEVICE 0xdc // Diagnostic Device |
kotakku | 0:b1ce54272580 | 64 | #define USB_CLASS_WIRELESS_CTRL 0xe0 // Wireless Controller |
kotakku | 0:b1ce54272580 | 65 | #define USB_CLASS_MISC 0xef // Miscellaneous |
kotakku | 0:b1ce54272580 | 66 | #define USB_CLASS_APP_SPECIFIC 0xfe // Application Specific |
kotakku | 0:b1ce54272580 | 67 | #define USB_CLASS_VENDOR_SPECIFIC 0xff // Vendor Specific |
kotakku | 0:b1ce54272580 | 68 | |
kotakku | 0:b1ce54272580 | 69 | // Additional Error Codes |
kotakku | 0:b1ce54272580 | 70 | #define USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED 0xD1 |
kotakku | 0:b1ce54272580 | 71 | #define USB_DEV_CONFIG_ERROR_DEVICE_INIT_INCOMPLETE 0xD2 |
kotakku | 0:b1ce54272580 | 72 | #define USB_ERROR_UNABLE_TO_REGISTER_DEVICE_CLASS 0xD3 |
kotakku | 0:b1ce54272580 | 73 | #define USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL 0xD4 |
kotakku | 0:b1ce54272580 | 74 | #define USB_ERROR_HUB_ADDRESS_OVERFLOW 0xD5 |
kotakku | 0:b1ce54272580 | 75 | #define USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL 0xD6 |
kotakku | 0:b1ce54272580 | 76 | #define USB_ERROR_EPINFO_IS_NULL 0xD7 |
kotakku | 0:b1ce54272580 | 77 | #define USB_ERROR_INVALID_ARGUMENT 0xD8 |
kotakku | 0:b1ce54272580 | 78 | #define USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE 0xD9 |
kotakku | 0:b1ce54272580 | 79 | #define USB_ERROR_INVALID_MAX_PKT_SIZE 0xDA |
kotakku | 0:b1ce54272580 | 80 | #define USB_ERROR_EP_NOT_FOUND_IN_TBL 0xDB |
kotakku | 0:b1ce54272580 | 81 | #define USB_ERROR_CONFIG_REQUIRES_ADDITIONAL_RESET 0xE0 |
kotakku | 0:b1ce54272580 | 82 | #define USB_ERROR_FailGetDevDescr 0xE1 |
kotakku | 0:b1ce54272580 | 83 | #define USB_ERROR_FailSetDevTblEntry 0xE2 |
kotakku | 0:b1ce54272580 | 84 | #define USB_ERROR_FailGetConfDescr 0xE3 |
kotakku | 0:b1ce54272580 | 85 | #define USB_ERROR_TRANSFER_TIMEOUT 0xFF |
kotakku | 0:b1ce54272580 | 86 | |
kotakku | 0:b1ce54272580 | 87 | #define USB_XFER_TIMEOUT 5000 // (5000) USB transfer timeout in milliseconds, per section 9.2.6.1 of USB 2.0 spec |
kotakku | 0:b1ce54272580 | 88 | //#define USB_NAK_LIMIT 32000 // NAK limit for a transfer. 0 means NAKs are not counted |
kotakku | 0:b1ce54272580 | 89 | #define USB_RETRY_LIMIT 3 // 3 retry limit for a transfer |
kotakku | 0:b1ce54272580 | 90 | #define USB_SETTLE_DELAY 200 // settle delay in milliseconds |
kotakku | 0:b1ce54272580 | 91 | |
kotakku | 0:b1ce54272580 | 92 | #define USB_NUMDEVICES 16 //number of USB devices |
kotakku | 0:b1ce54272580 | 93 | //#define HUB_MAX_HUBS 7 // maximum number of hubs that can be attached to the host controller |
kotakku | 0:b1ce54272580 | 94 | #define HUB_PORT_RESET_DELAY 20 // hub port reset delay 10 ms recomended, can be up to 20 ms |
kotakku | 0:b1ce54272580 | 95 | |
kotakku | 0:b1ce54272580 | 96 | /* USB state machine states */ |
kotakku | 0:b1ce54272580 | 97 | #define USB_STATE_MASK 0xf0 |
kotakku | 0:b1ce54272580 | 98 | |
kotakku | 0:b1ce54272580 | 99 | #define USB_STATE_DETACHED 0x10 |
kotakku | 0:b1ce54272580 | 100 | #define USB_DETACHED_SUBSTATE_INITIALIZE 0x11 |
kotakku | 0:b1ce54272580 | 101 | #define USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE 0x12 |
kotakku | 0:b1ce54272580 | 102 | #define USB_DETACHED_SUBSTATE_ILLEGAL 0x13 |
kotakku | 0:b1ce54272580 | 103 | #define USB_ATTACHED_SUBSTATE_SETTLE 0x20 |
kotakku | 0:b1ce54272580 | 104 | #define USB_ATTACHED_SUBSTATE_RESET_DEVICE 0x30 |
kotakku | 0:b1ce54272580 | 105 | #define USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE 0x40 |
kotakku | 0:b1ce54272580 | 106 | #define USB_ATTACHED_SUBSTATE_WAIT_SOF 0x50 |
kotakku | 0:b1ce54272580 | 107 | #define USB_ATTACHED_SUBSTATE_WAIT_RESET 0x51 |
kotakku | 0:b1ce54272580 | 108 | #define USB_ATTACHED_SUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE 0x60 |
kotakku | 0:b1ce54272580 | 109 | #define USB_STATE_ADDRESSING 0x70 |
kotakku | 0:b1ce54272580 | 110 | #define USB_STATE_CONFIGURING 0x80 |
kotakku | 0:b1ce54272580 | 111 | #define USB_STATE_RUNNING 0x90 |
kotakku | 0:b1ce54272580 | 112 | #define USB_STATE_ERROR 0xa0 |
kotakku | 0:b1ce54272580 | 113 | |
kotakku | 0:b1ce54272580 | 114 | class USBDeviceConfig |
kotakku | 0:b1ce54272580 | 115 | { |
kotakku | 0:b1ce54272580 | 116 | public: |
kotakku | 0:b1ce54272580 | 117 | virtual uint8_t Init(uint8_t parent __attribute__((unused)), uint8_t port __attribute__((unused)), bool lowspeed __attribute__((unused))) |
kotakku | 0:b1ce54272580 | 118 | { |
kotakku | 0:b1ce54272580 | 119 | return 0; |
kotakku | 0:b1ce54272580 | 120 | } |
kotakku | 0:b1ce54272580 | 121 | |
kotakku | 0:b1ce54272580 | 122 | virtual uint8_t ConfigureDevice(uint8_t parent __attribute__((unused)), uint8_t port __attribute__((unused)), bool lowspeed __attribute__((unused))) |
kotakku | 0:b1ce54272580 | 123 | { |
kotakku | 0:b1ce54272580 | 124 | return 0; |
kotakku | 0:b1ce54272580 | 125 | } |
kotakku | 0:b1ce54272580 | 126 | |
kotakku | 0:b1ce54272580 | 127 | virtual uint8_t Release() |
kotakku | 0:b1ce54272580 | 128 | { |
kotakku | 0:b1ce54272580 | 129 | return 0; |
kotakku | 0:b1ce54272580 | 130 | } |
kotakku | 0:b1ce54272580 | 131 | |
kotakku | 0:b1ce54272580 | 132 | virtual uint8_t Poll() |
kotakku | 0:b1ce54272580 | 133 | { |
kotakku | 0:b1ce54272580 | 134 | return 0; |
kotakku | 0:b1ce54272580 | 135 | } |
kotakku | 0:b1ce54272580 | 136 | |
kotakku | 0:b1ce54272580 | 137 | virtual uint8_t GetAddress() |
kotakku | 0:b1ce54272580 | 138 | { |
kotakku | 0:b1ce54272580 | 139 | return 0; |
kotakku | 0:b1ce54272580 | 140 | } |
kotakku | 0:b1ce54272580 | 141 | |
kotakku | 0:b1ce54272580 | 142 | virtual void ResetHubPort(uint8_t port __attribute__((unused))) |
kotakku | 0:b1ce54272580 | 143 | { |
kotakku | 0:b1ce54272580 | 144 | return; |
kotakku | 0:b1ce54272580 | 145 | } // Note used for hubs only! |
kotakku | 0:b1ce54272580 | 146 | |
kotakku | 0:b1ce54272580 | 147 | virtual bool VIDPIDOK(uint16_t vid __attribute__((unused)), uint16_t pid __attribute__((unused))) |
kotakku | 0:b1ce54272580 | 148 | { |
kotakku | 0:b1ce54272580 | 149 | return false; |
kotakku | 0:b1ce54272580 | 150 | } |
kotakku | 0:b1ce54272580 | 151 | |
kotakku | 0:b1ce54272580 | 152 | virtual bool DEVCLASSOK(uint8_t klass __attribute__((unused))) |
kotakku | 0:b1ce54272580 | 153 | { |
kotakku | 0:b1ce54272580 | 154 | return false; |
kotakku | 0:b1ce54272580 | 155 | } |
kotakku | 0:b1ce54272580 | 156 | |
kotakku | 0:b1ce54272580 | 157 | virtual bool DEVSUBCLASSOK(uint8_t subklass __attribute__((unused))) |
kotakku | 0:b1ce54272580 | 158 | { |
kotakku | 0:b1ce54272580 | 159 | return true; |
kotakku | 0:b1ce54272580 | 160 | } |
kotakku | 0:b1ce54272580 | 161 | }; |
kotakku | 0:b1ce54272580 | 162 | |
kotakku | 0:b1ce54272580 | 163 | /* USB Setup Packet Structure */ |
kotakku | 0:b1ce54272580 | 164 | typedef struct |
kotakku | 0:b1ce54272580 | 165 | { |
kotakku | 0:b1ce54272580 | 166 | |
kotakku | 0:b1ce54272580 | 167 | union { // offset description |
kotakku | 0:b1ce54272580 | 168 | uint8_t bmRequestType; // 0 Bit-map of request type |
kotakku | 0:b1ce54272580 | 169 | |
kotakku | 0:b1ce54272580 | 170 | struct |
kotakku | 0:b1ce54272580 | 171 | { |
kotakku | 0:b1ce54272580 | 172 | uint8_t recipient : 5; // Recipient of the request |
kotakku | 0:b1ce54272580 | 173 | uint8_t type : 2; // Type of request |
kotakku | 0:b1ce54272580 | 174 | uint8_t direction : 1; // Direction of data X-fer |
kotakku | 0:b1ce54272580 | 175 | } __attribute__((packed)); |
kotakku | 0:b1ce54272580 | 176 | } ReqType_u; |
kotakku | 0:b1ce54272580 | 177 | uint8_t bRequest; // 1 Request |
kotakku | 0:b1ce54272580 | 178 | |
kotakku | 0:b1ce54272580 | 179 | union { |
kotakku | 0:b1ce54272580 | 180 | uint16_t wValue; // 2 Depends on bRequest |
kotakku | 0:b1ce54272580 | 181 | |
kotakku | 0:b1ce54272580 | 182 | struct |
kotakku | 0:b1ce54272580 | 183 | { |
kotakku | 0:b1ce54272580 | 184 | uint8_t wValueLo; |
kotakku | 0:b1ce54272580 | 185 | uint8_t wValueHi; |
kotakku | 0:b1ce54272580 | 186 | } __attribute__((packed)); |
kotakku | 0:b1ce54272580 | 187 | } wVal_u; |
kotakku | 0:b1ce54272580 | 188 | uint16_t wIndex; // 4 Depends on bRequest |
kotakku | 0:b1ce54272580 | 189 | uint16_t wLength; // 6 Depends on bRequest |
kotakku | 0:b1ce54272580 | 190 | } __attribute__((packed)) SETUP_PKT, *PSETUP_PKT; |
kotakku | 0:b1ce54272580 | 191 | |
kotakku | 0:b1ce54272580 | 192 | // Base class for incoming data parser |
kotakku | 0:b1ce54272580 | 193 | |
kotakku | 0:b1ce54272580 | 194 | class USBReadParser |
kotakku | 0:b1ce54272580 | 195 | { |
kotakku | 0:b1ce54272580 | 196 | public: |
kotakku | 0:b1ce54272580 | 197 | virtual void Parse(const uint16_t len, const uint8_t *pbuf, const uint16_t &offset) = 0; |
kotakku | 0:b1ce54272580 | 198 | }; |
kotakku | 0:b1ce54272580 | 199 | |
kotakku | 0:b1ce54272580 | 200 | class USB : public MAX3421E |
kotakku | 0:b1ce54272580 | 201 | { |
kotakku | 0:b1ce54272580 | 202 | AddressPoolImpl<USB_NUMDEVICES> addrPool; |
kotakku | 0:b1ce54272580 | 203 | USBDeviceConfig *devConfig[USB_NUMDEVICES]; |
kotakku | 0:b1ce54272580 | 204 | uint8_t bmHubPre; |
kotakku | 0:b1ce54272580 | 205 | |
kotakku | 0:b1ce54272580 | 206 | private: |
kotakku | 0:b1ce54272580 | 207 | static Timer arduinoTimer; // for millis() & micros() function in Arduino |
kotakku | 0:b1ce54272580 | 208 | public: |
kotakku | 0:b1ce54272580 | 209 | static uint32_t read_ms() { return arduinoTimer.read_ms(); } |
kotakku | 0:b1ce54272580 | 210 | static uint32_t read_us() { return arduinoTimer.read_us(); } |
kotakku | 0:b1ce54272580 | 211 | |
kotakku | 0:b1ce54272580 | 212 | public: |
kotakku | 0:b1ce54272580 | 213 | USB(PinName mosi, PinName miso, PinName sclk, PinName ssel, PinName intr); |
kotakku | 0:b1ce54272580 | 214 | |
kotakku | 0:b1ce54272580 | 215 | void SetHubPreMask() |
kotakku | 0:b1ce54272580 | 216 | { |
kotakku | 0:b1ce54272580 | 217 | bmHubPre |= bmHUBPRE; |
kotakku | 0:b1ce54272580 | 218 | }; |
kotakku | 0:b1ce54272580 | 219 | |
kotakku | 0:b1ce54272580 | 220 | void ResetHubPreMask() |
kotakku | 0:b1ce54272580 | 221 | { |
kotakku | 0:b1ce54272580 | 222 | bmHubPre &= (~bmHUBPRE); |
kotakku | 0:b1ce54272580 | 223 | }; |
kotakku | 0:b1ce54272580 | 224 | |
kotakku | 0:b1ce54272580 | 225 | AddressPool &GetAddressPool() |
kotakku | 0:b1ce54272580 | 226 | { |
kotakku | 0:b1ce54272580 | 227 | return (AddressPool &)addrPool; |
kotakku | 0:b1ce54272580 | 228 | }; |
kotakku | 0:b1ce54272580 | 229 | |
kotakku | 0:b1ce54272580 | 230 | uint8_t RegisterDeviceClass(USBDeviceConfig *pdev) |
kotakku | 0:b1ce54272580 | 231 | { |
kotakku | 0:b1ce54272580 | 232 | for (uint8_t i = 0; i < USB_NUMDEVICES; i++) |
kotakku | 0:b1ce54272580 | 233 | { |
kotakku | 0:b1ce54272580 | 234 | if (!devConfig[i]) |
kotakku | 0:b1ce54272580 | 235 | { |
kotakku | 0:b1ce54272580 | 236 | devConfig[i] = pdev; |
kotakku | 0:b1ce54272580 | 237 | return 0; |
kotakku | 0:b1ce54272580 | 238 | } |
kotakku | 0:b1ce54272580 | 239 | } |
kotakku | 0:b1ce54272580 | 240 | return USB_ERROR_UNABLE_TO_REGISTER_DEVICE_CLASS; |
kotakku | 0:b1ce54272580 | 241 | }; |
kotakku | 0:b1ce54272580 | 242 | |
kotakku | 0:b1ce54272580 | 243 | void ForEachUsbDevice(UsbDeviceHandleFunc pfunc) |
kotakku | 0:b1ce54272580 | 244 | { |
kotakku | 0:b1ce54272580 | 245 | addrPool.ForEachUsbDevice(pfunc); |
kotakku | 0:b1ce54272580 | 246 | }; |
kotakku | 0:b1ce54272580 | 247 | uint8_t getUsbTaskState(void); |
kotakku | 0:b1ce54272580 | 248 | void setUsbTaskState(uint8_t state); |
kotakku | 0:b1ce54272580 | 249 | |
kotakku | 0:b1ce54272580 | 250 | EpInfo *getEpInfoEntry(uint8_t addr, uint8_t ep); |
kotakku | 0:b1ce54272580 | 251 | uint8_t setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo *eprecord_ptr); |
kotakku | 0:b1ce54272580 | 252 | |
kotakku | 0:b1ce54272580 | 253 | /* Control requests */ |
kotakku | 0:b1ce54272580 | 254 | uint8_t getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *dataptr); |
kotakku | 0:b1ce54272580 | 255 | uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t *dataptr); |
kotakku | 0:b1ce54272580 | 256 | |
kotakku | 0:b1ce54272580 | 257 | uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint8_t conf, USBReadParser *p); |
kotakku | 0:b1ce54272580 | 258 | |
kotakku | 0:b1ce54272580 | 259 | uint8_t getStrDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t index, uint16_t langid, uint8_t *dataptr); |
kotakku | 0:b1ce54272580 | 260 | uint8_t setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr); |
kotakku | 0:b1ce54272580 | 261 | uint8_t setConf(uint8_t addr, uint8_t ep, uint8_t conf_value); |
kotakku | 0:b1ce54272580 | 262 | /**/ |
kotakku | 0:b1ce54272580 | 263 | uint8_t ctrlData(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *dataptr, bool direction); |
kotakku | 0:b1ce54272580 | 264 | uint8_t ctrlStatus(uint8_t ep, bool direction, uint16_t nak_limit); |
kotakku | 0:b1ce54272580 | 265 | uint8_t inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t *data, uint8_t bInterval = 0); |
kotakku | 0:b1ce54272580 | 266 | uint8_t outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *data); |
kotakku | 0:b1ce54272580 | 267 | uint8_t dispatchPkt(uint8_t token, uint8_t ep, uint16_t nak_limit); |
kotakku | 0:b1ce54272580 | 268 | |
kotakku | 0:b1ce54272580 | 269 | void Task(void); |
kotakku | 0:b1ce54272580 | 270 | |
kotakku | 0:b1ce54272580 | 271 | uint8_t DefaultAddressing(uint8_t parent, uint8_t port, bool lowspeed); |
kotakku | 0:b1ce54272580 | 272 | uint8_t Configuring(uint8_t parent, uint8_t port, bool lowspeed); |
kotakku | 0:b1ce54272580 | 273 | uint8_t ReleaseDevice(uint8_t addr); |
kotakku | 0:b1ce54272580 | 274 | |
kotakku | 0:b1ce54272580 | 275 | uint8_t ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bRequest, uint8_t wValLo, uint8_t wValHi, |
kotakku | 0:b1ce54272580 | 276 | uint16_t wInd, uint16_t total, uint16_t nbytes, uint8_t *dataptr, USBReadParser *p); |
kotakku | 0:b1ce54272580 | 277 | |
kotakku | 0:b1ce54272580 | 278 | private: |
kotakku | 0:b1ce54272580 | 279 | void init(); |
kotakku | 0:b1ce54272580 | 280 | uint8_t SetAddress(uint8_t addr, uint8_t ep, EpInfo **ppep, uint16_t *nak_limit); |
kotakku | 0:b1ce54272580 | 281 | uint8_t OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8_t *data); |
kotakku | 0:b1ce54272580 | 282 | uint8_t InTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, uint8_t *data, uint8_t bInterval = 0); |
kotakku | 0:b1ce54272580 | 283 | uint8_t AttemptConfig(uint8_t driver, uint8_t parent, uint8_t port, bool lowspeed); |
kotakku | 0:b1ce54272580 | 284 | }; |
kotakku | 0:b1ce54272580 | 285 | |
kotakku | 0:b1ce54272580 | 286 | #if 0 //defined(USB_METHODS_INLINE) |
kotakku | 0:b1ce54272580 | 287 | //get device descriptor |
kotakku | 0:b1ce54272580 | 288 | |
kotakku | 0:b1ce54272580 | 289 | inline uint8_t USB::getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* dataptr) { |
kotakku | 0:b1ce54272580 | 290 | return ( ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, 0x00, USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, dataptr)); |
kotakku | 0:b1ce54272580 | 291 | } |
kotakku | 0:b1ce54272580 | 292 | //get configuration descriptor |
kotakku | 0:b1ce54272580 | 293 | |
kotakku | 0:b1ce54272580 | 294 | inline uint8_t USB::getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t* dataptr) { |
kotakku | 0:b1ce54272580 | 295 | return ( ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, dataptr)); |
kotakku | 0:b1ce54272580 | 296 | } |
kotakku | 0:b1ce54272580 | 297 | //get string descriptor |
kotakku | 0:b1ce54272580 | 298 | |
kotakku | 0:b1ce54272580 | 299 | inline uint8_t USB::getStrDescr(uint8_t addr, uint8_t ep, uint16_t nuint8_ts, uint8_t index, uint16_t langid, uint8_t* dataptr) { |
kotakku | 0:b1ce54272580 | 300 | return ( ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, index, USB_DESCRIPTOR_STRING, langid, nuint8_ts, dataptr)); |
kotakku | 0:b1ce54272580 | 301 | } |
kotakku | 0:b1ce54272580 | 302 | //set address |
kotakku | 0:b1ce54272580 | 303 | |
kotakku | 0:b1ce54272580 | 304 | inline uint8_t USB::setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr) { |
kotakku | 0:b1ce54272580 | 305 | return ( ctrlReq(oldaddr, ep, bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, NULL)); |
kotakku | 0:b1ce54272580 | 306 | } |
kotakku | 0:b1ce54272580 | 307 | //set configuration |
kotakku | 0:b1ce54272580 | 308 | |
kotakku | 0:b1ce54272580 | 309 | inline uint8_t USB::setConf(uint8_t addr, uint8_t ep, uint8_t conf_value) { |
kotakku | 0:b1ce54272580 | 310 | return ( ctrlReq(addr, ep, bmREQ_SET, USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, NULL)); |
kotakku | 0:b1ce54272580 | 311 | } |
kotakku | 0:b1ce54272580 | 312 | |
kotakku | 0:b1ce54272580 | 313 | Timer USB::arduinoTimer; |
kotakku | 0:b1ce54272580 | 314 | |
kotakku | 0:b1ce54272580 | 315 | #endif // defined(USB_METHODS_INLINE) |
kotakku | 0:b1ce54272580 | 316 | |
kotakku | 0:b1ce54272580 | 317 | #endif /* USBCORE_H */ |