-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathppro_protocol.c
265 lines (221 loc) · 7.89 KB
/
ppro_protocol.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* usb_pcan_protocol.c
*
* Created on: 24.07.2015
* Author: hd
*/
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include <libopencm3/usb/usbd.h>
#include "systime.h"
#include "external/pcan_usbpro_fw.h"
#include "external/pcan_usbpro_sizeof_rec.h"
#include "can.h"
#include "ppro_protocol.h"
#include "ppro_usb.h"
typedef struct {
uint16_t record_count;
uint16_t message_counter;
union pcan_usbpro_rec_t first_record;
} candle_usb_packet_t;
typedef struct {
struct {
uint16_t record_count;
uint16_t message_counter;
uint8_t data[120];
} output_buffer;
unsigned output_buffer_pos;
uint32_t bits_transferred;
} channel_data_t;
static usbd_device *dev = 0;
static channel_data_t channel_data[2];
void ppro_usb_protocol_init(usbd_device *usbd_dev) {
dev = usbd_dev;
memset(channel_data, 0, sizeof(channel_data));
}
static int ppro_usb_flush(uint8_t ep) {
assert((ep==1)||(ep==2));
channel_data_t *data = &channel_data[ep-1];
if (data->output_buffer.record_count > 0) {
int bytes_written = usbd_ep_write_packet(dev, 0x80|ep, &data->output_buffer, 4+data->output_buffer_pos);
if (bytes_written != 0) {
data->output_buffer.record_count = 0;
data->output_buffer_pos = 0;
data->output_buffer.message_counter++;
return bytes_written;
}
}
return 0;
}
void ppro_usb_flush_all(void) {
ppro_usb_flush(1);
ppro_usb_flush(2);
}
static int ppro_usb_buffer_space_left(channel_data_t *data) {
return sizeof(data->output_buffer.data) - data->output_buffer_pos;
}
static int ppro_usb_enqueue_record(uint8_t ep, union pcan_usbpro_rec_t *record) {
assert((ep==1)||(ep==2));
channel_data_t *data = &channel_data[ep-1];
int space_left = ppro_usb_buffer_space_left(data);
int record_length = pcan_usbpro_sizeof_rec(record->data_type);
assert(record_length>0); // record_length<=0 means that we don't know the record type. but we should know all messages that we are sending!
if (space_left < record_length) {
// buffer full? try to send now.
if (ppro_usb_flush(ep)>0) {
space_left = ppro_usb_buffer_space_left(data);
}
}
if (space_left >= record_length) {
data->output_buffer.record_count++;
memcpy(&data->output_buffer.data[data->output_buffer_pos], record, record_length);
data->output_buffer_pos += record_length;
return 0;
} else {
return -1;
}
}
static void ppro_set_bitrate(uint8_t channel, uint32_t ccbt) {
//uint8_t tripple_sample_mode = ccbt>>23 & 0x01;
uint8_t tseg2 = ((ccbt >> 20) & 0x07) + 1;
uint8_t tseg1 = ((ccbt >> 16) & 0x0F) + 1;
uint8_t sjw = ((ccbt >> 24) & 0x03) + 1;
uint16_t brp_ppro = (ccbt & 0x3fff) + 1;
uint16_t brp_stm = (24 * brp_ppro) / 56;
candle_can_set_bitrate(channel, brp_stm, tseg1, tseg2, sjw);
}
static void ppro_tx_message(uint8_t channel, uint32_t id_and_flags, uint8_t dlc, void *data) {
can_message_t msg;
assert((channel==0)||(channel==1));
if (dlc>8) { dlc = 8; }
msg.channel = channel;
msg.dlc = dlc;
msg.timestamp = 0;
msg.id_and_flags = id_and_flags;
memcpy(msg.data, data, dlc);
channel_data[channel].bits_transferred += candle_can_calc_message_len(&msg);
candle_can_send_message(&msg);
}
static void ppro_tx(union pcan_usbpro_rec_t *request) {
uint32_t id = request->canmsg_tx.id;
if (request->canmsg_tx.flags & 0x01) { id |= can_flag_rtr; }
if (request->canmsg_tx.flags & 0x02) { id |= can_flag_extid; }
ppro_tx_message(request->canmsg_tx.channel, id, request->canmsg_tx.dlc, request->canmsg_tx.data);
}
static void ppro_set_timestamp_mode(uint16_t timestamp_mode) {
(void)timestamp_mode;
ppro_status.timestamp_active = (timestamp_mode==1);
ppro_status.t_next_timestamp = 0;
}
static uint32_t ppro_get_device_id(uint8_t channel) {
// TODO implement device id handling
return channel;
}
static uint16_t ppro_get_error_status(uint8_t channel) {
// TODO implement me
(void)channel;
return 0;
}
static void ppro_request_busload(uint8_t channel, uint8_t mode, uint16_t prescaler, uint16_t quanta) {
(void)prescaler;
(void)quanta;
ppro_status.busload_mode[channel] = mode;
}
void ppro_rx_message(const can_message_t *msg) {
union pcan_usbpro_rec_t record;
record.canmsg_rx.data_type = DATA_TYPE_USB2CAN_STRUCT_CANMSG_RX_8;
record.canmsg_rx.client = 0;
record.canmsg_rx.timestamp32 = msg->timestamp;
record.canmsg_rx.channel = msg->channel;
record.canmsg_rx.id = msg->id_and_flags & 0x1FFFFFFF;
record.canmsg_rx.flags = msg->id_and_flags >> 30;
record.canmsg_rx.dlc = msg->dlc;
memcpy(record.canmsg_rx.data, msg->data, 8);
ppro_usb_enqueue_record(2, &record);
if (msg->channel<2) {
channel_data[msg->channel].bits_transferred += candle_can_calc_message_len(msg);
}
}
void ppro_usb_send_timestamp(uint8_t ep) {
union pcan_usbpro_rec_t record;
record.calibration_ts_rx.data_type = DATA_TYPE_USB2CAN_STRUCT_CALIBRATION_TIMESTAMP_RX;
record.calibration_ts_rx.dummy[0] = 0;
record.calibration_ts_rx.dummy[1] = 0;
record.calibration_ts_rx.dummy[2] = 0;
record.calibration_ts_rx.timestamp64[0] = 0;
record.calibration_ts_rx.timestamp64[1] = get_time_us64();
ppro_usb_enqueue_record(ep, &record);
}
void ppro_usb_send_busload(uint8_t ep, uint8_t channel) {
assert((channel==0)||(channel==1));
union pcan_usbpro_rec_t record;
record.buslast_rx.data_type = DATA_TYPE_USB2CAN_STRUCT_BUSLAST_RX;
record.buslast_rx.channel = channel;
record.buslast_rx.buslast_val = channel_data[0].bits_transferred;
channel_data[0].bits_transferred = 0;
record.buslast_rx.timestamp32 = get_time_us32();
ppro_usb_enqueue_record(ep, &record);
}
void ppro_usb_protocol_handle_data(uint8_t ep, uint8_t *buf, int len) {
union pcan_usbpro_rec_t record;
if (len>=4) {
candle_usb_packet_t *packet = (candle_usb_packet_t*)buf;
int pos = 4;
int num_records = 0;
while ((++num_records <= packet->record_count) && (pos<len)) {
union pcan_usbpro_rec_t *request = (union pcan_usbpro_rec_t*) &buf[pos];
switch (request->data_type) {
case DATA_TYPE_USB2CAN_STRUCT_FKT_SET_CANLED:
candle_can_set_led_mode(request->set_can_led.channel, request->set_can_led.mode, request->set_can_led.timeout);
break;
case DATA_TYPE_USB2CAN_STRUCT_FKT_SETCANBUSACTIVATE:
candle_can_set_bus_active(request->bus_activity.channel, request->bus_activity.onoff);
break;
case DATA_TYPE_USB2CAN_STRUCT_FKT_GETDEVICENR:
record.dev_nr.data_type = DATA_TYPE_USB2CAN_STRUCT_FKT_GETDEVICENR;
record.dev_nr.channel = request->dev_nr.channel;
record.dev_nr.serial_num = ppro_get_device_id(request->dev_nr.channel);
ppro_usb_enqueue_record(ep, &record);
break;
case DATA_TYPE_USB2CAN_STRUCT_FKT_SETBAUDRATE:
ppro_set_bitrate(request->baudrate.channel, request->baudrate.CCBT);
break;
case DATA_TYPE_USB2CAN_STRUCT_FKT_SETSILENTMODE:
candle_can_set_silent(request->silent_mode.channel, request->silent_mode.onoff);
break;
case DATA_TYPE_USB2CAN_STRUCT_FKT_SETFILTERMODE:
// ignored
//ppro_set_filter_mode(request->filer_mode.filter_mode);
break;
case DATA_TYPE_USB2CAN_STRUCT_FKT_SETGET_CALIBRATION_MSG:
ppro_set_timestamp_mode(request->calibration.mode);
ppro_usb_send_timestamp(2);
break;
case DATA_TYPE_USB2CAN_STRUCT_CANMSG_TX_0:
case DATA_TYPE_USB2CAN_STRUCT_CANMSG_TX_4:
case DATA_TYPE_USB2CAN_STRUCT_CANMSG_TX_8:
ppro_tx(request);
break;
case DATA_TYPE_USB2CAN_STRUCT_FKT_GETCANBUS_ERROR_STATUS:
record.error_status.data_type = DATA_TYPE_USB2CAN_STRUCT_FKT_GETCANBUS_ERROR_STATUS;
record.error_status.channel = request->error_status.channel;
record.error_status.status = ppro_get_error_status(request->error_status.channel);
ppro_usb_enqueue_record(ep, &record);
break;
case DATA_TYPE_USB2CAN_STRUCT_FKT_SETGET_BUSLAST_MSG:
ppro_request_busload(
request->buslast.channel,
request->buslast.mode,
request->buslast.prescaler,
request->buslast.sampletimequanta
);
break;
default:
// unknown request type
break;
}
pos += pcan_usbpro_sizeof_rec(request->data_type);
}
}
}