weather  2.0.0.0
weather.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 WEATHER_H
36 #define WEATHER_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 #include "drv_spi_master.h"
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
54 #define WEATHER_MAP_MIKROBUS( cfg, mikrobus ) \
55  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
56  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
57  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
58  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
59  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
60  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS );
61 
67 #define WEATHER_MASTER_I2C 0
68 #define WEATHER_MASTER_SPI 1
69 
75 #define WEATHER_RETVAL uint8_t
76 
77 #define WEATHER_OK 0x00
78 #define WEATHER_INIT_ERROR 0xFF
79 
85 #define WEATHER_DEF_CHIP_ID 0x60
86 #define WEATHER_SOFT_RESET 0xB6
87 
93 #define WEATHER_I2C_ADDRESS_0 0x76
94 #define WEATHER_I2C_ADDRESS_1 0x77
95 
101 #define WEATHER_REG_CHIP_ID 0xD0
102 #define WEATHER_REG_RESET 0xE0
103 #define WEATHER_REG_STATUS 0xF3
104 #define WEATHER_REG_CTRL_MEASUREMENT 0xF4
105 #define WEATHER_REG_CTRL_HUMIDITY 0xF2
106 #define WEATHER_REG_CONFIG 0xF5
107 #define WEATHER_REG_RAW_DATA 0xF7
108 #define WEATHER_REG_PRESSURE_MSB 0xF7
109 #define WEATHER_REG_PRESSURE_LSB 0xF8
110 #define WEATHER_REG_PRESSURE_XLSB 0xF9
111 #define WEATHER_REG_TEMPERATURE_MSB 0xFA
112 #define WEATHER_REG_TEMPERATURE_LSB 0xFB
113 #define WEATHER_REG_TEMPERATURE_XLSB 0xFC
114 #define WEATHER_REG_HUMIDITY_MSB 0xFD
115 #define WEATHER_REG_HUMIDITY_LSB 0xFE
116 
122 #define WEATHER_REG_CALIB_TEMPERATURE 0x88
123 #define WEATHER_REG_CALIB_TEMP_1_LSB 0x88
124 #define WEATHER_REG_CALIB_TEMP_1_MSB 0x89
125 #define WEATHER_REG_CALIB_TEMP_2_LSB 0x8A
126 #define WEATHER_REG_CALIB_TEMP_2_MSB 0x8B
127 #define WEATHER_REG_CALIB_TEMP_3_LSB 0x8C
128 #define WEATHER_REG_CALIB_TEMP_3_MSB 0x8D
129 
130 #define WEATHER_REG_CALIB_PRESSURE 0x8E
131 #define WEATHER_REG_CALIB_PRESS_1_LSB 0x8E
132 #define WEATHER_REG_CALIB_PRESS_1_MSB 0x8F
133 #define WEATHER_REG_CALIB_PRESS_2_LSB 0x90
134 #define WEATHER_REG_CALIB_PRESS_2_MSB 0x91
135 #define WEATHER_REG_CALIB_PRESS_3_LSB 0x92
136 #define WEATHER_REG_CALIB_PRESS_3_MSB 0x93
137 #define WEATHER_REG_CALIB_PRESS_4_LSB 0x94
138 #define WEATHER_REG_CALIB_PRESS_4_MSB 0x95
139 #define WEATHER_REG_CALIB_PRESS_5_LSB 0x96
140 #define WEATHER_REG_CALIB_PRESS_5_MSB 0x97
141 #define WEATHER_REG_CALIB_PRESS_6_LSB 0x98
142 #define WEATHER_REG_CALIB_PRESS_6_MSB 0x99
143 #define WEATHER_REG_CALIB_PRESS_7_LSB 0x9A
144 #define WEATHER_REG_CALIB_PRESS_7_MSB 0x9B
145 #define WEATHER_REG_CALIB_PRESS_8_LSB 0x9C
146 #define WEATHER_REG_CALIB_PRESS_8_MSB 0x9D
147 #define WEATHER_REG_CALIB_PRESS_9_LSB 0x9E
148 #define WEATHER_REG_CALIB_PRESS_9_MSB 0x9F
149 
150 #define WEATHER_REG_CALIB_HUMIDITY 0xA1
151 #define WEATHER_REG_CALIB_HUM_1 0xA1
152 #define WEATHER_REG_CALIB_HUM_2_LSB 0xE1
153 #define WEATHER_REG_CALIB_HUM_2_MSB 0xE2
154 #define WEATHER_REG_CALIB_HUM_3 0xE3
155 #define WEATHER_REG_CALIB_HUM_4_MSB 0xE4
156 #define WEATHER_REG_CALIB_HUM_4_LSB 0xE5
157 #define WEATHER_REG_CALIB_HUM_5_MSB 0xE6
158 #define WEATHER_REG_CALIB_HUM_6 0xE7
159 
165 #define WEATHER_HUM_OS_SKIPPED 0x00
166 #define WEATHER_HUM_OS_1X 0x01
167 #define WEATHER_HUM_OS_2X 0x02
168 #define WEATHER_HUM_OS_4X 0x03
169 #define WEATHER_HUM_OS_8X 0x04
170 #define WEATHER_HUM_OS_16X 0x05
171 
177 #define WEATHER_STATUS_MEASURING 0x08
178 #define WEATHER_STATUS_IM_UPDATE 0x01
179 
185 #define WEATHER_MCTRL_PRESS_OS_SKIPPED 0x00;
186 #define WEATHER_MCTRL_PRESS_OS_1X ( 0x01<<2 )
187 #define WEATHER_MCTRL_PRESS_OS_2X ( 0x02<<2 )
188 #define WEATHER_MCTRL_PRESS_OS_4X ( 0x03<<2 )
189 #define WEATHER_MCTRL_PRESS_OS_8X ( 0x04<<2 )
190 #define WEATHER_MCTRL_PRESS_OS_16X ( 0x05<<2 )
191 #define WEATHER_MCTRL_TEMP_OS_SKIPPED 0x00
192 #define WEATHER_MCTRL_TEMP_OS_1X ( 0x01<<5 )
193 #define WEATHER_MCTRL_TEMP_OS_2X ( 0x02<<5 )
194 #define WEATHER_MCTRL_TEMP_OS_4X ( 0x03<<5 )
195 #define WEATHER_MCTRL_TEMP_OS_8X ( 0x04<<5 )
196 #define WEATHER_MCTRL_TEMP_OS_16X ( 0x05<<5 )
197 #define WEATHER_MCTRL_MODE_SLEEP 0x00
198 #define WEATHER_MCTRL_MODE_FORCED 0x01
199 #define WEATHER_MCTRL_MODE_NORMAL 0x03
200 
206 #define WEATHER_CFG_STANDBY_TIME_0p5ms 0x00
207 #define WEATHER_CFG_STANDBY_TIME_62p5ms ( 0x01<<5 )
208 #define WEATHER_CFG_STANDBY_TIME_125ms ( 0x02<<5 )
209 #define WEATHER_CFG_STANDBY_TIME_250ms ( 0x03<<5 )
210 #define WEATHER_CFG_STANDBY_TIME_500ms ( 0x04<<5 )
211 #define WEATHER_CFG_STANDBY_TIME_1000ms ( 0x05<<5 )
212 #define WEATHER_CFG_STANDBY_TIME_10ms ( 0x06<<5 )
213 #define WEATHER_CFG_STANDBY_TIME_20ms ( 0x07<<5 )
214 #define WEATHER_CFG_FILTER_OFF 0x00
215 #define WEATHER_CFG_FILTER_2 ( 0x01<<2 )
216 #define WEATHER_CFG_FILTER_4 ( 0x02<<2 )
217 #define WEATHER_CFG_FILTER_8 ( 0x03<<2 )
218 #define WEATHER_CFG_FILTER_16 ( 0x04<<2 )
219  // End group macro
222 // --------------------------------------------------------------- PUBLIC TYPES
231 typedef uint8_t weather_select_t;
232 
236 typedef void ( *weather_master_io_t )( struct weather_s*, uint8_t, uint8_t*, uint8_t );
237 
241 typedef struct weather_s
242 {
243  // Modules
244 
245  i2c_master_t i2c;
246  spi_master_t spi;
247 
248  // ctx variable
249 
250  hal_i2c_address_t slave_address;
254 
255 } weather_t;
256 
260 typedef struct
261 {
262  // Communication gpio pins
263 
264  pin_name_t scl;
265  pin_name_t sda;
266  pin_name_t miso;
267  pin_name_t mosi;
268  pin_name_t sck;
269  pin_name_t cs;
270 
271  // static variable
272 
273  hal_i2c_speed_t i2c_speed;
274  hal_i2c_address_t i2c_address;
275  hal_spi_speed_t spi_speed;
276  hal_spi_mode_t spi_mode;
277 
279 
280 } weather_cfg_t;
281 
285 typedef struct
286 {
287  float temperature;
288  float pressure;
289  float humidity;
290 
292 
296 typedef struct
297 {
298  // CTRL register
299  uint8_t os_temp;
300  uint8_t os_press;
301  uint8_t os_hum;
302  uint8_t mode;
303 
304  // CFG register
305  uint8_t standby_time;
306  uint8_t filter;
307 
309  // End types group
311 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
317 #ifdef __cplusplus
318 extern "C"{
319 #endif
320 
329 void weather_cfg_setup ( weather_cfg_t *cfg );
330 
339 
356 void weather_default_cfg ( weather_t *ctx );
357 
368 void weather_generic_write ( weather_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
369 
380 void weather_generic_read ( weather_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
381 
390 uint8_t weather_get_device_id ( weather_t *ctx );
391 
397 void weather_software_reset ( weather_t *ctx );
398 
407 void weather_get_ambient_data( weather_t *ctx, weather_data_t *weather_data );
408 
418 
419 
420 #ifdef __cplusplus
421 }
422 #endif
423 #endif // _WEATHER_H_
424  // End public_function group
427 
428 // ------------------------------------------------------------------------- END
weather_measurement_cfg_t::os_press
uint8_t os_press
Definition: weather.h:300
weather_init
WEATHER_RETVAL weather_init(weather_t *ctx, weather_cfg_t *cfg)
Initialization function.
weather_measurement_cfg_t::standby_time
uint8_t standby_time
Definition: weather.h:305
weather_cfg_t::spi_mode
hal_spi_mode_t spi_mode
Definition: weather.h:276
weather_s::write_f
weather_master_io_t write_f
Definition: weather.h:251
weather_data_t::pressure
float pressure
Definition: weather.h:288
weather_data_t::temperature
float temperature
Definition: weather.h:287
weather_measurement_cfg_t::os_temp
uint8_t os_temp
Definition: weather.h:299
weather_cfg_t::cs
pin_name_t cs
Definition: weather.h:269
weather_cfg_t::sda
pin_name_t sda
Definition: weather.h:265
weather_cfg_t::scl
pin_name_t scl
Definition: weather.h:264
weather_data_t::humidity
float humidity
Definition: weather.h:289
weather_s::read_f
weather_master_io_t read_f
Definition: weather.h:252
weather_measurement_cfg_t
Measurement configuration structure definition.
Definition: weather.h:296
weather_cfg_setup
void weather_cfg_setup(weather_cfg_t *cfg)
Config Object Initialization function.
weather_s::i2c
i2c_master_t i2c
Definition: weather.h:245
weather_default_cfg
void weather_default_cfg(weather_t *ctx)
Click Default Configuration function.
weather_cfg_t::miso
pin_name_t miso
Definition: weather.h:266
weather_s::spi
spi_master_t spi
Definition: weather.h:246
weather_get_ambient_data
void weather_get_ambient_data(weather_t *ctx, weather_data_t *weather_data)
Weather data.
weather_cfg_t
Click configuration structure definition.
Definition: weather.h:260
weather_s::slave_address
hal_i2c_address_t slave_address
Definition: weather.h:250
WEATHER_RETVAL
#define WEATHER_RETVAL
Definition: weather.h:75
weather_generic_read
void weather_generic_read(weather_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
weather_cfg_t::spi_speed
hal_spi_speed_t spi_speed
Definition: weather.h:275
weather_master_io_t
void(* weather_master_io_t)(struct weather_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: weather.h:236
weather_get_device_id
uint8_t weather_get_device_id(weather_t *ctx)
Device ID.
weather_measurement_cfg_t::filter
uint8_t filter
Definition: weather.h:306
weather_t
struct weather_s weather_t
Click ctx object definition.
weather_measurement_cfg_t::mode
uint8_t mode
Definition: weather.h:302
weather_cfg_t::i2c_address
hal_i2c_address_t i2c_address
Definition: weather.h:274
weather_cfg_t::i2c_speed
hal_i2c_speed_t i2c_speed
Definition: weather.h:273
weather_measurement_cfg_t::os_hum
uint8_t os_hum
Definition: weather.h:301
weather_select_t
uint8_t weather_select_t
Communication type.
Definition: weather.h:231
weather_cfg_t::sel
weather_select_t sel
Definition: weather.h:278
weather_measurement_cfg
void weather_measurement_cfg(weather_t *ctx, weather_measurement_cfg_t *cfg)
Measurement config.
weather_s
Click ctx object definition.
Definition: weather.h:241
weather_data_t
Weather data structure definition.
Definition: weather.h:285
weather_generic_write
void weather_generic_write(weather_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
weather_cfg_t::mosi
pin_name_t mosi
Definition: weather.h:267
weather_software_reset
void weather_software_reset(weather_t *ctx)
Software reset function.
weather_s::master_sel
weather_select_t master_sel
Definition: weather.h:253
weather_cfg_t::sck
pin_name_t sck
Definition: weather.h:268