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