M5Unit-ANADIG 0.4.1 git rev:a449179
Loading...
Searching...
No Matches
unit_MCP4725.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_ANADIG_UNIT_MCP4725_HPP
11#define M5_UNIT_ANADIG_UNIT_MCP4725_HPP
12#include <M5UnitComponent.hpp>
13
14namespace m5 {
15namespace unit {
16
21namespace mcp4725 {
22
27enum class PowerDown : uint8_t {
28 Normal,
29 OHM_1K,
30 OHM_100K,
32};
33
34} // namespace mcp4725
35
40class UnitMCP4725 : public Component {
41 M5_UNIT_COMPONENT_HPP_BUILDER(UnitMCP4725, 0x60);
42
43public:
45 static constexpr uint16_t RESOLUTION{0x0FFF};
46
53 static inline float raw_to_voltage(const uint16_t raw, const float supply_voltage)
54 {
55 return static_cast<float>(raw) * supply_voltage / 4096.0f;
56 }
63 static inline uint16_t voltage_to_raw(const float mv, const float supply_voltage)
64 {
65 float val = m5::stl::clamp(mv, 0.0f, supply_voltage);
66 uint32_t raw = static_cast<uint32_t>((val / supply_voltage) * 4096.0f);
67 return static_cast<uint16_t>(raw > RESOLUTION ? RESOLUTION : raw);
68 }
69
74 struct config_t {
83 float supply_voltage{5000.f};
86 float saturation_voltage{3300.f};
87 };
88
93 explicit UnitMCP4725(const uint8_t addr = DEFAULT_ADDRESS) : Component(addr)
94 {
95 auto ccfg = component_config();
96 ccfg.clock = 400 * 1000U;
97 component_config(ccfg);
98 }
99 virtual ~UnitMCP4725()
100 {
101 }
102
107 virtual bool begin() override;
108
111
113 {
114 return _cfg;
115 }
117 inline void config(const config_t& cfg)
118 {
119 _cfg = cfg;
120 }
122
127 {
128 return _power_down;
129 }
131 inline uint16_t lastValue() const
132 {
133 return _last_value;
134 }
136
139
145 bool writePowerDown(const mcp4725::PowerDown pd);
147
151
157 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
158 inline bool writeVoltage(const T mv)
159 {
160 float limit = (_cfg.saturation_voltage > 0.f) ? _cfg.saturation_voltage : _cfg.supply_voltage;
161 return (mv >= 0.0f) &&
162 writeVoltage(voltage_to_raw(std::min(static_cast<float>(mv), limit), _cfg.supply_voltage));
163 }
170 inline bool writeVoltage(const uint16_t raw)
171 {
172 return write_voltage(Command::FastMode, raw);
173 }
180 template <typename T, typename std::enable_if<!std::is_same<uint16_t, T>::value && std::is_unsigned<T>::value,
181 std::nullptr_t>::type = nullptr>
182 inline bool writeVoltage(const T raw)
183 {
184 return writeVoltage(static_cast<uint16_t>(raw & RESOLUTION));
185 }
186
188
192
199 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
200 inline bool writeVoltageAndEEPROM(const T mv, const bool blocking = true)
201 {
202 float limit = (_cfg.saturation_voltage > 0.f) ? _cfg.saturation_voltage : _cfg.supply_voltage;
203 return (mv >= 0.0f) &&
204 writeVoltageAndEEPROM(voltage_to_raw(std::min(static_cast<float>(mv), limit), _cfg.supply_voltage),
205 blocking);
206 }
207
215 bool writeVoltageAndEEPROM(const uint16_t raw, const bool blocking = true);
216
224 template <typename T, typename std::enable_if<!std::is_same<uint16_t, T>::value && std::is_unsigned<T>::value,
225 std::nullptr_t>::type = nullptr>
226 bool writeVoltageAndEEPROM(const T raw, const bool blocking = true)
227 {
228 return writeVoltageAndEEPROM(static_cast<uint16_t>(raw & RESOLUTION), blocking);
229 }
231
241 bool generalReset();
242
249 bool readDACRegister(mcp4725::PowerDown& pd, uint16_t& raw);
256 bool readEEPROM(mcp4725::PowerDown& pd, uint16_t& raw);
257
258protected:
259 enum class Command : uint8_t {
260 FastMode, // 2bytes [0:0:PD1:PD0:D11:D10:D9:D8] [D7... D0]
261 WriteDAC, // 3bytes [0:1:0:X:X:PD1:PD0:X] [D11...D4] [D3:D2:D1:D0:X:X:X:X]
262 WriteDACAndEEPROM, // 3bytes [0:1:1:X:X:PD1:PD0:X] [D11...D4] [D3:D2:D1:D0:X:X:X:X]
263 };
264
265 bool write_voltage(const Command cmd, const uint16_t raw);
266 bool is_eeprom_ready();
267 uint32_t make_buffer(uint8_t buf[3], const uint16_t raw, const Command cmd) const;
268 bool read_status(uint8_t rbuf[5]);
269
270private:
271 mcp4725::PowerDown _power_down{};
272 uint16_t _last_value{};
273 config_t _cfg{};
274};
275
276} // namespace unit
277} // namespace m5
278
279#endif
Digital-to-analog signal conversion unit.
Definition unit_MCP4725.hpp:40
static float raw_to_voltage(const uint16_t raw, const float supply_voltage)
Raw value to voltage(mV)
Definition unit_MCP4725.hpp:53
bool writeVoltage(const uint16_t raw)
Output the voltage.
Definition unit_MCP4725.hpp:170
bool writePowerDown(const mcp4725::PowerDown pd)
Write the power down mode.
Definition unit_MCP4725.cpp:106
void config(const config_t &cfg)
Set the configuration.
Definition unit_MCP4725.hpp:117
static uint16_t voltage_to_raw(const float mv, const float supply_voltage)
Voltage(mV) to raw value.
Definition unit_MCP4725.hpp:63
static constexpr uint16_t RESOLUTION
Maximum raw output value (12 bits)
Definition unit_MCP4725.hpp:45
bool writeVoltageAndEEPROM(const T raw, const bool blocking=true)
Write to DAC register and EEPROM.
Definition unit_MCP4725.hpp:226
uint16_t lastValue() const
Gets the last output raw value.
Definition unit_MCP4725.hpp:131
virtual bool begin() override
Initialize the unit and apply config_t.
Definition unit_MCP4725.cpp:43
mcp4725::PowerDown powerDown() const
Gets the inner power down mode.
Definition unit_MCP4725.hpp:126
bool readEEPROM(mcp4725::PowerDown &pd, uint16_t &raw)
Read the EEPROM settings.
Definition unit_MCP4725.cpp:122
bool readDACRegister(mcp4725::PowerDown &pd, uint16_t &raw)
Read the DAC register.
Definition unit_MCP4725.cpp:112
bool writeVoltage(const T raw)
Output the voltage.
Definition unit_MCP4725.hpp:182
bool writeVoltage(const T mv)
Output the voltage.
Definition unit_MCP4725.hpp:158
UnitMCP4725(const uint8_t addr=DEFAULT_ADDRESS)
Constructor.
Definition unit_MCP4725.hpp:93
bool generalReset()
General reset.
Definition unit_MCP4725.cpp:97
config_t config()
Gets the configuration.
Definition unit_MCP4725.hpp:112
bool writeVoltageAndEEPROM(const T mv, const bool blocking=true)
Write to DAC register and EEPROM.
Definition unit_MCP4725.hpp:200
Top level namespace of M5Stack.
For MCP4725.
Unit-related namespace.
Settings for begin.
Definition unit_MCP4725.hpp:74
bool using_eeprom_settings
Definition unit_MCP4725.hpp:81
float saturation_voltage
Definition unit_MCP4725.hpp:86
float supply_voltage
VDD of MCP4725 chip (mV). Used for raw <-> voltage conversion.
Definition unit_MCP4725.hpp:83
PowerDown
Power-down mode.
Definition unit_MCP4725.hpp:27
@ OHM_500K
500k ohm resistor to ground
@ OHM_100K
100k ohm resistor to ground
@ OHM_1K
1k ohm resistor to ground