M5Unit-ANADIG 0.3.0 git rev:6ce23e4
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:
44 static constexpr uint16_t RESOLUTION{0x0FFF}; // 12bits max code
45
47 static inline float raw_to_voltage(const uint16_t raw, const float supply_voltage)
48 {
49 return static_cast<float>(raw) * supply_voltage / 4096.0f;
50 }
52 static inline uint16_t voltage_to_raw(const float mv, const float supply_voltage)
53 {
54 float val = m5::stl::clamp(mv, 0.0f, supply_voltage);
55 uint32_t raw = static_cast<uint32_t>((val / supply_voltage) * 4096.0f);
56 return static_cast<uint16_t>(raw > RESOLUTION ? RESOLUTION : raw);
57 }
58
63 struct config_t {
67 float supply_voltage{5000.f};
70 float saturation_voltage{3300.f};
71 };
72
73 explicit UnitMCP4725(const uint8_t addr = DEFAULT_ADDRESS) : Component(addr)
74 {
75 auto ccfg = component_config();
76 ccfg.clock = 400 * 1000U;
77 component_config(ccfg);
78 }
79 virtual ~UnitMCP4725()
80 {
81 }
82
83 virtual bool begin() override;
84
87
89 {
90 return _cfg;
91 }
93 inline void config(const config_t& cfg)
94 {
95 _cfg = cfg;
96 }
98
103 {
104 return _powerDown;
105 }
107 inline uint16_t lastValue() const
108 {
109 return _lastValue;
110 }
112
115
121 bool writePowerDown(const mcp4725::PowerDown pd);
123
127
133 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
134 inline bool writeVoltage(const T mv)
135 {
136 float limit = (_cfg.saturation_voltage > 0.f) ? _cfg.saturation_voltage : _cfg.supply_voltage;
137 return (mv >= 0.0f) &&
138 writeVoltage(voltage_to_raw(std::min(static_cast<float>(mv), limit), _cfg.supply_voltage));
139 }
146 inline bool writeVoltage(const uint16_t raw)
147 {
148 return write_voltage(Command::FastMode, raw);
149 }
151 template <typename T, typename std::enable_if<!std::is_same<uint16_t, T>::value && std::is_unsigned<T>::value,
152 std::nullptr_t>::type = nullptr>
153 inline bool writeVoltage(const T raw)
154 {
155 return writeVoltage(static_cast<uint16_t>(raw & RESOLUTION));
156 }
157
159
163
170 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
171 inline bool writeVoltageAndEEPROM(const T mv, const bool blocking = true)
172 {
173 float limit = (_cfg.saturation_voltage > 0.f) ? _cfg.saturation_voltage : _cfg.supply_voltage;
174 return (mv >= 0.0f) &&
175 writeVoltageAndEEPROM(voltage_to_raw(std::min(static_cast<float>(mv), limit), _cfg.supply_voltage),
176 blocking);
177 }
178
185 bool writeVoltageAndEEPROM(const uint16_t raw, const bool blocking = true);
186
188 template <typename T, typename std::enable_if<!std::is_same<uint16_t, T>::value && std::is_unsigned<T>::value,
189 std::nullptr_t>::type = nullptr>
190 bool writeVoltageAndEEPROM(const T raw, const bool blocking = true)
191 {
192 return writeVoltageAndEEPROM(static_cast<uint16_t>(raw & RESOLUTION), blocking);
193 }
195
205 bool generalReset();
206
213 bool readDACRegister(mcp4725::PowerDown& pd, uint16_t& raw);
220 bool readEEPROM(mcp4725::PowerDown& pd, uint16_t& raw);
221
222protected:
223 enum class Command : uint8_t {
224 FastMode, // 2bytes [0:0:PD1:PD0:D11:D10:D9:D8] [D7... D0]
225 WriteDAC, // 3bytes [0:1:0:X:X:PD1:PD0:X] [D11...D4] [D3:D2:D1:D0:X:X:X:X]
226 WriteDACAndEEPROM, // 3bytes [0:1:1:X:X:PD1:PD0:X] [D11...D4] [D3:D2:D1:D0:X:X:X:X]
227 };
228
229 bool write_voltage(const Command cmd, const uint16_t raw);
230 bool is_eeprom_ready();
231 uint32_t make_buffer(uint8_t buf[3], const uint16_t raw, const Command cmd);
232 bool read_status(uint8_t rbuf[5]);
233
234private:
235 mcp4725::PowerDown _powerDown{};
236 uint16_t _lastValue{};
237 config_t _cfg{};
238};
239
240} // namespace unit
241} // namespace m5
242
243#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:47
bool writeVoltage(const uint16_t raw)
Output the voltage.
Definition unit_MCP4725.hpp:146
bool writePowerDown(const mcp4725::PowerDown pd)
Write the power down mode.
Definition unit_MCP4725.cpp:87
void config(const config_t &cfg)
Set the configuration.
Definition unit_MCP4725.hpp:93
static uint16_t voltage_to_raw(const float mv, const float supply_voltage)
Voltage(mV) to raw value.
Definition unit_MCP4725.hpp:52
bool writeVoltageAndEEPROM(const T raw, const bool blocking=true)
Write to DAC register and EEPROM.
Definition unit_MCP4725.hpp:190
uint16_t lastValue() const
Gets the last output raw value.
Definition unit_MCP4725.hpp:107
mcp4725::PowerDown powerDown() const
Gets the inner power down mode.
Definition unit_MCP4725.hpp:102
bool readEEPROM(mcp4725::PowerDown &pd, uint16_t &raw)
Read the EEPROM settings.
Definition unit_MCP4725.cpp:104
bool readDACRegister(mcp4725::PowerDown &pd, uint16_t &raw)
Read the DAC register.
Definition unit_MCP4725.cpp:93
bool writeVoltage(const T raw)
Output the voltage.
Definition unit_MCP4725.hpp:153
bool writeVoltage(const T mv)
Output the voltage.
Definition unit_MCP4725.hpp:134
bool generalReset()
General reset.
Definition unit_MCP4725.cpp:78
config_t config()
Gets the configuration.
Definition unit_MCP4725.hpp:88
bool writeVoltageAndEEPROM(const T mv, const bool blocking=true)
Write to DAC register and EEPROM.
Definition unit_MCP4725.hpp:171
Top level namespace of M5Stack.
For MCP4725.
Unit-related namespace.
Settings for begin.
Definition unit_MCP4725.hpp:63
bool using_eeprom_settings
Using EEPROM settings on begin?
Definition unit_MCP4725.hpp:65
float saturation_voltage
Definition unit_MCP4725.hpp:70
float supply_voltage
VDD of MCP4725 chip (mV). Used for raw <-> voltage conversion.
Definition unit_MCP4725.hpp:67
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