grspw_api.c 27.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*
 * This is a part of PolyORB-HI-C distribution, a minimal
 * middleware written for generated code from AADL models.
 * You should use it with the Ocarina toolsuite.
 *
 * For more informations, please visit http://taste.tuxfamily.org/wiki
 *
 * Copyright (C) 2018 ESA & ISAE.
 */

11
12
/* This driver relies on the GRSPW2 Packet library proposed in RTEMS
 * 4.11 and 5. It supports a more sophisticated way to handle packets
13
 * in memory, coupled with DMA. See RCC manuals for more details.
14
15
 *
 * Note: this implementation relies on elements from the grspw-test
16
 * sample programs provided in RCC 1.3rc4 by Cobham Gaisler.
17
18
19
20
21
22
23
24
25
26
27
 */

#include "grspw_api.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h> // exit
#include <string.h> // memset

#include <rtems.h>

Antonia Francis's avatar
Antonia Francis committed
28
/* SpaceWire parameters */
29
#define SPW_PROT_ID 3
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

/* Configuration of the driver */

/* Total number of packets */
#define PKT_NBR 16

/* Packets allowed to transmission */
#define PKT_NBR_LIMIT (PKT_NBR / 2)

/* Number of supplementary bytes to avoid truncated packets */
#define EXTRA_BYTES 8

/* The header is coded on 4 bytes (documentation) */
#define HEADER_SIZE 4

/** Macros used with the GRSPW devices */
#define DEV(device) ((struct grspw_dev *)(device))

Antonia Francis's avatar
Antonia Francis committed
48
/* DECLARATION OF STRUCTURES AND RTEMS ELEMENTS */
49

Antonia Francis's avatar
Antonia Francis committed
50
/* Declaration of the transmission/reception task used in test_app */
51
52
53
54
55
56
rtems_task dma_task_rx(rtems_task_argument unused);
rtems_task dma_task_tx(rtems_task_argument unused);

/* Tasks ID corresponding*/
rtems_id tid_dma_rx, tid_dma_tx;

57
/* Driver internal data structures */
58

Antonia Francis's avatar
Antonia Francis committed
59
60
61
62
/**
 * \struct spwpkt.
 * \brief Structure used as a bridge in the grspw_pkt data and header implementation.
 */
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
struct spwpkt {
  struct grspw_pkt p;

  /* 32 bytes of data (- 4byte data-header) and 8 extra bytes to avoid
     truncated bad packets */
  int data[PKT_SIZE + EXTRA_BYTES];

  /* up to 16byte header (path address) before but 4 bytes is enough */
  int hdr[16];
};

/** Array listing all packet buffers used by application for each device */
struct spwpkt pkts[DEVS_MAX][PKT_NBR];

/* Macros used to make a task Wait */
#define BLOCK 2
#define timeout 32

/** Integer that will represent the maximum number of devices */
int nospw;

Antonia Francis's avatar
Antonia Francis committed
84
/* FORWARD DECLARATION OF ELEMENTS USED IN grspw_api_init FUNCTION */
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

/* Declaration of semaphores responsible of the synchronization between tasks */
rtems_id dma_sem;
rtems_id dma_sync_tx;
rtems_id dma_sync_rx;
/* Used in the Routing process */
extern int router_setup_custom(void);

/* Other functions */
void init_pkts(void);
int dev_init(int idx);

