Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cdcftdi.h Source File

cdcftdi.h

00001 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
00002 
00003 This software may be distributed and modified under the terms of the GNU
00004 General Public License version 2 (GPL2) as published by the Free Software
00005 Foundation and appearing in the file GPL2.TXT included in the packaging of
00006 this file. Please note that GPL2 Section 2[b] requires that all works based
00007 on this software must also be made publicly available under the terms of
00008 the GPL2 ("Copyleft").
00009 
00010 Contact information
00011 -------------------
00012 
00013 Circuits At Home, LTD
00014 Web      :  http://www.circuitsathome.com
00015 e-mail   :  support@circuitsathome.com
00016  */
00017 #if !defined(__CDCFTDI_H__)
00018 #define __CDCFTDI_H__
00019 
00020 #include "Usb.h"
00021 
00022 #define bmREQ_FTDI_OUT  0x40
00023 #define bmREQ_FTDI_IN   0xc0
00024 
00025 //#define bmREQ_FTDI_OUT                USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
00026 //#define bmREQ_FTDI_IN         USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
00027 
00028 #define FTDI_VID                        0x0403  // FTDI VID
00029 #define FTDI_PID                        0x6001  // FTDI PID
00030 
00031 #define FT232AM                         0x0200
00032 #define FT232BM                         0x0400
00033 #define FT2232                          0x0500
00034 #define FT232R                          0x0600
00035 
00036 // Commands
00037 #define FTDI_SIO_RESET                  0  /* Reset the port */
00038 #define FTDI_SIO_MODEM_CTRL             1  /* Set the modem control register */
00039 #define FTDI_SIO_SET_FLOW_CTRL          2  /* Set flow control register */
00040 #define FTDI_SIO_SET_BAUD_RATE          3  /* Set baud rate */
00041 #define FTDI_SIO_SET_DATA               4  /* Set the data characteristics of the port */
00042 #define FTDI_SIO_GET_MODEM_STATUS       5  /* Retrieve current value of modem status register */
00043 #define FTDI_SIO_SET_EVENT_CHAR         6  /* Set the event character */
00044 #define FTDI_SIO_SET_ERROR_CHAR         7  /* Set the error character */
00045 #define FTDI_SIO_SET_LATENCY_TIMER      9  /* Set the latency timer */
00046 #define FTDI_SIO_GET_LATENCY_TIMER      10 /* Get the latency timer */
00047 
00048 #define FTDI_SIO_RESET_SIO              0
00049 #define FTDI_SIO_RESET_PURGE_RX         1
00050 #define FTDI_SIO_RESET_PURGE_TX         2
00051 
00052 #define FTDI_SIO_SET_DATA_PARITY_NONE   (0x0 << 8 )
00053 #define FTDI_SIO_SET_DATA_PARITY_ODD    (0x1 << 8 )
00054 #define FTDI_SIO_SET_DATA_PARITY_EVEN   (0x2 << 8 )
00055 #define FTDI_SIO_SET_DATA_PARITY_MARK   (0x3 << 8 )
00056 #define FTDI_SIO_SET_DATA_PARITY_SPACE  (0x4 << 8 )
00057 #define FTDI_SIO_SET_DATA_STOP_BITS_1   (0x0 << 11)
00058 #define FTDI_SIO_SET_DATA_STOP_BITS_15  (0x1 << 11)
00059 #define FTDI_SIO_SET_DATA_STOP_BITS_2   (0x2 << 11)
00060 #define FTDI_SIO_SET_BREAK              (0x1 << 14)
00061 
00062 #define FTDI_SIO_SET_DTR_MASK           0x1
00063 #define FTDI_SIO_SET_DTR_HIGH           ( 1 | ( FTDI_SIO_SET_DTR_MASK  << 8))
00064 #define FTDI_SIO_SET_DTR_LOW            ( 0 | ( FTDI_SIO_SET_DTR_MASK  << 8))
00065 #define FTDI_SIO_SET_RTS_MASK           0x2
00066 #define FTDI_SIO_SET_RTS_HIGH           ( 2 | ( FTDI_SIO_SET_RTS_MASK << 8 ))
00067 #define FTDI_SIO_SET_RTS_LOW            ( 0 | ( FTDI_SIO_SET_RTS_MASK << 8 ))
00068 
00069   #define FTDI_SIO_DISABLE_FLOW_CTRL      0x0
00070 #define FTDI_SIO_RTS_CTS_HS             (0x1 << 8)
00071 #define FTDI_SIO_DTR_DSR_HS             (0x2 << 8)
00072 #define FTDI_SIO_XON_XOFF_HS            (0x4 << 8)
00073 
00074 #define FTDI_SIO_CTS_MASK               0x10
00075 #define FTDI_SIO_DSR_MASK               0x20
00076 #define FTDI_SIO_RI_MASK                0x40
00077 #define FTDI_SIO_RLSD_MASK              0x80
00078 
00079 class FTDI;
00080 
00081 class FTDIAsyncOper {
00082 public:
00083 
00084         virtual uint8_t OnInit(FTDI *pftdi __attribute__((unused))) {
00085                 return 0;
00086         };
00087 
00088         virtual uint8_t OnRelease(FTDI *pftdi __attribute__((unused))) {
00089                 return 0;
00090         };
00091 };
00092 
00093 
00094 // Only single port chips are currently supported by the library,
00095 //              so only three endpoints are allocated.
00096 #define FTDI_MAX_ENDPOINTS              3
00097 
00098 class FTDI : public USBDeviceConfig, public UsbConfigXtracter {
00099         static const uint8_t epDataInIndex; // DataIn endpoint index
00100         static const uint8_t epDataOutIndex; // DataOUT endpoint index
00101         static const uint8_t epInterruptInIndex; // InterruptIN  endpoint index
00102 
00103         FTDIAsyncOper *pAsync;
00104         Usb *pUsb;
00105         uint8_t bAddress;
00106         uint8_t bConfNum; // configuration number
00107         uint8_t bNumIface; // number of interfaces in the configuration
00108         uint8_t bNumEP; // total number of EP in the configuration
00109         uint32_t qNextPollTime; // next poll time
00110         volatile bool bPollEnable; // poll enable flag
00111         volatile bool ready; //device ready indicator
00112         uint16_t wFTDIType; // Type of FTDI chip
00113         uint16_t wIdProduct; // expected PID
00114 
00115         EpInfo epInfo[FTDI_MAX_ENDPOINTS];
00116 
00117         void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr);
00118 
00119 public:
00120         FTDI(Usb *pusb, FTDIAsyncOper *pasync, uint16_t idProduct = FTDI_PID);
00121 
00122         uint8_t SetBaudRate(uint32_t baud);
00123         uint8_t SetModemControl(uint16_t control);
00124         uint8_t SetFlowControl(uint8_t protocol, uint8_t xon = 0x11, uint8_t xoff = 0x13);
00125         uint8_t SetData(uint16_t databm);
00126         uint8_t SetLatency(uint8_t l);
00127         uint8_t GetLatency(uint8_t *l);
00128 
00129         // Methods for receiving and sending data
00130         uint8_t RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr);
00131         uint8_t SndData(uint16_t nbytes, uint8_t *dataptr);
00132 
00133         // USBDeviceConfig implementation
00134         uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
00135         uint8_t Release();
00136         uint8_t Poll();
00137 
00138         virtual uint8_t GetAddress() {
00139                 return bAddress;
00140         };
00141 
00142         // UsbConfigXtracter implementation
00143         void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
00144 
00145         virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
00146                 return (vid == FTDI_VID && pid == FTDI_PID);
00147         }
00148         virtual bool isReady() {
00149                 return ready;
00150         };
00151 
00152 };
00153 
00154 #endif // __CDCFTDI_H__