-
Notifications
You must be signed in to change notification settings - Fork 8
/
example.c
98 lines (77 loc) · 2.62 KB
/
example.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include "pinner.h"
#include "axidma.h"
#include "pinner_fns.h"
#define DATA_BUF_SIZE 20000
//Ensure DATA_BUF_SIZE >= NUM_BUFFERS*BUFFER_SZ
#define NUM_BUFFERS 10
#define BUFFER_SZ 1600
int main(int argc, char **argv) {
if (argc != 2) {
puts("Usage: example /dev/uioN, where /dev/uioN is the AXI DMA driver");
return -1;
}
axidma_ctx *ctx = axidma_open(argv[1]);
if (!ctx) {
return -1;
}
int pinner_fd = pinner_open();
char *sg_buf = malloc(5000);
//For the sake of testing, poison the initial SG list to see what's going on
for (int i = 0; i < 5000; i++) {
sg_buf[i] = 0x00;
}
char *data_buf = malloc(DATA_BUF_SIZE);
struct pinner_physlist sg_plist;
struct pinner_handle sg_handle;
int rc = pin_buf(pinner_fd, sg_buf, 5000, &sg_handle, &sg_plist);
if (rc < 0) {
return -1;
}
struct pinner_physlist data_plist;
struct pinner_handle data_handle;
rc = pin_buf(pinner_fd, data_buf, DATA_BUF_SIZE, &data_handle, &data_plist);
if (rc < 0) {
return -1;
}
sg_list *lst = axidma_list_new(sg_buf, &sg_plist, data_buf, &data_plist);
for (int i = 0; i < NUM_BUFFERS; i++) {
rc = axidma_add_entry(lst, BUFFER_SZ);
if (rc == ADD_ENTRY_SG_OOM) {
puts("Ran out of memory for SG descriptors");
break;
} else if (rc == ADD_ENTRY_BUF_OOM) {
puts("Ran out of memory for data");
break;
} else if (rc == ADD_ENTRY_ERROR) {
puts("Unrecoverable error");
exit(-1);
}
}
axidma_write_sg_list(ctx, lst, pinner_fd, &sg_handle);
axidma_s2mm_transfer(ctx, 1, 0);
s2mm_buf buf = axidma_dequeue_s2mm_buf(lst);
int cnt = 0;
while (buf.code != END_OF_LIST) {
if(buf.code == TRANSFER_SUCCESS) cnt++;
printf("Received %s buffer of length %u:\n", (buf.code == TRANSFER_SUCCESS ? "good" : "bad"), buf.len);
for (int i = 0; i < (buf.len) / 64; i++) {
uint32_t *word_ptr = ((uint32_t*) buf.base) + 16*i;
for (int j = 15; j >= 0; j--) {
printf("%08x ", word_ptr[j]);
}
}
puts("");
buf = axidma_dequeue_s2mm_buf(lst);
}
printf("Got %d good packets out of %d\n", cnt, NUM_BUFFERS);
axidma_list_del(lst);
axidma_close(ctx);
unpin_buf(pinner_fd, &data_handle);
unpin_buf(pinner_fd, &sg_handle);
pinner_close(pinner_fd);
return 0;
}