esp-idf/components/driver/deprecated/dac_common_legacy.c

147 lines
4.6 KiB
C
Raw Normal View History

2021-05-23 20:09:38 -04:00
/*
* SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
2021-05-23 20:09:38 -04:00
*
* SPDX-License-Identifier: Apache-2.0
*/
2019-07-19 09:20:16 -04:00
#include <string.h>
2021-08-25 05:02:19 -04:00
#include "esp_check.h"
2019-07-19 09:20:16 -04:00
#include "freertos/FreeRTOS.h"
#include "driver/rtc_io.h"
#include "driver/dac_types_legacy.h"
2019-07-19 09:20:16 -04:00
#include "soc/dac_periph.h"
#include "hal/gpio_types.h"
#include "hal/dac_ll.h"
#include "clk_ctrl_os.h"
2019-07-19 09:20:16 -04:00
extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
static __attribute__((unused)) const char *TAG = "DAC";
2019-07-19 09:20:16 -04:00
/*---------------------------------------------------------------
DAC
---------------------------------------------------------------*/
esp_err_t dac_pad_get_io_num(dac_channel_t channel, gpio_num_t *gpio_num)
{
2022-10-10 07:17:22 -04:00
ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
2019-07-19 09:20:16 -04:00
*gpio_num = (gpio_num_t)dac_periph_signal.dac_channel_io_num[channel];
return ESP_OK;
}
static esp_err_t dac_rtc_pad_init(dac_channel_t channel)
{
2022-10-10 07:17:22 -04:00
ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
2019-07-19 09:20:16 -04:00
gpio_num_t gpio_num = 0;
dac_pad_get_io_num(channel, &gpio_num);
rtc_gpio_init(gpio_num);
rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED);
rtc_gpio_pullup_dis(gpio_num);
rtc_gpio_pulldown_dis(gpio_num);
return ESP_OK;
}
esp_err_t dac_output_enable(dac_channel_t channel)
{
2022-10-10 07:17:22 -04:00
ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
2019-07-19 09:20:16 -04:00
dac_rtc_pad_init(channel);
portENTER_CRITICAL(&rtc_spinlock);
dac_ll_power_on(channel);
dac_ll_rtc_sync_by_adc(false);
2019-07-19 09:20:16 -04:00
portEXIT_CRITICAL(&rtc_spinlock);
return ESP_OK;
}
esp_err_t dac_output_disable(dac_channel_t channel)
{
2022-10-10 07:17:22 -04:00
ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
2019-07-19 09:20:16 -04:00
portENTER_CRITICAL(&rtc_spinlock);
dac_ll_power_down(channel);
2019-07-19 09:20:16 -04:00
portEXIT_CRITICAL(&rtc_spinlock);
return ESP_OK;
}
esp_err_t dac_output_voltage(dac_channel_t channel, uint8_t dac_value)
{
2022-10-10 07:17:22 -04:00
ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
2019-07-19 09:20:16 -04:00
portENTER_CRITICAL(&rtc_spinlock);
dac_ll_update_output_value(channel, dac_value);
2019-07-19 09:20:16 -04:00
portEXIT_CRITICAL(&rtc_spinlock);
return ESP_OK;
}
esp_err_t dac_out_voltage(dac_channel_t channel, uint8_t dac_value)
{
2022-10-10 07:17:22 -04:00
ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
2019-07-19 09:20:16 -04:00
portENTER_CRITICAL(&rtc_spinlock);
dac_ll_update_output_value(channel, dac_value);
2019-07-19 09:20:16 -04:00
portEXIT_CRITICAL(&rtc_spinlock);
return ESP_OK;
}
esp_err_t dac_cw_generator_enable(void)
{
portENTER_CRITICAL(&rtc_spinlock);
2022-10-10 07:17:22 -04:00
periph_rtc_dig_clk8m_enable();
dac_ll_cw_generator_enable();
2019-07-19 09:20:16 -04:00
portEXIT_CRITICAL(&rtc_spinlock);
return ESP_OK;
}
esp_err_t dac_cw_generator_disable(void)
{
portENTER_CRITICAL(&rtc_spinlock);
dac_ll_cw_generator_disable();
2022-10-10 07:17:22 -04:00
periph_rtc_dig_clk8m_disable();
2019-07-19 09:20:16 -04:00
portEXIT_CRITICAL(&rtc_spinlock);
return ESP_OK;
}
esp_err_t dac_cw_generator_config(dac_cw_config_t *cw)
{
2021-08-25 05:02:19 -04:00
ESP_RETURN_ON_FALSE(cw, ESP_ERR_INVALID_ARG, TAG, "invalid clock configuration");
2019-07-19 09:20:16 -04:00
portENTER_CRITICAL(&rtc_spinlock);
2022-12-22 02:16:26 -05:00
/* Enable the rtc8m clock temporary to get the correct frequency */
2022-10-10 07:17:22 -04:00
periph_rtc_dig_clk8m_enable();
uint32_t rtc_freq = periph_rtc_dig_clk8m_get_freq();
periph_rtc_dig_clk8m_disable();
dac_ll_cw_set_freq(cw->freq, rtc_freq);
dac_ll_cw_set_atten(cw->en_ch, (dac_cosine_atten_t)cw->scale);
dac_ll_cw_set_phase(cw->en_ch, (dac_cosine_phase_t)cw->phase);
2022-05-09 05:33:51 -04:00
dac_ll_cw_set_dc_offset(cw->en_ch, cw->offset);
2022-10-10 07:17:22 -04:00
dac_ll_cw_enable_channel(cw->en_ch, true);
2019-07-19 09:20:16 -04:00
portEXIT_CRITICAL(&rtc_spinlock);
return ESP_OK;
}
/**
* @brief This function will be called during start up, to check that this legacy DAC driver is not running along with the new driver
*/
__attribute__((constructor))
static void check_dac_legacy_driver_conflict(void)
{
// This function was declared as weak here. The new DAC driver has one implementation.
2022-10-10 07:17:22 -04:00
// So if the new DAC driver is not linked in, then `dac_priv_register_channel()` should be NULL at runtime.
extern __attribute__((weak)) esp_err_t dac_priv_register_channel(dac_channel_t chan_id, const char *mode_name);
if ((void *)dac_priv_register_channel != NULL) {
ESP_EARLY_LOGE(TAG, "CONFLICT! The new DAC driver is not allowed to be used together with the legacy driver");
abort();
}
ESP_EARLY_LOGW(TAG, "legacy driver is deprecated, please migrate to `driver/dac_oneshot.h`, `driver/dac_cosine.h` or `driver/dac_continuous.h` instead");
}