usbh_cp210x.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2024 ~ 2025, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef USBH_CP210X_H
  7. #define USBH_CP210X_H
  8. #include "usb_cdc.h"
  9. /* Requests */
  10. #define CP210X_IFC_ENABLE 0x00
  11. #define CP210X_SET_BAUDDIV 0x01
  12. #define CP210X_GET_BAUDDIV 0x02
  13. #define CP210X_SET_LINE_CTL 0x03 // Set parity, data bits, stop bits
  14. #define CP210X_GET_LINE_CTL 0x04
  15. #define CP210X_SET_BREAK 0x05
  16. #define CP210X_IMM_CHAR 0x06
  17. #define CP210X_SET_MHS 0x07 // Set DTR, RTS
  18. #define CP210X_GET_MDMSTS 0x08
  19. #define CP210X_SET_XON 0x09
  20. #define CP210X_SET_XOFF 0x0A
  21. #define CP210X_SET_EVENTMASK 0x0B
  22. #define CP210X_GET_EVENTMASK 0x0C
  23. #define CP210X_SET_CHAR 0x0D
  24. #define CP210X_GET_CHARS 0x0E
  25. #define CP210X_GET_PROPS 0x0F
  26. #define CP210X_GET_COMM_STATUS 0x10
  27. #define CP210X_RESET 0x11
  28. #define CP210X_PURGE 0x12
  29. #define CP210X_SET_FLOW 0x13
  30. #define CP210X_GET_FLOW 0x14
  31. #define CP210X_EMBED_EVENTS 0x15
  32. #define CP210X_GET_EVENTSTATE 0x16
  33. #define CP210X_SET_CHARS 0x19
  34. #define CP210X_GET_BAUDRATE 0x1D
  35. #define CP210X_SET_BAUDRATE 0x1E // Set baudrate
  36. #define CP210X_VENDOR_SPECIFIC 0xFF
  37. /* CP210X_VENDOR_SPECIFIC values */
  38. #define CP210X_GET_FW_VER 0x000E
  39. #define CP210X_READ_2NCONFIG 0x000E
  40. #define CP210X_GET_FW_VER_2N 0x0010
  41. #define CP210X_READ_LATCH 0x00C2
  42. #define CP210X_GET_PARTNUM 0x370B
  43. #define CP210X_GET_PORTCONFIG 0x370C
  44. #define CP210X_GET_DEVICEMODE 0x3711
  45. #define CP210X_WRITE_LATCH 0x37E1
  46. /* CP210X_IFC_ENABLE */
  47. #define CP210X_UART_ENABLE 0x0001
  48. #define CP210X_UART_DISABLE 0x0000
  49. /* CP210X_(SET|GET)_BAUDDIV */
  50. #define CP210X_BAUD_RATE_GEN_FREQ 0x384000
  51. /* CP210X_(SET|GET)_LINE_CTL */
  52. #define CP210X_BITS_DATA_MASK 0X0f00
  53. #define CP210X_BITS_DATA_5 0X0500
  54. #define CP210X_BITS_DATA_6 0X0600
  55. #define CP210X_BITS_DATA_7 0X0700
  56. #define CP210X_BITS_DATA_8 0X0800
  57. #define CP210X_BITS_DATA_9 0X0900
  58. #define CP210X_BITS_PARITY_MASK 0x00f0
  59. #define CP210X_BITS_PARITY_NONE 0x0000
  60. #define CP210X_BITS_PARITY_ODD 0x0010
  61. #define CP210X_BITS_PARITY_EVEN 0x0020
  62. #define CP210X_BITS_PARITY_MARK 0x0030
  63. #define CP210X_BITS_PARITY_SPACE 0x0040
  64. #define CP210X_BITS_STOP_MASK 0x000f
  65. #define CP210X_BITS_STOP_1 0x0000
  66. #define CP210X_BITS_STOP_1_5 0x0001
  67. #define CP210X_BITS_STOP_2 0x0002
  68. /* CP210X_SET_BREAK */
  69. #define CP210X_BREAK_ON 0x0001
  70. #define CP210X_BREAK_OFF 0x0000
  71. /* CP210X_(SET_MHS|GET_MDMSTS) */
  72. #define CP210X_CONTROL_DTR 0x0001
  73. #define CP210X_CONTROL_RTS 0x0002
  74. #define CP210X_CONTROL_CTS 0x0010
  75. #define CP210X_CONTROL_DSR 0x0020
  76. #define CP210X_CONTROL_RING 0x0040
  77. #define CP210X_CONTROL_DCD 0x0080
  78. #define CP210X_CONTROL_WRITE_DTR 0x0100
  79. #define CP210X_CONTROL_WRITE_RTS 0x0200
  80. /* CP210X_(GET|SET)_CHARS */
  81. struct cp210x_special_chars {
  82. uint8_t bEofChar;
  83. uint8_t bErrorChar;
  84. uint8_t bBreakChar;
  85. uint8_t bEventChar;
  86. uint8_t bXonChar;
  87. uint8_t bXoffChar;
  88. };
  89. /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
  90. struct cp210x_comm_status {
  91. uint32_t ulErrors;
  92. uint32_t ulHoldReasons;
  93. uint32_t ulAmountInInQueue;
  94. uint32_t ulAmountInOutQueue;
  95. uint8_t bEofReceived;
  96. uint8_t bWaitForImmediate;
  97. uint8_t bReserved;
  98. } __PACKED;
  99. /*
  100. * CP210X_PURGE - 16 bits passed in wValue of USB request.
  101. * SiLabs app note AN571 gives a strange description of the 4 bits:
  102. * bit 0 or bit 2 clears the transmit queue and 1 or 3 receive.
  103. * writing 1 to all, however, purges cp2108 well enough to avoid the hang.
  104. */
  105. #define PURGE_ALL 0x000f
  106. /* CP210X_EMBED_EVENTS */
  107. #define CP210X_ESCCHAR 0xec
  108. #define CP210X_LSR_OVERRUN BIT(1)
  109. #define CP210X_LSR_PARITY BIT(2)
  110. #define CP210X_LSR_FRAME BIT(3)
  111. #define CP210X_LSR_BREAK BIT(4)
  112. /* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */
  113. struct cp210x_flow_ctl {
  114. uint32_t lControlHandshake;
  115. uint32_t lFlowReplace;
  116. uint32_t lXonLimit;
  117. uint32_t lXoffLimit;
  118. };
  119. /* cp210x_flow_ctl::ulControlHandshake */
  120. #define CP210X_SERIAL_DTR_MASK (0x03 << 0)
  121. #define CP210X_SERIAL_DTR_INACTIVE (0 << 0)
  122. #define CP210X_SERIAL_DTR_ACTIVE (1 << 0)
  123. #define CP210X_SERIAL_DTR_FLOW_CTL (2 << 0)
  124. #define CP210X_SERIAL_CTS_HANDSHAKE BIT(3)
  125. #define CP210X_SERIAL_DSR_HANDSHAKE BIT(4)
  126. #define CP210X_SERIAL_DCD_HANDSHAKE BIT(5)
  127. #define CP210X_SERIAL_DSR_SENSITIVITY BIT(6)
  128. /* cp210x_flow_ctl::ulFlowReplace */
  129. #define CP210X_SERIAL_AUTO_TRANSMIT BIT(0)
  130. #define CP210X_SERIAL_AUTO_RECEIVE BIT(1)
  131. #define CP210X_SERIAL_ERROR_CHAR BIT(2)
  132. #define CP210X_SERIAL_NULL_STRIPPING BIT(3)
  133. #define CP210X_SERIAL_BREAK_CHAR BIT(4)
  134. #define CP210X_SERIAL_RTS_MASK (0x03 << 6)
  135. #define CP210X_SERIAL_RTS_INACTIVE (0 << 6)
  136. #define CP210X_SERIAL_RTS_ACTIVE (1 << 6)
  137. #define CP210X_SERIAL_RTS_FLOW_CTL (2 << 6)
  138. #define CP210X_SERIAL_XOFF_CONTINUE BIT(31)
  139. /* CP210X_VENDOR_SPECIFIC, CP210X_GET_DEVICEMODE call reads these 0x2 bytes. */
  140. struct cp210x_pin_mode {
  141. uint8_t eci;
  142. uint8_t sci;
  143. };
  144. #define CP210X_PIN_MODE_MODEM 0
  145. #define CP210X_PIN_MODE_GPIO BIT(0)
  146. /* Part number definitions */
  147. #define CP210X_PARTNUM_CP2101 0x01
  148. #define CP210X_PARTNUM_CP2102 0x02
  149. #define CP210X_PARTNUM_CP2103 0x03
  150. #define CP210X_PARTNUM_CP2104 0x04
  151. #define CP210X_PARTNUM_CP2105 0x05
  152. #define CP210X_PARTNUM_CP2108 0x08
  153. #define CP210X_PARTNUM_CP2102N_QFN28 0x20
  154. #define CP210X_PARTNUM_CP2102N_QFN24 0x21
  155. #define CP210X_PARTNUM_CP2102N_QFN20 0x22
  156. #define CP210X_PARTNUM_UNKNOWN 0xFF
  157. #ifdef __cplusplus
  158. extern "C" {
  159. #endif
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif /* USBH_CP210X_H */