-
Notifications
You must be signed in to change notification settings - Fork 1
/
xil_pci_app.c
100 lines (84 loc) · 2.53 KB
/
xil_pci_app.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
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <malloc.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#define DEVICE "/dev/ml605_pcie-0"
#define MEM_BUFFER_SIZE 16
int fd = 0;
int write_device() {
ssize_t ret;
char *data = (char *)malloc(1024 * sizeof(char));
printf("Enter the data to write into device\n");
scanf(" %[^\n]", data);
printf("Total length is %d B\n", strlen(data));
ret = write(fd, data, strlen(data));
if (ret == -1) printf("writting failed\n");
free(data);
return 0;
}
int read_device() {
int read_length = 0;
ssize_t ret;
char *data = (char *)malloc(1024 * sizeof(char));
printf("enter the length of the buffer to read\n");
scanf("%d", &read_length);
printf("the read length selected is %d\n", read_length);
memset(data, fd, sizeof(data));
data[0] = '\0';
ret = read(0, data, read_length);
printf("DEVICE_READ : %s\n", data);
if(ret == -1) printf("reading failed\n");
free(data);
return 0;
}
int main() {
int i = 0;
int value = 0;
uint32_t *shm = NULL;
if (access(DEVICE, F_OK) == -1) {
printf("module %s not loaded\n", DEVICE);
return -1;
}
while(1) {
printf("\tplease enter 1 to write\n");
printf("\tplease enter 2 to read\n");
printf("\tplease enter 3 mmap perform\n");
scanf("%d", &value);
switch(value) {
case 1:
fd = open(DEVICE, O_RDWR);
write_device();
close(fd);
break;
case 2:
fd = open(DEVICE, O_RDWR);
read_device();
close(fd);
break;
case 3:
fd = open(DEVICE, O_RDWR);
shm = mmap(NULL, MEM_BUFFER_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (MAP_FAILED == shm) {
printf("mmap: %s\n", strerror(errno));
return -1;
}
for (i = 0; i < MEM_BUFFER_SIZE; i++){
printf("DATA = 0x%08X\n", shm[i]);
}
// strcpy(shm, "User write to share memory!");
// printf("After write, shm = %s\n", shm);
munmap(shm, MEM_BUFFER_SIZE);
close(fd);
break;
default:
printf("Unknown option selected, please enter right one\n");
break;
}
}
return 0;
}