-
Notifications
You must be signed in to change notification settings - Fork 9
/
scd4x_i2c.c
454 lines (382 loc) · 13.6 KB
/
scd4x_i2c.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* THIS FILE IS AUTOMATICALLY GENERATED AND MUST NOT BE EDITED MANUALLY!
*
* I2C-Generator: 0.2.0
* Yaml Version: 0.1.1
* Template Version: 0.2.1-12-g7a8bea5
*/
/*
* Copyright (c) 2021, Sensirion AG
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Sensirion AG nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "scd4x_i2c.h"
#include "sensirion_common.h"
#include "sensirion_i2c.h"
#include "sensirion_i2c_hal.h"
#define SCD4X_I2C_ADDRESS 0x62
int16_t scd4x_start_periodic_measurement() {
int16_t error;
uint8_t buffer[2];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x21B1);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(1000);
return NO_ERROR;
}
int16_t scd4x_read_measurement_ticks(uint16_t* co2, uint16_t* temperature,
uint16_t* humidity) {
int16_t error;
uint8_t buffer[9];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0xEC05);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(1000);
error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 6);
if (error) {
return error;
}
*co2 = sensirion_common_bytes_to_uint16_t(&buffer[0]);
*temperature = sensirion_common_bytes_to_uint16_t(&buffer[2]);
*humidity = sensirion_common_bytes_to_uint16_t(&buffer[4]);
return NO_ERROR;
}
int16_t scd4x_read_measurement(uint16_t* co2, float* temperature_deg_c,
float* humidity_percent_rh) {
int16_t error;
uint16_t temperature;
uint16_t humidity;
error = scd4x_read_measurement_ticks(co2, &temperature, &humidity);
if (error) {
return error;
}
*temperature_deg_c = (float)temperature * 175.0f / 65535.0f - 45.0f;
*humidity_percent_rh = (float)humidity * 100.0f / 65535.0f;
return NO_ERROR;
}
int16_t scd4x_stop_periodic_measurement() {
int16_t error;
uint8_t buffer[2];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x3F86);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(500000);
return NO_ERROR;
}
int16_t scd4x_get_temperature_offset_ticks(uint16_t* t_offset) {
int16_t error;
uint8_t buffer[3];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x2318);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(1000);
error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 2);
if (error) {
return error;
}
*t_offset = sensirion_common_bytes_to_uint16_t(&buffer[0]);
return NO_ERROR;
}
int16_t scd4x_get_temperature_offset(float* t_offset_deg_c) {
int16_t error;
uint16_t t_offset;
error = scd4x_get_temperature_offset_ticks(&t_offset);
if (error) {
return error;
}
*t_offset_deg_c = (float)t_offset * 175.0f / 65536.0f;
return NO_ERROR;
}
int16_t scd4x_set_temperature_offset_ticks(uint16_t t_offset) {
int16_t error;
uint8_t buffer[5];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x241D);
offset = sensirion_i2c_add_uint16_t_to_buffer(&buffer[0], offset, t_offset);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(1000);
return NO_ERROR;
}
int16_t scd4x_set_temperature_offset(float t_offset_deg_c) {
uint16_t t_offset = (uint16_t)(t_offset_deg_c * 65536.0f / 175.0f + 0.5f);
return scd4x_set_temperature_offset_ticks(t_offset);
}
int16_t scd4x_get_sensor_altitude(uint16_t* sensor_altitude) {
int16_t error;
uint8_t buffer[3];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x2322);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(1000);
error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 2);
if (error) {
return error;
}
*sensor_altitude = sensirion_common_bytes_to_uint16_t(&buffer[0]);
return NO_ERROR;
}
int16_t scd4x_set_sensor_altitude(uint16_t sensor_altitude) {
int16_t error;
uint8_t buffer[5];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x2427);
offset = sensirion_i2c_add_uint16_t_to_buffer(&buffer[0], offset,
sensor_altitude);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(1000);
return NO_ERROR;
}
int16_t scd4x_set_ambient_pressure(uint16_t ambient_pressure) {
int16_t error;
uint8_t buffer[5];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0xE000);
offset = sensirion_i2c_add_uint16_t_to_buffer(&buffer[0], offset,
ambient_pressure);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(1000);
return NO_ERROR;
}
int16_t scd4x_perform_forced_recalibration(uint16_t target_co2_concentration,
uint16_t* frc_correction) {
int16_t error;
uint8_t buffer[5];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x362F);
offset = sensirion_i2c_add_uint16_t_to_buffer(&buffer[0], offset,
target_co2_concentration);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(400000);
error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 2);
if (error) {
return error;
}
*frc_correction = sensirion_common_bytes_to_uint16_t(&buffer[0]);
return NO_ERROR;
}
int16_t scd4x_get_automatic_self_calibration(uint16_t* asc_enabled) {
int16_t error;
uint8_t buffer[3];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x2313);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(1000);
error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 2);
if (error) {
return error;
}
*asc_enabled = sensirion_common_bytes_to_uint16_t(&buffer[0]);
return NO_ERROR;
}
int16_t scd4x_set_automatic_self_calibration(uint16_t asc_enabled) {
int16_t error;
uint8_t buffer[5];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x2416);
offset =
sensirion_i2c_add_uint16_t_to_buffer(&buffer[0], offset, asc_enabled);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(1000);
return NO_ERROR;
}
int16_t scd4x_start_low_power_periodic_measurement() {
uint8_t buffer[2];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x21AC);
return sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
}
int16_t scd4x_get_data_ready_flag(bool* data_ready_flag) {
int16_t error;
uint8_t buffer[3];
uint16_t offset = 0;
uint16_t local_data_ready = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0xE4B8);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(1000);
error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 2);
if (error) {
return error;
}
local_data_ready = sensirion_common_bytes_to_uint16_t(&buffer[0]);
*data_ready_flag = (local_data_ready & 0x7FF) != 0;
return NO_ERROR;
}
int16_t scd4x_persist_settings() {
int16_t error;
uint8_t buffer[2];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x3615);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(800000);
return NO_ERROR;
}
int16_t scd4x_get_serial_number(uint16_t* serial_0, uint16_t* serial_1,
uint16_t* serial_2) {
int16_t error;
uint8_t buffer[9];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x3682);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(1000);
error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 6);
if (error) {
return error;
}
*serial_0 = sensirion_common_bytes_to_uint16_t(&buffer[0]);
*serial_1 = sensirion_common_bytes_to_uint16_t(&buffer[2]);
*serial_2 = sensirion_common_bytes_to_uint16_t(&buffer[4]);
return NO_ERROR;
}
int16_t scd4x_perform_self_test(uint16_t* sensor_status) {
int16_t error;
uint8_t buffer[3];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x3639);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(10000000);
error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 2);
if (error) {
return error;
}
*sensor_status = sensirion_common_bytes_to_uint16_t(&buffer[0]);
return NO_ERROR;
}
int16_t scd4x_perform_factory_reset() {
int16_t error;
uint8_t buffer[2];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x3632);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(800000);
return NO_ERROR;
}
int16_t scd4x_reinit() {
int16_t error;
uint8_t buffer[2];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x3646);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(20000);
return NO_ERROR;
}
int16_t scd4x_measure_single_shot() {
int16_t error;
uint8_t buffer[2];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x219D);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(5000000);
return NO_ERROR;
}
int16_t scd4x_measure_single_shot_rht_only() {
int16_t error;
uint8_t buffer[2];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x2196);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(50000);
return NO_ERROR;
}
int16_t scd4x_power_down() {
int16_t error;
uint8_t buffer[2];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x36E0);
error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
if (error) {
return error;
}
sensirion_i2c_hal_sleep_usec(1000);
return NO_ERROR;
}
int16_t scd4x_wake_up() {
uint8_t buffer[2];
uint16_t offset = 0;
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x36F6);
// Sensor does not acknowledge the wake-up call, error is ignored
(void)sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset);
sensirion_i2c_hal_sleep_usec(20000);
return NO_ERROR;
}