drv_adc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-04-08 wangyq the first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "board.h"
  14. #include "drv_adc.h"
  15. #include <ald_gpio.h>
  16. #include <ald_adc.h>
  17. #ifdef RT_USING_ADC
  18. /* define adc instance */
  19. static struct rt_adc_device _device_adc0;
  20. /* enable or disable adc */
  21. static rt_err_t es32f0_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  22. {
  23. adc_handle_t *_hadc = (adc_handle_t *)device->parent.user_data;
  24. RT_ASSERT(device != RT_NULL);
  25. if (enabled)
  26. {
  27. ADC_ENABLE(_hadc);
  28. }
  29. else
  30. {
  31. ADC_DISABLE(_hadc);
  32. }
  33. return RT_EOK;
  34. }
  35. static adc_channel_t es32f0_adc_get_channel(rt_uint32_t channel)
  36. {
  37. adc_channel_t es32f0_channel;
  38. gpio_init_t gpio_initstruct;
  39. /* Initialize ADC pin */
  40. gpio_initstruct.mode = GPIO_MODE_INPUT;
  41. gpio_initstruct.pupd = GPIO_FLOATING;
  42. gpio_initstruct.odrv = GPIO_OUT_DRIVE_NORMAL;
  43. gpio_initstruct.flt = GPIO_FILTER_DISABLE;
  44. gpio_initstruct.type = GPIO_TYPE_CMOS;
  45. gpio_initstruct.func = GPIO_FUNC_0;
  46. /* select gpio pin as adc function */
  47. switch (channel)
  48. {
  49. case 0:
  50. es32f0_channel = ADC_CHANNEL_0;
  51. gpio_init(GPIOC, GPIO_PIN_0, &gpio_initstruct);
  52. break;
  53. case 1:
  54. es32f0_channel = ADC_CHANNEL_1;
  55. gpio_init(GPIOC, GPIO_PIN_1, &gpio_initstruct);
  56. break;
  57. case 2:
  58. es32f0_channel = ADC_CHANNEL_2;
  59. gpio_init(GPIOC, GPIO_PIN_2, &gpio_initstruct);
  60. break;
  61. case 3:
  62. es32f0_channel = ADC_CHANNEL_3;
  63. gpio_init(GPIOC, GPIO_PIN_3, &gpio_initstruct);
  64. break;
  65. case 4:
  66. es32f0_channel = ADC_CHANNEL_4;
  67. gpio_init(GPIOA, GPIO_PIN_0, &gpio_initstruct);
  68. break;
  69. case 5:
  70. es32f0_channel = ADC_CHANNEL_5;
  71. gpio_init(GPIOA, GPIO_PIN_1, &gpio_initstruct);
  72. break;
  73. case 6:
  74. es32f0_channel = ADC_CHANNEL_6;
  75. gpio_init(GPIOA, GPIO_PIN_2, &gpio_initstruct);
  76. break;
  77. case 7:
  78. es32f0_channel = ADC_CHANNEL_7;
  79. gpio_init(GPIOA, GPIO_PIN_3, &gpio_initstruct);
  80. break;
  81. case 8:
  82. es32f0_channel = ADC_CHANNEL_8;
  83. gpio_init(GPIOA, GPIO_PIN_4, &gpio_initstruct);
  84. break;
  85. case 9:
  86. es32f0_channel = ADC_CHANNEL_9;
  87. gpio_init(GPIOA, GPIO_PIN_5, &gpio_initstruct);
  88. break;
  89. case 10:
  90. es32f0_channel = ADC_CHANNEL_10;
  91. gpio_init(GPIOA, GPIO_PIN_6, &gpio_initstruct);
  92. break;
  93. case 11:
  94. es32f0_channel = ADC_CHANNEL_11;
  95. gpio_init(GPIOA, GPIO_PIN_7, &gpio_initstruct);
  96. break;
  97. case 12:
  98. es32f0_channel = ADC_CHANNEL_12;
  99. gpio_init(GPIOC, GPIO_PIN_4, &gpio_initstruct);
  100. break;
  101. case 13:
  102. es32f0_channel = ADC_CHANNEL_13;
  103. gpio_init(GPIOC, GPIO_PIN_5, &gpio_initstruct);
  104. break;
  105. case 14:
  106. es32f0_channel = ADC_CHANNEL_14;
  107. gpio_init(GPIOB, GPIO_PIN_0, &gpio_initstruct);
  108. break;
  109. case 15:
  110. es32f0_channel = ADC_CHANNEL_15;
  111. gpio_init(GPIOB, GPIO_PIN_1, &gpio_initstruct);
  112. break;
  113. case 16:
  114. es32f0_channel = ADC_CHANNEL_16;
  115. break;
  116. case 17:
  117. es32f0_channel = ADC_CHANNEL_17;
  118. break;
  119. case 18:
  120. es32f0_channel = ADC_CHANNEL_18;
  121. break;
  122. default:
  123. break;
  124. }
  125. return es32f0_channel;
  126. }
  127. static rt_err_t es32f0_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  128. {
  129. adc_handle_t *_hadc = (adc_handle_t *)device->parent.user_data;
  130. adc_channel_conf_t nm_config;
  131. RT_ASSERT(device != RT_NULL);
  132. RT_ASSERT(value != RT_NULL);
  133. /* config adc channel */
  134. nm_config.channel = es32f0_adc_get_channel(channel);
  135. nm_config.rank = ADC_NC_RANK_1;
  136. nm_config.sampling_time = ADC_SAMPLETIME_4;
  137. adc_normal_channel_config(_hadc, &nm_config);
  138. adc_normal_start(_hadc);
  139. if (adc_normal_poll_for_conversion(_hadc, 5000) == OK)
  140. *value = adc_normal_get_value(_hadc);
  141. return RT_EOK;
  142. }
  143. static const struct rt_adc_ops es32f0_adc_ops =
  144. {
  145. es32f0_adc_enabled,
  146. es32f0_get_adc_value,
  147. };
  148. int rt_hw_adc_init(void)
  149. {
  150. int result = RT_EOK;
  151. static adc_handle_t _h_adc0;
  152. /* adc function initialization */
  153. _h_adc0.perh = ADC0;
  154. _h_adc0.init.data_align = ADC_DATAALIGN_RIGHT;
  155. _h_adc0.init.scan_mode = ADC_SCAN_DISABLE;
  156. _h_adc0.init.cont_mode = DISABLE;
  157. _h_adc0.init.conv_nbr = ADC_NM_NBR_1;
  158. _h_adc0.init.disc_mode = DISABLE;
  159. _h_adc0.init.disc_nbr = ADC_DISC_NBR_1;
  160. _h_adc0.init.conv_res = ADC_CONV_RES_10;
  161. _h_adc0.init.clk_div = ADC_CKDIV_128;
  162. _h_adc0.init.nche_mode = ADC_NCHESEL_MODE_ALL;
  163. _h_adc0.init.neg_ref = ADC_NEG_REF_VSS;
  164. _h_adc0.init.pos_ref = ADC_POS_REF_VDD;
  165. adc_init(&_h_adc0);
  166. rt_hw_adc_register(&_device_adc0, "adc0", &es32f0_adc_ops, &_h_adc0);
  167. return result;
  168. }
  169. INIT_BOARD_EXPORT(rt_hw_adc_init);
  170. #endif