1
0

drv_adc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-05-16 shelton first version
  9. */
  10. #include "drv_common.h"
  11. #include "drv_adc.h"
  12. #if defined(BSP_USING_ADC1) || defined(BSP_USING_ADC2) || \
  13. defined(BSP_USING_ADC3)
  14. //#define DRV_DEBUG
  15. #define LOG_TAG "drv.adc"
  16. #include <drv_log.h>
  17. struct at32_adc
  18. {
  19. struct rt_adc_device at32_adc_device;
  20. adc_type *adc_x;
  21. char *name;
  22. };
  23. static struct at32_adc at32_adc_obj[] =
  24. {
  25. #ifdef BSP_USING_ADC1
  26. ADC1_CONFIG,
  27. #endif
  28. #ifdef BSP_USING_ADC2
  29. ADC2_CONFIG,
  30. #endif
  31. #ifdef BSP_USING_ADC3
  32. ADC3_CONFIG,
  33. #endif
  34. };
  35. static rt_err_t at32_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  36. {
  37. adc_type *adc_x;
  38. adc_base_config_type adc_config_struct;
  39. #if defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437)
  40. adc_common_config_type adc_common_struct;
  41. #endif
  42. RT_ASSERT(device != RT_NULL);
  43. adc_x = device->parent.user_data;
  44. at32_msp_adc_init(adc_x);
  45. #if defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437)
  46. adc_common_default_para_init(&adc_common_struct);
  47. /* config combine mode */
  48. adc_common_struct.combine_mode = ADC_INDEPENDENT_MODE;
  49. /* config division, adcclk is division by hclk */
  50. adc_common_struct.div = ADC_HCLK_DIV_4;
  51. /* config common dma mode,it's not useful in independent mode */
  52. adc_common_struct.common_dma_mode = ADC_COMMON_DMAMODE_DISABLE;
  53. /* config common dma request repeat */
  54. adc_common_struct.common_dma_request_repeat_state = FALSE;
  55. /* config adjacent adc sampling interval,it's useful for ordinary shifting mode */
  56. adc_common_struct.sampling_interval = ADC_SAMPLING_INTERVAL_5CYCLES;
  57. /* config inner temperature sensor and vintrv */
  58. adc_common_struct.tempervintrv_state = FALSE;
  59. /* config voltage battery */
  60. adc_common_struct.vbat_state = FALSE;
  61. adc_common_config(&adc_common_struct);
  62. #else
  63. #if !defined (SOC_SERIES_AT32F415)
  64. adc_combine_mode_select(ADC_INDEPENDENT_MODE);
  65. #endif
  66. adc_ordinary_conversion_trigger_set(adc_x, ADC12_ORDINARY_TRIG_SOFTWARE, TRUE);
  67. #endif
  68. /* adc_x configuration */
  69. adc_base_default_para_init(&adc_config_struct);
  70. adc_config_struct.data_align = ADC_RIGHT_ALIGNMENT;
  71. adc_config_struct.ordinary_channel_length = 1;
  72. adc_config_struct.repeat_mode = FALSE;
  73. adc_config_struct.sequence_mode = FALSE;
  74. adc_base_config(adc_x, &adc_config_struct);
  75. if (!enabled)
  76. {
  77. /* disable adc_x */
  78. adc_enable(adc_x, FALSE);
  79. }
  80. else
  81. {
  82. /* enable adc_x */
  83. adc_enable(adc_x, TRUE);
  84. /* enable adc_x calibration */
  85. adc_calibration_init(adc_x);
  86. /* check the end of adc_x reset calibration register */
  87. while(adc_calibration_init_status_get(adc_x) == SET)
  88. {
  89. }
  90. /* start adc_x calibration */
  91. adc_calibration_start(adc_x);
  92. /* check the end of adc_x calibration */
  93. while(adc_calibration_status_get(adc_x) == SET)
  94. {
  95. }
  96. }
  97. return RT_EOK;
  98. }
  99. static rt_err_t at32_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  100. {
  101. adc_type *adc_x;
  102. rt_uint32_t timeout = 0;
  103. RT_ASSERT(device != RT_NULL);
  104. adc_x = device->parent.user_data;
  105. /* adc_x regular channels configuration */
  106. #if defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437)
  107. adc_ordinary_channel_set(adc_x, (adc_channel_select_type)channel, 1, ADC_SAMPLETIME_247_5);
  108. #else
  109. adc_ordinary_channel_set(adc_x, (adc_channel_select_type)channel, 1, ADC_SAMPLETIME_239_5);
  110. #endif
  111. /* start adc_x software conversion */
  112. adc_ordinary_software_trigger_enable(adc_x, TRUE);
  113. /* wait for the adc to convert */
  114. #if defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437)
  115. while((adc_flag_get(adc_x, ADC_OCCE_FLAG) == RESET) && timeout < 0xFFFF)
  116. #else
  117. while((adc_flag_get(adc_x, ADC_CCE_FLAG) == RESET) && timeout < 0xFFFF)
  118. #endif
  119. {
  120. timeout ++;
  121. }
  122. if(timeout >= 0xFFFF)
  123. {
  124. LOG_D("channel%d converts timeout, please confirm adc_x enabled or not", channel);
  125. }
  126. /* get adc value */
  127. *value = adc_ordinary_conversion_data_get(adc_x);
  128. return RT_EOK;
  129. }
  130. static const struct rt_adc_ops at_adc_ops =
  131. {
  132. .enabled = at32_adc_enabled,
  133. .convert = at32_get_adc_value,
  134. };
  135. static int rt_hw_adc_init(void)
  136. {
  137. int result = RT_EOK;
  138. int i = 0;
  139. for (i = 0; i < sizeof(at32_adc_obj) / sizeof(at32_adc_obj[0]); i++)
  140. {
  141. /* register ADC device */
  142. if (rt_hw_adc_register(&at32_adc_obj[i].at32_adc_device, at32_adc_obj[i].name, &at_adc_ops, at32_adc_obj[i].adc_x) == RT_EOK)
  143. {
  144. LOG_D("%s register success", at32_adc_obj[i].name);
  145. }
  146. else
  147. {
  148. LOG_E("%s register failed", at32_adc_obj[i].name);
  149. result = -RT_ERROR;
  150. }
  151. }
  152. return result;
  153. }
  154. INIT_BOARD_EXPORT(rt_hw_adc_init);
  155. #endif /* BSP_USING_ADC */