Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Committer:
hudakz
Date:
Sun Jul 12 20:39:26 2020 +0000
Revision:
0:84353c479782
Child:
1:2263e77400e9
MAX3421E-based USB Host Shield Library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:84353c479782 1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
hudakz 0:84353c479782 2
hudakz 0:84353c479782 3 This software may be distributed and modified under the terms of the GNU
hudakz 0:84353c479782 4 General Public License version 2 (GPL2) as published by the Free Software
hudakz 0:84353c479782 5 Foundation and appearing in the file GPL2.TXT included in the packaging of
hudakz 0:84353c479782 6 this file. Please note that GPL2 Section 2[b] requires that all works based
hudakz 0:84353c479782 7 on this software must also be made publicly available under the terms of
hudakz 0:84353c479782 8 the GPL2 ("Copyleft").
hudakz 0:84353c479782 9
hudakz 0:84353c479782 10 Contact information
hudakz 0:84353c479782 11 -------------------
hudakz 0:84353c479782 12
hudakz 0:84353c479782 13 Circuits At Home, LTD
hudakz 0:84353c479782 14 Web : http://www.circuitsathome.com
hudakz 0:84353c479782 15 e-mail : support@circuitsathome.com
hudakz 0:84353c479782 16 */
hudakz 0:84353c479782 17 #if !defined(__CDCFTDI_H__)
hudakz 0:84353c479782 18 #define __CDCFTDI_H__
hudakz 0:84353c479782 19
hudakz 0:84353c479782 20 #include "Usb.h"
hudakz 0:84353c479782 21
hudakz 0:84353c479782 22 #define bmREQ_FTDI_OUT 0x40
hudakz 0:84353c479782 23 #define bmREQ_FTDI_IN 0xc0
hudakz 0:84353c479782 24
hudakz 0:84353c479782 25 //#define bmREQ_FTDI_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
hudakz 0:84353c479782 26 //#define bmREQ_FTDI_IN USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
hudakz 0:84353c479782 27
hudakz 0:84353c479782 28 #define FTDI_VID 0x0403 // FTDI VID
hudakz 0:84353c479782 29 #define FTDI_PID 0x6001 // FTDI PID
hudakz 0:84353c479782 30
hudakz 0:84353c479782 31 #define FT232AM 0x0200
hudakz 0:84353c479782 32 #define FT232BM 0x0400
hudakz 0:84353c479782 33 #define FT2232 0x0500
hudakz 0:84353c479782 34 #define FT232R 0x0600
hudakz 0:84353c479782 35
hudakz 0:84353c479782 36 // Commands
hudakz 0:84353c479782 37 #define FTDI_SIO_RESET 0 /* Reset the port */
hudakz 0:84353c479782 38 #define FTDI_SIO_MODEM_CTRL 1 /* Set the modem control register */
hudakz 0:84353c479782 39 #define FTDI_SIO_SET_FLOW_CTRL 2 /* Set flow control register */
hudakz 0:84353c479782 40 #define FTDI_SIO_SET_BAUD_RATE 3 /* Set baud rate */
hudakz 0:84353c479782 41 #define FTDI_SIO_SET_DATA 4 /* Set the data characteristics of the port */
hudakz 0:84353c479782 42 #define FTDI_SIO_GET_MODEM_STATUS 5 /* Retrieve current value of modem status register */
hudakz 0:84353c479782 43 #define FTDI_SIO_SET_EVENT_CHAR 6 /* Set the event character */
hudakz 0:84353c479782 44 #define FTDI_SIO_SET_ERROR_CHAR 7 /* Set the error character */
hudakz 0:84353c479782 45 #define FTDI_SIO_SET_LATENCY_TIMER 9 /* Set the latency timer */
hudakz 0:84353c479782 46 #define FTDI_SIO_GET_LATENCY_TIMER 10 /* Get the latency timer */
hudakz 0:84353c479782 47
hudakz 0:84353c479782 48 #define FTDI_SIO_RESET_SIO 0
hudakz 0:84353c479782 49 #define FTDI_SIO_RESET_PURGE_RX 1
hudakz 0:84353c479782 50 #define FTDI_SIO_RESET_PURGE_TX 2
hudakz 0:84353c479782 51
hudakz 0:84353c479782 52 #define FTDI_SIO_SET_DATA_PARITY_NONE (0x0 << 8 )
hudakz 0:84353c479782 53 #define FTDI_SIO_SET_DATA_PARITY_ODD (0x1 << 8 )
hudakz 0:84353c479782 54 #define FTDI_SIO_SET_DATA_PARITY_EVEN (0x2 << 8 )
hudakz 0:84353c479782 55 #define FTDI_SIO_SET_DATA_PARITY_MARK (0x3 << 8 )
hudakz 0:84353c479782 56 #define FTDI_SIO_SET_DATA_PARITY_SPACE (0x4 << 8 )
hudakz 0:84353c479782 57 #define FTDI_SIO_SET_DATA_STOP_BITS_1 (0x0 << 11)
hudakz 0:84353c479782 58 #define FTDI_SIO_SET_DATA_STOP_BITS_15 (0x1 << 11)
hudakz 0:84353c479782 59 #define FTDI_SIO_SET_DATA_STOP_BITS_2 (0x2 << 11)
hudakz 0:84353c479782 60 #define FTDI_SIO_SET_BREAK (0x1 << 14)
hudakz 0:84353c479782 61
hudakz 0:84353c479782 62 #define FTDI_SIO_SET_DTR_MASK 0x1
hudakz 0:84353c479782 63 #define FTDI_SIO_SET_DTR_HIGH ( 1 | ( FTDI_SIO_SET_DTR_MASK << 8))
hudakz 0:84353c479782 64 #define FTDI_SIO_SET_DTR_LOW ( 0 | ( FTDI_SIO_SET_DTR_MASK << 8))
hudakz 0:84353c479782 65 #define FTDI_SIO_SET_RTS_MASK 0x2
hudakz 0:84353c479782 66 #define FTDI_SIO_SET_RTS_HIGH ( 2 | ( FTDI_SIO_SET_RTS_MASK << 8 ))
hudakz 0:84353c479782 67 #define FTDI_SIO_SET_RTS_LOW ( 0 | ( FTDI_SIO_SET_RTS_MASK << 8 ))
hudakz 0:84353c479782 68
hudakz 0:84353c479782 69 #define FTDI_SIO_DISABLE_FLOW_CTRL 0x0
hudakz 0:84353c479782 70 #define FTDI_SIO_RTS_CTS_HS (0x1 << 8)
hudakz 0:84353c479782 71 #define FTDI_SIO_DTR_DSR_HS (0x2 << 8)
hudakz 0:84353c479782 72 #define FTDI_SIO_XON_XOFF_HS (0x4 << 8)
hudakz 0:84353c479782 73
hudakz 0:84353c479782 74 #define FTDI_SIO_CTS_MASK 0x10
hudakz 0:84353c479782 75 #define FTDI_SIO_DSR_MASK 0x20
hudakz 0:84353c479782 76 #define FTDI_SIO_RI_MASK 0x40
hudakz 0:84353c479782 77 #define FTDI_SIO_RLSD_MASK 0x80
hudakz 0:84353c479782 78
hudakz 0:84353c479782 79 class FTDI;
hudakz 0:84353c479782 80
hudakz 0:84353c479782 81 class FTDIAsyncOper {
hudakz 0:84353c479782 82 public:
hudakz 0:84353c479782 83
hudakz 0:84353c479782 84 virtual uint8_t OnInit(FTDI *pftdi __attribute__((unused))) {
hudakz 0:84353c479782 85 return 0;
hudakz 0:84353c479782 86 };
hudakz 0:84353c479782 87
hudakz 0:84353c479782 88 virtual uint8_t OnRelease(FTDI *pftdi __attribute__((unused))) {
hudakz 0:84353c479782 89 return 0;
hudakz 0:84353c479782 90 };
hudakz 0:84353c479782 91 };
hudakz 0:84353c479782 92
hudakz 0:84353c479782 93
hudakz 0:84353c479782 94 // Only single port chips are currently supported by the library,
hudakz 0:84353c479782 95 // so only three endpoints are allocated.
hudakz 0:84353c479782 96 #define FTDI_MAX_ENDPOINTS 3
hudakz 0:84353c479782 97
hudakz 0:84353c479782 98 class FTDI : public USBDeviceConfig, public UsbConfigXtracter {
hudakz 0:84353c479782 99 static const uint8_t epDataInIndex; // DataIn endpoint index
hudakz 0:84353c479782 100 static const uint8_t epDataOutIndex; // DataOUT endpoint index
hudakz 0:84353c479782 101 static const uint8_t epInterruptInIndex; // InterruptIN endpoint index
hudakz 0:84353c479782 102
hudakz 0:84353c479782 103 FTDIAsyncOper *pAsync;
hudakz 0:84353c479782 104 USB *pUsb;
hudakz 0:84353c479782 105 uint8_t bAddress;
hudakz 0:84353c479782 106 uint8_t bConfNum; // configuration number
hudakz 0:84353c479782 107 uint8_t bNumIface; // number of interfaces in the configuration
hudakz 0:84353c479782 108 uint8_t bNumEP; // total number of EP in the configuration
hudakz 0:84353c479782 109 uint32_t qNextPollTime; // next poll time
hudakz 0:84353c479782 110 volatile bool bPollEnable; // poll enable flag
hudakz 0:84353c479782 111 volatile bool ready; //device ready indicator
hudakz 0:84353c479782 112 uint16_t wFTDIType; // Type of FTDI chip
hudakz 0:84353c479782 113 uint16_t wIdProduct; // expected PID
hudakz 0:84353c479782 114
hudakz 0:84353c479782 115 EpInfo epInfo[FTDI_MAX_ENDPOINTS];
hudakz 0:84353c479782 116
hudakz 0:84353c479782 117 void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr);
hudakz 0:84353c479782 118
hudakz 0:84353c479782 119 public:
hudakz 0:84353c479782 120 FTDI(USB *pusb, FTDIAsyncOper *pasync, uint16_t idProduct = FTDI_PID);
hudakz 0:84353c479782 121
hudakz 0:84353c479782 122 uint8_t SetBaudRate(uint32_t baud);
hudakz 0:84353c479782 123 uint8_t SetModemControl(uint16_t control);
hudakz 0:84353c479782 124 uint8_t SetFlowControl(uint8_t protocol, uint8_t xon = 0x11, uint8_t xoff = 0x13);
hudakz 0:84353c479782 125 uint8_t SetData(uint16_t databm);
hudakz 0:84353c479782 126 uint8_t SetLatency(uint8_t l);
hudakz 0:84353c479782 127 uint8_t GetLatency(uint8_t *l);
hudakz 0:84353c479782 128
hudakz 0:84353c479782 129 // Methods for receiving and sending data
hudakz 0:84353c479782 130 uint8_t RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr);
hudakz 0:84353c479782 131 uint8_t SndData(uint16_t nbytes, uint8_t *dataptr);
hudakz 0:84353c479782 132
hudakz 0:84353c479782 133 // USBDeviceConfig implementation
hudakz 0:84353c479782 134 uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
hudakz 0:84353c479782 135 uint8_t Release();
hudakz 0:84353c479782 136 uint8_t Poll();
hudakz 0:84353c479782 137
hudakz 0:84353c479782 138 virtual uint8_t GetAddress() {
hudakz 0:84353c479782 139 return bAddress;
hudakz 0:84353c479782 140 };
hudakz 0:84353c479782 141
hudakz 0:84353c479782 142 // UsbConfigXtracter implementation
hudakz 0:84353c479782 143 void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
hudakz 0:84353c479782 144
hudakz 0:84353c479782 145 virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
hudakz 0:84353c479782 146 return (vid == FTDI_VID && pid == FTDI_PID);
hudakz 0:84353c479782 147 }
hudakz 0:84353c479782 148 virtual bool isReady() {
hudakz 0:84353c479782 149 return ready;
hudakz 0:84353c479782 150 };
hudakz 0:84353c479782 151
hudakz 0:84353c479782 152 };
hudakz 0:84353c479782 153
hudakz 0:84353c479782 154 #endif // __CDCFTDI_H__