You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

cdcftdi.h 5.4KB

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