USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
address.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 #if !defined(__ADDRESS_H__)
18 #define __ADDRESS_H__
19 
20 #include <inttypes.h>
21 #include <stddef.h>
22 #include "max3421e.h"
23 
24 
25 /* NAK powers. To save space in endpoint data structure, amount of retries before giving up and returning 0x4 is stored in */
26 /* bmNakPower as a power of 2. The actual nak_limit is then calculated as nak_limit = ( 2^bmNakPower - 1) */
27 #define USB_NAK_MAX_POWER 15 //NAK binary order maximum value
28 #define USB_NAK_DEFAULT 14 //default 32K-1 NAKs before giving up
29 #define USB_NAK_NOWAIT 1 //Single NAK stops transfer
30 #define USB_NAK_NONAK 0 //Do not count NAKs, stop retrying after USB Timeout
31 
32 struct EpInfo {
33  uint8_t epAddr; // Endpoint address
34  uint8_t maxPktSize; // Maximum packet size
35 
36  union {
37  uint8_t epAttribs;
38 
39  struct {
40  uint8_t bmSndToggle : 1; // Send toggle, when zero bmSNDTOG0, bmSNDTOG1 otherwise
41  uint8_t bmRcvToggle : 1; // Send toggle, when zero bmRCVTOG0, bmRCVTOG1 otherwise
42  uint8_t bmNakPower : 6; // Binary order for NAK_LIMIT value
43  } __attribute__((packed));
44  };
45 } __attribute__((packed));
46 
47 // 7 6 5 4 3 2 1 0
48 // ---------------------------------
49 // | | H | P | P | P | A | A | A |
50 // ---------------------------------
51 //
52 // H - if 1 the address is a hub address
53 // P - parent hub address
54 // A - device address / port number in case of hub
55 //
56 
58 
59  union {
60 
61  struct {
62  uint8_t bmAddress : 3; // device address/port number
63  uint8_t bmParent : 3; // parent hub address
64  uint8_t bmHub : 1; // hub flag
65  uint8_t bmReserved : 1; // reserved, must be zerro
66  } __attribute__((packed));
67  uint8_t devAddress;
68  };
69 } __attribute__((packed));
70 
71 #define bmUSB_DEV_ADDR_ADDRESS 0x07
72 #define bmUSB_DEV_ADDR_PARENT 0x38
73 #define bmUSB_DEV_ADDR_HUB 0x40
74 
75 struct UsbDevice {
76  EpInfo *epinfo; // endpoint info pointer
77  uint8_t address; // address
78  uint8_t epcount; // number of endpoints
79  bool lowspeed; // indicates if a device is the low speed one
80  // uint8_t devclass; // device class
81 } __attribute__((packed));
82 
83 class AddressPool {
84 public:
85  virtual UsbDevice* GetUsbDevicePtr(uint8_t addr) = 0;
86  virtual uint8_t AllocAddress(uint8_t parent, bool is_hub = false, uint8_t port = 0) = 0;
87  virtual void FreeAddress(uint8_t addr) = 0;
88 };
89 
90 typedef void (*UsbDeviceHandleFunc)(UsbDevice *pdev);
91 
92 #define ADDR_ERROR_INVALID_INDEX 0xFF
93 #define ADDR_ERROR_INVALID_ADDRESS 0xFF
94 
95 template <const uint8_t MAX_DEVICES_ALLOWED>
96 class AddressPoolImpl : public AddressPool {
97  EpInfo dev0ep; //Endpoint data structure used during enumeration for uninitialized device
98 
99  uint8_t hubCounter; // hub counter is kept
100  // in order to avoid hub address duplication
101 
102  UsbDevice thePool[MAX_DEVICES_ALLOWED];
103 
104  // Initializes address pool entry
105 
106  void InitEntry(uint8_t index) {
107  thePool[index].address = 0;
108  thePool[index].epcount = 1;
109  thePool[index].lowspeed = 0;
110  thePool[index].epinfo = &dev0ep;
111  };
112  // Returns thePool index for a given address
113 
114  uint8_t FindAddressIndex(uint8_t address = 0) {
115  for(uint8_t i = 1; i < MAX_DEVICES_ALLOWED; i++) {
116  if(thePool[i].address == address)
117  return i;
118  }
119  return 0;
120  };
121  // Returns thePool child index for a given parent
122 
123  uint8_t FindChildIndex(UsbDeviceAddress addr, uint8_t start = 1) {
124  for(uint8_t i = (start < 1 || start >= MAX_DEVICES_ALLOWED) ? 1 : start; i < MAX_DEVICES_ALLOWED; i++) {
125  if(((UsbDeviceAddress*) & thePool[i].address)->bmParent == addr.bmAddress)
126  return i;
127  }
128  return 0;
129  };
130  // Frees address entry specified by index parameter
131 
132  void FreeAddressByIndex(uint8_t index) {
133  // Zerro field is reserved and should not be affected
134  if(index == 0)
135  return;
136 
137  // If a hub was switched off all port addresses should be freed
138  if(((UsbDeviceAddress*) & thePool[index].address)->bmHub == 1) {
139  for(uint8_t i = 1; (i = FindChildIndex(*((UsbDeviceAddress*) & thePool[index].address), i));)
140  FreeAddressByIndex(i);
141 
142  // If the hub had the last allocated address, hubCounter should be decremented
143  if(hubCounter == ((UsbDeviceAddress*) & thePool[index].address)->bmAddress)
144  hubCounter--;
145  }
146  InitEntry(index);
147  }
148  // Initializes the whole address pool at once
149 
150  void InitAllAddresses() {
151  for(uint8_t i = 1; i < MAX_DEVICES_ALLOWED; i++)
152  InitEntry(i);
153 
154  hubCounter = 0;
155  };
156 
157 public:
158 
159  AddressPoolImpl() : hubCounter(0) {
160  // Zero address is reserved
161  InitEntry(0);
162 
163  thePool[0].address = 0;
164  thePool[0].epinfo = &dev0ep;
165  dev0ep.epAddr = 0;
166  dev0ep.maxPktSize = 8;
167  dev0ep.epAttribs = 0; //set DATA0/1 toggles to 0
168  dev0ep.bmNakPower = USB_NAK_MAX_POWER;
169 
170  InitAllAddresses();
171  };
172  // Returns a pointer to a specified address entry
173 
174  virtual UsbDevice* GetUsbDevicePtr(uint8_t addr) {
175  if(!addr)
176  return thePool;
177 
178  uint8_t index = FindAddressIndex(addr);
179 
180  return(!index) ? NULL : thePool + index;
181  };
182 
183  // Performs an operation specified by pfunc for each addressed device
184 
186  if(!pfunc)
187  return;
188 
189  for(uint8_t i = 1; i < MAX_DEVICES_ALLOWED; i++)
190  if(thePool[i].address)
191  pfunc(thePool + i);
192  };
193  // Allocates new address
194 
195  virtual uint8_t AllocAddress(uint8_t parent, bool is_hub = false, uint8_t port = 0) {
196  /* if (parent != 0 && port == 0)
197  Serial.println("PRT:0"); */
198 
199  if(parent > 127 || port > 7)
200  return 0;
201 
202  if(is_hub && hubCounter == 7)
203  return 0;
204 
205  // finds first empty address entry starting from one
206  uint8_t index = FindAddressIndex(0);
207 
208  if(!index) // if empty entry is not found
209  return 0;
210 
211  if(parent == 0) {
212  if(is_hub) {
213  thePool[index].address = 0x41;
214  hubCounter++;
215  } else
216  thePool[index].address = 1;
217 
218  return thePool[index].address;
219  }
220 
221  UsbDeviceAddress addr;
222 
223  addr.bmParent = ((UsbDeviceAddress*) & parent)->bmAddress;
224 
225  if(is_hub) {
226  addr.bmHub = 1;
227  addr.bmAddress = ++hubCounter;
228  } else {
229  addr.bmHub = 0;
230  addr.bmAddress = port;
231  }
232  thePool[index].address = *((uint8_t*) & addr);
233  /*
234  Serial.print("Addr:");
235  Serial.print(addr.bmHub, HEX);
236  Serial.print(".");
237  Serial.print(addr.bmParent, HEX);
238  Serial.print(".");
239  Serial.println(addr.bmAddress, HEX);
240  */
241  return thePool[index].address;
242  };
243  // Empties pool entry
244 
245  virtual void FreeAddress(uint8_t addr) {
246  // if the root hub is disconnected all the addresses should be initialized
247  if(addr == 0x41) {
248  InitAllAddresses();
249  return;
250  }
251  uint8_t index = FindAddressIndex(addr);
252  FreeAddressByIndex(index);
253  };
254  // Returns number of hubs attached
255  // It can be rather helpfull to find out if there are hubs attached than getting the exact number of hubs.
256  //uint8_t GetNumHubs()
257  //{
258  // return hubCounter;
259  //};
260  //uint8_t GetNumDevices()
261  //{
262  // uint8_t counter = 0;
263 
264  // for (uint8_t i=1; i<MAX_DEVICES_ALLOWED; i++)
265  // if (thePool[i].address != 0);
266  // counter ++;
267 
268  // return counter;
269  //};
270 };
271 
272 #endif // __ADDRESS_H__