moisture  2.0.0.0
moisture.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 MOISTURE_H
36 #define MOISTURE_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 
42 // -------------------------------------------------------------- PUBLIC MACROS
52 #define MOISTURE_MAP_MIKROBUS( cfg, mikrobus ) \
53  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
54  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
55  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
56 
62 #define MOISTURE_RETVAL uint8_t
63 
64 #define MOISTURE_OK 0x00
65 #define MOISTURE_INIT_ERROR 0xFF
66 
72 #define MOISTURE_DEV_ADR_GND 0x2A
73 #define MOISTURE_DEV_ADR_VCC 0x2B
74 
80 #define MOISTURE_DEFAULT_OFFSET 0x00AA
81 
87 #define MOISTURE_REG_DATA 0x00
88 #define MOISTURE_REG_RCNT 0x08
89 #define MOISTURE_REG_OFFSET 0x0C
90 #define MOISTURE_REG_SETTLE_CNT 0x10
91 #define MOISTURE_REG_CLOCK_DIVIDERS 0x14
92 #define MOISTURE_REG_STATUS 0x18
93 #define MOISTURE_REG_ERR_CFG 0x19
94 #define MOISTURE_REG_CFG 0x1A
95 #define MOISTURE_REG_MUX_CFG 0x1B
96 #define MOISTURE_REG_RESET_DEV 0x1C
97 #define MOISTURE_REG_DRIVE_CURR 0x1E
98 #define MOISTURE_REG_DEVICE_ID 0x7F
99 #define MOISTURE_REG_MFC_ID 0x7E
100 
106 #define MOISTURE_DRIVE_CURR_0_016mA 0x0000
107 #define MOISTURE_DRIVE_CURR_0_018mA 0x0800
108 #define MOISTURE_DRIVE_CURR_0_021mA 0x1000
109 #define MOISTURE_DRIVE_CURR_0_025mA 0x1800
110 #define MOISTURE_DRIVE_CURR_0_028mA 0x2000
111 #define MOISTURE_DRIVE_CURR_0_033mA 0x2800
112 #define MOISTURE_DRIVE_CURR_0_038mA 0x3000
113 #define MOISTURE_DRIVE_CURR_0_044mA 0x3800
114 #define MOISTURE_DRIVE_CURR_0_052mA 0x4000
115 #define MOISTURE_DRIVE_CURR_0_060mA 0x4800
116 #define MOISTURE_DRIVE_CURR_0_069mA 0x5000
117 #define MOISTURE_DRIVE_CURR_0_081mA 0x5800
118 #define MOISTURE_DRIVE_CURR_0_093mA 0x6000
119 #define MOISTURE_DRIVE_CURR_0_108mA 0x6800
120 #define MOISTURE_DRIVE_CURR_0_126mA 0x7000
121 #define MOISTURE_DRIVE_CURR_0_146mA 0x7800
122 #define MOISTURE_DRIVE_CURR_0_169mA 0x8000
123 #define MOISTURE_DRIVE_CURR_0_196mA 0x8800
124 #define MOISTURE_DRIVE_CURR_0_228mA 0x9000
125 #define MOISTURE_DRIVE_CURR_0_264mA 0x9800
126 #define MOISTURE_DRIVE_CURR_0_307mA 0xA000
127 #define MOISTURE_DRIVE_CURR_0_356mA 0xA800
128 #define MOISTURE_DRIVE_CURR_0_413mA 0xB000
129 #define MOISTURE_DRIVE_CURR_0_479mA 0xB800
130 #define MOISTURE_DRIVE_CURR_0_555mA 0xC000
131 #define MOISTURE_DRIVE_CURR_0_644mA 0xC800
132 #define MOISTURE_DRIVE_CURR_0_747mA 0xD000
133 #define MOISTURE_DRIVE_CURR_0_867mA 0xD800
134 #define MOISTURE_DRIVE_CURR_1_006mA 0xE000
135 #define MOISTURE_DRIVE_CURR_1_167mA 0xE800
136 #define MOISTURE_DRIVE_CURR_1_354mA 0xF000
137 #define MOISTURE_DRIVE_CURR_1_571mA 0xF800
138 
144 #define MOISTURE_RESET_DEVICE 0x8000
145 #define MOISTURE_OUTPUT_GAIN_x1 0x0000
146 #define MOISTURE_OUTPUT_GAIN_x4 0x0200
147 #define MOISTURE_OUTPUT_GAIN_x8 0x0400
148 #define MOISTURE_OUTPUT_GAIN_x16 0x0600
149 
155 #define MOISTURE_MUX_CFG_AUTOSCAN_MODE 0x8000
156 #define MOISTURE_MUX_CFG_CONTINUOUS_MODE 0x0000
157 #define MOISTURE_MUX_CFG_RR_SEQUENCE_CH_01 0x0000
158 #define MOISTURE_MUX_CFG_DEGLITCH_1MHz 0x0001
159 #define MOISTURE_MUX_CFG_DEGLITCH_3_3MHz 0x0004
160 #define MOISTURE_MUX_CFG_DEGLITCH_10MHz 0x0005
161 #define MOISTURE_MUX_CFG_DEGLITCH_33MHz 0x0007
162 
168 #define MOISTURE_CFG_ACTIVE_CHAN_CH0 0x0000
169 #define MOISTURE_CFG_DEVICE_ACTIVE 0x0000
170 #define MOISTURE_CFG_DEVICE_IN_SLEEP_MODE 0x2000
171 #define MOISTURE_CFG_FULL_CURRENT_MODE 0x0000
172 #define MOISTURE_CFG_LOW_POWER_MODE 0x0800
173 #define MOISTURE_CFG_INT_PIN_ENABLE 0x0000
174 #define MOISTURE_CFG_INT_PIN_DISABLE 0x0080
175 
181 #define MOISTURE_ERR_CFG_WATCHDOG_TIMEOUT 0x2000
182 #define MOISTURE_ERR_CFG_AMPLITUDE_HIGH 0x1000
183 #define MOISTURE_ERR_CFG_AMPLITUDE_LOW 0x0800
184 #define MOISTURE_ERR_CFG_DATA_RDY_IN_PIN 0x0001
185 
191 #define MOISTURE_STATUS_ERR_CHAN_0 0x0000
192 #define MOISTURE_STATUS_ERR_WD 0x0800
193 #define MOISTURE_STATUS_ERR_AHW 0x0400
194 #define MOISTURE_STATUS_ERR_AHL 0x0200
195 #define MOISTURE_STATUS_DATA_RDY 0x0040
196 #define MOISTURE_STATUS_CH0_UNREADCONV 0x0008
197 
203 #define MOISTURE_CLOCK_DIVIDERS_x1 0x00001
204 #define MOISTURE_CLOCK_DIVIDERS_x2 0x00002
205 #define MOISTURE_CLOCK_DIVIDERS_x3 0x00003
206 #define MOISTURE_CLOCK_DIVIDERS_x4 0x00004
207 #define MOISTURE_CLOCK_DIVIDERS_x5 0x00005
208 #define MOISTURE_CLOCK_DIVIDERS_x6 0x00006
209 #define MOISTURE_CLOCK_DIVIDERS_x7 0x00007
210 #define MOISTURE_CLOCK_DIVIDERS_x8 0x00008
211 #define MOISTURE_CLOCK_DIVIDERS_x9 0x00009
212 #define MOISTURE_CLOCK_DIVIDERS_x10 0x0000A
213 #define MOISTURE_CLOCK_DIVIDERS_x11 0x0000B
214 #define MOISTURE_CLOCK_DIVIDERS_x12 0x0000C
215 #define MOISTURE_CLOCK_DIVIDERS_x13 0x0000D
216 #define MOISTURE_CLOCK_DIVIDERS_x14 0x0000E
217 #define MOISTURE_CLOCK_DIVIDERS_x15 0x0000F
218 
224 #define MOISTURE_FDC2112_ID 0x3054
225 #define MOISTURE_FDC2212_ID 0x3055
226 
232 #define MOISTURE_SETTLE_CNT_CFG_DEFAULT 0x0064
233 #define MOISTURE_RCNT_CFG_DEFAULT 0x010F
234 #define MOISTURE_COMBINED_VAL_CH0_DIV 0x2001
235 #define MOISTURE_DRIVE_CURR_CH0 0x7C00
236 #define MOISTURE_ALL_ERR_ENABLED 0xFFFF
237 #define MOISTURE_REG_CFG_DEFAULT_SETTINGS 0x0000
238 #define MOISTURE_INPUT_DEGLITCH_FILT_BWDTH_33MHZ 0x0007
239  // End group macro
242 // --------------------------------------------------------------- PUBLIC TYPES
251 typedef struct
252 {
253  // Input pins
254 
255  digital_in_t int_pin;
256 
257  // Modules
258 
259  i2c_master_t i2c;
260 
261  // ctx variable
262 
263  uint8_t slave_address;
264  uint16_t calib_air_data;
265 
266 } moisture_t;
267 
271 typedef struct
272 {
273  // Communication gpio pins
274 
275  pin_name_t scl;
276  pin_name_t sda;
277 
278  // Additional gpio pins
279 
280  pin_name_t int_pin;
281 
282  // static variable
283 
284  uint32_t i2c_speed;
285  uint8_t i2c_address;
286 
288  // End types group
290 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
291 
297 #ifdef __cplusplus
298 extern "C"{
299 #endif
300 
310 
320 
384 
395 void moisture_generic_write ( moisture_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
396 
407 void moisture_generic_read ( moisture_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
408 
418 void moisture_write_word ( moisture_t *ctx, uint8_t reg, uint16_t data_in );
419 
429 uint16_t moisture_read_word ( moisture_t *ctx, uint8_t reg );
430 
439 
504 void moisture_cfg( moisture_t *ctx, uint16_t gain, uint16_t offset, uint16_t clk_div );
505 
515 uint8_t moisture_get_data ( moisture_t *ctx );
516 
525 void moisture_cal ( moisture_t *ctx );
526 
536 
537 #ifdef __cplusplus
538 }
539 #endif
540 #endif // _MOISTURE_H_
541  // End public_function group
544 
545 // ------------------------------------------------------------------------ END
moisture_cal
void moisture_cal(moisture_t *ctx)
Chip calibration function.
moisture_t::calib_air_data
uint16_t calib_air_data
Definition: moisture.h:264
moisture_t::i2c
i2c_master_t i2c
Definition: moisture.h:259
moisture_check_interrupt
uint8_t moisture_check_interrupt(moisture_t *ctx)
Soft reset function.
MOISTURE_RETVAL
#define MOISTURE_RETVAL
Definition: moisture.h:62
moisture_cfg_setup
void moisture_cfg_setup(moisture_cfg_t *cfg)
Config Object Initialization function.
moisture_init
MOISTURE_RETVAL moisture_init(moisture_t *ctx, moisture_cfg_t *cfg)
Initialization function.
moisture_read_word
uint16_t moisture_read_word(moisture_t *ctx, uint8_t reg)
Read word function.
moisture_cfg_t::i2c_address
uint8_t i2c_address
Definition: moisture.h:285
moisture_cfg_t::int_pin
pin_name_t int_pin
Definition: moisture.h:280
moisture_default_cfg
void moisture_default_cfg(moisture_t *ctx)
Click Default Configuration function.
moisture_t::int_pin
digital_in_t int_pin
Definition: moisture.h:255
moisture_cfg_t
Click configuration structure definition.
Definition: moisture.h:272
moisture_t::slave_address
uint8_t slave_address
Definition: moisture.h:263
moisture_generic_read
void moisture_generic_read(moisture_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
moisture_get_data
uint8_t moisture_get_data(moisture_t *ctx)
Measurement data function.
moisture_t
Click ctx object definition.
Definition: moisture.h:252
moisture_cfg_t::scl
pin_name_t scl
Definition: moisture.h:275
moisture_generic_write
void moisture_generic_write(moisture_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
moisture_cfg_t::sda
pin_name_t sda
Definition: moisture.h:276
moisture_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: moisture.h:284
moisture_soft_reset
void moisture_soft_reset(moisture_t *ctx)
Soft reset function.
moisture_write_word
void moisture_write_word(moisture_t *ctx, uint8_t reg, uint16_t data_in)
Write word function.
moisture_cfg
void moisture_cfg(moisture_t *ctx, uint16_t gain, uint16_t offset, uint16_t clk_div)
Configure device function.