]> rtime.felk.cvut.cz Git - mf6xx.git/blob - src/uio/mf614/userspace/test_application/main.c
5ad4093a63bdc7c27a84127fa4410dcd59c27c35
[mf6xx.git] / src / uio / mf614 / userspace / test_application / main.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <dirent.h>
8 #include <sys/types.h>
9 #include <sys/mman.h>
10 #include <stdint.h> // uintX_t
11 #include <unistd.h>
12
13 /* Hardware specific */
14 #define AD_LO           0x0
15 #define AD_HI           0x1
16 #define AD_CTRL         0x0
17
18
19 #define BUFF_SMALL      32
20 #define BUFF_MID        256
21 #define min(a, b)       ((a) > (b) ? (b) : (a))
22
23
24 int status;
25 void* mf614_mem = NULL;
26
27
28 void mf614_adc_init(int channel)
29 {
30         *(((uint8_t *) mf614_mem) + AD_CTRL) = 1 << 6 | 0 << 5 | 0 << 4 /* RNG */| 0 << 3 /*BIP*/| channel;
31 }
32
33 int open_device(char* path) {
34         status = open(path, O_RDWR);
35         if (status == -1) {
36                 perror("open()");
37                 return -1;
38         }
39
40         return status;
41 }
42
43 void wait_for_interrupts(int device_fd)
44 {
45         read(device_fd, NULL, 1);
46 }
47
48 int disable_interrupts(int device_fd)
49 {
50         uint32_t control_value = 0;
51
52         status = write(device_fd, &control_value, sizeof(uint32_t));
53         if (status == -1) {
54                 perror("write()");
55                 return -1;
56         }
57
58         return status;
59 }
60
61 int enable_interrupts(int device_fd)
62 {
63         uint32_t control_value = 1;
64
65         status = write(device_fd, &control_value, sizeof(uint32_t));
66         if (status == -1) {
67                 perror("write()");
68                 return -1;
69         }
70
71         return status;
72 }
73
74 void list_available_mem_regions(char* device)
75 {
76         char path[] = "/sys/class/uio/";
77         char subdir[] = "/maps/";
78         char directory[BUFF_MID];
79         memset(directory, '\0', BUFF_MID);
80
81         DIR *dip;
82         struct dirent *dit;
83
84         strncat(directory, path, strlen(path));
85         strncat(directory, device, min(strlen(device), 8));
86         strncat(directory, subdir, strlen(subdir));
87
88         dip = opendir(directory);
89         if (dip == NULL) {
90                 perror("opendir");
91                 return;
92         }
93
94         while ((dit = readdir(dip)) != NULL) {
95                 if (strcmp(dit->d_name, ".") && strcmp(dit->d_name, "..")) {
96                         printf(" %s\n", dit->d_name);
97                 }
98         }
99
100         status = closedir(dip);
101         if (status == -1) {
102                 perror("closedir()");
103                 return;
104         }
105         
106 }
107
108
109 void list_available_io_ports(char *device)
110 {
111         char path[] = "/sys/class/uio/";
112         char subdir[] = "/portio/";
113         char directory[BUFF_MID];
114         memset(directory, '\0', BUFF_MID);
115
116         DIR *dip;
117         struct dirent *dit;
118
119         strncat(directory, path, strlen(path));
120         strncat(directory, device, min(strlen(device), 8));
121         strncat(directory, subdir, strlen(subdir));
122
123         status = access(directory, F_OK);
124         if (status == -1) {
125                 printf(" There are no IO port available\n");
126                 return;
127         }
128
129         dip = opendir(directory);
130         if (dip == NULL) {
131                 perror("opendir");
132                 return;
133         }
134
135         while ((dit = readdir(dip)) != NULL) {
136                 if (strcmp(dit->d_name, ".") && strcmp(dit->d_name, "..")) {
137                         printf(" %s\n", dit->d_name);
138                 }
139         }
140
141         status = closedir(dip);
142         if (status == -1) {
143                 perror("closedir()");
144                 return;
145         }
146
147 }
148
149
150 void run_simple_tests(char* dev_name)
151 {
152         int device_fd;
153         char buff[BUFF_SMALL];
154         memset(buff, '\0', BUFF_SMALL);
155
156         strncat(buff, "/dev/", 5);
157         strncat(buff, dev_name, min(strlen(dev_name), 8));
158
159         printf("Opening %s\n", buff);
160
161         device_fd = open_device(buff);
162         if (device_fd != -1) {
163                 printf("Tring to enable interrupts\n");
164                 status = enable_interrupts(device_fd);
165                 if (status != -1) {
166                         printf(" Probably OK\n");
167                 }
168                 
169                 printf("Tring to disable interrupts\n");
170                 status = disable_interrupts(device_fd);
171                 if (status != -1) {
172                         printf(" Probably OK\n");
173                 }
174         }
175
176
177         printf("Checking for available memory regions exported by the UIO driver\n");
178         list_available_mem_regions(dev_name);
179
180         printf("Checking for available IO ports exported by the UIO driver\n");
181         list_available_io_ports(dev_name);
182 }
183
184
185 int main(int argc, char* argv[])
186 {
187         int device_fd;
188         int mem_size = 1; //TODO
189         float adc_value;
190         char buff[BUFF_SMALL];
191         char read_buff[BUFF_SMALL];
192         memset(buff, '\0', BUFF_SMALL);
193
194
195         if (argc < 2) {
196                 printf("Usage: %s UIO_DEVICE\n   UIO_DEVICE\tname of uio device in /dev\n", argv[0]);
197                 return 1;
198         }       
199
200
201         strncat(buff, "/dev/", 5);
202         strncat(buff, argv[1], min(strlen(argv[1]), 8));
203
204         device_fd = open_device(buff);
205         if (device_fd != -1) {
206                 mf614_mem = mmap(0, mem_size * sysconf(_SC_PAGESIZE), PROT_READ | PROT_WRITE, MAP_SHARED, device_fd, 0);
207                 if (mf614_mem == MAP_FAILED) {
208                         perror("mmap()");
209                 }
210         
211
212                 while (1){
213                         
214                         for (int i = 0; i <= 7; i++) {
215                                 enable_interrupts(device_fd);
216                                 mf614_adc_init(i);
217                                 status = read(device_fd, read_buff, sizeof(uint32_t)); // should block until interrupt received
218                 
219                                 // ... got interrupt -- handle it
220                                 adc_value = *(((uint8_t *) mf614_mem) + AD_LO) | (*(((uint8_t *) mf614_mem) + AD_HI) << 8);
221                                 printf("[CH %d] ADC = %f V\n", i, adc_value/819);
222                         }
223                         printf("\n");
224                         sleep(1);
225                 }
226         }
227
228
229         return 0;
230 }