1 /*
2 * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7
8 #include <string.h>
9 #include "esp_log.h"
10 #include "esp_err.h"
11 #include "esp_check.h"
12 #include "esp_pm.h"
13 #include "freertos/FreeRTOS.h"
14 #include "freertos/semphr.h"
15 #include "freertos/timers.h"
16 #include "driver/rtc_io.h"
17 #include "driver/dac.h"
18 #include "soc/dac_periph.h"
19 #include "hal/dac_hal.h"
20
21 static const char *TAG = "DAC";
22
23 extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
24 #define DAC_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
25 #define DAC_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
26
27 #ifdef CONFIG_PM_ENABLE
28 static esp_pm_lock_handle_t s_dac_digi_lock = NULL;
29 #endif //CONFIG_PM_ENABLE
30
31 /*---------------------------------------------------------------
32 Digital controller setting
33 ---------------------------------------------------------------*/
34
dac_digi_init(void)35 esp_err_t dac_digi_init(void)
36 {
37 DAC_ENTER_CRITICAL();
38 dac_hal_digi_init();
39 DAC_EXIT_CRITICAL();
40
41 return ESP_OK;
42 }
43
dac_digi_deinit(void)44 esp_err_t dac_digi_deinit(void)
45 {
46 #ifdef CONFIG_PM_ENABLE
47 if (s_dac_digi_lock) {
48 esp_pm_lock_delete(s_dac_digi_lock);
49 s_dac_digi_lock = NULL;
50 }
51 #endif
52 DAC_ENTER_CRITICAL();
53 dac_hal_digi_deinit();
54 DAC_EXIT_CRITICAL();
55
56 return ESP_OK;
57 }
58
dac_digi_controller_config(const dac_digi_config_t * cfg)59 esp_err_t dac_digi_controller_config(const dac_digi_config_t *cfg)
60 {
61 ESP_RETURN_ON_FALSE(cfg->mode < DAC_CONV_MAX, ESP_ERR_INVALID_ARG, TAG, "DAC mode error");
62 ESP_RETURN_ON_FALSE(cfg->interval > 0 && cfg->interval < 4096, ESP_ERR_INVALID_ARG, TAG, "DAC interval error");
63 ESP_RETURN_ON_FALSE(cfg->dig_clk.div_num < 256, ESP_ERR_INVALID_ARG, TAG, "DAC clk div_num error");
64 ESP_RETURN_ON_FALSE(cfg->dig_clk.div_b > 0 && cfg->dig_clk.div_b < 64, ESP_ERR_INVALID_ARG, TAG, "DAC clk div_b error");
65 ESP_RETURN_ON_FALSE(cfg->dig_clk.div_a < 64, ESP_ERR_INVALID_ARG, TAG, "DAC clk div_a error");
66 #ifdef CONFIG_PM_ENABLE
67 esp_err_t err;
68 if (s_dac_digi_lock == NULL) {
69 if (cfg->dig_clk.use_apll) {
70 err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "dac_dma", &s_dac_digi_lock);
71 } else {
72 err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "dac_dma", &s_dac_digi_lock);
73 }
74 if (err != ESP_OK) {
75 s_dac_digi_lock = NULL;
76 ESP_LOGE(TAG, "DAC-DMA pm lock error");
77 return err;
78 }
79 }
80 #endif //CONFIG_PM_ENABLE
81
82 DAC_ENTER_CRITICAL();
83 dac_hal_digi_controller_config(cfg);
84 DAC_EXIT_CRITICAL();
85
86 return ESP_OK;
87 }
88
dac_digi_start(void)89 esp_err_t dac_digi_start(void)
90 {
91 #ifdef CONFIG_PM_ENABLE
92 ESP_RETURN_ON_FALSE(s_dac_digi_lock, ESP_FAIL, TAG, "Should start after call `dac_digi_controller_config`");
93 esp_pm_lock_acquire(s_dac_digi_lock);
94 #endif
95 DAC_ENTER_CRITICAL();
96 dac_hal_digi_start();
97 DAC_EXIT_CRITICAL();
98
99 return ESP_OK;
100 }
101
dac_digi_stop(void)102 esp_err_t dac_digi_stop(void)
103 {
104 #ifdef CONFIG_PM_ENABLE
105 if (s_dac_digi_lock) {
106 esp_pm_lock_release(s_dac_digi_lock);
107 }
108 #endif
109 DAC_ENTER_CRITICAL();
110 dac_hal_digi_stop();
111 DAC_EXIT_CRITICAL();
112
113 return ESP_OK;
114 }
115
dac_digi_fifo_reset(void)116 esp_err_t dac_digi_fifo_reset(void)
117 {
118 DAC_ENTER_CRITICAL();
119 dac_hal_digi_fifo_reset();
120 DAC_EXIT_CRITICAL();
121
122 return ESP_OK;
123 }
124
dac_digi_reset(void)125 esp_err_t dac_digi_reset(void)
126 {
127 DAC_ENTER_CRITICAL();
128 dac_hal_digi_reset();
129 DAC_EXIT_CRITICAL();
130
131 return ESP_OK;
132 }
133