1 // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /*******************************************************************************
16 * NOTICE
17 * The ll is not public api, don't use in application code.
18 * See readme.md in hal/include/hal/readme.md
19 ******************************************************************************/
20
21 #pragma once
22
23 #include <stdlib.h>
24 #include "soc/dac_periph.h"
25 #include "hal/dac_types.h"
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /**
32 * Power on dac module and start output voltage.
33 *
34 * @note Before powering up, make sure the DAC PAD is set to RTC PAD and floating status.
35 * @param channel DAC channel num.
36 */
dac_ll_power_on(dac_channel_t channel)37 static inline void dac_ll_power_on(dac_channel_t channel)
38 {
39 RTCIO.pad_dac[channel].dac_xpd_force = 1;
40 RTCIO.pad_dac[channel].xpd_dac = 1;
41 }
42
43 /**
44 * Power done dac module and stop output voltage.
45 *
46 * @param channel DAC channel num.
47 */
dac_ll_power_down(dac_channel_t channel)48 static inline void dac_ll_power_down(dac_channel_t channel)
49 {
50 RTCIO.pad_dac[channel].dac_xpd_force = 0;
51 RTCIO.pad_dac[channel].xpd_dac = 0;
52 }
53
54 /**
55 * Output voltage with value (8 bit).
56 *
57 * @param channel DAC channel num.
58 * @param value Output value. Value range: 0 ~ 255.
59 * The corresponding range of voltage is 0v ~ VDD3P3_RTC.
60 */
dac_ll_update_output_value(dac_channel_t channel,uint8_t value)61 static inline void dac_ll_update_output_value(dac_channel_t channel, uint8_t value)
62 {
63 if (channel == DAC_CHANNEL_1) {
64 SENS.sar_dac_ctrl2.dac_cw_en1 = 0;
65 RTCIO.pad_dac[channel].dac = value;
66 } else if (channel == DAC_CHANNEL_2) {
67 SENS.sar_dac_ctrl2.dac_cw_en2 = 0;
68 RTCIO.pad_dac[channel].dac = value;
69 }
70 }
71
72 /**
73 * Enable/disable the synchronization operation function of ADC1 and DAC.
74 *
75 * @note If enabled(default), ADC RTC controller sampling will cause the DAC channel output voltage.
76 *
77 * @param enable Enable or disable adc and dac synchronization function.
78 */
dac_ll_rtc_sync_by_adc(bool enable)79 static inline void dac_ll_rtc_sync_by_adc(bool enable)
80 {
81 SENS.sar_meas_ctrl2.sar1_dac_xpd_fsm = enable;
82 }
83
84 /************************************/
85 /* DAC cosine wave generator API's */
86 /************************************/
87 /**
88 * Enable cosine wave generator output.
89 */
dac_ll_cw_generator_enable(void)90 static inline void dac_ll_cw_generator_enable(void)
91 {
92 SENS.sar_dac_ctrl1.sw_tone_en = 1;
93 }
94
95 /**
96 * Disable cosine wave generator output.
97 */
dac_ll_cw_generator_disable(void)98 static inline void dac_ll_cw_generator_disable(void)
99 {
100 SENS.sar_dac_ctrl1.sw_tone_en = 0;
101 }
102
103 /**
104 * Enable the cosine wave generator of DAC channel.
105 *
106 * @param channel DAC channel num.
107 * @param enable
108 */
dac_ll_cw_set_channel(dac_channel_t channel,bool enable)109 static inline void dac_ll_cw_set_channel(dac_channel_t channel, bool enable)
110 {
111 if (channel == DAC_CHANNEL_1) {
112 SENS.sar_dac_ctrl2.dac_cw_en1 = enable;
113 } else if (channel == DAC_CHANNEL_2) {
114 SENS.sar_dac_ctrl2.dac_cw_en2 = enable;
115 }
116 }
117
118 /**
119 * Set frequency of cosine wave generator output.
120 *
121 * @note We know that CLK8M is about 8M, but don't know the actual value. so this freq have limited error.
122 * @param freq_hz CW generator frequency. Range: 130(130Hz) ~ 55000(100KHz).
123 */
dac_ll_cw_set_freq(uint32_t freq)124 static inline void dac_ll_cw_set_freq(uint32_t freq)
125 {
126 uint32_t sw_freq = freq * 0xFFFF / RTC_FAST_CLK_FREQ_APPROX;
127 SENS.sar_dac_ctrl1.sw_fstep = (sw_freq > 0xFFFF) ? 0xFFFF : sw_freq;
128 }
129
130 /**
131 * Set the amplitude of the cosine wave generator output.
132 *
133 * @param channel DAC channel num.
134 * @param scale The multiple of the amplitude. The max amplitude is VDD3P3_RTC.
135 */
dac_ll_cw_set_scale(dac_channel_t channel,dac_cw_scale_t scale)136 static inline void dac_ll_cw_set_scale(dac_channel_t channel, dac_cw_scale_t scale)
137 {
138 if (channel == DAC_CHANNEL_1) {
139 SENS.sar_dac_ctrl2.dac_scale1 = scale;
140 } else if (channel == DAC_CHANNEL_2) {
141 SENS.sar_dac_ctrl2.dac_scale2 = scale;
142 }
143 }
144
145 /**
146 * Set the phase of the cosine wave generator output.
147 *
148 * @param channel DAC channel num.
149 * @param scale Phase value.
150 */
dac_ll_cw_set_phase(dac_channel_t channel,dac_cw_phase_t phase)151 static inline void dac_ll_cw_set_phase(dac_channel_t channel, dac_cw_phase_t phase)
152 {
153 if (channel == DAC_CHANNEL_1) {
154 SENS.sar_dac_ctrl2.dac_inv1 = phase;
155 } else if (channel == DAC_CHANNEL_2) {
156 SENS.sar_dac_ctrl2.dac_inv2 = phase;
157 }
158 }
159
160 /**
161 * Set the voltage value of the DC component of the cosine wave generator output.
162 *
163 * @note The DC offset setting should be after phase setting.
164 * @note Unreasonable settings can cause the signal to be oversaturated.
165 * @param channel DAC channel num.
166 * @param offset DC value. Range: -128 ~ 127.
167 */
dac_ll_cw_set_dc_offset(dac_channel_t channel,int8_t offset)168 static inline void dac_ll_cw_set_dc_offset(dac_channel_t channel, int8_t offset)
169 {
170 if (channel == DAC_CHANNEL_1) {
171 if (SENS.sar_dac_ctrl2.dac_inv1 == DAC_CW_PHASE_180) {
172 offset = 0 - offset;
173 }
174 SENS.sar_dac_ctrl2.dac_dc1 = offset ? offset : (-128 - offset);
175 } else if (channel == DAC_CHANNEL_2) {
176 if (SENS.sar_dac_ctrl2.dac_inv2 == DAC_CW_PHASE_180) {
177 offset = 0 - offset;
178 }
179 SENS.sar_dac_ctrl2.dac_dc2 = offset ? offset : (-128 - offset);
180 }
181 }
182
183 /************************************/
184 /* DAC DMA API's */
185 /************************************/
186 /**
187 * Enable/disable DAC output data from I2S DMA.
188 * I2S_CLK connect to DAC_CLK, I2S_DATA_OUT connect to DAC_DATA.
189 */
dac_ll_digi_enable_dma(bool enable)190 static inline void dac_ll_digi_enable_dma(bool enable)
191 {
192 SENS.sar_dac_ctrl1.dac_dig_force = enable;
193 SENS.sar_dac_ctrl1.dac_clk_inv = enable;
194 }
195
196 #ifdef __cplusplus
197 }
198 #endif
199