REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
DAC60508.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
4
5#include "DAC60508.h"
6
7const SPISettings functions::DAC60508::DEFAULT_SPI_SETTINGS{4'000'000, MSBFIRST, SPI_MODE2};
8
9uint16_t functions::DAC60508::read_register(uint8_t address) const {
11 get_raw_spi().transfer((address & 0b0'000'1111) | 0b1'000'0000);
12 get_raw_spi().transfer16(0);
15 get_raw_spi().transfer(0);
16 auto data = get_raw_spi().transfer16(0);
18 return data;
19}
20
21bool functions::DAC60508::write_register(uint8_t address, uint16_t data) const {
22 begin_communication();
23 get_raw_spi().transfer(address & 0b0'000'1111);
24 get_raw_spi().transfer16(data);
25 end_communication();
26
27#ifdef ANABRID_PEDANTIC
28 return data == read_register(address);
29#endif
30 return true;
31}
32
33functions::DAC60508::DAC60508(bus::addr_t address, float reference_voltage)
34 : functions::DataFunction(address, DEFAULT_SPI_SETTINGS), v_ref(reference_voltage) {}
35
36uint16_t functions::DAC60508::float_to_raw(float value) const {
37 if (value <= 0.0f)
38 return RAW_ZERO;
39 if (value >= v_ref)
40 return RAW_MAX;
41 return static_cast<uint16_t>(roundf(value / v_ref * static_cast<float>(RAW_MAX)));
42}
43
44float functions::DAC60508::raw_to_float(uint16_t raw) const {
45 return static_cast<float>(raw) / static_cast<float>(0x0FFF) * v_ref;
46}
47
48bool functions::DAC60508::set_channel_raw(uint8_t idx, uint16_t value) const {
49 if (value > RAW_MAX)
50 return false;
51 return write_register(REG_DAC(idx), value << 4);
52}
53
54bool functions::DAC60508::set_channel(uint8_t idx, float value) const {
55 return write_register(REG_DAC(idx), float_to_raw(value) << 4);
56}
57
59 // It's unclear whether these settings are correct for all our purposes,
60 // use set_external_reference and set_double_gain to change them after initialization.
61 // Compare with https://www.ti.com/lit/ds/symlink/dac60508.pdf for details.
62 // REG_CONFIG = 0 sets internal reference, sane ALARM, SDO, no power-down
63 // REG_GAIN = 0 sets no ref voltage divisor and gain=1 for all channels
64 return write_register(functions::DAC60508::REG_CONFIG, 0b0000'0000'0000'0000) and
65 write_register(functions::DAC60508::REG_GAIN, 0b0000'0000'0000'0000);
66}
67
69 auto config = read_register(REG_CONFIG);
70 if (set)
71 config |= 0b00'00000'1'00000000;
72 else
73 config &= ~0b00'00000'1'00000000;
74 return write_register(REG_CONFIG, config);
75}
76
77bool functions::DAC60508::set_double_gain(uint8_t idx, bool set) const {
78 if (idx >= 8)
79 return false;
80 // Set GAIN for one channel
81 auto gain = read_register(REG_GAIN);
82 uint16_t mask = 1 << idx;
83 if (set)
84 gain |= mask;
85 else
86 gain &= ~mask;
87 return write_register(REG_GAIN, gain);
88}
89
91 // Set GAIN for all channels
92 auto gain = read_register(REG_GAIN);
93 uint16_t mask = 0x00FF;
94 if (set)
95 gain |= mask;
96 else
97 gain &= ~mask;
98 return write_register(REG_GAIN, gain);
99}
bool set_double_gain(uint8_t idx, bool set=true) const
Definition DAC60508.cpp:77
static constexpr uint8_t REG_GAIN
Definition DAC60508.h:23
static constexpr uint8_t REG_CONFIG
Definition DAC60508.h:22
DAC60508(bus::addr_t address, float reference_voltage=2.5f)
Definition DAC60508.cpp:33
bool set_channel(uint8_t idx, float value) const
Definition DAC60508.cpp:54
bool set_channel_raw(uint8_t idx, uint16_t value) const
Raw value can be between RAW_ZERO and RAW_MAX.
Definition DAC60508.cpp:48
bool set_external_reference(bool set=true) const
Definition DAC60508.cpp:68
uint16_t float_to_raw(float value) const
Value gets clamped to min and max values.
Definition DAC60508.cpp:36
bool init() const
Definition DAC60508.cpp:58
float raw_to_float(uint16_t value) const
Definition DAC60508.cpp:44
static const SPISettings DEFAULT_SPI_SETTINGS
Definition DAC60508.h:7
A DataFunction class wraps SPI communication over the digital bus.
Definition functions.h:47
void end_communication() const
static SPIClass & get_raw_spi()
void begin_communication() const
Definition functions.cpp:19
const bus::addr_t address
Definition functions.h:23
namespace for internal helpers
Definition icmd.h:6