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 
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  digital_out_t cs;
244 
245  // Modules
246 
247  i2c_master_t i2c;
248  spi_master_t spi;
249 
250  // ctx variable
251 
252  uint8_t slave_address;
253  pin_name_t chip_select;
257 
259 
263 typedef struct
264 {
265  // Communication gpio pins
266 
267  pin_name_t scl;
268  pin_name_t sda;
269  pin_name_t miso;
270  pin_name_t mosi;
271  pin_name_t sck;
272  pin_name_t cs;
273 
274  // static variable
275 
276  uint32_t i2c_speed;
277  uint8_t i2c_address;
278 
279  uint32_t spi_speed;
280  spi_master_mode_t spi_mode;
281  spi_master_chip_select_polarity_t cs_polarity;
282 
284 
285 } weather_cfg_t;
286 
290 typedef struct
291 {
292  float temperature;
293  float pressure;
294  float humidity;
295 
297 
301 typedef struct
302 {
303  // CTRL register
304  uint8_t os_temp;
305  uint8_t os_press;
306  uint8_t os_hum;
307  uint8_t mode;
308 
309  // CFG register
310  uint8_t standby_time;
311  uint8_t filter;
312 
314  // End types group
316 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
322 #ifdef __cplusplus
323 extern "C"{
324 #endif
325 
335 
344 
362 
373 void weather_generic_write ( weather_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
374 
385 void weather_generic_read ( weather_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
386 
396 
403 
413 
423 
424 
425 #ifdef __cplusplus
426 }
427 #endif
428 #endif // _WEATHER_H_
429  // End public_function group
432 
433 // ------------------------------------------------------------------------- END
weather_measurement_cfg_t::os_press
uint8_t os_press
Definition: weather.h:305
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:310
weather_s::write_f
weather_master_io_t write_f
Definition: weather.h:254
weather_data_t::pressure
float pressure
Definition: weather.h:293
weather_data_t::temperature
float temperature
Definition: weather.h:292
weather_measurement_cfg_t::os_temp
uint8_t os_temp
Definition: weather.h:304
weather_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: weather.h:276
weather_cfg_t::cs
pin_name_t cs
Definition: weather.h:272
weather_cfg_t::sda
pin_name_t sda
Definition: weather.h:268
weather_cfg_t::scl
pin_name_t scl
Definition: weather.h:267
weather_data_t::humidity
float humidity
Definition: weather.h:294
weather_s::read_f
weather_master_io_t read_f
Definition: weather.h:255
weather_measurement_cfg_t
Measurement configuration structure definition.
Definition: weather.h:302
weather_t
struct weather_s weather_t
Click ctx object definition.
weather_cfg_t::i2c_address
uint8_t i2c_address
Definition: weather.h:277
weather_cfg_setup
void weather_cfg_setup(weather_cfg_t *cfg)
Config Object Initialization function.
weather_s::slave_address
uint8_t slave_address
Definition: weather.h:252
weather_s::i2c
i2c_master_t i2c
Definition: weather.h:247
weather_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: weather.h:280
weather_default_cfg
void weather_default_cfg(weather_t *ctx)
Click Default Configuration function.
weather_s::chip_select
pin_name_t chip_select
Definition: weather.h:253
weather_cfg_t::miso
pin_name_t miso
Definition: weather.h:269
weather_s::spi
spi_master_t spi
Definition: weather.h:248
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:264
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
uint32_t spi_speed
Definition: weather.h:279
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:311
weather_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: weather.h:281
weather_measurement_cfg_t::mode
uint8_t mode
Definition: weather.h:307
weather_measurement_cfg_t::os_hum
uint8_t os_hum
Definition: weather.h:306
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:283
weather_measurement_cfg
void weather_measurement_cfg(weather_t *ctx, weather_measurement_cfg_t *cfg)
Measurement config.
weather_s::cs
digital_out_t cs
Definition: weather.h:243
weather_s
Click ctx object definition.
Definition: weather.h:242
weather_data_t
Weather data structure definition.
Definition: weather.h:291
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:270
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:256
weather_cfg_t::sck
pin_name_t sck
Definition: weather.h:271