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 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_i2c_master.h"
48 
69 #define LDC_REG_DATA_CH0 0x00
70 #define LDC_REG_DATA_CH1 0x02
71 #define LDC_REG_RCOUNT_CH0 0x08
72 #define LDC_REG_RCOUNT_CH1 0x09
73 #define LDC_REG_OFFSET_CH0 0x0C
74 #define LDC_REG_OFFSET_CH1 0x0D
75 #define LDC_REG_SETTLECOUNT_CH0 0x10
76 #define LDC_REG_SETTLECOUNT_CH1 0x11
77 #define LDC_REG_CLOCK_DIVIDERS_CH0 0x14
78 #define LDC_REG_CLOCK_DIVIDERS_CH1 0x15
79 #define LDC_REG_STATUS 0x18
80 #define LDC_REG_ERROR_CONFIG 0x19
81 #define LDC_REG_CONFIG 0x1A
82 #define LDC_REG_MUX_CONFIG 0x1B
83 #define LDC_REG_RESET_DEV 0x1C
84 #define LDC_REG_DRIVE_CURRENT_CH0 0x1E
85 #define LDC_REG_DRIVE_CURRENT_CH1 0x1F
86 #define LDC_REG_MANUFACTURER_ID 0x7E
87 #define LDC_REG_DEVICE_ID 0x7F
88  // ldc_reg
90 
105 #define LDC_MANUFACTURER_ID 0x5449
106 #define LDC_DEVICE_ID 0x3054
107 
112 #define LDC_INTERNAL_FREQUENCY 43.4/*<MHz*/
113 #define LDC_FREQUENCY_RESOLUTION 4096
114 #define LDC_SENSOR_CAPACITANCE 0.00022/*<uF*/
115 
120 #define LDC_GAIN_1 0x0000
121 #define LDC_GAIN_4 0x0200
122 #define LDC_GAIN_8 0x0400
123 #define LDC_GAIN_16 0x0600
124 
129 #define LDC_OVER_RANGE 0x8
130 #define LDC_UNDER_RANGE 0x4
131 #define LDC_WATCHDOG 0x3
132 
137 #define LDC_STATUS_DRDY 0x0040
138 
144 #define LDC_SET_DEV_ADDR 0x2B
145  // ldc_set
147 
162 #define LDC_MAP_MIKROBUS( cfg, mikrobus ) \
163  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
164  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
165  cfg.sd = MIKROBUS( mikrobus, MIKROBUS_RST ); \
166  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
167  // ldc_map // ldc
170 
175 typedef struct
176 {
177  // Output pins
178  digital_out_t sd;
180  // Input pins
181  digital_in_t int_pin;
183  // Modules
184  i2c_master_t i2c;
186  // I2C slave address
187  uint8_t slave_address;
189 } ldc_t;
190 
195 typedef struct
196 {
197  pin_name_t scl;
198  pin_name_t sda;
200  pin_name_t sd;
201  pin_name_t int_pin;
203  uint32_t i2c_speed;
204  uint8_t i2c_address;
206 } ldc_cfg_t;
207 
212 typedef enum
213 {
214  LDC_OK = 0,
215  LDC_ERROR = -1,
218  LDC_ERROR_WATCHDOG = -4
219 
221 
237 void ldc_cfg_setup ( ldc_cfg_t *cfg );
238 
252 err_t ldc_init ( ldc_t *ctx, ldc_cfg_t *cfg );
253 
267 err_t ldc_default_cfg ( ldc_t *ctx );
268 
283 err_t ldc_generic_write ( ldc_t *ctx, uint8_t reg, uint16_t tx_data );
284 
299 err_t ldc_generic_read ( ldc_t *ctx, uint8_t reg, uint16_t *rx_data );
300 
308 uint8_t ldc_get_interrupt ( ldc_t *ctx );
309 
318 void ldc_set_shut_down ( ldc_t *ctx, uint8_t state );
319 
337 err_t ldc_get_frequency ( ldc_t *ctx, uint8_t channel, uint16_t divider, float *frequency );
338 
347 float ldc_calculate_inductance ( float frequency );
348 
349 
350 #ifdef __cplusplus
351 }
352 #endif
353 #endif // LDC_H
354  // ldc
356 
357 // ------------------------------------------------------------------------ END
ldc_cfg_t::scl
pin_name_t scl
Definition: ldc.h:197
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:198
ldc_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: ldc.h:203
ldc_cfg_t::sd
pin_name_t sd
Definition: ldc.h:200
LDC_ERROR_WATCHDOG
@ LDC_ERROR_WATCHDOG
Definition: ldc.h:218
ldc_t
LDC Click context object.
Definition: ldc.h:176
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:217
ldc_t::i2c
i2c_master_t i2c
Definition: ldc.h:184
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:201
ldc_t::sd
digital_out_t sd
Definition: ldc.h:178
LDC_OK
@ LDC_OK
Definition: ldc.h:214
ldc_return_value_t
ldc_return_value_t
LDC Click return value data.
Definition: ldc.h:213
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:204
LDC_ERROR
@ LDC_ERROR
Definition: ldc.h:215
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:187
ldc_t::int_pin
digital_in_t int_pin
Definition: ldc.h:181
divider
uint16_t divider
Definition: main.c:37
LDC_ERROR_UNDER_RANGE
@ LDC_ERROR_UNDER_RANGE
Definition: ldc.h:216
ldc_cfg_t
LDC Click configuration object.
Definition: ldc.h:196