📄 pci550x_timer.c
字号:
/* * pci550x user test/demo program - Timers * * Invoke this test program from the command line as follows: * * $ ./pci550x_timer -f /dev/pci550xN [-t TIMER] [-F USECS] * * -f /dev/pci550xN = device node (N=device #) * -t TIMER = timer select (0 or 1) * -p USECS = timer polling rate in microseconds * -F USECS = usec timer frequency (2,4,6.....131070) * * EXAMPLE: run the test on device 2 timer 1 with a timer output * frequency of 200 microseconds and a timer polling rate of 1 second. * * $ ./pci550x_timer -f /dev/pci550x2 -t1 -F200 -p1000000 */#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 { __u32 f; __u16 h[2];} tmr;int c;int fd, rc;char device[PATH_MAX] = "/dev/pci550x0";int brd_type;char errmsg[80];unsigned int frequency = 100000;unsigned long poll = 0;unsigned long count;unsigned int timer = 0;unsigned long overflow = 0;void sighandler(int signum) { /* Disable the Timer */ if (timer) { rc = ioctl(fd, PCI550X_IOCT_TMR1_ENA, PCI550X_DISABLE); if (rc == -1) { perror("ioctl PCI550X_IOCT_TMR1_ENA"); exit(1); } printf("TIMER1 Disabled\n"); } else { rc = ioctl(fd, PCI550X_IOCT_TMR0_ENA, PCI550X_DISABLE); if (rc == -1) { perror("ioctl PCI550X_IOCT_TMR0_ENA"); exit(1); } printf("TIMER0 Disabled\n"); } exit(0);}int main(int argc, char **argv) { /* get command line args */ opterr = 0; while ((c = getopt(argc, argv, "f:t:F:p:h")) != -1) switch(c) { case 'f': strncpy(device, optarg, PATH_MAX); break; case 'F': if ((sscanf(optarg, "%d", &frequency)) == 0) frequency = 0; else if ( (frequency < 2) || (frequency > 131070) || (frequency % 2) ) { printf("invalid timer frequency: %d\n", frequency); exit(1); } break; case 'p': if ((sscanf(optarg, "%ld", &poll)) == 0) poll = 0; break; case 't': if ((sscanf(optarg, "%d", &timer)) == 0) timer = 0; else if (timer > 1) { printf("error: invalid timer\n"); exit(1); } break; case 'h': printf("usage: pci550x_timer -f /dev/pci550xN " "[-t TIMER] [-F USECS]\n"); printf("-f /dev/pci550xN where N = board number\n"); printf("-t TIMER = timer select (0 or 1)\n"); printf("-F USECS = timer output frequency in " "microseconds (2,4,6,...131070\n"); printf("-p USECS = timer 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 Timer */ if (timer) { rc = ioctl(fd, PCI550X_IOCT_TMR1_ENA, PCI550X_DISABLE); if (rc == -1) { perror("ioctl PCI550X_IOCT_TMR1_ENA"); exit(1); } printf("TIMER1 Disabled\n"); } else { rc = ioctl(fd, PCI550X_IOCT_TMR0_ENA, PCI550X_DISABLE); if (rc == -1) { perror("ioctl PCI550X_IOCT_TMR0_ENA"); exit(1); } } /* Load the Timer */ if (timer) { rc = ioctl(fd, PCI550X_IOCS_TMR1_LOAD, &frequency); if (rc == -1) { perror("ioctl PCI550X_IOCS_TMR1_LOAD"); exit(1); } } else { rc = ioctl(fd, PCI550X_IOCS_TMR0_LOAD, &frequency); if (rc == -1) { perror("ioctl PCI550X_TMR00_LOAD"); exit(1); } } /* Enable the Timer */ if (timer) { rc = ioctl(fd, PCI550X_IOCT_TMR1_ENA, PCI550X_ENABLE); if (rc == -1) { perror("ioctl PCI550X_IOCT_TMR1_ENA"); exit(1); } else printf("TIMER1 Enabled\n"); } else { rc = ioctl(fd, PCI550X_IOCT_TMR0_ENA, PCI550X_ENABLE); if (rc == -1) { perror("ioctl PCI550X_IOCT_TMR0_ENA"); exit(1); } printf("TIMER0 Enabled\n"); } /* install the signal handler */ signal(SIGINT, &sighandler); while (1) { if (poll) usleep(poll); if (timer) { rc = ioctl(fd, PCI550X_IOCG_TMR1_POLL, &tmr.f); if (rc == -1) { perror("ioctl PCI550X_IOCG_TMR1_POLL"); exit(1); } if (!(tmr.h[0] & TMR1_CTRL_ENA)) { printf("error: TIMER1 disabled\n"); exit(1); } else printf("%s:%s TIMER1 enabled at %d usecs\n", device, brd_names[brd_type],frequency); } else { rc = ioctl(fd, PCI550X_IOCG_TMR0_POLL, &tmr.f); if (rc == -1) { perror("ioctl PCI550X_IOCG_TMR0_POLL"); exit(1); } if (!(tmr.h[0] & TMR0_CTRL_ENA)) { printf("error: TIMER0 disabled\n"); exit(1); } else printf("%s:%s TIMER0 enabled at %d usecs\n", device, brd_names[brd_type],frequency); } } close(fd); return(0); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -