24 #include "libsigrok-internal.h"
29 #define LOG_PREFIX "serial-cp2110"
32 #ifdef HAVE_SERIAL_COMM
41 #define CP2110_MAX_BYTES_PER_REQUEST 63
43 static const struct vid_pid_item vid_pid_items_cp2110[] = {
48 enum cp2110_report_id {
49 CP2110_UART_ENDIS = 0x41,
50 CP2110_UART_STATUS = 0x42,
51 CP2110_FIFO_PURGE = 0x43,
52 CP2110_UART_CONFIG = 0x50,
55 enum cp2110_uart_enable_param {
56 CP2110_UART_DISABLE = 0,
57 CP2110_UART_ENABLE = 1,
60 enum cp2110_fifo_purge_flag {
61 CP2110_FIFO_PURGE_TX = (1 << 0),
62 CP2110_FIFO_PURGE_RX = (1 << 1),
65 enum cp2110_uart_config_bitrate {
66 CP2110_BAUDRATE_MIN = 300,
67 CP2110_BAUDRATE_MAX = 1000000,
70 enum cp2110_uart_config_databits {
71 CP2110_DATABITS_MIN = 5,
72 CP2110_DATABITS_MAX = 8,
75 enum cp2110_uart_config_parity {
76 CP2110_PARITY_NONE = 0,
77 CP2110_PARITY_EVEN = 1,
78 CP2110_PARITY_ODD = 2,
79 CP2110_PARITY_MARK = 3,
80 CP2110_PARITY_SPACE = 4,
83 enum cp2110_uart_config_stopbits {
84 CP2110_STOPBITS_SHORT = 0,
85 CP2110_STOPBITS_LONG = 1,
89 enum cp2110_uart_config_flowctrl {
90 CP2110_FLOWCTRL_NONE = 0,
91 CP2110_FLOWCTRL_HARD = 1,
94 static int cp2110_set_params(
struct sr_serial_dev_inst *serial,
95 int baudrate,
int bits,
int parity,
int stopbits,
96 int flowcontrol,
int rts,
int dtr)
103 if (baudrate < CP2110_BAUDRATE_MIN || baudrate > CP2110_BAUDRATE_MAX) {
104 sr_err(
"CP2110: baudrate %d out of range", baudrate);
107 if (bits < CP2110_DATABITS_MIN || bits > CP2110_DATABITS_MAX) {
108 sr_err(
"CP2110: %d databits out of range", bits);
111 bits -= CP2110_DATABITS_MIN;
114 parity = CP2110_PARITY_NONE;
117 parity = CP2110_PARITY_ODD;
120 parity = CP2110_PARITY_EVEN;
123 parity = CP2110_PARITY_MARK;
125 case SP_PARITY_SPACE:
126 parity = CP2110_PARITY_SPACE;
129 sr_err(
"CP2110: unknown parity spec %d", parity);
134 stopbits = CP2110_STOPBITS_SHORT;
137 stopbits = CP2110_STOPBITS_LONG;
140 sr_err(
"CP2110: unknown stop bits spec %d", stopbits);
143 switch (flowcontrol) {
144 case SP_FLOWCONTROL_NONE:
145 flowcontrol = CP2110_FLOWCTRL_NONE;
147 case SP_FLOWCONTROL_XONXOFF:
148 sr_err(
"CP2110: unsupported XON/XOFF flow control spec");
150 case SP_FLOWCONTROL_RTSCTS:
151 flowcontrol = CP2110_FLOWCTRL_HARD;
154 sr_err(
"CP2110: unknown flow control spec %d", flowcontrol);
163 report[replen++] = CP2110_UART_ENDIS;
164 report[replen++] = CP2110_UART_ENABLE;
181 report[replen++] = CP2110_UART_CONFIG;
182 WB32(&report[replen], baudrate);
183 replen +=
sizeof(uint32_t);
184 report[replen++] = parity;
185 report[replen++] = flowcontrol;
186 report[replen++] = bits;
187 report[replen++] = stopbits;
205 static int cp2110_read_bytes(
struct sr_serial_dev_inst *serial,
206 uint8_t *data,
int space,
unsigned int timeout)
208 uint8_t buffer[1 + CP2110_MAX_BYTES_PER_REQUEST];
220 memset(buffer, 0,
sizeof(buffer));
228 sr_dbg(
"DBG: %s() got report len %d, 0x%02x.", __func__, rc, buffer[0]);
234 if (count > CP2110_MAX_BYTES_PER_REQUEST)
236 sr_dbg(
"DBG: %s(), got %d UART RX bytes.", __func__, count);
241 memcpy(data, &buffer[1], count);
245 static int cp2110_write_bytes(
struct sr_serial_dev_inst *serial,
246 const uint8_t *data,
int size)
248 uint8_t buffer[1 + CP2110_MAX_BYTES_PER_REQUEST];
251 sr_dbg(
"DBG: %s() shall send UART TX data, len %d.", __func__, size);
255 if (size > CP2110_MAX_BYTES_PER_REQUEST) {
256 size = CP2110_MAX_BYTES_PER_REQUEST;
257 sr_dbg(
"DBG: %s() capping size to %d.", __func__, size);
266 memcpy(&buffer[1], data, size);
275 static int cp2110_flush(
struct sr_serial_dev_inst *serial)
280 sr_dbg(
"DBG: %s() discarding RX and TX FIFO data.", __func__);
282 buffer[0] = CP2110_FIFO_PURGE;
283 buffer[1] = CP2110_FIFO_PURGE_TX | CP2110_FIFO_PURGE_RX;
290 static int cp2110_drain(
struct sr_serial_dev_inst *serial)
294 uint16_t tx_fill, rx_fill;
296 sr_dbg(
"DBG: %s() waiting for TX data to drain.", __func__);
310 memset(buffer, 0,
sizeof(buffer));
311 buffer[0] = CP2110_UART_STATUS;
313 if (rc !=
sizeof(buffer)) {
317 if (buffer[0] != CP2110_UART_STATUS) {
321 rx_fill = RB16(&buffer[1]);
322 tx_fill = RB16(&buffer[3]);
330 sr_dbg(
"DBG: %s() TX drained, rc %d, RX fill %u, returning.",
331 __func__, rc, (
unsigned int)rx_fill);
335 static struct ser_hid_chip_functions chip_cp2110 = {
336 .chipname =
"cp2110",
337 .chipdesc =
"SiLabs CP2110",
338 .vid_pid_items = vid_pid_items_cp2110,
339 .max_bytes_per_request = CP2110_MAX_BYTES_PER_REQUEST,
340 .set_params = cp2110_set_params,
341 .read_bytes = cp2110_read_bytes,
342 .write_bytes = cp2110_write_bytes,
343 .flush = cp2110_flush,
344 .drain = cp2110_drain,
346 SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_cp2110 = &chip_cp2110;
350 SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_cp2110 = NULL;
Generic/unspecified error.
SR_PRIV int ser_hid_hidapi_set_data(struct sr_serial_dev_inst *serial, uint8_t ep, const uint8_t *data, size_t len, int timeout)
SR_PRIV int ser_hid_hidapi_set_report(struct sr_serial_dev_inst *serial, const uint8_t *data, size_t len)
The public libsigrok header file to be used by frontends.
SR_PRIV int ser_hid_hidapi_get_data(struct sr_serial_dev_inst *serial, uint8_t ep, uint8_t *data, size_t len, int timeout)