• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <string.h>
17 #include "esp_log.h"
18 #include "esp_err.h"
19 #include "esp_osal/esp_osal.h"
20 #include "esp_osal/semphr.h"
21 #include "esp_osal/timers.h"
22 #include "driver/rtc_io.h"
23 #include "driver/dac.h"
24 #include "soc/dac_periph.h"
25 #include "hal/dac_hal.h"
26 
27 extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
28 
29 static const char *DAC_TAG = "DAC";
30 
31 #define DAC_CHECK(a, str, ret_val) ({                                               \
32     if (!(a)) {                                                                     \
33         ESP_LOGE(DAC_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str);                \
34         return (ret_val);                                                           \
35     }                                                                               \
36 })
37 
38 /*---------------------------------------------------------------
39                     DAC
40 ---------------------------------------------------------------*/
dac_pad_get_io_num(dac_channel_t channel,gpio_num_t * gpio_num)41 esp_err_t dac_pad_get_io_num(dac_channel_t channel, gpio_num_t *gpio_num)
42 {
43     DAC_CHECK(channel < DAC_CHANNEL_MAX, "DAC channel error", ESP_ERR_INVALID_ARG);
44 
45     *gpio_num = (gpio_num_t)dac_periph_signal.dac_channel_io_num[channel];
46 
47     return ESP_OK;
48 }
49 
dac_rtc_pad_init(dac_channel_t channel)50 static esp_err_t dac_rtc_pad_init(dac_channel_t channel)
51 {
52     DAC_CHECK(channel < DAC_CHANNEL_MAX, "DAC channel error", ESP_ERR_INVALID_ARG);
53 
54     gpio_num_t gpio_num = 0;
55     dac_pad_get_io_num(channel, &gpio_num);
56     rtc_gpio_init(gpio_num);
57     rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED);
58     rtc_gpio_pullup_dis(gpio_num);
59     rtc_gpio_pulldown_dis(gpio_num);
60 
61     return ESP_OK;
62 }
63 
dac_output_enable(dac_channel_t channel)64 esp_err_t dac_output_enable(dac_channel_t channel)
65 {
66     DAC_CHECK(channel < DAC_CHANNEL_MAX, "DAC channel error", ESP_ERR_INVALID_ARG);
67 
68     dac_rtc_pad_init(channel);
69     portENTER_CRITICAL(&rtc_spinlock);
70     dac_hal_power_on(channel);
71     dac_hal_rtc_sync_by_adc(false);
72     portEXIT_CRITICAL(&rtc_spinlock);
73 
74     return ESP_OK;
75 }
76 
dac_output_disable(dac_channel_t channel)77 esp_err_t dac_output_disable(dac_channel_t channel)
78 {
79     DAC_CHECK(channel < DAC_CHANNEL_MAX, "DAC channel error", ESP_ERR_INVALID_ARG);
80 
81     portENTER_CRITICAL(&rtc_spinlock);
82     dac_hal_power_down(channel);
83     portEXIT_CRITICAL(&rtc_spinlock);
84 
85     return ESP_OK;
86 }
87 
dac_output_voltage(dac_channel_t channel,uint8_t dac_value)88 esp_err_t dac_output_voltage(dac_channel_t channel, uint8_t dac_value)
89 {
90     DAC_CHECK(channel < DAC_CHANNEL_MAX, "DAC channel error", ESP_ERR_INVALID_ARG);
91 
92     portENTER_CRITICAL(&rtc_spinlock);
93     dac_hal_update_output_value(channel, dac_value);
94     portEXIT_CRITICAL(&rtc_spinlock);
95 
96     return ESP_OK;
97 }
98 
dac_out_voltage(dac_channel_t channel,uint8_t dac_value)99 esp_err_t dac_out_voltage(dac_channel_t channel, uint8_t dac_value)
100 {
101     DAC_CHECK(channel < DAC_CHANNEL_MAX, "DAC channel error", ESP_ERR_INVALID_ARG);
102 
103     portENTER_CRITICAL(&rtc_spinlock);
104     dac_hal_update_output_value(channel, dac_value);
105     portEXIT_CRITICAL(&rtc_spinlock);
106 
107     return ESP_OK;
108 }
109 
dac_cw_generator_enable(void)110 esp_err_t dac_cw_generator_enable(void)
111 {
112     portENTER_CRITICAL(&rtc_spinlock);
113     dac_hal_cw_generator_enable();
114     portEXIT_CRITICAL(&rtc_spinlock);
115 
116     return ESP_OK;
117 }
118 
dac_cw_generator_disable(void)119 esp_err_t dac_cw_generator_disable(void)
120 {
121     portENTER_CRITICAL(&rtc_spinlock);
122     dac_hal_cw_generator_disable();
123     portEXIT_CRITICAL(&rtc_spinlock);
124 
125     return ESP_OK;
126 }
127 
dac_cw_generator_config(dac_cw_config_t * cw)128 esp_err_t dac_cw_generator_config(dac_cw_config_t *cw)
129 {
130     if (!cw) return ESP_ERR_INVALID_ARG;
131 
132     portENTER_CRITICAL(&rtc_spinlock);
133     dac_hal_cw_generator_config(cw);
134     portEXIT_CRITICAL(&rtc_spinlock);
135 
136     return ESP_OK;
137 }
138