REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
AD840X.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3//
4// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
5
6#include "AD840X.h"
7
8const SPISettings functions::AD8402::DEFAULT_SPI_SETTINGS{4'000'000, MSBFIRST, SPI_MODE3};
9
10functions::AD8402::AD8402(bus::addr_t address) : functions::DataFunction(address, DEFAULT_SPI_SETTINGS) {}
11
12bool functions::AD8402::write_channel_raw(uint8_t ch, uint8_t raw) {
13 if (ch > 1)
14 return false;
15 transfer16(ch << 8 | raw);
16 return true;
17}
18
19const SPISettings functions::AD8403::DEFAULT_SPI_SETTINGS{100'000, MSBFIRST, SPI_MODE0};
20
21functions::AD8403::AD8403(bus::addr_t address) : functions::DataFunction(address, DEFAULT_SPI_SETTINGS) {}
22
23bool functions::AD8403::write_channel_raw(uint8_t ch, uint8_t raw) {
24 // The chip has a 10 bit shift register for receiving [2 bit address][8 bit value].
25 // The address bits must be sent first, followed by the value bits.
26 // When addressing (=CS) is disabled, the received command word is applied.
27 // Since we always send 16 bits, we must send 6 "empty" bits and have the command word in the last 10 bits we
28 // send.
29 if (ch > 3)
30 return false;
31 auto data_out = ch << 8 | raw;
32 transfer16(data_out);
33
34#ifdef ANABRID_PEDANTIC
35 // We also need to consider the shifting of bits when we send the data the second time in PEDANTIC mode.
36 // While we send the 6 empty bits, the previous data inside the shift register is already being clocked out.
37 // That means that the first 10 bits of data being read back are the last 10 bits we sent in.
38 auto read_back = transfer16(data_out);
39 return (read_back >> 6) == data_out;
40#else
41 return true;
42#endif
43}
bool write_channel_raw(uint8_t ch, uint8_t raw)
Channel can be 0 or 1. Will return false on invalid channels.
Definition AD840X.cpp:12
AD8402(bus::addr_t address)
Definition AD840X.cpp:10
static const SPISettings DEFAULT_SPI_SETTINGS
Definition AD840X.h:8
AD8403(bus::addr_t address)
Definition AD840X.cpp:21
static const SPISettings DEFAULT_SPI_SETTINGS
Definition AD840X.h:19
bool write_channel_raw(uint8_t ch, uint8_t raw)
Channel can be beteen 0 and 3.
Definition AD840X.cpp:23
A DataFunction class wraps SPI communication over the digital bus.
Definition functions.h:47
namespace for internal helpers
Definition icmd.h:6