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, uint8_t alt_function_idx)
11 : functions::DataFunction(address, alt_function_idx, DEFAULT_SPI_SETTINGS) {}
12
13bool functions::AD8402::write_channel_raw(uint8_t ch, uint8_t raw) {
14 if (ch > 1)
15 return false;
16 transfer16(ch << 8 | raw);
17 return true;
18}
19
20const SPISettings functions::AD8403::DEFAULT_SPI_SETTINGS{100'000, MSBFIRST, SPI_MODE0};
21
22functions::AD8403::AD8403(bus::addr_t address, uint8_t alt_function_idx)
23 : functions::DataFunction(address, alt_function_idx, DEFAULT_SPI_SETTINGS) {}
24
25bool functions::AD8403::write_channel_raw(uint8_t ch, uint8_t raw) {
26 // The chip has a 10 bit shift register for receiving [2 bit address][8 bit value].
27 // The address bits must be sent first, followed by the value bits.
28 // When addressing (=CS) is disabled, the received command word is applied.
29 // Since we always send 16 bits, we must send 6 "empty" bits and have the command word in the last 10 bits we
30 // send.
31 if (ch > 3)
32 return false;
33 auto data_out = ch << 8 | raw;
34 transfer16(data_out);
35
36#ifdef ANABRID_PEDANTIC
37 // We also need to consider the shifting of bits when we send the data the second time in PEDANTIC mode.
38 // While we send the 6 empty bits, the previous data inside the shift register is already being clocked out.
39 // That means that the first 10 bits of data being read back are the last 10 bits we sent in.
40 auto read_back = transfer16(data_out);
41 return (read_back >> 6) == data_out;
42#else
43 return true;
44#endif
45}
AD8402(bus::addr_t address, uint8_t alt_function_idx)
Definition AD840X.cpp:10
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:13
DataFunction(bus::addr_t address, uint8_t alt_function_idx, const SPISettings &spiSettings)
Definition functions.cpp:24
static const SPISettings DEFAULT_SPI_SETTINGS
Definition AD840X.h:8
AD8403(bus::addr_t address, uint8_t alt_function_idx)
Definition AD840X.cpp:22
DataFunction(bus::addr_t address, uint8_t alt_function_idx, const SPISettings &spiSettings)
Definition functions.cpp:24
static const SPISettings DEFAULT_SPI_SETTINGS
Definition AD840X.h:20
bool write_channel_raw(uint8_t ch, uint8_t raw)
Channel can be beteen 0 and 3.
Definition AD840X.cpp:25
uint16_t transfer16(uint16_t data_in) const
Definition functions.cpp:81
const bus::addr_t address
Definition functions.h:23
namespace for internal helpers
Definition icmd.h:6