void grspw_api_init(void){
  int i;
  extern struct router_hw_info router_hw;

Antonia Francis's avatar
Antonia Francis committed
101
  /* INITIALIZING ROUTER PORTS, DEVICES AND DMA */
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
  /* Initialize two GRSPW AMBA ports */
  printf("Setting up SpaceWire router\n");
  if (router_setup_custom()) {
    printf("Failed router initialization, assuming that it does not exists\n");
  }
  else {
    /* on-chip router found */
    if (router_hw.nports_amba < 2) {
      printf("Error. Router with less than 2 AMBA ports not supported\n");
      exit(0);
    }
  }
  /* Obtain the number of GRSPW devices registered to the driver.*/
  nospw = grspw_dev_count();
  printf("dev_count = %d\n", grspw_dev_count());
  printf("nospw = %d\n",nospw);
  if (nospw < 1) {
    /* no device found */
    printf("Found no SpaceWire cores, aborting\n");
    exit(0);
  }

  if (nospw > DEVS_MAX) {
    /* too many devices, restriction awaited */
    printf("Limiting to %d SpaceWire devices\n", DEVS_MAX);
    nospw = DEVS_MAX;
  }

  memset(devs, 0, sizeof(devs));
  /* Initialize all GRSPW devices registered to the driver*/
  for (i=0; i<nospw; i++) {
    if (dev_init(i)) {
      printf("Failed to initialize GRSPW%d\n", i);
      exit(0);
    }
    fflush(NULL);
  }
  /* Initialize GRSPW (Device and DMA) */
  init_pkts();
  printf("\n\nStarting SpW DMA channels\n");
  for (i = 0; i < nospw; i++) {
    printf("Starting GRSPW%d: ", i);
    fflush(NULL);
    if (grspw_start(DEV(&devs[i]))) {
      printf("Failed to initialize GRSPW%d\n", i);
      exit(0);
    }
    printf("DMA Started Successfully\n");
  }
  fflush(NULL);

  /* Run SpaceWire Test application */
  rtems_task_create(
                    rtems_build_name( 'T', 'D', 'R', 'X' ),
                    10, RTEMS_MINIMUM_STACK_SIZE * 10, RTEMS_DEFAULT_MODES,
                    RTEMS_FLOATING_POINT, &tid_dma_rx);
  rtems_task_create(
                    rtems_build_name( 'T', 'D', 'T', 'X' ),
                    10, RTEMS_MINIMUM_STACK_SIZE * 10, RTEMS_DEFAULT_MODES,
                    RTEMS_FLOATING_POINT, &tid_dma_tx);

  /* Device Semaphore responsible of the synchronisation, created with
     count = 1 */
  if (rtems_semaphore_create(rtems_build_name('S', 'E', 'M', '0'), 1,
                             RTEMS_FIFO | RTEMS_SIMPLE_BINARY_SEMAPHORE | \
                             RTEMS_NO_INHERIT_PRIORITY | RTEMS_LOCAL |	\
                             RTEMS_NO_PRIORITY_CEILING, 1, &dma_sem) != RTEMS_SUCCESSFUL){
    printf("Failed creating dma_sem Semaphore\n");
    exit(0);
  }
  /* Device Semaphore responsible of the transmission task, created with count = 0 */
  if (rtems_semaphore_create(rtems_build_name('S', 'E', 'M', '1'), 0,
                             RTEMS_FIFO | RTEMS_SIMPLE_BINARY_SEMAPHORE | \
                             RTEMS_NO_INHERIT_PRIORITY | RTEMS_LOCAL |	\
                             RTEMS_NO_PRIORITY_CEILING, 0, &dma_sync_tx) != RTEMS_SUCCESSFUL){
    printf("Failed creating dma_sync_tx Semaphore\n");
    exit(0);
  }
  /* Device Semaphore responsible of the reception task, created with count = 0 */
  if (rtems_semaphore_create(rtems_build_name('S', 'E', 'M', '2'), 0,
                             RTEMS_FIFO | RTEMS_SIMPLE_BINARY_SEMAPHORE | \
                             RTEMS_NO_INHERIT_PRIORITY | RTEMS_LOCAL |	\
                             RTEMS_NO_PRIORITY_CEILING, 0, &dma_sync_rx) != RTEMS_SUCCESSFUL){
    printf("Failed creating dma_sync_rx Semaphore\n");
    exit(0);
  }

  rtems_task_start(tid_dma_rx, dma_task_rx, 0);
  rtems_task_start(tid_dma_tx, dma_task_tx, 0);

}
Antonia Francis's avatar
Antonia Francis committed
193
194
195
196
197
198
199
200
/**
 * \brief Function that initialize all packets of the application, splitting them in RX and TX packets.
 * 
 * Using the spwpkt structure as a bridge to fill the different packets. \n
 * The initialized TX packets are put in the tx_buf_list to be sent (in test_app). \n
 * The initialized RX packets are put in the rx_list so that the "prepare" function (in dma_rx) 
 * put them in the rx_ready list, to provide empty packets for Reception. \n
 */
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
void init_pkts(void){
  struct spwpkt *pkt;
  int i, j;
  memset(&pkts[0][0], 0, sizeof(pkts));

  for (i = 0; i < DEVS_MAX; i++) {
    grspw_list_clr(&devs[i].rx_list);
    grspw_list_clr(&devs[i].tx_list);
    grspw_list_clr(&devs[i].tx_buf_list);
    devs[i].rx_list_cnt = 0;
    devs[i].tx_list_cnt = 0;
    devs[i].tx_buf_list_cnt = 0;
    devs[i].rx_buf_list_cnt = 0;

    for (j = 0, pkt = &pkts[i][0]; j < PKT_NBR; j++, pkt = &pkts[i][j]){
      pkt->p.pkt_id = (i << 8) + j; /* unused */
      /* pkt->p.data has also as a value the adress of pkt.data, pkt being a spwpkt */
      pkt->p.data = &pkt->data[0];
      pkt->p.hdr = &pkt->hdr[0];
      /* (PKT_NBR - PKT_NBR_LIMIT) Packets allowed to reception */
      if (j < PKT_NBR - PKT_NBR_LIMIT) {
        /* RX buffer */
        /* Add to device RX list */
        grspw_list_append(&devs[i].rx_list, &pkt->p);
        devs[i].rx_list_cnt++;
      }
      /* (PKT_NBR_LIMIT) Packets allowed to transmission */
      else {
        /* TX buffer */
        pkt->p.dlen = PKT_SIZE;

        /* Initialize at 0 on the space destined to the data */
        memset(pkt->p.data + HEADER_SIZE, 0, PKT_SIZE - HEADER_SIZE);

        /* Adding the packet to device tx_buf_list */
        grspw_list_append(&devs[i].tx_buf_list, &pkt->p);
        devs[i].tx_buf_list_cnt++;
      }
    }
  }
}

