drv_adc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2020-2021, Bluetrum Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-02-01 greedyhao first version
  9. */
  10. #include "drv_gpio.h"
  11. #ifdef BSP_USING_ADC0
  12. #include "adc_config.h"
  13. // #define DRV_DEBUG
  14. #define LOG_TAG "drv.adc"
  15. #include <drv_log.h>
  16. struct ab32_adc
  17. {
  18. struct rt_adc_device ab32_adc_device;
  19. hal_sfr_t adc_dat_handle;
  20. char *name;
  21. };
  22. enum
  23. {
  24. #ifdef BSP_USING_ADC0
  25. ADC0_INDEX,
  26. #endif
  27. ADC_INDEX_END
  28. };
  29. static struct ab32_adc ab32_adc_obj[] =
  30. {
  31. #ifdef BSP_USING_ADC0
  32. ADC0_CONFIG,
  33. #endif
  34. };
  35. static rt_err_t ab32_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  36. {
  37. RT_ASSERT(device != RT_NULL);
  38. hal_adc_enable(enabled);
  39. return RT_EOK;
  40. }
  41. static rt_uint32_t ab32_adc_get_channel(rt_uint32_t channel)
  42. {
  43. rt_uint32_t ab32_channel = 0;
  44. switch (channel)
  45. {
  46. case 0:
  47. ab32_channel = ADC_CHANNEL_0;
  48. break;
  49. case 1:
  50. ab32_channel = ADC_CHANNEL_1;
  51. break;
  52. case 2:
  53. ab32_channel = ADC_CHANNEL_2;
  54. break;
  55. case 3:
  56. ab32_channel = ADC_CHANNEL_3;
  57. break;
  58. case 4:
  59. ab32_channel = ADC_CHANNEL_4;
  60. break;
  61. case 5:
  62. ab32_channel = ADC_CHANNEL_5;
  63. break;
  64. case 6:
  65. ab32_channel = ADC_CHANNEL_6;
  66. break;
  67. case 7:
  68. ab32_channel = ADC_CHANNEL_7;
  69. break;
  70. case 8:
  71. ab32_channel = ADC_CHANNEL_8;
  72. break;
  73. case 9:
  74. ab32_channel = ADC_CHANNEL_9;
  75. break;
  76. case 10:
  77. ab32_channel = ADC_CHANNEL_10;
  78. break;
  79. case 11:
  80. ab32_channel = ADC_CHANNEL_11;
  81. break;
  82. case 12:
  83. ab32_channel = ADC_CHANNEL_12;
  84. break;
  85. case 13:
  86. ab32_channel = ADC_CHANNEL_13;
  87. break;
  88. case 14:
  89. ab32_channel = ADC_CHANNEL_14;
  90. break;
  91. case 15:
  92. ab32_channel = ADC_CHANNEL_15;
  93. break;
  94. }
  95. return ab32_channel;
  96. }
  97. static rt_err_t ab32_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  98. {
  99. hal_sfr_t ab32_adc_handler;
  100. RT_ASSERT(device != RT_NULL);
  101. RT_ASSERT(value != RT_NULL);
  102. ab32_adc_handler = device->parent.user_data;
  103. hal_adc_start(ab32_adc_get_channel(channel));
  104. hal_adc_poll_for_conversion(1000);
  105. *value = ab32_adc_handler[channel];
  106. return RT_EOK;
  107. }
  108. static const struct rt_adc_ops _adc_ops =
  109. {
  110. .enabled = ab32_adc_enabled,
  111. .convert = ab32_get_adc_value,
  112. };
  113. static int ab32_adc_init(void)
  114. {
  115. int result = RT_EOK;
  116. int i = 0;
  117. if (ADC_INDEX_END == 0) {
  118. return result;
  119. }
  120. CLKCON0 |= BIT(28); // enable adc clock
  121. for (i = 0; i < sizeof(ab32_adc_obj) / sizeof(ab32_adc_obj[0]); i++) {
  122. if (rt_hw_adc_register(&ab32_adc_obj[i].ab32_adc_device, ab32_adc_obj[i].name, &_adc_ops, (const void *)ab32_adc_obj[i].adc_dat_handle) == RT_EOK)
  123. {
  124. LOG_D("%s init success", ab32_adc_obj[i].name);
  125. }
  126. else
  127. {
  128. LOG_E("%s register failed", ab32_adc_obj[i].name);
  129. result = -RT_ERROR;
  130. }
  131. }
  132. return result;
  133. }
  134. INIT_BOARD_EXPORT(ab32_adc_init);
  135. #endif