weather  2.0.0.0
weather.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef WEATHER_H
36 #define WEATHER_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 WEATHER_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 );
75 
81 #define WEATHER_MASTER_I2C 0
82 #define WEATHER_MASTER_SPI 1
83 
89 #define WEATHER_OK 0
90 #define WEATHER_ERROR -1
91 
97 #define WEATHER_DEF_CHIP_ID 0x60
98 #define WEATHER_SOFT_RESET 0xB6
99 
105 #define WEATHER_I2C_ADDRESS_0 0x76
106 #define WEATHER_I2C_ADDRESS_1 0x77
107 
113 #define WEATHER_REG_CHIP_ID 0xD0
114 #define WEATHER_REG_RESET 0xE0
115 #define WEATHER_REG_STATUS 0xF3
116 #define WEATHER_REG_CTRL_MEASUREMENT 0xF4
117 #define WEATHER_REG_CTRL_HUMIDITY 0xF2
118 #define WEATHER_REG_CONFIG 0xF5
119 #define WEATHER_REG_RAW_DATA 0xF7
120 #define WEATHER_REG_PRESSURE_MSB 0xF7
121 #define WEATHER_REG_PRESSURE_LSB 0xF8
122 #define WEATHER_REG_PRESSURE_XLSB 0xF9
123 #define WEATHER_REG_TEMPERATURE_MSB 0xFA
124 #define WEATHER_REG_TEMPERATURE_LSB 0xFB
125 #define WEATHER_REG_TEMPERATURE_XLSB 0xFC
126 #define WEATHER_REG_HUMIDITY_MSB 0xFD
127 #define WEATHER_REG_HUMIDITY_LSB 0xFE
128 
134 #define WEATHER_REG_CALIB_TEMPERATURE 0x88
135 #define WEATHER_REG_CALIB_TEMP_1_LSB 0x88
136 #define WEATHER_REG_CALIB_TEMP_1_MSB 0x89
137 #define WEATHER_REG_CALIB_TEMP_2_LSB 0x8A
138 #define WEATHER_REG_CALIB_TEMP_2_MSB 0x8B
139 #define WEATHER_REG_CALIB_TEMP_3_LSB 0x8C
140 #define WEATHER_REG_CALIB_TEMP_3_MSB 0x8D
141 
142 #define WEATHER_REG_CALIB_PRESSURE 0x8E
143 #define WEATHER_REG_CALIB_PRESS_1_LSB 0x8E
144 #define WEATHER_REG_CALIB_PRESS_1_MSB 0x8F
145 #define WEATHER_REG_CALIB_PRESS_2_LSB 0x90
146 #define WEATHER_REG_CALIB_PRESS_2_MSB 0x91
147 #define WEATHER_REG_CALIB_PRESS_3_LSB 0x92
148 #define WEATHER_REG_CALIB_PRESS_3_MSB 0x93
149 #define WEATHER_REG_CALIB_PRESS_4_LSB 0x94
150 #define WEATHER_REG_CALIB_PRESS_4_MSB 0x95
151 #define WEATHER_REG_CALIB_PRESS_5_LSB 0x96
152 #define WEATHER_REG_CALIB_PRESS_5_MSB 0x97
153 #define WEATHER_REG_CALIB_PRESS_6_LSB 0x98
154 #define WEATHER_REG_CALIB_PRESS_6_MSB 0x99
155 #define WEATHER_REG_CALIB_PRESS_7_LSB 0x9A
156 #define WEATHER_REG_CALIB_PRESS_7_MSB 0x9B
157 #define WEATHER_REG_CALIB_PRESS_8_LSB 0x9C
158 #define WEATHER_REG_CALIB_PRESS_8_MSB 0x9D
159 #define WEATHER_REG_CALIB_PRESS_9_LSB 0x9E
160 #define WEATHER_REG_CALIB_PRESS_9_MSB 0x9F
161 
162 #define WEATHER_REG_CALIB_HUMIDITY 0xA1
163 #define WEATHER_REG_CALIB_HUM_1 0xA1
164 #define WEATHER_REG_CALIB_HUM_2_LSB 0xE1
165 #define WEATHER_REG_CALIB_HUM_2_MSB 0xE2
166 #define WEATHER_REG_CALIB_HUM_3 0xE3
167 #define WEATHER_REG_CALIB_HUM_4_MSB 0xE4
168 #define WEATHER_REG_CALIB_HUM_4_LSB 0xE5
169 #define WEATHER_REG_CALIB_HUM_5_MSB 0xE6
170 #define WEATHER_REG_CALIB_HUM_6 0xE7
171 
177 #define WEATHER_HUM_OS_SKIPPED 0x00
178 #define WEATHER_HUM_OS_1X 0x01
179 #define WEATHER_HUM_OS_2X 0x02
180 #define WEATHER_HUM_OS_4X 0x03
181 #define WEATHER_HUM_OS_8X 0x04
182 #define WEATHER_HUM_OS_16X 0x05
183 
189 #define WEATHER_STATUS_MEASURING 0x08
190 #define WEATHER_STATUS_IM_UPDATE 0x01
191 
197 #define WEATHER_MCTRL_PRESS_OS_SKIPPED 0x00;
198 #define WEATHER_MCTRL_PRESS_OS_1X ( 0x01<<2 )
199 #define WEATHER_MCTRL_PRESS_OS_2X ( 0x02<<2 )
200 #define WEATHER_MCTRL_PRESS_OS_4X ( 0x03<<2 )
201 #define WEATHER_MCTRL_PRESS_OS_8X ( 0x04<<2 )
202 #define WEATHER_MCTRL_PRESS_OS_16X ( 0x05<<2 )
203 #define WEATHER_MCTRL_TEMP_OS_SKIPPED 0x00
204 #define WEATHER_MCTRL_TEMP_OS_1X ( 0x01<<5 )
205 #define WEATHER_MCTRL_TEMP_OS_2X ( 0x02<<5 )
206 #define WEATHER_MCTRL_TEMP_OS_4X ( 0x03<<5 )
207 #define WEATHER_MCTRL_TEMP_OS_8X ( 0x04<<5 )
208 #define WEATHER_MCTRL_TEMP_OS_16X ( 0x05<<5 )
209 #define WEATHER_MCTRL_MODE_SLEEP 0x00
210 #define WEATHER_MCTRL_MODE_FORCED 0x01
211 #define WEATHER_MCTRL_MODE_NORMAL 0x03
212 
218 #define WEATHER_CFG_STANDBY_TIME_0p5ms 0x00
219 #define WEATHER_CFG_STANDBY_TIME_62p5ms ( 0x01<<5 )
220 #define WEATHER_CFG_STANDBY_TIME_125ms ( 0x02<<5 )
221 #define WEATHER_CFG_STANDBY_TIME_250ms ( 0x03<<5 )
222 #define WEATHER_CFG_STANDBY_TIME_500ms ( 0x04<<5 )
223 #define WEATHER_CFG_STANDBY_TIME_1000ms ( 0x05<<5 )
224 #define WEATHER_CFG_STANDBY_TIME_10ms ( 0x06<<5 )
225 #define WEATHER_CFG_STANDBY_TIME_20ms ( 0x07<<5 )
226 #define WEATHER_CFG_FILTER_OFF 0x00
227 #define WEATHER_CFG_FILTER_2 ( 0x01<<2 )
228 #define WEATHER_CFG_FILTER_4 ( 0x02<<2 )
229 #define WEATHER_CFG_FILTER_8 ( 0x03<<2 )
230 #define WEATHER_CFG_FILTER_16 ( 0x04<<2 )
231  // End group macro
234 // --------------------------------------------------------------- PUBLIC TYPES
243 typedef err_t ( *weather_master_io_t )( struct weather_s*, uint8_t, uint8_t*, uint8_t );
244 
245 typedef struct
246 {
247  // Temperature digital value
248  uint16_t temp_1;
249  int16_t temp_2;
250  int16_t temp_3;
251  // Humidity digital value
252  uint8_t hum_1;
253  int16_t hum_2;
254  uint8_t hum_3;
255  int16_t hum_4;
256  int16_t hum_5;
257  int8_t hum_6;
258  // Pressure digital value
259  uint16_t press_1;
260  int16_t press_2;
261  int16_t press_3;
262  int16_t press_4;
263  int16_t press_5;
264  int16_t press_6;
265  int16_t press_7;
266  int16_t press_8;
267  int16_t press_9;
268 
269  int32_t t_fine;
270 
272 
273 typedef struct
274 {
275  uint32_t temperature;
276  uint32_t pressure;
277  uint32_t humidity;
278 
280 
284 typedef struct weather_s
285 {
286  // Modules
287  i2c_master_t i2c;
288  spi_master_t spi;
289 
290  // ctx variable
291  uint8_t slave_address;
292  pin_name_t chip_select;
295  uint8_t master_sel;
296 
298 
300 
304 typedef struct
305 {
306  // Communication gpio pins
307  pin_name_t scl;
308  pin_name_t sda;
309  pin_name_t miso;
310  pin_name_t mosi;
311  pin_name_t sck;
312  pin_name_t cs;
313 
314  // static variable
315  uint32_t i2c_speed;
316  uint8_t i2c_address;
317 
318  uint32_t spi_speed;
319  spi_master_mode_t spi_mode;
320  spi_master_chip_select_polarity_t cs_polarity;
321 
322  uint8_t sel;
323 
324 } weather_cfg_t;
325 
329 typedef struct
330 {
331  float temperature;
332  float pressure;
333  float humidity;
334 
336 
340 typedef struct
341 {
342  // CTRL register
343  uint8_t os_temp;
344  uint8_t os_press;
345  uint8_t os_hum;
346  uint8_t mode;
347 
348  // CFG register
349  uint8_t standby_time;
350  uint8_t filter;
351 
353  // End types group
355 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
361 #ifdef __cplusplus
362 extern "C"{
363 #endif
364 
374 
382 err_t weather_init ( weather_t *ctx, weather_cfg_t *cfg );
383 
401 
412 err_t weather_generic_write ( weather_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
413 
424 err_t weather_generic_read ( weather_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
425 
434 err_t weather_get_device_id ( weather_t *ctx, uint8_t *device_id );
435 
442 
451 err_t weather_get_ambient_data( weather_t *ctx, weather_data_t *weather_data );
452 
462 
463 #ifdef __cplusplus
464 }
465 #endif
466 #endif // _WEATHER_H_
467  // End public_function group
470 
471 // ------------------------------------------------------------------------- END
weather_digital_data_t
Definition: weather.h:246
weather_digital_data_t::press_9
int16_t press_9
Definition: weather.h:267
weather_measurement_cfg_t::os_press
uint8_t os_press
Definition: weather.h:344
weather_digital_data_t::temp_3
int16_t temp_3
Definition: weather.h:250
weather_generic_read
err_t weather_generic_read(weather_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
weather_digital_data_t::hum_1
uint8_t hum_1
Definition: weather.h:252
weather_measurement_cfg_t::standby_time
uint8_t standby_time
Definition: weather.h:349
weather_digital_data_t::temp_1
uint16_t temp_1
Definition: weather.h:248
weather_s::write_f
weather_master_io_t write_f
Definition: weather.h:293
weather_digital_data_t::t_fine
int32_t t_fine
Definition: weather.h:269
weather_data_t::pressure
float pressure
Definition: weather.h:332
weather_data_t::temperature
float temperature
Definition: weather.h:331
weather_measurement_cfg_t::os_temp
uint8_t os_temp
Definition: weather.h:343
weather_get_ambient_data
err_t weather_get_ambient_data(weather_t *ctx, weather_data_t *weather_data)
Weather data.
weather_master_io_t
err_t(* weather_master_io_t)(struct weather_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: weather.h:243
weather_digital_data_t::press_1
uint16_t press_1
Definition: weather.h:259
weather_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: weather.h:315
weather_digital_data_t::press_8
int16_t press_8
Definition: weather.h:266
weather_digital_data_t::press_3
int16_t press_3
Definition: weather.h:261
weather_raw_data_t::temperature
uint32_t temperature
Definition: weather.h:275
weather_cfg_t::cs
pin_name_t cs
Definition: weather.h:312
weather_cfg_t::sda
pin_name_t sda
Definition: weather.h:308
weather_cfg_t::scl
pin_name_t scl
Definition: weather.h:307
weather_data_t::humidity
float humidity
Definition: weather.h:333
weather_s::read_f
weather_master_io_t read_f
Definition: weather.h:294
weather_measurement_cfg_t
Measurement configuration structure definition.
Definition: weather.h:341
weather_t
struct weather_s weather_t
Click ctx object definition.
weather_cfg_t::i2c_address
uint8_t i2c_address
Definition: weather.h:316
weather_default_cfg
err_t weather_default_cfg(weather_t *ctx)
Click Default Configuration function.
weather_cfg_setup
void weather_cfg_setup(weather_cfg_t *cfg)
Config Object Initialization function.
weather_software_reset
err_t weather_software_reset(weather_t *ctx)
Software reset function.
weather_s::slave_address
uint8_t slave_address
Definition: weather.h:291
weather_s::i2c
i2c_master_t i2c
Definition: weather.h:287
weather_measurement_cfg
err_t weather_measurement_cfg(weather_t *ctx, weather_measurement_cfg_t *cfg)
Measurement config.
weather_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: weather.h:319
weather_s::chip_select
pin_name_t chip_select
Definition: weather.h:292
weather_cfg_t::miso
pin_name_t miso
Definition: weather.h:309
weather_s::spi
spi_master_t spi
Definition: weather.h:288
weather_cfg_t
Click configuration structure definition.
Definition: weather.h:305
weather_digital_data_t::hum_5
int16_t hum_5
Definition: weather.h:256
weather_digital_data_t::press_7
int16_t press_7
Definition: weather.h:265
weather_cfg_t::spi_speed
uint32_t spi_speed
Definition: weather.h:318
weather_generic_write
err_t weather_generic_write(weather_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
weather_get_device_id
err_t weather_get_device_id(weather_t *ctx, uint8_t *device_id)
Device ID.
weather_digital_data_t::press_6
int16_t press_6
Definition: weather.h:264
weather_measurement_cfg_t::filter
uint8_t filter
Definition: weather.h:350
weather_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: weather.h:320
weather_s::calib_params
weather_digital_data_t calib_params
Definition: weather.h:297
weather_digital_data_t::press_2
int16_t press_2
Definition: weather.h:260
weather_measurement_cfg_t::mode
uint8_t mode
Definition: weather.h:346
weather_measurement_cfg_t::os_hum
uint8_t os_hum
Definition: weather.h:345
weather_init
err_t weather_init(weather_t *ctx, weather_cfg_t *cfg)
Initialization function.
weather_digital_data_t::hum_2
int16_t hum_2
Definition: weather.h:253
weather_raw_data_t::humidity
uint32_t humidity
Definition: weather.h:277
weather_s::master_sel
uint8_t master_sel
Definition: weather.h:295
weather_digital_data_t::hum_3
uint8_t hum_3
Definition: weather.h:254
weather_raw_data_t
Definition: weather.h:274
weather_digital_data_t::hum_4
int16_t hum_4
Definition: weather.h:255
weather_s
Click ctx object definition.
Definition: weather.h:285
weather_digital_data_t::hum_6
int8_t hum_6
Definition: weather.h:257
weather_cfg_t::sel
uint8_t sel
Definition: weather.h:322
weather_raw_data_t::pressure
uint32_t pressure
Definition: weather.h:276
weather_data_t
Weather data structure definition.
Definition: weather.h:330
weather_digital_data_t::temp_2
int16_t temp_2
Definition: weather.h:249
weather_cfg_t::mosi
pin_name_t mosi
Definition: weather.h:310
weather_digital_data_t::press_5
int16_t press_5
Definition: weather.h:263
weather_digital_data_t::press_4
int16_t press_4
Definition: weather.h:262
weather_cfg_t::sck
pin_name_t sck
Definition: weather.h:311