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