Antonia Francis's avatar
Antonia Francis committed
243
/* Function playing with the timecode */
244
245
246
247
248
249
void app_tc_isr(void *data, int tc);
void app_tc_isr(void *data, int tc){
        struct grspw_device *dev = data;
        printf("GRSPW%d: TC-ISR received 0x%02x\n", dev->index, tc);
}

Antonia Francis's avatar
Antonia Francis committed
250
/** Variable used to configure a GRSPW-device cfg attribute */
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
struct grspw_config dev_def_cfg =
{
  .adrcfg =
  {
    .promiscuous = 1, /* Detect all packets */
    .def_addr = 32, /* updated bu dev_init() */
    .def_mask = 0,
    .dma_nacfg =
    {
      /* Since only one DMA Channel is used, only
       * the default Address|Mask is used.
       */
      {
        .node_en = 0,
        .node_addr = 0,
        .node_mask = 0,
      },
      {
        .node_en = 0,
        .node_addr = 0,
        .node_mask = 0,
      },
      {
        .node_en = 0,
        .node_addr = 0,
        .node_mask = 0,
      },
      {
        .node_en = 0,
        .node_addr = 0,
        .node_mask = 0,
      },
    },
  },
  .rmap_cfg = 0,		/* Disable RMAP */
  .rmap_dstkey = 0,	/* No RMAP DESTKEY needed when disabled */
  .tc_cfg = TCOPTS_EN_TX|TCOPTS_EN_RX,/* Enable TimeCode */
  .tc_isr_callback = app_tc_isr,/* TimeCode ISR */
  .tc_isr_arg = NULL,	/* No TimeCode ISR Argument */
  .enable_chan_mask = 1,	/* Enable only the first DMA Channel */
  .chan =
  {
    {
      .flags = DMAFLAG_NO_SPILL,
      .rxmaxlen = PKT_SIZE+4,
      .rx_irq_en_cnt = 0, /* Disable RX IRQ generation */
      .tx_irq_en_cnt = 0, /* Disable TX IRQ generation */
    },
    /* The other 3 DMA Channels are unused */
  },
};

Antonia Francis's avatar
Antonia Francis committed
303
304
305
/**
 * \brief Function fully initializing the idx Device.
 * 
306
 * It especially returns an error message if only one port is available or
Antonia Francis's avatar
Antonia Francis committed
307
308
309
310
311
 * if the device can't open correctly. \n
 * It also resets all packets lists. \
 * \param idx identifier of the device.
 * \return 0 if successful.
 * \return -1 if there is an error.
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
 */
