USB_Host_Shield_2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
cdcftdi.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(__CDCFTDI_H__)
18 #define __CDCFTDI_H__
19 
20 #include <inttypes.h>
21 #include <avr/pgmspace.h>
22 #include "avrpins.h"
23 #include "max3421e.h"
24 #include "usbhost.h"
25 #include "usb_ch9.h"
26 #include "Usb.h"
27 
28 #if defined(ARDUINO) && ARDUINO >=100
29 #include "Arduino.h"
30 #else
31 #include <WProgram.h>
32 #endif
33 
34 #include "printhex.h"
35 #include "hexdump.h"
36 #include "message.h"
37 
38 #include "confdescparser.h"
39 
40 #define bmREQ_FTDI_OUT 0x40
41 #define bmREQ_FTDI_IN 0xc0
42 
43 //#define bmREQ_FTDI_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
44 //#define bmREQ_FTDI_IN USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
45 
46 #define FTDI_VID 0x0403 // FTDI VID
47 #define FTDI_PID 0x6001 // FTDI PID
48 
49 #define FT232AM 0x0200
50 #define FT232BM 0x0400
51 #define FT2232 0x0500
52 #define FT232R 0x0600
53 
54 // Commands
55 #define FTDI_SIO_RESET 0 /* Reset the port */
56 #define FTDI_SIO_MODEM_CTRL 1 /* Set the modem control register */
57 #define FTDI_SIO_SET_FLOW_CTRL 2 /* Set flow control register */
58 #define FTDI_SIO_SET_BAUD_RATE 3 /* Set baud rate */
59 #define FTDI_SIO_SET_DATA 4 /* Set the data characteristics of the port */
60 #define FTDI_SIO_GET_MODEM_STATUS 5 /* Retrieve current value of modem status register */
61 #define FTDI_SIO_SET_EVENT_CHAR 6 /* Set the event character */
62 #define FTDI_SIO_SET_ERROR_CHAR 7 /* Set the error character */
63 
64 #define FTDI_SIO_RESET_SIO 0
65 #define FTDI_SIO_RESET_PURGE_RX 1
66 #define FTDI_SIO_RESET_PURGE_TX 2
67 
68 #define FTDI_SIO_SET_DATA_PARITY_NONE (0x0 << 8 )
69 #define FTDI_SIO_SET_DATA_PARITY_ODD (0x1 << 8 )
70 #define FTDI_SIO_SET_DATA_PARITY_EVEN (0x2 << 8 )
71 #define FTDI_SIO_SET_DATA_PARITY_MARK (0x3 << 8 )
72 #define FTDI_SIO_SET_DATA_PARITY_SPACE (0x4 << 8 )
73 #define FTDI_SIO_SET_DATA_STOP_BITS_1 (0x0 << 11)
74 #define FTDI_SIO_SET_DATA_STOP_BITS_15 (0x1 << 11)
75 #define FTDI_SIO_SET_DATA_STOP_BITS_2 (0x2 << 11)
76 #define FTDI_SIO_SET_BREAK (0x1 << 14)
77 
78 #define FTDI_SIO_SET_DTR_MASK 0x1
79 #define FTDI_SIO_SET_DTR_HIGH ( 1 | ( FTDI_SIO_SET_DTR_MASK << 8))
80 #define FTDI_SIO_SET_DTR_LOW ( 0 | ( FTDI_SIO_SET_DTR_MASK << 8))
81 #define FTDI_SIO_SET_RTS_MASK 0x2
82 #define FTDI_SIO_SET_RTS_HIGH ( 2 | ( FTDI_SIO_SET_RTS_MASK << 8 ))
83 #define FTDI_SIO_SET_RTS_LOW ( 0 | ( FTDI_SIO_SET_RTS_MASK << 8 ))
84 
85 #define FTDI_SIO_DISABLE_FLOW_CTRL 0x0
86 #define FTDI_SIO_RTS_CTS_HS (0x1 << 8)
87 #define FTDI_SIO_DTR_DSR_HS (0x2 << 8)
88 #define FTDI_SIO_XON_XOFF_HS (0x4 << 8)
89 
90 #define FTDI_SIO_CTS_MASK 0x10
91 #define FTDI_SIO_DSR_MASK 0x20
92 #define FTDI_SIO_RI_MASK 0x40
93 #define FTDI_SIO_RLSD_MASK 0x80
94 
95 class FTDI;
96 
98 {
99 public:
100  virtual uint8_t OnInit(FTDI *pftdi) = 0;
101 };
102 
103 
104 // Only single port chips are currently supported by the library,
105 // so only three endpoints are allocated.
106 #define FTDI_MAX_ENDPOINTS 3
107 
108 class FTDI : public USBDeviceConfig, public UsbConfigXtracter
109 {
110  static const uint8_t epDataInIndex; // DataIn endpoint index
111  static const uint8_t epDataOutIndex; // DataOUT endpoint index
112  static const uint8_t epInterruptInIndex; // InterruptIN endpoint index
113 
114  FTDIAsyncOper *pAsync;
115  USB *pUsb;
116  uint8_t bAddress;
117  uint8_t bConfNum; // configuration number
118  uint8_t bNumIface; // number of interfaces in the configuration
119  uint8_t bNumEP; // total number of EP in the configuration
120  uint32_t qNextPollTime; // next poll time
121  bool bPollEnable; // poll enable flag
122  uint16_t wFTDIType; // Type of FTDI chip
123 
124  EpInfo epInfo[FTDI_MAX_ENDPOINTS];
125 
126  void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr);
127 
128 public:
129  FTDI(USB *pusb, FTDIAsyncOper *pasync);
130 
131  uint8_t SetBaudRate(uint32_t baud);
132  uint8_t SetModemControl(uint16_t control);
133  uint8_t SetFlowControl(uint8_t protocol, uint8_t xon = 0x11, uint8_t xoff = 0x13);
134  uint8_t SetData(uint16_t databm);
135 
136  // Methods for recieving and sending data
137  uint8_t RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr);
138  uint8_t SndData(uint16_t nbytes, uint8_t *dataptr);
139 
140  // USBDeviceConfig implementation
141  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
142  virtual uint8_t Release();
143  virtual uint8_t Poll();
144  virtual uint8_t GetAddress() { return bAddress; };
145 
146  // UsbConfigXtracter implementation
147  virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
148 };
149 
150 #endif // __CDCFTDI_H__