drv_adc.c 5.5 KB

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