drv_adc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Change Logs:
  19. * Date Author Notes
  20. * 2019-04-03 wangyq the first version
  21. * 2019-11-01 wangyq update libraries
  22. * 2021-04-20 liuhy the second version
  23. */
  24. #include <rthw.h>
  25. #include <rtthread.h>
  26. #include <rtdevice.h>
  27. #include "board.h"
  28. #include "drv_adc.h"
  29. #include <ald_gpio.h>
  30. #include <ald_adc.h>
  31. #ifdef RT_USING_ADC
  32. /* define adc instance */
  33. static struct rt_adc_device _device_adc0;
  34. /* enable or disable adc */
  35. static rt_err_t es32f0_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  36. {
  37. adc_handle_t *_hadc = (adc_handle_t *)device->parent.user_data;
  38. RT_ASSERT(device != RT_NULL);
  39. if (enabled)
  40. {
  41. ADC_ENABLE(_hadc); ;
  42. }
  43. else
  44. {
  45. ADC_DISABLE(_hadc);
  46. }
  47. return RT_EOK;
  48. }
  49. static adc_channel_t es32f0_adc_get_channel(rt_uint32_t channel)
  50. {
  51. adc_channel_t es32f0_channel;
  52. gpio_init_t gpio_initstruct;
  53. /* Initialize ADC pin */
  54. gpio_initstruct.mode = GPIO_MODE_INPUT;
  55. gpio_initstruct.pupd = GPIO_FLOATING;
  56. gpio_initstruct.odrv = GPIO_OUT_DRIVE_NORMAL;
  57. gpio_initstruct.flt = GPIO_FILTER_DISABLE;
  58. gpio_initstruct.type = GPIO_TYPE_CMOS;
  59. gpio_initstruct.func = GPIO_FUNC_0;
  60. /* select gpio pin as adc function */
  61. switch (channel)
  62. {
  63. case 0:
  64. es32f0_channel = ADC_CHANNEL_0;
  65. ald_gpio_init(ES_GPIO_ADC_CH0_GPIO, ES_GPIO_ADC_CH0_PIN, &gpio_initstruct);
  66. break;
  67. case 1:
  68. es32f0_channel = ADC_CHANNEL_1;
  69. ald_gpio_init(ES_GPIO_ADC_CH1_GPIO, ES_GPIO_ADC_CH1_PIN, &gpio_initstruct);
  70. break;
  71. case 2:
  72. es32f0_channel = ADC_CHANNEL_2;
  73. ald_gpio_init(ES_GPIO_ADC_CH2_GPIO, ES_GPIO_ADC_CH2_PIN, &gpio_initstruct);
  74. break;
  75. case 3:
  76. es32f0_channel = ADC_CHANNEL_3;
  77. ald_gpio_init(ES_GPIO_ADC_CH3_GPIO, ES_GPIO_ADC_CH3_PIN, &gpio_initstruct);
  78. break;
  79. case 4:
  80. es32f0_channel = ADC_CHANNEL_4;
  81. ald_gpio_init(ES_GPIO_ADC_CH4_GPIO, ES_GPIO_ADC_CH4_PIN, &gpio_initstruct);
  82. break;
  83. case 5:
  84. es32f0_channel = ADC_CHANNEL_5;
  85. ald_gpio_init(ES_GPIO_ADC_CH5_GPIO, ES_GPIO_ADC_CH5_PIN, &gpio_initstruct);
  86. break;
  87. case 6:
  88. es32f0_channel = ADC_CHANNEL_6;
  89. ald_gpio_init(ES_GPIO_ADC_CH6_GPIO, ES_GPIO_ADC_CH6_PIN, &gpio_initstruct);
  90. break;
  91. case 7:
  92. es32f0_channel = ADC_CHANNEL_7;
  93. ald_gpio_init(ES_GPIO_ADC_CH7_GPIO, ES_GPIO_ADC_CH7_PIN, &gpio_initstruct);
  94. break;
  95. case 8:
  96. es32f0_channel = ADC_CHANNEL_8;
  97. ald_gpio_init(ES_GPIO_ADC_CH8_GPIO, ES_GPIO_ADC_CH8_PIN, &gpio_initstruct);
  98. break;
  99. case 9:
  100. es32f0_channel = ADC_CHANNEL_9;
  101. ald_gpio_init(ES_GPIO_ADC_CH9_GPIO, ES_GPIO_ADC_CH9_PIN, &gpio_initstruct);
  102. break;
  103. case 10:
  104. es32f0_channel = ADC_CHANNEL_10;
  105. ald_gpio_init(ES_GPIO_ADC_CH10_GPIO, ES_GPIO_ADC_CH10_PIN, &gpio_initstruct);
  106. break;
  107. case 11:
  108. es32f0_channel = ADC_CHANNEL_11;
  109. ald_gpio_init(ES_GPIO_ADC_CH11_GPIO, ES_GPIO_ADC_CH11_PIN, &gpio_initstruct);
  110. break;
  111. case 12:
  112. es32f0_channel = ADC_CHANNEL_12;
  113. ald_gpio_init(ES_GPIO_ADC_CH12_GPIO, ES_GPIO_ADC_CH12_PIN, &gpio_initstruct);
  114. break;
  115. case 13:
  116. es32f0_channel = ADC_CHANNEL_13;
  117. ald_gpio_init(ES_GPIO_ADC_CH13_GPIO, ES_GPIO_ADC_CH13_PIN, &gpio_initstruct);
  118. break;
  119. case 14:
  120. es32f0_channel = ADC_CHANNEL_14;
  121. ald_gpio_init(ES_GPIO_ADC_CH14_GPIO, ES_GPIO_ADC_CH14_PIN, &gpio_initstruct);
  122. break;
  123. case 15:
  124. es32f0_channel = ADC_CHANNEL_15;
  125. ald_gpio_init(ES_GPIO_ADC_CH15_GPIO, ES_GPIO_ADC_CH15_PIN, &gpio_initstruct);
  126. break;
  127. default:
  128. break;
  129. }
  130. return es32f0_channel;
  131. }
  132. static rt_err_t es32f0_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  133. {
  134. adc_handle_t *_hadc = (adc_handle_t *)device->parent.user_data;
  135. adc_nch_conf_t nm_config;
  136. RT_ASSERT(device != RT_NULL);
  137. RT_ASSERT(value != RT_NULL);
  138. /* config adc channel */
  139. nm_config.ch = es32f0_adc_get_channel(channel);
  140. nm_config.idx = ADC_NCH_IDX_1;
  141. /*aaabbbccc*/
  142. nm_config.samp = ES_ADC0_NCH_SAMPLETIME;
  143. nm_config.samp = ADC_SAMPLETIME_4;
  144. ald_adc_normal_channel_config(_hadc, &nm_config);
  145. ald_adc_normal_start(_hadc);
  146. if (ald_adc_normal_poll_for_conversion(_hadc, 5000) == OK)
  147. *value = ald_adc_normal_get_value(_hadc);
  148. return RT_EOK;
  149. }
  150. static const struct rt_adc_ops es32f0_adc_ops =
  151. {
  152. es32f0_adc_enabled,
  153. es32f0_get_adc_value,
  154. };
  155. int rt_hw_adc_init(void)
  156. {
  157. int result = RT_EOK;
  158. adc_handle_t _h_adc;
  159. _h_adc.init.scan = DISABLE;
  160. _h_adc.init.cont = DISABLE;
  161. _h_adc.init.disc = ADC_ALL_DISABLE;
  162. _h_adc.init.disc_nr = ADC_DISC_NR_1;
  163. _h_adc.init.nche_sel = ADC_NCHESEL_MODE_ALL;
  164. _h_adc.init.n_ref = ADC_NEG_REF_VSS;
  165. _h_adc.init.p_ref = ADC_POS_REF_VDD;
  166. _h_adc.init.nch_nr = ADC_NCH_NR_16;
  167. #ifdef BSP_USING_ADC0
  168. static adc_handle_t _h_adc0;
  169. _h_adc0.init = _h_adc.init;
  170. _h_adc0.perh = ADC0;
  171. _h_adc0.init.align = ES_ADC0_ALIGN;
  172. _h_adc0.init.data_bit = ES_ADC0_DATA_BIT;
  173. _h_adc0.init.div = ES_ADC0_CLK_DIV;
  174. ald_adc_init(&_h_adc0);
  175. result = rt_hw_adc_register(&_device_adc0, ES_DEVICE_NAME_ADC0, &es32f0_adc_ops, &_h_adc0);
  176. #endif /*BSP_USING_ADC0*/
  177. return result;
  178. }
  179. INIT_BOARD_EXPORT(rt_hw_adc_init);
  180. #endif