drv_adc.c 5.7 KB

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