USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
adk.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2 
3 This software may be distributed and modified under the terms of the GNU
4 General Public License version 2 (GPL2) as published by the Free Software
5 Foundation and appearing in the file GPL2.TXT included in the packaging of
6 this file. Please note that GPL2 Section 2[b] requires that all works based
7 on this software must also be made publicly available under the terms of
8 the GPL2 ("Copyleft").
9 
10 Contact information
11 -------------------
12 
13 Circuits At Home, LTD
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
16 */
17 
18 /* Google ADK interface support header */
19 
20 #if !defined(_ADK_H_)
21 #define _ADK_H_
22 
23 #include <inttypes.h>
24 #include <avr/pgmspace.h>
25 #include "avrpins.h"
26 #include "max3421e.h"
27 #include "usbhost.h"
28 #include "usb_ch9.h"
29 #include "Usb.h"
30 
31 #if defined(ARDUINO) && ARDUINO >=100
32 #include "Arduino.h"
33 #else
34 #include <WProgram.h>
35 #endif
36 
37 #include "printhex.h"
38 #include "hexdump.h"
39 #include "message.h"
40 
41 #include "confdescparser.h"
42 
43 #define ADK_VID 0x18D1
44 #define ADK_PID 0x2D00
45 #define ADB_PID 0x2D01
46 
47 #define XOOM //enables repeating getProto() and getConf() attempts
48  //necessary for slow devices such as Motorola XOOM
49  //defined by default, can be commented out to save memory
50 
51 /* requests */
52 
53 #define ADK_GETPROTO 51 //check USB accessory protocol version
54 #define ADK_SENDSTR 52 //send identifying string
55 #define ADK_ACCSTART 53 //start device in accessory mode
56 
57 #define bmREQ_ADK_GET USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_VENDOR|USB_SETUP_RECIPIENT_DEVICE
58 #define bmREQ_ADK_SEND USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_VENDOR|USB_SETUP_RECIPIENT_DEVICE
59 
60 #define ACCESSORY_STRING_MANUFACTURER 0
61 #define ACCESSORY_STRING_MODEL 1
62 #define ACCESSORY_STRING_DESCRIPTION 2
63 #define ACCESSORY_STRING_VERSION 3
64 #define ACCESSORY_STRING_URI 4
65 #define ACCESSORY_STRING_SERIAL 5
66 
67 #define ADK_MAX_ENDPOINTS 3 //endpoint 0, bulk_IN, bulk_OUT
68 
69 class ADK;
70 
71 class ADK : public USBDeviceConfig, public UsbConfigXtracter
72 {
73 private:
74  /* ID strings */
75  const char* manufacturer;
76  const char* model;
77  const char* description;
78  const char* version;
79  const char* uri;
80  const char* serial;
81 
82  /* ADK proprietary requests */
83  uint8_t getProto( uint8_t* adkproto );
84  uint8_t sendStr( uint8_t index, const char* str );
85  uint8_t switchAcc( void );
86 
87 protected:
88  static const uint8_t epDataInIndex; // DataIn endpoint index
89  static const uint8_t epDataOutIndex; // DataOUT endpoint index
90 
91  /* mandatory members */
93  uint8_t bAddress;
94  uint8_t bConfNum; // configuration number
95 
96  uint8_t bNumEP; // total number of EP in the configuration
97  bool ready;
98 
99  /* Endpoint data structure */
101 
103 
104 public:
105  ADK(USB *pUsb, const char* manufacturer,
106  const char* model,
107  const char* description,
108  const char* version,
109  const char* uri,
110  const char* serial);
111 
112  // Methods for receiving and sending data
113  uint8_t RcvData(uint16_t *nbytesptr, uint8_t *dataptr);
114  uint8_t SndData(uint16_t nbytes, uint8_t *dataptr);
115 
116 
117  // USBDeviceConfig implementation
118  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
119  virtual uint8_t Release();
120  virtual uint8_t Poll(){}; //not implemented
121  virtual uint8_t GetAddress() { return bAddress; };
122  virtual bool isReady() { return ready; };
123 
124  //UsbConfigXtracter implementation
125  virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
126 }; //class ADK : public USBDeviceConfig ...
127 
128 /* get ADK protocol version */
129 /* returns 2 bytes in *adkproto */
130 inline uint8_t ADK::getProto( uint8_t* adkproto )
131 {
132  return( pUsb->ctrlReq(bAddress, 0, bmREQ_ADK_GET, ADK_GETPROTO, 0, 0, 0, 2, 2, adkproto, NULL ));
133 }
134 /* send ADK string */
135 inline uint8_t ADK::sendStr( uint8_t index, const char* str )
136 {
137  return( pUsb->ctrlReq(bAddress, 0, bmREQ_ADK_SEND, ADK_SENDSTR, 0, 0, index, strlen(str) + 1, strlen(str) + 1, (uint8_t*)str, NULL));
138 }
139 /* switch to accessory mode */
140 inline uint8_t ADK::switchAcc( void )
141 {
142  return( pUsb->ctrlReq(bAddress, 0, bmREQ_ADK_SEND, ADK_ACCSTART, 0, 0, 0, 0, 0, NULL, NULL));
143 }
144 
145 #endif // _ADK_H_