-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrtems-occan.c
510 lines (422 loc) · 11.9 KB
/
rtems-occan.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/* Simple OC_CAN interface test.
*
* Gaisler Research 2007,
* Daniel Hellström
*
*/
#include <rtems.h>
#define CONFIGURE_INIT
#include <bsp.h> /* for device driver prototypes */
rtems_task Init( rtems_task_argument argument); /* forward declaration needed */
/* configuration information */
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_NULL_DRIVER 1
#define CONFIGURE_MAXIMUM_TASKS 8
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
#define CONFIGURE_EXTRA_TASK_STACKS (3 * RTEMS_MINIMUM_STACK_SIZE)
#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 16
#define CONFIGURE_INIT_TASK_PRIORITY 100
/* Configure RTEMS */
#include <rtems/confdefs.h>
/* Configure Driver manager */
#if defined(RTEMS_DRVMGR_STARTUP) && defined(LEON3) /* if --drvmgr was given to configure */
/* Add Timer and UART Driver for this example */
#ifdef CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_DRIVER_AMBAPP_GAISLER_GPTIMER
#endif
#ifdef CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_DRIVER_AMBAPP_GAISLER_APBUART
#endif
#endif
#define CONFIGURE_DRIVER_AMBAPP_GAISLER_PCIF /* PCI is for GR-701 OCCAN */
#define CONFIGURE_DRIVER_AMBAPP_GAISLER_GRPCI /* PCI is for GR-701 OCCAN */
#define CONFIGURE_DRIVER_PCI_GR_701 /* GR-701 PCI TARGET has a OCCAN core */
#ifdef LEON2
/* PCI support for AT697 */
#define CONFIGURE_DRIVER_LEON2_AT697PCI
/* AMBA PnP Support for GRLIB-LEON2 */
#define CONFIGURE_DRIVER_LEON2_AMBAPP
#endif
#define CONFIGURE_DRIVER_AMBAPP_GAISLER_OCCAN /* OCCAN Driver */
#include <drvmgr/drvmgr_confdefs.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <occan.h>
#include "occan_lib.h"
/* Include driver configurations and system initialization */
#include "config.c"
#define BTR0 0x27
#define BTR1 0x3e
#define DO_FILTER_TEST
//#undef MULTI_BOARD
//#define TASK_RX
//#define TASK_TX
#ifdef MULTI_BOARD
/* Only one CAN core is used either as TX or RX */
#define OCCAN_DEVICE_TX_NAME "/dev/occan0"
#define OCCAN_DEVICE_RX_NAME "/dev/occan0"
#else
/* Loopback mode - both CAN cores are used one for RX and one for TX */
#define OCCAN_DEVICE_TX_NAME "/dev/occan0"
#define OCCAN_DEVICE_RX_NAME "/dev/occan1"
#endif
rtems_task task1(rtems_task_argument argument);
rtems_task task2(rtems_task_argument argument);
/*extern int errno;*/
rtems_id Task_id[3]; /* array of task ids */
rtems_name Task_name[3]; /* array of task names */
/* =========================================================
initialisation */
rtems_task Init(
rtems_task_argument ignored
)
{
rtems_status_code status;
/* Initialize Driver manager, in config.c */
system_init();
printf("******** Starting Gaisler OCCAN sample ********\n");
Task_name[1] = rtems_build_name( 'T', 'S', 'K', 'A' );
Task_name[2] = rtems_build_name( 'T', 'S', 'K', 'B' );
status = rtems_task_create(
Task_name[1], 1, RTEMS_MINIMUM_STACK_SIZE * 2,
RTEMS_DEFAULT_MODES,
RTEMS_DEFAULT_ATTRIBUTES, &Task_id[1]
);
status = rtems_task_create(
Task_name[2], 1, RTEMS_MINIMUM_STACK_SIZE * 2,
RTEMS_DEFAULT_MODES,
RTEMS_DEFAULT_ATTRIBUTES, &Task_id[2]
);
/* Starting receiver first */
#ifdef TASK_RX
status = rtems_task_start(Task_id[2], task2, 2);
#endif
#ifdef TASK_TX
status = rtems_task_start(Task_id[1], task1, 1);
#endif
status = rtems_task_delete(RTEMS_SELF);
}
void task1_afilter_test(occan_t chan);
void task2_afilter_test(occan_t chan);
#define SPEED_250K 250000
#define TSK1_RX_LEN 32
#define TSK1_TX_LEN 8
#define TSK2_RX_LEN 32
#define TSK2_TX_LEN 8
#define ID_GAISLER 0x2000
/* =========================================================
task1 */
rtems_task task1(
rtems_task_argument unused
)
{
occan_t chan;
CANMsg msgs[3];
int i=0,j=0,left,sent;
occan_stats stats;
printf("Starting task 1\n");
/* open device */
chan=occanlib_open(OCCAN_DEVICE_TX_NAME);
if ( !chan ){
printf("Failed to open device driver 0\n");
rtems_task_delete(RTEMS_SELF);
return;
}
#ifdef DO_FILTER_TEST
/* Do filter test */
task1_afilter_test(chan);
occanlib_stop(chan);
#endif
/* before starting set up
* ¤ Speed
* ¤ Buffer length
* ¤ no Filter
* ¤ blocking mode
*/
printf("Task1: Setting speed\n");
//occanlib_set_speed(chan,SPEED_250K);
occanlib_set_btrs(chan,BTR0,BTR1);
printf("Task1: Setting buf len\n");
occanlib_set_buf_length(chan,TSK1_RX_LEN,TSK1_TX_LEN);
/* total blocking mode */
printf("Task1: Setting blk mode\n");
occanlib_set_blocking_mode(chan,1,1);
/* Set accept all filter */
/* occanlib_set_filter(occan_t chan, struct occan_afilter *new_filter);*/
/* Start link */
printf("Task1: Starting\n");
occanlib_start(chan);
printf("Task1: Entring TX loop\n");
while(1){
/* Send 1 STD Message */
msgs[0].extended = 0;
msgs[0].rtr = 0;
msgs[0].sshot = 0;
msgs[0].id = 10+i;
msgs[0].len = 4 + (i&1);
msgs[0].data[0] = 0x2;
msgs[0].data[1] = 0xc4;
msgs[0].data[2] = 0x4f;
msgs[0].data[3] = 0xf2;
msgs[0].data[4] = 0x23;
printf("Task1: sending 1 STD msg\n");
occanlib_send(chan,&msgs[0]);
/* Send 3 EXT Message */
msgs[0].extended = 1;
msgs[0].rtr = 0;
msgs[0].sshot = 0;
msgs[0].id = 10+i;
msgs[0].len = 4 + (i&1);
msgs[0].data[0] = 0x2;
msgs[0].data[1] = 0xc4;
msgs[0].data[2] = 0x4f;
msgs[0].data[3] = 0xf2;
msgs[0].data[4] = 0x23;
msgs[1].extended = 1;
msgs[1].rtr = 0;
msgs[1].sshot = 0;
msgs[1].id = 10+i;
msgs[1].len = 8;
msgs[1].data[0] = 0xaa;
msgs[1].data[1] = 0xbb;
msgs[1].data[2] = 0x11;
msgs[1].data[3] = 0x22;
msgs[1].data[4] = 'U';
msgs[1].data[5] = 0x12;
msgs[1].data[6] = 0xff;
msgs[1].data[7] = 0x00;
msgs[2].extended = 1;
msgs[2].rtr = 0;
msgs[2].sshot = 0;
msgs[2].id = (10+i) | ID_GAISLER;
msgs[2].len = 7;
msgs[2].data[0] = 'G';
msgs[2].data[1] = 'a';
msgs[2].data[2] = 'i';
msgs[2].data[3] = 's';
msgs[2].data[4] = 'l';
msgs[2].data[5] = 'e';
msgs[2].data[6] = 'r';
/* Send 3 EXT Messages */
printf("Task1: sending 3 EXT msg\n");
left = 3;
while ( left > 0 ){
sent = occanlib_send_multiple(chan,&msgs[3-left],left);
if ( sent < 0 ){
printf("Task1: Error, aborting 3 EXT sending\n");
}else{
left -= sent;
}
}
j++;
if ( j == 10 ){
/* debug start/stop */
printf("---------------- Task1: Stopping ----------------\n");
occanlib_stop(chan);
printf("---------------- Task1: Starting again --------------\n");
occanlib_start(chan);
}
if ( (j & 0x1f) == 0x1f ){
printf("---------------- Task1: Printing Stats --------------\n");
if ( !occanlib_get_stats(chan,&stats) ){
occanlib_stats_print(&stats);
}
printf("---------------- Task1: done Printing --------------\n");
}
/* pause for 1 sec */
sleep(1);
i++;
if ( i>240 )
i=0;
}
}
/* =========================================================
task2 */
rtems_task task2(
rtems_task_argument unused
)
{
occan_t chan;
CANMsg msgs[3];
int i,cnt,msgcnt;
struct occan_afilter afilt;
printf("Starting task 2\n");
/* open device */
chan=occanlib_open(OCCAN_DEVICE_RX_NAME);
if ( !chan ){
printf("Failed to open device driver 1\n");
rtems_task_delete(RTEMS_SELF);
return;
}
#ifdef DO_FILTER_TEST
/* do filter test 2 */
task2_afilter_test(chan);
occanlib_stop(chan);
#endif
/* before starting set up
* ¤ Speed
* ¤ Buffer length
* ¤ no Filter
* ¤ blocking mode
*/
printf("Task2: Setting speed\n");
//occanlib_set_speed(chan,SPEED_250K);
occanlib_set_btrs(chan,BTR0,BTR1);
printf("Task2: Setting buf len\n");
occanlib_set_buf_length(chan,TSK2_RX_LEN,TSK2_TX_LEN);
/* total blocking mode */
printf("Task2: Setting blk mode\n");
occanlib_set_blocking_mode(chan,0,1);
/* Set filter to accept all */
afilt.single_mode = 1;
afilt.code[0] = 0x00;
afilt.code[1] = 0x00;
afilt.code[2] = 0x00;
afilt.code[3] = 0x00;
afilt.mask[0] = 0xff; /* don't care */
afilt.mask[1] = 0xff;
afilt.mask[2] = 0xff;
afilt.mask[3] = 0xff;
occanlib_set_filter(chan,&afilt);
/* Start link */
printf("Task2: Starting\n");
occanlib_start(chan);
msgcnt=0;
printf("Task2: Entering rx loop\n");
while(2){
/* blocking read */
cnt = occanlib_recv_multiple(chan,msgs,3);
if ( cnt > 0 ){
printf("Task2: Got %d messages\n",cnt);
for(i=0; i<cnt; i++){
if ( msgs[i].id & ID_GAISLER ){
printf("----- GAISLER MESSAGE -----\n");
}
print_msg(msgcnt,&msgs[i]);
if ( msgs[i].id & ID_GAISLER ){
printf("---------------------------\n");
}
msgcnt++;
}
}else if ( cnt < 0) {
printf("Task2: Experienced RX error\n");
}else{
/* if in non-blocking mode we work with other stuff here */
printf("Task2: waiting 1s\n");
sleep(1);
}
}
}
#ifdef DO_FILTER_TEST
/************* acceptance filter test *************/
void task1_afilter_test(occan_t chan){
CANMsg msgs[16];
int left, sent, i;
occan_stats stats;
printf("Task1: Setting speed\n");
occanlib_set_speed(chan,SPEED_250K);
/* total blocking mode */
printf("Task1: Setting blk mode\n");
occanlib_set_blocking_mode(chan,1,1);
printf("Task1: Setting buf len\n");
occanlib_set_buf_length(chan,TSK1_RX_LEN,TSK1_TX_LEN);
/* Build messages */
for(i=0; i<16; i++){
msgs[i].id = 0x1 << i;
msgs[i].extended = 1;
msgs[i].rtr = 0;
msgs[i].sshot = 0;
msgs[i].len = 1;
msgs[i].data[0] = i;
printf("Task1: Message %d: ID: 0x%lx\n",i,msgs[i].id);
}
/* Start */
occanlib_start(chan);
printf("Task1: sending 16 EXT msgs\n");
left = 16;
while ( left > 0 ){
sent = occanlib_send_multiple(chan,&msgs[16-left],left);
if ( sent < 0 ){
printf("Task1: Error, aborting 16 EXT sending\n");
break;
}else{
left -= sent;
}
}
printf("Task1: Exiting (%d)\n",left);
sleep(4);
printf("---------------- Task1: Printing Stats --------------\n");
if ( !occanlib_get_stats(chan,&stats) ){
occanlib_stats_print(&stats);
}
printf("---------------- Task1: done Printing --------------\n");
}
void task2_afilter_test(occan_t chan){
int tries, cnt, tot, i;
CANMsg msgs[16];
struct occan_afilter filt;
occan_stats stats;
printf("Task2: Setting speed\n");
occanlib_set_speed(chan,SPEED_250K);
/* total blocking mode */
printf("Task2: Setting non-blk mode\n");
occanlib_set_blocking_mode(chan,0,0);
printf("Task2: Setting buf len\n");
occanlib_set_buf_length(chan,TSK1_RX_LEN,TSK1_TX_LEN);
/* Set up filter so that odd messages is filtered out
* It can be done with a single filter.
*
* Odd Messages: ID = 1<<i (i=odd) 0xaaaaaaa
* Even: ID = 1<<i (i=even 0,2,4) 0x55555
*
* All odd bits must be zero:
*
* Mask = 0x aaaa aaaa
* Code = 0x0
*
*/
filt.single_mode = 1;
filt.code[0] = 0;
filt.code[1] = 0;
filt.code[2] = 0;
filt.code[3] = 0;
filt.mask[0] = 0xaa;
filt.mask[1] = 0xaa;
filt.mask[2] = 0xaa;
filt.mask[3] = 0xaf;
occanlib_set_filter(chan,&filt);
/* Start */
occanlib_start(chan);
tot=tries=0;
while(tries < 5){
/* Read 1 message */
cnt = occanlib_recv_multiple(chan,msgs,16);
printf("Task2: Got %d Message(s)\n",cnt);
if ( cnt > 0 ){
for(i=0; i<cnt; i++){
if ( msgs[i].data[0] & 1 ){
printf("Task2: ERROR! GOT ODD MESSAGE\n");
}
printf("Task2: MSG %d ID: 0x%lx, Data[0]: %d(0x%x)\n",tot,msgs[i].id,msgs[i].data[0],msgs[i].data[0]);
tot++;
}
}
tries++;
sleep(1);
}
if ( tot != 8 ){
printf("Task2: Total count is wrong: %d, tries: %d\n",tot,tries);
}
printf("Task2: Exiting filtering test\n");
printf("---------------- Task2: Printing Stats --------------\n");
if ( !occanlib_get_stats(chan,&stats) ){
occanlib_stats_print(&stats);
}
printf("---------------- Task2: done Printing --------------\n");
}
#endif