2 * Application using MF624 UIO driver
4 * Copyright (C) 2011 Rostislav Lisovy <lisovy@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <sys/types.h>
30 #include <stdint.h> // uintX_t
37 /****************************************************************/
40 int bar_mapping_fill(bar_mapping_t *barmap, const char *uio_dev, int map_nr)
45 static size_t page_size;
47 page_size = sysconf(_SC_PAGESIZE);
49 ssiz = snprintf(NULL, 0, "/sys/class/uio/%s/maps/map%i/", uio_dev, map_nr);
52 /* add reserve to store each size addr, name, offset, size */
56 snprintf(s, ssiz, "/sys/class/uio/%s/maps/map%i/addr", uio_dev, map_nr);
57 file = fopen(s, "rb");
60 fscanf(file, "%"SCNiPTR, &barmap->phys_addr);
63 snprintf(s, ssiz, "/sys/class/uio/%s/maps/map%i/offset", uio_dev, map_nr);
64 file = fopen(s, "rb");
66 barmap->offset = barmap->phys_addr & (page_size - 1);
68 fscanf(file, "%"SCNi32, &barmap->offset);
72 snprintf(s, ssiz, "/sys/class/uio/%s/maps/map%i/size", uio_dev, map_nr);
73 file = fopen(s, "rb");
76 fscanf(file, "%"SCNi32, &barmap->size);
79 barmap->mmap_offset = page_size * map_nr + barmap->offset;
84 int bar_mapping_setup(bar_mapping_t *barmap, int device_fd)
86 static size_t page_mask = 0;
91 page_mask = sysconf(_SC_PAGESIZE) - 1;
93 mmap_start = barmap->mmap_offset & ~page_mask;
94 mmap_size = barmap->mmap_offset + barmap->size + page_mask - mmap_start;
95 mmap_size &= ~page_mask;
97 barmap->mmap_addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, device_fd, mmap_start);
98 if (barmap->mmap_addr == MAP_FAILED) {
102 barmap->virt_addr = (uintptr_t)barmap->mmap_addr;
103 barmap->virt_addr += barmap->mmap_offset & page_mask;
108 /****************************************************************/
110 #define BUFF_SMALL 32
112 #define min(a, b) ((a) > (b) ? (b) : (a))
114 /* Hardware specific */
116 #define GPIOC_reg 0x54
119 #define ADCTRL_reg 0x00
120 #define ADDATA0_reg 0x00
121 #define ADDATA1_reg 0x02
122 #define ADDATA2_reg 0x04
123 #define ADDATA3_reg 0x06
124 #define ADDATA4_reg 0x08
125 #define ADDATA5_reg 0x0a
126 #define ADDATA6_reg 0x0c
127 #define ADDATA7_reg 0x0e
128 #define ADSTART_reg 0x20
130 #define DOUT_reg 0x10
141 #define GPIOC_DACEN_mask (1 << 26)
142 #define GPIOC_LDAC_mask (1 << 23)
143 #define GPIOC_EOLC_mask (1 << 17)
148 static uint32_t dac_channel2reg[] = {
159 static uint32_t adc_channel2reg[] = {
172 #define MFST2REG(mfst, bar_num, reg_offs) \
173 ((void *)(mfst->bar##bar_num.virt_addr + (reg_offs)))
175 mf624_state_t mf624_state;
177 /* Print one byte as binary number */
178 void print_8bin(int nr)
181 for (i = 7; i >= 0; i--) {
182 printf("%d" , ((nr & (1 << i)) > 0));
190 void DIO_write(mf624_state_t* mfst, int16_t val)
192 mf624_write16(val, MFST2REG(mfst, 2, DOUT_reg));
195 uint16_t DIO_read(mf624_state_t* mfst)
197 return mf624_read16(MFST2REG(mfst, 2, DIN_reg)) & 0xFF;
200 void DAC_enable(mf624_state_t* mfst)
202 // Setting DACEN and LDAC bits in GPIO register influences all DACs
203 mf624_write32((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg))
204 | GPIOC_DACEN_mask) // enable output
205 & ~GPIOC_LDAC_mask, // enable conversion
206 MFST2REG(mfst, 0, GPIOC_reg));
211 int DAC_write(mf624_state_t* mfst, dac_channel_t channel, int val)
213 if (channel > sizeof(dac_channel2reg)/sizeof(*dac_channel2reg))
216 mf624_write16(val, MFST2REG(mfst, 2, dac_channel2reg[channel]));
220 int ADC_enable(mf624_state_t* mfst, adc_channel_t channel)
222 mfst->ADC_enabled = 0;
224 if (channel > sizeof(adc_channel2reg)/sizeof(*adc_channel2reg))
227 mfst->ADC_enabled = (1 << channel);
229 mfst->ADC_enabled &= 0xFF;
230 mf624_write16(mfst->ADC_enabled, MFST2REG(mfst, 2, ADCTRL_reg));
231 //print_8bin(ADC_enabled);
236 /* This function blocks until conversion is finished */
237 double ADC_read(mf624_state_t* mfst, adc_channel_t channel)
242 // Activate trigger to start conversion
243 mf624_read16(MFST2REG(mfst, 2, ADSTART_reg));
245 // Check if conversion has finished
246 while((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg)) & GPIOC_EOLC_mask)) {
247 for (i = 0; i < 1000; i++) {} // small wait
250 ADC_enable(mfst, channel);
251 result = mf624_read16(MFST2REG(mfst, 2, ADDATA0_reg));
253 return 10.0 * ((int16_t) (result << 2)) / (double) 0x8000;
257 int open_device(char* path) {
260 device_fd = open(path, O_RDWR | O_SYNC);
261 if (device_fd == -1) {
269 void wait_for_interrupts(int device_fd)
271 read(device_fd, NULL, 1);
274 int disable_interrupts(int device_fd)
276 uint32_t control_value = 0;
279 status = write(device_fd, &control_value, sizeof(uint32_t));
288 int enable_interrupts(int device_fd)
290 uint32_t control_value = 1;
293 status = write(device_fd, &control_value, sizeof(uint32_t));
302 void list_available_mem_regions(char* device)
305 char path[] = "/sys/class/uio/";
306 char subdir[] = "/maps/";
307 char directory[BUFF_MID];
308 memset(directory, '\0', BUFF_MID);
313 strncat(directory, path, strlen(path));
314 strncat(directory, device, min(strlen(device), 8));
315 strncat(directory, subdir, strlen(subdir));
317 dip = opendir(directory);
323 while ((dit = readdir(dip)) != NULL) {
324 if (strcmp(dit->d_name, ".") && strcmp(dit->d_name, "..")) {
325 printf(" %s\n", dit->d_name);
329 status = closedir(dip);
331 perror("closedir()");
338 void list_available_io_ports(char *device)
341 char path[] = "/sys/class/uio/";
342 char subdir[] = "/portio/";
343 char directory[BUFF_MID];
344 memset(directory, '\0', BUFF_MID);
349 strncat(directory, path, strlen(path));
350 strncat(directory, device, min(strlen(device), 8));
351 strncat(directory, subdir, strlen(subdir));
353 status = access(directory, F_OK);
355 printf(" There are no IO port available\n");
359 dip = opendir(directory);
365 while ((dit = readdir(dip)) != NULL) {
366 if (strcmp(dit->d_name, ".") && strcmp(dit->d_name, "..")) {
367 printf(" %s\n", dit->d_name);
371 status = closedir(dip);
373 perror("closedir()");
380 void run_simple_tests(char* dev_name)
384 char buff[BUFF_SMALL];
385 memset(buff, '\0', BUFF_SMALL);
387 strncat(buff, "/dev/", 5);
388 strncat(buff, dev_name, min(strlen(dev_name), 8));
390 printf("Opening %s\n", buff);
392 device_fd = open_device(buff);
393 if (device_fd != -1) {
394 printf("Tring to enable interrupts\n");
395 status = enable_interrupts(device_fd);
397 printf(" Probably OK\n");
400 printf("Tring to disable interrupts\n");
401 status = disable_interrupts(device_fd);
403 printf(" Probably OK\n");
408 printf("Checking for available memory regions exported by the UIO driver\n");
409 list_available_mem_regions(dev_name);
411 printf("Checking for available IO ports exported by the UIO driver\n");
412 list_available_io_ports(dev_name);
415 int mmap_regions(mf624_state_t* mfst)
417 if (bar_mapping_fill(&mfst->bar0, mfst->uio_dev, 0) < 0) {
418 fprintf(stderr, "%s bar0 mapping fill failed\n", mfst->uio_dev);
422 if (bar_mapping_fill(&mfst->bar2, mfst->uio_dev, 1) < 0) {
423 fprintf(stderr, "%s bar2 mapping fill failed\n", mfst->uio_dev);
427 if (bar_mapping_fill(&mfst->bar4, mfst->uio_dev, 2) < 0) {
428 fprintf(stderr, "%s bar4 mapping fill failed\n", mfst->uio_dev);
432 if (bar_mapping_setup(&mfst->bar0, mfst->device_fd) < 0) {
433 fprintf(stderr, "%s bar0 mapping setup failed\n", mfst->uio_dev);
437 if (bar_mapping_setup(&mfst->bar2, mfst->device_fd) < 0) {
438 fprintf(stderr, "%s bar2 mapping setup failed\n", mfst->uio_dev);
442 if (bar_mapping_setup(&mfst->bar4, mfst->device_fd) < 0) {
443 fprintf(stderr, "%s bar4 mapping setup failed\n", mfst->uio_dev);
448 printf("bar0.phys_addr = %"PRIxPTR"\n", mfst->bar0.phys_addr);
449 printf("bar2.phys_addr = %"PRIxPTR"\n", mfst->bar2.phys_addr);
450 printf("bar4.phys_addr = %"PRIxPTR"\n", mfst->bar4.phys_addr);
452 printf("bar0.virt_addr = %"PRIxPTR"\n", mfst->bar0.virt_addr);
453 printf("bar2.virt_addr = %"PRIxPTR"\n", mfst->bar2.virt_addr);
454 printf("bar4.virt_addr = %"PRIxPTR"\n", mfst->bar4.virt_addr);
461 /*int main(int argc, char* argv[])
463 mf624_state_t* mfst = &mf624_state;
464 char buff[BUFF_SMALL];
465 memset(buff, '\0', BUFF_SMALL);
468 printf("Usage: %s UIO_DEVICE\n UIO_DEVICE\tname of uio device in /dev\n", argv[0]);
472 mfst->uio_dev = argv[1];
474 strncat(buff, "/dev/", 5);
475 strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
477 mfst->device_fd = open_device(buff);
478 if (mfst->device_fd < 0) {
479 fprintf(stderr, "%s open failed (%s)!\n", mfst->uio_dev, strerror(errno));
482 if (mmap_regions(mfst) < 0) {
483 fprintf(stderr, "%s mmap_regions failed (%s)!\n", mfst->uio_dev, strerror(errno));
490 printf("Reading DIO: ");
491 print_8bin(DIO_read(mfst));
494 printf("Setting DA1 to 10 V\n");
495 DAC_write(mfst, DA1, 0x3FFF);
498 printf("Reading ADC0: ");
499 printf("%f V\n", ADC_read(mfst, AD0));
502 printf("Reading ADC1: ");
503 printf("%f V\n", ADC_read(mfst, AD1));
506 printf("Setting DIO to 0xff\n");
507 DIO_write(mfst, 0xff);
510 printf("Setting DIO to 0x00\n");
511 DIO_write(mfst, 0x00);
514 printf("Setting DA1 to 5 V\n");
515 DAC_write(mfst, DA1, 0x3000);
518 printf("Reading ADC0: ");
519 printf("%f V\n", ADC_read(mfst, AD0));
522 printf("Reading ADC1: ");
523 printf("%f V\n", ADC_read(mfst, AD1));
525 printf("----------------------\n\n");