-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect_3008.c
138 lines (112 loc) · 3.36 KB
/
collect_3008.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <time.h>
#include <sys/time.h>
#include <getopt.h>
#include <errno.h>
#include "collect_3008.h"
#define MAX(a,b) (((a)>(b))?(a):(b))
#define maxframes MAX(10000, (POLL_FREQ*CAP_SECS))
int64_t get_counter(); //declared in main_timer
extern const long cpufreq; //defined in main_timer
static int fds[3];
static volatile int framecount=0;
static frame_t data[maxframes];
char *sysfs_names[] = {"/sys/bus/iio/devices/iio:device0/in_voltage1-voltage0_raw", "/sys/bus/iio/devices/iio:device0/in_voltage3-voltage2_raw", "/sys/bus/iio/devices/iio:device0/in_voltage7_raw"};
void init_3008()
{
for(int i=0; i<3;i++)
{
fds[i] = open(sysfs_names[i], O_RDONLY);
if(fds[i]<0) {
perror("open()");
printf("%s\n", sysfs_names[i]);
}
}
}
void deinit_3008()
{
}
int fast_atoi(const char *buff)
{
int c = 0, sign = 0, x = 0;
const char *p = buff;
for(c = *(p++); (c < 48 || c > 57); c = *(p++)) {if (c == 45) {sign = 1; c = *(p++); break;}}; // eat whitespaces and check sign
for(; c > 47 && c < 58; c = *(p++)) x = (x << 1) + (x << 3) + c - 48;
return sign ? -x : x;
}
int read_adc(int fd) {
char sval[10];
int val=0;
memset(sval, 0, 10);
if(read(fd, sval, 10)<0) {
perror("read()");
} else {
val = fast_atoi(sval);
lseek(fd, 0, SEEK_SET);
}
return val;
}
void poll_3008()
{
int val;
int64_t ts=0;
if(framecount >= maxframes) return;
val = read_adc(fds[0]);
ts = get_counter();
data[framecount].ch1.val = val;
data[framecount].ch1.ts = ts;
val = read_adc(fds[1]);
ts = get_counter();
data[framecount].ch2.val = val;
data[framecount].ch2.ts = ts;
val = read_adc(fds[2]);
ts = get_counter();
data[framecount].rpm.val = val;
data[framecount].rpm.ts = ts;
framecount++;
}
int cap_done_3008()
{
return framecount >= maxframes;
}
int64_t ts_to_nano(int64_t ts)
{
double fracsec = (double)ts/(double)cpufreq;
return (int64_t)(fracsec * (double)NSEC_PER_SEC);
}
void dump_data_3008(char* filename)
{
//filename is a stem, we create 3 files based on it.
char fnamec1[80], fnamec2[80], fnamerpm[80];
sprintf(fnamec1, "%s-ch1.csv", filename);
sprintf(fnamec2, "%s-ch2.csv", filename);
sprintf(fnamerpm, "%s-rpm.csv", filename);
FILE* fc1 = fopen(fnamec1, "w");
FILE* fc2 = fopen(fnamec2, "w");
FILE* frpm = fopen(fnamerpm, "w");
int64_t c1_l=data[0].ch1.ts, c2_l=data[0].ch2.ts, rpm_l=data[0].rpm.ts;
int64_t c1_offset=0, c2_offset=0, rpm_offset=0;
for(int i=0; i<framecount; i++) {
//convert cycle-count timpestamps to nanos here
int64_t tmp = data[i].ch1.ts;
if(c1_l-tmp>2147483647) c1_offset+=4294967295;
fprintf(fc1,"%lld,%d\n", ts_to_nano(data[i].ch1.ts+c1_offset), data[i].ch1.val);
c1_l = tmp;
tmp = data[i].ch2.ts;
if(c2_l-tmp>2147483647) c2_offset+=4294967295;
fprintf(fc2,"%lld,%d\n", ts_to_nano(data[i].ch2.ts+c2_offset), data[i].ch2.val);
c2_l = tmp;
tmp = data[i].rpm.ts;
if(rpm_l-tmp>2147483647) rpm_offset+=4294967295;
fprintf(frpm,"%lld,%d\n", ts_to_nano(data[i].rpm.ts+rpm_offset), data[i].rpm.val);
rpm_l = tmp;
}
}