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