1 /*
2  * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 
8 #include <zephyr/kernel.h>
9 
10 #include <string.h>
11 #include "esp_check.h"
12 #include "driver/rtc_io.h"
13 #include "driver/dac_types_legacy.h"
14 #include "soc/dac_periph.h"
15 #include "hal/gpio_types.h"
16 #include "hal/dac_ll.h"
17 #include "clk_ctrl_os.h"
18 
19 extern int rtc_spinlock;
20 
21 #define RTC_ENTER_CRITICAL()    do { rtc_spinlock = irq_lock(); } while(0)
22 #define RTC_EXIT_CRITICAL()    irq_unlock(rtc_spinlock);
23 
24 static __attribute__((unused)) const char *TAG = "DAC";
25 
26 /*---------------------------------------------------------------
27                     DAC
28 ---------------------------------------------------------------*/
dac_pad_get_io_num(dac_channel_t channel,gpio_num_t * gpio_num)29 esp_err_t dac_pad_get_io_num(dac_channel_t channel, gpio_num_t *gpio_num)
30 {
31     ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
32 
33     *gpio_num = (gpio_num_t)dac_periph_signal.dac_channel_io_num[channel];
34 
35     return ESP_OK;
36 }
37 
dac_rtc_pad_init(dac_channel_t channel)38 static esp_err_t dac_rtc_pad_init(dac_channel_t channel)
39 {
40     ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
41 
42     gpio_num_t gpio_num = 0;
43     dac_pad_get_io_num(channel, &gpio_num);
44     rtc_gpio_init(gpio_num);
45     rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED);
46     rtc_gpio_pullup_dis(gpio_num);
47     rtc_gpio_pulldown_dis(gpio_num);
48 
49     return ESP_OK;
50 }
51 
dac_output_enable(dac_channel_t channel)52 esp_err_t dac_output_enable(dac_channel_t channel)
53 {
54     ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
55 
56     dac_rtc_pad_init(channel);
57     RTC_ENTER_CRITICAL();
58     dac_ll_power_on(channel);
59     dac_ll_rtc_sync_by_adc(false);
60     RTC_EXIT_CRITICAL();
61 
62     return ESP_OK;
63 }
64 
dac_output_disable(dac_channel_t channel)65 esp_err_t dac_output_disable(dac_channel_t channel)
66 {
67     ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
68 
69     RTC_ENTER_CRITICAL();
70     dac_ll_power_down(channel);
71     RTC_EXIT_CRITICAL();
72 
73     return ESP_OK;
74 }
75 
dac_output_voltage(dac_channel_t channel,uint8_t dac_value)76 esp_err_t dac_output_voltage(dac_channel_t channel, uint8_t dac_value)
77 {
78     ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
79 
80     RTC_ENTER_CRITICAL();
81     dac_ll_update_output_value(channel, dac_value);
82     RTC_EXIT_CRITICAL();
83 
84     return ESP_OK;
85 }
86 
dac_out_voltage(dac_channel_t channel,uint8_t dac_value)87 esp_err_t dac_out_voltage(dac_channel_t channel, uint8_t dac_value)
88 {
89     ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
90 
91     RTC_ENTER_CRITICAL();
92     dac_ll_update_output_value(channel, dac_value);
93     RTC_EXIT_CRITICAL();
94 
95     return ESP_OK;
96 }
97 
dac_cw_generator_enable(void)98 esp_err_t dac_cw_generator_enable(void)
99 {
100     RTC_ENTER_CRITICAL();
101     periph_rtc_dig_clk8m_enable();
102     dac_ll_cw_generator_enable();
103     RTC_EXIT_CRITICAL();
104 
105     return ESP_OK;
106 }
107 
dac_cw_generator_disable(void)108 esp_err_t dac_cw_generator_disable(void)
109 {
110     RTC_ENTER_CRITICAL();
111     dac_ll_cw_generator_disable();
112     periph_rtc_dig_clk8m_disable();
113     RTC_EXIT_CRITICAL();
114 
115     return ESP_OK;
116 }
117 
dac_cw_generator_config(dac_cw_config_t * cw)118 esp_err_t dac_cw_generator_config(dac_cw_config_t *cw)
119 {
120     ESP_RETURN_ON_FALSE(cw, ESP_ERR_INVALID_ARG, TAG, "invalid clock configuration");
121     RTC_ENTER_CRITICAL();
122     /* Enable the rtc8m clock temporary to get the correct frequency */
123     periph_rtc_dig_clk8m_enable();
124     uint32_t rtc_freq = periph_rtc_dig_clk8m_get_freq();
125     periph_rtc_dig_clk8m_disable();
126     dac_ll_cw_set_freq(cw->freq, rtc_freq);
127     dac_ll_cw_set_atten(cw->en_ch, (dac_cosine_atten_t)cw->scale);
128     dac_ll_cw_set_phase(cw->en_ch, (dac_cosine_phase_t)cw->phase);
129     dac_ll_cw_set_dc_offset(cw->en_ch, cw->offset);
130     dac_ll_cw_enable_channel(cw->en_ch, true);
131     RTC_EXIT_CRITICAL();
132 
133     return ESP_OK;
134 }
135