-
Notifications
You must be signed in to change notification settings - Fork 21
/
counter.c
51 lines (41 loc) · 1.01 KB
/
counter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int fd;
int log2_Ncycles;
uint32_t phase_inc;
double phase_in, freq_in;
uint32_t count;
void *cfg;
char *name = "/dev/mem";
const int freq = 125000000; // Hz
int Ncycles;
if (argc == 3)
{
log2_Ncycles = atoi(argv[1]);
freq_in = atof(argv[2]);
}
else
{
log2_Ncycles = 1;
freq_in = 1.;
}
phase_inc = (uint32_t)(2.147482*freq_in);
Ncycles = 1<<log2_Ncycles;
if((fd = open(name, O_RDWR)) < 0)
{
perror("open");
return 1;
}
cfg = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x42000000);
*((uint32_t *)(cfg + 8)) = (0x1f & log2_Ncycles) + (phase_inc << 5); // set log2_Ncycles and phase_inc
count = *((uint32_t *)(cfg + 0));
printf("Counts: %5d, cycles: %5d, frequency: %6.5f Hz\n", count, Ncycles, (double)Ncycles/count*freq);
munmap(cfg, sysconf(_SC_PAGESIZE));
return 0;
}