drv_adc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-02-25 iysheng first version
  9. * 2022-05-03 BruceOu optimization adc
  10. */
  11. #include "drv_adc.h"
  12. #define DBG_TAG "drv.adc"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. #ifdef RT_USING_ADC
  16. #if defined(BSP_USING_ADC0)
  17. struct rt_adc_device adc0;
  18. #endif
  19. #if defined(BSP_USING_ADC1)
  20. struct rt_adc_device adc1;
  21. #endif
  22. #if defined(BSP_USING_ADC2)
  23. struct rt_adc_device adc2;
  24. #endif
  25. #define MAX_EXTERN_ADC_CHANNEL 16
  26. static const struct gd32_adc adc_obj[] = {
  27. #ifdef BSP_USING_ADC0
  28. {
  29. ADC0,
  30. RCU_ADC0,
  31. {
  32. GET_PIN(A, 0), GET_PIN(A, 1), GET_PIN(A, 2), GET_PIN(A, 3),
  33. GET_PIN(A, 4), GET_PIN(A, 5), GET_PIN(A, 6), GET_PIN(A, 7),
  34. GET_PIN(B, 0), GET_PIN(B, 1), GET_PIN(C, 0), GET_PIN(C, 1),
  35. GET_PIN(C, 2), GET_PIN(C, 3), GET_PIN(C, 4), GET_PIN(C, 5),
  36. },
  37. &adc0,
  38. "adc0",
  39. },
  40. #endif
  41. #ifdef BSP_USING_ADC1
  42. {
  43. ADC1,
  44. RCU_ADC1,
  45. {
  46. GET_PIN(A, 0), GET_PIN(A, 1), GET_PIN(A, 2), GET_PIN(A, 3),
  47. GET_PIN(A, 4), GET_PIN(A, 5), GET_PIN(A, 6), GET_PIN(A, 7),
  48. GET_PIN(B, 0), GET_PIN(B, 1), GET_PIN(C, 0), GET_PIN(C, 1),
  49. GET_PIN(C, 2), GET_PIN(C, 3), GET_PIN(C, 4), GET_PIN(C, 5),
  50. },
  51. &adc1,
  52. "adc1",
  53. },
  54. #endif
  55. #ifdef BSP_USING_ADC2
  56. {
  57. ADC2,
  58. RCU_ADC2,
  59. {
  60. GET_PIN(A, 0), GET_PIN(A, 1), GET_PIN(A, 2), GET_PIN(A, 3),
  61. GET_PIN(A, 4), GET_PIN(A, 5), GET_PIN(A, 6), GET_PIN(A, 7),
  62. GET_PIN(B, 0), GET_PIN(B, 1), GET_PIN(C, 0), GET_PIN(C, 1),
  63. GET_PIN(C, 2), GET_PIN(C, 3), GET_PIN(C, 4), GET_PIN(C, 5),
  64. },
  65. &adc2,
  66. "adc2",
  67. },
  68. #endif
  69. };
  70. /**
  71. * @brief ADC MSP Initialization
  72. * This function configures the hardware resources.
  73. * @param adc_clk, pin
  74. * @retval None
  75. */
  76. static void gd32_adc_gpio_init(rcu_periph_enum adc_clk, rt_base_t pin)
  77. {
  78. /* enable ADC clock */
  79. rcu_periph_clock_enable(adc_clk);
  80. #if defined SOC_SERIES_GD32F4xx
  81. /* configure adc pin */
  82. gpio_mode_set(PIN_GDPORT(pin), GPIO_MODE_ANALOG, GPIO_PUPD_NONE, PIN_GDPIN(pin));
  83. #else
  84. /* configure adc pin */
  85. gpio_init(PIN_GDPORT(pin), GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, PIN_GDPIN(pin));
  86. #endif
  87. }
  88. /**
  89. * @brief ADC enable
  90. * This function enable adc.
  91. * @param device, channel, enabled
  92. * @retval None
  93. */
  94. static rt_err_t gd32_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  95. {
  96. uint32_t adc_periph;
  97. struct gd32_adc * adc = (struct gd32_adc *)device->parent.user_data;
  98. if (channel >= MAX_EXTERN_ADC_CHANNEL)
  99. {
  100. LOG_E("invalid channel");
  101. return -RT_EINVAL;
  102. }
  103. adc_periph = (uint32_t )(adc->adc_periph);
  104. if (enabled == ENABLE)
  105. {
  106. gd32_adc_gpio_init(adc->adc_clk, adc->adc_pins[channel]);
  107. adc_channel_length_config(adc_periph, ADC_REGULAR_CHANNEL, 1);
  108. adc_data_alignment_config(adc_periph, ADC_DATAALIGN_RIGHT);
  109. #if defined SOC_SERIES_GD32F4xx
  110. adc_external_trigger_source_config(adc_periph, ADC_REGULAR_CHANNEL, ADC_EXTTRIG_REGULAR_EXTI_11);
  111. #else
  112. adc_external_trigger_source_config(adc_periph, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE);
  113. #endif
  114. adc_external_trigger_config(adc_periph, ADC_REGULAR_CHANNEL, ENABLE);
  115. #if defined SOC_SERIES_GD32F4xx
  116. adc_regular_channel_config(adc_periph, 0, channel, ADC_SAMPLETIME_480);
  117. #else
  118. adc_regular_channel_config(adc_periph, 0, channel, ADC_SAMPLETIME_13POINT5);
  119. #endif
  120. adc_enable(adc_periph);
  121. /* ADC calibration and reset calibration */
  122. adc_calibration_enable(adc_periph);
  123. }
  124. else
  125. {
  126. adc_disable(adc_periph);
  127. }
  128. return 0;
  129. }
  130. /**
  131. * @brief convert adc.
  132. * This function get adc value.
  133. * @param device, channel, value
  134. * @retval None
  135. */
  136. static rt_err_t gd32_adc_convert(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  137. {
  138. uint32_t adc_periph;
  139. struct gd32_adc * adc = (struct gd32_adc *)(device->parent.user_data);
  140. if (!value)
  141. {
  142. LOG_E("invalid param");
  143. return -RT_EINVAL;
  144. }
  145. adc_periph = (uint32_t )(adc->adc_periph);
  146. adc_software_trigger_enable(adc_periph, ADC_REGULAR_CHANNEL);
  147. while(!adc_flag_get(adc_periph, ADC_FLAG_EOC)){};
  148. // clear flag
  149. adc_flag_clear(adc_periph, ADC_FLAG_EOC);
  150. *value = adc_regular_data_read(adc_periph);
  151. return 0;
  152. }
  153. static struct rt_adc_ops gd32_adc_ops = {
  154. .enabled = gd32_adc_enabled,
  155. .convert = gd32_adc_convert,
  156. };
  157. static int rt_hw_adc_init(void)
  158. {
  159. int ret, i = 0;
  160. for (; i < sizeof(adc_obj) / sizeof(adc_obj[0]); i++)
  161. {
  162. ret = rt_hw_adc_register(adc_obj[i].adc, \
  163. (const char *)adc_obj[i].device_name, \
  164. &gd32_adc_ops, &adc_obj[i]);
  165. if (ret != RT_EOK)
  166. {
  167. /* TODO err handler */
  168. LOG_E("failed register %s, err=%d", adc_obj[i].device_name, ret);
  169. }
  170. }
  171. return ret;
  172. }
  173. INIT_BOARD_EXPORT(rt_hw_adc_init);
  174. #endif