drv_adc.c 4.7 KB

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