int dev_init(int idx){
        struct grspw_device *dev = &devs[idx];
        int i, ctrl, clkdiv, tc;

        printf(" Initializing SpaceWire device %d\n", idx);

        memset(dev, 0, sizeof(struct grspw_device));

        dev->index = idx;
        /* pointer relative to the good opening of the device */
        dev->dh = grspw_open(idx);
        if (dev->dh == NULL) {
                printf("Failed to open GRSPW device %d\n", idx);
                return -1;
        }
        grspw_hw_support(dev->dh, &dev->hwsup);

#ifdef PRINT_GRSPW_RESET_CFG
        grspw_config_read(DEV(dev), &dev->cfg);
        printf("\n\n---- DEFAULT CONFIGURATION FROM DRIVER/HARDWARE ----\n");
        grspw_cfg_print(&dev->hwsup, &dev->cfg);
#endif

        dev->cfg = dev_def_cfg;
        dev->cfg.adrcfg.def_addr = 32 + idx;
        dev->cfg.tc_isr_arg = dev;
        tc = TCOPTS_EN_TX | TCOPTS_EN_RX | TCOPTS_EN_RXIRQ;
        grspw_tc_ctrl(dev->dh, &tc);

        if (grspw_cfg_set(DEV(dev), &dev->cfg)) {
          grspw_close(dev->dh);
          return -1;
        }

#ifdef PRINT_GRSPW_RESET_CFG
        printf("\n\n---- APPLICATION CONFIGURATION ----\n");
        grspw_cfg_print(&dev->hwsup, &dev->cfg);
        printf("\n\n");
#endif

        /* This will result in an error if only one port available */
        if (dev->hwsup.nports < 2) {
          int port = 1;
          if ( grspw_port_ctrl(dev->dh, &port) == 0 ) {
            printf("Succeeded to select port1, however only one PORT on dev %d!\n", dev->index);
            return -1;
          }
        }

        /* Try to bring link up at fastest clockdiv but do not touch
         * start-up clockdivisor */
        clkdiv = -1;
        grspw_link_ctrl(dev->dh, NULL, NULL, &clkdiv);
        ctrl = LINKOPTS_ENABLE | LINKOPTS_AUTOSTART | LINKOPTS_START;
        clkdiv &= 0xff00;
        grspw_link_ctrl(dev->dh, &ctrl, NULL, &clkdiv);

#if 0
        if ( (dev->hwsup.hw_version >> 16) == GAISLER_SPW2_DMA ){
          printf(" NOTE: running on SPW-ROUTER DMA SpaceWire link (no link-state available)\n");
        }
        else{
          printf(" After Link Start: %d\n", (int)grspw_link_state(dev->dh));
        }
#endif

        dev->run = 0;

        grspw_stats_clr(dev->dh);

        for (i=0; i<dev->hwsup.ndma_chans; i++) {
          if (dev->dma[i]){
            grspw_dma_stats_clr(dev->dma[i]);
          }
        }
        grspw_list_clr(&dev->rx_list);
        grspw_list_clr(&dev->tx_list);
        grspw_list_clr(&dev->tx_buf_list);
        dev->rx_list_cnt = dev->tx_list_cnt = dev->tx_buf_list_cnt = dev->rx_buf_list_cnt= 0;

        return 0;
}

Antonia Francis's avatar
Antonia Francis committed
396
397
398
/**
 * \brief Function that close all dma channels for a specified device idx.
 *
399
 * If the dma channel is active,
Antonia Francis's avatar
Antonia Francis committed
400
401
402
403
404
405
 * it is closed and the NULL value is imput in the dma array. \n
 * If the closing is correctly done, returns 0. \n
 * 
 * \param idx identifier of the device.
 * \return 0 if a successful closing is done.
 * \return the result of dma_close if there is an error.
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
 */
int dev_dma_close_all(int idx){
  struct grspw_device *dev = &devs[idx];
  int i, rc;
  /* Going through all dma channels, close if opened before*/
  for (i=0; i<dev->hwsup.ndma_chans; i++) {
    if (dev->dma[i]) {
      rc = grspw_dma_close(dev->dma[i]);
      if (rc){
        return rc;
      }
      dev->dma[i] = NULL;
    }
  }
  return 0;
}

void dev_cleanup(int idx){
  struct grspw_device *dev = &devs[idx];
  if (dev->dh == NULL)
    return;

  /* Stop all DMA activity first */
  grspw_stop(DEV(dev));

  /* Wait for other tasks to be thrown out from driver */
  rtems_task_wake_after(4);

  /* Close all DMA channels */
  if (dev_dma_close_all(idx)) {
    printf("FAILED to close GRSPW%d DMA\n", idx);
  }

  /* Close the selected device, which needs all DMA channel closed*/
  if (grspw_close(dev->dh)) {
    printf("FAILED to close GRSPW%d\n", idx);
  }
  dev->dh = NULL;
}

Antonia Francis's avatar
Antonia Francis committed
446
/* FORWARD DECLARATION OF PKT_INIT_HDR FUNCTION */
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
void pkt_init_hdr(struct grspw_pkt *pkt, struct route_entry *route, int idx);

