M5Unit-ANADIG 0.1.0 git rev:3fbd370
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
23enum class PowerDown : uint8_t {
24 Normal,
25 OHM_1K,
26 OHM_100K,
28};
29
30} // namespace mcp4725
31
36class UnitMCP4725 : public Component {
37 M5_UNIT_COMPONENT_HPP_BUILDER(UnitMCP4725, 0x60);
38
39public:
40 static constexpr uint16_t RESOLUTION{0x0FFF}; // 12bits
41 static constexpr float MAXIMUM_VOLTAGE{3300.f}; // mV
42
44 static inline float raw_to_voltage(const uint16_t raw, const float supply_voltage = 5000.f)
45 {
46 return static_cast<float>(raw) * supply_voltage / RESOLUTION;
47 }
49 static inline uint16_t voltage_to_raw(const float mv, const float supply_voltage = 5000.f)
50 {
51 float val = std::fmin(std::fmax(mv, 0.0f), MAXIMUM_VOLTAGE);
52 return static_cast<uint16_t>((val / supply_voltage) * RESOLUTION);
53 }
54
59 struct config_t {
63 float supply_voltage{5000.f};
64 };
65
66 explicit UnitMCP4725(const uint8_t addr = DEFAULT_ADDRESS) : Component(addr)
67 {
68 auto ccfg = component_config();
69 ccfg.clock = 400 * 1000U;
70 component_config(ccfg);
71 }
72 virtual ~UnitMCP4725()
73 {
74 }
75
76 virtual bool begin() override;
77
80
82 {
83 return _cfg;
84 }
86 inline void config(const config_t& cfg)
87 {
88 _cfg = cfg;
89 }
91
96 {
97 return _powerDown;
98 }
100 inline uint16_t lastValue() const
101 {
102 return _lastValue;
103 }
105
108
114 bool writePowerDown(const mcp4725::PowerDown pd);
116
120
126 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
127 inline bool writeVoltage(const T mv)
128 {
129 return (mv >= 0.0f) && writeVoltage(voltage_to_raw((float)mv, _cfg.supply_voltage));
130 }
137 inline bool writeVoltage(const uint16_t raw)
138 {
139 return write_voltage(Command::FastMode, raw);
140 }
142 template <typename T, typename std::enable_if<!std::is_same<uint16_t, T>::value && std::is_unsigned<T>::value,
143 std::nullptr_t>::type = nullptr>
144 inline bool writeVoltage(const T raw)
145 {
146 return writeVoltage(static_cast<uint16_t>(raw & RESOLUTION));
147 }
148
150
154
161 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
162 inline bool writeVoltageAndEEPROM(const T mv, const bool blocking = true)
163 {
164 return (mv >= 0.0f) && writeVoltageAndEEPROM(voltage_to_raw((float)mv, _cfg.supply_voltage), blocking);
165 }
166
173 bool writeVoltageAndEEPROM(const uint16_t raw, const bool blocking = true);
174
176 template <typename T, typename std::enable_if<!std::is_same<uint16_t, T>::value && std::is_unsigned<T>::value,
177 std::nullptr_t>::type = nullptr>
178 bool writeVoltageAndEEPROM(const T raw, const bool blocking = true)
179 {
180 return writeVoltageAndEEPROM(static_cast<uint16_t>(raw & RESOLUTION), blocking);
181 }
183
191 bool generalReset();
192
199 bool readDACRegister(mcp4725::PowerDown& pd, uint16_t& raw);
206 bool readEEPROM(mcp4725::PowerDown& pd, uint16_t& raw);
207
208protected:
209 enum class Command : uint8_t {
210 FastMode, // 2bytes [0:0:PD1:PD0:D11:D10:D9:D8] [D7... D0]
211 WriteDAC, // 3bytes [0:1:0:X:X:PD1:PD0:X] [D11...D4] [D3:D2:D1:D0:X:X:X:X]
212 WriteDACAndEEPROM, // 3bytes [0:1:1:X:X:PD1:PD0:X] [D11...D4] [D3:D2:D1:D0:X:X:X:X]
213 };
214
215 bool write_voltage(const Command cmd, const uint16_t raw);
216 bool is_eeprom_ready();
217 uint32_t make_buffer(uint8_t buf[3], const uint16_t raw, const Command cmd);
218 bool read_status(uint8_t rbuf[5]);
219
220private:
221 mcp4725::PowerDown _powerDown{};
222 uint16_t _lastValue{};
223 config_t _cfg{};
224};
225
226} // namespace unit
227} // namespace m5
228
229#endif
Digital-to-analog signal conversion unit.
Definition unit_MCP4725.hpp:36
bool writeVoltage(const uint16_t raw)
Output the voltage.
Definition unit_MCP4725.hpp:137
bool writePowerDown(const mcp4725::PowerDown pd)
Write the power down mode.
Definition unit_MCP4725.cpp:88
void config(const config_t &cfg)
Set the configration.
Definition unit_MCP4725.hpp:86
static uint16_t voltage_to_raw(const float mv, const float supply_voltage=5000.f)
Voltage(mV) to raw value.
Definition unit_MCP4725.hpp:49
bool writeVoltageAndEEPROM(const T raw, const bool blocking=true)
Write to DAC register and EEPROM.
Definition unit_MCP4725.hpp:178
uint16_t lastValue() const
Gets the last output raw value.
Definition unit_MCP4725.hpp:100
mcp4725::PowerDown powerDown() const
Gets the iner power down mode.
Definition unit_MCP4725.hpp:95
bool readEEPROM(mcp4725::PowerDown &pd, uint16_t &raw)
Read the EEPROM settings.
Definition unit_MCP4725.cpp:105
bool readDACRegister(mcp4725::PowerDown &pd, uint16_t &raw)
Read the DAC register.
Definition unit_MCP4725.cpp:94
bool writeVoltage(const T raw)
Output the voltage.
Definition unit_MCP4725.hpp:144
bool writeVoltage(const T mv)
Output the voltage.
Definition unit_MCP4725.hpp:127
bool generalReset()
General reset.
Definition unit_MCP4725.cpp:79
config_t config()
Gets the configration.
Definition unit_MCP4725.hpp:81
bool writeVoltageAndEEPROM(const T mv, const bool blocking=true)
Write to DAC register and EEPROM.
Definition unit_MCP4725.hpp:162
static float raw_to_voltage(const uint16_t raw, const float supply_voltage=5000.f)
Raw value to voltage(mV)
Definition unit_MCP4725.hpp:44
Top level namespace of M5stack.
For MCP4725.
Unit-related namespace.
Settings for begin.
Definition unit_MCP4725.hpp:59
bool using_eeprom_settings
Using EEPROM settings on begin?
Definition unit_MCP4725.hpp:61
float supply_voltage
Voltage supplied mV (Used to calculate output values)
Definition unit_MCP4725.hpp:63
PowerDown
Definition unit_MCP4725.hpp:23
@ OHM_500K
500k ohm resistor to ground
@ OHM_100K
100k ohm resistor to ground
@ OHM_1K
1k ohm resistor to ground