-
Notifications
You must be signed in to change notification settings - Fork 6
/
rtems-spwtest-2boards.c
514 lines (453 loc) · 17.6 KB
/
rtems-spwtest-2boards.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
511
512
513
514
/* Simple SpaceWire interface test for 2 boards.
* When using two boards define MULTI_BOARD and
* one of TASK_RX or TASK_TX.
*
* The main SpaceWire example for oe board is rtems-spacewire.
*
* Gaisler Research 2007,
* Daniel Hellström
*
*/
#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_NULL_DRIVER 1
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#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 32
#define CONFIGURE_INIT_TASK_PRIORITY 100
#define CONFIGURE_MAXIMUM_DRIVERS 16
/* #define CONFIGURE_INIT_TASK_INITIAL_MODES (RTEMS_PREEMPT | \ */
/* RTEMS_TIMESLICE | \ */
/* RTEMS_ASR | \ */
/* RTEMS_INTERRUPT_LEVEL(0)) */
#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 RASTA-IO and GR-701 GRSPW */
#define CONFIGURE_DRIVER_AMBAPP_GAISLER_GRPCI /* PCI is for RASTA-IO and GR-701 GRSPW */
#define CONFIGURE_DRIVER_PCI_GR_RASTA_IO /* GR-RASTA-IO has three GRSPW SpaceWire cores */
#define CONFIGURE_DRIVER_PCI_GR_701 /* GR-701 has three GRSPW SpaceWire cores */
#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_GRSPW /* GRSPW Driver */
#include <drvmgr/drvmgr_confdefs.h>
#include <rtems.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#undef ENABLE_NETWORK
/* Include driver configurations and system initialization */
#include "config.c"
#include <grspw.h>
/* Select GRSPW core to be used in sample application.
* - /dev/grspw0 (First ON-CHIP core)
* - /dev/grspw1 (Second ON-CHIP core)
* - /dev/rastaio0/grspw0 (The GRSPW core on first GR-RASTA-IO board)
* - /dev/rastaio0/grspw1 (The second GRSPW core on first GR-RASTA-IO board)
* - /dev/gr701_0/grspw0 (The first GRSPW core on first GR-701 board)
* - /dev/gr701_1/grspw0 (The first GRSPW core on second GR-701 board)
*/
/*#define GRSPW_DEVICE_NAME1 "/dev/grspw0"
#define GRSPW_DEVICE_NAME2 "/dev/grspw1"*/
#define GRSPW_DEVICE_NAME1 "/dev/rastaio0/grspw0"
#define GRSPW_DEVICE_NAME2 "/dev/rastaio0/grspw1"
rtems_task task1(rtems_task_argument argument);
rtems_task task2(rtems_task_argument argument);
void print_config(spw_config *cnf);
void print_statistics(spw_stats *stats);
void check_init_config(spw_config *cnf);
char *link_status(int status);
extern int errno;
char *lstates[6] = {"Error-reset",
"Error-wait",
"Ready",
"Started",
"Connecting",
"Run"
};
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
)
{
FILE *f; int fd;
rtems_status_code status;
printf("******** Starting RTEMS Spacewire test 2 ********\n");
system_init();
Task_name[1] = rtems_build_name( 'S', 'E', 'N', 'D' );
Task_name[2] = rtems_build_name( 'R', 'E', 'C', 'V' );
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]
);
#ifdef TASK_TX
status = rtems_task_start(Task_id[1], task1, 1);
#endif
#ifdef TASK_RX
status = rtems_task_start(Task_id[2], task2, 2);
#endif
status = rtems_task_delete(RTEMS_SELF);
}
unsigned int init1 = 0, init2 = 0;
static char tx_pkt[4096];
static char tx_hpkt[256];
#define NODE_ADR_TX 1
#define NODE_ADR_RX 2
#ifdef TASK_TX
/* =========================================================
sender task */
rtems_task task1(
rtems_task_argument unused
)
{
rtems_status_code status;
rtems_event_set out;
int i;
int j;
int k;
int fd;
int lstatus;
int stat;
int ret;
spw_ioctl_pkt_send *pkt = (spw_ioctl_pkt_send *)malloc(sizeof(spw_ioctl_pkt_send));
spw_stats *statistics = (spw_stats *)malloc(sizeof(spw_stats));
spw_config *config = (spw_config *)malloc(sizeof(spw_config));
spw_ioctl_packetsize *ps = (spw_ioctl_packetsize *)malloc(sizeof(spw_ioctl_packetsize));
printf("**** Transmit test task started ****\n");
printf("\n");
printf("Opening " GRSPW_DEVICE_NAME1 ": ");
j=0;
fd = open(GRSPW_DEVICE_NAME1, O_RDWR);
if ( fd < 0 ){
printf("Failed to open " GRSPW_DEVICE_NAME1 " (%d)\n",errno);
exit(0);
}
printf("opened successfully\n");
printf("**** Checking initial configuration for " GRSPW_DEVICE_NAME1 " ****\n");
printf("\n");
if (ioctl(fd, SPACEWIRE_IOCTRL_GET_CONFIG, config) == -1) {
printf("ioctl failed: SPACEWIRE_IOCTRL_GET_CONFIG \n");
exit(0);
}
print_config(config);
if (ioctl(fd, SPACEWIRE_IOCTRL_SET_NODEADDR, NODE_ADR_TX) == -1) {
printf("ioctl failed: SPACEWIRE_IOCTRL_SET_NODEADDR \n");
exit(0);
}
if (ioctl(fd, SPACEWIRE_IOCTRL_GET_CONFIG, config) == -1) {
printf("ioctl failed: SPACEWIRE_IOCTRL_GET_CONFIG\n");
exit(0);
}
if (ioctl(fd, SPACEWIRE_IOCTRL_SET_TXBLOCK, 1) == -1) {
printf("ioctl failed: SPACEWIRE_IOCTRL_SET_TXBLOCK\n");
exit(0);
}
printf("Trying to bring link up\n");
while(1){
if (ioctl(fd, SPACEWIRE_IOCTRL_START, 0) == -1) {
sched_yield(); /*printf("ioctl failed: SPACEWIRE_IOCTRL_START\n");*/
}else
break;
}
printf("Link is up\n");
printf("Disabling transmitter and receiver\n");
if ( ioctl(fd, SPACEWIRE_IOCTRL_STOP, 0) == -1 ){
printf("ioctl failed: SPACEWIRE_IOCTRL_STOP (%d)\n",errno);
exit(0);
}
printf("Trying to bring link up again\n");
while(1){
if (ioctl(fd, SPACEWIRE_IOCTRL_START, 0) == -1) {
sched_yield(); /*printf("ioctl failed: SPACEWIRE_IOCTRL_START\n");*/
}else
break;
}
printf("Link is up\n");
if (ioctl(fd, SPACEWIRE_IOCTRL_SET_CLKDIV, 0) == -1) {
printf("ioctl failed: SPACEWIRE_IOCTRL_SET_CLKDIV\n");
exit(0);
}
print_config(config);
if (ioctl(fd, SPACEWIRE_IOCTRL_GET_LINK_STATUS, &(lstatus)) == -1) {
printf("ioctl failed: SPACEWIRE_IOCTRL_GET_LINK_STATUS\n");
exit(0);
}
printf("Spacewire link 1 is in %s state\n", link_status(lstatus));
/* printf("Set link error event id and link error irq\n"); */
/* printf("Task Id 2: %i \n", Task_id[1]); */
/* ioctl(fd, SPACEWIRE_IOCTRL_SET_EVENT_ID, Task_id[1]); */
/* ioctl(fd, SPACEWIRE_IOCTRL_SET_LINK_ERR_IRQ, 1); */
/* rtems_event_receive(RTEMS_ALL_EVENTS, RTEMS_WAIT | RTEMS_EVENT_ANY, RTEMS_NO_TIMEOUT, &out); */
/* printf("Got event 1\n"); */
/* ioctl(fd, SPACEWIRE_IOCTRL_SET_DISABLE_ERR, 1); */
/* rtems_event_receive(RTEMS_ALL_EVENTS, RTEMS_WAIT | RTEMS_EVENT_ANY, RTEMS_NO_TIMEOUT, &out); */
/* printf("Got event 2\n"); */
/* ioctl(fd, SPACEWIRE_IOCTRL_SET_LINK_ERR_IRQ, 0); */
/* ioctl(fd, SPACEWIRE_IOCTRL_LINKSTART, 1); */
/* printf("Link error test finished\n"); */
/*config print finished, allow task 2 to print its config*/
i=0;
while(1){
printf("-------------- Sending Initiator ----------------\n");
tx_pkt[0] = NODE_ADR_RX;
tx_pkt[1] = 'G';
tx_pkt[2] = 'A';
tx_pkt[3] = 'I';
tx_pkt[4] = 'S';
tx_pkt[5] = 'L';
tx_pkt[6] = 'E';
tx_pkt[7] = 'R';
tx_pkt[8] = ' ';
tx_pkt[9] = 'S';
tx_pkt[10] = 'P';
tx_pkt[11] = 'W';
tx_pkt[12] = '0'+i;
tx_pkt[13] = 0;
/* Send packet */
if ( (ret=write(fd, tx_pkt, 14)) <= 0 ){
printf("Write failed, errno: %d, ret: %d\n",errno,ret);
}
/* Sleep 5s */
printf("Sleeping 1s\n");
sleep(1);
i++;
if ( i>9 )
i=0;
}
}
#endif
#ifdef TASK_RX
/* =========================================================
receiver task */
rtems_task task2(
rtems_task_argument unused
)
{
rtems_status_code status;
spw_config *config;
spw_stats *statistics = (spw_stats *)malloc(sizeof(spw_stats));
char *rx_pkt;
char *cx_pkt;
char *cx_hpkt;
int fd;
rtems_event_set event0, event1;
int len;
int i;
int j;
int k;
int st;
int lstatus;
int rd;
int tmp;
rx_pkt = (char *)malloc(4096);
cx_pkt = (char *)malloc(4096);
cx_hpkt = (char *)malloc(256);
config = (spw_config *)malloc(sizeof(spw_config));
spw_ioctl_packetsize *ps = (spw_ioctl_packetsize *)malloc(sizeof(spw_ioctl_packetsize));
printf(" **** Receive test task started ****\n ");
printf("Opening " GRSPW_DEVICE_NAME2 ": ");
j = 0;
errno=0;
if ((fd = open(GRSPW_DEVICE_NAME2, O_RDONLY)) < 0 ){
printf("Failed to open " GRSPW_DEVICE_NAME2 ", errno: %d, ret: %d\n",errno,fd);
exit(0);
}
printf("opened " GRSPW_DEVICE_NAME2 " successfully\n");
/* Make driver use the default timings as given by HW when reset */
#if 0
/* make driver calculate timings from system clock */
if ( ioctl(fd,SPACEWIRE_IOCTRL_SET_COREFREQ,0) == -1 ){
printf("ioctl failed: SPACEWIRE_IOCTRL_SET_COREFREQ, errno: %d\n",errno);
exit(0);
}
/* make driver calculate timings from 40MHz spacewire clock */
if ( ioctl(fd,SPACEWIRE_IOCTRL_SET_COREFREQ,40000) == -1 ){
printf("ioctl failed: SPACEWIRE_IOCTRL_SET_COREFREQ, errno: %d\n",errno);
exit(0);
}
#endif
if (ioctl(fd, SPACEWIRE_IOCTRL_GET_CONFIG, config) == -1) {
printf("ioctl failed: SPACEWIRE_IOCTRL_GET_CONFIG\n");
exit(0);
}
printf(" Checking initial configuration for Spacewire 2 ****\n");
/* check_init_config(config);*/
print_config(config);
if (ioctl(fd, SPACEWIRE_IOCTRL_SET_NODEADDR, NODE_ADR_RX) == -1) {
printf("ioctl failed: SPACEWIRE_IOCTRL_SET_NODEADDR\n");
exit(0);
}
if (ioctl(fd, SPACEWIRE_IOCTRL_SET_RXBLOCK, 1) == -1) {
printf("ioctl failed: SPACEWIRE_IOCTRL_SET_RXBLOCK\n");
exit(0);
}
/* bring link up */
printf("Trying to bring link up\n");
while(1){
if (ioctl(fd, SPACEWIRE_IOCTRL_START, 0) == -1) {
sched_yield(); /*printf("ioctl failed: SPACEWIRE_IOCTRL_START\n");*/
}else
break;
}
printf("Link is up, Changing speed\n");
if (ioctl(fd, SPACEWIRE_IOCTRL_SET_CLKDIV, 0) == -1) {
printf("ioctl failed: SPACEWIRE_IOCTRL_SET_CLKDIV\n");
exit(0);
}
if (ioctl(fd, SPACEWIRE_IOCTRL_GET_CONFIG, config) == -1) {
printf("ioctl failed: SPACEWIRE_IOCTRL_GET_CONFIG\n");
exit(0);
}
print_config(config);
if (ioctl(fd, SPACEWIRE_IOCTRL_GET_LINK_STATUS, &(lstatus)) == -1) {
printf("ioctl failed: SPACEWIRE_IOCTRL_GET_LINK_STATUS\n");
exit(0);
}
printf("Spacewire link 2 is in %s state\n", link_status(lstatus));
while(1){
printf("-------------- Receiving Initiator ----------------\n");
tmp = 13;
len = read(fd, rx_pkt, tmp);
if ( len <= 0 ){
printf("Read Failed, errno: %d, ret: %d\n",errno,len);
sleep(1);
continue;
}
/* handle message */
printf("Got Message of length %d, to address %d\n",len,rx_pkt[0]);
printf("Message data: [ 0x%x 0x%x ...] (%s)\n",rx_pkt[1],rx_pkt[2],&rx_pkt[0]);
}
close(fd);
}
#endif
/* =========================================================
event task */
void print_config(spw_config *cnf)
{
printf("\n");
printf(" ******** CONFIG ******** \n");
printf("Node Address: %i\n", cnf->nodeaddr);
printf("Destination Key: %i\n", cnf->destkey);
printf("Clock Divider: %i\n", cnf->clkdiv);
printf("Rx Maximum Packet: %i\n", cnf->rxmaxlen);
printf("Timer: %i\n", cnf->timer);
printf("Linkdisabled: %i\n", cnf->linkdisabled);
printf("Linkstart: %i\n", cnf->linkstart);
printf("Disconnect: %i\n", cnf->disconnect);
printf("Promiscuous: %i\n", cnf->promiscuous);
printf("RMAP Enable: %i\n", cnf->rmapen);
printf("RMAP Buffer Disable: %i\n", cnf->rmapbufdis);
printf("Check Rmap Error: %i\n", cnf->check_rmap_err);
printf("Remove Protocol ID: %i\n", cnf->rm_prot_id);
printf("Blocking Transmit: %i\n", cnf->tx_blocking);
printf("Disable when Link Error: %i\n", cnf->disable_err);
printf("Link Error IRQ Enabled: %i\n", cnf->link_err_irq);
printf("Link Error Event Task ID: %i\n", cnf->event_id);
printf("RMAP Available: %i\n", cnf->is_rmap);
printf("RMAP CRC Available: %i\n", cnf->is_rmapcrc);
printf("Unaligned Receive Buffer Support: %i\n", cnf->is_rxunaligned);
printf("\n");
}
void print_statistics(spw_stats *stats)
{
printf("\n");
printf(" ******** STATISTICS ******** \n");
printf("Transmit link errors: %i\n", stats->tx_link_err);
printf("Receiver RMAP header CRC errors: %i\n", stats->rx_rmap_header_crc_err);
printf("Receiver RMAP data CRC errors: %i\n", stats->rx_rmap_data_crc_err);
printf("Receiver EEP errors: %i\n", stats->rx_eep_err);
printf("Receiver truncation errors: %i\n", stats->rx_truncated);
printf("Parity errors: %i\n", stats->parity_err);
printf("Escape errors: %i\n", stats->escape_err);
printf("Credit errors: %i\n", stats->credit_err);
printf("Disconnect errors: %i\n", stats->disconnect_err);
printf("Write synchronization errors: %i\n", stats->write_sync_err);
printf("Early EOP/EEP: %i\n", stats->early_ep);
printf("Invalid Node Address: %i\n", stats->invalid_address);
printf("Packets transmitted: %i\n", stats->packets_sent);
printf("Packets received: %i\n", stats->packets_received);
}
void check_init_config(spw_config *cnf)
{
if (cnf->nodeaddr != 254) {
printf("Incorrect initial node address! Expected: %i Got: %i\n", 254, cnf->nodeaddr);
exit(0);
}
if (cnf->destkey != 0) {
printf("Incorrect initial destination key! Expected: %i Got: %i\n", 0, cnf->destkey);
exit(0);
}
if (cnf->rxmaxlen != 1024) {
printf("Incorrect initial Rx maximum packet length! Expected: %i Got: %i\n", 1024 , cnf->rxmaxlen);
exit(0);
}
if (cnf->linkdisabled != 0) {
printf("Incorrect initial linkdisabled value! Expected: %i Got: %i\n", 0, cnf->linkdisabled);
exit(0);
}
if (cnf->linkstart != 1) {
printf("Incorrect initial linkstart value! Expected: %i Got: %i\n", 1, cnf->linkstart);
exit(0);
}
if (cnf->promiscuous != 0) {
printf("Incorrect initial promiscuous mode value! Expected: %i Got: %i\n", 0, cnf->promiscuous);
exit(0);
}
if (cnf->check_rmap_err != 0) {
printf("Incorrect initial check rmap error value! Expected: %i Got: %i\n", 0, cnf->check_rmap_err);
exit(0);
}
if (cnf->rm_prot_id != 0) {
printf("Incorrect initial remove protocol id value! Expected: %i Got: %i\n", 0, cnf->rm_prot_id);
exit(0);
}
if (cnf->tx_blocking != 0) {
printf("Incorrect initial tx blocking! Expected: %i Got: %i\n", 0, cnf->tx_blocking);
exit(0);
}
if (cnf->disable_err != 0) {
printf("Incorrect initial disable when link error value! Expected: %i Got: %i\n", 0, cnf->disable_err);
exit(0);
}
if (cnf->link_err_irq != 0) {
printf("Incorrect initial link error enable value! Expected: %i Got: %i\n", 0, cnf->link_err_irq);
exit(0);
}
if (cnf->event_id != 0) {
printf("Incorrect initial event id! Expected: %i Got: %i\n", 0, cnf->event_id);
exit(0);
}
}
char *link_status(int status)
{
return lstates[status];
}