drv_adc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-06-03 hqfang the first version.
  9. *
  10. */
  11. #include "drv_adc.h"
  12. #ifdef BSP_USING_ADC
  13. #if !defined(BSP_USING_ADC0) && !defined(BSP_USING_ADC1)
  14. #error "Please define at least one BSP_USING_ADCx"
  15. /* this driver can be disabled at menuconfig -> Hardware Drivers Config -> On-chip Peripheral Drivers -> Enable ADC */
  16. #endif
  17. static struct gd32_adc_config adc_config[] =
  18. {
  19. #ifdef BSP_USING_ADC0
  20. {
  21. "adc0",
  22. ADC0,
  23. },
  24. #endif
  25. #ifdef BSP_USING_ADC1
  26. {
  27. "adc1",
  28. ADC1,
  29. },
  30. #endif
  31. };
  32. static struct gd32_adc adc_obj[sizeof(adc_config) / sizeof(adc_config[0])] = {0};
  33. static void gd32_adc_init(struct gd32_adc_config *config)
  34. {
  35. RT_ASSERT(config != RT_NULL);
  36. adc_deinit(config->adc_periph);
  37. ADC_CTL0(config->adc_periph) &= ~(ADC_CTL0_SYNCM);
  38. ADC_CTL0(config->adc_periph) |= ADC_MODE_FREE;
  39. ADC_CTL1(config->adc_periph) |= ADC_CTL1_TSVREN;
  40. adc_resolution_config(config->adc_periph, ADC_RESOLUTION_12B);
  41. /* ADC contineous function enable */
  42. adc_special_function_config(config->adc_periph, ADC_SCAN_MODE, ENABLE);
  43. /* ADC data alignment config */
  44. adc_data_alignment_config(config->adc_periph, ADC_DATAALIGN_RIGHT);
  45. /* ADC channel length config */
  46. adc_channel_length_config(config->adc_periph, ADC_REGULAR_CHANNEL, 1);
  47. adc_external_trigger_source_config(config->adc_periph, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_NONE);
  48. /* ADC enable */
  49. adc_external_trigger_config(config->adc_periph, ADC_REGULAR_CHANNEL, ENABLE);
  50. adc_enable(config->adc_periph);
  51. adc_calibration_enable(config->adc_periph);
  52. }
  53. static rt_err_t gd32_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  54. {
  55. if (channel > ADC_CHANNEL_17)
  56. {
  57. return RT_EINVAL;
  58. }
  59. return RT_EOK;
  60. }
  61. static rt_err_t gd32_adc_convert(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  62. {
  63. struct gd32_adc_config *config;
  64. RT_ASSERT(device != RT_NULL);
  65. if (channel > ADC_CHANNEL_17)
  66. {
  67. return RT_EINVAL;
  68. }
  69. config = (struct gd32_adc_config *)(device->parent.user_data);
  70. if (channel > ADC_CHANNEL_15)
  71. {
  72. adc_regular_channel_config(config->adc_periph, 0, channel, ADC_SAMPLETIME_239POINT5);
  73. }
  74. else
  75. {
  76. adc_regular_channel_config(config->adc_periph, 0, channel, ADC_SAMPLETIME_55POINT5);
  77. }
  78. adc_software_trigger_enable(config->adc_periph, ADC_REGULAR_CHANNEL);
  79. while (SET != adc_flag_get(config->adc_periph, ADC_FLAG_EOC));
  80. adc_flag_clear(config->adc_periph, ADC_FLAG_EOC);
  81. *value = ADC_RDATA(config->adc_periph);
  82. return RT_EOK;
  83. }
  84. static struct rt_adc_ops gd32_adc_ops =
  85. {
  86. .enabled = gd32_adc_enabled,
  87. .convert = gd32_adc_convert,
  88. };
  89. int rt_hw_adc_init(void)
  90. {
  91. int i = 0;
  92. int result = RT_EOK;
  93. #if defined(BSP_USING_ADC0)
  94. rcu_periph_clock_enable(RCU_ADC0);
  95. #endif
  96. #if defined(BSP_USING_ADC1)
  97. rcu_periph_clock_enable(RCU_ADC1);
  98. #endif
  99. rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV8);
  100. for (i = 0; i < sizeof(adc_obj) / sizeof(adc_obj[0]); i++)
  101. {
  102. adc_obj[i].config = &adc_config[i];
  103. gd32_adc_init(&adc_config[i]);
  104. rt_hw_adc_register(&adc_obj[i].adc_device, \
  105. adc_obj[i].config->name, &gd32_adc_ops, adc_obj[i].config);
  106. }
  107. return result;
  108. }
  109. INIT_DEVICE_EXPORT(rt_hw_adc_init);
  110. #endif /* BSP_USING_ADC */