-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmp180.c
355 lines (280 loc) · 8.98 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "bmp180.h"
#include "FreeRTOS.h"
#include "queue.h"
#include "task.h"
#include "espressif/esp_common.h"
#include "espressif/sdk_private.h"
#include "i2c/i2c.h"
#ifndef LOCAL
#define LOCAL static
#endif
#define BMP180_RX_QUEUE_SIZE 10
#define BMP180_TASK_PRIORITY 9
#define BMP180_DEVICE_ADDRESS 0x77
#define BMP180_VERSION_REG 0xD0
#define BMP180_CONTROL_REG 0xF4
#define BMP180_RESET_REG 0xE0
#define BMP180_OUT_MSB_REG 0xF6
#define BMP180_OUT_LSB_REG 0xF7
#define BMP180_OUT_XLSB_REG 0xF8
#define BMP180_CALIBRATION_REG 0xAA
//
// Values for BMP180_CONTROL_REG
//
#define BMP180_MEASURE_TEMP 0x2E
#define BMP180_MEASURE_PRESS_OSS0 0x34
#define BMP180_MEASURE_PRESS_OSS1 0x74
#define BMP180_MEASURE_PRESS_OSS2 0xB4
#define BMP180_MEASURE_PRESS_OSS3 0xF4
#define BMP180_DEFAULT_CONV_TIME 5000
//
// CHIP ID stored in BMP180_VERSION_REG
//
#define BMP180_CHIP_ID 0x55
//
// Reset value for BMP180_RESET_REG
//
#define BMP180_RESET_VALUE 0xB6
// BMP180_Event_Command
typedef struct
{
uint8_t cmd;
const xQueueHandle* resultQueue;
} bmp180_command_t;
// Just works due to the fact that xQueueHandle is a "void *"
static xQueueHandle bmp180_rx_queue = NULL;
static xTaskHandle bmp180_task_handle = NULL;
// Calibration constants
static int16_t AC1;
static int16_t AC2;
static int16_t AC3;
static uint16_t AC4;
static uint16_t AC5;
static uint16_t AC6;
static int16_t B1;
static int16_t B2;
static int16_t MB;
static int16_t MC;
static int16_t MD;
//
// Forward declarations
//
LOCAL void bmp180_meassure(const bmp180_command_t* command);
LOCAL bool bmp180_informUser_Impl(const xQueueHandle* resultQueue, uint8_t cmd, bmp180_temp_t temperatue, bmp180_press_t pressure);
// Set default implementation .. User gets result as bmp180_result_t event
bool (*bmp180_informUser)(const xQueueHandle* resultQueue, uint8_t cmd, bmp180_temp_t temperatue, bmp180_press_t pressure) = bmp180_informUser_Impl;
// I2C Driver Task
LOCAL void bmp180_driver_task(void *pvParameters)
{
// Data to be received from user
bmp180_command_t current_command;
#ifdef DEBUG
// Wait for commands from the outside
printf("%s: Started Task\n", __FUNCTION__);
#endif
while(1)
{
// Wait for user to insert commands
if (xQueueReceive(bmp180_rx_queue, ¤t_command, portMAX_DELAY) == pdTRUE)
{
#ifdef DEBUG
printf("%s: Received user command %d 0x%p\n", __FUNCTION__, current_command.cmd, current_command.resultQueue);
#endif
// use user provided queue
if (current_command.resultQueue != NULL)
{
// Work on it ...
bmp180_meassure(¤t_command);
}
}
}
}
LOCAL uint8_t bmp180_readRegister8(uint8_t reg)
{
uint8_t r = 0;
if (!i2c_slave_read(BMP180_DEVICE_ADDRESS, reg, &r, 1))
{
r = 0;
}
return r;
}
LOCAL int16_t bmp180_readRegister16(uint8_t reg)
{
uint8_t d[] = { 0, 0 };
int16_t r = 0;
if (i2c_slave_read(BMP180_DEVICE_ADDRESS, reg, d, 2))
{
r = ((int16_t)d[0]<<8) | (d[1]);
}
return r;
}
LOCAL void bmp180_start_Messurement(uint8_t cmd)
{
uint8_t d[] = { BMP180_CONTROL_REG, cmd };
i2c_slave_write(BMP180_DEVICE_ADDRESS, d, 2);
}
LOCAL int16_t bmp180_getUncompensatedMessurement(uint8_t cmd)
{
// Write Start Code into reg 0xF4 (Currently without oversampling ...)
bmp180_start_Messurement((cmd==BMP180_TEMPERATURE)?BMP180_MEASURE_TEMP:BMP180_MEASURE_PRESS_OSS0);
// Wait 5ms Datasheet states 4.5ms
sdk_os_delay_us(BMP180_DEFAULT_CONV_TIME);
return (int16_t)bmp180_readRegister16(BMP180_OUT_MSB_REG);
}
LOCAL void bmp180_fillInternalConstants(void)
{
AC1 = bmp180_readRegister16(BMP180_CALIBRATION_REG+0);
AC2 = bmp180_readRegister16(BMP180_CALIBRATION_REG+2);
AC3 = bmp180_readRegister16(BMP180_CALIBRATION_REG+4);
AC4 = bmp180_readRegister16(BMP180_CALIBRATION_REG+6);
AC5 = bmp180_readRegister16(BMP180_CALIBRATION_REG+8);
AC6 = bmp180_readRegister16(BMP180_CALIBRATION_REG+10);
B1 = bmp180_readRegister16(BMP180_CALIBRATION_REG+12);
B2 = bmp180_readRegister16(BMP180_CALIBRATION_REG+14);
MB = bmp180_readRegister16(BMP180_CALIBRATION_REG+16);
MC = bmp180_readRegister16(BMP180_CALIBRATION_REG+18);
MD = bmp180_readRegister16(BMP180_CALIBRATION_REG+20);
#ifdef DEBUG
printf("%s: AC1:=%d AC2:=%d AC3:=%d AC4:=%u AC5:=%u AC6:=%u \n", __FUNCTION__, AC1, AC2, AC3, AC4, AC5, AC6);
printf("%s: B1:=%d B2:=%d\n", __FUNCTION__, B1, B2);
printf("%s: MB:=%d MC:=%d MD:=%d\n", __FUNCTION__, MB, MC, MD);
#endif
}
LOCAL bool bmp180_create_communication_queues()
{
// Just create them once
if (bmp180_rx_queue==NULL)
{
bmp180_rx_queue = xQueueCreate(BMP180_RX_QUEUE_SIZE, sizeof(bmp180_result_t));
}
return (bmp180_rx_queue!=NULL);
}
LOCAL bool bmp180_is_avaialble()
{
return (bmp180_readRegister8(BMP180_VERSION_REG)==BMP180_CHIP_ID);
}
LOCAL bool bmp180_createTask()
{
// We already have a task
portBASE_TYPE x = pdPASS;
if (bmp180_task_handle==NULL)
{
x = xTaskCreate(bmp180_driver_task, (signed char *)"bmp180_driver_task", 256, NULL, BMP180_TASK_PRIORITY, &bmp180_task_handle);
}
return (x==pdPASS);
}
LOCAL void bmp180_meassure(const bmp180_command_t* command)
{
int32_t T, P;
// Init result to 0
T = P = 0;
if (command->resultQueue != NULL)
{
int32_t UT, X1, X2, B5;
//
// Temperature is always needed ... Also required for pressure only
//
// Calculation taken from BMP180 Datasheet
UT = (int32_t)bmp180_getUncompensatedMessurement(BMP180_TEMPERATURE);
X1 = (UT - (int32_t)AC6) * ((int32_t)AC5) >> 15;
X2 = ((int32_t)MC << 11) / (X1 + (int32_t)MD);
B5 = X1 + X2;
T = (B5 + 8) >> 4;
#ifdef DEBUG
printf("%s: T:= %ld.%d\n", __FUNCTION__, T/10, abs(T%10));
#endif
// Do we also need pressure?
if (command->cmd & BMP180_PRESSURE)
{
int32_t X3, B3, B6;
uint32_t B4, B7, UP;
UP = ((uint32_t)bmp180_getUncompensatedMessurement(BMP180_PRESSURE) & 0xFFFF);
// Calculation taken from BMP180 Datasheet
B6 = B5 - 4000;
X1 = ((int32_t)B2 * ((B6 * B6) >> 12)) >> 11;
X2 = ((int32_t)AC2 * B6) >> 11;
X3 = X1 + X2;
B3 = (((int32_t)AC1 * 4 + X3) + 2) >> 2;
X1 = ((int32_t)AC3 * B6) >> 13;
X2 = ((int32_t)B1 * ((B6 * B6) >> 12)) >> 16;
X3 = ((X1 + X2) + 2) >> 2;
B4 = ((uint32_t)AC4 * (uint32_t)(X3 + 32768)) >> 15;
B7 = (UP - B3) * (uint32_t)(50000UL);
if (B7 < 0x80000000)
{
P = (B7 * 2) / B4;
}
else
{
P = (B7 / B4) * 2;
}
X1 = (P >> 8) * (P >> 8);
X1 = (X1 * 3038) >> 16;
X2 = (-7357 * P) >> 16;
P = P + ((X1 + X2 + (int32_t)3791) >> 4);
#ifdef DEBUG
printf("%s: P:= %ld\n", __FUNCTION__, P);
#endif
}
// Inform the user ...
if (!bmp180_informUser(command->resultQueue, command->cmd, ((bmp180_temp_t)T)/10.0, (bmp180_press_t)P))
{
// Failed to send info to user
printf("%s: Unable to inform user bmp180_informUser returned \"false\"\n", __FUNCTION__);
}
}
}
// Default user inform implementation
LOCAL bool bmp180_informUser_Impl(const xQueueHandle* resultQueue, uint8_t cmd, bmp180_temp_t temperatue, bmp180_press_t pressure)
{
bmp180_result_t result;
result.cmd = cmd;
result.temperatue = temperatue;
result.pressure = pressure;
return (xQueueSend(*resultQueue, &result, 0) == pdTRUE);
}
// Just init all needed queues
bool bmp180_init(uint8_t scl, uint8_t sda)
{
// 1. Create required queues
bool result = false;
if (bmp180_create_communication_queues())
{
// 2. Init i2c driver
i2c_init(scl, sda);
// 3. Check for bmp180 ...
if (bmp180_is_avaialble())
{
// 4. Init all internal constants ...
bmp180_fillInternalConstants();
// 5. Start driver task
if (bmp180_createTask())
{
// We are finished
result = true;
}
}
}
return result;
}
void bmp180_trigger_measurement(const xQueueHandle* resultQueue)
{
bmp180_command_t c;
c.cmd = BMP180_PRESSURE + BMP180_TEMPERATURE;
c.resultQueue = resultQueue;
xQueueSend(bmp180_rx_queue, &c, 0);
}
void bmp180_trigger_pressure_measurement(const xQueueHandle* resultQueue)
{
bmp180_command_t c;
c.cmd = BMP180_PRESSURE;
c.resultQueue = resultQueue;
xQueueSend(bmp180_rx_queue, &c, 0);
}
void bmp180_trigger_temperature_measurement(const xQueueHandle* resultQueue)
{
bmp180_command_t c;
c.cmd = BMP180_TEMPERATURE;
c.resultQueue = resultQueue;
xQueueSend(bmp180_rx_queue, &c, 0);
}