DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
fdc2214.h
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 fdc2214.h
6 * @brief TI FDC2114/2214 capacitance-to-digital field-sensing codec (DETWS_ENABLE_FDC2214).
7 *
8 * The FDC2x14 measures the resonant frequency of an external LC tank; a capacitance shift (a finger
9 * near the electrode, a liquid rising past it, a material change) moves that frequency, so watching the
10 * 28-bit conversion result gives proximity / level / material sensing without contact. Each channel's
11 * result is two 16-bit registers - a DATA MSB register whose top 4 bits are error flags and low 12 bits
12 * are the data MSB, and a DATA LSB register - which combine into the 28-bit reading. `f_sensor =
13 * data / 2^28 * f_ref`.
14 *
15 * This codec is pure and host-tested: ::fdc2214_data combines the register pair, ::fdc2214_error pulls
16 * the flags, ::fdc2214_sensor_freq_hz scales to frequency, and ::fdc2214_build_config emits a
17 * single-channel continuous-conversion bring-up as `(reg, msb, lsb)` triples. On an ESP32 the binding
18 * replays that config and reads the channel over I2C (Wire); only that touches hardware. Bridge the
19 * readings northbound like any sensor.
20 */
21
22#ifndef DETERMINISTICESPASYNCWEBSERVER_FDC2214_H
23#define DETERMINISTICESPASYNCWEBSERVER_FDC2214_H
24
25#include <stddef.h>
26#include <stdint.h>
27
28// Register map (channel 0; CH1..3 follow at +2 / +1 offsets).
29#define FDC2214_REG_DATA_CH0_MSB 0x00
30#define FDC2214_REG_DATA_CH0_LSB 0x01
31#define FDC2214_REG_RCOUNT_CH0 0x08
32#define FDC2214_REG_SETTLECOUNT_CH0 0x10
33#define FDC2214_REG_CLOCK_DIVIDERS_CH0 0x14
34#define FDC2214_REG_STATUS 0x18
35#define FDC2214_REG_ERROR_CONFIG 0x19
36#define FDC2214_REG_CONFIG 0x1A
37#define FDC2214_REG_MUX_CONFIG 0x1B
38#define FDC2214_REG_DRIVE_CURRENT_CH0 0x1E
39#define FDC2214_REG_MANUFACTURER_ID 0x7E
40#define FDC2214_REG_DEVICE_ID 0x7F
41
42#define FDC2214_MANUFACTURER_ID 0x5449 ///< "TI".
43#define FDC2214_DEVICE_ID 0x3055 ///< FDC2214 (the 12-bit FDC2114 reads 0x3054).
44
45/** @brief Largest config sequence in bytes: 7 register writes * 3 bytes (reg, msb, lsb). */
46#define FDC2214_CONFIG_MAX 21
47
48/** @brief Combine a DATA MSB register (low 12 bits) and DATA LSB register into the 28-bit result. */
49uint32_t fdc2214_data(uint16_t msb_reg, uint16_t lsb_reg);
50
51/** @brief The 4 error flags from the top of a DATA MSB register (bits 15:12). */
52uint8_t fdc2214_error(uint16_t msb_reg);
53
54/** @brief Sensor frequency in Hz for a 28-bit result against a reference clock: data / 2^28 * fref. */
55uint64_t fdc2214_sensor_freq_hz(uint32_t data28, uint32_t fref_hz);
56
57/**
58 * @brief Emit a single-channel (CH0) continuous-conversion bring-up as `(reg, val_msb, val_lsb)` triples.
59 *
60 * Writes RCOUNT, SETTLECOUNT, CLOCK_DIVIDERS, DRIVE_CURRENT, ERROR_CONFIG, MUX_CONFIG, then CONFIG last
61 * (CONFIG starts the conversion, so it must be written after the rest). Replay each triple as a 16-bit
62 * I2C register write.
63 * @return bytes written (7 * 3 = 21), or 0 if @p cap < FDC2214_CONFIG_MAX.
64 */
65size_t fdc2214_build_config(uint8_t *buf, size_t cap, uint16_t rcount, uint16_t settlecount);
66
67// --- ESP32 binding (I2C via Wire; no-ops on a host build) ------------------------------------
68
69/** @brief Verify the device id and apply the CH0 config at @p addr. @return true if present + acked. */
70bool fdc2214_begin(uint8_t addr, uint16_t rcount, uint16_t settlecount);
71
72/** @brief Read channel 0's 28-bit conversion result into @p out. @return false on I2C error. */
73bool fdc2214_read_ch0(uint32_t *out);
74
75#endif // DETERMINISTICESPASYNCWEBSERVER_FDC2214_H
uint32_t fdc2214_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.
uint8_t fdc2214_error(uint16_t msb_reg)
The 4 error flags from the top of a DATA MSB register (bits 15:12).
bool fdc2214_read_ch0(uint32_t *out)
Read channel 0's 28-bit conversion result into out.
bool fdc2214_begin(uint8_t addr, uint16_t rcount, uint16_t settlecount)
Verify the device id and apply the CH0 config at addr.
size_t fdc2214_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.
uint64_t fdc2214_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.