2 * This file is part of the libsigrok project.
4 * Copyright (C) 2017 Carl-Fredrik Sundström <audio.cf@gmail.com>
5 * Copyright (C) 2017-2019 Gerhard Sittig <gerhard.sittig@gmx.net>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <libsigrok/libsigrok.h>
24 #include "libsigrok-internal.h"
25 #include "serial_hid.h"
28 #define LOG_PREFIX "serial-cp2110"
30 #ifdef HAVE_SERIAL_COMM
36 * Support serial-over-HID, specifically the SiLabs CP2110 chip.
39 #define CP2110_MAX_BYTES_PER_REQUEST 63
41 static const struct vid_pid_item vid_pid_items_cp2110
[] = {
46 enum cp2110_report_id
{
47 CP2110_UART_ENDIS
= 0x41,
48 CP2110_UART_STATUS
= 0x42,
49 CP2110_FIFO_PURGE
= 0x43,
50 CP2110_UART_CONFIG
= 0x50,
53 enum cp2110_uart_enable_param
{
54 CP2110_UART_DISABLE
= 0,
55 CP2110_UART_ENABLE
= 1,
58 enum cp2110_fifo_purge_flag
{
59 CP2110_FIFO_PURGE_TX
= (1 << 0),
60 CP2110_FIFO_PURGE_RX
= (1 << 1),
63 enum cp2110_uart_config_bitrate
{
64 CP2110_BAUDRATE_MIN
= 300,
65 CP2110_BAUDRATE_MAX
= 1000000,
68 enum cp2110_uart_config_databits
{
69 CP2110_DATABITS_MIN
= 5,
70 CP2110_DATABITS_MAX
= 8,
73 enum cp2110_uart_config_parity
{
74 CP2110_PARITY_NONE
= 0,
75 CP2110_PARITY_EVEN
= 1,
76 CP2110_PARITY_ODD
= 2,
77 CP2110_PARITY_MARK
= 3,
78 CP2110_PARITY_SPACE
= 4,
81 enum cp2110_uart_config_stopbits
{
82 CP2110_STOPBITS_SHORT
= 0,
83 CP2110_STOPBITS_LONG
= 1,
86 /* Hardware flow control on CP2110 is RTS/CTS only. */
87 enum cp2110_uart_config_flowctrl
{
88 CP2110_FLOWCTRL_NONE
= 0,
89 CP2110_FLOWCTRL_HARD
= 1,
92 static int cp2110_set_params(struct sr_serial_dev_inst
*serial
,
93 int baudrate
, int bits
, int parity
, int stopbits
,
94 int flowcontrol
, int rts
, int dtr
)
100 /* Map serial API specs to CP2110 register values. Check ranges. */
101 if (baudrate
< CP2110_BAUDRATE_MIN
|| baudrate
> CP2110_BAUDRATE_MAX
) {
102 sr_err("CP2110: baudrate %d out of range", baudrate
);
105 if (bits
< CP2110_DATABITS_MIN
|| bits
> CP2110_DATABITS_MAX
) {
106 sr_err("CP2110: %d databits out of range", bits
);
109 bits
-= CP2110_DATABITS_MIN
;
112 parity
= CP2110_PARITY_NONE
;
115 parity
= CP2110_PARITY_ODD
;
118 parity
= CP2110_PARITY_EVEN
;
121 parity
= CP2110_PARITY_MARK
;
123 case SP_PARITY_SPACE
:
124 parity
= CP2110_PARITY_SPACE
;
127 sr_err("CP2110: unknown parity spec %d", parity
);
132 stopbits
= CP2110_STOPBITS_SHORT
;
135 stopbits
= CP2110_STOPBITS_LONG
;
138 sr_err("CP2110: unknown stop bits spec %d", stopbits
);
141 switch (flowcontrol
) {
142 case SP_FLOWCONTROL_NONE
:
143 flowcontrol
= CP2110_FLOWCTRL_NONE
;
145 case SP_FLOWCONTROL_XONXOFF
:
146 sr_err("CP2110: unsupported XON/XOFF flow control spec");
148 case SP_FLOWCONTROL_RTSCTS
:
149 flowcontrol
= CP2110_FLOWCTRL_HARD
;
152 sr_err("CP2110: unknown flow control spec %d", flowcontrol
);
157 * Enable the UART. Report layout:
158 * @0, length 1, enabled state (0: disable, 1: enable)
161 report
[replen
++] = CP2110_UART_ENDIS
;
162 report
[replen
++] = CP2110_UART_ENABLE
;
163 rc
= ser_hid_hidapi_set_report(serial
, report
, replen
);
170 * Setup bitrate and frame format. Report layout:
171 * (@-1, length 1, report number)
172 * @0, length 4, bitrate (big endian format)
173 * @4, length 1, parity
174 * @5, length 1, flow control
175 * @6, length 1, data bits (0: 5, 1: 6, 2: 7, 3: 8)
176 * @7, length 1, stop bits
179 report
[replen
++] = CP2110_UART_CONFIG
;
180 WB32(&report
[replen
], baudrate
);
181 replen
+= sizeof(uint32_t);
182 report
[replen
++] = parity
;
183 report
[replen
++] = flowcontrol
;
184 report
[replen
++] = bits
;
185 report
[replen
++] = stopbits
;
186 rc
= ser_hid_hidapi_set_report(serial
, report
, replen
);
193 * Currently not implemented: Control RTS and DTR state.
194 * TODO are these controlled via GPIO requests?
195 * GPIO.1 == RTS, can't find DTR in AN433 table 4.3
203 static int cp2110_read_bytes(struct sr_serial_dev_inst
*serial
,
204 uint8_t *data
, int space
, unsigned int timeout
)
206 uint8_t buffer
[1 + CP2110_MAX_BYTES_PER_REQUEST
];
213 * Check for available input data from the serial port.
215 * @0, length 1, number of bytes, range 0-63
216 * @1, length N, data bytes
218 memset(buffer
, 0, sizeof(buffer
));
219 rc
= ser_hid_hidapi_get_data(serial
, 0, buffer
, sizeof(buffer
), timeout
);
220 if (rc
== SR_ERR_TIMEOUT
)
226 sr_dbg("DBG: %s() got report len %d, 0x%02x.", __func__
, rc
, buffer
[0]);
228 /* Check the length spec, get the byte count. */
232 if (count
> CP2110_MAX_BYTES_PER_REQUEST
)
234 sr_dbg("DBG: %s(), got %d UART RX bytes.", __func__
, count
);
238 /* Pass received data bytes and their count to the caller. */
239 memcpy(data
, &buffer
[1], count
);
243 static int cp2110_write_bytes(struct sr_serial_dev_inst
*serial
,
244 const uint8_t *data
, int size
)
246 uint8_t buffer
[1 + CP2110_MAX_BYTES_PER_REQUEST
];
249 sr_dbg("DBG: %s() shall send UART TX data, len %d.", __func__
, size
);
253 if (size
> CP2110_MAX_BYTES_PER_REQUEST
) {
254 size
= CP2110_MAX_BYTES_PER_REQUEST
;
255 sr_dbg("DBG: %s() capping size to %d.", __func__
, size
);
259 * Packet layout to send serial data to the USB HID chip:
260 * @0, length 1, number of bytes, range 0-63
261 * @1, length N, data bytes
264 memcpy(&buffer
[1], data
, size
);
265 rc
= ser_hid_hidapi_set_data(serial
, 0, buffer
, sizeof(buffer
), 0);
273 static int cp2110_flush(struct sr_serial_dev_inst
*serial
)
278 sr_dbg("DBG: %s() discarding RX and TX FIFO data.", __func__
);
280 buffer
[0] = CP2110_FIFO_PURGE
;
281 buffer
[1] = CP2110_FIFO_PURGE_TX
| CP2110_FIFO_PURGE_RX
;
282 rc
= ser_hid_hidapi_set_data(serial
, 0, buffer
, sizeof(buffer
), 0);
283 if (rc
!= sizeof(buffer
))
288 static int cp2110_drain(struct sr_serial_dev_inst
*serial
)
292 uint16_t tx_fill
, rx_fill
;
294 sr_dbg("DBG: %s() waiting for TX data to drain.", __func__
);
297 * Keep retrieving the UART status until the FIFO is found empty,
298 * or an error occured.
300 * @0, length 1, report ID
301 * @1, length 2, number of bytes in the TX FIFO (up to 480)
302 * @3, length 2, number of bytes in the RX FIFO (up to 480)
303 * @5, length 1, error status (parity and overrun error flags)
304 * @6, length 1, line break status
308 memset(buffer
, 0, sizeof(buffer
));
309 buffer
[0] = CP2110_UART_STATUS
;
310 rc
= ser_hid_hidapi_get_data(serial
, 0, buffer
, sizeof(buffer
), 0);
311 if (rc
!= sizeof(buffer
)) {
315 if (buffer
[0] != CP2110_UART_STATUS
) {
319 rx_fill
= RB16(&buffer
[1]);
320 tx_fill
= RB16(&buffer
[3]);
328 sr_dbg("DBG: %s() TX drained, rc %d, RX fill %u, returning.",
329 __func__
, rc
, (unsigned int)rx_fill
);
333 static struct ser_hid_chip_functions chip_cp2110
= {
334 .chipname
= "cp2110",
335 .chipdesc
= "SiLabs CP2110",
336 .vid_pid_items
= vid_pid_items_cp2110
,
337 .max_bytes_per_request
= CP2110_MAX_BYTES_PER_REQUEST
,
338 .set_params
= cp2110_set_params
,
339 .read_bytes
= cp2110_read_bytes
,
340 .write_bytes
= cp2110_write_bytes
,
341 .flush
= cp2110_flush
,
342 .drain
= cp2110_drain
,
344 SR_PRIV
struct ser_hid_chip_functions
*ser_hid_chip_funcs_cp2110
= &chip_cp2110
;
348 SR_PRIV
struct ser_hid_chip_functions
*ser_hid_chip_funcs_cp2110
= NULL
;