adc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 "freertos/FreeRTOS.h"
  13. #include "freertos/xtensa_api.h"
  14. #include "freertos/semphr.h"
  15. #include "freertos/timers.h"
  16. #include "esp_pm.h"
  17. #include "esp_intr_alloc.h"
  18. #include "driver/periph_ctrl.h"
  19. #include "driver/rtc_io.h"
  20. #include "driver/rtc_cntl.h"
  21. #include "driver/gpio.h"
  22. #include "driver/adc.h"
  23. #include "esp_efuse_rtc_table.h"
  24. #include "hal/adc_types.h"
  25. #include "hal/adc_hal.h"
  26. #define ADC_CHECK_RET(fun_ret) ({ \
  27. if (fun_ret != ESP_OK) { \
  28. ESP_LOGE(ADC_TAG,"%s:%d\n",__FUNCTION__,__LINE__); \
  29. return ESP_FAIL; \
  30. } \
  31. })
  32. static const char *ADC_TAG = "ADC";
  33. #define ADC_CHECK(a, str, ret_val) ({ \
  34. if (!(a)) { \
  35. ESP_LOGE(ADC_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  36. return (ret_val); \
  37. } \
  38. })
  39. #define ADC_GET_IO_NUM(periph, channel) (adc_channel_io_map[periph][channel])
  40. #define ADC_CHANNEL_CHECK(periph, channel) ADC_CHECK(channel < SOC_ADC_CHANNEL_NUM(periph), "ADC"#periph" channel error", ESP_ERR_INVALID_ARG)
  41. extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
  42. #define ADC_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
  43. #define ADC_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
  44. #ifdef CONFIG_PM_ENABLE
  45. static esp_pm_lock_handle_t s_adc_digi_arbiter_lock = NULL;
  46. #endif //CONFIG_PM_ENABLE
  47. esp_err_t adc_cal_offset(adc_ll_num_t adc_n, adc_channel_t channel, adc_atten_t atten);
  48. /*---------------------------------------------------------------
  49. Digital controller setting
  50. ---------------------------------------------------------------*/
  51. esp_err_t adc_digi_init(void)
  52. {
  53. adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
  54. ADC_ENTER_CRITICAL();
  55. adc_hal_init();
  56. adc_hal_arbiter_config(&config);
  57. ADC_EXIT_CRITICAL();
  58. adc_hal_calibration_init(ADC_NUM_1);
  59. adc_hal_calibration_init(ADC_NUM_2);
  60. return ESP_OK;
  61. }
  62. esp_err_t adc_digi_deinit(void)
  63. {
  64. #ifdef CONFIG_PM_ENABLE
  65. if (s_adc_digi_arbiter_lock) {
  66. esp_pm_lock_delete(s_adc_digi_arbiter_lock);
  67. s_adc_digi_arbiter_lock = NULL;
  68. }
  69. #endif
  70. adc_power_release();
  71. ADC_ENTER_CRITICAL();
  72. adc_hal_digi_deinit();
  73. ADC_EXIT_CRITICAL();
  74. return ESP_OK;
  75. }
  76. esp_err_t adc_digi_controller_config(const adc_digi_config_t *config)
  77. {
  78. #ifdef CONFIG_PM_ENABLE
  79. esp_err_t err;
  80. if (s_adc_digi_arbiter_lock == NULL) {
  81. if (config->dig_clk.use_apll) {
  82. err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "adc_dma", &s_adc_digi_arbiter_lock);
  83. } else {
  84. err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "adc_dma", &s_adc_digi_arbiter_lock);
  85. }
  86. if (err != ESP_OK) {
  87. s_adc_digi_arbiter_lock = NULL;
  88. ESP_LOGE(ADC_TAG, "ADC-DMA pm lock error");
  89. return err;
  90. }
  91. }
  92. #endif //CONFIG_PM_ENABLE
  93. if (config->conv_mode & ADC_CONV_SINGLE_UNIT_1) {
  94. for (int i = 0; i < config->adc1_pattern_len; i++) {
  95. adc_cal_offset(ADC_NUM_1, config->adc1_pattern[i].channel, config->adc1_pattern[i].atten);
  96. }
  97. }
  98. if (config->conv_mode & ADC_CONV_SINGLE_UNIT_2) {
  99. for (int i = 0; i < config->adc2_pattern_len; i++) {
  100. adc_cal_offset(ADC_NUM_2, config->adc2_pattern[i].channel, config->adc2_pattern[i].atten);
  101. }
  102. }
  103. /* If enable digtal controller, adc xpd should always on. */
  104. adc_power_acquire();
  105. ADC_ENTER_CRITICAL();
  106. adc_hal_digi_controller_config(config);
  107. ADC_EXIT_CRITICAL();
  108. return ESP_OK;
  109. }
  110. esp_err_t adc_arbiter_config(adc_unit_t adc_unit, adc_arbiter_t *config)
  111. {
  112. if (adc_unit & ADC_UNIT_1) {
  113. return ESP_ERR_NOT_SUPPORTED;
  114. }
  115. ADC_ENTER_CRITICAL();
  116. adc_hal_arbiter_config(config);
  117. ADC_EXIT_CRITICAL();
  118. return ESP_OK;
  119. }
  120. /**
  121. * @brief Set ADC module controller.
  122. * There are five SAR ADC controllers:
  123. * Two digital controller: Continuous conversion mode (DMA). High performance with multiple channel scan modes;
  124. * Two RTC controller: Single conversion modes (Polling). For low power purpose working during deep sleep;
  125. * the other is dedicated for Power detect (PWDET / PKDET), Only support ADC2.
  126. *
  127. * @note Only ADC2 support arbiter to switch controllers automatically. Access to the ADC is based on the priority of the controller.
  128. * @note For ADC1, Controller access is mutually exclusive.
  129. *
  130. * @param adc_unit ADC unit.
  131. * @param ctrl ADC controller, Refer to `adc_ll_controller_t`.
  132. *
  133. * @return
  134. * - ESP_OK Success
  135. */
  136. esp_err_t adc_set_controller(adc_unit_t adc_unit, adc_ll_controller_t ctrl)
  137. {
  138. adc_arbiter_t config = {0};
  139. adc_arbiter_t cfg = ADC_ARBITER_CONFIG_DEFAULT();
  140. if (adc_unit & ADC_UNIT_1) {
  141. adc_hal_set_controller(ADC_NUM_1, ctrl);
  142. }
  143. if (adc_unit & ADC_UNIT_2) {
  144. adc_hal_set_controller(ADC_NUM_2, ctrl);
  145. switch (ctrl) {
  146. case ADC2_CTRL_FORCE_PWDET:
  147. config.pwdet_pri = 2;
  148. config.mode = ADC_ARB_MODE_SHIELD;
  149. adc_hal_arbiter_config(&config);
  150. adc_hal_set_controller(ADC_NUM_2, ADC2_CTRL_PWDET);
  151. break;
  152. case ADC2_CTRL_FORCE_RTC:
  153. config.rtc_pri = 2;
  154. config.mode = ADC_ARB_MODE_SHIELD;
  155. adc_hal_arbiter_config(&config);
  156. adc_hal_set_controller(ADC_NUM_2, ADC_CTRL_RTC);
  157. break;
  158. case ADC2_CTRL_FORCE_ULP:
  159. config.rtc_pri = 2;
  160. config.mode = ADC_ARB_MODE_SHIELD;
  161. adc_hal_arbiter_config(&config);
  162. adc_hal_set_controller(ADC_NUM_2, ADC_CTRL_ULP);
  163. break;
  164. case ADC2_CTRL_FORCE_DIG:
  165. config.dig_pri = 2;
  166. config.mode = ADC_ARB_MODE_SHIELD;
  167. adc_hal_arbiter_config(&config);
  168. adc_hal_set_controller(ADC_NUM_2, ADC_CTRL_DIG);
  169. break;
  170. default:
  171. adc_hal_arbiter_config(&cfg);
  172. break;
  173. }
  174. }
  175. return ESP_OK;
  176. }
  177. esp_err_t adc_digi_start(void)
  178. {
  179. #ifdef CONFIG_PM_ENABLE
  180. ADC_CHECK((s_adc_digi_arbiter_lock), "Should start after call `adc_digi_controller_config`", ESP_FAIL);
  181. esp_pm_lock_acquire(s_adc_digi_arbiter_lock);
  182. #endif
  183. ADC_ENTER_CRITICAL();
  184. adc_hal_digi_enable();
  185. ADC_EXIT_CRITICAL();
  186. return ESP_OK;
  187. }
  188. esp_err_t adc_digi_stop(void)
  189. {
  190. #ifdef CONFIG_PM_ENABLE
  191. if (s_adc_digi_arbiter_lock) {
  192. esp_pm_lock_release(s_adc_digi_arbiter_lock);
  193. }
  194. #endif
  195. ADC_ENTER_CRITICAL();
  196. adc_hal_digi_disable();
  197. ADC_EXIT_CRITICAL();
  198. return ESP_OK;
  199. }
  200. /**
  201. * @brief Reset FSM of adc digital controller.
  202. *
  203. * @return
  204. * - ESP_OK Success
  205. */
  206. esp_err_t adc_digi_reset(void)
  207. {
  208. ADC_ENTER_CRITICAL();
  209. adc_hal_digi_reset();
  210. adc_hal_digi_clear_pattern_table(ADC_NUM_1);
  211. adc_hal_digi_clear_pattern_table(ADC_NUM_2);
  212. ADC_EXIT_CRITICAL();
  213. return ESP_OK;
  214. }
  215. /*************************************/
  216. /* Digital controller filter setting */
  217. /*************************************/
  218. esp_err_t adc_digi_filter_reset(adc_digi_filter_idx_t idx)
  219. {
  220. ADC_ENTER_CRITICAL();
  221. if (idx == ADC_DIGI_FILTER_IDX0) {
  222. adc_hal_digi_filter_reset(ADC_NUM_1);
  223. } else if (idx == ADC_DIGI_FILTER_IDX1) {
  224. adc_hal_digi_filter_reset(ADC_NUM_2);
  225. }
  226. ADC_EXIT_CRITICAL();
  227. return ESP_OK;
  228. }
  229. esp_err_t adc_digi_filter_set_config(adc_digi_filter_idx_t idx, adc_digi_filter_t *config)
  230. {
  231. ADC_ENTER_CRITICAL();
  232. if (idx == ADC_DIGI_FILTER_IDX0) {
  233. adc_hal_digi_filter_set_factor(ADC_NUM_1, config->mode);
  234. } else if (idx == ADC_DIGI_FILTER_IDX1) {
  235. adc_hal_digi_filter_set_factor(ADC_NUM_2, config->mode);
  236. }
  237. ADC_EXIT_CRITICAL();
  238. return ESP_OK;
  239. }
  240. esp_err_t adc_digi_filter_get_config(adc_digi_filter_idx_t idx, adc_digi_filter_t *config)
  241. {
  242. ADC_ENTER_CRITICAL();
  243. if (idx == ADC_DIGI_FILTER_IDX0) {
  244. config->adc_unit = ADC_UNIT_1;
  245. config->channel = ADC_CHANNEL_MAX;
  246. adc_hal_digi_filter_get_factor(ADC_NUM_1, &config->mode);
  247. } else if (idx == ADC_DIGI_FILTER_IDX1) {
  248. config->adc_unit = ADC_UNIT_2;
  249. config->channel = ADC_CHANNEL_MAX;
  250. adc_hal_digi_filter_get_factor(ADC_NUM_2, &config->mode);
  251. }
  252. ADC_EXIT_CRITICAL();
  253. return ESP_OK;
  254. }
  255. esp_err_t adc_digi_filter_enable(adc_digi_filter_idx_t idx, bool enable)
  256. {
  257. ADC_ENTER_CRITICAL();
  258. if (idx == ADC_DIGI_FILTER_IDX0) {
  259. adc_hal_digi_filter_enable(ADC_NUM_1, enable);
  260. } else if (idx == ADC_DIGI_FILTER_IDX1) {
  261. adc_hal_digi_filter_enable(ADC_NUM_2, enable);
  262. }
  263. ADC_EXIT_CRITICAL();
  264. return ESP_OK;
  265. }
  266. /**
  267. * @brief Get the filtered data of adc digital controller filter. For debug.
  268. * The data after each measurement and filtering is updated to the DMA by the digital controller. But it can also be obtained manually through this API.
  269. *
  270. * @note For ESP32S2, The filter will filter all the enabled channel data of the each ADC unit at the same time.
  271. * @param idx Filter index.
  272. * @return Filtered data. if <0, the read data invalid.
  273. */
  274. int adc_digi_filter_read_data(adc_digi_filter_idx_t idx)
  275. {
  276. if (idx == ADC_DIGI_FILTER_IDX0) {
  277. return adc_hal_digi_filter_read_data(ADC_NUM_1);
  278. } else if (idx == ADC_DIGI_FILTER_IDX1) {
  279. return adc_hal_digi_filter_read_data(ADC_NUM_2);
  280. } else {
  281. return -1;
  282. }
  283. }
  284. /**************************************/
  285. /* Digital controller monitor setting */
  286. /**************************************/
  287. esp_err_t adc_digi_monitor_set_config(adc_digi_monitor_idx_t idx, adc_digi_monitor_t *config)
  288. {
  289. ADC_ENTER_CRITICAL();
  290. if (idx == ADC_DIGI_MONITOR_IDX0) {
  291. adc_hal_digi_monitor_config(ADC_NUM_1, config);
  292. } else if (idx == ADC_DIGI_MONITOR_IDX1) {
  293. adc_hal_digi_monitor_config(ADC_NUM_2, config);
  294. }
  295. ADC_EXIT_CRITICAL();
  296. return ESP_OK;
  297. }
  298. esp_err_t adc_digi_monitor_enable(adc_digi_monitor_idx_t idx, bool enable)
  299. {
  300. ADC_ENTER_CRITICAL();
  301. if (idx == ADC_DIGI_MONITOR_IDX0) {
  302. adc_hal_digi_monitor_enable(ADC_NUM_1, enable);
  303. } else if (idx == ADC_DIGI_MONITOR_IDX1) {
  304. adc_hal_digi_monitor_enable(ADC_NUM_2, enable);
  305. }
  306. ADC_EXIT_CRITICAL();
  307. return ESP_OK;
  308. }
  309. /**************************************/
  310. /* Digital controller intr setting */
  311. /**************************************/
  312. esp_err_t adc_digi_intr_enable(adc_unit_t adc_unit, adc_digi_intr_t intr_mask)
  313. {
  314. ADC_ENTER_CRITICAL();
  315. if (adc_unit & ADC_UNIT_1) {
  316. adc_hal_digi_intr_enable(ADC_NUM_1, intr_mask);
  317. }
  318. if (adc_unit & ADC_UNIT_2) {
  319. adc_hal_digi_intr_enable(ADC_NUM_2, intr_mask);
  320. }
  321. ADC_EXIT_CRITICAL();
  322. return ESP_OK;
  323. }
  324. esp_err_t adc_digi_intr_disable(adc_unit_t adc_unit, adc_digi_intr_t intr_mask)
  325. {
  326. ADC_ENTER_CRITICAL();
  327. if (adc_unit & ADC_UNIT_1) {
  328. adc_hal_digi_intr_disable(ADC_NUM_1, intr_mask);
  329. }
  330. if (adc_unit & ADC_UNIT_2) {
  331. adc_hal_digi_intr_disable(ADC_NUM_2, intr_mask);
  332. }
  333. ADC_EXIT_CRITICAL();
  334. return ESP_OK;
  335. }
  336. esp_err_t adc_digi_intr_clear(adc_unit_t adc_unit, adc_digi_intr_t intr_mask)
  337. {
  338. ADC_ENTER_CRITICAL();
  339. if (adc_unit & ADC_UNIT_1) {
  340. adc_hal_digi_intr_clear(ADC_NUM_1, intr_mask);
  341. }
  342. if (adc_unit & ADC_UNIT_2) {
  343. adc_hal_digi_intr_clear(ADC_NUM_2, intr_mask);
  344. }
  345. ADC_EXIT_CRITICAL();
  346. return ESP_OK;
  347. }
  348. uint32_t adc_digi_intr_get_status(adc_unit_t adc_unit)
  349. {
  350. uint32_t ret = 0;
  351. ADC_ENTER_CRITICAL();
  352. if (adc_unit & ADC_UNIT_1) {
  353. ret = adc_hal_digi_get_intr_status(ADC_NUM_1);
  354. }
  355. if (adc_unit & ADC_UNIT_2) {
  356. ret = adc_hal_digi_get_intr_status(ADC_NUM_2);
  357. }
  358. ADC_EXIT_CRITICAL();
  359. return ret;
  360. }
  361. static uint8_t s_isr_registered = 0;
  362. static intr_handle_t s_adc_isr_handle = NULL;
  363. esp_err_t adc_digi_isr_register(void (*fn)(void *), void *arg, int intr_alloc_flags)
  364. {
  365. ADC_CHECK((fn != NULL), "Parameter error", ESP_ERR_INVALID_ARG);
  366. ADC_CHECK(s_isr_registered == 0, "ADC ISR have installed, can not install again", ESP_FAIL);
  367. esp_err_t ret = esp_intr_alloc(ETS_APB_ADC_INTR_SOURCE, intr_alloc_flags, fn, arg, &s_adc_isr_handle);
  368. if (ret == ESP_OK) {
  369. s_isr_registered = 1;
  370. }
  371. return ret;
  372. }
  373. esp_err_t adc_digi_isr_deregister(void)
  374. {
  375. esp_err_t ret = ESP_FAIL;
  376. if (s_isr_registered) {
  377. ret = esp_intr_free(s_adc_isr_handle);
  378. if (ret == ESP_OK) {
  379. s_isr_registered = 0;
  380. }
  381. }
  382. return ret;
  383. }
  384. /*---------------------------------------------------------------
  385. RTC controller setting
  386. ---------------------------------------------------------------*/
  387. /*---------------------------------------------------------------
  388. Calibration
  389. ---------------------------------------------------------------*/
  390. static uint16_t s_adc_cali_param[ADC_NUM_MAX][ADC_ATTEN_MAX] = { {0}, {0} };
  391. //NOTE: according to calibration version, different types of lock may be taken during the process:
  392. // 1. Semaphore when reading efuse
  393. // 2. Spinlock when actually doing ADC calibration
  394. //This function shoudn't be called inside critical section or ISR
  395. uint32_t adc_get_calibration_offset(adc_ll_num_t adc_n, adc_channel_t channel, adc_atten_t atten)
  396. {
  397. #ifdef CONFIG_IDF_ENV_FPGA
  398. return 0;
  399. #endif
  400. if (s_adc_cali_param[adc_n][atten]) {
  401. ESP_LOGV(ADC_TAG, "Use calibrated val ADC%d atten=%d: %04X", adc_n, atten, s_adc_cali_param[adc_n][atten]);
  402. return (uint32_t)s_adc_cali_param[adc_n][atten];
  403. }
  404. uint32_t dout = 0;
  405. // check if we can fetch the values from eFuse.
  406. int version = esp_efuse_rtc_table_read_calib_version();
  407. if (version == 2) {
  408. int tag = esp_efuse_rtc_table_get_tag(version, adc_n + 1, atten, RTCCALIB_V2_PARAM_VINIT);
  409. dout = esp_efuse_rtc_table_get_parsed_efuse_value(tag, false);
  410. } else {
  411. adc_power_acquire();
  412. ADC_ENTER_CRITICAL();
  413. const bool internal_gnd = true;
  414. dout = adc_hal_self_calibration(adc_n, channel, atten, internal_gnd);
  415. ADC_EXIT_CRITICAL();
  416. adc_power_release();
  417. }
  418. ESP_LOGD(ADC_TAG, "Calib(V%d) ADC%d atten=%d: %04X", version, adc_n, atten, dout);
  419. s_adc_cali_param[adc_n][atten] = (uint16_t)dout;
  420. return dout;
  421. }
  422. esp_err_t adc_cal_offset(adc_ll_num_t adc_n, adc_channel_t channel, adc_atten_t atten)
  423. {
  424. adc_hal_calibration_init(adc_n);
  425. uint32_t cal_val = adc_get_calibration_offset(adc_n, channel, atten);
  426. ADC_ENTER_CRITICAL();
  427. adc_hal_set_calibration_param(adc_n, cal_val);
  428. ADC_EXIT_CRITICAL();
  429. return ESP_OK;
  430. }