environment 2.0.0.0
environment.h
Go to the documentation of this file.
1/*
2 * MikroSDK - MikroE Software Development Kit
3 * Copyright (c) 2019, MikroElektronika - www.mikroe.com
4 * All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
33// ----------------------------------------------------------------------------
34
35#ifndef ENVIRONMENT_H
36#define ENVIRONMENT_H
37
42#ifdef PREINIT_SUPPORTED
43#include "preinit.h"
44#endif
45
46#ifdef MikroCCoreVersion
47 #if MikroCCoreVersion >= 1
48 #include "delays.h"
49 #endif
50#endif
51
52#include "drv_digital_out.h"
53#include "drv_digital_in.h"
54#include "drv_i2c_master.h"
55#include "drv_spi_master.h"
56
57// -------------------------------------------------------------- PUBLIC MACROS
68#define ENVIRONMENT_MAP_MIKROBUS( cfg, mikrobus ) \
69 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
70 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
71 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
72 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
73 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
74 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS )
81#define ENVIRONMENT_MASTER_I2C 0
82#define ENVIRONMENT_MASTER_SPI 1
89#define ENVIRONMENT_RETVAL uint8_t
90
91#define ENVIRONMENT_OK 0x00
92#define ENVIRONMENT_INIT_ERROR 0xFF
95#define ENVIRONMENT_POLL_PERIOD_MS 10
96
101#define ENVIRONMENT_DEVICE_SLAVE_ADDR_0 0x76
102#define ENVIRONMENT_DEVICE_SLAVE_ADDR_1 0x77
103#define ENVIRONMENT_CHIP_ID 0x61
104#define ENVIRONMENT_COEFF_SIZE 41
105#define ENVIRONMENT_COEFF_ADDR1_LEN 25
106#define ENVIRONMENT_COEFF_ADDR2_LEN 16
107#define ENVIRONMENT_FIELD_LENGTH 15
108#define ENVIRONMENT_FIELD_ADDR_OFFSET 17
109#define ENVIRONMENT_SOFT_RESET_CMD 0xB6
110#define ENVIRONMENT_DEVICE_OK 0
111#define ENVIRONMENT_DEVICE_ERROR 1
112#define ENVIRONMENT_E_NULL_PTR -1
113#define ENVIRONMENT_E_COM_FAIL -2
114#define ENVIRONMENT_E_DEV_NOT_FOUND -3
115#define ENVIRONMENT_E_INVALID_LENGTH -4
116#define ENVIRONMENT_W_DEFINE_PWR_MODE 1
117#define ENVIRONMENT_W_NO_NEW_DATA 2
118#define ENVIRONMENT_I_MIN_CORRECTION 1
119#define ENVIRONMENT_I_MAX_CORRECTION 2
126#define ENVIRONMENT_ADDR_RES_HEAT_VAL_ADDR 0x00
127#define ENVIRONMENT_ADDR_RES_HEAT_RANGE_ADDR 0x02
128#define ENVIRONMENT_ADDR_RANGE_SW_ERR_ADDR 0x04
129#define ENVIRONMENT_ADDR_SENS_CONF_START 0x5A
130#define ENVIRONMENT_ADDR_GAS_CONF_START 0x64
131#define ENVIRONMENT_FIELD0_ADDR 0x1d
132#define ENVIRONMENT_RES_HEAT0_ADDR 0x5a
133#define ENVIRONMENT_GAS_WAIT0_ADDR 0x64
134#define ENVIRONMENT_CONF_HEAT_CTRL_ADDR 0x70
135#define ENVIRONMENT_CONF_ODR_RUN_GAS_NBC_ADDR 0x71
136#define ENVIRONMENT_CONF_OS_H_ADDR 0x72
137#define ENVIRONMENT_MEM_PAGE_ADDR 0xf3
138#define ENVIRONMENT_CONF_T_P_MODE_ADDR 0x74
139#define ENVIRONMENT_CONF_ODR_FILT_ADDR 0x75
140#define ENVIRONMENT_COEFF_ADDR1 0x89
141#define ENVIRONMENT_COEFF_ADDR2 0xe1
142#define ENVIRONMENT_CHIP_ID_ADDR 0xd0
143#define ENVIRONMENT_SOFT_RESET_ADDR 0xe0
150#define ENVIRONMENT_ENABLE_HEATER 0x00
151#define ENVIRONMENT_DISABLE_HEATER 0x08
152#define ENVIRONMENT_DISABLE_GAS_MEAS 0x00
153#define ENVIRONMENT_ENABLE_GAS_MEAS 0x01
154#define ENVIRONMENT_OS_NONE 0
155#define ENVIRONMENT_OS_1X 1
156#define ENVIRONMENT_OS_2X 2
157#define ENVIRONMENT_OS_4X 3
158#define ENVIRONMENT_OS_8X 4
159#define ENVIRONMENT_OS_16X 5
160#define ENVIRONMENT_FILTER_SIZE_0 0
161#define ENVIRONMENT_FILTER_SIZE_1 1
162#define ENVIRONMENT_FILTER_SIZE_3 2
163#define ENVIRONMENT_FILTER_SIZE_7 3
164#define ENVIRONMENT_FILTER_SIZE_15 4
165#define ENVIRONMENT_FILTER_SIZE_31 5
166#define ENVIRONMENT_FILTER_SIZE_63 6
167#define ENVIRONMENT_FILTER_SIZE_127 7
168#define ENVIRONMENT_SLEEP_MODE 0
169#define ENVIRONMENT_FORCED_MODE 1
170#define ENVIRONMENT_RESET_PERIOD 10
171#define ENVIRONMENT_MEM_PAGE0 0x10
172#define ENVIRONMENT_MEM_PAGE1 0x00
173#define ENVIRONMENT_HUM_REG_SHIFT_VAL 4
174#define ENVIRONMENT_RUN_GAS_DISABLE 0
175#define ENVIRONMENT_RUN_GAS_ENABLE 1
176#define ENVIRONMENT_TMP_BUFFER_LENGTH 40
177#define ENVIRONMENT_REG_BUFFER_LENGTH 6
178#define ENVIRONMENT_FIELD_DATA_LENGTH 3
179#define ENVIRONMENT_GAS_REG_BUF_LENGTH 20
180#define ENVIRONMENT_OST_SEL 1
181#define ENVIRONMENT_OSP_SEL 2
182#define ENVIRONMENT_OSH_SEL 4
183#define ENVIRONMENT_GAS_MEAS_SEL 8
184#define ENVIRONMENT_FILTER_SEL 16
185#define ENVIRONMENT_HCNTRL_SEL 32
186#define ENVIRONMENT_RUN_GAS_SEL 64
187#define ENVIRONMENT_NBCONV_SEL 128
188#define ENVIRONMENT_GAS_SENSOR_SEL ( ENVIRONMENT_GAS_MEAS_SEL | \
189 ENVIRONMENT_RUN_GAS_SEL | \
190 ENVIRONMENT_NBCONV_SEL )
191#define ENVIRONMENT_NBCONV_MIN 0
192#define ENVIRONMENT_NBCONV_MAX 10
193#define ENVIRONMENT_GAS_MEAS_MSK 0x30
194#define ENVIRONMENT_NBCONV_MSK 0X0F
195#define ENVIRONMENT_FILTER_MSK 0X1C
196#define ENVIRONMENT_OST_MSK 0XE0
197#define ENVIRONMENT_OSP_MSK 0X1C
198#define ENVIRONMENT_OSH_MSK 0X07
199#define ENVIRONMENT_HCTRL_MSK 0x08
200#define ENVIRONMENT_RUN_GAS_MSK 0x10
201#define ENVIRONMENT_MODE_MSK 0x03
202#define ENVIRONMENT_RHRANGE_MSK 0x30
203#define ENVIRONMENT_RSERROR_MSK 0xf0
204#define ENVIRONMENT_NEW_DATA_MSK 0x80
205#define ENVIRONMENT_GAS_INDEX_MSK 0x0f
206#define ENVIRONMENT_GAS_RANGE_MSK 0x0f
207#define ENVIRONMENT_GASM_VALID_MSK 0x20
208#define ENVIRONMENT_HEAT_STAB_MSK 0x10
209#define ENVIRONMENT_MEM_PAGE_MSK 0x10
210#define ENVIRONMENT_SPI_RD_MSK 0x80
211#define ENVIRONMENT_SPI_WR_MSK 0x7f
212#define ENVIRONMENT_BIT_H1_DATA_MSK 0x0F
213#define ENVIRONMENT_GAS_MEAS_POS 4
214#define ENVIRONMENT_FILTER_POS 2
215#define ENVIRONMENT_OST_POS 5
216#define ENVIRONMENT_OSP_POS 2
217#define ENVIRONMENT_RUN_GAS_POS 4
218#define ENVIRONMENT_T2_LSB_REG 1
219#define ENVIRONMENT_T2_MSB_REG 2
220#define ENVIRONMENT_T3_REG 3
221#define ENVIRONMENT_P1_LSB_REG 5
222#define ENVIRONMENT_P1_MSB_REG 6
223#define ENVIRONMENT_P2_LSB_REG 7
224#define ENVIRONMENT_P2_MSB_REG 8
225#define ENVIRONMENT_P3_REG 9
226#define ENVIRONMENT_P4_LSB_REG 11
227#define ENVIRONMENT_P4_MSB_REG 12
228#define ENVIRONMENT_P5_LSB_REG 13
229#define ENVIRONMENT_P5_MSB_REG 14
230#define ENVIRONMENT_P7_REG 15
231#define ENVIRONMENT_P6_REG 16
232#define ENVIRONMENT_P8_LSB_REG 19
233#define ENVIRONMENT_P8_MSB_REG 20
234#define ENVIRONMENT_P9_LSB_REG 21
235#define ENVIRONMENT_P9_MSB_REG 22
236#define ENVIRONMENT_P10_REG 23
237#define ENVIRONMENT_H2_MSB_REG 25
238#define ENVIRONMENT_H2_LSB_REG 26
239#define ENVIRONMENT_H1_LSB_REG 26
240#define ENVIRONMENT_H1_MSB_REG 27
241#define ENVIRONMENT_H3_REG 28
242#define ENVIRONMENT_H4_REG 29
243#define ENVIRONMENT_H5_REG 30
244#define ENVIRONMENT_H6_REG 31
245#define ENVIRONMENT_H7_REG 32
246#define ENVIRONMENT_T1_LSB_REG 33
247#define ENVIRONMENT_T1_MSB_REG 34
248#define ENVIRONMENT_GH2_LSB_REG 35
249#define ENVIRONMENT_GH2_MSB_REG 36
250#define ENVIRONMENT_GH1_REG 37
251#define ENVIRONMENT_GH3_REG 38
252#define ENVIRONMENT_REG_FILTER_INDEX 5
253#define ENVIRONMENT_REG_TEMP_INDEX 4
254#define ENVIRONMENT_REG_PRES_INDEX 4
255#define ENVIRONMENT_REG_HUM_INDEX 2
256#define ENVIRONMENT_REG_NBCONV_INDEX 1
257#define ENVIRONMENT_REG_RUN_GAS_INDEX 1
258#define ENVIRONMENT_REG_HCTRL_INDEX 0
259#define ENVIRONMENT_MAX_OVERFLOW_VAL 0x40000000
260#define ENVIRONMENT_DATA_TEMPERATURE 0x01
261#define ENVIRONMENT_DATA_PRESSURE 0x02
262#define ENVIRONMENT_DATA_HUMIDITY 0x03
263#define BME680_CONCAT_BYTES(msb, lsb) (((uint16_t)msb << 8) | (uint16_t)lsb)
264#define BME680_SET_BITS(reg_data, bitname, data) \
265 ((reg_data & ~(bitname##_MSK)) | \
266 ((data << bitname##_POS) & bitname##_MSK))
267#define BME680_SET_BITS_POS_0(reg_data, bitname, data) \
268 ((reg_data & ~(bitname##_MSK)) | \
269 (data & bitname##_MSK))
270
271 // End group macro
272// --------------------------------------------------------------- PUBLIC TYPES
278typedef struct
279{
280 uint8_t status;
281 uint8_t gas_index;
282 uint8_t meas_index;
283 int16_t temperature;
284 float pressure;
285 uint32_t humidity;
287
289
290typedef struct
291{
292 uint16_t par_h1;
293 uint16_t par_h2;
294 int8_t par_h3;
295 int8_t par_h4;
296 int8_t par_h5;
297 uint8_t par_h6;
298 int8_t par_h7;
299 int8_t par_gh1;
300 int16_t par_gh2;
301 int8_t par_gh3;
302 uint16_t par_t1;
303 int16_t par_t2;
304 int8_t par_t3;
305 uint16_t par_p1;
306 int16_t par_p2;
307 int8_t par_p3;
308 int16_t par_p4;
309 int16_t par_p5;
310 int8_t par_p6;
311 int8_t par_p7;
312 int16_t par_p8;
313 int16_t par_p9;
314 uint8_t par_p10;
315 int32_t t_fine;
319
321
322typedef struct
323{
324 uint8_t os_hum;
325 uint8_t os_temp;
326 uint8_t os_pres;
327 uint8_t filter;
328
330
331
332typedef struct
333{
334 uint8_t nb_conv;
335 uint8_t heatr_ctrl;
336 uint8_t run_gas;
337 uint16_t heatr_temp;
338 uint16_t heatr_dur;
339
341
345typedef uint8_t environment_select_t;
346
350typedef void ( *environment_master_io_t )( struct environment_s*, uint8_t, uint8_t*, uint8_t );
351
381
385typedef struct
386{
387 // Communication gpio pins
388
389 pin_name_t scl;
390 pin_name_t sda;
391 pin_name_t miso;
392 pin_name_t mosi;
393 pin_name_t sck;
394 pin_name_t cs;
395
396 // static variable
397
398 uint32_t i2c_speed;
399 uint8_t i2c_address;
400
401 uint32_t spi_speed;
402 spi_master_mode_t spi_mode;
403 spi_master_chip_select_polarity_t cs_polarity;
404
406
408
409 // End types group
410
411// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
417#ifdef __cplusplus
418extern "C"{
419#endif
420
430
439
449
460void environment_generic_write ( environment_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
461
472void environment_generic_read ( environment_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
473
486void environment_write_byte ( environment_t *ctx, uint8_t reg_address, uint8_t write_data );
487
501uint8_t environment_read_byte ( environment_t *ctx, uint8_t reg_address );
502
517
533
544
559
574
590float environment_get_ambient_data ( environment_t *ctx, uint8_t data_in );
591
605
619
632
645
646#ifdef __cplusplus
647}
648#endif
649#endif // _ENVIRONMENT_H_
650
651 // End public_function group
653
654// ------------------------------------------------------------------------- END
#define ENVIRONMENT_RETVAL
Definition environment.h:89
uint32_t environment_get_gas_resistance(environment_t *ctx)
Gets gas resistance data function.
void environment_generic_write(environment_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
float environment_get_pressure(environment_t *ctx)
Gets pressure in degrees Celsius function.
void environment_default_config(environment_t *ctx)
Set default configuration for the BMP680 sensor function.
float environment_get_humidity(environment_t *ctx)
Gets humidity in percentage [ % ] function.
void environment_default_cfg(environment_t *ctx)
Click Default Configuration function.
void environment_soft_reset(environment_t *ctx)
Software reset function.
uint8_t environment_read_byte(environment_t *ctx, uint8_t reg_address)
Generic the read byte of data function.
void environment_write_byte(environment_t *ctx, uint8_t reg_address, uint8_t write_data)
Generic the write byte of data function.
uint8_t environment_get_device_id(environment_t *ctx)
Get device ID function.
void environment_get_calibration_data(environment_t *ctx)
Get factory calibration parameters function.
uint8_t environment_check_id(environment_t *ctx)
Check device ID function.
void environment_cfg_setup(environment_cfg_t *cfg)
Config Object Initialization function.
void environment_generic_read(environment_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
float environment_get_temperature(environment_t *ctx)
Gets temperature in degrees Celsius function.
ENVIRONMENT_RETVAL environment_init(environment_t *ctx, environment_cfg_t *cfg)
Initialization function.
float environment_get_ambient_data(environment_t *ctx, uint8_t data_in)
Get ambient data function.
uint8_t environment_select_t
Communication type.
Definition environment.h:345
void(* environment_master_io_t)(struct environment_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition environment.h:350
struct environment_s environment_t
Click ctx object definition.
Definition environment.h:291
int16_t par_p4
Definition environment.h:308
int8_t par_h5
Definition environment.h:296
int8_t par_p6
Definition environment.h:310
uint16_t par_p1
Definition environment.h:305
int8_t par_h3
Definition environment.h:294
uint16_t par_h1
Definition environment.h:292
int8_t par_gh1
Definition environment.h:299
int8_t par_h4
Definition environment.h:295
int16_t par_p2
Definition environment.h:306
int8_t par_p3
Definition environment.h:307
uint8_t par_p10
Definition environment.h:314
int16_t par_gh2
Definition environment.h:300
uint8_t res_heat_range
Definition environment.h:316
int8_t par_h7
Definition environment.h:298
uint16_t par_h2
Definition environment.h:293
int8_t res_heat_val
Definition environment.h:317
int8_t par_gh3
Definition environment.h:301
int16_t par_p8
Definition environment.h:312
uint16_t par_t1
Definition environment.h:302
int8_t range_sw_err
Definition environment.h:318
int16_t par_p9
Definition environment.h:313
int32_t t_fine
Definition environment.h:315
int16_t par_t2
Definition environment.h:303
int8_t par_p7
Definition environment.h:311
int16_t par_p5
Definition environment.h:309
int8_t par_t3
Definition environment.h:304
uint8_t par_h6
Definition environment.h:297
Definition environment.h:279
int16_t temperature
Definition environment.h:283
uint32_t humidity
Definition environment.h:285
uint32_t gas_resistance
Definition environment.h:286
float pressure
Definition environment.h:284
uint8_t gas_index
Definition environment.h:281
uint8_t meas_index
Definition environment.h:282
uint8_t status
Definition environment.h:280
Definition environment.h:333
uint8_t nb_conv
Definition environment.h:334
uint8_t run_gas
Definition environment.h:336
uint16_t heatr_temp
Definition environment.h:337
uint8_t heatr_ctrl
Definition environment.h:335
uint16_t heatr_dur
Definition environment.h:338
Definition environment.h:323
uint8_t os_temp
Definition environment.h:325
uint8_t os_hum
Definition environment.h:324
uint8_t filter
Definition environment.h:327
uint8_t os_pres
Definition environment.h:326
Click configuration structure definition.
Definition environment.h:386
environment_select_t sel
Definition environment.h:405
uint32_t i2c_speed
Definition environment.h:398
spi_master_chip_select_polarity_t cs_polarity
Definition environment.h:403
pin_name_t sck
Definition environment.h:393
spi_master_mode_t spi_mode
Definition environment.h:402
pin_name_t mosi
Definition environment.h:392
uint32_t spi_speed
Definition environment.h:401
pin_name_t scl
Definition environment.h:389
pin_name_t miso
Definition environment.h:391
pin_name_t sda
Definition environment.h:390
pin_name_t cs
Definition environment.h:394
uint8_t i2c_address
Definition environment.h:399
Click ctx object definition.
Definition environment.h:356
digital_out_t cs
Definition environment.h:357
spi_master_t spi
Definition environment.h:362
T_ENVIRONMENT_BME680_CALIB_DATA calib
Definition environment.h:373
environment_select_t master_sel
Definition environment.h:370
environment_master_io_t read_f
Definition environment.h:369
i2c_master_t i2c
Definition environment.h:361
uint8_t device_power_mode
Definition environment.h:378
T_ENVIRONMENT_BME680_FIELD_DATA d_data
Definition environment.h:372
uint8_t slave_address
Definition environment.h:366
environment_master_io_t write_f
Definition environment.h:368
T_ENVIRONMENT_BME680_TPH_SETTINGS tph_sett
Definition environment.h:374
T_ENVIRONMENT_BME680_GAS_SETTINGS gas_sett
Definition environment.h:375
pin_name_t chip_select
Definition environment.h:367
uint8_t device_mem_page
Definition environment.h:377