drv_adc.c 5.9 KB

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