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/cdcprolific.h@0:b1ce54272580, 2020-01-18 (annotated)
- Committer:
- kotakku
- Date:
- Sat Jan 18 15:06:35 2020 +0000
- Revision:
- 0:b1ce54272580
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 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(__CDCPROLIFIC_H__) |
kotakku | 0:b1ce54272580 | 18 | #define __CDCPROLIFIC_H__ |
kotakku | 0:b1ce54272580 | 19 | |
kotakku | 0:b1ce54272580 | 20 | #include "cdcacm.h" |
kotakku | 0:b1ce54272580 | 21 | |
kotakku | 0:b1ce54272580 | 22 | //#define PL2303_COMPAT // Uncomment it if you have compatibility problems |
kotakku | 0:b1ce54272580 | 23 | |
kotakku | 0:b1ce54272580 | 24 | #define PL_VID 0x067B |
kotakku | 0:b1ce54272580 | 25 | #define CHECK_PID(pid) ( pid != 0x2303 && pid != 0x0609 ) |
kotakku | 0:b1ce54272580 | 26 | |
kotakku | 0:b1ce54272580 | 27 | //#define PL_PID 0x0609 |
kotakku | 0:b1ce54272580 | 28 | |
kotakku | 0:b1ce54272580 | 29 | #define PROLIFIC_REV_H 0x0202 |
kotakku | 0:b1ce54272580 | 30 | #define PROLIFIC_REV_X 0x0300 |
kotakku | 0:b1ce54272580 | 31 | #define PROLIFIC_REV_HX_CHIP_D 0x0400 |
kotakku | 0:b1ce54272580 | 32 | #define PROLIFIC_REV_1 0x0001 |
kotakku | 0:b1ce54272580 | 33 | |
kotakku | 0:b1ce54272580 | 34 | #define kXOnChar '\x11' |
kotakku | 0:b1ce54272580 | 35 | #define kXOffChar '\x13' |
kotakku | 0:b1ce54272580 | 36 | |
kotakku | 0:b1ce54272580 | 37 | #define SPECIAL_SHIFT (5) |
kotakku | 0:b1ce54272580 | 38 | #define SPECIAL_MASK ((1<<SPECIAL_SHIFT) - 1) |
kotakku | 0:b1ce54272580 | 39 | #define STATE_ALL ( PD_RS232_S_MASK | PD_S_MASK ) |
kotakku | 0:b1ce54272580 | 40 | #define FLOW_RX_AUTO ( PD_RS232_A_RFR | PD_RS232_A_DTR | PD_RS232_A_RXO ) |
kotakku | 0:b1ce54272580 | 41 | #define FLOW_TX_AUTO ( PD_RS232_A_CTS | PD_RS232_A_DSR | PD_RS232_A_TXO | PD_RS232_A_DCD ) |
kotakku | 0:b1ce54272580 | 42 | #define CAN_BE_AUTO ( FLOW_RX_AUTO | FLOW_TX_AUTO ) |
kotakku | 0:b1ce54272580 | 43 | #define CAN_NOTIFY ( PD_RS232_N_MASK ) |
kotakku | 0:b1ce54272580 | 44 | #define EXTERNAL_MASK ( PD_S_MASK | (PD_RS232_S_MASK & ~PD_RS232_S_LOOP) ) |
kotakku | 0:b1ce54272580 | 45 | #define INTERNAL_DELAY ( PD_RS232_S_LOOP ) |
kotakku | 0:b1ce54272580 | 46 | #define DEFAULT_AUTO ( PD_RS232_A_DTR | PD_RS232_A_RFR | PD_RS232_A_CTS | PD_RS232_A_DSR ) |
kotakku | 0:b1ce54272580 | 47 | #define DEFAULT_NOTIFY 0x00 |
kotakku | 0:b1ce54272580 | 48 | #define DEFAULT_STATE ( PD_S_TX_ENABLE | PD_S_RX_ENABLE | PD_RS232_A_TXO | PD_RS232_A_RXO ) |
kotakku | 0:b1ce54272580 | 49 | |
kotakku | 0:b1ce54272580 | 50 | #define CONTINUE_SEND 1 |
kotakku | 0:b1ce54272580 | 51 | #define PAUSE_SEND 2 |
kotakku | 0:b1ce54272580 | 52 | |
kotakku | 0:b1ce54272580 | 53 | #define kRxAutoFlow ((UInt32)( PD_RS232_A_RFR | PD_RS232_A_DTR | PD_RS232_A_RXO )) |
kotakku | 0:b1ce54272580 | 54 | #define kTxAutoFlow ((UInt32)( PD_RS232_A_CTS | PD_RS232_A_DSR | PD_RS232_A_TXO | PD_RS232_A_DCD )) |
kotakku | 0:b1ce54272580 | 55 | #define kControl_StateMask ((UInt32)( PD_RS232_S_CTS | PD_RS232_S_DSR | PD_RS232_S_CAR | PD_RS232_S_RI )) |
kotakku | 0:b1ce54272580 | 56 | #define kRxQueueState ((UInt32)( PD_S_RXQ_EMPTY | PD_S_RXQ_LOW_WATER | PD_S_RXQ_HIGH_WATER | PD_S_RXQ_FULL )) |
kotakku | 0:b1ce54272580 | 57 | #define kTxQueueState ((UInt32)( PD_S_TXQ_EMPTY | PD_S_TXQ_LOW_WATER | PD_S_TXQ_HIGH_WATER | PD_S_TXQ_FULL )) |
kotakku | 0:b1ce54272580 | 58 | |
kotakku | 0:b1ce54272580 | 59 | #define kCONTROL_DTR 0x01 |
kotakku | 0:b1ce54272580 | 60 | #define kCONTROL_RTS 0x02 |
kotakku | 0:b1ce54272580 | 61 | |
kotakku | 0:b1ce54272580 | 62 | #define kStateTransientMask 0x74 |
kotakku | 0:b1ce54272580 | 63 | #define kBreakError 0x04 |
kotakku | 0:b1ce54272580 | 64 | #define kFrameError 0x10 |
kotakku | 0:b1ce54272580 | 65 | #define kParityError 0x20 |
kotakku | 0:b1ce54272580 | 66 | #define kOverrunError 0x40 |
kotakku | 0:b1ce54272580 | 67 | |
kotakku | 0:b1ce54272580 | 68 | #define kCTS 0x80 |
kotakku | 0:b1ce54272580 | 69 | #define kDSR 0x02 |
kotakku | 0:b1ce54272580 | 70 | #define kRI 0x08 |
kotakku | 0:b1ce54272580 | 71 | #define kDCD 0x01 |
kotakku | 0:b1ce54272580 | 72 | #define kHandshakeInMask ((UInt32)( PD_RS232_S_CTS | PD_RS232_S_DSR | PD_RS232_S_CAR | PD_RS232_S_RI )) |
kotakku | 0:b1ce54272580 | 73 | |
kotakku | 0:b1ce54272580 | 74 | #define VENDOR_WRITE_REQUEST_TYPE 0x40 |
kotakku | 0:b1ce54272580 | 75 | #define VENDOR_WRITE_REQUEST 0x01 |
kotakku | 0:b1ce54272580 | 76 | |
kotakku | 0:b1ce54272580 | 77 | #define VENDOR_READ_REQUEST_TYPE 0xc0 |
kotakku | 0:b1ce54272580 | 78 | #define VENDOR_READ_REQUEST 0x01 |
kotakku | 0:b1ce54272580 | 79 | |
kotakku | 0:b1ce54272580 | 80 | // Device Configuration Registers (DCR0, DCR1, DCR2) |
kotakku | 0:b1ce54272580 | 81 | #define SET_DCR0 0x00 |
kotakku | 0:b1ce54272580 | 82 | #define GET_DCR0 0x80 |
kotakku | 0:b1ce54272580 | 83 | #define DCR0_INIT 0x01 |
kotakku | 0:b1ce54272580 | 84 | #define DCR0_INIT_H 0x41 |
kotakku | 0:b1ce54272580 | 85 | #define DCR0_INIT_X 0x61 |
kotakku | 0:b1ce54272580 | 86 | |
kotakku | 0:b1ce54272580 | 87 | #define SET_DCR1 0x01 |
kotakku | 0:b1ce54272580 | 88 | #define GET_DCR1 0x81 |
kotakku | 0:b1ce54272580 | 89 | #define DCR1_INIT_H 0x80 |
kotakku | 0:b1ce54272580 | 90 | #define DCR1_INIT_X 0x00 |
kotakku | 0:b1ce54272580 | 91 | |
kotakku | 0:b1ce54272580 | 92 | #define SET_DCR2 0x02 |
kotakku | 0:b1ce54272580 | 93 | #define GET_DCR2 0x82 |
kotakku | 0:b1ce54272580 | 94 | #define DCR2_INIT_H 0x24 |
kotakku | 0:b1ce54272580 | 95 | #define DCR2_INIT_X 0x44 |
kotakku | 0:b1ce54272580 | 96 | |
kotakku | 0:b1ce54272580 | 97 | // On-chip Data Buffers: |
kotakku | 0:b1ce54272580 | 98 | #define RESET_DOWNSTREAM_DATA_PIPE 0x08 |
kotakku | 0:b1ce54272580 | 99 | #define RESET_UPSTREAM_DATA_PIPE 0x09 |
kotakku | 0:b1ce54272580 | 100 | |
kotakku | 0:b1ce54272580 | 101 | |
kotakku | 0:b1ce54272580 | 102 | #define PL_MAX_ENDPOINTS 4 |
kotakku | 0:b1ce54272580 | 103 | |
kotakku | 0:b1ce54272580 | 104 | enum tXO_State { |
kotakku | 0:b1ce54272580 | 105 | kXOnSent = -2, |
kotakku | 0:b1ce54272580 | 106 | kXOffSent = -1, |
kotakku | 0:b1ce54272580 | 107 | kXO_Idle = 0, |
kotakku | 0:b1ce54272580 | 108 | kXOffNeeded = 1, |
kotakku | 0:b1ce54272580 | 109 | kXOnNeeded = 2 |
kotakku | 0:b1ce54272580 | 110 | }; |
kotakku | 0:b1ce54272580 | 111 | |
kotakku | 0:b1ce54272580 | 112 | enum pl2303_type { |
kotakku | 0:b1ce54272580 | 113 | unknown, |
kotakku | 0:b1ce54272580 | 114 | type_0, /* don't know the difference between type 0 and */ |
kotakku | 0:b1ce54272580 | 115 | type_1, /* type 1, until someone from prolific tells us... */ |
kotakku | 0:b1ce54272580 | 116 | rev_X, |
kotakku | 0:b1ce54272580 | 117 | rev_HX, /* HX version of the pl2303 chip */ |
kotakku | 0:b1ce54272580 | 118 | rev_H |
kotakku | 0:b1ce54272580 | 119 | }; |
kotakku | 0:b1ce54272580 | 120 | |
kotakku | 0:b1ce54272580 | 121 | |
kotakku | 0:b1ce54272580 | 122 | class PL2303 : public ACM { |
kotakku | 0:b1ce54272580 | 123 | uint16_t wPLType; // Type of chip |
kotakku | 0:b1ce54272580 | 124 | |
kotakku | 0:b1ce54272580 | 125 | public: |
kotakku | 0:b1ce54272580 | 126 | PL2303(USB *pusb, CDCAsyncOper *pasync); |
kotakku | 0:b1ce54272580 | 127 | |
kotakku | 0:b1ce54272580 | 128 | // USBDeviceConfig implementation |
kotakku | 0:b1ce54272580 | 129 | uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed); |
kotakku | 0:b1ce54272580 | 130 | //virtual uint8_t Release(); |
kotakku | 0:b1ce54272580 | 131 | //virtual uint8_t Poll(); |
kotakku | 0:b1ce54272580 | 132 | //virtual uint8_t GetAddress() { return bAddress; }; |
kotakku | 0:b1ce54272580 | 133 | |
kotakku | 0:b1ce54272580 | 134 | //// UsbConfigXtracter implementation |
kotakku | 0:b1ce54272580 | 135 | //virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep); |
kotakku | 0:b1ce54272580 | 136 | |
kotakku | 0:b1ce54272580 | 137 | #ifdef PL2303_COMPAT |
kotakku | 0:b1ce54272580 | 138 | private: |
kotakku | 0:b1ce54272580 | 139 | /* Prolific proprietary requests */ |
kotakku | 0:b1ce54272580 | 140 | uint8_t vendorRead( uint8_t val_lo, uint8_t val_hi, uint16_t index, uint8_t* buf ); |
kotakku | 0:b1ce54272580 | 141 | uint8_t vendorWrite( uint8_t val_lo, uint8_t val_hi, uint8_t index ); |
kotakku | 0:b1ce54272580 | 142 | #endif |
kotakku | 0:b1ce54272580 | 143 | }; |
kotakku | 0:b1ce54272580 | 144 | |
kotakku | 0:b1ce54272580 | 145 | #ifdef PL2303_COMPAT |
kotakku | 0:b1ce54272580 | 146 | /* vendor read request */ |
kotakku | 0:b1ce54272580 | 147 | inline uint8_t PL2303::vendorRead( uint8_t val_lo, uint8_t val_hi, uint16_t index, uint8_t* buf ) |
kotakku | 0:b1ce54272580 | 148 | { |
kotakku | 0:b1ce54272580 | 149 | return( pUsb->ctrlReq(bAddress, 0, VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, val_lo, val_hi, index, 1, 1, buf, NULL )); |
kotakku | 0:b1ce54272580 | 150 | } |
kotakku | 0:b1ce54272580 | 151 | |
kotakku | 0:b1ce54272580 | 152 | /* vendor write request */ |
kotakku | 0:b1ce54272580 | 153 | inline uint8_t PL2303::vendorWrite( uint8_t val_lo, uint8_t val_hi, uint8_t index ) |
kotakku | 0:b1ce54272580 | 154 | { |
kotakku | 0:b1ce54272580 | 155 | return( pUsb->ctrlReq(bAddress, 0, VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, val_lo, val_hi, index, 0, 0, NULL, NULL )); |
kotakku | 0:b1ce54272580 | 156 | } |
kotakku | 0:b1ce54272580 | 157 | #endif |
kotakku | 0:b1ce54272580 | 158 | |
kotakku | 0:b1ce54272580 | 159 | #endif // __CDCPROLIFIC_H__ |
kotakku | 0:b1ce54272580 | 160 |