MAX3421E-based USB Host Shield Library
Dependents: UsbHostMAX3421E_Hello
adk.h@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 support header */ |
hudakz | 0:84353c479782 | 19 | |
hudakz | 0:84353c479782 | 20 | #if !defined(_ADK_H_) |
hudakz | 0:84353c479782 | 21 | #define _ADK_H_ |
hudakz | 0:84353c479782 | 22 | |
hudakz | 0:84353c479782 | 23 | #include "Usb.h" |
hudakz | 0:84353c479782 | 24 | |
hudakz | 0:84353c479782 | 25 | #define ADK_VID 0x18D1 |
hudakz | 0:84353c479782 | 26 | #define ADK_PID 0x2D00 |
hudakz | 0:84353c479782 | 27 | #define ADB_PID 0x2D01 |
hudakz | 0:84353c479782 | 28 | |
hudakz | 0:84353c479782 | 29 | #define XOOM //enables repeating getProto() and getConf() attempts |
hudakz | 0:84353c479782 | 30 | //necessary for slow devices such as Motorola XOOM |
hudakz | 0:84353c479782 | 31 | //defined by default, can be commented out to save memory |
hudakz | 0:84353c479782 | 32 | |
hudakz | 0:84353c479782 | 33 | /* requests */ |
hudakz | 0:84353c479782 | 34 | |
hudakz | 0:84353c479782 | 35 | #define ADK_GETPROTO 51 //check USB accessory protocol version |
hudakz | 0:84353c479782 | 36 | #define ADK_SENDSTR 52 //send identifying string |
hudakz | 0:84353c479782 | 37 | #define ADK_ACCSTART 53 //start device in accessory mode |
hudakz | 0:84353c479782 | 38 | |
hudakz | 0:84353c479782 | 39 | #define bmREQ_ADK_GET USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_VENDOR|USB_SETUP_RECIPIENT_DEVICE |
hudakz | 0:84353c479782 | 40 | #define bmREQ_ADK_SEND USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_VENDOR|USB_SETUP_RECIPIENT_DEVICE |
hudakz | 0:84353c479782 | 41 | |
hudakz | 0:84353c479782 | 42 | #define ACCESSORY_STRING_MANUFACTURER 0 |
hudakz | 0:84353c479782 | 43 | #define ACCESSORY_STRING_MODEL 1 |
hudakz | 0:84353c479782 | 44 | #define ACCESSORY_STRING_DESCRIPTION 2 |
hudakz | 0:84353c479782 | 45 | #define ACCESSORY_STRING_VERSION 3 |
hudakz | 0:84353c479782 | 46 | #define ACCESSORY_STRING_URI 4 |
hudakz | 0:84353c479782 | 47 | #define ACCESSORY_STRING_SERIAL 5 |
hudakz | 0:84353c479782 | 48 | |
hudakz | 0:84353c479782 | 49 | #define ADK_MAX_ENDPOINTS 3 //endpoint 0, bulk_IN, bulk_OUT |
hudakz | 0:84353c479782 | 50 | |
hudakz | 0:84353c479782 | 51 | class ADK; |
hudakz | 0:84353c479782 | 52 | |
hudakz | 0:84353c479782 | 53 | class ADK : public USBDeviceConfig, public UsbConfigXtracter { |
hudakz | 0:84353c479782 | 54 | private: |
hudakz | 0:84353c479782 | 55 | /* ID strings */ |
hudakz | 0:84353c479782 | 56 | const char* manufacturer; |
hudakz | 0:84353c479782 | 57 | const char* model; |
hudakz | 0:84353c479782 | 58 | const char* description; |
hudakz | 0:84353c479782 | 59 | const char* version; |
hudakz | 0:84353c479782 | 60 | const char* uri; |
hudakz | 0:84353c479782 | 61 | const char* serial; |
hudakz | 0:84353c479782 | 62 | |
hudakz | 0:84353c479782 | 63 | /* ADK proprietary requests */ |
hudakz | 0:84353c479782 | 64 | uint8_t getProto(uint8_t* adkproto); |
hudakz | 0:84353c479782 | 65 | uint8_t sendStr(uint8_t index, const char* str); |
hudakz | 0:84353c479782 | 66 | uint8_t switchAcc(void); |
hudakz | 0:84353c479782 | 67 | |
hudakz | 0:84353c479782 | 68 | protected: |
hudakz | 0:84353c479782 | 69 | static const uint8_t epDataInIndex; // DataIn endpoint index |
hudakz | 0:84353c479782 | 70 | static const uint8_t epDataOutIndex; // DataOUT endpoint index |
hudakz | 0:84353c479782 | 71 | |
hudakz | 0:84353c479782 | 72 | /* mandatory members */ |
hudakz | 0:84353c479782 | 73 | USB *pUsb; |
hudakz | 0:84353c479782 | 74 | uint8_t bAddress; |
hudakz | 0:84353c479782 | 75 | uint8_t bConfNum; // configuration number |
hudakz | 0:84353c479782 | 76 | |
hudakz | 0:84353c479782 | 77 | uint8_t bNumEP; // total number of EP in the configuration |
hudakz | 0:84353c479782 | 78 | bool ready; |
hudakz | 0:84353c479782 | 79 | |
hudakz | 0:84353c479782 | 80 | /* Endpoint data structure */ |
hudakz | 0:84353c479782 | 81 | EpInfo epInfo[ADK_MAX_ENDPOINTS]; |
hudakz | 0:84353c479782 | 82 | |
hudakz | 0:84353c479782 | 83 | void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr); |
hudakz | 0:84353c479782 | 84 | |
hudakz | 0:84353c479782 | 85 | public: |
hudakz | 0:84353c479782 | 86 | ADK(USB *pUsb, const char* manufacturer, |
hudakz | 0:84353c479782 | 87 | const char* model, |
hudakz | 0:84353c479782 | 88 | const char* description, |
hudakz | 0:84353c479782 | 89 | const char* version, |
hudakz | 0:84353c479782 | 90 | const char* uri, |
hudakz | 0:84353c479782 | 91 | const char* serial); |
hudakz | 0:84353c479782 | 92 | |
hudakz | 0:84353c479782 | 93 | // Methods for receiving and sending data |
hudakz | 0:84353c479782 | 94 | uint8_t RcvData(uint16_t *nbytesptr, uint8_t *dataptr); |
hudakz | 0:84353c479782 | 95 | uint8_t SndData(uint16_t nbytes, uint8_t *dataptr); |
hudakz | 0:84353c479782 | 96 | |
hudakz | 0:84353c479782 | 97 | |
hudakz | 0:84353c479782 | 98 | // USBDeviceConfig implementation |
hudakz | 0:84353c479782 | 99 | uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed); |
hudakz | 0:84353c479782 | 100 | uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed); |
hudakz | 0:84353c479782 | 101 | uint8_t Release(); |
hudakz | 0:84353c479782 | 102 | |
hudakz | 0:84353c479782 | 103 | virtual uint8_t Poll() { |
hudakz | 0:84353c479782 | 104 | return 0; |
hudakz | 0:84353c479782 | 105 | }; |
hudakz | 0:84353c479782 | 106 | |
hudakz | 0:84353c479782 | 107 | virtual uint8_t GetAddress() { |
hudakz | 0:84353c479782 | 108 | return bAddress; |
hudakz | 0:84353c479782 | 109 | }; |
hudakz | 0:84353c479782 | 110 | |
hudakz | 0:84353c479782 | 111 | virtual bool isReady() { |
hudakz | 0:84353c479782 | 112 | return ready; |
hudakz | 0:84353c479782 | 113 | }; |
hudakz | 0:84353c479782 | 114 | |
hudakz | 0:84353c479782 | 115 | virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) { |
hudakz | 0:84353c479782 | 116 | return (vid == ADK_VID && (pid == ADK_PID || pid == ADB_PID)); |
hudakz | 0:84353c479782 | 117 | }; |
hudakz | 0:84353c479782 | 118 | |
hudakz | 0:84353c479782 | 119 | //UsbConfigXtracter implementation |
hudakz | 0:84353c479782 | 120 | void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep); |
hudakz | 0:84353c479782 | 121 | }; //class ADK : public USBDeviceConfig ... |
hudakz | 0:84353c479782 | 122 | |
hudakz | 0:84353c479782 | 123 | /* get ADK protocol version */ |
hudakz | 0:84353c479782 | 124 | |
hudakz | 0:84353c479782 | 125 | /* returns 2 bytes in *adkproto */ |
hudakz | 0:84353c479782 | 126 | inline uint8_t ADK::getProto(uint8_t* adkproto) { |
hudakz | 0:84353c479782 | 127 | return ( pUsb->ctrlReq(bAddress, 0, bmREQ_ADK_GET, ADK_GETPROTO, 0, 0, 0, 2, 2, adkproto, NULL)); |
hudakz | 0:84353c479782 | 128 | } |
hudakz | 0:84353c479782 | 129 | |
hudakz | 0:84353c479782 | 130 | /* send ADK string */ |
hudakz | 0:84353c479782 | 131 | inline uint8_t ADK::sendStr(uint8_t index, const char* str) { |
hudakz | 0:84353c479782 | 132 | return ( pUsb->ctrlReq(bAddress, 0, bmREQ_ADK_SEND, ADK_SENDSTR, 0, 0, index, strlen(str) + 1, strlen(str) + 1, (uint8_t*)str, NULL)); |
hudakz | 0:84353c479782 | 133 | } |
hudakz | 0:84353c479782 | 134 | |
hudakz | 0:84353c479782 | 135 | /* switch to accessory mode */ |
hudakz | 0:84353c479782 | 136 | inline uint8_t ADK::switchAcc(void) { |
hudakz | 0:84353c479782 | 137 | return ( pUsb->ctrlReq(bAddress, 0, bmREQ_ADK_SEND, ADK_ACCSTART, 0, 0, 0, 0, 0, NULL, NULL)); |
hudakz | 0:84353c479782 | 138 | } |
hudakz | 0:84353c479782 | 139 | |
hudakz | 0:84353c479782 | 140 | #endif // _ADK_H_ |