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/adk.cpp@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 | |
kotakku | 0:b1ce54272580 | 18 | /* Google ADK interface */ |
kotakku | 0:b1ce54272580 | 19 | |
kotakku | 0:b1ce54272580 | 20 | #include "adk.h" |
kotakku | 0:b1ce54272580 | 21 | |
kotakku | 0:b1ce54272580 | 22 | const uint8_t ADK::epDataInIndex = 1; |
kotakku | 0:b1ce54272580 | 23 | const uint8_t ADK::epDataOutIndex = 2; |
kotakku | 0:b1ce54272580 | 24 | |
kotakku | 0:b1ce54272580 | 25 | ADK::ADK(USB *p, const char* manufacturer, |
kotakku | 0:b1ce54272580 | 26 | const char* model, |
kotakku | 0:b1ce54272580 | 27 | const char* description, |
kotakku | 0:b1ce54272580 | 28 | const char* version, |
kotakku | 0:b1ce54272580 | 29 | const char* uri, |
kotakku | 0:b1ce54272580 | 30 | const char* serial) : |
kotakku | 0:b1ce54272580 | 31 | |
kotakku | 0:b1ce54272580 | 32 | /* ADK ID Strings */ |
kotakku | 0:b1ce54272580 | 33 | manufacturer(manufacturer), |
kotakku | 0:b1ce54272580 | 34 | model(model), |
kotakku | 0:b1ce54272580 | 35 | description(description), |
kotakku | 0:b1ce54272580 | 36 | version(version), |
kotakku | 0:b1ce54272580 | 37 | uri(uri), |
kotakku | 0:b1ce54272580 | 38 | serial(serial), |
kotakku | 0:b1ce54272580 | 39 | pUsb(p), //pointer to USB class instance - mandatory |
kotakku | 0:b1ce54272580 | 40 | bAddress(0), //device address - mandatory |
kotakku | 0:b1ce54272580 | 41 | bConfNum(0), //configuration number |
kotakku | 0:b1ce54272580 | 42 | bNumEP(1), //if config descriptor needs to be parsed |
kotakku | 0:b1ce54272580 | 43 | ready(false) { |
kotakku | 0:b1ce54272580 | 44 | // initialize endpoint data structures |
kotakku | 0:b1ce54272580 | 45 | for(uint8_t i = 0; i < ADK_MAX_ENDPOINTS; i++) { |
kotakku | 0:b1ce54272580 | 46 | epInfo[i].epAddr = 0; |
kotakku | 0:b1ce54272580 | 47 | epInfo[i].maxPktSize = (i) ? 0 : 8; |
kotakku | 0:b1ce54272580 | 48 | epInfo[i].bmSndToggle = 0; |
kotakku | 0:b1ce54272580 | 49 | epInfo[i].bmRcvToggle = 0; |
kotakku | 0:b1ce54272580 | 50 | epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER; |
kotakku | 0:b1ce54272580 | 51 | }//for(uint8_t i=0; i<ADK_MAX_ENDPOINTS; i++... |
kotakku | 0:b1ce54272580 | 52 | |
kotakku | 0:b1ce54272580 | 53 | // register in USB subsystem |
kotakku | 0:b1ce54272580 | 54 | if(pUsb) { |
kotakku | 0:b1ce54272580 | 55 | pUsb->RegisterDeviceClass(this); //set devConfig[] entry |
kotakku | 0:b1ce54272580 | 56 | } |
kotakku | 0:b1ce54272580 | 57 | } |
kotakku | 0:b1ce54272580 | 58 | |
kotakku | 0:b1ce54272580 | 59 | uint8_t ADK::ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed) { |
kotakku | 0:b1ce54272580 | 60 | return Init(parent, port, lowspeed); // Just call Init. Yes, really! |
kotakku | 0:b1ce54272580 | 61 | } |
kotakku | 0:b1ce54272580 | 62 | |
kotakku | 0:b1ce54272580 | 63 | /* Connection initialization of an Android phone */ |
kotakku | 0:b1ce54272580 | 64 | uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed) { |
kotakku | 0:b1ce54272580 | 65 | uint8_t buf[sizeof (USB_DEVICE_DESCRIPTOR)]; |
kotakku | 0:b1ce54272580 | 66 | USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf); |
kotakku | 0:b1ce54272580 | 67 | uint8_t rcode; |
kotakku | 0:b1ce54272580 | 68 | uint8_t num_of_conf; // number of configurations |
kotakku | 0:b1ce54272580 | 69 | UsbDevice *p = NULL; |
kotakku | 0:b1ce54272580 | 70 | EpInfo *oldep_ptr = NULL; |
kotakku | 0:b1ce54272580 | 71 | |
kotakku | 0:b1ce54272580 | 72 | // get memory address of USB device address pool |
kotakku | 0:b1ce54272580 | 73 | AddressPool &addrPool = pUsb->GetAddressPool(); |
kotakku | 0:b1ce54272580 | 74 | |
kotakku | 0:b1ce54272580 | 75 | USBTRACE("\r\nADK Init"); |
kotakku | 0:b1ce54272580 | 76 | |
kotakku | 0:b1ce54272580 | 77 | // check if address has already been assigned to an instance |
kotakku | 0:b1ce54272580 | 78 | if(bAddress) { |
kotakku | 0:b1ce54272580 | 79 | USBTRACE("\r\nAddress in use"); |
kotakku | 0:b1ce54272580 | 80 | return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE; |
kotakku | 0:b1ce54272580 | 81 | } |
kotakku | 0:b1ce54272580 | 82 | |
kotakku | 0:b1ce54272580 | 83 | // Get pointer to pseudo device with address 0 assigned |
kotakku | 0:b1ce54272580 | 84 | p = addrPool.GetUsbDevicePtr(0); |
kotakku | 0:b1ce54272580 | 85 | |
kotakku | 0:b1ce54272580 | 86 | if(!p) { |
kotakku | 0:b1ce54272580 | 87 | USBTRACE("\r\nAddress not found"); |
kotakku | 0:b1ce54272580 | 88 | return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL; |
kotakku | 0:b1ce54272580 | 89 | } |
kotakku | 0:b1ce54272580 | 90 | |
kotakku | 0:b1ce54272580 | 91 | if(!p->epinfo) { |
kotakku | 0:b1ce54272580 | 92 | USBTRACE("epinfo is null\r\n"); |
kotakku | 0:b1ce54272580 | 93 | return USB_ERROR_EPINFO_IS_NULL; |
kotakku | 0:b1ce54272580 | 94 | } |
kotakku | 0:b1ce54272580 | 95 | |
kotakku | 0:b1ce54272580 | 96 | // Save old pointer to EP_RECORD of address 0 |
kotakku | 0:b1ce54272580 | 97 | oldep_ptr = p->epinfo; |
kotakku | 0:b1ce54272580 | 98 | |
kotakku | 0:b1ce54272580 | 99 | // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence |
kotakku | 0:b1ce54272580 | 100 | p->epinfo = epInfo; |
kotakku | 0:b1ce54272580 | 101 | |
kotakku | 0:b1ce54272580 | 102 | p->lowspeed = lowspeed; |
kotakku | 0:b1ce54272580 | 103 | |
kotakku | 0:b1ce54272580 | 104 | // Get device descriptor |
kotakku | 0:b1ce54272580 | 105 | rcode = pUsb->getDevDescr(0, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*)buf); |
kotakku | 0:b1ce54272580 | 106 | |
kotakku | 0:b1ce54272580 | 107 | // Restore p->epinfo |
kotakku | 0:b1ce54272580 | 108 | p->epinfo = oldep_ptr; |
kotakku | 0:b1ce54272580 | 109 | |
kotakku | 0:b1ce54272580 | 110 | if(rcode) { |
kotakku | 0:b1ce54272580 | 111 | goto FailGetDevDescr; |
kotakku | 0:b1ce54272580 | 112 | } |
kotakku | 0:b1ce54272580 | 113 | |
kotakku | 0:b1ce54272580 | 114 | // Allocate new address according to device class |
kotakku | 0:b1ce54272580 | 115 | bAddress = addrPool.AllocAddress(parent, false, port); |
kotakku | 0:b1ce54272580 | 116 | |
kotakku | 0:b1ce54272580 | 117 | // Extract Max Packet Size from device descriptor |
kotakku | 0:b1ce54272580 | 118 | epInfo[0].maxPktSize = udd->bMaxPacketSize0; |
kotakku | 0:b1ce54272580 | 119 | |
kotakku | 0:b1ce54272580 | 120 | // Assign new address to the device |
kotakku | 0:b1ce54272580 | 121 | rcode = pUsb->setAddr(0, 0, bAddress); |
kotakku | 0:b1ce54272580 | 122 | if(rcode) { |
kotakku | 0:b1ce54272580 | 123 | p->lowspeed = false; |
kotakku | 0:b1ce54272580 | 124 | addrPool.FreeAddress(bAddress); |
kotakku | 0:b1ce54272580 | 125 | bAddress = 0; |
kotakku | 0:b1ce54272580 | 126 | //USBTRACE2("setAddr:",rcode); |
kotakku | 0:b1ce54272580 | 127 | return rcode; |
kotakku | 0:b1ce54272580 | 128 | }//if (rcode... |
kotakku | 0:b1ce54272580 | 129 | |
kotakku | 0:b1ce54272580 | 130 | //USBTRACE2("\r\nAddr:", bAddress); |
kotakku | 0:b1ce54272580 | 131 | // Spec says you should wait at least 200ms. |
kotakku | 0:b1ce54272580 | 132 | //delay(300); |
kotakku | 0:b1ce54272580 | 133 | |
kotakku | 0:b1ce54272580 | 134 | p->lowspeed = false; |
kotakku | 0:b1ce54272580 | 135 | |
kotakku | 0:b1ce54272580 | 136 | //get pointer to assigned address record |
kotakku | 0:b1ce54272580 | 137 | p = addrPool.GetUsbDevicePtr(bAddress); |
kotakku | 0:b1ce54272580 | 138 | if(!p) { |
kotakku | 0:b1ce54272580 | 139 | return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL; |
kotakku | 0:b1ce54272580 | 140 | } |
kotakku | 0:b1ce54272580 | 141 | |
kotakku | 0:b1ce54272580 | 142 | p->lowspeed = lowspeed; |
kotakku | 0:b1ce54272580 | 143 | |
kotakku | 0:b1ce54272580 | 144 | // Assign epInfo to epinfo pointer - only EP0 is known |
kotakku | 0:b1ce54272580 | 145 | rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo); |
kotakku | 0:b1ce54272580 | 146 | if(rcode) { |
kotakku | 0:b1ce54272580 | 147 | goto FailSetDevTblEntry; |
kotakku | 0:b1ce54272580 | 148 | } |
kotakku | 0:b1ce54272580 | 149 | |
kotakku | 0:b1ce54272580 | 150 | //check if ADK device is already in accessory mode; if yes, configure and exit |
kotakku | 0:b1ce54272580 | 151 | if(udd->idVendor == ADK_VID && |
kotakku | 0:b1ce54272580 | 152 | (udd->idProduct == ADK_PID || udd->idProduct == ADB_PID)) { |
kotakku | 0:b1ce54272580 | 153 | USBTRACE("\r\nAcc.mode device detected"); |
kotakku | 0:b1ce54272580 | 154 | /* go through configurations, find first bulk-IN, bulk-OUT EP, fill epInfo and quit */ |
kotakku | 0:b1ce54272580 | 155 | num_of_conf = udd->bNumConfigurations; |
kotakku | 0:b1ce54272580 | 156 | |
kotakku | 0:b1ce54272580 | 157 | //USBTRACE2("\r\nNC:",num_of_conf); |
kotakku | 0:b1ce54272580 | 158 | for(uint8_t i = 0; i < num_of_conf; i++) { |
kotakku | 0:b1ce54272580 | 159 | ConfigDescParser < 0, 0, 0, 0 > confDescrParser(this); |
kotakku | 0:b1ce54272580 | 160 | delay(1); |
kotakku | 0:b1ce54272580 | 161 | rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser); |
kotakku | 0:b1ce54272580 | 162 | #if defined(XOOM) |
kotakku | 0:b1ce54272580 | 163 | //added by Jaylen Scott Vanorden |
kotakku | 0:b1ce54272580 | 164 | if(rcode) { |
kotakku | 0:b1ce54272580 | 165 | USBTRACE2("\r\nGot 1st bad code for config: ", rcode); |
kotakku | 0:b1ce54272580 | 166 | // Try once more |
kotakku | 0:b1ce54272580 | 167 | rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser); |
kotakku | 0:b1ce54272580 | 168 | } |
kotakku | 0:b1ce54272580 | 169 | #endif |
kotakku | 0:b1ce54272580 | 170 | if(rcode) { |
kotakku | 0:b1ce54272580 | 171 | goto FailGetConfDescr; |
kotakku | 0:b1ce54272580 | 172 | } |
kotakku | 0:b1ce54272580 | 173 | if(bNumEP > 2) { |
kotakku | 0:b1ce54272580 | 174 | break; |
kotakku | 0:b1ce54272580 | 175 | } |
kotakku | 0:b1ce54272580 | 176 | } // for (uint8_t i=0; i<num_of_conf; i++... |
kotakku | 0:b1ce54272580 | 177 | |
kotakku | 0:b1ce54272580 | 178 | if(bNumEP == 3) { |
kotakku | 0:b1ce54272580 | 179 | // Assign epInfo to epinfo pointer - this time all 3 endpoins |
kotakku | 0:b1ce54272580 | 180 | rcode = pUsb->setEpInfoEntry(bAddress, 3, epInfo); |
kotakku | 0:b1ce54272580 | 181 | if(rcode) { |
kotakku | 0:b1ce54272580 | 182 | goto FailSetDevTblEntry; |
kotakku | 0:b1ce54272580 | 183 | } |
kotakku | 0:b1ce54272580 | 184 | } |
kotakku | 0:b1ce54272580 | 185 | |
kotakku | 0:b1ce54272580 | 186 | // Set Configuration Value |
kotakku | 0:b1ce54272580 | 187 | rcode = pUsb->setConf(bAddress, 0, bConfNum); |
kotakku | 0:b1ce54272580 | 188 | if(rcode) { |
kotakku | 0:b1ce54272580 | 189 | goto FailSetConfDescr; |
kotakku | 0:b1ce54272580 | 190 | } |
kotakku | 0:b1ce54272580 | 191 | /* print endpoint structure */ |
kotakku | 0:b1ce54272580 | 192 | /* |
kotakku | 0:b1ce54272580 | 193 | USBTRACE("\r\nEndpoint Structure:"); |
kotakku | 0:b1ce54272580 | 194 | USBTRACE("\r\nEP0:"); |
kotakku | 0:b1ce54272580 | 195 | USBTRACE2("\r\nAddr: ", epInfo[0].epAddr); |
kotakku | 0:b1ce54272580 | 196 | USBTRACE2("\r\nMax.pkt.size: ", epInfo[0].maxPktSize); |
kotakku | 0:b1ce54272580 | 197 | USBTRACE2("\r\nAttr: ", epInfo[0].epAttribs); |
kotakku | 0:b1ce54272580 | 198 | USBTRACE("\r\nEpout:"); |
kotakku | 0:b1ce54272580 | 199 | USBTRACE2("\r\nAddr: ", epInfo[epDataOutIndex].epAddr); |
kotakku | 0:b1ce54272580 | 200 | USBTRACE2("\r\nMax.pkt.size: ", epInfo[epDataOutIndex].maxPktSize); |
kotakku | 0:b1ce54272580 | 201 | USBTRACE2("\r\nAttr: ", epInfo[epDataOutIndex].epAttribs); |
kotakku | 0:b1ce54272580 | 202 | USBTRACE("\r\nEpin:"); |
kotakku | 0:b1ce54272580 | 203 | USBTRACE2("\r\nAddr: ", epInfo[epDataInIndex].epAddr); |
kotakku | 0:b1ce54272580 | 204 | USBTRACE2("\r\nMax.pkt.size: ", epInfo[epDataInIndex].maxPktSize); |
kotakku | 0:b1ce54272580 | 205 | USBTRACE2("\r\nAttr: ", epInfo[epDataInIndex].epAttribs); |
kotakku | 0:b1ce54272580 | 206 | */ |
kotakku | 0:b1ce54272580 | 207 | |
kotakku | 0:b1ce54272580 | 208 | USBTRACE("\r\nConfiguration successful"); |
kotakku | 0:b1ce54272580 | 209 | ready = true; |
kotakku | 0:b1ce54272580 | 210 | return 0; //successful configuration |
kotakku | 0:b1ce54272580 | 211 | }//if( buf->idVendor == ADK_VID... |
kotakku | 0:b1ce54272580 | 212 | |
kotakku | 0:b1ce54272580 | 213 | //probe device - get accessory protocol revision |
kotakku | 0:b1ce54272580 | 214 | { |
kotakku | 0:b1ce54272580 | 215 | uint16_t adkproto = -1; |
kotakku | 0:b1ce54272580 | 216 | delay(1); |
kotakku | 0:b1ce54272580 | 217 | rcode = getProto((uint8_t*) & adkproto); |
kotakku | 0:b1ce54272580 | 218 | #if defined(XOOM) |
kotakku | 0:b1ce54272580 | 219 | //added by Jaylen Scott Vanorden |
kotakku | 0:b1ce54272580 | 220 | if(rcode) { |
kotakku | 0:b1ce54272580 | 221 | USBTRACE2("\r\nGot 1st bad code for proto: ", rcode); |
kotakku | 0:b1ce54272580 | 222 | // Try once more |
kotakku | 0:b1ce54272580 | 223 | rcode = getProto((uint8_t*) & adkproto); |
kotakku | 0:b1ce54272580 | 224 | } |
kotakku | 0:b1ce54272580 | 225 | #endif |
kotakku | 0:b1ce54272580 | 226 | if(rcode) { |
kotakku | 0:b1ce54272580 | 227 | goto FailGetProto; //init fails |
kotakku | 0:b1ce54272580 | 228 | } |
kotakku | 0:b1ce54272580 | 229 | USBTRACE2("\r\nADK protocol rev. ", adkproto); |
kotakku | 0:b1ce54272580 | 230 | } |
kotakku | 0:b1ce54272580 | 231 | |
kotakku | 0:b1ce54272580 | 232 | delay(100); |
kotakku | 0:b1ce54272580 | 233 | |
kotakku | 0:b1ce54272580 | 234 | //sending ID strings |
kotakku | 0:b1ce54272580 | 235 | sendStr(ACCESSORY_STRING_MANUFACTURER, manufacturer); |
kotakku | 0:b1ce54272580 | 236 | delay(10); |
kotakku | 0:b1ce54272580 | 237 | sendStr(ACCESSORY_STRING_MODEL, model); |
kotakku | 0:b1ce54272580 | 238 | delay(10); |
kotakku | 0:b1ce54272580 | 239 | sendStr(ACCESSORY_STRING_DESCRIPTION, description); |
kotakku | 0:b1ce54272580 | 240 | delay(10); |
kotakku | 0:b1ce54272580 | 241 | sendStr(ACCESSORY_STRING_VERSION, version); |
kotakku | 0:b1ce54272580 | 242 | delay(10); |
kotakku | 0:b1ce54272580 | 243 | sendStr(ACCESSORY_STRING_URI, uri); |
kotakku | 0:b1ce54272580 | 244 | delay(10); |
kotakku | 0:b1ce54272580 | 245 | sendStr(ACCESSORY_STRING_SERIAL, serial); |
kotakku | 0:b1ce54272580 | 246 | |
kotakku | 0:b1ce54272580 | 247 | delay(100); |
kotakku | 0:b1ce54272580 | 248 | |
kotakku | 0:b1ce54272580 | 249 | //switch to accessory mode |
kotakku | 0:b1ce54272580 | 250 | //the Android phone will reset |
kotakku | 0:b1ce54272580 | 251 | rcode = switchAcc(); |
kotakku | 0:b1ce54272580 | 252 | if(rcode) { |
kotakku | 0:b1ce54272580 | 253 | goto FailSwAcc; //init fails |
kotakku | 0:b1ce54272580 | 254 | } |
kotakku | 0:b1ce54272580 | 255 | rcode = USB_ERROR_CONFIG_REQUIRES_ADDITIONAL_RESET; |
kotakku | 0:b1ce54272580 | 256 | delay(100); // Give Android a chance to do its reset. This is a guess, and possibly could be lower. |
kotakku | 0:b1ce54272580 | 257 | goto SwAttempt; //switch to accessory mode attempted |
kotakku | 0:b1ce54272580 | 258 | |
kotakku | 0:b1ce54272580 | 259 | /* diagnostic messages */ |
kotakku | 0:b1ce54272580 | 260 | FailGetDevDescr: |
kotakku | 0:b1ce54272580 | 261 | #ifdef DEBUG_USB_HOST |
kotakku | 0:b1ce54272580 | 262 | NotifyFailGetDevDescr(rcode); |
kotakku | 0:b1ce54272580 | 263 | goto Fail; |
kotakku | 0:b1ce54272580 | 264 | #endif |
kotakku | 0:b1ce54272580 | 265 | |
kotakku | 0:b1ce54272580 | 266 | FailSetDevTblEntry: |
kotakku | 0:b1ce54272580 | 267 | #ifdef DEBUG_USB_HOST |
kotakku | 0:b1ce54272580 | 268 | NotifyFailSetDevTblEntry(rcode); |
kotakku | 0:b1ce54272580 | 269 | goto Fail; |
kotakku | 0:b1ce54272580 | 270 | #endif |
kotakku | 0:b1ce54272580 | 271 | |
kotakku | 0:b1ce54272580 | 272 | FailGetConfDescr: |
kotakku | 0:b1ce54272580 | 273 | #ifdef DEBUG_USB_HOST |
kotakku | 0:b1ce54272580 | 274 | NotifyFailGetConfDescr(rcode); |
kotakku | 0:b1ce54272580 | 275 | goto Fail; |
kotakku | 0:b1ce54272580 | 276 | #endif |
kotakku | 0:b1ce54272580 | 277 | |
kotakku | 0:b1ce54272580 | 278 | FailSetConfDescr: |
kotakku | 0:b1ce54272580 | 279 | #ifdef DEBUG_USB_HOST |
kotakku | 0:b1ce54272580 | 280 | NotifyFailSetConfDescr(rcode); |
kotakku | 0:b1ce54272580 | 281 | goto Fail; |
kotakku | 0:b1ce54272580 | 282 | #endif |
kotakku | 0:b1ce54272580 | 283 | |
kotakku | 0:b1ce54272580 | 284 | FailGetProto: |
kotakku | 0:b1ce54272580 | 285 | #ifdef DEBUG_USB_HOST |
kotakku | 0:b1ce54272580 | 286 | USBTRACE("\r\ngetProto:"); |
kotakku | 0:b1ce54272580 | 287 | goto Fail; |
kotakku | 0:b1ce54272580 | 288 | #endif |
kotakku | 0:b1ce54272580 | 289 | |
kotakku | 0:b1ce54272580 | 290 | FailSwAcc: |
kotakku | 0:b1ce54272580 | 291 | #ifdef DEBUG_USB_HOST |
kotakku | 0:b1ce54272580 | 292 | USBTRACE("\r\nswAcc:"); |
kotakku | 0:b1ce54272580 | 293 | goto Fail; |
kotakku | 0:b1ce54272580 | 294 | #endif |
kotakku | 0:b1ce54272580 | 295 | |
kotakku | 0:b1ce54272580 | 296 | //FailOnInit: |
kotakku | 0:b1ce54272580 | 297 | // USBTRACE("OnInit:"); |
kotakku | 0:b1ce54272580 | 298 | // goto Fail; |
kotakku | 0:b1ce54272580 | 299 | // |
kotakku | 0:b1ce54272580 | 300 | SwAttempt: |
kotakku | 0:b1ce54272580 | 301 | #ifdef DEBUG_USB_HOST |
kotakku | 0:b1ce54272580 | 302 | USBTRACE("\r\nAccessory mode switch attempt"); |
kotakku | 0:b1ce54272580 | 303 | Fail: |
kotakku | 0:b1ce54272580 | 304 | #endif |
kotakku | 0:b1ce54272580 | 305 | //USBTRACE2("\r\nADK Init Failed, error code: ", rcode); |
kotakku | 0:b1ce54272580 | 306 | //NotifyFail(rcode); |
kotakku | 0:b1ce54272580 | 307 | Release(); |
kotakku | 0:b1ce54272580 | 308 | return rcode; |
kotakku | 0:b1ce54272580 | 309 | } |
kotakku | 0:b1ce54272580 | 310 | |
kotakku | 0:b1ce54272580 | 311 | /* Extracts bulk-IN and bulk-OUT endpoint information from config descriptor */ |
kotakku | 0:b1ce54272580 | 312 | void ADK::EndpointXtract(uint8_t conf, uint8_t iface __attribute__((unused)), uint8_t alt __attribute__((unused)), uint8_t proto __attribute__((unused)), const USB_ENDPOINT_DESCRIPTOR *pep) { |
kotakku | 0:b1ce54272580 | 313 | //ErrorMessage<uint8_t>(PSTR("Conf.Val"), conf); |
kotakku | 0:b1ce54272580 | 314 | //ErrorMessage<uint8_t>(PSTR("Iface Num"), iface); |
kotakku | 0:b1ce54272580 | 315 | //ErrorMessage<uint8_t>(PSTR("Alt.Set"), alt); |
kotakku | 0:b1ce54272580 | 316 | |
kotakku | 0:b1ce54272580 | 317 | //added by Yuuichi Akagawa |
kotakku | 0:b1ce54272580 | 318 | if(bNumEP == 3) { |
kotakku | 0:b1ce54272580 | 319 | return; |
kotakku | 0:b1ce54272580 | 320 | } |
kotakku | 0:b1ce54272580 | 321 | |
kotakku | 0:b1ce54272580 | 322 | bConfNum = conf; |
kotakku | 0:b1ce54272580 | 323 | |
kotakku | 0:b1ce54272580 | 324 | if((pep->bmAttributes & bmUSB_TRANSFER_TYPE) == USB_TRANSFER_TYPE_BULK) { |
kotakku | 0:b1ce54272580 | 325 | uint8_t index = ((pep->bEndpointAddress & 0x80) == 0x80) ? epDataInIndex : epDataOutIndex; |
kotakku | 0:b1ce54272580 | 326 | // Fill in the endpoint info structure |
kotakku | 0:b1ce54272580 | 327 | epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F); |
kotakku | 0:b1ce54272580 | 328 | epInfo[index].maxPktSize = (uint8_t)pep->wMaxPacketSize; |
kotakku | 0:b1ce54272580 | 329 | |
kotakku | 0:b1ce54272580 | 330 | bNumEP++; |
kotakku | 0:b1ce54272580 | 331 | |
kotakku | 0:b1ce54272580 | 332 | //PrintEndpointDescriptor(pep); |
kotakku | 0:b1ce54272580 | 333 | } |
kotakku | 0:b1ce54272580 | 334 | } |
kotakku | 0:b1ce54272580 | 335 | |
kotakku | 0:b1ce54272580 | 336 | /* Performs a cleanup after failed Init() attempt */ |
kotakku | 0:b1ce54272580 | 337 | uint8_t ADK::Release() { |
kotakku | 0:b1ce54272580 | 338 | pUsb->GetAddressPool().FreeAddress(bAddress); |
kotakku | 0:b1ce54272580 | 339 | |
kotakku | 0:b1ce54272580 | 340 | bNumEP = 1; //must have to be reset to 1 |
kotakku | 0:b1ce54272580 | 341 | |
kotakku | 0:b1ce54272580 | 342 | bAddress = 0; |
kotakku | 0:b1ce54272580 | 343 | ready = false; |
kotakku | 0:b1ce54272580 | 344 | return 0; |
kotakku | 0:b1ce54272580 | 345 | } |
kotakku | 0:b1ce54272580 | 346 | |
kotakku | 0:b1ce54272580 | 347 | uint8_t ADK::RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr) { |
kotakku | 0:b1ce54272580 | 348 | //USBTRACE2("\r\nAddr: ", bAddress ); |
kotakku | 0:b1ce54272580 | 349 | //USBTRACE2("\r\nEP: ",epInfo[epDataInIndex].epAddr); |
kotakku | 0:b1ce54272580 | 350 | return pUsb->inTransfer(bAddress, epInfo[epDataInIndex].epAddr, bytes_rcvd, dataptr); |
kotakku | 0:b1ce54272580 | 351 | } |
kotakku | 0:b1ce54272580 | 352 | |
kotakku | 0:b1ce54272580 | 353 | uint8_t ADK::SndData(uint16_t nbytes, uint8_t *dataptr) { |
kotakku | 0:b1ce54272580 | 354 | return pUsb->outTransfer(bAddress, epInfo[epDataOutIndex].epAddr, nbytes, dataptr); |
kotakku | 0:b1ce54272580 | 355 | } |
kotakku | 0:b1ce54272580 | 356 | |
kotakku | 0:b1ce54272580 | 357 | void ADK::PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr) { |
kotakku | 0:b1ce54272580 | 358 | Notify(PSTR("Endpoint descriptor:"), 0x80); |
kotakku | 0:b1ce54272580 | 359 | Notify(PSTR("\r\nLength:\t\t"), 0x80); |
kotakku | 0:b1ce54272580 | 360 | D_PrintHex<uint8_t > (ep_ptr->bLength, 0x80); |
kotakku | 0:b1ce54272580 | 361 | Notify(PSTR("\r\nType:\t\t"), 0x80); |
kotakku | 0:b1ce54272580 | 362 | D_PrintHex<uint8_t > (ep_ptr->bDescriptorType, 0x80); |
kotakku | 0:b1ce54272580 | 363 | Notify(PSTR("\r\nAddress:\t"), 0x80); |
kotakku | 0:b1ce54272580 | 364 | D_PrintHex<uint8_t > (ep_ptr->bEndpointAddress, 0x80); |
kotakku | 0:b1ce54272580 | 365 | Notify(PSTR("\r\nAttributes:\t"), 0x80); |
kotakku | 0:b1ce54272580 | 366 | D_PrintHex<uint8_t > (ep_ptr->bmAttributes, 0x80); |
kotakku | 0:b1ce54272580 | 367 | Notify(PSTR("\r\nMaxPktSize:\t"), 0x80); |
kotakku | 0:b1ce54272580 | 368 | D_PrintHex<uint16_t > (ep_ptr->wMaxPacketSize, 0x80); |
kotakku | 0:b1ce54272580 | 369 | Notify(PSTR("\r\nPoll Intrv:\t"), 0x80); |
kotakku | 0:b1ce54272580 | 370 | D_PrintHex<uint8_t > (ep_ptr->bInterval, 0x80); |
kotakku | 0:b1ce54272580 | 371 | Notify(PSTR("\r\n"), 0x80); |
kotakku | 0:b1ce54272580 | 372 | } |
kotakku | 0:b1ce54272580 | 373 |