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/cdcftdi.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 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 | #if !defined(__CDCFTDI_H__) |
kotakku | 0:b1ce54272580 | 18 | #define __CDCFTDI_H__ |
kotakku | 0:b1ce54272580 | 19 | |
kotakku | 0:b1ce54272580 | 20 | #include "Usb.h" |
kotakku | 0:b1ce54272580 | 21 | |
kotakku | 0:b1ce54272580 | 22 | #define bmREQ_FTDI_OUT 0x40 |
kotakku | 0:b1ce54272580 | 23 | #define bmREQ_FTDI_IN 0xc0 |
kotakku | 0:b1ce54272580 | 24 | |
kotakku | 0:b1ce54272580 | 25 | //#define bmREQ_FTDI_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE |
kotakku | 0:b1ce54272580 | 26 | //#define bmREQ_FTDI_IN USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE |
kotakku | 0:b1ce54272580 | 27 | |
kotakku | 0:b1ce54272580 | 28 | #define FTDI_VID 0x0403 // FTDI VID |
kotakku | 0:b1ce54272580 | 29 | #define FTDI_PID 0x6001 // FTDI PID |
kotakku | 0:b1ce54272580 | 30 | |
kotakku | 0:b1ce54272580 | 31 | #define FT232AM 0x0200 |
kotakku | 0:b1ce54272580 | 32 | #define FT232BM 0x0400 |
kotakku | 0:b1ce54272580 | 33 | #define FT2232 0x0500 |
kotakku | 0:b1ce54272580 | 34 | #define FT232R 0x0600 |
kotakku | 0:b1ce54272580 | 35 | |
kotakku | 0:b1ce54272580 | 36 | // Commands |
kotakku | 0:b1ce54272580 | 37 | #define FTDI_SIO_RESET 0 /* Reset the port */ |
kotakku | 0:b1ce54272580 | 38 | #define FTDI_SIO_MODEM_CTRL 1 /* Set the modem control register */ |
kotakku | 0:b1ce54272580 | 39 | #define FTDI_SIO_SET_FLOW_CTRL 2 /* Set flow control register */ |
kotakku | 0:b1ce54272580 | 40 | #define FTDI_SIO_SET_BAUD_RATE 3 /* Set baud rate */ |
kotakku | 0:b1ce54272580 | 41 | #define FTDI_SIO_SET_DATA 4 /* Set the data characteristics of the port */ |
kotakku | 0:b1ce54272580 | 42 | #define FTDI_SIO_GET_MODEM_STATUS 5 /* Retrieve current value of modem status register */ |
kotakku | 0:b1ce54272580 | 43 | #define FTDI_SIO_SET_EVENT_CHAR 6 /* Set the event character */ |
kotakku | 0:b1ce54272580 | 44 | #define FTDI_SIO_SET_ERROR_CHAR 7 /* Set the error character */ |
kotakku | 0:b1ce54272580 | 45 | #define FTDI_SIO_SET_LATENCY_TIMER 9 /* Set the latency timer */ |
kotakku | 0:b1ce54272580 | 46 | #define FTDI_SIO_GET_LATENCY_TIMER 10 /* Get the latency timer */ |
kotakku | 0:b1ce54272580 | 47 | |
kotakku | 0:b1ce54272580 | 48 | #define FTDI_SIO_RESET_SIO 0 |
kotakku | 0:b1ce54272580 | 49 | #define FTDI_SIO_RESET_PURGE_RX 1 |
kotakku | 0:b1ce54272580 | 50 | #define FTDI_SIO_RESET_PURGE_TX 2 |
kotakku | 0:b1ce54272580 | 51 | |
kotakku | 0:b1ce54272580 | 52 | #define FTDI_SIO_SET_DATA_PARITY_NONE (0x0 << 8 ) |
kotakku | 0:b1ce54272580 | 53 | #define FTDI_SIO_SET_DATA_PARITY_ODD (0x1 << 8 ) |
kotakku | 0:b1ce54272580 | 54 | #define FTDI_SIO_SET_DATA_PARITY_EVEN (0x2 << 8 ) |
kotakku | 0:b1ce54272580 | 55 | #define FTDI_SIO_SET_DATA_PARITY_MARK (0x3 << 8 ) |
kotakku | 0:b1ce54272580 | 56 | #define FTDI_SIO_SET_DATA_PARITY_SPACE (0x4 << 8 ) |
kotakku | 0:b1ce54272580 | 57 | #define FTDI_SIO_SET_DATA_STOP_BITS_1 (0x0 << 11) |
kotakku | 0:b1ce54272580 | 58 | #define FTDI_SIO_SET_DATA_STOP_BITS_15 (0x1 << 11) |
kotakku | 0:b1ce54272580 | 59 | #define FTDI_SIO_SET_DATA_STOP_BITS_2 (0x2 << 11) |
kotakku | 0:b1ce54272580 | 60 | #define FTDI_SIO_SET_BREAK (0x1 << 14) |
kotakku | 0:b1ce54272580 | 61 | |
kotakku | 0:b1ce54272580 | 62 | #define FTDI_SIO_SET_DTR_MASK 0x1 |
kotakku | 0:b1ce54272580 | 63 | #define FTDI_SIO_SET_DTR_HIGH ( 1 | ( FTDI_SIO_SET_DTR_MASK << 8)) |
kotakku | 0:b1ce54272580 | 64 | #define FTDI_SIO_SET_DTR_LOW ( 0 | ( FTDI_SIO_SET_DTR_MASK << 8)) |
kotakku | 0:b1ce54272580 | 65 | #define FTDI_SIO_SET_RTS_MASK 0x2 |
kotakku | 0:b1ce54272580 | 66 | #define FTDI_SIO_SET_RTS_HIGH ( 2 | ( FTDI_SIO_SET_RTS_MASK << 8 )) |
kotakku | 0:b1ce54272580 | 67 | #define FTDI_SIO_SET_RTS_LOW ( 0 | ( FTDI_SIO_SET_RTS_MASK << 8 )) |
kotakku | 0:b1ce54272580 | 68 | |
kotakku | 0:b1ce54272580 | 69 | #define FTDI_SIO_DISABLE_FLOW_CTRL 0x0 |
kotakku | 0:b1ce54272580 | 70 | #define FTDI_SIO_RTS_CTS_HS (0x1 << 8) |
kotakku | 0:b1ce54272580 | 71 | #define FTDI_SIO_DTR_DSR_HS (0x2 << 8) |
kotakku | 0:b1ce54272580 | 72 | #define FTDI_SIO_XON_XOFF_HS (0x4 << 8) |
kotakku | 0:b1ce54272580 | 73 | |
kotakku | 0:b1ce54272580 | 74 | #define FTDI_SIO_CTS_MASK 0x10 |
kotakku | 0:b1ce54272580 | 75 | #define FTDI_SIO_DSR_MASK 0x20 |
kotakku | 0:b1ce54272580 | 76 | #define FTDI_SIO_RI_MASK 0x40 |
kotakku | 0:b1ce54272580 | 77 | #define FTDI_SIO_RLSD_MASK 0x80 |
kotakku | 0:b1ce54272580 | 78 | |
kotakku | 0:b1ce54272580 | 79 | class FTDI; |
kotakku | 0:b1ce54272580 | 80 | |
kotakku | 0:b1ce54272580 | 81 | class FTDIAsyncOper { |
kotakku | 0:b1ce54272580 | 82 | public: |
kotakku | 0:b1ce54272580 | 83 | |
kotakku | 0:b1ce54272580 | 84 | virtual uint8_t OnInit(FTDI *pftdi __attribute__((unused))) { |
kotakku | 0:b1ce54272580 | 85 | return 0; |
kotakku | 0:b1ce54272580 | 86 | }; |
kotakku | 0:b1ce54272580 | 87 | |
kotakku | 0:b1ce54272580 | 88 | virtual uint8_t OnRelease(FTDI *pftdi __attribute__((unused))) { |
kotakku | 0:b1ce54272580 | 89 | return 0; |
kotakku | 0:b1ce54272580 | 90 | }; |
kotakku | 0:b1ce54272580 | 91 | }; |
kotakku | 0:b1ce54272580 | 92 | |
kotakku | 0:b1ce54272580 | 93 | |
kotakku | 0:b1ce54272580 | 94 | // Only single port chips are currently supported by the library, |
kotakku | 0:b1ce54272580 | 95 | // so only three endpoints are allocated. |
kotakku | 0:b1ce54272580 | 96 | #define FTDI_MAX_ENDPOINTS 3 |
kotakku | 0:b1ce54272580 | 97 | |
kotakku | 0:b1ce54272580 | 98 | class FTDI : public USBDeviceConfig, public UsbConfigXtracter { |
kotakku | 0:b1ce54272580 | 99 | static const uint8_t epDataInIndex; // DataIn endpoint index |
kotakku | 0:b1ce54272580 | 100 | static const uint8_t epDataOutIndex; // DataOUT endpoint index |
kotakku | 0:b1ce54272580 | 101 | static const uint8_t epInterruptInIndex; // InterruptIN endpoint index |
kotakku | 0:b1ce54272580 | 102 | |
kotakku | 0:b1ce54272580 | 103 | FTDIAsyncOper *pAsync; |
kotakku | 0:b1ce54272580 | 104 | USB *pUsb; |
kotakku | 0:b1ce54272580 | 105 | uint8_t bAddress; |
kotakku | 0:b1ce54272580 | 106 | uint8_t bConfNum; // configuration number |
kotakku | 0:b1ce54272580 | 107 | uint8_t bNumIface; // number of interfaces in the configuration |
kotakku | 0:b1ce54272580 | 108 | uint8_t bNumEP; // total number of EP in the configuration |
kotakku | 0:b1ce54272580 | 109 | uint32_t qNextPollTime; // next poll time |
kotakku | 0:b1ce54272580 | 110 | volatile bool bPollEnable; // poll enable flag |
kotakku | 0:b1ce54272580 | 111 | volatile bool ready; //device ready indicator |
kotakku | 0:b1ce54272580 | 112 | uint16_t wFTDIType; // Type of FTDI chip |
kotakku | 0:b1ce54272580 | 113 | uint16_t wIdProduct; // expected PID |
kotakku | 0:b1ce54272580 | 114 | |
kotakku | 0:b1ce54272580 | 115 | EpInfo epInfo[FTDI_MAX_ENDPOINTS]; |
kotakku | 0:b1ce54272580 | 116 | |
kotakku | 0:b1ce54272580 | 117 | void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr); |
kotakku | 0:b1ce54272580 | 118 | |
kotakku | 0:b1ce54272580 | 119 | public: |
kotakku | 0:b1ce54272580 | 120 | FTDI(USB *pusb, FTDIAsyncOper *pasync, uint16_t idProduct = FTDI_PID); |
kotakku | 0:b1ce54272580 | 121 | |
kotakku | 0:b1ce54272580 | 122 | uint8_t SetBaudRate(uint32_t baud); |
kotakku | 0:b1ce54272580 | 123 | uint8_t SetModemControl(uint16_t control); |
kotakku | 0:b1ce54272580 | 124 | uint8_t SetFlowControl(uint8_t protocol, uint8_t xon = 0x11, uint8_t xoff = 0x13); |
kotakku | 0:b1ce54272580 | 125 | uint8_t SetData(uint16_t databm); |
kotakku | 0:b1ce54272580 | 126 | uint8_t SetLatency(uint8_t l); |
kotakku | 0:b1ce54272580 | 127 | uint8_t GetLatency(uint8_t *l); |
kotakku | 0:b1ce54272580 | 128 | |
kotakku | 0:b1ce54272580 | 129 | // Methods for receiving and sending data |
kotakku | 0:b1ce54272580 | 130 | uint8_t RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr); |
kotakku | 0:b1ce54272580 | 131 | uint8_t SndData(uint16_t nbytes, uint8_t *dataptr); |
kotakku | 0:b1ce54272580 | 132 | |
kotakku | 0:b1ce54272580 | 133 | // USBDeviceConfig implementation |
kotakku | 0:b1ce54272580 | 134 | uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed); |
kotakku | 0:b1ce54272580 | 135 | uint8_t Release(); |
kotakku | 0:b1ce54272580 | 136 | uint8_t Poll(); |
kotakku | 0:b1ce54272580 | 137 | |
kotakku | 0:b1ce54272580 | 138 | virtual uint8_t GetAddress() { |
kotakku | 0:b1ce54272580 | 139 | return bAddress; |
kotakku | 0:b1ce54272580 | 140 | }; |
kotakku | 0:b1ce54272580 | 141 | |
kotakku | 0:b1ce54272580 | 142 | // UsbConfigXtracter implementation |
kotakku | 0:b1ce54272580 | 143 | void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep); |
kotakku | 0:b1ce54272580 | 144 | |
kotakku | 0:b1ce54272580 | 145 | virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) { |
kotakku | 0:b1ce54272580 | 146 | return (vid == FTDI_VID && pid == FTDI_PID); |
kotakku | 0:b1ce54272580 | 147 | } |
kotakku | 0:b1ce54272580 | 148 | virtual bool isReady() { |
kotakku | 0:b1ce54272580 | 149 | return ready; |
kotakku | 0:b1ce54272580 | 150 | }; |
kotakku | 0:b1ce54272580 | 151 | |
kotakku | 0:b1ce54272580 | 152 | }; |
kotakku | 0:b1ce54272580 | 153 | |
kotakku | 0:b1ce54272580 | 154 | #endif // __CDCFTDI_H__ |
kotakku | 0:b1ce54272580 | 155 |