ProtoCore v0.0.2
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
ldc1614.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file ldc1614.cpp
6 * @brief LDC1614 inductance-to-digital codec + ESP32 binding (see ldc1614.h).
7 */
8
10#include "protocore_config.h"
11
12#if PC_ENABLE_LDC1614
13
14#if defined(ARDUINO)
16#include <Arduino.h>
17#include <Wire.h>
18#endif
19uint32_t pc_ldc1614_data(uint16_t msb_reg, uint16_t lsb_reg)
20{
21 return ((uint32_t)(msb_reg & 0x0FFF) << 16) | lsb_reg;
22}
23
24uint8_t pc_ldc1614_error(uint16_t msb_reg)
25{
26 return (uint8_t)((msb_reg >> 12) & 0x0F);
27}
28
29uint64_t pc_ldc1614_sensor_freq_hz(uint32_t data28, uint32_t fref_hz)
30{
31 return ((uint64_t)data28 * fref_hz) >> 28;
32}
33
34size_t pc_ldc1614_build_config(uint8_t *buf, size_t cap, uint16_t rcount, uint16_t settlecount)
35{
36 if (!buf || cap < LDC1614_CONFIG_MAX)
37 {
38 return 0;
39 }
40 const uint16_t seq[][2] = {
41 {LDC1614_REG_RCOUNT_CH0, rcount},
42 {LDC1614_REG_SETTLECOUNT_CH0, settlecount},
43 {LDC1614_REG_CLOCK_DIVIDERS_CH0, 0x1001}, // FIN_SEL=1, FREF_DIVIDER=1
44 {LDC1614_REG_DRIVE_CURRENT_CH0, 0x9000}, // sensor drive current
45 {LDC1614_REG_ERROR_CONFIG, 0x0000}, // no error reporting on INTB
46 {LDC1614_REG_MUX_CONFIG, 0x020D}, // single channel CH0, 10 MHz deglitch
47 {LDC1614_REG_CONFIG, 0x1601}, // active CH0, internal ref, full current, start
48 };
49 size_t o = 0;
50 for (size_t i = 0; i < sizeof(seq) / sizeof(seq[0]); i++)
51 {
52 buf[o++] = (uint8_t)seq[i][0];
53 buf[o++] = (uint8_t)(seq[i][1] >> 8);
54 buf[o++] = (uint8_t)seq[i][1];
55 }
56 return o;
57}
58
59#if defined(ARDUINO)
60
61namespace
62{
63// All LDC1614 I2C-binding state, owned by one instance (internal linkage): the device address,
64// so it is one named owner, unreachable from any other translation unit.
65struct Ldc1614Ctx
66{
67 uint8_t addr = 0x2A;
68};
69Ldc1614Ctx s_ldc;
70
71bool read16(uint8_t reg, uint16_t *out)
72{
73 Wire.beginTransmission(s_ldc.addr);
74 Wire.write(reg);
75 if (Wire.endTransmission(false) != 0)
76 {
77 return false;
78 }
79 if (Wire.requestFrom((int)s_ldc.addr, 2) != 2)
80 {
81 return false;
82 }
83 uint16_t hi = Wire.read();
84 uint16_t lo = Wire.read();
85 *out = (uint16_t)((hi << 8) | lo);
86 return true;
87}
88
89bool write16(uint8_t reg, uint16_t val)
90{
91 Wire.beginTransmission(s_ldc.addr);
92 Wire.write(reg);
93 Wire.write((uint8_t)(val >> 8));
94 Wire.write((uint8_t)val);
95 return Wire.endTransmission() == 0;
96}
97} // namespace
98
99bool pc_ldc1614_begin(uint8_t addr, uint16_t rcount, uint16_t settlecount)
100{
101 pc_i2c_begin();
102 s_ldc.addr = addr;
103 uint16_t id = 0;
104 if (!read16(LDC1614_REG_DEVICE_ID, &id))
105 {
106 return false;
107 }
108 if (id != LDC1614_DEVICE_ID)
109 {
110 return false;
111 }
112 uint8_t seq[LDC1614_CONFIG_MAX];
113 size_t n = pc_ldc1614_build_config(seq, sizeof(seq), rcount, settlecount);
114 for (size_t i = 0; i + 3 <= n; i += 3)
115 {
116 if (!write16(seq[i], (uint16_t)((seq[i + 1] << 8) | seq[i + 2])))
117 {
118 return false;
119 }
120 }
121 return true;
122}
123
124bool pc_ldc1614_read_ch0(uint32_t *out)
125{
126 if (!out)
127 {
128 return false;
129 }
130 uint16_t msb = 0;
131 uint16_t lsb = 0;
132 if (!read16(LDC1614_REG_DATA_CH0_MSB, &msb) || !read16(LDC1614_REG_DATA_CH0_LSB, &lsb))
133 {
134 return false;
135 }
136 *out = pc_ldc1614_data(msb, lsb);
137 return true;
138}
139
140#endif // ARDUINO
141
142#endif // PC_ENABLE_LDC1614
The one owner of the shared I2C bus bring-up for the peripheral drivers.
void pc_i2c_begin()
Bring up the shared I2C bus on PC_I2C_SDA_PIN / PC_I2C_SCL_PIN (-1 = default).
Definition i2c.h:33
TI LDC1614 inductance-to-digital field-sensing codec (PC_ENABLE_LDC1614).
#define LDC1614_REG_CONFIG
Definition ldc1614.h:35
#define LDC1614_REG_ERROR_CONFIG
Definition ldc1614.h:34
bool pc_ldc1614_begin(uint8_t addr, uint16_t rcount, uint16_t settlecount)
Verify the device id and apply the CH0 config at addr.
#define LDC1614_DEVICE_ID
LDC1614 / LDC1612.
Definition ldc1614.h:42
#define LDC1614_REG_DRIVE_CURRENT_CH0
Definition ldc1614.h:37
#define LDC1614_REG_DATA_CH0_MSB
Definition ldc1614.h:28
#define LDC1614_REG_CLOCK_DIVIDERS_CH0
Definition ldc1614.h:32
uint8_t pc_ldc1614_error(uint16_t msb_reg)
The 4 error flags from the top of a DATA MSB register (bits 15:12).
#define LDC1614_REG_DEVICE_ID
Definition ldc1614.h:39
#define LDC1614_REG_DATA_CH0_LSB
Definition ldc1614.h:29
uint64_t pc_ldc1614_sensor_freq_hz(uint32_t data28, uint32_t fref_hz)
Sensor frequency in Hz for a 28-bit result against a reference clock: data / 2^28 * fref.
bool pc_ldc1614_read_ch0(uint32_t *out)
Read channel 0's 28-bit conversion result into out.
size_t pc_ldc1614_build_config(uint8_t *buf, size_t cap, uint16_t rcount, uint16_t settlecount)
Emit a single-channel (CH0) continuous-conversion bring-up as (reg, val_msb, val_lsb) triples.
uint32_t pc_ldc1614_data(uint16_t msb_reg, uint16_t lsb_reg)
Combine a DATA MSB register (low 12 bits) and DATA LSB register into the 28-bit result.
#define LDC1614_REG_MUX_CONFIG
Definition ldc1614.h:36
#define LDC1614_REG_SETTLECOUNT_CH0
Definition ldc1614.h:31
#define LDC1614_CONFIG_MAX
Largest config sequence in bytes: 7 register writes * 3 bytes (reg, msb, lsb).
Definition ldc1614.h:45
#define LDC1614_REG_RCOUNT_CH0
Definition ldc1614.h:30
User-facing configuration for ProtoCore.