1 /*
2  * Copyright (c) 2024, sakumisu
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef USBH_CP210X_H
7 #define USBH_CP210X_H
8 
9 #include "usb_cdc.h"
10 
11 /* Requests */
12 #define CP210X_IFC_ENABLE      0x00
13 #define CP210X_SET_BAUDDIV     0x01
14 #define CP210X_GET_BAUDDIV     0x02
15 #define CP210X_SET_LINE_CTL    0x03 // Set parity, data bits, stop bits
16 #define CP210X_GET_LINE_CTL    0x04
17 #define CP210X_SET_BREAK       0x05
18 #define CP210X_IMM_CHAR        0x06
19 #define CP210X_SET_MHS         0x07 // Set DTR, RTS
20 #define CP210X_GET_MDMSTS      0x08
21 #define CP210X_SET_XON         0x09
22 #define CP210X_SET_XOFF        0x0A
23 #define CP210X_SET_EVENTMASK   0x0B
24 #define CP210X_GET_EVENTMASK   0x0C
25 #define CP210X_SET_CHAR        0x0D
26 #define CP210X_GET_CHARS       0x0E
27 #define CP210X_GET_PROPS       0x0F
28 #define CP210X_GET_COMM_STATUS 0x10
29 #define CP210X_RESET           0x11
30 #define CP210X_PURGE           0x12
31 #define CP210X_SET_FLOW        0x13
32 #define CP210X_GET_FLOW        0x14
33 #define CP210X_EMBED_EVENTS    0x15
34 #define CP210X_GET_EVENTSTATE  0x16
35 #define CP210X_SET_CHARS       0x19
36 #define CP210X_GET_BAUDRATE    0x1D
37 #define CP210X_SET_BAUDRATE    0x1E // Set baudrate
38 #define CP210X_VENDOR_SPECIFIC 0xFF
39 
40 struct usbh_cp210x {
41     struct usbh_hubport *hport;
42     struct usb_endpoint_descriptor *bulkin;  /* Bulk IN endpoint */
43     struct usb_endpoint_descriptor *bulkout; /* Bulk OUT endpoint */
44     struct usbh_urb bulkout_urb;
45     struct usbh_urb bulkin_urb;
46 
47     struct cdc_line_coding line_coding;
48 
49     uint8_t intf;
50     uint8_t minor;
51 
52     void *user_data;
53 };
54 
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58 
59 int usbh_cp210x_set_line_coding(struct usbh_cp210x *ftdi_class, struct cdc_line_coding *line_coding);
60 int usbh_cp210x_get_line_coding(struct usbh_cp210x *ftdi_class, struct cdc_line_coding *line_coding);
61 int usbh_cp210x_set_line_state(struct usbh_cp210x *ftdi_class, bool dtr, bool rts);
62 
63 int usbh_cp210x_bulk_in_transfer(struct usbh_cp210x *cp210x_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout);
64 int usbh_cp210x_bulk_out_transfer(struct usbh_cp210x *cp210x_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout);
65 
66 void usbh_cp210x_run(struct usbh_cp210x *cp210x_class);
67 void usbh_cp210x_stop(struct usbh_cp210x *cp210x_class);
68 
69 #ifdef __cplusplus
70 }
71 #endif
72 
73 #endif /* USBH_CP210X_H */
74