libsigrok  0.5.2
sigrok hardware access and backend library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
serial_hid_ch9325.c
Go to the documentation of this file.
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2017-2019 Gerhard Sittig <gerhard.sittig@gmx.net>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 
20 #include <config.h>
21 #include <glib.h>
22 #include <libsigrok/libsigrok.h>
23 #include "libsigrok-internal.h"
24 #include "serial_hid.h"
25 #include <string.h>
26 
27 /** @cond PRIVATE */
28 #define LOG_PREFIX "serial-ch9325"
29 /** @endcond */
30 
31 #ifdef HAVE_SERIAL_COMM
32 #ifdef HAVE_LIBHIDAPI
33 
34 /**
35  * @file
36  *
37  * Support serial-over-HID, specifically the WCH CH9325 chip.
38  */
39 
40 #define CH9325_MAX_BYTES_PER_REQUEST 7
41 
42 static const struct vid_pid_item vid_pid_items_ch9325[] = {
43  { 0x1a86, 0xe008, }, /* CH9325 */
44  /*
45  * Strictly speaking Hoitek HE2325U is a different chip, but
46  * shares the programming model with WCH CH9325, and works
47  * with the same support code.
48  */
49  { 0x04fa, 0x2490, }, /* HE2325U */
50  ALL_ZERO
51 };
52 
53 static int ch9325_set_params(struct sr_serial_dev_inst *serial,
54  int baudrate, int bits, int parity, int stopbits,
55  int flowcontrol, int rts, int dtr)
56 {
57  uint8_t report[6];
58  int replen;
59  int rc;
60  GString *text;
61 
62  (void)parity;
63  (void)stopbits;
64  (void)flowcontrol;
65  (void)rts;
66  (void)dtr;
67 
68  /*
69  * Setup bitrate and frame format. Report layout:
70  * (@-1, length 1, report number)
71  * @0, length 2, bitrate (little endian format)
72  * @2, length 1, unknown (parity? stop bits?)
73  * @3, length 1, unknown (parity? stop bits?)
74  * @4, length 1, data bits (0: 5, 1: 6, etc, 3: 8)
75  */
76  replen = 0;
77  report[replen++] = 0;
78  WL16(&report[replen], baudrate);
79  replen += sizeof(uint16_t);
80  report[replen++] = 0x00;
81  report[replen++] = 0x00;
82  report[replen++] = bits - 5;
83  rc = ser_hid_hidapi_set_report(serial, report, replen);
84  text = sr_hexdump_new(report, replen);
85  sr_dbg("DBG: %s() report %s => rc %d", __func__, text->str, rc);
86  sr_hexdump_free(text);
87  if (rc < 0)
88  return SR_ERR;
89  if (rc != replen)
90  return SR_ERR;
91 
92  return SR_OK;
93 }
94 
95 static int ch9325_read_bytes(struct sr_serial_dev_inst *serial,
96  uint8_t *data, int space, unsigned int timeout)
97 {
98  uint8_t buffer[1 + CH9325_MAX_BYTES_PER_REQUEST];
99  int rc;
100  int count;
101 
102  /*
103  * Check for available input data from the serial port.
104  * Packet layout:
105  * @0, length 1, number of bytes, OR-ed with 0xf0
106  * @1, length N, data bytes (up to 7 bytes)
107  */
108  rc = ser_hid_hidapi_get_data(serial, 2, buffer, sizeof(buffer), timeout);
109  if (rc < 0)
110  return SR_ERR;
111  if (rc == 0)
112  return 0;
113  sr_dbg("DBG: %s() got report len %d, 0x%02x.", __func__, rc, buffer[0]);
114 
115  /* Check the length spec, get the byte count. */
116  count = buffer[0];
117  if ((count & 0xf0) != 0xf0)
118  return SR_ERR;
119  count &= 0x0f;
120  sr_dbg("DBG: %s(), got %d UART RX bytes.", __func__, count);
121  if (count > space)
122  return SR_ERR;
123 
124  /* Pass received data bytes and their count to the caller. */
125  memcpy(data, &buffer[1], count);
126  return count;
127 }
128 
129 static int ch9325_write_bytes(struct sr_serial_dev_inst *serial,
130  const uint8_t *data, int size)
131 {
132  uint8_t buffer[1 + CH9325_MAX_BYTES_PER_REQUEST];
133  int rc;
134 
135  sr_dbg("DBG: %s() shall send UART TX data, len %d.", __func__, size);
136 
137  if (size < 1)
138  return 0;
139  if (size > CH9325_MAX_BYTES_PER_REQUEST) {
140  size = CH9325_MAX_BYTES_PER_REQUEST;
141  sr_dbg("DBG: %s() capping size to %d.", __func__, size);
142  }
143 
144  /*
145  * Packet layout to send serial data to the USB HID chip:
146  * (@-1, length 1, report number)
147  * @0, length 1, number of bytes, OR-ed with 0xf0
148  * @1, length N, data bytes (up to 7 bytes)
149  */
150  buffer[0] = size; /* YES! TX is *without* 0xf0! */
151  memcpy(&buffer[1], data, size);
152  rc = ser_hid_hidapi_set_data(serial, 2, buffer, sizeof(buffer), 0);
153  if (rc < 0)
154  return rc;
155  if (rc == 0)
156  return 0;
157  return size;
158 }
159 
160 static struct ser_hid_chip_functions chip_ch9325 = {
161  .chipname = "ch9325",
162  .chipdesc = "WCH CH9325",
163  .vid_pid_items = vid_pid_items_ch9325,
164  .max_bytes_per_request = CH9325_MAX_BYTES_PER_REQUEST,
165  .set_params = ch9325_set_params,
166  .read_bytes = ch9325_read_bytes,
167  .write_bytes = ch9325_write_bytes,
168 };
169 SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_ch9325 = &chip_ch9325;
170 
171 #else
172 
173 SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_ch9325 = NULL;
174 
175 #endif
176 #endif
Generic/unspecified error.
Definition: libsigrok.h:68
No error.
Definition: libsigrok.h:67
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 GString * sr_hexdump_new(const uint8_t *data, const size_t len)
Convert a sequence of bytes to its textual representation ("hex dump").
Definition: strutil.c:603
SR_PRIV void sr_hexdump_free(GString *s)
Free a hex dump text that was created by sr_hexdump_new().
Definition: strutil.c:623
#define SR_PRIV
Definition: libsigrok.h:128
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)