REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
AD9834.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 "AD9834.h"
7
8const SPISettings functions::AD9834::DEFAULT_SPI_SETTINGS{4'000'000, MSBFIRST, SPI_MODE2};
9
10functions::AD9834::AD9834(bus::addr_t address) : functions::DataFunction(address, DEFAULT_SPI_SETTINGS) {}
11
13 control_register |= CONTROL_B28;
14 control_register |= CONTROL_PINSW;
15 control_register |= CONTROL_SLEEP1;
16 control_register |= CONTROL_SLEEP12;
17 control_register |= CONTROL_DIV2;
18
19 begin_communication();
20 get_raw_spi().transfer16(CONTROL_RESET);
21 get_raw_spi().transfer16(control_register);
22 end_communication();
23
24 // TODO: Check if pedantic mode can be implemented
25 return true;
26}
27
29 control_register |= CONTROL_SLEEP1;
30 control_register |= CONTROL_SLEEP12;
31
32 _write_control_register();
33}
34
36 control_register &= ~CONTROL_SLEEP1;
37 control_register &= ~CONTROL_SLEEP12;
38
39 _write_control_register();
40}
41
42void functions::AD9834::write_wave_form(WaveForm wave_form) {
43 switch (wave_form) {
44 case WaveForm::TRIANGLE:
45 control_register &= ~ENABLE_SQUARE_WAVE;
46 control_register |= ENABLE_TRIANGLE_WAVE;
47 break;
48 case WaveForm::SINE_AND_SQUARE:
49 control_register &= ~ENABLE_TRIANGLE_WAVE;
50 control_register |= ENABLE_SQUARE_WAVE;
51 break;
52 case WaveForm::SINE:
53 control_register &= ~ENABLE_TRIANGLE_WAVE;
54 control_register &= ~ENABLE_SQUARE_WAVE;
55 break;
56 }
57 _write_control_register();
58}
59
61 freq_register = static_cast<uint32_t>((freq * static_cast<float>(1 << 28)) / MASTER_CLK);
62
63 begin_communication();
64 get_raw_spi().transfer16((freq_register & REG_FREQ_MASK) | REG_FREQ_0); // Frequency register 0 LSB
65 get_raw_spi().transfer16(((freq_register >> 14) & REG_FREQ_MASK) | REG_FREQ_0); // Frequency register 0 MSB
66 end_communication();
67}
68
70 phase_register = static_cast<uint16_t>(fmod(phase, 2.0f * PI) * 4096.0f / (2.0f * PI));
71
72 begin_communication();
73 get_raw_spi().transfer16((phase_register & REG_PHASE_MASK) | REG_PHASE_0); // Phase register 0
74 end_communication();
75}
76
78 return static_cast<float>(freq_register * MASTER_CLK / static_cast<float>(1 << 28));
79}
80
82 return static_cast<float>(phase_register) / 4096.0f * 2.0f * PI;
83}
84
85void functions::AD9834::_write_control_register() const {
86 begin_communication();
87 get_raw_spi().transfer16(control_register);
88 end_communication();
89}
void write_phase(float phase)
Sets the phase of the outputs synchronised to the reset pin. Possible values are mapped to [0,...
Definition AD9834.cpp:69
void awake()
Resumes outputs to regular operation, according to the previously specified frequencies.
Definition AD9834.cpp:35
void write_frequency(float freq)
Sets the frequency of the sine / triangle output in Hz.
Definition AD9834.cpp:60
float get_real_phase() const
Returns the actually set phase, containing rounding errors. Possible values are [0,...
Definition AD9834.cpp:81
void write_wave_form(WaveForm wave_form)
Sets the wave form of the function generator output.
Definition AD9834.cpp:42
void sleep()
Sets the sine / triangle output to zero. The square output will stay at high or low level.
Definition AD9834.cpp:28
AD9834(bus::addr_t address)
Definition AD9834.cpp:10
bool init()
Initialises the chip and puts it into sleep mode.
Definition AD9834.cpp:12
static const SPISettings DEFAULT_SPI_SETTINGS
Definition AD9834.h:8
float get_real_frequency() const
Returns the actually set frequency, containing rounding errors.
Definition AD9834.cpp:77
A DataFunction class wraps SPI communication over the digital bus.
Definition functions.h:47
uint32_t
Definition flasher.cpp:195
namespace for internal helpers
Definition icmd.h:6