📄 pci550x_counter.c
字号:
/* * pci550x user test/demo program - Counters * * Invoke this test program from the command line as follows: * * $ ./pci550x_counter -f /dev/pci550xN [-C] [-c COUNTER] [-p USECS] * * -f /dev/pci550xN = device node (N=device #) * -c COUNTER = counter select (0 or 1) * -p USECS = counter polling rate in microseconds * -s START = start count (0 - 65535) * * EXAMPLE: run the test on device 2 counter1 with a counter polling * frequency of 200 microseconds and a start count of 600 * * $ ./pci550x_counter -f /dev/pci550x2 -c1 -p200 -s600 */#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/fcntl.h>#include <sys/ioctl.h>#include <signal.h>#include <unistd.h>#include <string.h>#include <linux/limits.h>#include <linux/types.h>#define _GNU_SOURCE#include <getopt.h>#include "pci550x.h"/* GLOBAL DATA */union { u_int32_t f; u_int16_t h[2];} cntr;int c;int fd, rc;char device[PATH_MAX] = "/dev/pci550x0";int brd_type;char errmsg[80];unsigned long poll= 0;unsigned long count;unsigned int counter = 0;unsigned int start = 0;unsigned long overflow = 0;void sighandler(int signum) { /* Disable the Counter */ if (counter) { rc = ioctl(fd, PCI550X_IOCT_CNTR1_ENA, PCI550X_DISABLE); if (rc == -1) { perror("ioctl PCI550X_IOCT_CNTR1_ENA"); exit(1); } printf("COUNTER1 Disabled\n"); } else { rc = ioctl(fd, PCI550X_IOCT_CNTR0_ENA, PCI550X_DISABLE); if (rc == -1) { perror("ioctl PCI550X_IOCT_CNTR0_ENA"); exit(1); } printf("COUNTER0 Disabled\n"); } exit(0);}int main(int argc, char **argv) { /* get command line args */ opterr = 0; while ((c = getopt(argc, argv, "s:f:d:c:p:h")) != -1) switch(c) { case 's': if ((sscanf(optarg, "%d", &start)) == 0) start = 0; else if (start > 65535) { printf("error: invalid start count: %d\n", start); exit(1); } break; case 'f': strncpy(device, optarg, PATH_MAX); break; case 'p': if ((sscanf(optarg, "%ld", &poll)) == 0) poll = 0; break; case 'c': if ((sscanf(optarg, "%d", &counter)) == 0) counter = 0; else if (counter > 1) { printf("error: invalid counter\n"); exit(1); } break; case 'h': printf("usage: pci550x_counter -f /dev/pci550xN " "[-c COUNTER] [-p USECS]\n"); printf("-f /dev/pci550xN where N = board number\n"); printf("-s START = start count (0 - 65535)\n"); printf("-c COUNTER = counter select (0 or 1)\n"); printf("-p USECS = counter polling rate in " "microseconds\n"); exit(0); break; default: break; } /* open the device */ fd = open(device, O_RDWR); if(fd == -1) { sprintf(errmsg, "open failed on device %s", device); perror(errmsg); exit(1); } else { printf("open succeeded on %s\n", device); } /* query device type */ rc = ioctl(fd, PCI550X_IOCG_BRD_TYPE, &brd_type); if(rc == -1) { perror("ioctl PCI550X_IOCG_BRD_TYPE"); exit(1); } else { printf("ADAC Board Type: %s\n", brd_names[brd_type]); } /* Disable the Counter */ if (counter) { rc = ioctl(fd, PCI550X_IOCT_CNTR1_ENA, PCI550X_DISABLE); if (rc == -1) { perror("ioctl PCI550X_IOCT_CNTR1_ENA"); exit(1); } printf("COUNTER1 Disabled\n"); } else { rc = ioctl(fd, PCI550X_IOCT_CNTR0_ENA, PCI550X_DISABLE); if (rc == -1) { perror("ioctl PCI550X_IOCT_CNTR0_ENA"); exit(1); } } /* Load the Counter */ if (counter) { rc = ioctl(fd, PCI550X_IOCS_CNTR1_LOAD, &start); if (rc == -1) { perror("ioctl PCI550X_IOCS_CNTR1_LOAD"); exit(1); } } else { rc = ioctl(fd, PCI550X_IOCS_CNTR0_LOAD, &start); if (rc == -1) { perror("ioctl PCI550X_IOCS_CNTR0_LOAD"); exit(1); } } /* Enable the Counter */ if (counter) { rc = ioctl(fd, PCI550X_IOCT_CNTR1_ENA, PCI550X_ENABLE); if (rc == -1) { perror("ioctl PCI550X_IOCT_CNTR1_ENA"); exit(1); } else printf("COUNTER1 Enabled\n"); } else { rc = ioctl(fd, PCI550X_IOCT_CNTR0_ENA, PCI550X_ENABLE); if (rc == -1) { perror("ioctl PCI550X_IOCT_CNTR0_ENA"); exit(1); } printf("COUNTER0 Enabled\n"); } /* install the signal handler */ signal(SIGINT, &sighandler); while (1) { if (poll) usleep(poll); if (counter) { rc = ioctl(fd, PCI550X_IOCG_CNTR1_POLL, &cntr.f); if (rc == -1) { perror("ioctl PCI550X_IOCG_CNTR1_POLL"); exit(1); } if (cntr.h[0] & CNTR1_CTRL_ERR) { printf("error: COUNTER1 frequency too high\n"); exit(1); } else if (cntr.h[0] & CNTR1_CTRL_OVF) overflow += (65536 - start); count = (cntr.h[1] - start) + overflow; printf("%s:%s COUNTER1: %ld\n",device, brd_names[brd_type], count); } else { rc = ioctl(fd, PCI550X_IOCG_CNTR0_POLL, &cntr.f); if (rc == -1) { perror("ioctl PCI550X_IOCG_CNTR0_POLL"); exit(1); } if (cntr.h[0] & CNTR0_CTRL_ERR) { printf("error: COUNTER0 frequency too high\n"); exit(1); } else if (cntr.h[0] & CNTR0_CTRL_OVF) overflow += (65536 - start); count = (cntr.h[1] - start) + overflow; printf("%s:%s COUNTER0: %ld\n",device, brd_names[brd_type], count); } } close(fd); return(0); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -