1 /*
2  * File:   usb.h
3  * Author: karl
4  */
5 
6 #ifndef STLINK_USB_H
7 #define STLINK_USB_H
8 
9 #include <stdbool.h>
10 
11 #include <stlink.h>
12 #include <libusb_settings.h>
13 #include "logging.h"
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 #define STLINK_USB_VID_ST                   0x0483
20 #define STLINK_USB_PID_STLINK               0x3744
21 #define STLINK_USB_PID_STLINK_32L           0x3748
22 #define STLINK_USB_PID_STLINK_32L_AUDIO     0x374a
23 #define STLINK_USB_PID_STLINK_NUCLEO        0x374b
24 #define STLINK_USB_PID_STLINK_V2_1          0x3752
25 #define STLINK_USB_PID_STLINK_V3_USBLOADER  0x374d
26 #define STLINK_USB_PID_STLINK_V3E_PID       0x374e
27 #define STLINK_USB_PID_STLINK_V3S_PID       0x374f
28 #define STLINK_USB_PID_STLINK_V3_2VCP_PID   0x3753
29 
30 #define STLINK_V1_USB_PID(pid) ((pid) == STLINK_USB_PID_STLINK)
31 
32 #define STLINK_V2_USB_PID(pid) ((pid) == STLINK_USB_PID_STLINK_32L || \
33                                 (pid) == STLINK_USB_PID_STLINK_32L_AUDIO || \
34                                 (pid) == STLINK_USB_PID_STLINK_NUCLEO)
35 
36 #define STLINK_V2_1_USB_PID(pid) ((pid) == STLINK_USB_PID_STLINK_V2_1)
37 
38 #define STLINK_V3_USB_PID(pid) ((pid) == STLINK_USB_PID_STLINK_V3_USBLOADER || \
39                                 (pid) == STLINK_USB_PID_STLINK_V3E_PID || \
40                                 (pid) == STLINK_USB_PID_STLINK_V3S_PID || \
41                                 (pid) == STLINK_USB_PID_STLINK_V3_2VCP_PID)
42 
43 #define STLINK_SUPPORTED_USB_PID(pid) (STLINK_V1_USB_PID(pid) || \
44                                        STLINK_V2_USB_PID(pid) || \
45                                        STLINK_V2_1_USB_PID(pid) || \
46                                        STLINK_V3_USB_PID(pid))
47 
48 #define STLINK_SG_SIZE 31
49 #define STLINK_CMD_SIZE 16
50 
51 struct stlink_libusb {
52     libusb_context* libusb_ctx;
53     libusb_device_handle* usb_handle;
54     unsigned int ep_req;
55     unsigned int ep_rep;
56     unsigned int ep_trace;
57     int protocoll;
58     unsigned int sg_transfer_idx;
59     unsigned int cmd_len;
60 };
61 
62 /**
63  * Open a stlink
64  * @param verbose Verbosity loglevel
65  * @param connect Type of connect to target
66  * @param serial  Serial number to search for, when NULL the first stlink found is opened (binary format)
67  * @retval NULL   Error while opening the stlink
68  * @retval !NULL  Stlink found and ready to use
69  */
70 stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect, char serial[STLINK_SERIAL_BUFFER_SIZE], int freq);
71 size_t stlink_probe_usb(stlink_t **stdevs[], enum connect_type connect, int freq);
72 void stlink_probe_usb_free(stlink_t **stdevs[], size_t size);
73 
74 #ifdef __cplusplus
75 }
76 #endif
77 
78 #endif // STLINK_USB_H
79