drv_adc.c 5.2 KB

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