adc.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include "sdkconfig.h"
  9. #include "esp_types.h"
  10. #include "esp_log.h"
  11. #include "sys/lock.h"
  12. #include "soc/rtc.h"
  13. #include "soc/periph_defs.h"
  14. #include "freertos/FreeRTOS.h"
  15. #include "freertos/xtensa_api.h"
  16. #include "freertos/semphr.h"
  17. #include "freertos/timers.h"
  18. #include "esp_intr_alloc.h"
  19. #include "driver/rtc_io.h"
  20. #include "driver/rtc_cntl.h"
  21. #include "driver/gpio.h"
  22. #include "driver/adc.h"
  23. #ifndef NDEBUG
  24. // Enable built-in checks in queue.h in debug builds
  25. #define INVARIANTS
  26. #endif
  27. #include "sys/queue.h"
  28. #include "hal/adc_types.h"
  29. #include "hal/adc_hal.h"
  30. #define ADC_GET_IO_NUM(periph, channel) (adc_channel_io_map[periph][channel])
  31. extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
  32. #define ADC_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
  33. #define ADC_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
  34. /*---------------------------------------------------------------
  35. HALL SENSOR
  36. ---------------------------------------------------------------*/
  37. static int hall_sensor_get_value(void) //hall sensor without LNA
  38. {
  39. int hall_value;
  40. adc_power_acquire();
  41. ADC_ENTER_CRITICAL();
  42. /* disable other peripherals. */
  43. adc_ll_amp_disable();
  44. adc_ll_hall_enable();
  45. // set controller
  46. adc_ll_set_controller( ADC_NUM_1, ADC_LL_CTRL_RTC );
  47. hall_value = adc_hal_hall_convert();
  48. adc_ll_hall_disable();
  49. ADC_EXIT_CRITICAL();
  50. adc_power_release();
  51. return hall_value;
  52. }
  53. /**
  54. * To Be Deprecated
  55. */
  56. extern esp_err_t adc_common_gpio_init(adc_unit_t adc_unit, adc_channel_t channel);
  57. int hall_sensor_read(void)
  58. {
  59. adc_common_gpio_init(ADC_UNIT_1, ADC1_CHANNEL_0);
  60. adc_common_gpio_init(ADC_UNIT_1, ADC1_CHANNEL_3);
  61. adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_0);
  62. adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_0);
  63. return hall_sensor_get_value();
  64. }