ulp_adc.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #include "ulp_adc.h"
  8. #include "esp_err.h"
  9. #include "esp_check.h"
  10. #include "esp_log.h"
  11. #include "esp_adc/adc_oneshot.h"
  12. #include "hal/adc_hal_common.h"
  13. #include "esp_private/esp_sleep_internal.h"
  14. #include "esp_private/adc_share_hw_ctrl.h"
  15. static const char *TAG = "ulp_adc";
  16. esp_err_t ulp_adc_init(const ulp_adc_cfg_t *cfg)
  17. {
  18. esp_err_t ret = ESP_OK;
  19. ESP_GOTO_ON_FALSE(cfg, ESP_ERR_INVALID_ARG, err, TAG, "cfg == NULL");
  20. ESP_GOTO_ON_FALSE(cfg->adc_n == ADC_UNIT_1, ESP_ERR_INVALID_ARG, err, TAG, "Only ADC_UNIT_1 is supported for now");
  21. //-------------ADC1 Init---------------//
  22. adc_oneshot_unit_handle_t adc1_handle;
  23. adc_oneshot_unit_init_cfg_t init_config1 = {
  24. .unit_id = cfg->adc_n,
  25. .ulp_mode = cfg->ulp_mode,
  26. };
  27. if (init_config1.ulp_mode == ADC_ULP_MODE_DISABLE) {
  28. /* Default to RISCV for backward compatibility */
  29. ESP_LOGI(TAG, "No ulp mode specified in cfg struct, default to riscv");
  30. init_config1.ulp_mode = ADC_ULP_MODE_RISCV;
  31. }
  32. ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));
  33. //-------------ADC1 Config---------------//
  34. adc_oneshot_chan_cfg_t config = {
  35. .bitwidth = cfg->width,
  36. .atten = cfg->atten,
  37. };
  38. ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, cfg->channel, &config));
  39. //Calibrate the ADC
  40. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  41. adc_set_hw_calibration_code(cfg->adc_n, cfg->atten);
  42. #endif
  43. err:
  44. return ret;
  45. }