-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbmp180.c
228 lines (191 loc) · 6.02 KB
/
bmp180.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <math.h>
#include <linux/i2c-dev.h>
#include <stdint.h>
int reg_write_byte(int handle, uint8_t devaddr, uint8_t regaddr,
uint8_t content) {
struct i2c_rdwr_ioctl_data iocall; // structure pass to i2c driver
struct i2c_msg message;
uint8_t buffer[2];
buffer[0] = regaddr;
buffer[1] = content;
iocall.nmsgs = 1;
iocall.msgs = &message;
message.addr = devaddr;
message.flags = 0; //write
message.buf = (char*) buffer;
message.len = sizeof(buffer);
if (ioctl(handle, I2C_RDWR, (unsigned long) &iocall) < 0) {
printf("error during reg_write_byte()\n");
return -1;
}
return 0;
}
int reg_read_byte(int handle, uint8_t devaddr, uint8_t regaddr,
uint8_t* content) {
struct i2c_rdwr_ioctl_data iocall; // structure pass to i2c driver
struct i2c_msg messages[2];
iocall.nmsgs = 2;
iocall.msgs = messages;
messages[0].addr = devaddr;
messages[0].flags = 0; //write
messages[0].buf = (char*) ®addr;
messages[0].len = 1;
messages[1].addr = devaddr;
messages[1].flags = I2C_M_RD; //READ
messages[1].buf = (char*) content;
messages[1].len = 1;
if (ioctl(handle, I2C_RDWR, (unsigned long) &iocall) < 0) {
printf("error during reg_read_byte()\n");
return -1;
}
return 0;
}
int reg_read_short(int handle, uint8_t devaddr, uint8_t regaddr,
uint16_t* content) {
struct i2c_rdwr_ioctl_data iocall; // structure pass to i2c driver
struct i2c_msg messages[2];
uint8_t buffer[2];
iocall.nmsgs = 2;
iocall.msgs = messages;
messages[0].addr = devaddr;
messages[0].flags = 0; //write
messages[0].buf = (char*) ®addr;
messages[0].len = 1;
messages[1].addr = devaddr;
messages[1].flags = I2C_M_RD; //READ
messages[1].buf = (char*) buffer;
messages[1].len = 2;
if (ioctl(handle, I2C_RDWR, (unsigned long) &iocall) < 0) {
printf("error during reg_read_short()\n");
return -1;
}
*content = (uint16_t) (buffer[0] << 8) | buffer[1];
return 0;
}
int reg_read_24(int handle, uint8_t devaddr, uint8_t regaddr, int32_t* content) {
struct i2c_rdwr_ioctl_data iocall; // structure pass to i2c driver
struct i2c_msg messages[2];
uint8_t buffer[3];
iocall.nmsgs = 2;
iocall.msgs = messages;
messages[0].addr = devaddr;
messages[0].flags = 0; //write
messages[0].buf = (char*) ®addr;
messages[0].len = 1;
messages[1].addr = devaddr;
messages[1].flags = I2C_M_RD; //READ
messages[1].buf = (char*) buffer;
messages[1].len = 3;
if (ioctl(handle, I2C_RDWR, (unsigned long) &iocall) < 0) {
printf("error during reg_read_short()\n");
return -1;
}
*content = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
return 0;
}
typedef struct {
int16_t AC1; // = 408;
int16_t AC2; // = -72;
int16_t AC3; // =-14383;
uint16_t AC4; //=32741;
uint16_t AC5; //=32757;
uint16_t AC6; //=23153;
int16_t B1; //= 6190;
int16_t B2; //= 4;
int16_t MB; //= -32768;
int16_t MC; //= -8711;
int16_t MD; //= 2868;
} bmp180_calibration;
#define BMP180_I2C_ADDR (0x77)
int bmp180_read_cal(int fd, bmp180_calibration* cal) {
int res = 0;
res |= reg_read_short(fd, BMP180_I2C_ADDR, 0xaa, (uint16_t*) &cal->AC1);
res |= reg_read_short(fd, BMP180_I2C_ADDR, 0xac, (uint16_t*) &cal->AC2);
res |= reg_read_short(fd, BMP180_I2C_ADDR, 0xae, (uint16_t*) &cal->AC3);
res |= reg_read_short(fd, BMP180_I2C_ADDR, 0xb0, (uint16_t*) &cal->AC4);
res |= reg_read_short(fd, BMP180_I2C_ADDR, 0xb2, (uint16_t*) &cal->AC5);
res |= reg_read_short(fd, BMP180_I2C_ADDR, 0xb4, (uint16_t*) &cal->AC6);
res |= reg_read_short(fd, BMP180_I2C_ADDR, 0xb6, (uint16_t*) &cal->B1);
res |= reg_read_short(fd, BMP180_I2C_ADDR, 0xb8, (uint16_t*) &cal->B2);
res |= reg_read_short(fd, BMP180_I2C_ADDR, 0xba, (uint16_t*) &cal->MB);
res |= reg_read_short(fd, BMP180_I2C_ADDR, 0xbc, (uint16_t*) &cal->MC);
res |= reg_read_short(fd, BMP180_I2C_ADDR, 0xbe, (uint16_t*) &cal->MD);
return res;
}
int main(void) {
const char* device = "/dev/i2c-1";
int fd = open(device, O_RDWR);
if (fd < 0) {
perror("Failed to open device");
return -1;
}
ioctl(fd, I2C_TIMEOUT, 3); // i2c timeout, 1 for 10ms ; if too small, i2c may lose response
ioctl(fd, I2C_RETRIES, 3); // i2c retry limit
bmp180_calibration cal;
if (0 == bmp180_read_cal(fd, &cal)) {
// printf("Calibration read\n",cal.AC1);
} else {
printf("Error reading calibration\n");
return -1;
}
int32_t X1;
int32_t X2;
int32_t X3;
int32_t B3;
int32_t B4;
int32_t B5;
int32_t B6;
int32_t B7;
int32_t T;
int32_t p;
int16_t oss = 3;
reg_write_byte(fd, BMP180_I2C_ADDR, 0xf4, 0x2e); //Start Temperature Measurement
usleep(4500); //Wait for Temperature measurement
uint16_t temp16;
reg_read_short(fd, BMP180_I2C_ADDR, 0xf6, &temp16);
int32_t UT = temp16;
reg_write_byte(fd, BMP180_I2C_ADDR, 0xf4, 0x34 + (oss << 6)); //Start Pressure Measurement
usleep(25500); //Wait for precise pressure measurement
int32_t UP;
reg_read_24(fd, BMP180_I2C_ADDR, 0xf6, &UP);
UP = UP >> (8 - oss);
X1 = (UT - cal.AC6) * cal.AC5 / (1 << 15);
X2 = cal.MC * (1 << 11) / (X1 + cal.MD);
B5 = (X1 + X2);
T = (B5 + 8) / (1 << 4);
// printf("Temperature is %i.%i°C\n",T/10,T%10);
B6 = B5 - 4000;
X1 = (cal.B2 * (B6 * B6 / (1 << 12))) / (1 << 11);
X2 = cal.AC2 * B6 / (1 << 11);
X3 = X1 + X2;
B3 = (((cal.AC1 * 4 + X3) << oss) + 2) / 4;
X1 = cal.AC3 * B6 / (1 << 13);
X2 = (cal.B1 * (B6 * B6 / (1 << 12))) / (1 << 16);
X3 = ((X1 + X2) + 2) / (1 << 2);
B4 = cal.AC4 * (uint32_t) (X3 + 32768) / (1 << 15);
B7 = ((uint32_t) UP - B3) * (50000 >> oss);
// if(B7<0x80000000)
// p=(B7*2)/B4;
// else
p = (B7 / B4) * 2;
X1 = (p / (1 << 8)) * (p / (1 << 8));
X1 = (X1 * 3038) / (1 << 16);
X2 = (-7357 * p) / (1 << 16);
p = p + (X1 + X2 + 3791) / (1 << 4);
// printf("Pressure is %i Pa\n",p);
double p0;
double pd;
pd=(double)p;
double alt = 125;
p0 = p / pow(1 - alt / 44330.0, 5.255);
// printf("Assuming we are at %.1f m elevation the Pressure at sea level is %0.1f hPa\n",alt,p0/100);
printf("%i.%i;", T / 10, T % 10);
printf("%0.1f;", pd / 100);
printf("%0.1f\n", p0 / 100);
return (0);
}