ldc2  2.0.0.0
ldc2.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 LDC2_H
29 #define LDC2_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "drv_digital_out.h"
36 #include "drv_digital_in.h"
37 #include "drv_spi_master.h"
38 #include "spi_specifics.h"
39 
60 #define LDC2_REG_DEVICE_ID 0x00
61 #define LDC2_REG_RP_MAX 0x01
62 #define LDC2_REG_RP_MIN 0x02
63 #define LDC2_REG_WDT_FREQ 0x03
64 #define LDC2_REG_LDC_CONFIG 0x04
65 #define LDC2_REG_COMPARATOR_THLD_HIGH 0x07
66 #define LDC2_REG_COMPARATOR_THLD_LOW 0x09
67 #define LDC2_REG_INTB_TERMINAL_CONFIG 0x0A
68 #define LDC2_REG_POWER_CONFIG 0x0B
69 #define LDC2_REG_STATUS 0x20
70 #define LDC2_REG_PROXIMITY_DATA 0x22
71 #define LDC2_REG_FREQ_CNT_DATA_LSB 0x23
72 #define LDC2_REG_FREQ_CNT_DATA_MID_BYTE 0x24
73 #define LDC2_REG_FREQ_CNT_DATA_MSB 0x25
74  // ldc2_reg
76 
91 #define LDC2_DEVICE_ID 0x84
92 
97 #define LDC2_SPI_READ_MASK 0x80
98 #define LDC2_SPI_WRITE_MASK 0x7F
99 
104 #define LDC2_COMPARATOR_THLD_MIN 0x00
105 #define LDC2_COMPARATOR_THLD_MAX 0xFF
106 
111 #define LDC2_STATUS_OSC_STOPPED 0x80
112 #define LDC2_STATUS_NO_NEW_DATA 0x40
113 #define LDC2_STATUS_WAKE_UP_DISABLED 0x20
114 #define LDC2_STATUS_PROX_DATA_LESS_THAN_THLD 0x10
115 
120 #define LDC2_MHZ_TO_HZ 1000000.0
121 #define LDC2_EXTERNAL_OSC_MHZ 1.8432
122 #define LDC2_PARALLEL_CAPACITANCE_uF 0.000047
123 #define LDC2_MIN_SENSOR_FREQ_CONST 68.94
124 #define LDC2_MIN_SENSOR_FREQ_DIVIDER 2500.0
125 #define LDC2_ROUND_TO_NEAREST_INT 0.5
126 
135 #define LDC2_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
136 #define LDC2_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
137  // ldc2_set
139 
154 #define LDC2_MAP_MIKROBUS( cfg, mikrobus ) \
155  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
156  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
157  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
158  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
159  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
160  // ldc2_map // ldc2
163 
168 typedef struct
169 {
170  digital_in_t int_pin;
172  // Modules
173  spi_master_t spi;
175  pin_name_t chip_select;
177 } ldc2_t;
178 
183 typedef struct
184 {
185  // Communication gpio pins
186  pin_name_t miso;
187  pin_name_t mosi;
188  pin_name_t sck;
189  pin_name_t cs;
191  // Additional gpio pins
192  pin_name_t int_pin;
194  // static variable
195  uint32_t spi_speed;
196  spi_master_mode_t spi_mode;
197  spi_master_chip_select_polarity_t cs_polarity;
199 } ldc2_cfg_t;
200 
205 typedef enum
206 {
207  LDC2_OK = 0,
209 
211 
216 typedef enum
217 {
250 
252 
257 typedef enum
258 {
262 
264 
269 typedef enum
270 {
277 
279 
284 typedef enum
285 {
290 
292 
297 typedef enum
298 {
301 
303 
319 void ldc2_cfg_setup ( ldc2_cfg_t *cfg );
320 
334 err_t ldc2_init ( ldc2_t *ctx, ldc2_cfg_t *cfg );
335 
348 err_t ldc2_default_cfg ( ldc2_t *ctx );
349 
364 err_t ldc2_generic_write ( ldc2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
365 
380 err_t ldc2_generic_read ( ldc2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
381 
392 err_t ldc2_check_device_id ( ldc2_t *ctx );
393 
407 err_t ldc2_set_rp_range ( ldc2_t *ctx, ldc2_rp_range_t rp_min, ldc2_rp_range_t rp_max );
408 
423 err_t ldc2_set_ldc_config ( ldc2_t *ctx, ldc2_amplitude_t amplitude, ldc2_resp_time_t res_time );
424 
437 err_t ldc2_set_comparator_threshold ( ldc2_t *ctx, uint8_t thld_low, uint8_t thld_high );
438 
451 err_t ldc2_set_interrupt_mode ( ldc2_t *ctx, ldc2_int_mode_t mode );
452 
465 err_t ldc2_set_power_mode ( ldc2_t *ctx, ldc2_pwr_mode_t mode );
466 
478 err_t ldc2_get_status ( ldc2_t *ctx, uint8_t *status );
479 
493 err_t ldc2_read_raw_data ( ldc2_t *ctx, uint8_t *prox_data, uint32_t *freq_cnt );
494 
507 err_t ldc2_get_response_time ( ldc2_t *ctx, uint16_t *res_time );
508 
520 err_t ldc2_get_sensor_frequency ( ldc2_t *ctx, float *freq );
521 
535 err_t ldc2_get_rp_range ( ldc2_t *ctx, float *rp_min, float *rp_max );
536 
547 err_t ldc2_set_wdt_frequency ( ldc2_t *ctx );
548 
557 uint8_t ldc2_get_int_pin ( ldc2_t *ctx );
558 
571 err_t ldc2_measure_resonance_impedance ( ldc2_t *ctx, uint8_t *prox_data, float *rp_data );
572 
585 err_t ldc2_measure_inductance ( ldc2_t *ctx, float *freq, float *inductance );
586 
587 #ifdef __cplusplus
588 }
589 #endif
590 #endif // LDC2_H
591  // ldc2
593 
594 // ------------------------------------------------------------------------ END
LDC2_RP_38p785
Definition: ldc2.h:235
LDC2_ERROR
Definition: ldc2.h:208
LDC2_RP_12p120
Definition: ldc2.h:239
LDC2_PWR_MODE_STANDBY
Definition: ldc2.h:299
ldc2_get_status
err_t ldc2_get_status(ldc2_t *ctx, uint8_t *status)
LDC 2 get status function.
LDC2_AMPLITUDE_1V
Definition: ldc2.h:259
LDC2_RP_3p078
Definition: ldc2.h:244
LDC2_RESPONSE_TIME_6144
Definition: ldc2.h:276
LDC2_RP_1308p997
Definition: ldc2.h:222
LDC2_INTB_MODE_DRDY
Definition: ldc2.h:289
LDC2_RP_3141p593
Definition: ldc2.h:219
LDC2_RP_193p926
Definition: ldc2.h:229
ldc2_cfg_t::spi_speed
uint32_t spi_speed
Definition: ldc2.h:195
ldc2_read_raw_data
err_t ldc2_read_raw_data(ldc2_t *ctx, uint8_t *prox_data, uint32_t *freq_cnt)
LDC 2 read raw data function.
spi_specifics.h
This file contains SPI specific macros, functions, etc.
LDC2_OK
Definition: ldc2.h:207
LDC2_RP_1745p329
Definition: ldc2.h:221
LDC2_RP_64p642
Definition: ldc2.h:233
LDC2_INTB_MODE_DISABLED
Definition: ldc2.h:286
LDC2_RP_436p332
Definition: ldc2.h:226
LDC2_AMPLITUDE_2V
Definition: ldc2.h:260
LDC2_INTB_MODE_COMPARATOR_OUT
Definition: ldc2.h:288
ldc2_set_wdt_frequency
err_t ldc2_set_wdt_frequency(ldc2_t *ctx)
LDC 2 set watchdog timer frequency function.
ldc2_get_rp_range
err_t ldc2_get_rp_range(ldc2_t *ctx, float *rp_min, float *rp_max)
LDC 2 get Rp range function.
ldc2_cfg_setup
void ldc2_cfg_setup(ldc2_cfg_t *cfg)
LDC 2 configuration object setup function.
ldc2_pwr_mode_t
ldc2_pwr_mode_t
LDC 2 Click Power configuration values.
Definition: ldc2.h:297
LDC2_RP_2p394
Definition: ldc2.h:245
LDC2_RP_1p796
Definition: ldc2.h:246
LDC2_RP_1p026
Definition: ldc2.h:248
ldc2_generic_write
err_t ldc2_generic_write(ldc2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
LDC 2 data writing function.
LDC2_RP_5p387
Definition: ldc2.h:242
LDC2_RP_16p160
Definition: ldc2.h:238
ldc2_t::chip_select
pin_name_t chip_select
Definition: ldc2.h:175
LDC2_RP_981p748
Definition: ldc2.h:223
ldc2_default_cfg
err_t ldc2_default_cfg(ldc2_t *ctx)
LDC 2 default configuration function.
LDC2_RP_1p347
Definition: ldc2.h:247
ldc2_cfg_t
LDC 2 Click configuration object.
Definition: ldc2.h:183
ldc2_check_device_id
err_t ldc2_check_device_id(ldc2_t *ctx)
LDC 2 check device ID function.
LDC2_AMPLITUDE_4V
Definition: ldc2.h:261
ldc2_set_power_mode
err_t ldc2_set_power_mode(ldc2_t *ctx, ldc2_pwr_mode_t mode)
LDC 2 set power mode function.
ldc2_set_interrupt_mode
err_t ldc2_set_interrupt_mode(ldc2_t *ctx, ldc2_int_mode_t mode)
LDC 2 set interrupt mode function.
LDC2_RP_9p235
Definition: ldc2.h:240
LDC2_RP_83p111
Definition: ldc2.h:232
ldc2_amplitude_t
ldc2_amplitude_t
LDC 2 Click amplitude values.
Definition: ldc2.h:257
ldc2_cfg_t::int_pin
pin_name_t int_pin
Definition: ldc2.h:192
LDC2_RP_21p547
Definition: ldc2.h:237
LDC2_RP_747p998
Definition: ldc2.h:224
ldc2_get_int_pin
uint8_t ldc2_get_int_pin(ldc2_t *ctx)
LDC 2 get int pin function.
ldc2_generic_read
err_t ldc2_generic_read(ldc2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
LDC 2 data reading function.
LDC2_RESPONSE_TIME_384
Definition: ldc2.h:272
LDC2_RP_27p704
Definition: ldc2.h:236
LDC2_RESPONSE_TIME_1536
Definition: ldc2.h:274
ldc2_cfg_t::sck
pin_name_t sck
Definition: ldc2.h:188
ldc2_get_sensor_frequency
err_t ldc2_get_sensor_frequency(ldc2_t *ctx, float *freq)
LDC 2 get sensor frequency function.
LDC2_RP_3926p991
Definition: ldc2.h:218
LDC2_RP_109p083
Definition: ldc2.h:231
ldc2_set_comparator_threshold
err_t ldc2_set_comparator_threshold(ldc2_t *ctx, uint8_t thld_low, uint8_t thld_high)
LDC 2 set comparator threshold function.
ldc2_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: ldc2.h:196
ldc2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: ldc2.h:197
LDC2_RESPONSE_TIME_192
Definition: ldc2.h:271
LDC2_INTB_MODE_WAKE_UP
Definition: ldc2.h:287
ldc2_t
LDC 2 Click context object.
Definition: ldc2.h:168
LDC2_RP_48p481
Definition: ldc2.h:234
LDC2_RESPONSE_TIME_3072
Definition: ldc2.h:275
ldc2_rp_range_t
ldc2_rp_range_t
LDC 2 Click Rp range values.
Definition: ldc2.h:216
ldc2_measure_inductance
err_t ldc2_measure_inductance(ldc2_t *ctx, float *freq, float *inductance)
LDC 2 measure inductance function.
LDC2_RP_4p309
Definition: ldc2.h:243
LDC2_RP_2243p995
Definition: ldc2.h:220
ldc2_cfg_t::miso
pin_name_t miso
Definition: ldc2.h:186
ldc2_cfg_t::cs
pin_name_t cs
Definition: ldc2.h:189
LDC2_PWR_MODE_ACTIVE
Definition: ldc2.h:300
ldc2_set_ldc_config
err_t ldc2_set_ldc_config(ldc2_t *ctx, ldc2_amplitude_t amplitude, ldc2_resp_time_t res_time)
LDC 2 set LDC config function.
LDC2_RP_0p798
Definition: ldc2.h:249
ldc2_t::spi
spi_master_t spi
Definition: ldc2.h:173
LDC2_RP_7p182
Definition: ldc2.h:241
ldc2_t::int_pin
digital_in_t int_pin
Definition: ldc2.h:170
LDC2_RP_581p776
Definition: ldc2.h:225
ldc2_set_rp_range
err_t ldc2_set_rp_range(ldc2_t *ctx, ldc2_rp_range_t rp_min, ldc2_rp_range_t rp_max)
LDC 2 set Rp range function.
LDC2_RESPONSE_TIME_768
Definition: ldc2.h:273
ldc2_cfg_t::mosi
pin_name_t mosi
Definition: ldc2.h:187
ldc2_measure_resonance_impedance
err_t ldc2_measure_resonance_impedance(ldc2_t *ctx, uint8_t *prox_data, float *rp_data)
LDC 2 measure resonance impedance function.
ldc2_return_value_t
ldc2_return_value_t
LDC 2 Click return value data.
Definition: ldc2.h:205
LDC2_RP_249p333
Definition: ldc2.h:228
ldc2_init
err_t ldc2_init(ldc2_t *ctx, ldc2_cfg_t *cfg)
LDC 2 initialization function.
ldc2_resp_time_t
ldc2_resp_time_t
LDC 2 Click response time values.
Definition: ldc2.h:269
ldc2_int_mode_t
ldc2_int_mode_t
LDC 2 Click INTB Terminal configuration values.
Definition: ldc2.h:284
LDC2_RP_145p444
Definition: ldc2.h:230
LDC2_RP_349p066
Definition: ldc2.h:227
ldc2_get_response_time
err_t ldc2_get_response_time(ldc2_t *ctx, uint16_t *res_time)
LDC 2 get response time function.