initial commit
[rpipulser] / pulser.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <assert.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <sys/mman.h>
8
9 #define IOBASEADDR              0x3f000000
10 #define IOSIZE                  16777216
11
12 #define IOREGADDR(x)    ((x) | IOBASEADDR)
13
14 #define REG_GPFSEL0             *((volatile uint32_t*)IOREGADDR(0x200000))
15 #define REG_GPFSEL1             *((volatile uint32_t*)IOREGADDR(0x200004))
16 #define REG_GPFSEL2             *((volatile uint32_t*)IOREGADDR(0x200008))
17 #define REG_GPSET0              *((volatile uint32_t*)IOREGADDR(0x20001c))
18 #define REG_GPSET1              *((volatile uint32_t*)IOREGADDR(0x200020))
19 #define REG_GPCLR0              *((volatile uint32_t*)IOREGADDR(0x200028))
20 #define REG_GPCLR1              *((volatile uint32_t*)IOREGADDR(0x20002c))
21
22 #define FSEL_OUT(x)             (1 << (((x) % 10) * 3))
23
24 void print_usage(const char *argv0);
25
26 int npulses = 1;
27 int freqhz = 1;
28 int duty = 128; /* duty cycle 0-255 */
29 int pin = 24;
30
31 int main(int argc, char **argv)
32 {
33         int i, fd;
34         void *ptr;
35         long ontime, offtime;
36
37         for(i=1; i<argc; i++) {
38                 if(argv[i][0] == '-') {
39                         if(argv[i][2] == 0) {
40                                 switch(argv[i][1]) {
41                                 case 'n':
42                                         if(!argv[++i] || (npulses = atoi(argv[i])) < 1) {
43                                                 fprintf(stderr, "-n must be followed by the number of pulses\n");
44                                                 return 1;
45                                         }
46                                         break;
47
48                                 case 'f':
49                                         if(!argv[++i] || (freqhz = atoi(argv[i])) < 1) {
50                                                 fprintf(stderr, "-f must be followed by the pulse frequency\n");
51                                                 return 1;
52                                         }
53                                         break;
54
55                                 case 'd':
56                                         if(!argv[++i] || (duty = atoi(argv[i])) < 1 || duty >= 256) {
57                                                 fprintf(stderr, "-d must be followed by the duty cycle: 1-255\n");
58                                                 return 1;
59                                         }
60                                         break;
61
62                                 case 'h':
63                                         print_usage(argv[0]);
64                                         return 0;
65
66                                 default:
67                                         fprintf(stderr, "invalid option: %s\n", argv[i]);
68                                         print_usage(argv[0]);
69                                         return 1;
70                                 }
71                         } else {
72                                 fprintf(stderr, "invalid option: %s\n", argv[i]);
73                                 print_usage(argv[0]);
74                                 return 1;
75                         }
76                 } else {
77                         fprintf(stderr, "unexpected argument: %s\n", argv[i]);
78                         print_usage(argv[0]);
79                         return 1;
80                 }
81         }
82
83
84         if((fd = open("/dev/mem", O_RDWR)) == -1) {
85                 perror("failed to open /dev/mem");
86                 return 1;
87         }
88         if((ptr = mmap((void*)IOBASEADDR, IOSIZE, PROT_READ | PROT_WRITE,
89                                         MAP_SHARED | MAP_FIXED, fd, IOBASEADDR)) == (void*)-1) {
90                 perror("failed to map IO space");
91                 return 1;
92         }
93         assert(ptr == (void*)IOBASEADDR);
94
95         ontime = duty * 1000000 / (freqhz * 256);
96         offtime = 1000000 / freqhz - ontime;
97
98         printf("%d pulses, %d hz, %d/256 duty cycle\n", npulses, freqhz, duty);
99         printf("timing: %ldus on, %ldus off\n", ontime, offtime);
100
101         REG_GPCLR0 = 1 << pin;
102         if(pin < 10) {
103                 REG_GPFSEL0 |= FSEL_OUT(pin);
104         } else if(pin < 20) {
105                 REG_GPFSEL1 |= FSEL_OUT(pin);
106         } else if(pin < 30) {
107                 REG_GPFSEL2 |= FSEL_OUT(pin);
108         }
109
110         for(i=0; i<npulses; i++) {
111                 REG_GPSET0 = 1 << pin;
112                 usleep(ontime);
113                 REG_GPCLR0 = 1 << pin;
114                 usleep(offtime);
115         }
116
117         REG_GPCLR0 = 1 << pin;
118
119         munmap(ptr, IOSIZE);
120         close(fd);
121         return 0;
122 }
123
124 void print_usage(const char *argv0)
125 {
126         printf("Usage: %s [options]\n", argv0);
127         printf("Options:\n");
128         printf(" -n: number of pulses to emit (default: 1)\n");
129         printf(" -f: frequency of pulses in hz (default: 1)\n");
130         printf(" -d: duty cycle [1, 255] (default: 128)\n");
131         printf(" -h: print usage and exit\n");
132 }