drv_adc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_data_alignment_config(adc_periph, ADC_DATAALIGN_RIGHT);
  108. #if defined SOC_SERIES_GD32F4xx
  109. adc_channel_length_config(adc_periph, ADC_ROUTINE_CHANNEL, 1);
  110. adc_external_trigger_source_config(adc_periph, ADC_ROUTINE_CHANNEL, ADC_EXTTRIG_ROUTINE_EXTI_11);
  111. adc_external_trigger_config(adc_periph, ADC_ROUTINE_CHANNEL, ENABLE);
  112. #else
  113. adc_channel_length_config(adc_periph, ADC_REGULAR_CHANNEL, 1);
  114. adc_external_trigger_source_config(adc_periph, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE);
  115. adc_external_trigger_config(adc_periph, ADC_REGULAR_CHANNEL, ENABLE);
  116. #endif
  117. adc_enable(adc_periph);
  118. rt_hw_us_delay(1);
  119. /* ADC calibration and reset calibration */
  120. adc_calibration_enable(adc_periph);
  121. }
  122. else
  123. {
  124. adc_disable(adc_periph);
  125. }
  126. return RT_EOK;
  127. }
  128. /**
  129. * @brief convert adc.
  130. * This function get adc value.
  131. * @param device, channel, value
  132. * @retval None
  133. */
  134. static rt_err_t gd32_adc_convert(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  135. {
  136. uint32_t adc_periph;
  137. uint32_t timeout = 0;
  138. struct gd32_adc *adc = (struct gd32_adc *)(device->parent.user_data);
  139. if (!value)
  140. {
  141. LOG_E("invalid param");
  142. return -RT_EINVAL;
  143. }
  144. adc_periph = (uint32_t)(adc->adc_periph);
  145. adc_flag_clear(adc_periph, ADC_FLAG_EOC | ADC_FLAG_STRC);
  146. #if defined SOC_SERIES_GD32F4xx
  147. adc_routine_channel_config(adc_periph, 0, channel, ADC_SAMPLETIME_480);
  148. adc_software_trigger_enable(adc_periph, ADC_ROUTINE_CHANNEL);
  149. #else
  150. adc_regular_channel_config(adc_periph, 0, channel, ADC_SAMPLETIME_13POINT5);
  151. adc_software_trigger_enable(adc_periph, ADC_REGULAR_CHANNEL);
  152. #endif
  153. while (!adc_flag_get(adc_periph, ADC_FLAG_EOC))
  154. {
  155. if(timeout >= 100)
  156. {
  157. adc_flag_clear(adc_periph, ADC_FLAG_EOC | ADC_FLAG_STRC);
  158. LOG_E("Convert Timeout");
  159. return -RT_ETIMEOUT;
  160. }
  161. else
  162. {
  163. timeout++;
  164. rt_hw_us_delay(1);
  165. }
  166. }
  167. #if defined SOC_SERIES_GD32F4xx
  168. *value = adc_routine_data_read(adc_periph);
  169. #else
  170. *value = adc_regular_data_read(adc_periph);
  171. #endif
  172. adc_flag_clear(adc_periph, ADC_FLAG_EOC | ADC_FLAG_STRC);
  173. return RT_EOK;
  174. }
  175. static struct rt_adc_ops gd32_adc_ops = {
  176. .enabled = gd32_adc_enabled,
  177. .convert = gd32_adc_convert,
  178. };
  179. static int rt_hw_adc_init(void)
  180. {
  181. int ret, i = 0;
  182. for (; i < sizeof(adc_obj) / sizeof(adc_obj[0]); i++)
  183. {
  184. ret = rt_hw_adc_register(adc_obj[i].adc, \
  185. (const char *)adc_obj[i].device_name, \
  186. &gd32_adc_ops, &adc_obj[i]);
  187. if (ret != RT_EOK)
  188. {
  189. /* TODO err handler */
  190. LOG_E("failed register %s, err=%d", adc_obj[i].device_name, ret);
  191. }
  192. }
  193. return ret;
  194. }
  195. INIT_BOARD_EXPORT(rt_hw_adc_init);
  196. #endif