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
35 /****************************************************************/
37 typedef struct bar_mapping_t {
46 int bar_mapping_fill(bar_mapping_t *barmap, const char *uio_dev, int map_nr)
51 static size_t page_size;
53 page_size = sysconf(_SC_PAGESIZE);
55 ssiz = snprintf(NULL, 0, "/sys/class/uio/%s/maps/map%i/", uio_dev, map_nr);
58 /* add reserve to store each size addr, name, offset, size */
60 s = alloca(ssiz + 6 + 1);
62 snprintf(s, ssiz, "/sys/class/uio/%s/maps/map%i/addr", uio_dev, map_nr);
63 file = fopen(s, "rb");
66 fscanf(file, "%"SCNiPTR, &barmap->phys_addr);
69 snprintf(s, ssiz, "/sys/class/uio/%s/maps/map%i/offset", uio_dev, map_nr);
70 file = fopen(s, "rb");
72 barmap->offset = barmap->phys_addr & (page_size - 1);
74 fscanf(file, "%"SCNi32, &barmap->offset);
78 snprintf(s, ssiz, "/sys/class/uio/%s/maps/map%i/size", uio_dev, map_nr);
79 file = fopen(s, "rb");
82 fscanf(file, "%"SCNi32, &barmap->size);
85 barmap->mmap_offset = page_size * map_nr + barmap->offset;
90 int bar_mapping_setup(bar_mapping_t *barmap, int device_fd)
92 static size_t page_mask = 0;
97 page_mask = sysconf(_SC_PAGESIZE) - 1;
99 mmap_start = barmap->mmap_offset & ~page_mask;
100 mmap_size = barmap->mmap_offset + barmap->size + page_mask - mmap_start;
101 mmap_size &= ~page_mask;
103 barmap->mmap_addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, device_fd, mmap_start);
104 if (barmap->mmap_addr == MAP_FAILED) {
108 barmap->virt_addr = (uintptr_t)barmap->mmap_addr;
109 barmap->virt_addr += barmap->mmap_offset & page_mask;
114 /****************************************************************/
116 #define BUFF_SMALL 32
118 #define min(a, b) ((a) > (b) ? (b) : (a))
120 /* Hardware specific */
122 #define GPIOC_reg 0x54
125 #define ADCTRL_reg 0x00
126 #define ADDATA0_reg 0x00
127 #define ADDATA1_reg 0x02
128 #define ADDATA2_reg 0x04
129 #define ADDATA3_reg 0x06
130 #define ADDATA4_reg 0x08
131 #define ADDATA5_reg 0x0a
132 #define ADDATA6_reg 0x0c
133 #define ADDATA7_reg 0x0e
134 #define ADSTART_reg 0x20
136 #define DOUT_reg 0x10
147 #define GPIOC_DACEN_mask (1 << 26)
148 #define GPIOC_LDAC_mask (1 << 23)
149 #define GPIOC_EOLC_mask (1 << 17)
152 typedef enum {DA0, DA1, DA2, DA3, DA4, DA5, DA6, DA7} dac_channel_t;
153 typedef enum {AD0, AD1, AD2, AD3, AD4, AD5, AD6, AD7} adc_channel_t;
155 static uint32_t dac_channel2reg[] = {
166 static uint32_t adc_channel2reg[] = {
177 typedef struct mf624_state_t {
184 int ADC_enabled; // Which ADCs are enabled
187 #define MFST2REG(mfst, bar_num, reg_offs) \
188 ((void *)(mfst->bar##bar_num.virt_addr + (reg_offs)))
190 mf624_state_t mf624_state;
192 /* Print one byte as binary number */
193 void print_8bin(int nr)
196 for (i = 7; i >= 0; i--) {
197 printf("%d" , ((nr & (1 << i)) > 0));
203 static inline int16_t mf624_read16(void *ptr)
205 return *(volatile uint16_t*)ptr;
207 static inline int32_t mf624_read32(void *ptr)
209 return *(volatile uint32_t*) ptr;
212 static inline void mf624_write16(uint16_t val, void *ptr)
214 *(volatile uint16_t*) ptr = val;
217 static inline void mf624_write32(uint32_t val, void *ptr)
219 *(volatile uint32_t*) ptr = val;
222 void DIO_write(mf624_state_t* mfst, int16_t val)
224 mf624_write16(val, MFST2REG(mfst, 2, DOUT_reg));
227 uint16_t DIO_read(mf624_state_t* mfst)
229 return mf624_read16(MFST2REG(mfst, 2, DIN_reg)) & 0xFF;
232 void DAC_enable(mf624_state_t* mfst)
234 // Setting DACEN and LDAC bits in GPIO register influences all DACs
235 mf624_write32((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg))
236 | GPIOC_DACEN_mask) // enable output
237 & ~GPIOC_LDAC_mask, // enable conversion
238 MFST2REG(mfst, 0, GPIOC_reg));
241 int DAC_write(mf624_state_t* mfst, dac_channel_t channel, int val)
243 if (channel > sizeof(dac_channel2reg)/sizeof(*dac_channel2reg))
246 mf624_write16(val, MFST2REG(mfst, 2, dac_channel2reg[channel]));
250 int ADC_enable(mf624_state_t* mfst, adc_channel_t channel)
252 mfst->ADC_enabled = 0;
254 if (channel > sizeof(adc_channel2reg)/sizeof(*adc_channel2reg))
257 mfst->ADC_enabled = (1 << channel);
259 mfst->ADC_enabled &= 0xFF;
260 mf624_write16(mfst->ADC_enabled, MFST2REG(mfst, 2, ADCTRL_reg));
261 //print_8bin(ADC_enabled);
266 /* This function blocks until conversion is finished */
267 double ADC_read(mf624_state_t* mfst, adc_channel_t channel)
272 // Activate trigger to start conversion
273 mf624_read16(MFST2REG(mfst, 2, ADSTART_reg));
275 // Check if conversion has finished
276 while((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg)) & GPIOC_EOLC_mask)) {
277 for (i = 0; i < 1000; i++) {} // small wait
280 ADC_enable(mfst, channel);
281 result = mf624_read16(MFST2REG(mfst, 2, ADDATA0_reg));
283 return 10.0 * ((int16_t) (result << 2)) / (double) 0x8000;
287 int open_device(char* path) {
290 device_fd = open(path, O_RDWR | O_SYNC);
291 if (device_fd == -1) {
299 void wait_for_interrupts(int device_fd)
301 read(device_fd, NULL, 1);
304 int disable_interrupts(int device_fd)
306 uint32_t control_value = 0;
309 status = write(device_fd, &control_value, sizeof(uint32_t));
318 int enable_interrupts(int device_fd)
320 uint32_t control_value = 1;
323 status = write(device_fd, &control_value, sizeof(uint32_t));
332 void list_available_mem_regions(char* device)
335 char path[] = "/sys/class/uio/";
336 char subdir[] = "/maps/";
337 char directory[BUFF_MID];
338 memset(directory, '\0', BUFF_MID);
343 strncat(directory, path, strlen(path));
344 strncat(directory, device, min(strlen(device), 8));
345 strncat(directory, subdir, strlen(subdir));
347 dip = opendir(directory);
353 while ((dit = readdir(dip)) != NULL) {
354 if (strcmp(dit->d_name, ".") && strcmp(dit->d_name, "..")) {
355 printf(" %s\n", dit->d_name);
359 status = closedir(dip);
361 perror("closedir()");
368 void list_available_io_ports(char *device)
371 char path[] = "/sys/class/uio/";
372 char subdir[] = "/portio/";
373 char directory[BUFF_MID];
374 memset(directory, '\0', BUFF_MID);
379 strncat(directory, path, strlen(path));
380 strncat(directory, device, min(strlen(device), 8));
381 strncat(directory, subdir, strlen(subdir));
383 status = access(directory, F_OK);
385 printf(" There are no IO port available\n");
389 dip = opendir(directory);
395 while ((dit = readdir(dip)) != NULL) {
396 if (strcmp(dit->d_name, ".") && strcmp(dit->d_name, "..")) {
397 printf(" %s\n", dit->d_name);
401 status = closedir(dip);
403 perror("closedir()");
410 void run_simple_tests(char* dev_name)
414 char buff[BUFF_SMALL];
415 memset(buff, '\0', BUFF_SMALL);
417 strncat(buff, "/dev/", 5);
418 strncat(buff, dev_name, min(strlen(dev_name), 8));
420 printf("Opening %s\n", buff);
422 device_fd = open_device(buff);
423 if (device_fd != -1) {
424 printf("Tring to enable interrupts\n");
425 status = enable_interrupts(device_fd);
427 printf(" Probably OK\n");
430 printf("Tring to disable interrupts\n");
431 status = disable_interrupts(device_fd);
433 printf(" Probably OK\n");
438 printf("Checking for available memory regions exported by the UIO driver\n");
439 list_available_mem_regions(dev_name);
441 printf("Checking for available IO ports exported by the UIO driver\n");
442 list_available_io_ports(dev_name);
445 int mmap_regions(mf624_state_t* mfst)
447 if (bar_mapping_fill(&mfst->bar0, mfst->uio_dev, 0) < 0) {
448 fprintf(stderr, "%s bar0 mapping fill failed\n", mfst->uio_dev);
452 if (bar_mapping_fill(&mfst->bar2, mfst->uio_dev, 1) < 0) {
453 fprintf(stderr, "%s bar2 mapping fill failed\n", mfst->uio_dev);
457 if (bar_mapping_fill(&mfst->bar4, mfst->uio_dev, 2) < 0) {
458 fprintf(stderr, "%s bar4 mapping fill failed\n", mfst->uio_dev);
462 if (bar_mapping_setup(&mfst->bar0, mfst->device_fd) < 0) {
463 fprintf(stderr, "%s bar0 mapping setup failed\n", mfst->uio_dev);
467 if (bar_mapping_setup(&mfst->bar2, mfst->device_fd) < 0) {
468 fprintf(stderr, "%s bar2 mapping setup failed\n", mfst->uio_dev);
472 if (bar_mapping_setup(&mfst->bar4, mfst->device_fd) < 0) {
473 fprintf(stderr, "%s bar4 mapping setup failed\n", mfst->uio_dev);
478 printf("bar0.phys_addr = %"PRIxPTR"\n", mfst->bar0.phys_addr);
479 printf("bar2.phys_addr = %"PRIxPTR"\n", mfst->bar2.phys_addr);
480 printf("bar4.phys_addr = %"PRIxPTR"\n", mfst->bar4.phys_addr);
482 printf("bar0.virt_addr = %"PRIxPTR"\n", mfst->bar0.virt_addr);
483 printf("bar2.virt_addr = %"PRIxPTR"\n", mfst->bar2.virt_addr);
484 printf("bar4.virt_addr = %"PRIxPTR"\n", mfst->bar4.virt_addr);
491 int main(int argc, char* argv[])
493 mf624_state_t* mfst = &mf624_state;
494 char buff[BUFF_SMALL];
495 memset(buff, '\0', BUFF_SMALL);
498 printf("Usage: %s UIO_DEVICE\n UIO_DEVICE\tname of uio device in /dev\n", argv[0]);
502 mfst->uio_dev = argv[1];
504 strncat(buff, "/dev/", 5);
505 strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
507 mfst->device_fd = open_device(buff);
508 if (mfst->device_fd < 0) {
509 fprintf(stderr, "%s open failed (%s)!\n", mfst->uio_dev, strerror(errno));
512 if (mmap_regions(mfst) < 0) {
513 fprintf(stderr, "%s mmap_regions failed (%s)!\n", mfst->uio_dev, strerror(errno));
520 printf("Reading DIO: ");
521 print_8bin(DIO_read(mfst));
524 printf("Setting DA1 to 10 V\n");
525 DAC_write(mfst, DA1, 0x3FFF);
528 printf("Reading ADC0: ");
529 printf("%f V\n", ADC_read(mfst, AD0));
532 printf("Reading ADC1: ");
533 printf("%f V\n", ADC_read(mfst, AD1));
536 printf("Setting DIO to 0xff\n");
537 DIO_write(mfst, 0xff);
540 printf("Setting DIO to 0x00\n");
541 DIO_write(mfst, 0x00);
544 printf("Setting DA1 to 5 V\n");
545 DAC_write(mfst, DA1, 0x3000);
548 printf("Reading ADC0: ");
549 printf("%f V\n", ADC_read(mfst, AD0));
552 printf("Reading ADC1: ");
553 printf("%f V\n", ADC_read(mfst, AD1));
555 printf("----------------------\n\n");