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
32extern "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_spi_master.h"
52#include "spi_specifics.h"
53
74#define LDC2_REG_DEVICE_ID 0x00
75#define LDC2_REG_RP_MAX 0x01
76#define LDC2_REG_RP_MIN 0x02
77#define LDC2_REG_WDT_FREQ 0x03
78#define LDC2_REG_LDC_CONFIG 0x04
79#define LDC2_REG_COMPARATOR_THLD_HIGH 0x07
80#define LDC2_REG_COMPARATOR_THLD_LOW 0x09
81#define LDC2_REG_INTB_TERMINAL_CONFIG 0x0A
82#define LDC2_REG_POWER_CONFIG 0x0B
83#define LDC2_REG_STATUS 0x20
84#define LDC2_REG_PROXIMITY_DATA 0x22
85#define LDC2_REG_FREQ_CNT_DATA_LSB 0x23
86#define LDC2_REG_FREQ_CNT_DATA_MID_BYTE 0x24
87#define LDC2_REG_FREQ_CNT_DATA_MSB 0x25
88
89 // ldc2_reg
90
105#define LDC2_DEVICE_ID 0x84
106
111#define LDC2_SPI_READ_MASK 0x80
112#define LDC2_SPI_WRITE_MASK 0x7F
113
118#define LDC2_COMPARATOR_THLD_MIN 0x00
119#define LDC2_COMPARATOR_THLD_MAX 0xFF
120
125#define LDC2_STATUS_OSC_STOPPED 0x80
126#define LDC2_STATUS_NO_NEW_DATA 0x40
127#define LDC2_STATUS_WAKE_UP_DISABLED 0x20
128#define LDC2_STATUS_PROX_DATA_LESS_THAN_THLD 0x10
129
134#define LDC2_MHZ_TO_HZ 1000000.0
135#define LDC2_EXTERNAL_OSC_MHZ 1.8432
136#define LDC2_PARALLEL_CAPACITANCE_uF 0.000047
137#define LDC2_MIN_SENSOR_FREQ_CONST 68.94
138#define LDC2_MIN_SENSOR_FREQ_DIVIDER 2500.0
139#define LDC2_ROUND_TO_NEAREST_INT 0.5
140#define LDC2_MATH_TWO_PI 6.28318530717958
141
150#define LDC2_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
151#define LDC2_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
152
153 // ldc2_set
154
169#define LDC2_MAP_MIKROBUS( cfg, mikrobus ) \
170 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
171 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
172 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
173 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
174 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
175
176 // ldc2_map
177 // ldc2
178
183typedef struct
184{
185 digital_in_t int_pin;
187 // Modules
188 spi_master_t spi;
190 pin_name_t chip_select;
192} ldc2_t;
193
198typedef struct
199{
200 // Communication gpio pins
201 pin_name_t miso;
202 pin_name_t mosi;
203 pin_name_t sck;
204 pin_name_t cs;
206 // Additional gpio pins
207 pin_name_t int_pin;
209 // static variable
210 uint32_t spi_speed;
211 spi_master_mode_t spi_mode;
212 spi_master_chip_select_polarity_t cs_polarity;
214} ldc2_cfg_t;
215
220typedef enum
221{
223 LDC2_ERROR = -1
224
226
267
279
294
307
318
335
349err_t ldc2_init ( ldc2_t *ctx, ldc2_cfg_t *cfg );
350
364
379err_t ldc2_generic_write ( ldc2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
380
395err_t ldc2_generic_read ( ldc2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
396
408
423
439
452err_t ldc2_set_comparator_threshold ( ldc2_t *ctx, uint8_t thld_low, uint8_t thld_high );
453
467
481
493err_t ldc2_get_status ( ldc2_t *ctx, uint8_t *status );
494
508err_t ldc2_read_raw_data ( ldc2_t *ctx, uint8_t *prox_data, uint32_t *freq_cnt );
509
522err_t ldc2_get_response_time ( ldc2_t *ctx, uint16_t *res_time );
523
535err_t ldc2_get_sensor_frequency ( ldc2_t *ctx, float *freq );
536
550err_t ldc2_get_rp_range ( ldc2_t *ctx, float *rp_min, float *rp_max );
551
563
572uint8_t ldc2_get_int_pin ( ldc2_t *ctx );
573
586err_t ldc2_measure_resonance_impedance ( ldc2_t *ctx, uint8_t *prox_data, float *rp_data );
587
600err_t ldc2_measure_inductance ( ldc2_t *ctx, float *freq, float *inductance );
601
602#ifdef __cplusplus
603}
604#endif
605#endif // LDC2_H
606
607 // ldc2
608
609// ------------------------------------------------------------------------ END
err_t ldc2_generic_write(ldc2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
LDC 2 data writing function.
err_t ldc2_generic_read(ldc2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
LDC 2 data reading function.
err_t ldc2_get_response_time(ldc2_t *ctx, uint16_t *res_time)
LDC 2 get response time function.
err_t ldc2_set_wdt_frequency(ldc2_t *ctx)
LDC 2 set watchdog timer frequency function.
err_t ldc2_get_rp_range(ldc2_t *ctx, float *rp_min, float *rp_max)
LDC 2 get Rp range function.
err_t ldc2_default_cfg(ldc2_t *ctx)
LDC 2 default configuration function.
err_t ldc2_set_interrupt_mode(ldc2_t *ctx, ldc2_int_mode_t mode)
LDC 2 set interrupt mode function.
err_t ldc2_read_raw_data(ldc2_t *ctx, uint8_t *prox_data, uint32_t *freq_cnt)
LDC 2 read raw data function.
err_t ldc2_get_sensor_frequency(ldc2_t *ctx, float *freq)
LDC 2 get sensor frequency function.
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.
uint8_t ldc2_get_int_pin(ldc2_t *ctx)
LDC 2 get int pin function.
err_t ldc2_measure_resonance_impedance(ldc2_t *ctx, uint8_t *prox_data, float *rp_data)
LDC 2 measure resonance impedance function.
err_t ldc2_measure_inductance(ldc2_t *ctx, float *freq, float *inductance)
LDC 2 measure inductance function.
err_t ldc2_check_device_id(ldc2_t *ctx)
LDC 2 check device ID function.
err_t ldc2_get_status(ldc2_t *ctx, uint8_t *status)
LDC 2 get status function.
void ldc2_cfg_setup(ldc2_cfg_t *cfg)
LDC 2 configuration object setup function.
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.
err_t ldc2_set_power_mode(ldc2_t *ctx, ldc2_pwr_mode_t mode)
LDC 2 set power mode function.
err_t ldc2_init(ldc2_t *ctx, ldc2_cfg_t *cfg)
LDC 2 initialization function.
err_t ldc2_set_comparator_threshold(ldc2_t *ctx, uint8_t thld_low, uint8_t thld_high)
LDC 2 set comparator threshold function.
ldc2_return_value_t
LDC 2 Click return value data.
Definition ldc2.h:221
@ LDC2_ERROR
Definition ldc2.h:223
@ LDC2_OK
Definition ldc2.h:222
ldc2_rp_range_t
LDC 2 Click Rp range values.
Definition ldc2.h:232
@ LDC2_RP_21p547
Definition ldc2.h:252
@ LDC2_RP_64p642
Definition ldc2.h:248
@ LDC2_RP_581p776
Definition ldc2.h:240
@ LDC2_RP_27p704
Definition ldc2.h:251
@ LDC2_RP_145p444
Definition ldc2.h:245
@ LDC2_RP_747p998
Definition ldc2.h:239
@ LDC2_RP_1308p997
Definition ldc2.h:237
@ LDC2_RP_1p026
Definition ldc2.h:263
@ LDC2_RP_249p333
Definition ldc2.h:243
@ LDC2_RP_2p394
Definition ldc2.h:260
@ LDC2_RP_3p078
Definition ldc2.h:259
@ LDC2_RP_7p182
Definition ldc2.h:256
@ LDC2_RP_0p798
Definition ldc2.h:264
@ LDC2_RP_3926p991
Definition ldc2.h:233
@ LDC2_RP_193p926
Definition ldc2.h:244
@ LDC2_RP_1745p329
Definition ldc2.h:236
@ LDC2_RP_12p120
Definition ldc2.h:254
@ LDC2_RP_16p160
Definition ldc2.h:253
@ LDC2_RP_3141p593
Definition ldc2.h:234
@ LDC2_RP_48p481
Definition ldc2.h:249
@ LDC2_RP_2243p995
Definition ldc2.h:235
@ LDC2_RP_4p309
Definition ldc2.h:258
@ LDC2_RP_109p083
Definition ldc2.h:246
@ LDC2_RP_436p332
Definition ldc2.h:241
@ LDC2_RP_1p796
Definition ldc2.h:261
@ LDC2_RP_38p785
Definition ldc2.h:250
@ LDC2_RP_83p111
Definition ldc2.h:247
@ LDC2_RP_5p387
Definition ldc2.h:257
@ LDC2_RP_981p748
Definition ldc2.h:238
@ LDC2_RP_1p347
Definition ldc2.h:262
@ LDC2_RP_9p235
Definition ldc2.h:255
@ LDC2_RP_349p066
Definition ldc2.h:242
ldc2_int_mode_t
LDC 2 Click INTB Terminal configuration values.
Definition ldc2.h:300
@ LDC2_INTB_MODE_DRDY
Definition ldc2.h:304
@ LDC2_INTB_MODE_WAKE_UP
Definition ldc2.h:302
@ LDC2_INTB_MODE_DISABLED
Definition ldc2.h:301
@ LDC2_INTB_MODE_COMPARATOR_OUT
Definition ldc2.h:303
ldc2_resp_time_t
LDC 2 Click response time values.
Definition ldc2.h:285
@ LDC2_RESPONSE_TIME_384
Definition ldc2.h:287
@ LDC2_RESPONSE_TIME_1536
Definition ldc2.h:289
@ LDC2_RESPONSE_TIME_192
Definition ldc2.h:286
@ LDC2_RESPONSE_TIME_768
Definition ldc2.h:288
@ LDC2_RESPONSE_TIME_6144
Definition ldc2.h:291
@ LDC2_RESPONSE_TIME_3072
Definition ldc2.h:290
ldc2_pwr_mode_t
LDC 2 Click Power configuration values.
Definition ldc2.h:313
@ LDC2_PWR_MODE_STANDBY
Definition ldc2.h:314
@ LDC2_PWR_MODE_ACTIVE
Definition ldc2.h:315
ldc2_amplitude_t
LDC 2 Click amplitude values.
Definition ldc2.h:273
@ LDC2_AMPLITUDE_2V
Definition ldc2.h:275
@ LDC2_AMPLITUDE_4V
Definition ldc2.h:276
@ LDC2_AMPLITUDE_1V
Definition ldc2.h:274
This file contains SPI specific macros, functions, etc.
LDC 2 Click configuration object.
Definition ldc2.h:199
spi_master_chip_select_polarity_t cs_polarity
Definition ldc2.h:212
pin_name_t sck
Definition ldc2.h:203
spi_master_mode_t spi_mode
Definition ldc2.h:211
pin_name_t mosi
Definition ldc2.h:202
uint32_t spi_speed
Definition ldc2.h:210
pin_name_t int_pin
Definition ldc2.h:207
pin_name_t miso
Definition ldc2.h:201
pin_name_t cs
Definition ldc2.h:204
LDC 2 Click context object.
Definition ldc2.h:184
spi_master_t spi
Definition ldc2.h:188
digital_in_t int_pin
Definition ldc2.h:185
pin_name_t chip_select
Definition ldc2.h:190