dac_common.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. dac_hal_rtc_sync_by_adc(false);
  59. portEXIT_CRITICAL(&rtc_spinlock);
  60. return ESP_OK;
  61. }
  62. esp_err_t dac_output_disable(dac_channel_t channel)
  63. {
  64. DAC_CHECK(channel < DAC_CHANNEL_MAX, "DAC channel error", ESP_ERR_INVALID_ARG);
  65. portENTER_CRITICAL(&rtc_spinlock);
  66. dac_hal_power_down(channel);
  67. portEXIT_CRITICAL(&rtc_spinlock);
  68. return ESP_OK;
  69. }
  70. esp_err_t dac_output_voltage(dac_channel_t channel, uint8_t dac_value)
  71. {
  72. DAC_CHECK(channel < DAC_CHANNEL_MAX, "DAC channel error", ESP_ERR_INVALID_ARG);
  73. portENTER_CRITICAL(&rtc_spinlock);
  74. dac_hal_update_output_value(channel, dac_value);
  75. portEXIT_CRITICAL(&rtc_spinlock);
  76. return ESP_OK;
  77. }
  78. esp_err_t dac_out_voltage(dac_channel_t channel, uint8_t dac_value)
  79. {
  80. DAC_CHECK(channel < DAC_CHANNEL_MAX, "DAC channel error", ESP_ERR_INVALID_ARG);
  81. portENTER_CRITICAL(&rtc_spinlock);
  82. dac_hal_update_output_value(channel, dac_value);
  83. portEXIT_CRITICAL(&rtc_spinlock);
  84. return ESP_OK;
  85. }
  86. esp_err_t dac_cw_generator_enable(void)
  87. {
  88. portENTER_CRITICAL(&rtc_spinlock);
  89. dac_hal_cw_generator_enable();
  90. portEXIT_CRITICAL(&rtc_spinlock);
  91. return ESP_OK;
  92. }
  93. esp_err_t dac_cw_generator_disable(void)
  94. {
  95. portENTER_CRITICAL(&rtc_spinlock);
  96. dac_hal_cw_generator_disable();
  97. portEXIT_CRITICAL(&rtc_spinlock);
  98. return ESP_OK;
  99. }
  100. esp_err_t dac_cw_generator_config(dac_cw_config_t *cw)
  101. {
  102. if (!cw) return ESP_ERR_INVALID_ARG;
  103. portENTER_CRITICAL(&rtc_spinlock);
  104. dac_hal_cw_generator_config(cw);
  105. portEXIT_CRITICAL(&rtc_spinlock);
  106. return ESP_OK;
  107. }