dac.c 4.3 KB

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