drv_adc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file drv_adc.c
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "board.h"
  35. #include "drv_adc.h"
  36. #ifdef RT_USING_ADC
  37. #if defined(BSP_USING_ADC1) || defined(BSP_USING_ADC2) || defined(BSP_USING_ADC3) || defined(BSP_USING_ADC4)
  38. /* this driver can be disabled at menuconfig -> Hardware Drivers Config -> On-chip Peripheral Drivers -> Enable ADC */
  39. static struct n32_adc_config adc_config[] =
  40. {
  41. #ifdef BSP_USING_ADC1
  42. {
  43. "adc1",
  44. ADC1,
  45. },
  46. #endif
  47. #ifdef BSP_USING_ADC2
  48. {
  49. "adc2",
  50. ADC2,
  51. },
  52. #endif
  53. #ifdef BSP_USING_ADC3
  54. {
  55. "adc3",
  56. ADC3,
  57. },
  58. #endif
  59. #ifdef BSP_USING_ADC4
  60. {
  61. "adc4",
  62. ADC4,
  63. },
  64. #endif
  65. };
  66. static struct n32_adc adc_obj[sizeof(adc_config) / sizeof(adc_config[0])] = {0};
  67. static void n32_adc_init(struct n32_adc_config *config)
  68. {
  69. ADC_InitType ADC_InitStructure;
  70. /* ADC configuration ------------------------------------------------------*/
  71. ADC_InitStructure.WorkMode = ADC_WORKMODE_INDEPENDENT;
  72. ADC_InitStructure.MultiChEn = DISABLE;
  73. ADC_InitStructure.ContinueConvEn = DISABLE;
  74. ADC_InitStructure.ExtTrigSelect = ADC_EXT_TRIGCONV_NONE;
  75. ADC_InitStructure.DatAlign = ADC_DAT_ALIGN_R;
  76. ADC_InitStructure.ChsNumber = 1;
  77. ADC_Init((ADC_Module*)config->adc_periph, &ADC_InitStructure);
  78. /* Enable ADC */
  79. ADC_Enable((ADC_Module*)config->adc_periph, ENABLE);
  80. /* Check ADC Ready */
  81. while(ADC_GetFlagStatusNew((ADC_Module*)config->adc_periph, ADC_FLAG_RDY) == RESET)
  82. ;
  83. /* Start ADC calibration */
  84. ADC_StartCalibration((ADC_Module*)config->adc_periph);
  85. /* Check the end of ADC calibration */
  86. while (ADC_GetCalibrationStatus((ADC_Module*)config->adc_periph))
  87. ;
  88. }
  89. static rt_err_t n32_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  90. {
  91. if (channel > ADC_CH_18)
  92. {
  93. return RT_EINVAL;
  94. }
  95. return RT_EOK;
  96. }
  97. static rt_err_t n32_adc_convert(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  98. {
  99. struct n32_adc_config *config;
  100. RT_ASSERT(device != RT_NULL);
  101. if (channel > ADC_CH_18)
  102. {
  103. return RT_EINVAL;
  104. }
  105. config = (struct n32_adc_config *)(device->parent.user_data);
  106. ADC_ConfigRegularChannel((ADC_Module*)config->adc_periph, channel, 1, ADC_SAMP_TIME_239CYCLES5);
  107. /* Start ADC Software Conversion */
  108. ADC_EnableSoftwareStartConv((ADC_Module*)config->adc_periph, ENABLE);
  109. while(ADC_GetFlagStatus((ADC_Module*)config->adc_periph, ADC_FLAG_ENDC)==0)
  110. {
  111. }
  112. ADC_ClearFlag((ADC_Module*)config->adc_periph, ADC_FLAG_ENDC);
  113. ADC_ClearFlag((ADC_Module*)config->adc_periph, ADC_FLAG_STR);
  114. *value=ADC_GetDat((ADC_Module*)config->adc_periph);
  115. return RT_EOK;
  116. }
  117. static struct rt_adc_ops n32_adc_ops =
  118. {
  119. .enabled = n32_adc_enabled,
  120. .convert = n32_adc_convert,
  121. };
  122. int rt_hw_adc_init(void)
  123. {
  124. int i = 0;
  125. int result = RT_EOK;
  126. #if defined(BSP_USING_ADC1)
  127. RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC1, ENABLE);
  128. /* Configure PC.00 PC.01 as analog input -------------------------*/
  129. GPIOInit(GPIOC, GPIO_Mode_AIN, GPIO_Speed_50MHz, GPIO_PIN_0 | GPIO_PIN_1);
  130. #endif /* BSP_USING_ADC1 */
  131. #if defined(BSP_USING_ADC2)
  132. RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC2, ENABLE);
  133. /* Configure PC.02 PC.03 as analog input -------------------------*/
  134. GPIOInit(GPIOC, GPIO_Mode_AIN, GPIO_Speed_50MHz, GPIO_PIN_2 | GPIO_PIN_3);
  135. #endif /* BSP_USING_ADC2 */
  136. #if defined(BSP_USING_ADC3)
  137. RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC3, ENABLE);
  138. /* Configure PD.10 PD.11 as analog input -------------------------*/
  139. GPIOInit(GPIOD, GPIO_Mode_AIN, GPIO_Speed_50MHz, GPIO_PIN_10 | GPIO_PIN_11);
  140. #endif /* BSP_USING_ADC3 */
  141. #if defined(BSP_USING_ADC4)
  142. RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC4, ENABLE);
  143. /* Configure PD.12 PD.13 as analog input -------------------------*/
  144. GPIOInit(GPIOD, GPIO_Mode_AIN, GPIO_Speed_50MHz, GPIO_PIN_12 | GPIO_PIN_13);
  145. #endif /* BSP_USING_ADC4 */
  146. /* RCC_ADCHCLK_DIV16*/
  147. ADC_ConfigClk(ADC_CTRL3_CKMOD_AHB, RCC_ADCHCLK_DIV16);
  148. for (i = 0; i < sizeof(adc_obj) / sizeof(adc_obj[0]); i++)
  149. {
  150. adc_obj[i].config = &adc_config[i];
  151. n32_adc_init(&adc_config[i]);
  152. rt_hw_adc_register(&adc_obj[i].adc_device, \
  153. adc_obj[i].config->name, &n32_adc_ops, adc_obj[i].config);
  154. }
  155. return result;
  156. }
  157. INIT_DEVICE_EXPORT(rt_hw_adc_init);
  158. #endif /* defined(BSP_USING_ADC1) || defined(BSP_USING_ADC2) || defined(BSP_USING_ADC3) || defined(BSP_USING_ADC4) */
  159. #endif /* RT_USING_ADC */