-
Notifications
You must be signed in to change notification settings - Fork 16
/
ppro_usb.c
238 lines (186 loc) · 6.1 KB
/
ppro_usb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* usb_pcan.c
*
* Created on: 24.07.2015
* Author: hd
*/
#include "ppro_usb.h"
#include <string.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/usb/usbd.h>
#include "systime.h"
#include "external/pcan_usbpro_fw.h"
#include "ppro_protocol.h"
#include "ppro_usb_descr.h"
ppro_status_t ppro_status;
static usbd_device *usbdev;
static uint8_t usbd_control_buffer[512];
static struct pcan_usbpro_bootloader_info_t bootloader_info = {
.ctrl_type = 0x11112222,
.version = {1, 8, 3, 0},
.day = 26,
.month = 5,
.year = 9,
.dummy = 0,
.serial_num_high = 0xFFFFFFFF,
.serial_num_low = 0x00000001,
.hw_type = 0x00000100,
.hw_rev = 0x00000000
};
static struct pcan_usbpro_ext_firmware_info_t firmware_info = {
.ctrl_type = 0x11223344,
.version = {1, 3, 3, 0 },
.day = 18,
.month = 6,
.year = 10,
.dummy = 0x40,
.fw_type = 0x00000100
};
static struct pcan_usbpro_uc_chipid_t uc_chip_id = {
.ctrl_type = 0, // TODO find valid values
.chip_id = 0
};
static struct pcan_usbpro_usb_chipid_t usb_chip_id = {
.ctrl_type = 0, // TODO find valid values
.chip_id = 0
};
static struct pcan_usbpro_device_nr_t device_nr = {
.ctrl_type = 0x3738393a,
.device_nr = 1
};
static struct pcan_usbpro_cpld_info_t cpld_info = {
.ctrl_type = 0x1a1a2277,
.cpld_nr = 7
};
static struct pcan_usbpro_info_mode_t info_mode = {
.ctrl_type = 0, // TODO find valid values
.mode = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
.flags = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }
};
static struct pcan_usbpro_time_mode_t time_mode = {
.ctrl_type = 0,
.time_mode = 0,
.flags = 0
};
static void ppro_usb_rx_handler(usbd_device *usbd_dev, uint8_t ep)
{
uint8_t buf[128];
int len = usbd_ep_read_packet(usbd_dev, ep, buf, sizeof(buf));
ppro_usb_protocol_handle_data(ep, buf, len);
}
static int ppro_usb_control_request(usbd_device *usbd_dev,
struct usb_setup_data *req, uint8_t **buf, uint16_t *len,
void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req))
{
(void)complete;
(void)buf;
(void)usbd_dev;
static uint8_t buffer[128];
if (req->bRequest == 0) { // get information
switch (req->wValue) {
case USB_VENDOR_REQUEST_wVALUE_INFO_BOOTLOADER:
memcpy(*buf, &bootloader_info, sizeof(bootloader_info));
*len = sizeof(bootloader_info);
break;
case USB_VENDOR_REQUEST_wVALUE_INFO_FIRMWARE:
memcpy(*buf, &firmware_info, sizeof(firmware_info));
*len = sizeof(firmware_info);
break;
case USB_VENDOR_REQUEST_wVALUE_INFO_uC_CHIPID:
memcpy(*buf, &uc_chip_id, sizeof(uc_chip_id));
*len = sizeof(uc_chip_id);
break;
case USB_VENDOR_REQUEST_wVALUE_INFO_USB_CHIPID:
memcpy(*buf, &usb_chip_id, sizeof(usb_chip_id));
*len = sizeof(usb_chip_id);
break;
case USB_VENDOR_REQUEST_wVALUE_INFO_DEVICENR:
memcpy(*buf, &device_nr, sizeof(device_nr));
*len = sizeof(device_nr);
break;
case USB_VENDOR_REQUEST_wVALUE_INFO_CPLD:
memcpy(*buf, &cpld_info, sizeof(cpld_info));
*len = sizeof(cpld_info);
break;
case USB_VENDOR_REQUEST_wVALUE_INFO_MODE:
memcpy(*buf, &info_mode, sizeof(info_mode));
*len = sizeof(info_mode);
break;
case USB_VENDOR_REQUEST_wVALUE_INFO_TIMEMODE:
memcpy(*buf, &time_mode, sizeof(time_mode));
*len = sizeof(time_mode);
break;
}
return 1;
} else if (req->bRequest == 2) { // config ?
if (req->wValue==USB_VENDOR_REQUEST_wVALUE_SETFKT_INTERFACE_DRIVER_LOADED) {
usbd_ep_read_packet(usbd_dev, 0, buffer, 16);
// data is 16 bytes, of which only 2 are used:
// data[0] == driver (CAN=0, LIN=1)
// data[1] == driver loaded (0/1)
return USBD_REQ_HANDLED;
}
}
return 0;
}
static void ppro_usb_set_config(usbd_device *usbd_dev, uint16_t wValue)
{
(void)wValue;
usbd_register_control_callback(usbd_dev, 0x43, 0x7F, ppro_usb_control_request);
usbd_ep_setup(usbd_dev, 0x01, USB_ENDPOINT_ATTR_BULK, 128, ppro_usb_rx_handler);
usbd_ep_setup(usbd_dev, 0x81, USB_ENDPOINT_ATTR_BULK, 128, NULL);
usbd_ep_setup(usbd_dev, 0x02, USB_ENDPOINT_ATTR_BULK, 128, ppro_usb_rx_handler);
usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK, 128, NULL);
usbd_ep_setup(usbd_dev, 0x03, USB_ENDPOINT_ATTR_BULK, 128, ppro_usb_rx_handler);
usbd_ep_setup(usbd_dev, 0x83, USB_ENDPOINT_ATTR_BULK, 128, NULL);
#ifdef USE_USB_HS
usbd_ep_setup(usbd_dev, 0x04, USB_ENDPOINT_ATTR_BULK, 128, ppro_usb_rx_handler);
usbd_ep_setup(usbd_dev, 0x84, USB_ENDPOINT_ATTR_BULK, 128, candle_usb_tx_handler);
usbd_ep_setup(usbd_dev, 0x05, USB_ENDPOINT_ATTR_BULK, 128, ppro_usb_rx_handler);
usbd_ep_setup(usbd_dev, 0x85, USB_ENDPOINT_ATTR_BULK, 128, candle_usb_tx_handler);
#endif
//candle_usb_send_timestamp(usbd_dev, 2);
}
void ppro_usb_init(void) {
memset(&ppro_status, 0, sizeof(ppro_status));
#ifdef USE_USB_HS
rcc_periph_clock_enable(RCC_GPIOB);
rcc_periph_clock_enable(RCC_OTGHS);
gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO13 | GPIO14 | GPIO15);
gpio_set_af(GPIOB, GPIO_AF12, GPIO13 | GPIO14 | GPIO15);
usbdev = usbd_init(&otghs_usb_driver, &device_descriptor, &config,
usb_strings, 10,
usbd_control_buffer, sizeof(usbd_control_buffer));
#else
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_OTGFS);
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9 | GPIO11 | GPIO12);
gpio_set_af(GPIOA, GPIO_AF10, GPIO9 | GPIO11 | GPIO12);
usbdev = usbd_init(&otgfs_usb_driver, &device_descriptor, &config_descriptor,
usb_strings, 10,
usbd_control_buffer, sizeof(usbd_control_buffer));
#ifdef DISABLE_VBUS_SENSE
// Disable VBUS sense
#include <libopencm3/stm32/otg_fs.h>
OTG_FS_GCCFG |= OTG_GCCFG_NOVBUSSENS;
#endif
#endif
ppro_usb_protocol_init(usbdev);
usbd_register_set_config_callback(usbdev, ppro_usb_set_config);
}
void ppro_usb_poll(void) {
usbd_poll(usbdev);
ppro_usb_flush_all();
uint32_t now = get_time_ms();
if (ppro_status.timestamp_active && (ppro_status.t_next_timestamp <= now)) {
ppro_usb_send_timestamp(2);
ppro_status.t_next_timestamp = now + 1000;
}
for (uint8_t i=0; i<2; i++) {
if (ppro_status.busload_mode[i] && (ppro_status.t_next_busload[i] <= now)) {
ppro_usb_send_busload(2, i);
ppro_status.t_next_busload[i] = now + 8;
}
}
}