drv_adc.c 6.4 KB

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