+++ /dev/null
-#include <stdio.h>
-#include <errno.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <string.h>
-#include <dirent.h>
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <stdint.h> // uintX_t
-#include <unistd.h>
-
-/* Hardware specific */
-#define AD_LO 0x0
-#define AD_HI 0x1
-#define AD_CTRL 0x0
-
-
-#define BUFF_SMALL 32
-#define BUFF_MID 256
-#define min(a, b) ((a) > (b) ? (b) : (a))
-
-
-int status;
-void* mf614_mem = NULL;
-
-
-void mf614_adc_init(int channel)
-{
- *(((uint8_t *) mf614_mem) + AD_CTRL) = 1 << 6 | 0 << 5 | 0 << 4 /* RNG */| 0 << 3 /*BIP*/| channel;
-}
-
-int open_device(char* path) {
- status = open(path, O_RDWR);
- if (status == -1) {
- perror("open()");
- return -1;
- }
-
- return status;
-}
-
-void wait_for_interrupts(int device_fd)
-{
- read(device_fd, NULL, 1);
-}
-
-int disable_interrupts(int device_fd)
-{
- uint32_t control_value = 0;
-
- status = write(device_fd, &control_value, sizeof(uint32_t));
- if (status == -1) {
- perror("write()");
- return -1;
- }
-
- return status;
-}
-
-int enable_interrupts(int device_fd)
-{
- uint32_t control_value = 1;
-
- status = write(device_fd, &control_value, sizeof(uint32_t));
- if (status == -1) {
- perror("write()");
- return -1;
- }
-
- return status;
-}
-
-void list_available_mem_regions(char* device)
-{
- char path[] = "/sys/class/uio/";
- char subdir[] = "/maps/";
- char directory[BUFF_MID];
- memset(directory, '\0', BUFF_MID);
-
- DIR *dip;
- struct dirent *dit;
-
- strncat(directory, path, strlen(path));
- strncat(directory, device, min(strlen(device), 8));
- strncat(directory, subdir, strlen(subdir));
-
- dip = opendir(directory);
- if (dip == NULL) {
- perror("opendir");
- return;
- }
-
- while ((dit = readdir(dip)) != NULL) {
- if (strcmp(dit->d_name, ".") && strcmp(dit->d_name, "..")) {
- printf(" %s\n", dit->d_name);
- }
- }
-
- status = closedir(dip);
- if (status == -1) {
- perror("closedir()");
- return;
- }
-
-}
-
-
-void list_available_io_ports(char *device)
-{
- char path[] = "/sys/class/uio/";
- char subdir[] = "/portio/";
- char directory[BUFF_MID];
- memset(directory, '\0', BUFF_MID);
-
- DIR *dip;
- struct dirent *dit;
-
- strncat(directory, path, strlen(path));
- strncat(directory, device, min(strlen(device), 8));
- strncat(directory, subdir, strlen(subdir));
-
- status = access(directory, F_OK);
- if (status == -1) {
- printf(" There are no IO port available\n");
- return;
- }
-
- dip = opendir(directory);
- if (dip == NULL) {
- perror("opendir");
- return;
- }
-
- while ((dit = readdir(dip)) != NULL) {
- if (strcmp(dit->d_name, ".") && strcmp(dit->d_name, "..")) {
- printf(" %s\n", dit->d_name);
- }
- }
-
- status = closedir(dip);
- if (status == -1) {
- perror("closedir()");
- return;
- }
-
-}
-
-
-void run_simple_tests(char* dev_name)
-{
- int device_fd;
- char buff[BUFF_SMALL];
- memset(buff, '\0', BUFF_SMALL);
-
- strncat(buff, "/dev/", 5);
- strncat(buff, dev_name, min(strlen(dev_name), 8));
-
- printf("Opening %s\n", buff);
-
- device_fd = open_device(buff);
- if (device_fd != -1) {
- printf("Tring to enable interrupts\n");
- status = enable_interrupts(device_fd);
- if (status != -1) {
- printf(" Probably OK\n");
- }
-
- printf("Tring to disable interrupts\n");
- status = disable_interrupts(device_fd);
- if (status != -1) {
- printf(" Probably OK\n");
- }
- }
-
-
- printf("Checking for available memory regions exported by the UIO driver\n");
- list_available_mem_regions(dev_name);
-
- printf("Checking for available IO ports exported by the UIO driver\n");
- list_available_io_ports(dev_name);
-}
-
-
-int main(int argc, char* argv[])
-{
- int device_fd;
- int mem_size = 1; //TODO
- float adc_value;
- char buff[BUFF_SMALL];
- char read_buff[BUFF_SMALL];
- memset(buff, '\0', BUFF_SMALL);
-
-
- if (argc < 2) {
- printf("Usage: %s UIO_DEVICE\n UIO_DEVICE\tname of uio device in /dev\n", argv[0]);
- return 1;
- }
-
-
- strncat(buff, "/dev/", 5);
- strncat(buff, argv[1], min(strlen(argv[1]), 8));
-
- device_fd = open_device(buff);
- if (device_fd != -1) {
- mf614_mem = mmap(0, mem_size * sysconf(_SC_PAGESIZE), PROT_READ | PROT_WRITE, MAP_SHARED, device_fd, 0);
- if (mf614_mem == MAP_FAILED) {
- perror("mmap()");
- }
-
-
- while (1){
-
- for (int i = 0; i <= 7; i++) {
- enable_interrupts(device_fd);
- mf614_adc_init(i);
- status = read(device_fd, read_buff, sizeof(uint32_t)); // should block until interrupt received
-
- // ... got interrupt -- handle it
- adc_value = *(((uint8_t *) mf614_mem) + AD_LO) | (*(((uint8_t *) mf614_mem) + AD_HI) << 8);
- printf("[CH %d] ADC = %f V\n", i, adc_value/819);
- }
- printf("\n");
- sleep(1);
- }
- }
-
-
- return 0;
-}