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
hidcomposite.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 | #include "hidcomposite.h" |
hudakz | 0:84353c479782 | 19 | |
hudakz | 0:84353c479782 | 20 | HIDComposite::HIDComposite(USB *p) : |
hudakz | 0:84353c479782 | 21 | USBHID(p), |
hudakz | 0:84353c479782 | 22 | qNextPollTime(0), |
hudakz | 0:84353c479782 | 23 | pollInterval(0), |
hudakz | 0:84353c479782 | 24 | bPollEnable(false), |
hudakz | 0:84353c479782 | 25 | bHasReportId(false) { |
hudakz | 0:84353c479782 | 26 | Initialize(); |
hudakz | 0:84353c479782 | 27 | |
hudakz | 0:84353c479782 | 28 | if(pUsb) |
hudakz | 0:84353c479782 | 29 | pUsb->RegisterDeviceClass(this); |
hudakz | 0:84353c479782 | 30 | } |
hudakz | 0:84353c479782 | 31 | |
hudakz | 0:84353c479782 | 32 | uint16_t HIDComposite::GetHidClassDescrLen(uint8_t type, uint8_t num) { |
hudakz | 0:84353c479782 | 33 | for(uint8_t i = 0, n = 0; i < HID_MAX_HID_CLASS_DESCRIPTORS; i++) { |
hudakz | 0:84353c479782 | 34 | if(descrInfo[i].bDescrType == type) { |
hudakz | 0:84353c479782 | 35 | if(n == num) |
hudakz | 0:84353c479782 | 36 | return descrInfo[i].wDescriptorLength; |
hudakz | 0:84353c479782 | 37 | n++; |
hudakz | 0:84353c479782 | 38 | } |
hudakz | 0:84353c479782 | 39 | } |
hudakz | 0:84353c479782 | 40 | return 0; |
hudakz | 0:84353c479782 | 41 | } |
hudakz | 0:84353c479782 | 42 | |
hudakz | 0:84353c479782 | 43 | void HIDComposite::Initialize() { |
hudakz | 0:84353c479782 | 44 | for(uint8_t i = 0; i < MAX_REPORT_PARSERS; i++) { |
hudakz | 0:84353c479782 | 45 | rptParsers[i].rptId = 0; |
hudakz | 0:84353c479782 | 46 | rptParsers[i].rptParser = NULL; |
hudakz | 0:84353c479782 | 47 | } |
hudakz | 0:84353c479782 | 48 | for(uint8_t i = 0; i < HID_MAX_HID_CLASS_DESCRIPTORS; i++) { |
hudakz | 0:84353c479782 | 49 | descrInfo[i].bDescrType = 0; |
hudakz | 0:84353c479782 | 50 | descrInfo[i].wDescriptorLength = 0; |
hudakz | 0:84353c479782 | 51 | } |
hudakz | 0:84353c479782 | 52 | for(uint8_t i = 0; i < maxHidInterfaces; i++) { |
hudakz | 0:84353c479782 | 53 | hidInterfaces[i].bmInterface = 0; |
hudakz | 0:84353c479782 | 54 | hidInterfaces[i].bmProtocol = 0; |
hudakz | 0:84353c479782 | 55 | |
hudakz | 0:84353c479782 | 56 | for(uint8_t j = 0; j < maxEpPerInterface; j++) |
hudakz | 0:84353c479782 | 57 | hidInterfaces[i].epIndex[j] = 0; |
hudakz | 0:84353c479782 | 58 | } |
hudakz | 0:84353c479782 | 59 | for(uint8_t i = 0; i < totalEndpoints; i++) { |
hudakz | 0:84353c479782 | 60 | epInfo[i].epAddr = 0; |
hudakz | 0:84353c479782 | 61 | epInfo[i].maxPktSize = (i) ? 0 : 8; |
hudakz | 0:84353c479782 | 62 | epInfo[i].bmSndToggle = 0; |
hudakz | 0:84353c479782 | 63 | epInfo[i].bmRcvToggle = 0; |
hudakz | 0:84353c479782 | 64 | epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER; |
hudakz | 0:84353c479782 | 65 | } |
hudakz | 0:84353c479782 | 66 | bNumEP = 1; |
hudakz | 0:84353c479782 | 67 | bNumIface = 0; |
hudakz | 0:84353c479782 | 68 | bConfNum = 0; |
hudakz | 0:84353c479782 | 69 | pollInterval = 0; |
hudakz | 0:84353c479782 | 70 | } |
hudakz | 0:84353c479782 | 71 | |
hudakz | 0:84353c479782 | 72 | bool HIDComposite::SetReportParser(uint8_t id, HIDReportParser *prs) { |
hudakz | 0:84353c479782 | 73 | for(uint8_t i = 0; i < MAX_REPORT_PARSERS; i++) { |
hudakz | 0:84353c479782 | 74 | if(rptParsers[i].rptId == 0 && rptParsers[i].rptParser == NULL) { |
hudakz | 0:84353c479782 | 75 | rptParsers[i].rptId = id; |
hudakz | 0:84353c479782 | 76 | rptParsers[i].rptParser = prs; |
hudakz | 0:84353c479782 | 77 | return true; |
hudakz | 0:84353c479782 | 78 | } |
hudakz | 0:84353c479782 | 79 | } |
hudakz | 0:84353c479782 | 80 | return false; |
hudakz | 0:84353c479782 | 81 | } |
hudakz | 0:84353c479782 | 82 | |
hudakz | 0:84353c479782 | 83 | HIDReportParser* HIDComposite::GetReportParser(uint8_t id) { |
hudakz | 0:84353c479782 | 84 | if(!bHasReportId) |
hudakz | 0:84353c479782 | 85 | return ((rptParsers[0].rptParser) ? rptParsers[0].rptParser : NULL); |
hudakz | 0:84353c479782 | 86 | |
hudakz | 0:84353c479782 | 87 | for(uint8_t i = 0; i < MAX_REPORT_PARSERS; i++) { |
hudakz | 0:84353c479782 | 88 | if(rptParsers[i].rptId == id) |
hudakz | 0:84353c479782 | 89 | return rptParsers[i].rptParser; |
hudakz | 0:84353c479782 | 90 | } |
hudakz | 0:84353c479782 | 91 | return NULL; |
hudakz | 0:84353c479782 | 92 | } |
hudakz | 0:84353c479782 | 93 | |
hudakz | 0:84353c479782 | 94 | uint8_t HIDComposite::Init(uint8_t parent, uint8_t port, bool lowspeed) { |
hudakz | 0:84353c479782 | 95 | const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR); |
hudakz | 0:84353c479782 | 96 | |
hudakz | 0:84353c479782 | 97 | uint8_t buf[constBufSize]; |
hudakz | 0:84353c479782 | 98 | USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf); |
hudakz | 0:84353c479782 | 99 | uint8_t rcode; |
hudakz | 0:84353c479782 | 100 | UsbDevice *p = NULL; |
hudakz | 0:84353c479782 | 101 | EpInfo *oldep_ptr = NULL; |
hudakz | 0:84353c479782 | 102 | uint8_t len = 0; |
hudakz | 0:84353c479782 | 103 | |
hudakz | 0:84353c479782 | 104 | uint8_t num_of_conf; // number of configurations |
hudakz | 0:84353c479782 | 105 | //uint8_t num_of_intf; // number of interfaces |
hudakz | 0:84353c479782 | 106 | |
hudakz | 0:84353c479782 | 107 | AddressPool &addrPool = pUsb->GetAddressPool(); |
hudakz | 0:84353c479782 | 108 | |
hudakz | 0:84353c479782 | 109 | USBTRACE("HU Init\r\n"); |
hudakz | 0:84353c479782 | 110 | |
hudakz | 0:84353c479782 | 111 | if(bAddress) |
hudakz | 0:84353c479782 | 112 | return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE; |
hudakz | 0:84353c479782 | 113 | |
hudakz | 0:84353c479782 | 114 | // Get pointer to pseudo device with address 0 assigned |
hudakz | 0:84353c479782 | 115 | p = addrPool.GetUsbDevicePtr(0); |
hudakz | 0:84353c479782 | 116 | |
hudakz | 0:84353c479782 | 117 | if(!p) |
hudakz | 0:84353c479782 | 118 | return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL; |
hudakz | 0:84353c479782 | 119 | |
hudakz | 0:84353c479782 | 120 | if(!p->epinfo) { |
hudakz | 0:84353c479782 | 121 | USBTRACE("epinfo\r\n"); |
hudakz | 0:84353c479782 | 122 | return USB_ERROR_EPINFO_IS_NULL; |
hudakz | 0:84353c479782 | 123 | } |
hudakz | 0:84353c479782 | 124 | |
hudakz | 0:84353c479782 | 125 | // Save old pointer to EP_RECORD of address 0 |
hudakz | 0:84353c479782 | 126 | oldep_ptr = p->epinfo; |
hudakz | 0:84353c479782 | 127 | |
hudakz | 0:84353c479782 | 128 | // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence |
hudakz | 0:84353c479782 | 129 | p->epinfo = epInfo; |
hudakz | 0:84353c479782 | 130 | |
hudakz | 0:84353c479782 | 131 | p->lowspeed = lowspeed; |
hudakz | 0:84353c479782 | 132 | |
hudakz | 0:84353c479782 | 133 | // Get device descriptor |
hudakz | 0:84353c479782 | 134 | rcode = pUsb->getDevDescr(0, 0, 8, (uint8_t*)buf); |
hudakz | 0:84353c479782 | 135 | |
hudakz | 0:84353c479782 | 136 | if(!rcode) |
hudakz | 0:84353c479782 | 137 | len = (buf[0] > constBufSize) ? constBufSize : buf[0]; |
hudakz | 0:84353c479782 | 138 | |
hudakz | 0:84353c479782 | 139 | if(rcode) { |
hudakz | 0:84353c479782 | 140 | // Restore p->epinfo |
hudakz | 0:84353c479782 | 141 | p->epinfo = oldep_ptr; |
hudakz | 0:84353c479782 | 142 | |
hudakz | 0:84353c479782 | 143 | goto FailGetDevDescr; |
hudakz | 0:84353c479782 | 144 | } |
hudakz | 0:84353c479782 | 145 | |
hudakz | 0:84353c479782 | 146 | // Restore p->epinfo |
hudakz | 0:84353c479782 | 147 | p->epinfo = oldep_ptr; |
hudakz | 0:84353c479782 | 148 | |
hudakz | 0:84353c479782 | 149 | // Allocate new address according to device class |
hudakz | 0:84353c479782 | 150 | bAddress = addrPool.AllocAddress(parent, false, port); |
hudakz | 0:84353c479782 | 151 | |
hudakz | 0:84353c479782 | 152 | if(!bAddress) |
hudakz | 0:84353c479782 | 153 | return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL; |
hudakz | 0:84353c479782 | 154 | |
hudakz | 0:84353c479782 | 155 | // Extract Max Packet Size from the device descriptor |
hudakz | 0:84353c479782 | 156 | epInfo[0].maxPktSize = udd->bMaxPacketSize0; |
hudakz | 0:84353c479782 | 157 | |
hudakz | 0:84353c479782 | 158 | // Assign new address to the device |
hudakz | 0:84353c479782 | 159 | rcode = pUsb->setAddr(0, 0, bAddress); |
hudakz | 0:84353c479782 | 160 | |
hudakz | 0:84353c479782 | 161 | if(rcode) { |
hudakz | 0:84353c479782 | 162 | p->lowspeed = false; |
hudakz | 0:84353c479782 | 163 | addrPool.FreeAddress(bAddress); |
hudakz | 0:84353c479782 | 164 | bAddress = 0; |
hudakz | 0:84353c479782 | 165 | USBTRACE2("setAddr:", rcode); |
hudakz | 0:84353c479782 | 166 | return rcode; |
hudakz | 0:84353c479782 | 167 | } |
hudakz | 0:84353c479782 | 168 | |
hudakz | 0:84353c479782 | 169 | //delay(2); //per USB 2.0 sect.9.2.6.3 |
hudakz | 0:84353c479782 | 170 | |
hudakz | 0:84353c479782 | 171 | USBTRACE2("Addr:", bAddress); |
hudakz | 0:84353c479782 | 172 | |
hudakz | 0:84353c479782 | 173 | p->lowspeed = false; |
hudakz | 0:84353c479782 | 174 | |
hudakz | 0:84353c479782 | 175 | p = addrPool.GetUsbDevicePtr(bAddress); |
hudakz | 0:84353c479782 | 176 | |
hudakz | 0:84353c479782 | 177 | if(!p) |
hudakz | 0:84353c479782 | 178 | return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL; |
hudakz | 0:84353c479782 | 179 | |
hudakz | 0:84353c479782 | 180 | p->lowspeed = lowspeed; |
hudakz | 0:84353c479782 | 181 | |
hudakz | 0:84353c479782 | 182 | if(len) |
hudakz | 0:84353c479782 | 183 | rcode = pUsb->getDevDescr(bAddress, 0, len, (uint8_t*)buf); |
hudakz | 0:84353c479782 | 184 | |
hudakz | 0:84353c479782 | 185 | if(rcode) |
hudakz | 0:84353c479782 | 186 | goto FailGetDevDescr; |
hudakz | 0:84353c479782 | 187 | |
hudakz | 0:84353c479782 | 188 | VID = udd->idVendor; // Can be used by classes that inherits this class to check the VID and PID of the connected device |
hudakz | 0:84353c479782 | 189 | PID = udd->idProduct; |
hudakz | 0:84353c479782 | 190 | |
hudakz | 0:84353c479782 | 191 | num_of_conf = udd->bNumConfigurations; |
hudakz | 0:84353c479782 | 192 | |
hudakz | 0:84353c479782 | 193 | // Assign epInfo to epinfo pointer |
hudakz | 0:84353c479782 | 194 | rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo); |
hudakz | 0:84353c479782 | 195 | |
hudakz | 0:84353c479782 | 196 | if(rcode) |
hudakz | 0:84353c479782 | 197 | goto FailSetDevTblEntry; |
hudakz | 0:84353c479782 | 198 | |
hudakz | 0:84353c479782 | 199 | USBTRACE2("NC:", num_of_conf); |
hudakz | 0:84353c479782 | 200 | |
hudakz | 0:84353c479782 | 201 | for(uint8_t i = 0; i < num_of_conf; i++) { |
hudakz | 0:84353c479782 | 202 | //HexDumper<USBReadParser, uint16_t, uint16_t> HexDump; |
hudakz | 0:84353c479782 | 203 | ConfigDescParser<USB_CLASS_HID, 0, 0, |
hudakz | 0:84353c479782 | 204 | CP_MASK_COMPARE_CLASS> confDescrParser(this); |
hudakz | 0:84353c479782 | 205 | |
hudakz | 0:84353c479782 | 206 | //rcode = pUsb->getConfDescr(bAddress, 0, i, &HexDump); |
hudakz | 0:84353c479782 | 207 | rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser); |
hudakz | 0:84353c479782 | 208 | |
hudakz | 0:84353c479782 | 209 | if(rcode) |
hudakz | 0:84353c479782 | 210 | goto FailGetConfDescr; |
hudakz | 0:84353c479782 | 211 | |
hudakz | 0:84353c479782 | 212 | if(bNumEP > 1) |
hudakz | 0:84353c479782 | 213 | break; |
hudakz | 0:84353c479782 | 214 | } // for |
hudakz | 0:84353c479782 | 215 | |
hudakz | 0:84353c479782 | 216 | if(bNumEP < 2) |
hudakz | 0:84353c479782 | 217 | return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED; |
hudakz | 0:84353c479782 | 218 | |
hudakz | 0:84353c479782 | 219 | // Assign epInfo to epinfo pointer |
hudakz | 0:84353c479782 | 220 | rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo); |
hudakz | 0:84353c479782 | 221 | |
hudakz | 0:84353c479782 | 222 | USBTRACE2("Cnf:", bConfNum); |
hudakz | 0:84353c479782 | 223 | |
hudakz | 0:84353c479782 | 224 | // Set Configuration Value |
hudakz | 0:84353c479782 | 225 | rcode = pUsb->setConf(bAddress, 0, bConfNum); |
hudakz | 0:84353c479782 | 226 | |
hudakz | 0:84353c479782 | 227 | if(rcode) |
hudakz | 0:84353c479782 | 228 | goto FailSetConfDescr; |
hudakz | 0:84353c479782 | 229 | |
hudakz | 0:84353c479782 | 230 | USBTRACE2("NumIface:", bNumIface); |
hudakz | 0:84353c479782 | 231 | |
hudakz | 0:84353c479782 | 232 | for(uint8_t i = 0; i < bNumIface; i++) { |
hudakz | 0:84353c479782 | 233 | if(hidInterfaces[i].epIndex[epInterruptInIndex] == 0) |
hudakz | 0:84353c479782 | 234 | continue; |
hudakz | 0:84353c479782 | 235 | |
hudakz | 0:84353c479782 | 236 | USBTRACE2("SetIdle:", hidInterfaces[i].bmInterface); |
hudakz | 0:84353c479782 | 237 | |
hudakz | 0:84353c479782 | 238 | rcode = SetIdle(hidInterfaces[i].bmInterface, 0, 0); |
hudakz | 0:84353c479782 | 239 | |
hudakz | 0:84353c479782 | 240 | if(rcode && rcode != hrSTALL) |
hudakz | 0:84353c479782 | 241 | goto FailSetIdle; |
hudakz | 0:84353c479782 | 242 | } |
hudakz | 0:84353c479782 | 243 | |
hudakz | 0:84353c479782 | 244 | USBTRACE("HU configured\r\n"); |
hudakz | 0:84353c479782 | 245 | |
hudakz | 0:84353c479782 | 246 | OnInitSuccessful(); |
hudakz | 0:84353c479782 | 247 | |
hudakz | 0:84353c479782 | 248 | bPollEnable = true; |
hudakz | 0:84353c479782 | 249 | return 0; |
hudakz | 0:84353c479782 | 250 | |
hudakz | 0:84353c479782 | 251 | FailGetDevDescr: |
hudakz | 0:84353c479782 | 252 | #ifdef DEBUG_USB_HOST |
hudakz | 0:84353c479782 | 253 | NotifyFailGetDevDescr(); |
hudakz | 0:84353c479782 | 254 | goto Fail; |
hudakz | 0:84353c479782 | 255 | #endif |
hudakz | 0:84353c479782 | 256 | |
hudakz | 0:84353c479782 | 257 | FailSetDevTblEntry: |
hudakz | 0:84353c479782 | 258 | #ifdef DEBUG_USB_HOST |
hudakz | 0:84353c479782 | 259 | NotifyFailSetDevTblEntry(); |
hudakz | 0:84353c479782 | 260 | goto Fail; |
hudakz | 0:84353c479782 | 261 | #endif |
hudakz | 0:84353c479782 | 262 | |
hudakz | 0:84353c479782 | 263 | FailGetConfDescr: |
hudakz | 0:84353c479782 | 264 | #ifdef DEBUG_USB_HOST |
hudakz | 0:84353c479782 | 265 | NotifyFailGetConfDescr(); |
hudakz | 0:84353c479782 | 266 | goto Fail; |
hudakz | 0:84353c479782 | 267 | #endif |
hudakz | 0:84353c479782 | 268 | |
hudakz | 0:84353c479782 | 269 | FailSetConfDescr: |
hudakz | 0:84353c479782 | 270 | #ifdef DEBUG_USB_HOST |
hudakz | 0:84353c479782 | 271 | NotifyFailSetConfDescr(); |
hudakz | 0:84353c479782 | 272 | goto Fail; |
hudakz | 0:84353c479782 | 273 | #endif |
hudakz | 0:84353c479782 | 274 | |
hudakz | 0:84353c479782 | 275 | |
hudakz | 0:84353c479782 | 276 | FailSetIdle: |
hudakz | 0:84353c479782 | 277 | #ifdef DEBUG_USB_HOST |
hudakz | 0:84353c479782 | 278 | USBTRACE("SetIdle:"); |
hudakz | 0:84353c479782 | 279 | #endif |
hudakz | 0:84353c479782 | 280 | |
hudakz | 0:84353c479782 | 281 | #ifdef DEBUG_USB_HOST |
hudakz | 0:84353c479782 | 282 | Fail: |
hudakz | 0:84353c479782 | 283 | NotifyFail(rcode); |
hudakz | 0:84353c479782 | 284 | #endif |
hudakz | 0:84353c479782 | 285 | Release(); |
hudakz | 0:84353c479782 | 286 | return rcode; |
hudakz | 0:84353c479782 | 287 | } |
hudakz | 0:84353c479782 | 288 | |
hudakz | 0:84353c479782 | 289 | HIDComposite::HIDInterface* HIDComposite::FindInterface(uint8_t iface, uint8_t alt, uint8_t proto) { |
hudakz | 0:84353c479782 | 290 | for(uint8_t i = 0; i < bNumIface && i < maxHidInterfaces; i++) |
hudakz | 0:84353c479782 | 291 | if(hidInterfaces[i].bmInterface == iface && hidInterfaces[i].bmAltSet == alt |
hudakz | 0:84353c479782 | 292 | && hidInterfaces[i].bmProtocol == proto) |
hudakz | 0:84353c479782 | 293 | return hidInterfaces + i; |
hudakz | 0:84353c479782 | 294 | return NULL; |
hudakz | 0:84353c479782 | 295 | } |
hudakz | 0:84353c479782 | 296 | |
hudakz | 0:84353c479782 | 297 | void HIDComposite::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *pep) { |
hudakz | 0:84353c479782 | 298 | //ErrorMessage<uint8_t>(PSTR("\r\nConf.Val"), conf); |
hudakz | 0:84353c479782 | 299 | //ErrorMessage<uint8_t>(PSTR("Iface Num"), iface); |
hudakz | 0:84353c479782 | 300 | //ErrorMessage<uint8_t>(PSTR("Alt.Set"), alt); |
hudakz | 0:84353c479782 | 301 | |
hudakz | 0:84353c479782 | 302 | bConfNum = conf; |
hudakz | 0:84353c479782 | 303 | |
hudakz | 0:84353c479782 | 304 | uint8_t index = 0; |
hudakz | 0:84353c479782 | 305 | HIDInterface *piface = FindInterface(iface, alt, proto); |
hudakz | 0:84353c479782 | 306 | |
hudakz | 0:84353c479782 | 307 | // Fill in interface structure in case of new interface |
hudakz | 0:84353c479782 | 308 | if(!piface) { |
hudakz | 0:84353c479782 | 309 | piface = hidInterfaces + bNumIface; |
hudakz | 0:84353c479782 | 310 | piface->bmInterface = iface; |
hudakz | 0:84353c479782 | 311 | piface->bmAltSet = alt; |
hudakz | 0:84353c479782 | 312 | piface->bmProtocol = proto; |
hudakz | 0:84353c479782 | 313 | bNumIface++; |
hudakz | 0:84353c479782 | 314 | } |
hudakz | 0:84353c479782 | 315 | |
hudakz | 0:84353c479782 | 316 | if((pep->bmAttributes & bmUSB_TRANSFER_TYPE) == USB_TRANSFER_TYPE_INTERRUPT) |
hudakz | 0:84353c479782 | 317 | index = (pep->bEndpointAddress & 0x80) == 0x80 ? epInterruptInIndex : epInterruptOutIndex; |
hudakz | 0:84353c479782 | 318 | |
hudakz | 0:84353c479782 | 319 | if(!SelectInterface(iface, proto)) |
hudakz | 0:84353c479782 | 320 | index = 0; |
hudakz | 0:84353c479782 | 321 | |
hudakz | 0:84353c479782 | 322 | if(index) { |
hudakz | 0:84353c479782 | 323 | // Fill in the endpoint info structure |
hudakz | 0:84353c479782 | 324 | epInfo[bNumEP].epAddr = (pep->bEndpointAddress & 0x0F); |
hudakz | 0:84353c479782 | 325 | epInfo[bNumEP].maxPktSize = (uint8_t)pep->wMaxPacketSize; |
hudakz | 0:84353c479782 | 326 | epInfo[bNumEP].bmSndToggle = 0; |
hudakz | 0:84353c479782 | 327 | epInfo[bNumEP].bmRcvToggle = 0; |
hudakz | 0:84353c479782 | 328 | epInfo[bNumEP].bmNakPower = USB_NAK_NOWAIT; |
hudakz | 0:84353c479782 | 329 | |
hudakz | 0:84353c479782 | 330 | // Fill in the endpoint index list |
hudakz | 0:84353c479782 | 331 | piface->epIndex[index] = bNumEP; //(pep->bEndpointAddress & 0x0F); |
hudakz | 0:84353c479782 | 332 | |
hudakz | 0:84353c479782 | 333 | if(pollInterval < pep->bInterval) // Set the polling interval as the largest polling interval obtained from endpoints |
hudakz | 0:84353c479782 | 334 | pollInterval = pep->bInterval; |
hudakz | 0:84353c479782 | 335 | |
hudakz | 0:84353c479782 | 336 | bNumEP++; |
hudakz | 0:84353c479782 | 337 | } |
hudakz | 0:84353c479782 | 338 | } |
hudakz | 0:84353c479782 | 339 | |
hudakz | 0:84353c479782 | 340 | uint8_t HIDComposite::Release() { |
hudakz | 0:84353c479782 | 341 | pUsb->GetAddressPool().FreeAddress(bAddress); |
hudakz | 0:84353c479782 | 342 | |
hudakz | 0:84353c479782 | 343 | bNumEP = 1; |
hudakz | 0:84353c479782 | 344 | bAddress = 0; |
hudakz | 0:84353c479782 | 345 | qNextPollTime = 0; |
hudakz | 0:84353c479782 | 346 | bPollEnable = false; |
hudakz | 0:84353c479782 | 347 | return 0; |
hudakz | 0:84353c479782 | 348 | } |
hudakz | 0:84353c479782 | 349 | |
hudakz | 0:84353c479782 | 350 | void HIDComposite::ZeroMemory(uint8_t len, uint8_t *buf) { |
hudakz | 0:84353c479782 | 351 | for(uint8_t i = 0; i < len; i++) |
hudakz | 0:84353c479782 | 352 | buf[i] = 0; |
hudakz | 0:84353c479782 | 353 | } |
hudakz | 0:84353c479782 | 354 | |
hudakz | 0:84353c479782 | 355 | uint8_t HIDComposite::Poll() { |
hudakz | 0:84353c479782 | 356 | uint8_t rcode = 0; |
hudakz | 0:84353c479782 | 357 | |
hudakz | 0:84353c479782 | 358 | if(!bPollEnable) |
hudakz | 0:84353c479782 | 359 | return 0; |
hudakz | 0:84353c479782 | 360 | |
hudakz | 0:84353c479782 | 361 | if((int32_t)((uint32_t)millis() - qNextPollTime) >= 0L) { |
hudakz | 0:84353c479782 | 362 | qNextPollTime = (uint32_t)millis() + pollInterval; |
hudakz | 0:84353c479782 | 363 | |
hudakz | 0:84353c479782 | 364 | uint8_t buf[constBuffLen]; |
hudakz | 0:84353c479782 | 365 | |
hudakz | 0:84353c479782 | 366 | for(uint8_t i = 0; i < bNumIface; i++) { |
hudakz | 0:84353c479782 | 367 | uint8_t index = hidInterfaces[i].epIndex[epInterruptInIndex]; |
hudakz | 0:84353c479782 | 368 | |
hudakz | 0:84353c479782 | 369 | if (index == 0) |
hudakz | 0:84353c479782 | 370 | continue; |
hudakz | 0:84353c479782 | 371 | |
hudakz | 0:84353c479782 | 372 | uint16_t read = (uint16_t)epInfo[index].maxPktSize; |
hudakz | 0:84353c479782 | 373 | |
hudakz | 0:84353c479782 | 374 | ZeroMemory(constBuffLen, buf); |
hudakz | 0:84353c479782 | 375 | |
hudakz | 0:84353c479782 | 376 | uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[index].epAddr, &read, buf); |
hudakz | 0:84353c479782 | 377 | |
hudakz | 0:84353c479782 | 378 | if(rcode) { |
hudakz | 0:84353c479782 | 379 | if(rcode != hrNAK) |
hudakz | 0:84353c479782 | 380 | USBTRACE3("(hidcomposite.h) Poll:", rcode, 0x81); |
hudakz | 0:84353c479782 | 381 | continue; |
hudakz | 0:84353c479782 | 382 | } |
hudakz | 0:84353c479782 | 383 | |
hudakz | 0:84353c479782 | 384 | if(read == 0) |
hudakz | 0:84353c479782 | 385 | continue; |
hudakz | 0:84353c479782 | 386 | |
hudakz | 0:84353c479782 | 387 | if(read > constBuffLen) |
hudakz | 0:84353c479782 | 388 | read = constBuffLen; |
hudakz | 0:84353c479782 | 389 | |
hudakz | 0:84353c479782 | 390 | #if 0 |
hudakz | 0:84353c479782 | 391 | Notify(PSTR("\r\nBuf: "), 0x80); |
hudakz | 0:84353c479782 | 392 | |
hudakz | 0:84353c479782 | 393 | for(uint8_t i = 0; i < read; i++) { |
hudakz | 0:84353c479782 | 394 | D_PrintHex<uint8_t > (buf[i], 0x80); |
hudakz | 0:84353c479782 | 395 | Notify(PSTR(" "), 0x80); |
hudakz | 0:84353c479782 | 396 | } |
hudakz | 0:84353c479782 | 397 | |
hudakz | 0:84353c479782 | 398 | Notify(PSTR("\r\n"), 0x80); |
hudakz | 0:84353c479782 | 399 | #endif |
hudakz | 0:84353c479782 | 400 | ParseHIDData(this, epInfo[index].epAddr, bHasReportId, (uint8_t)read, buf); |
hudakz | 0:84353c479782 | 401 | |
hudakz | 0:84353c479782 | 402 | HIDReportParser *prs = GetReportParser(((bHasReportId) ? *buf : 0)); |
hudakz | 0:84353c479782 | 403 | |
hudakz | 0:84353c479782 | 404 | if(prs) |
hudakz | 0:84353c479782 | 405 | prs->Parse(this, bHasReportId, (uint8_t)read, buf); |
hudakz | 0:84353c479782 | 406 | } |
hudakz | 0:84353c479782 | 407 | |
hudakz | 0:84353c479782 | 408 | } |
hudakz | 0:84353c479782 | 409 | return rcode; |
hudakz | 0:84353c479782 | 410 | } |
hudakz | 0:84353c479782 | 411 | |
hudakz | 0:84353c479782 | 412 | // Send a report to interrupt out endpoint. This is NOT SetReport() request! |
hudakz | 0:84353c479782 | 413 | uint8_t HIDComposite::SndRpt(uint16_t nbytes, uint8_t *dataptr) { |
hudakz | 0:84353c479782 | 414 | return pUsb->outTransfer(bAddress, epInfo[epInterruptOutIndex].epAddr, nbytes, dataptr); |
hudakz | 0:84353c479782 | 415 | } |