drv_adc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-11-23 Chushicheng first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "drv_adc.h"
  13. #include "esp_adc/adc_oneshot.h"
  14. #include "esp_adc/adc_cali.h"
  15. #include "esp_adc/adc_cali_scheme.h"
  16. #include "hal/adc_types.h"
  17. #ifdef BSP_USING_ADC
  18. #define DBG_TAG "drv.adc"
  19. #define DBG_LVL DBG_INFO
  20. #include <rtdbg.h>
  21. #define EXAMPLE_READ_LEN 256
  22. #define GET_UNIT(x) ((x>>3) & 0x1)
  23. /* esp i2c dirver class */
  24. struct esp_adc
  25. {
  26. struct rt_adc_ops ops;
  27. struct rt_adc_device adc_device;
  28. rt_uint8_t adc_id;
  29. adc_oneshot_unit_handle_t handle;
  30. adc_cali_handle_t cali_handle;
  31. rt_uint8_t do_calibration1;
  32. };
  33. static struct esp_adc_config adc_config[] =
  34. {
  35. #ifdef BSP_USING_ADC1
  36. {
  37. .adc_id = ADC_UNIT_1,
  38. .device_name = "adc1",
  39. },
  40. #endif
  41. };
  42. static struct esp_adc esp_adc_obj[sizeof(adc_config) / sizeof(adc_config[0])];
  43. static rt_err_t _adc_enabled(struct rt_adc_device *device, rt_int8_t channel, rt_bool_t enabled)
  44. {
  45. struct esp_adc *_adc = rt_container_of(device, struct esp_adc, adc_device);
  46. if(enabled)
  47. {
  48. //-------------ADC Init---------------//
  49. adc_oneshot_unit_init_cfg_t init_config = {
  50. .unit_id = _adc->adc_id,
  51. };
  52. ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &_adc->handle));
  53. //-------------ADC Config---------------//
  54. adc_oneshot_chan_cfg_t config = {
  55. .bitwidth = ADC_BITWIDTH_DEFAULT,
  56. .atten = ADC_ATTEN_DB_11,
  57. };
  58. ESP_ERROR_CHECK(adc_oneshot_config_channel(_adc->handle, channel, &config));
  59. //-------------ADC Calibration Init---------------//
  60. adc_cali_curve_fitting_config_t cali_config = {
  61. .unit_id = _adc->adc_id,
  62. .atten = ADC_ATTEN_DB_11,
  63. .bitwidth = ADC_BITWIDTH_DEFAULT,
  64. };
  65. if (adc_cali_create_scheme_curve_fitting(&cali_config, &_adc->cali_handle) == ESP_OK)
  66. {
  67. _adc->do_calibration1 = 1;
  68. }
  69. else
  70. {
  71. _adc->do_calibration1 = 0;
  72. }
  73. // _adc->do_calibration1 = example_adc_calibration_init(_adc->adc_id, ADC_ATTEN_DB_11, &_adc->cali_handle);
  74. }
  75. else
  76. {
  77. ESP_ERROR_CHECK(adc_oneshot_del_unit(_adc->handle));
  78. ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(_adc->cali_handle));
  79. }
  80. return RT_EOK;
  81. }
  82. static rt_err_t _adc_get_value(struct rt_adc_device *device, rt_int8_t channel, rt_uint32_t *value)
  83. {
  84. RT_ASSERT(device != RT_NULL);
  85. RT_ASSERT(value != RT_NULL);
  86. rt_uint32_t adc_raw = 0;
  87. struct esp_adc *_adc = rt_container_of(device, struct esp_adc, adc_device);
  88. ESP_ERROR_CHECK(adc_oneshot_read(_adc->handle, channel, &adc_raw));
  89. if (_adc->do_calibration1)
  90. {
  91. ESP_ERROR_CHECK(adc_cali_raw_to_voltage(_adc->cali_handle, adc_raw, value));
  92. }
  93. else
  94. {
  95. *value = adc_raw;
  96. }
  97. return RT_EOK;
  98. }
  99. static const struct rt_adc_ops esp_adc_ops =
  100. {
  101. .enabled = _adc_enabled,
  102. .convert = _adc_get_value,
  103. .get_resolution = RT_NULL,
  104. .get_vref = RT_NULL,
  105. };
  106. int rt_hw_adc_init(void)
  107. {
  108. int result = RT_EOK;
  109. for (rt_size_t i = 0; i < sizeof(esp_adc_obj) / sizeof(struct esp_adc); i++)
  110. {
  111. esp_adc_obj[i].adc_id = adc_config[i].adc_id;
  112. /* register ADC device */
  113. if (rt_hw_adc_register(&esp_adc_obj[i].adc_device, adc_config[i].device_name, &esp_adc_ops, &adc_config[i]) == RT_EOK)
  114. {
  115. LOG_D("%s init success", adc_config[i].device_name);
  116. }
  117. else
  118. {
  119. LOG_E("%s register failed", adc_config[i].device_name);
  120. result = -RT_ERROR;
  121. }
  122. }
  123. return result;
  124. }
  125. INIT_BOARD_EXPORT(rt_hw_adc_init);
  126. #endif /* BSP_USING_ADC */