DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
fdc2214.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 fdc2214.cpp
6 * @brief FDC2114/2214 capacitance-to-digital codec + ESP32 binding (see fdc2214.h).
7 */
8
10#include "ServerConfig.h"
11
12#if DETWS_ENABLE_FDC2214
13
14uint32_t fdc2214_data(uint16_t msb_reg, uint16_t lsb_reg)
15{
16 return ((uint32_t)(msb_reg & 0x0FFF) << 16) | lsb_reg;
17}
18
19uint8_t fdc2214_error(uint16_t msb_reg)
20{
21 return (uint8_t)((msb_reg >> 12) & 0x0F);
22}
23
24uint64_t fdc2214_sensor_freq_hz(uint32_t data28, uint32_t fref_hz)
25{
26 return ((uint64_t)data28 * fref_hz) >> 28;
27}
28
29size_t fdc2214_build_config(uint8_t *buf, size_t cap, uint16_t rcount, uint16_t settlecount)
30{
31 if (!buf || cap < FDC2214_CONFIG_MAX)
32 return 0;
33 // (register, value) writes; CONFIG is written last because it starts the conversion.
34 const uint16_t seq[][2] = {
35 {FDC2214_REG_RCOUNT_CH0, rcount},
36 {FDC2214_REG_SETTLECOUNT_CH0, settlecount},
37 {FDC2214_REG_CLOCK_DIVIDERS_CH0, 0x1001}, // FIN_SEL=1, FREF_DIVIDER=1
38 {FDC2214_REG_DRIVE_CURRENT_CH0, 0x8C40}, // mid drive current
39 {FDC2214_REG_ERROR_CONFIG, 0x0000}, // no error reporting on INTB
40 {FDC2214_REG_MUX_CONFIG, 0x020D}, // single channel CH0, 10 MHz deglitch
41 {FDC2214_REG_CONFIG, 0x1E01}, // active CH0, internal ref, full current, start
42 };
43 size_t o = 0;
44 for (size_t i = 0; i < sizeof(seq) / sizeof(seq[0]); i++)
45 {
46 buf[o++] = (uint8_t)seq[i][0];
47 buf[o++] = (uint8_t)(seq[i][1] >> 8);
48 buf[o++] = (uint8_t)seq[i][1];
49 }
50 return o;
51}
52
53#if defined(ARDUINO)
54
55#include "services/i2c.h"
56#include <Arduino.h>
57#include <Wire.h>
58
59namespace
60{
61// All FDC2214 I2C-binding state, owned by one instance (internal linkage): the device address,
62// so it is one named owner, unreachable from any other translation unit.
63struct Fdc2214Ctx
64{
65 uint8_t addr = 0x2A;
66};
67Fdc2214Ctx s_fdc;
68
69bool read16(uint8_t reg, uint16_t *out)
70{
71 Wire.beginTransmission(s_fdc.addr);
72 Wire.write(reg);
73 if (Wire.endTransmission(false) != 0)
74 return false;
75 if (Wire.requestFrom((int)s_fdc.addr, 2) != 2)
76 return false;
77 uint16_t hi = Wire.read();
78 uint16_t lo = Wire.read();
79 *out = (uint16_t)((hi << 8) | lo);
80 return true;
81}
82
83bool write16(uint8_t reg, uint16_t val)
84{
85 Wire.beginTransmission(s_fdc.addr);
86 Wire.write(reg);
87 Wire.write((uint8_t)(val >> 8));
88 Wire.write((uint8_t)val);
89 return Wire.endTransmission() == 0;
90}
91} // namespace
92
93bool fdc2214_begin(uint8_t addr, uint16_t rcount, uint16_t settlecount)
94{
96 s_fdc.addr = addr;
97 uint16_t id = 0;
98 if (!read16(FDC2214_REG_DEVICE_ID, &id))
99 return false;
100 if (id != FDC2214_DEVICE_ID && id != 0x3054) // 0x3054 = FDC2114 (12-bit sibling)
101 return false;
102 uint8_t seq[FDC2214_CONFIG_MAX];
103 size_t n = fdc2214_build_config(seq, sizeof(seq), rcount, settlecount);
104 for (size_t i = 0; i + 3 <= n; i += 3)
105 if (!write16(seq[i], (uint16_t)((seq[i + 1] << 8) | seq[i + 2])))
106 return false;
107 return true;
108}
109
110bool fdc2214_read_ch0(uint32_t *out)
111{
112 if (!out)
113 return false;
114 uint16_t msb = 0;
115 uint16_t lsb = 0;
116 if (!read16(FDC2214_REG_DATA_CH0_MSB, &msb) || !read16(FDC2214_REG_DATA_CH0_LSB, &lsb))
117 return false;
118 *out = fdc2214_data(msb, lsb);
119 return true;
120}
121
122#endif // ARDUINO
123
124#endif // DETWS_ENABLE_FDC2214
User-facing configuration for DeterministicESPAsyncWebServer.
TI FDC2114/2214 capacitance-to-digital field-sensing codec (DETWS_ENABLE_FDC2214).
#define FDC2214_DEVICE_ID
FDC2214 (the 12-bit FDC2114 reads 0x3054).
Definition fdc2214.h:43
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.
#define FDC2214_REG_MUX_CONFIG
Definition fdc2214.h:37
#define FDC2214_REG_DEVICE_ID
Definition fdc2214.h:40
#define FDC2214_REG_DATA_CH0_LSB
Definition fdc2214.h:30
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.
#define FDC2214_REG_RCOUNT_CH0
Definition fdc2214.h:31
#define FDC2214_REG_DATA_CH0_MSB
Definition fdc2214.h:29
#define FDC2214_REG_CLOCK_DIVIDERS_CH0
Definition fdc2214.h:33
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.
#define FDC2214_CONFIG_MAX
Largest config sequence in bytes: 7 register writes * 3 bytes (reg, msb, lsb).
Definition fdc2214.h:46
#define FDC2214_REG_DRIVE_CURRENT_CH0
Definition fdc2214.h:38
#define FDC2214_REG_SETTLECOUNT_CH0
Definition fdc2214.h:32
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.
#define FDC2214_REG_CONFIG
Definition fdc2214.h:36
#define FDC2214_REG_ERROR_CONFIG
Definition fdc2214.h:35
The one owner of the shared I2C bus bring-up for the peripheral drivers.
void detws_i2c_begin()
Bring up the shared I2C bus on DETWS_I2C_SDA_PIN / DETWS_I2C_SCL_PIN (-1 = default).
Definition i2c.h:33