ldc  2.0.0.0
ldc.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef LDC_H
29 #define LDC_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_i2c_master.h"
52 
73 #define LDC_REG_DATA_CH0 0x00
74 #define LDC_REG_DATA_CH1 0x02
75 #define LDC_REG_RCOUNT_CH0 0x08
76 #define LDC_REG_RCOUNT_CH1 0x09
77 #define LDC_REG_OFFSET_CH0 0x0C
78 #define LDC_REG_OFFSET_CH1 0x0D
79 #define LDC_REG_SETTLECOUNT_CH0 0x10
80 #define LDC_REG_SETTLECOUNT_CH1 0x11
81 #define LDC_REG_CLOCK_DIVIDERS_CH0 0x14
82 #define LDC_REG_CLOCK_DIVIDERS_CH1 0x15
83 #define LDC_REG_STATUS 0x18
84 #define LDC_REG_ERROR_CONFIG 0x19
85 #define LDC_REG_CONFIG 0x1A
86 #define LDC_REG_MUX_CONFIG 0x1B
87 #define LDC_REG_RESET_DEV 0x1C
88 #define LDC_REG_DRIVE_CURRENT_CH0 0x1E
89 #define LDC_REG_DRIVE_CURRENT_CH1 0x1F
90 #define LDC_REG_MANUFACTURER_ID 0x7E
91 #define LDC_REG_DEVICE_ID 0x7F
92  // ldc_reg
94 
109 #define LDC_MANUFACTURER_ID 0x5449
110 #define LDC_DEVICE_ID 0x3054
111 
116 #define LDC_INTERNAL_FREQUENCY 43.4/*<MHz*/
117 #define LDC_FREQUENCY_RESOLUTION 4096
118 #define LDC_SENSOR_CAPACITANCE 0.00022/*<uF*/
119 
124 #define LDC_GAIN_1 0x0000
125 #define LDC_GAIN_4 0x0200
126 #define LDC_GAIN_8 0x0400
127 #define LDC_GAIN_16 0x0600
128 
133 #define LDC_OVER_RANGE 0x8
134 #define LDC_UNDER_RANGE 0x4
135 #define LDC_WATCHDOG 0x3
136 
141 #define LDC_STATUS_DRDY 0x0040
142 
148 #define LDC_SET_DEV_ADDR 0x2B
149  // ldc_set
151 
166 #define LDC_MAP_MIKROBUS( cfg, mikrobus ) \
167  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
168  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
169  cfg.sd = MIKROBUS( mikrobus, MIKROBUS_RST ); \
170  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
171  // ldc_map // ldc
174 
179 typedef struct
180 {
181  // Output pins
182  digital_out_t sd;
184  // Input pins
185  digital_in_t int_pin;
187  // Modules
188  i2c_master_t i2c;
190  // I2C slave address
191  uint8_t slave_address;
193 } ldc_t;
194 
199 typedef struct
200 {
201  pin_name_t scl;
202  pin_name_t sda;
204  pin_name_t sd;
205  pin_name_t int_pin;
207  uint32_t i2c_speed;
208  uint8_t i2c_address;
210 } ldc_cfg_t;
211 
216 typedef enum
217 {
218  LDC_OK = 0,
219  LDC_ERROR = -1,
222  LDC_ERROR_WATCHDOG = -4
223 
225 
241 void ldc_cfg_setup ( ldc_cfg_t *cfg );
242 
256 err_t ldc_init ( ldc_t *ctx, ldc_cfg_t *cfg );
257 
271 err_t ldc_default_cfg ( ldc_t *ctx );
272 
287 err_t ldc_generic_write ( ldc_t *ctx, uint8_t reg, uint16_t tx_data );
288 
303 err_t ldc_generic_read ( ldc_t *ctx, uint8_t reg, uint16_t *rx_data );
304 
312 uint8_t ldc_get_interrupt ( ldc_t *ctx );
313 
322 void ldc_set_shut_down ( ldc_t *ctx, uint8_t state );
323 
341 err_t ldc_get_frequency ( ldc_t *ctx, uint8_t channel, uint16_t divider, float *frequency );
342 
351 float ldc_calculate_inductance ( float frequency );
352 
353 
354 #ifdef __cplusplus
355 }
356 #endif
357 #endif // LDC_H
358  // ldc
360 
361 // ------------------------------------------------------------------------ END
ldc_cfg_t::scl
pin_name_t scl
Definition: ldc.h:201
ldc_generic_write
err_t ldc_generic_write(ldc_t *ctx, uint8_t reg, uint16_t tx_data)
LDC I2C writing function.
ldc_cfg_t::sda
pin_name_t sda
Definition: ldc.h:202
ldc_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: ldc.h:207
ldc_cfg_t::sd
pin_name_t sd
Definition: ldc.h:204
LDC_ERROR_WATCHDOG
@ LDC_ERROR_WATCHDOG
Definition: ldc.h:222
ldc_t
LDC Click context object.
Definition: ldc.h:180
ldc_default_cfg
err_t ldc_default_cfg(ldc_t *ctx)
LDC default configuration function.
ldc_init
err_t ldc_init(ldc_t *ctx, ldc_cfg_t *cfg)
LDC initialization function.
LDC_ERROR_OVER_RANGE
@ LDC_ERROR_OVER_RANGE
Definition: ldc.h:221
ldc_t::i2c
i2c_master_t i2c
Definition: ldc.h:188
ldc_cfg_setup
void ldc_cfg_setup(ldc_cfg_t *cfg)
LDC configuration object setup function.
ldc_get_interrupt
uint8_t ldc_get_interrupt(ldc_t *ctx)
Get interrupt pin status.
ldc_cfg_t::int_pin
pin_name_t int_pin
Definition: ldc.h:205
ldc_t::sd
digital_out_t sd
Definition: ldc.h:182
LDC_OK
@ LDC_OK
Definition: ldc.h:218
ldc_return_value_t
ldc_return_value_t
LDC Click return value data.
Definition: ldc.h:217
ldc_calculate_inductance
float ldc_calculate_inductance(float frequency)
Calculate inductance relative to frequency.
ldc_cfg_t::i2c_address
uint8_t i2c_address
Definition: ldc.h:208
LDC_ERROR
@ LDC_ERROR
Definition: ldc.h:219
ldc_get_frequency
err_t ldc_get_frequency(ldc_t *ctx, uint8_t channel, uint16_t divider, float *frequency)
Get frequency value calulated for specific channel.
ldc_set_shut_down
void ldc_set_shut_down(ldc_t *ctx, uint8_t state)
Set shut down pin status.
ldc_generic_read
err_t ldc_generic_read(ldc_t *ctx, uint8_t reg, uint16_t *rx_data)
LDC I2C reading function.
ldc_t::slave_address
uint8_t slave_address
Definition: ldc.h:191
ldc_t::int_pin
digital_in_t int_pin
Definition: ldc.h:185
divider
uint16_t divider
Definition: main.c:37
LDC_ERROR_UNDER_RANGE
@ LDC_ERROR_UNDER_RANGE
Definition: ldc.h:220
ldc_cfg_t
LDC Click configuration object.
Definition: ldc.h:200