size_t grspw_sending
(int device,
 struct route_entry * p_route,
 void *message, int message_size){

  rtems_semaphore_obtain(dma_sem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
  size_t message_size_sent = message_size;
  struct grspw_pkt *pkt;
  /* Get a TX packet buffer */
  /* Taking a packet at the head */
  pkt = devs[device].tx_buf_list.head;
  if (pkt != NULL) {
    /* The new head is the following pkt, pkt->next */
    devs[device].tx_buf_list.head = pkt->next;
    devs[device].tx_buf_list_cnt--;
    if (pkt->next == NULL){
      /* It was the last TX packet */
      devs[device].tx_buf_list.tail = NULL;
    }
    /* Header Init */
    pkt_init_hdr(pkt,p_route, device);

    /* Message Initialisation, assuming 1 byte = 1 char */
    if (message_size <= PKT_SIZE - HEADER_SIZE){
      memcpy (pkt->data + HEADER_SIZE, message, message_size);
      /* The header is included in the dlen on HEADER_SIZE bytes */
      pkt->dlen  = message_size + HEADER_SIZE;
      char *data = ((char *)pkt->data);
      printf("data[1]=%d", data[1]);
      printf("\n");
    }
    else{
      printf("Message too long not initialized ");
    }
    printf(" X%d: scheduling packet on GRSPW%d\n",device, device);
    /* Send packet by adding it to the tx_list */
    grspw_list_append(&devs[device].tx_list, pkt);
    devs[device].tx_list_cnt++;
    rtems_semaphore_release(dma_sync_tx);
  }
  else {
    /* Tx_buf_list is empty */
    printf("No free transmit buffers available\n");
    /* the dma_sync_tx is not released */
    //continue;
  }
  rtems_semaphore_release(dma_sem);
  return message_size_sent;
}

size_t grspw_receiving(int device,void *message){
500
  rtems_semaphore_obtain(dma_sync_rx, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
501
502
  rtems_semaphore_obtain(dma_sem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
  size_t message_size_received = 0;
503
504
  int actual_device;
  
505
  struct grspw_pkt *pkt;
506
507
  /* Get a RX packet buffer from the HEAD */

508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
  /* For the time being, we ignore device and scan all devices, this disable routing for the moment */
  
  for (actual_device = 0; actual_device < nospw; actual_device++) {
    pkt = devs[actual_device].rx_buf_list.head;
    if (pkt != NULL) {
      /* The new head is the following pkt, pkt->next */
      devs[actual_device].rx_buf_list.head = pkt->next;
      devs[actual_device].rx_buf_list_cnt--;
      if (pkt->next == NULL){
	/* It was the last RX packet */
	devs[actual_device].rx_buf_list.tail = NULL;
      }
      
      /* Assuming 1 byte = 1 char */
      int offset = HEADER_SIZE - 1;
      while ( ( ((char *) pkt->data)[offset] != SPW_PROT_ID) && (offset >= 0))
	offset--;
      
      offset += 3;
      
      /*
	computation of the offset to determine where is value '155'
	we need to add two bytes after the 155 for the offset
	
      */
      
      /* The header is included in the dlen on HEADER_SIZE bytes */
      message_size_received = pkt->dlen - offset;
      
      if (message_size_received <= PKT_SIZE - HEADER_SIZE){
	memcpy (message,pkt->data + offset, message_size_received);
      }
      else{
	printf("Message too long not received ");
      }
      /* Reschedule packet by adding it to the rx_list */
      grspw_list_append(&devs[actual_device].rx_list, pkt);
      devs[actual_device].rx_list_cnt++;

      rtems_semaphore_release(dma_sem);
      return message_size_received;
549
550
    }
  }
551

552
553
554
555
  rtems_semaphore_release(dma_sem);
  return message_size_received;
}

Antonia Francis's avatar
Antonia Francis committed
556
557
558
559
/**
 * \struct pkt_hdr.
 * \brief SpaceWire packet payload (data) content layout.
 * 
560
561
 * Structure used to fill the first bytes of the GRSPW-pkt Data.
 * It is particularly used in the pkt_init_hdr function.
Antonia Francis's avatar
Antonia Francis committed
562
 */
563
564
565
566
567
568
569
570
571
572
573
574
575
576
struct pkt_hdr {
  unsigned char addr;
  unsigned char protid;
  /* Port index of source */
  unsigned char port_src;

  /* Resv2 : Zero for now */
  unsigned char resv2;

  /* Data array used to implement the GRSPW-pkt data */
  //unsigned int data[(PKT_SIZE-4)/4];
  unsigned int data[PKT_SIZE + EXTRA_BYTES];
};

Antonia Francis's avatar
Antonia Francis committed
577
578
579
580
581
/**
 * \brief Function that initialize the packet header of the pkt, and the Data payload header.
 * 
 * The pkt is leaving the idx Device. \n
 * The pkt_hdr structure is used to modify the first bytes of the GRSPW-pkt Data (destination, device..). \n
582
 * The hdr array is used to implement in the GRSPW-pkt Hdr the different Adresses,
Antonia Francis's avatar
Antonia Francis committed
583
584
585
586
587
588
 * IF there are non-first Destination Addresses. \n
 * 
 * \param pkt the pkt targeted. 
 * \param route the routing table. 
 * \param idx identifier of the device. 
 */
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
void pkt_init_hdr(struct grspw_pkt *pkt, struct route_entry *route, int idx){
  int i;
  struct pkt_hdr *pkt_hdr = (struct pkt_hdr *)pkt->data;
  unsigned char *hdr = pkt->hdr;

  /* In path addressing we put non-first Destination Addresses in
   * header. If there is only one destination, (i=0)
   * route->dstadr[0] is always non-zero (written in test_app) */
  i = 0;
  while ( route->dstadr[i+1] != 0 ){
    hdr[i] = route->dstadr[i];
    i++;
  }
  printf("hdr[0] = %d, \n", hdr[0]);
  /* */
  pkt->hlen = i;

  /* Put last address/Destination device in pkt_hdr->addr */
  pkt_hdr->addr = route->dstadr[i];
  pkt_hdr->protid = SPW_PROT_ID;
  printf("pkt_hdr->protid = %d, \n", pkt_hdr->protid);

  /* Sending device */
  pkt_hdr->port_src = idx;
  pkt_hdr->resv2 = 0;
}

Antonia Francis's avatar
Antonia Francis committed
616
/* FORWARD DECLARATION OF FUNCTIONS USED IN TRANSMISSION AND RECEPTION */
617
618
619
int dma_process_rx(struct grspw_device *dev);
int dma_process_tx(struct grspw_device *dev);

Antonia Francis's avatar
Antonia Francis committed
620
621
622
623
/**
 * \brief Task handling the reception.
 * 
 * The dma_sem semaphore is used so that the tasks don't overlap. \n
624
 * The dma_sync_rx is used so that the task isn't periodic but is triggered
Antonia Francis's avatar
Antonia Francis committed
625
626
627
 * only when something is about to be received. \n
 * It checks all active devices and calls the dma_process_rx function. \n
 */
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
rtems_task dma_task_rx(rtems_task_argument unused){
  int i;
  struct grspw_device *dev;
  printf("Started DMA-rx control task\n");

  while (true) {
    rtems_semaphore_obtain(dma_sem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);

    for (i = 0; i < nospw; i++) {
      dev = &devs[i];
      if (dev->dh == NULL)
        continue;
      dma_process_rx(dev);
    }
    rtems_semaphore_release(dma_sem);
  }

  printf(" DMA task shutdown\n");
  rtems_task_delete(RTEMS_SELF);
}
Antonia Francis's avatar
Antonia Francis committed
648
649
650
651
/**
 * \brief Task handling the transmission.
 * 
 * The dma_sem semaphore is used so that the tasks don't overlap. \n
652
 * The dma_sync_tx is used so that the task isn't periodic but is triggered
Antonia Francis's avatar
Antonia Francis committed
653
654
655
 * only when something is about to be sent. \n
 * It checks all active devices and calls the dma_process_tx function. \n
 */
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
rtems_task dma_task_tx(rtems_task_argument unused){
  int i;
  struct grspw_device *dev;
  printf("Started DMA-tx control task\n");

  while (true) {
    rtems_semaphore_obtain(dma_sync_tx, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
    rtems_semaphore_obtain(dma_sem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
    for (i = 0; i < nospw; i++) {
      dev = &devs[i];
      if (dev->dh == NULL)
        continue;
      dma_process_tx(dev);
    }
    rtems_semaphore_release(dma_sem);
    //rtems_task_wake_after(20);
  }

  printf(" DMA task shutdown\n");
  rtems_task_delete(RTEMS_SELF);
}

Antonia Francis's avatar
Antonia Francis committed
678
679
680
681
682
683
684
/**
 * \brief Function called in the task dma_process_rx, used in the receiving process.
 *
 * The list lst is cleared to receive the received packets. \n
 * A rx_wait is done, then the packets are received from the RX_RECV
 *   list (cf documentation) with rx_recv. \n
 * The obtained message is printed, then the packets are moved
685
 *   (append) to the rx_buf_list. (They will be moved from the
Antonia Francis's avatar
Antonia Francis committed
686
687
688
689
690
691
 *   rx_buf_list to the rx_list in the grspw_receiving function.) \n
 * Then the reception is prepared with rx_prepare with putting Empty
 *   packet from rx_list to RX-READY (cf documentation). \n
 * The rx_list is then cleared. \n
 * \return 0 if there is no conflict.
 */
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
int dma_process_rx(struct grspw_device *dev){

  int cnt, rc;
  struct grspw_list lst;
  struct grspw_pkt *pkt;
  unsigned char *c;

  int rx_ready, rx_sched, rx_recv, rx_hwcnt;
  int tx_send, tx_sched, tx_sent, tx_hwcnt;

  /* Counting the packets */
  grspw_dma_rx_count(dev->dma[0], &rx_ready, &rx_sched, &rx_recv, &rx_hwcnt);
  grspw_dma_tx_count(dev->dma[0], &tx_send, &tx_sched, &tx_sent, &tx_hwcnt);

  if (rx_hwcnt >= 127) {
    printf(" DMA DRVQ RX_READY: %d\n", rx_ready);
    printf(" DMA DRVQ RX_SCHED: %d\n", rx_sched);
    printf(" DMA DRVQ RX_RECV: %d\n", rx_recv);
    printf(" DMA DRVQ RX_HWCNT: %d\n", rx_hwcnt);
  }
Antonia Francis's avatar
Antonia Francis committed
712
  /* RX PART */
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
  /* Try to receive packets on receiver interface */
  grspw_list_clr(&lst);

  /* Initialize cnt to receive as many packets as possible */
  cnt = -1;

  /* The wait function is used to prioritize the tasks */
  rc = grspw_dma_rx_wait(dev->dma[0], BLOCK, 0, BLOCK, timeout);

  /* Received Packets go to lst */
  rc = grspw_dma_rx_recv(dev->dma[0], 0, &lst, &cnt);

  /* Checking the reception */
  if (rc != 0) {
    printf("rx_recv failed %d\n", rc);
    return -1;
  }

  if (cnt > 0) {
    printf("GRSPW%d: Received %d packets", dev->index, cnt);

    for (pkt = lst.head; pkt; pkt = pkt->next) {
      if ((pkt->flags & RXPKT_FLAG_RX) == 0) {
        printf(" PKT not received.. buf ret\n");
        continue;
      } else if (pkt->flags &
                 (RXPKT_FLAG_EEOP | RXPKT_FLAG_TRUNK))
        {
          printf(" PKT RX errors:");
          if (pkt->flags & RXPKT_FLAG_TRUNK)
            printf(" truncated");
          if (pkt->flags & RXPKT_FLAG_EEOP)
            printf(" EEP");
          printf(" (0x%x)", pkt->flags);
        } else
        printf(" \n");

      /* Printing what has been received */
      /* The informations are in the data array */
      c = (unsigned char *)pkt->data;
      printf(" hlen of length %d bytes,", pkt->hlen);
      printf(" dlen of length %d bytes,", pkt->dlen);
      printf("PKT of length %d bytes", pkt->dlen + pkt->hlen);
      printf("\n");

      /* The header is included in the dlen on HEADER_SIZE bytes */
      for (int i=0;i<pkt->dlen;i++){
        c[i] = ((unsigned char *)pkt->data)[i];
        printf(" %c", c[i]);
      }
      printf("\n");

      /* Adding that to work on the header/data */
      for (int i=0;i<pkt->dlen;i++){
        c[i] = ((unsigned char *)pkt->data)[i];
        printf(" %d", c[i]);
      }
      printf("\n");
    }

    /* Reuse packet by moving packets from lst to rx_buf_list (put in
     * rx_list in test_app)
     */

    grspw_list_append_list(&dev->rx_buf_list, &lst);
    dev->rx_buf_list_cnt += cnt;
  }

781
782
783
784
785
786
787
788
  /* Release the semaphore blocking task in grspw_receiving() if
   * something is found in the buffer
   */
  pkt = (&dev->rx_buf_list)->head;
  if (pkt != NULL) {
    rtems_semaphore_release(dma_sync_rx);
  }

789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
  /* Prepare receiver with packet buffers */
  if (dev->rx_list_cnt > 0) {
    /* Put Empty packets from rx_list in RX-READY */
    rc = grspw_dma_rx_prepare
      (dev->dma[0], 0, &dev->rx_list, dev->rx_list_cnt);

    if (rc != 0) {
      /* Pkts not successfully added */
      printf("rx_prep failed %d\n", rc);
      return -1;
    }

    grspw_list_clr(&dev->rx_list);
    dev->rx_list_cnt = 0;
  }
  return 0;
}

Antonia Francis's avatar
Antonia Francis committed
807
808
809
810
811
812
813
814
815
816
817
818
819
/**
 * \brief Function called in the task dma_process_tx, used in the sending process.
 *
 * The list lst is cleared to receive the sent packets. \n
 * The packets are received from the TX_SENT list (cf documentation) with tx_reclaim. \n
 * The packets are moved (append) to the tx_buf_list. \n
 * (They will be moved from the tx_buf_list to the tx_list in the grspw_sending function.) \n
 * The obtained message is printed. \n
 * Then the sending is prepared with tx_send with putting the packet from tx_list to TX-SEND (cf documentation). \n
 * The tx_list is then cleared. \n
 * \return 0 if there is no conflict.
 */

820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/** Function called in the task dma_process_tx, used in the sending process :
 * The list lst is cleared to receive the sent packets.
 * The packets are received from the TX_SENT list (cf documentation) with tx_reclaim.
 * The packets are moved (append) to the tx_buf_list.
 * (They will be moved from the tx_buf_list to the tx_list in the grspw_sending function.)
 * The obtained message is printed.
 * Then the sending is prepared with tx_send with putting the packet from tx_list to TX-SEND (cf documentation).
 * The tx_list is then cleared.
*/

int dma_process_tx(struct grspw_device *dev){
  int cnt, rc, i;
  struct grspw_list lst;
  struct grspw_pkt *pkt;
  unsigned char *c;

  int rx_ready, rx_sched, rx_recv, rx_hwcnt;
  int tx_send, tx_sched, tx_sent, tx_hwcnt;

  grspw_dma_rx_count(dev->dma[0], &rx_ready, &rx_sched, &rx_recv, &rx_hwcnt);
  grspw_dma_tx_count(dev->dma[0], &tx_send, &tx_sched, &tx_sent, &tx_hwcnt);
  if (rx_hwcnt >= 127) {
    printf(" DMA DRVQ TX_SEND: %d\n", tx_send);
    printf(" DMA DRVQ TX_SCHED: %d\n", tx_sched);
    printf(" DMA DRVQ TX_SENT: %d\n", tx_sent);
    printf(" DMA DRVQ TX_HWCNT: %d\n", tx_hwcnt);
  }

Antonia Francis's avatar
Antonia Francis committed
848
  /* TX PART */
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
  /* Reclaim already transmitted buffers */
  /* Initialize cnt to receive as many packets as possible */
  cnt = -1;
  grspw_list_clr(&lst);

  /* Moving already-sent packets from the SENT queue to lst */
  rc = grspw_dma_tx_reclaim(dev->dma[0], 0, &lst, &cnt);
  if (rc != 0) {
    printf("tx_reclaim failed %d\n", rc);
    exit(0);
  }

  if (cnt > 0) {
    /* Clear transmission flags */
    pkt = lst.head;
    while (pkt) {
      if ((pkt->flags & TXPKT_FLAG_TX) == 0)
        printf(" One Packet TX failed\n");
      else if (pkt->flags & TXPKT_FLAG_LINKERR)
        printf(" One Packet with TX errors\n");
      pkt = pkt->next;
    }
    /* Moving the retrieved packets from lst to tx_buf_list */
    grspw_list_append_list(&dev->tx_buf_list, &lst);
    dev->tx_buf_list_cnt += cnt;
  }

  if (dev->tx_list_cnt > 0) {
    printf("GRSPW%d: Sending %d packets\n", dev->index,dev->tx_list_cnt);
    for (pkt = dev->tx_list.head; pkt; pkt = pkt->next) {
      printf(" hlen of length %d bytes,", pkt->hlen);
      printf(" dlen of length %d bytes,", pkt->dlen);
      printf(" PKT of length %d bytes", pkt->hlen+pkt->dlen);
      printf("\n");
      /* Printing what is going to be sent */
      c = (unsigned char *)pkt->data;
      for (i=0;i< pkt->dlen;i++){
        c[i] = ((unsigned char *)pkt->data)[i];
        printf(" %c", c[i]);
      }
      printf("\n");
      /*Adding that to work on the header/data */
      for (i=0;i< pkt->dlen;i++){
        c[i] = ((unsigned char *)pkt->data)[i];
        printf(" %d", c[i]);
      }
      printf("\n");
    }
    /* Moving to-be-sent packets from the tx_list to the send queue */
    rc = grspw_dma_tx_send(dev->dma[0], 0, &dev->tx_list,
                           dev->tx_list_cnt);
    if (rc != 0) {
      printf("tx_send failed %d\n", rc);
      exit(0);
                        }
    dev->tx_list_cnt = 0;
    grspw_list_clr(&dev->tx_list);
  }
  return 0;
}