adc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 <esp_types.h>
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/xtensa_api.h"
  19. #include "freertos/semphr.h"
  20. #include "freertos/timers.h"
  21. #include "esp_log.h"
  22. #include "soc/rtc.h"
  23. #include "rtc_io.h"
  24. #include "adc.h"
  25. #include "dac.h"
  26. #include "sys/lock.h"
  27. #include "driver/gpio.h"
  28. #include "adc1_i2s_private.h"
  29. #include "hal/adc_types.h"
  30. #include "hal/adc_hal.h"
  31. #include "hal/dac_hal.h"
  32. #define ADC_MAX_MEAS_NUM_DEFAULT (255)
  33. #define ADC_MEAS_NUM_LIM_DEFAULT (1)
  34. #define DIG_ADC_OUTPUT_FORMAT_DEFUALT (ADC_DIG_FORMAT_12BIT)
  35. #define DIG_ADC_ATTEN_DEFUALT (ADC_ATTEN_DB_11)
  36. #define DIG_ADC_BIT_WIDTH_DEFUALT (ADC_WIDTH_BIT_12)
  37. #define ADC_CHECK_RET(fun_ret) ({ \
  38. if (fun_ret != ESP_OK) { \
  39. ESP_LOGE(ADC_TAG,"%s:%d\n",__FUNCTION__,__LINE__); \
  40. return ESP_FAIL; \
  41. } \
  42. })
  43. static const char *ADC_TAG = "ADC";
  44. #define ADC_CHECK(a, str, ret_val) ({ \
  45. if (!(a)) { \
  46. ESP_LOGE(ADC_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
  47. return (ret_val); \
  48. } \
  49. })
  50. #define ADC_GET_IO_NUM(periph, channel) (adc_channel_io_map[periph][channel])
  51. #define ADC_CHANNEL_CHECK(periph, channel) ADC_CHECK(channel < SOC_ADC_CHANNEL_NUM(periph), "ADC"#periph" channel error", ESP_ERR_INVALID_ARG)
  52. extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
  53. #define ADC_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
  54. #define ADC_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
  55. /*
  56. In ADC2, there're two locks used for different cases:
  57. 1. lock shared with app and WIFI:
  58. when wifi using the ADC2, we assume it will never stop,
  59. so app checks the lock and returns immediately if failed.
  60. 2. lock shared between tasks:
  61. when several tasks sharing the ADC2, we want to guarantee
  62. all the requests will be handled.
  63. Since conversions are short (about 31us), app returns the lock very soon,
  64. we use a spinlock to stand there waiting to do conversions one by one.
  65. adc2_spinlock should be acquired first, then adc2_wifi_lock or rtc_spinlock.
  66. */
  67. // This gets incremented when adc_power_acquire() is called, and decremented when
  68. // adc_power_release() is called. ADC is powered down when the value reaches zero.
  69. // Should be modified within critical section (ADC_ENTER/EXIT_CRITICAL).
  70. static int s_adc_power_on_cnt;
  71. static void adc_power_on_internal(void);
  72. static void adc_power_off_internal(void);
  73. //prevent ADC2 being used by wifi and other tasks at the same time.
  74. static _lock_t adc2_wifi_lock;
  75. //prevent ADC2 being used by tasks (regardless of WIFI)
  76. static portMUX_TYPE adc2_spinlock = portMUX_INITIALIZER_UNLOCKED;
  77. //prevent ADC1 being used by I2S dma and other tasks at the same time.
  78. static _lock_t adc1_i2s_lock;
  79. /*---------------------------------------------------------------
  80. ADC Common
  81. ---------------------------------------------------------------*/
  82. void adc_power_acquire(void)
  83. {
  84. ADC_ENTER_CRITICAL();
  85. s_adc_power_on_cnt++;
  86. if (s_adc_power_on_cnt == 1) {
  87. adc_power_on_internal();
  88. }
  89. ADC_EXIT_CRITICAL();
  90. }
  91. void adc_power_release(void)
  92. {
  93. ADC_ENTER_CRITICAL();
  94. s_adc_power_on_cnt--;
  95. /* Sanity check */
  96. if (s_adc_power_on_cnt < 0) {
  97. ADC_EXIT_CRITICAL();
  98. ESP_LOGE(ADC_TAG, "%s called, but s_adc_power_on_cnt == 0", __func__);
  99. abort();
  100. } else if (s_adc_power_on_cnt == 0) {
  101. adc_power_off_internal();
  102. }
  103. ADC_EXIT_CRITICAL();
  104. }
  105. static void adc_power_on_internal(void)
  106. {
  107. ADC_ENTER_CRITICAL();
  108. /* Set the power always on to increase precision. */
  109. adc_hal_set_power_manage(ADC_POWER_SW_ON);
  110. ADC_EXIT_CRITICAL();
  111. }
  112. void adc_power_on(void) __attribute__((alias("adc_power_on_internal")));
  113. static void adc_power_off_internal(void)
  114. {
  115. ADC_ENTER_CRITICAL();
  116. adc_hal_set_power_manage(ADC_POWER_SW_OFF);
  117. ADC_EXIT_CRITICAL();
  118. }
  119. void adc_power_off(void) __attribute__((alias("adc_power_off_internal")));
  120. esp_err_t adc_set_clk_div(uint8_t clk_div)
  121. {
  122. ADC_ENTER_CRITICAL();
  123. adc_hal_set_clk_div(clk_div);
  124. ADC_EXIT_CRITICAL();
  125. return ESP_OK;
  126. }
  127. esp_err_t adc_set_i2s_data_source(adc_i2s_source_t src)
  128. {
  129. ADC_CHECK(src < ADC_I2S_DATA_SRC_MAX, "ADC i2s data source error", ESP_ERR_INVALID_ARG);
  130. ADC_ENTER_CRITICAL();
  131. adc_hal_dig_set_data_source(src);
  132. ADC_EXIT_CRITICAL();
  133. return ESP_OK;
  134. }
  135. esp_err_t adc_gpio_init(adc_unit_t adc_unit, adc_channel_t channel)
  136. {
  137. gpio_num_t gpio_num = 0;
  138. if (adc_unit & ADC_UNIT_1) {
  139. ADC_CHANNEL_CHECK(ADC_NUM_1, channel);
  140. gpio_num = ADC_GET_IO_NUM(ADC_NUM_1, channel);
  141. ADC_CHECK_RET(rtc_gpio_init(gpio_num));
  142. ADC_CHECK_RET(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED));
  143. ADC_CHECK_RET(rtc_gpio_pulldown_dis(gpio_num));
  144. ADC_CHECK_RET(rtc_gpio_pullup_dis(gpio_num));
  145. }
  146. if (adc_unit & ADC_UNIT_2) {
  147. ADC_CHANNEL_CHECK(ADC_NUM_2, channel);
  148. gpio_num = ADC_GET_IO_NUM(ADC_NUM_2, channel);
  149. ADC_CHECK_RET(rtc_gpio_init(gpio_num));
  150. ADC_CHECK_RET(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED));
  151. ADC_CHECK_RET(rtc_gpio_pulldown_dis(gpio_num));
  152. ADC_CHECK_RET(rtc_gpio_pullup_dis(gpio_num));
  153. }
  154. return ESP_OK;
  155. }
  156. esp_err_t adc_set_data_inv(adc_unit_t adc_unit, bool inv_en)
  157. {
  158. ADC_ENTER_CRITICAL();
  159. if (adc_unit & ADC_UNIT_1) {
  160. adc_hal_output_invert(ADC_NUM_1, inv_en);
  161. }
  162. if (adc_unit & ADC_UNIT_2) {
  163. adc_hal_output_invert(ADC_NUM_1, inv_en);
  164. }
  165. ADC_EXIT_CRITICAL();
  166. return ESP_OK;
  167. }
  168. esp_err_t adc_set_data_width(adc_unit_t adc_unit, adc_bits_width_t bits)
  169. {
  170. ADC_CHECK(bits < ADC_WIDTH_MAX, "ADC bit width error", ESP_ERR_INVALID_ARG);
  171. ADC_ENTER_CRITICAL();
  172. if (adc_unit & ADC_UNIT_1) {
  173. adc_hal_rtc_set_output_format(ADC_NUM_1, bits);
  174. }
  175. if (adc_unit & ADC_UNIT_2) {
  176. adc_hal_rtc_set_output_format(ADC_NUM_2, bits);
  177. adc_hal_pwdet_set_cct(SOC_ADC_PWDET_CCT_DEFAULT);
  178. }
  179. ADC_EXIT_CRITICAL();
  180. return ESP_OK;
  181. }
  182. /* this function should be called in the critical section. */
  183. static int adc_convert(adc_ll_num_t adc_n, int channel)
  184. {
  185. return adc_hal_convert(adc_n, channel);
  186. }
  187. /*-------------------------------------------------------------------------------------
  188. * ADC I2S
  189. *------------------------------------------------------------------------------------*/
  190. esp_err_t adc_i2s_mode_init(adc_unit_t adc_unit, adc_channel_t channel)
  191. {
  192. if (adc_unit & ADC_UNIT_1) {
  193. ADC_CHECK((SOC_ADC_SUPPORT_DMA_MODE(ADC_NUM_1)), "ADC1 not support DMA for now.", ESP_ERR_INVALID_ARG);
  194. ADC_CHANNEL_CHECK(ADC_NUM_1, channel);
  195. }
  196. if (adc_unit & ADC_UNIT_2) {
  197. ADC_CHECK((SOC_ADC_SUPPORT_DMA_MODE(ADC_NUM_2)), "ADC2 not support DMA for now.", ESP_ERR_INVALID_ARG);
  198. ADC_CHANNEL_CHECK(ADC_NUM_2, channel);
  199. }
  200. adc_ll_pattern_table_t adc1_pattern[1];
  201. adc_ll_pattern_table_t adc2_pattern[1];
  202. adc_hal_dig_config_t dig_cfg = {
  203. .conv_limit_en = ADC_MEAS_NUM_LIM_DEFAULT,
  204. .conv_limit_num = ADC_MAX_MEAS_NUM_DEFAULT,
  205. .clk_div = SOC_ADC_DIGI_SAR_CLK_DIV_DEFAULT,
  206. .format = DIG_ADC_OUTPUT_FORMAT_DEFUALT,
  207. .conv_mode = (adc_ll_convert_mode_t)adc_unit,
  208. };
  209. if (adc_unit & ADC_UNIT_1) {
  210. adc1_pattern[0].atten = DIG_ADC_ATTEN_DEFUALT;
  211. adc1_pattern[0].bit_width = DIG_ADC_BIT_WIDTH_DEFUALT;
  212. adc1_pattern[0].channel = channel;
  213. dig_cfg.adc1_pattern_len = 1;
  214. dig_cfg.adc1_pattern = adc1_pattern;
  215. }
  216. if (adc_unit & ADC_UNIT_2) {
  217. adc2_pattern[0].atten = DIG_ADC_ATTEN_DEFUALT;
  218. adc2_pattern[0].bit_width = DIG_ADC_BIT_WIDTH_DEFUALT;
  219. adc2_pattern[0].channel = channel;
  220. dig_cfg.adc2_pattern_len = 1;
  221. dig_cfg.adc2_pattern = adc2_pattern;
  222. }
  223. adc_gpio_init(adc_unit, channel);
  224. ADC_ENTER_CRITICAL();
  225. adc_hal_init();
  226. adc_hal_dig_controller_config(&dig_cfg);
  227. ADC_EXIT_CRITICAL();
  228. return ESP_OK;
  229. }
  230. /*-------------------------------------------------------------------------------------
  231. * ADC1
  232. *------------------------------------------------------------------------------------*/
  233. esp_err_t adc1_pad_get_io_num(adc1_channel_t channel, gpio_num_t *gpio_num)
  234. {
  235. ADC_CHANNEL_CHECK(ADC_NUM_1, channel);
  236. int io = ADC_GET_IO_NUM(ADC_NUM_1, channel);
  237. if (io < 0) {
  238. return ESP_ERR_INVALID_ARG;
  239. } else {
  240. *gpio_num = (gpio_num_t)io;
  241. }
  242. return ESP_OK;
  243. }
  244. esp_err_t adc1_config_channel_atten(adc1_channel_t channel, adc_atten_t atten)
  245. {
  246. ADC_CHANNEL_CHECK(ADC_NUM_1, channel);
  247. ADC_CHECK(atten < ADC_ATTEN_MAX, "ADC Atten Err", ESP_ERR_INVALID_ARG);
  248. adc_gpio_init(ADC_UNIT_1, channel);
  249. /* Workaround: Disable the synchronization operation function of ADC1 and DAC.
  250. If enabled(default), ADC RTC controller sampling will cause the DAC channel output voltage. */
  251. dac_hal_rtc_sync_by_adc(false);
  252. adc_hal_set_atten(ADC_NUM_1, channel, atten);
  253. return ESP_OK;
  254. }
  255. esp_err_t adc1_config_width(adc_bits_width_t width_bit)
  256. {
  257. ADC_CHECK(width_bit < ADC_WIDTH_MAX, "ADC bit width error", ESP_ERR_INVALID_ARG);
  258. adc_hal_rtc_set_output_format(ADC_NUM_1, width_bit);
  259. adc_hal_output_invert(ADC_NUM_1, true);
  260. return ESP_OK;
  261. }
  262. esp_err_t adc1_i2s_mode_acquire(void)
  263. {
  264. /* Use locks to avoid digtal and RTC controller conflicts.
  265. for adc1, block until acquire the lock. */
  266. _lock_acquire( &adc1_i2s_lock );
  267. ESP_LOGD( ADC_TAG, "i2s mode takes adc1 lock." );
  268. ADC_ENTER_CRITICAL();
  269. adc_power_acquire();
  270. adc_hal_set_power_manage(ADC_POWER_SW_ON);
  271. /* switch SARADC into DIG channel */
  272. adc_hal_set_controller(ADC_NUM_1, ADC_CTRL_DIG);
  273. ADC_EXIT_CRITICAL();
  274. return ESP_OK;
  275. }
  276. esp_err_t adc1_adc_mode_acquire(void)
  277. {
  278. /* Use locks to avoid digtal and RTC controller conflicts.
  279. for adc1, block until acquire the lock. */
  280. _lock_acquire( &adc1_i2s_lock );
  281. ADC_ENTER_CRITICAL();
  282. adc_power_acquire();
  283. /* switch SARADC into RTC channel. */
  284. adc_hal_set_controller(ADC_NUM_1, ADC_CTRL_RTC);
  285. ADC_EXIT_CRITICAL();
  286. return ESP_OK;
  287. }
  288. esp_err_t adc1_lock_release(void)
  289. {
  290. ADC_CHECK((uint32_t *)adc1_i2s_lock != NULL, "adc1 lock release called before acquire", ESP_ERR_INVALID_STATE );
  291. /* Use locks to avoid digtal and RTC controller conflicts.
  292. for adc1, block until acquire the lock. */
  293. _lock_release( &adc1_i2s_lock );
  294. return ESP_OK;
  295. }
  296. int adc1_get_raw(adc1_channel_t channel)
  297. {
  298. uint16_t adc_value;
  299. ADC_CHANNEL_CHECK(ADC_NUM_1, channel);
  300. adc1_adc_mode_acquire();
  301. adc_power_acquire();
  302. ADC_ENTER_CRITICAL();
  303. /* disable other peripherals. */
  304. adc_hal_hall_disable();
  305. /* currently the LNA is not open, close it by default. */
  306. adc_hal_amp_disable();
  307. /* set controller */
  308. adc_hal_set_controller(ADC_NUM_1, ADC_CTRL_RTC);
  309. /* start conversion */
  310. adc_value = adc_convert(ADC_NUM_1, channel);
  311. ADC_EXIT_CRITICAL();
  312. adc_power_release();
  313. adc1_lock_release();
  314. return adc_value;
  315. }
  316. int adc1_get_voltage(adc1_channel_t channel) //Deprecated. Use adc1_get_raw() instead
  317. {
  318. return adc1_get_raw(channel);
  319. }
  320. void adc1_ulp_enable(void)
  321. {
  322. adc_power_acquire();
  323. ADC_ENTER_CRITICAL();
  324. adc_hal_set_controller(ADC_NUM_1, ADC_CTRL_ULP);
  325. /* since most users do not need LNA and HALL with uLP, we disable them here
  326. open them in the uLP if needed. */
  327. /* disable other peripherals. */
  328. adc_hal_hall_disable();
  329. adc_hal_amp_disable();
  330. ADC_EXIT_CRITICAL();
  331. }
  332. /*---------------------------------------------------------------
  333. ADC2
  334. ---------------------------------------------------------------*/
  335. esp_err_t adc2_pad_get_io_num(adc2_channel_t channel, gpio_num_t *gpio_num)
  336. {
  337. ADC_CHANNEL_CHECK(ADC_NUM_2, channel);
  338. int io = ADC_GET_IO_NUM(ADC_NUM_2, channel);
  339. if (io < 0) {
  340. return ESP_ERR_INVALID_ARG;
  341. } else {
  342. *gpio_num = (gpio_num_t)io;
  343. }
  344. return ESP_OK;
  345. }
  346. esp_err_t adc2_wifi_acquire(void)
  347. {
  348. /* Wi-Fi module will use adc2. Use locks to avoid conflicts. */
  349. _lock_acquire( &adc2_wifi_lock );
  350. adc_power_acquire();
  351. ESP_LOGD( ADC_TAG, "Wi-Fi takes adc2 lock." );
  352. return ESP_OK;
  353. }
  354. esp_err_t adc2_wifi_release(void)
  355. {
  356. ADC_CHECK((uint32_t *)adc2_wifi_lock != NULL, "wifi release called before acquire", ESP_ERR_INVALID_STATE );
  357. _lock_release( &adc2_wifi_lock );
  358. ESP_LOGD( ADC_TAG, "Wi-Fi returns adc2 lock." );
  359. return ESP_OK;
  360. }
  361. static esp_err_t adc2_pad_init(adc2_channel_t channel)
  362. {
  363. gpio_num_t gpio_num = 0;
  364. ADC_CHECK_RET(adc2_pad_get_io_num(channel, &gpio_num));
  365. ADC_CHECK_RET(rtc_gpio_init(gpio_num));
  366. ADC_CHECK_RET(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED));
  367. ADC_CHECK_RET(rtc_gpio_pulldown_dis(gpio_num));
  368. ADC_CHECK_RET(rtc_gpio_pullup_dis(gpio_num));
  369. return ESP_OK;
  370. }
  371. esp_err_t adc2_config_channel_atten(adc2_channel_t channel, adc_atten_t atten)
  372. {
  373. ADC_CHANNEL_CHECK(ADC_NUM_2, channel);
  374. ADC_CHECK(atten <= ADC_ATTEN_11db, "ADC2 Atten Err", ESP_ERR_INVALID_ARG);
  375. adc2_pad_init(channel);
  376. portENTER_CRITICAL( &adc2_spinlock );
  377. //lazy initialization
  378. //avoid collision with other tasks
  379. if ( _lock_try_acquire( &adc2_wifi_lock ) == -1 ) {
  380. //try the lock, return if failed (wifi using).
  381. portEXIT_CRITICAL( &adc2_spinlock );
  382. return ESP_ERR_TIMEOUT;
  383. }
  384. adc_hal_set_atten(ADC_NUM_2, channel, atten);
  385. _lock_release( &adc2_wifi_lock );
  386. portEXIT_CRITICAL( &adc2_spinlock );
  387. return ESP_OK;
  388. }
  389. static inline void adc2_config_width(adc_bits_width_t width_bit)
  390. {
  391. ADC_ENTER_CRITICAL();
  392. adc_hal_rtc_set_output_format(ADC_NUM_2, width_bit);
  393. adc_hal_pwdet_set_cct(SOC_ADC_PWDET_CCT_DEFAULT);
  394. adc_hal_output_invert(ADC_NUM_2, true);
  395. ADC_EXIT_CRITICAL();
  396. }
  397. static inline void adc2_dac_disable( adc2_channel_t channel)
  398. {
  399. if ( channel == ADC2_CHANNEL_8 ) { // the same as DAC channel 1
  400. dac_output_disable(DAC_CHANNEL_1);
  401. } else if ( channel == ADC2_CHANNEL_9 ) {
  402. dac_output_disable(DAC_CHANNEL_2);
  403. }
  404. }
  405. //registers in critical section with adc1:
  406. //SENS_SAR_START_FORCE_REG,
  407. esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int *raw_out)
  408. {
  409. uint16_t adc_value = 0;
  410. ADC_CHECK(channel < ADC2_CHANNEL_MAX, "ADC Channel Err", ESP_ERR_INVALID_ARG);
  411. //in critical section with whole rtc module
  412. adc_power_acquire();
  413. //avoid collision with other tasks
  414. portENTER_CRITICAL(&adc2_spinlock);
  415. //lazy initialization
  416. //try the lock, return if failed (wifi using).
  417. if ( _lock_try_acquire( &adc2_wifi_lock ) == -1 ) {
  418. portEXIT_CRITICAL( &adc2_spinlock );
  419. adc_power_release();
  420. return ESP_ERR_TIMEOUT;
  421. }
  422. //disable other peripherals
  423. #ifdef CONFIG_ADC_DISABLE_DAC
  424. adc2_dac_disable(channel);
  425. #endif
  426. // set controller
  427. // in critical section with whole rtc module
  428. // because the PWDET use the same registers, place it here.
  429. adc2_config_width(width_bit);
  430. adc_hal_set_controller(ADC_NUM_2, ADC_CTRL_RTC);
  431. //start converting
  432. adc_value = adc_convert(ADC_NUM_2, channel);
  433. _lock_release( &adc2_wifi_lock );
  434. portEXIT_CRITICAL(&adc2_spinlock);
  435. adc_power_release();
  436. *raw_out = (int)adc_value;
  437. return ESP_OK;
  438. }
  439. esp_err_t adc2_vref_to_gpio(gpio_num_t gpio)
  440. {
  441. adc_power_acquire(); //Select power source of ADC
  442. if (adc_hal_vref_output(gpio) != true) {
  443. adc_power_release();
  444. return ESP_ERR_INVALID_ARG;
  445. } else {
  446. //Configure RTC gpio
  447. rtc_gpio_init(gpio);
  448. rtc_gpio_set_direction(gpio, RTC_GPIO_MODE_DISABLED);
  449. rtc_gpio_pullup_dis(gpio);
  450. rtc_gpio_pulldown_dis(gpio);
  451. return ESP_OK;
  452. }
  453. }
  454. /*---------------------------------------------------------------
  455. HALL SENSOR
  456. ---------------------------------------------------------------*/
  457. static int hall_sensor_get_value(void) //hall sensor without LNA
  458. {
  459. int hall_value;
  460. adc_power_acquire();
  461. ADC_ENTER_CRITICAL();
  462. /* disable other peripherals. */
  463. adc_hal_amp_disable();
  464. adc_hal_hall_enable();
  465. // set controller
  466. adc_hal_set_controller( ADC_NUM_1, ADC_CTRL_RTC );
  467. hall_value = adc_hal_hall_convert();
  468. ADC_EXIT_CRITICAL();
  469. return hall_value;
  470. }
  471. int hall_sensor_read(void)
  472. {
  473. adc_gpio_init(ADC_UNIT_1, ADC1_CHANNEL_0);
  474. adc_gpio_init(ADC_UNIT_1, ADC1_CHANNEL_3);
  475. adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_0);
  476. adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_0);
  477. return hall_sensor_get_value();
  478. }