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 );
81#define WEATHER_MASTER_I2C 0
82#define WEATHER_MASTER_SPI 1
89#define WEATHER_OK 0
90#define WEATHER_ERROR -1
97#define WEATHER_DEF_CHIP_ID 0x60
98#define WEATHER_SOFT_RESET 0xB6
105#define WEATHER_I2C_ADDRESS_0 0x76
106#define WEATHER_I2C_ADDRESS_1 0x77
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
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
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
189#define WEATHER_STATUS_MEASURING 0x08
190#define WEATHER_STATUS_IM_UPDATE 0x01
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
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 )
233 // End group macro
234// --------------------------------------------------------------- PUBLIC TYPES
243typedef err_t ( *weather_master_io_t )( struct weather_s*, uint8_t, uint8_t*, uint8_t );
244
245typedef 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
273typedef struct
274{
275 uint32_t temperature;
276 uint32_t pressure;
277 uint32_t humidity;
278
280
284typedef struct weather_s
285{
286 // Modules
287 i2c_master_t i2c;
288 spi_master_t spi;
289
290 // ctx variable
292 pin_name_t chip_select;
295 uint8_t master_sel;
296
298
300
304typedef 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
325
329typedef struct
330{
332 float pressure;
333 float humidity;
334
336
340typedef 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
350 uint8_t filter;
351
353
354 // End types group
355// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
361#ifdef __cplusplus
362extern "C"{
363#endif
364
374
383
401
412err_t weather_generic_write ( weather_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
413
424err_t weather_generic_read ( weather_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
425
434err_t weather_get_device_id ( weather_t *ctx, uint8_t *device_id );
435
442
452
462
463#ifdef __cplusplus
464}
465#endif
466#endif // _WEATHER_H_
467
468 // End public_function group
470
471// ------------------------------------------------------------------------- END
err_t weather_generic_write(weather_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
err_t weather_generic_read(weather_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
err_t weather_get_device_id(weather_t *ctx, uint8_t *device_id)
Device ID.
err_t weather_software_reset(weather_t *ctx)
Software reset function.
err_t weather_get_ambient_data(weather_t *ctx, weather_data_t *weather_data)
Weather data.
void weather_cfg_setup(weather_cfg_t *cfg)
Config Object Initialization function.
err_t weather_default_cfg(weather_t *ctx)
Click Default Configuration function.
err_t weather_measurement_cfg(weather_t *ctx, weather_measurement_cfg_t *cfg)
Measurement config.
err_t weather_init(weather_t *ctx, weather_cfg_t *cfg)
Initialization function.
err_t(* weather_master_io_t)(struct weather_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition weather.h:243
struct weather_s weather_t
Click ctx object definition.
Click configuration structure definition.
Definition weather.h:305
uint32_t i2c_speed
Definition weather.h:315
spi_master_chip_select_polarity_t cs_polarity
Definition weather.h:320
pin_name_t sck
Definition weather.h:311
spi_master_mode_t spi_mode
Definition weather.h:319
pin_name_t mosi
Definition weather.h:310
uint32_t spi_speed
Definition weather.h:318
uint8_t sel
Definition weather.h:322
pin_name_t scl
Definition weather.h:307
pin_name_t miso
Definition weather.h:309
pin_name_t sda
Definition weather.h:308
pin_name_t cs
Definition weather.h:312
uint8_t i2c_address
Definition weather.h:316
Weather data structure definition.
Definition weather.h:330
float humidity
Definition weather.h:333
float pressure
Definition weather.h:332
float temperature
Definition weather.h:331
Definition weather.h:246
uint8_t hum_3
Definition weather.h:254
int16_t press_4
Definition weather.h:262
int16_t press_7
Definition weather.h:265
int16_t hum_4
Definition weather.h:255
int16_t hum_2
Definition weather.h:253
int8_t hum_6
Definition weather.h:257
int16_t press_3
Definition weather.h:261
int16_t hum_5
Definition weather.h:256
int16_t press_5
Definition weather.h:263
int16_t temp_3
Definition weather.h:250
int16_t press_6
Definition weather.h:264
int32_t t_fine
Definition weather.h:269
uint16_t press_1
Definition weather.h:259
uint16_t temp_1
Definition weather.h:248
int16_t temp_2
Definition weather.h:249
int16_t press_9
Definition weather.h:267
uint8_t hum_1
Definition weather.h:252
int16_t press_2
Definition weather.h:260
int16_t press_8
Definition weather.h:266
Measurement configuration structure definition.
Definition weather.h:341
uint8_t mode
Definition weather.h:346
uint8_t os_temp
Definition weather.h:343
uint8_t os_hum
Definition weather.h:345
uint8_t filter
Definition weather.h:350
uint8_t os_press
Definition weather.h:344
uint8_t standby_time
Definition weather.h:349
Definition weather.h:274
uint32_t pressure
Definition weather.h:276
uint32_t humidity
Definition weather.h:277
uint32_t temperature
Definition weather.h:275
Click ctx object definition.
Definition weather.h:285
weather_digital_data_t calib_params
Definition weather.h:297
weather_master_io_t write_f
Definition weather.h:293
spi_master_t spi
Definition weather.h:288
uint8_t master_sel
Definition weather.h:295
weather_master_io_t read_f
Definition weather.h:294
i2c_master_t i2c
Definition weather.h:287
uint8_t slave_address
Definition weather.h:291
pin_name_t chip_select
Definition weather.h:292