dac_common_legacy.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "esp_check.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "driver/rtc_io.h"
  10. #include "driver/dac_types_legacy.h"
  11. #include "soc/dac_periph.h"
  12. #include "hal/gpio_types.h"
  13. #include "hal/dac_ll.h"
  14. #include "clk_ctrl_os.h"
  15. extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
  16. static __attribute__((unused)) const char *TAG = "DAC";
  17. /*---------------------------------------------------------------
  18. DAC
  19. ---------------------------------------------------------------*/
  20. esp_err_t dac_pad_get_io_num(dac_channel_t channel, gpio_num_t *gpio_num)
  21. {
  22. ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
  23. *gpio_num = (gpio_num_t)dac_periph_signal.dac_channel_io_num[channel];
  24. return ESP_OK;
  25. }
  26. static esp_err_t dac_rtc_pad_init(dac_channel_t channel)
  27. {
  28. ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
  29. gpio_num_t gpio_num = 0;
  30. dac_pad_get_io_num(channel, &gpio_num);
  31. rtc_gpio_init(gpio_num);
  32. rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED);
  33. rtc_gpio_pullup_dis(gpio_num);
  34. rtc_gpio_pulldown_dis(gpio_num);
  35. return ESP_OK;
  36. }
  37. esp_err_t dac_output_enable(dac_channel_t channel)
  38. {
  39. ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
  40. dac_rtc_pad_init(channel);
  41. portENTER_CRITICAL(&rtc_spinlock);
  42. dac_ll_power_on(channel);
  43. dac_ll_rtc_sync_by_adc(false);
  44. portEXIT_CRITICAL(&rtc_spinlock);
  45. return ESP_OK;
  46. }
  47. esp_err_t dac_output_disable(dac_channel_t channel)
  48. {
  49. ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
  50. portENTER_CRITICAL(&rtc_spinlock);
  51. dac_ll_power_down(channel);
  52. portEXIT_CRITICAL(&rtc_spinlock);
  53. return ESP_OK;
  54. }
  55. esp_err_t dac_output_voltage(dac_channel_t channel, uint8_t dac_value)
  56. {
  57. ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
  58. portENTER_CRITICAL(&rtc_spinlock);
  59. dac_ll_update_output_value(channel, dac_value);
  60. portEXIT_CRITICAL(&rtc_spinlock);
  61. return ESP_OK;
  62. }
  63. esp_err_t dac_out_voltage(dac_channel_t channel, uint8_t dac_value)
  64. {
  65. ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
  66. portENTER_CRITICAL(&rtc_spinlock);
  67. dac_ll_update_output_value(channel, dac_value);
  68. portEXIT_CRITICAL(&rtc_spinlock);
  69. return ESP_OK;
  70. }
  71. esp_err_t dac_cw_generator_enable(void)
  72. {
  73. portENTER_CRITICAL(&rtc_spinlock);
  74. periph_rtc_dig_clk8m_enable();
  75. dac_ll_cw_generator_enable();
  76. portEXIT_CRITICAL(&rtc_spinlock);
  77. return ESP_OK;
  78. }
  79. esp_err_t dac_cw_generator_disable(void)
  80. {
  81. portENTER_CRITICAL(&rtc_spinlock);
  82. dac_ll_cw_generator_disable();
  83. periph_rtc_dig_clk8m_disable();
  84. portEXIT_CRITICAL(&rtc_spinlock);
  85. return ESP_OK;
  86. }
  87. esp_err_t dac_cw_generator_config(dac_cw_config_t *cw)
  88. {
  89. ESP_RETURN_ON_FALSE(cw, ESP_ERR_INVALID_ARG, TAG, "invalid clock configuration");
  90. portENTER_CRITICAL(&rtc_spinlock);
  91. /* Enable the rtc8m clock temporary to get the correct frequency */
  92. periph_rtc_dig_clk8m_enable();
  93. uint32_t rtc_freq = periph_rtc_dig_clk8m_get_freq();
  94. periph_rtc_dig_clk8m_disable();
  95. dac_ll_cw_set_freq(cw->freq, rtc_freq);
  96. dac_ll_cw_set_atten(cw->en_ch, (dac_cosine_atten_t)cw->scale);
  97. dac_ll_cw_set_phase(cw->en_ch, (dac_cosine_phase_t)cw->phase);
  98. dac_ll_cw_set_dc_offset(cw->en_ch, cw->offset);
  99. dac_ll_cw_enable_channel(cw->en_ch, true);
  100. portEXIT_CRITICAL(&rtc_spinlock);
  101. return ESP_OK;
  102. }
  103. /**
  104. * @brief This function will be called during start up, to check that this legacy DAC driver is not running along with the new driver
  105. */
  106. __attribute__((constructor))
  107. static void check_dac_legacy_driver_conflict(void)
  108. {
  109. // This function was declared as weak here. The new DAC driver has one implementation.
  110. // So if the new DAC driver is not linked in, then `dac_priv_register_channel()` should be NULL at runtime.
  111. extern __attribute__((weak)) esp_err_t dac_priv_register_channel(dac_channel_t chan_id, const char *mode_name);
  112. if ((void *)dac_priv_register_channel != NULL) {
  113. ESP_EARLY_LOGE(TAG, "CONFLICT! The new DAC driver is not allowed to be used together with the legacy driver");
  114. abort();
  115. }
  116. 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");
  117. }