drv_adc.c 5.1 KB

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