2 * This file is part of the libsigrok project.
4 * Copyright (C) 2017-2019 Gerhard Sittig <gerhard.sittig@gmx.net>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <libsigrok/libsigrok.h>
23 #include "libsigrok-internal.h"
24 #include "serial_hid.h"
27 #define LOG_PREFIX "serial-ch9325"
29 #ifdef HAVE_SERIAL_COMM
35 * Support serial-over-HID, specifically the WCH CH9325 chip.
38 #define CH9325_MAX_BYTES_PER_REQUEST 7
40 static const struct vid_pid_item vid_pid_items_ch9325[] = {
41 { 0x1a86, 0xe008, }, /* CH9325 */
43 * Strictly speaking Hoitek HE2325U is a different chip, but
44 * shares the programming model with WCH CH9325, and works
45 * with the same support code.
47 { 0x04fa, 0x2490, }, /* HE2325U */
51 static int ch9325_set_params(struct sr_serial_dev_inst *serial,
52 int baudrate, int bits, int parity, int stopbits,
53 int flowcontrol, int rts, int dtr)
67 * Setup bitrate and frame format. Report layout:
68 * (@-1, length 1, report number)
69 * @0, length 2, bitrate (little endian format)
70 * @2, length 1, unknown (parity? stop bits?)
71 * @3, length 1, unknown (parity? stop bits?)
72 * @4, length 1, data bits (0: 5, 1: 6, etc, 3: 8)
76 WL16(&report[replen], baudrate);
77 replen += sizeof(uint16_t);
78 report[replen++] = 0x00;
79 report[replen++] = 0x00;
80 report[replen++] = bits - 5;
81 rc = ser_hid_hidapi_set_report(serial, report, replen);
82 text = sr_hexdump_new(report, replen);
83 sr_dbg("DBG: %s() report %s => rc %d", __func__, text->str, rc);
84 sr_hexdump_free(text);
93 static int ch9325_read_bytes(struct sr_serial_dev_inst *serial,
94 uint8_t *data, int space, unsigned int timeout)
96 uint8_t buffer[1 + CH9325_MAX_BYTES_PER_REQUEST];
101 * Check for available input data from the serial port.
103 * @0, length 1, number of bytes, OR-ed with 0xf0
104 * @1, length N, data bytes (up to 7 bytes)
106 rc = ser_hid_hidapi_get_data(serial, 2, buffer, sizeof(buffer), timeout);
111 sr_dbg("DBG: %s() got report len %d, 0x%02x.", __func__, rc, buffer[0]);
113 /* Check the length spec, get the byte count. */
115 if ((count & 0xf0) != 0xf0)
118 sr_dbg("DBG: %s(), got %d UART RX bytes.", __func__, count);
122 /* Pass received data bytes and their count to the caller. */
123 memcpy(data, &buffer[1], count);
127 static int ch9325_write_bytes(struct sr_serial_dev_inst *serial,
128 const uint8_t *data, int size)
130 uint8_t buffer[1 + CH9325_MAX_BYTES_PER_REQUEST];
133 sr_dbg("DBG: %s() shall send UART TX data, len %d.", __func__, size);
137 if (size > CH9325_MAX_BYTES_PER_REQUEST) {
138 size = CH9325_MAX_BYTES_PER_REQUEST;
139 sr_dbg("DBG: %s() capping size to %d.", __func__, size);
143 * Packet layout to send serial data to the USB HID chip:
144 * (@-1, length 1, report number)
145 * @0, length 1, number of bytes, OR-ed with 0xf0
146 * @1, length N, data bytes (up to 7 bytes)
148 buffer[0] = size; /* YES! TX is *without* 0xf0! */
149 memcpy(&buffer[1], data, size);
150 rc = ser_hid_hidapi_set_data(serial, 2, buffer, sizeof(buffer), 0);
158 static struct ser_hid_chip_functions chip_ch9325 = {
159 .chipname = "ch9325",
160 .chipdesc = "WCH CH9325",
161 .vid_pid_items = vid_pid_items_ch9325,
162 .max_bytes_per_request = CH9325_MAX_BYTES_PER_REQUEST,
163 .set_params = ch9325_set_params,
164 .read_bytes = ch9325_read_bytes,
165 .write_bytes = ch9325_write_bytes,
167 SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_ch9325 = &chip_ch9325;
171 SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_ch9325 = NULL;