USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
cdcftdi.cpp
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 #include "cdcftdi.h"
18 
19 const uint8_t FTDI::epDataInIndex = 1;
20 const uint8_t FTDI::epDataOutIndex = 2;
21 const uint8_t FTDI::epInterruptInIndex = 3;
22 
24 pAsync(pasync),
25 pUsb(p),
26 bAddress(0),
27 bNumEP(1),
28 wFTDIType(0) {
29  for (uint8_t i = 0; i < FTDI_MAX_ENDPOINTS; i++) {
30  epInfo[i].epAddr = 0;
31  epInfo[i].maxPktSize = (i) ? 0 : 8;
32  epInfo[i].epAttribs = 0;
33 
34  //if (!i)
35  epInfo[i].bmNakPower = USB_NAK_MAX_POWER;
36  }
37  if (pUsb)
38  pUsb->RegisterDeviceClass(this);
39 }
40 
41 uint8_t FTDI::Init(uint8_t parent, uint8_t port, bool lowspeed) {
42  const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR);
43 
44  uint8_t buf[constBufSize];
45  uint8_t rcode;
46  UsbDevice *p = NULL;
47  EpInfo *oldep_ptr = NULL;
48  //uint8_t len = 0;
49  //uint16_t cd_len = 0;
50 
51  uint8_t num_of_conf; // number of configurations
52  //uint8_t num_of_intf; // number of interfaces
53 
54  AddressPool &addrPool = pUsb->GetAddressPool();
55 
56  USBTRACE("FTDI Init\r\n");
57 
58  if (bAddress)
60 
61  // Get pointer to pseudo device with address 0 assigned
62  p = addrPool.GetUsbDevicePtr(0);
63 
64  if (!p)
66 
67  if (!p->epinfo) {
68  USBTRACE("epinfo\r\n");
70  }
71 
72  // Save old pointer to EP_RECORD of address 0
73  oldep_ptr = p->epinfo;
74 
75  // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
76  p->epinfo = epInfo;
77 
78  p->lowspeed = lowspeed;
79 
80  // Get device descriptor
81  rcode = pUsb->getDevDescr(0, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*)buf);
82 
83  // Restore p->epinfo
84  p->epinfo = oldep_ptr;
85 
86  if (rcode)
87  goto FailGetDevDescr;
88 
89  if (((USB_DEVICE_DESCRIPTOR*)buf)->idVendor != FTDI_VID || ((USB_DEVICE_DESCRIPTOR*)buf)->idProduct != FTDI_PID)
91 
92  // Save type of FTDI chip
93  wFTDIType = ((USB_DEVICE_DESCRIPTOR*)buf)->bcdDevice;
94 
95  // Allocate new address according to device class
96  bAddress = addrPool.AllocAddress(parent, false, port);
97 
98  if (!bAddress)
100 
101  // Extract Max Packet Size from the device descriptor
102  epInfo[0].maxPktSize = (uint8_t)((USB_DEVICE_DESCRIPTOR*)buf)->bMaxPacketSize0;
103 
104  // Assign new address to the device
105  rcode = pUsb->setAddr(0, 0, bAddress);
106 
107  if (rcode) {
108  p->lowspeed = false;
109  addrPool.FreeAddress(bAddress);
110  bAddress = 0;
111  USBTRACE2("setAddr:", rcode);
112  return rcode;
113  }
114 
115  USBTRACE2("Addr:", bAddress);
116 
117  p->lowspeed = false;
118 
119  p = addrPool.GetUsbDevicePtr(bAddress);
120 
121  if (!p)
123 
124  p->lowspeed = lowspeed;
125 
126  num_of_conf = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations;
127 
128  // Assign epInfo to epinfo pointer
129  rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
130 
131  if (rcode)
132  goto FailSetDevTblEntry;
133 
134  USBTRACE2("NC:", num_of_conf);
135 
136  for (uint8_t i = 0; i < num_of_conf; i++) {
139 
140  rcode = pUsb->getConfDescr(bAddress, 0, i, &HexDump);
141 
142  if (rcode)
143  goto FailGetConfDescr;
144 
145  rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
146 
147  if (rcode)
148  goto FailGetConfDescr;
149 
150  if (bNumEP > 1)
151  break;
152  } // for
153 
154  if (bNumEP < 2)
156 
157  USBTRACE2("NumEP:", bNumEP);
158 
159  // Assign epInfo to epinfo pointer
160  rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo);
161 
162  USBTRACE2("Conf:", bConfNum);
163 
164  // Set Configuration Value
165  rcode = pUsb->setConf(bAddress, 0, bConfNum);
166 
167  if (rcode)
168  goto FailSetConfDescr;
169 
170  rcode = pAsync->OnInit(this);
171 
172  if (rcode)
173  goto FailOnInit;
174 
175  USBTRACE("FTDI configured\r\n");
176 
177  bPollEnable = true;
178  return 0;
179 
180 FailGetDevDescr:
182  goto Fail;
183 
184 FailSetDevTblEntry:
186  goto Fail;
187 
188 FailGetConfDescr:
190  goto Fail;
191 
192 FailSetConfDescr:
194  goto Fail;
195 
196 FailOnInit:
197  USBTRACE("OnInit:");
198 
199 Fail:
200  NotifyFail(rcode);
201  Release();
202  return rcode;
203 }
204 
205 void FTDI::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *pep) {
206  ErrorMessage<uint8_t > (PSTR("Conf.Val"), conf);
207  ErrorMessage<uint8_t > (PSTR("Iface Num"), iface);
208  ErrorMessage<uint8_t > (PSTR("Alt.Set"), alt);
209 
210  bConfNum = conf;
211 
212  uint8_t index;
213 
214  if ((pep->bmAttributes & 0x03) == 3 && (pep->bEndpointAddress & 0x80) == 0x80)
215  index = epInterruptInIndex;
216  else
217  if ((pep->bmAttributes & 0x02) == 2)
218  index = ((pep->bEndpointAddress & 0x80) == 0x80) ? epDataInIndex : epDataOutIndex;
219  else
220  return;
221 
222  // Fill in the endpoint info structure
223  epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F);
224  epInfo[index].maxPktSize = (uint8_t)pep->wMaxPacketSize;
225  epInfo[index].epAttribs = 0;
226 
227  bNumEP++;
228 
229  PrintEndpointDescriptor(pep);
230 }
231 
232 uint8_t FTDI::Release() {
233  pUsb->GetAddressPool().FreeAddress(bAddress);
234 
235  bAddress = 0;
236  bNumEP = 1;
237  qNextPollTime = 0;
238  bPollEnable = false;
239  return 0;
240 }
241 
242 uint8_t FTDI::Poll() {
243  uint8_t rcode = 0;
244 
245  //if (!bPollEnable)
246  // return 0;
247 
248  //if (qNextPollTime <= millis())
249  //{
250  // Serial.println(bAddress, HEX);
251 
252  // qNextPollTime = millis() + 100;
253  //}
254  return rcode;
255 }
256 
257 uint8_t FTDI::SetBaudRate(uint32_t baud) {
258  uint16_t baud_value, baud_index = 0;
259  uint32_t divisor3;
260 
261  divisor3 = 48000000 / 2 / baud; // divisor shifted 3 bits to the left
262 
263  if (wFTDIType == FT232AM) {
264  if ((divisor3 & 0x7) == 7)
265  divisor3++; // round x.7/8 up to x+1
266 
267  baud_value = divisor3 >> 3;
268  divisor3 &= 0x7;
269 
270  if (divisor3 == 1) baud_value |= 0xc000;
271  else // 0.125
272  if (divisor3 >= 4) baud_value |= 0x4000;
273  else // 0.5
274  if (divisor3 != 0) baud_value |= 0x8000; // 0.25
275  if (baud_value == 1) baud_value = 0; /* special case for maximum baud rate */
276  } else {
277  static const unsigned char divfrac [8] = {0, 3, 2, 0, 1, 1, 2, 3};
278  static const unsigned char divindex[8] = {0, 0, 0, 1, 0, 1, 1, 1};
279 
280  baud_value = divisor3 >> 3;
281  baud_value |= divfrac [divisor3 & 0x7] << 14;
282  baud_index = divindex[divisor3 & 0x7];
283 
284  /* Deal with special cases for highest baud rates. */
285  if (baud_value == 1) baud_value = 0;
286  else // 1.0
287  if (baud_value == 0x4001) baud_value = 1; // 1.5
288  }
289  USBTRACE2("baud_value:", baud_value);
290  USBTRACE2("baud_index:", baud_index);
291  return pUsb->ctrlReq(bAddress, 0, bmREQ_FTDI_OUT, FTDI_SIO_SET_BAUD_RATE, baud_value & 0xff, baud_value >> 8, baud_index, 0, 0, NULL, NULL);
292 }
293 
294 uint8_t FTDI::SetModemControl(uint16_t signal) {
295  return pUsb->ctrlReq(bAddress, 0, bmREQ_FTDI_OUT, FTDI_SIO_MODEM_CTRL, signal & 0xff, signal >> 8, 0, 0, 0, NULL, NULL);
296 }
297 
298 uint8_t FTDI::SetFlowControl(uint8_t protocol, uint8_t xon, uint8_t xoff) {
299  return pUsb->ctrlReq(bAddress, 0, bmREQ_FTDI_OUT, FTDI_SIO_SET_FLOW_CTRL, xon, xoff, protocol << 8, 0, 0, NULL, NULL);
300 }
301 
302 uint8_t FTDI::SetData(uint16_t databm) {
303  return pUsb->ctrlReq(bAddress, 0, bmREQ_FTDI_OUT, FTDI_SIO_SET_DATA, databm & 0xff, databm >> 8, 0, 0, 0, NULL, NULL);
304 }
305 
306 uint8_t FTDI::RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr) {
307  return pUsb->inTransfer(bAddress, epInfo[epDataInIndex].epAddr, bytes_rcvd, dataptr);
308 }
309 
310 uint8_t FTDI::SndData(uint16_t nbytes, uint8_t *dataptr) {
311  return pUsb->outTransfer(bAddress, epInfo[epDataOutIndex].epAddr, nbytes, dataptr);
312 }
313 
314 void FTDI::PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr) {
315  Notify(PSTR("Endpoint descriptor:"), 0x80);
316  Notify(PSTR("\r\nLength:\t\t"), 0x80);
317  PrintHex<uint8_t > (ep_ptr->bLength, 0x80);
318  Notify(PSTR("\r\nType:\t\t"), 0x80);
319  PrintHex<uint8_t > (ep_ptr->bDescriptorType, 0x80);
320  Notify(PSTR("\r\nAddress:\t"), 0x80);
321  PrintHex<uint8_t > (ep_ptr->bEndpointAddress, 0x80);
322  Notify(PSTR("\r\nAttributes:\t"), 0x80);
323  PrintHex<uint8_t > (ep_ptr->bmAttributes, 0x80);
324  Notify(PSTR("\r\nMaxPktSize:\t"), 0x80);
325  PrintHex<uint16_t > (ep_ptr->wMaxPacketSize, 0x80);
326  Notify(PSTR("\r\nPoll Intrv:\t"), 0x80);
327  PrintHex<uint8_t > (ep_ptr->bInterval, 0x80);
328  Notify(PSTR("\r\n"), 0x80);
329 }