PageRenderTime 29ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/boards/arm/stm32h7/stm32h747i-disco/src/stm32_adc.c

https://bitbucket.org/zapparello/nuttx
C | 227 lines | 103 code | 41 blank | 83 comment | 17 complexity | dd15a2f78462cab1ced220c3abcb4409 MD5 | raw file
Possible License(s): Apache-2.0
  1. /****************************************************************************
  2. * boards/arm/stm32h7/stm32h747i-disco/src/stm32_adc.c
  3. *
  4. * Copyright (C) 2019 Gregory Nutt. All rights reserved.
  5. * Authors: Gregory Nutt <gnutt@nuttx.org>
  6. * David Sidrane <david.sidrane@nscdg.com>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <errno.h>
  41. #include <debug.h>
  42. #include <nuttx/board.h>
  43. #include <nuttx/analog/adc.h>
  44. #include <arch/board/board.h>
  45. #include "chip.h"
  46. #include "stm32_gpio.h"
  47. #include "stm32_adc.h"
  48. #include "stm32h747i-disco.h"
  49. #ifdef CONFIG_ADC
  50. /****************************************************************************
  51. * Pre-processor Definitions
  52. ****************************************************************************/
  53. /* Configuration ************************************************************/
  54. /* Up to 3 ADC interfaces are supported */
  55. #if defined(CONFIG_STM32H7_ADC1) || defined(CONFIG_STM32H7_ADC2) || \
  56. defined(CONFIG_STM32H7_ADC3)
  57. #ifndef CONFIG_STM32H7_ADC1
  58. # warning "Channel information only available for ADC1"
  59. #endif
  60. /* The number of ADC channels in the conversion list */
  61. #define ADC1_NCHANNELS 5
  62. #define ADC3_NCHANNELS 1
  63. /****************************************************************************
  64. * Private Data
  65. ****************************************************************************/
  66. #ifdef CONFIG_STM32H7_ADC1
  67. /* Identifying number of each ADC channel: Variable Resistor.
  68. *
  69. * ADC1: {5, 10, 12, 13, 15};
  70. */
  71. static const uint8_t g_adc1_chanlist[ADC1_NCHANNELS] =
  72. {
  73. 5, 10, 12, 13, 15
  74. };
  75. /* Configurations of pins used by each ADC channels
  76. *
  77. * ADC1: {GPIO_ADC12_INP5, GPIO_ADC123_INP10, GPIO_ADC123_INP12,
  78. * GPIO_ADC12_INP13, GPIO_ADC12_INP15};
  79. */
  80. static const uint32_t g_adc1_pinlist[ADC1_NCHANNELS] =
  81. {
  82. GPIO_ADC12_INP5, GPIO_ADC123_INP10, GPIO_ADC123_INP12, GPIO_ADC12_INP13,
  83. GPIO_ADC12_INP15
  84. };
  85. #endif
  86. #ifdef CONFIG_STM32H7_ADC3
  87. /* Identifying number of each ADC channel: Variable Resistor.
  88. *
  89. * ADC3: {6,};
  90. */
  91. static const uint8_t g_adc3_chanlist[ADC1_NCHANNELS] =
  92. {
  93. 6
  94. };
  95. /* Configurations of pins used by each ADC channels
  96. *
  97. *
  98. * ADC3: {GPIO_ADC3_INP6}
  99. */
  100. static const uint32_t g_adc3_pinlist[ADC3_NCHANNELS] =
  101. {
  102. GPIO_ADC3_INP6
  103. };
  104. #endif
  105. /****************************************************************************
  106. * Public Functions
  107. ****************************************************************************/
  108. /****************************************************************************
  109. * Name: stm32_adc_setup
  110. *
  111. * Description:
  112. * Initialize ADC and register the ADC driver.
  113. *
  114. ****************************************************************************/
  115. int stm32_adc_setup(void)
  116. {
  117. #if defined(CONFIG_STM32H7_ADC1) || defined(CONFIG_STM32H7_ADC3)
  118. static bool initialized = false;
  119. struct adc_dev_s *adc;
  120. int ret;
  121. int i;
  122. char devname[] = "/dev/adc0";
  123. /* Check if we have already initialized */
  124. if (!initialized)
  125. {
  126. #endif
  127. #if defined(CONFIG_STM32H7_ADC1)
  128. /* Configure the pins as analog inputs for the selected channels */
  129. for (i = 0; i < ADC1_NCHANNELS; i++)
  130. {
  131. if (g_adc1_pinlist[i] != 0)
  132. {
  133. stm32_configgpio(g_adc1_pinlist[i]);
  134. }
  135. }
  136. /* Call stm32_adcinitialize() to get an instance of the ADC interface */
  137. adc = stm32h7_adc_initialize(1, g_adc1_chanlist, ADC1_NCHANNELS);
  138. if (adc == NULL)
  139. {
  140. aerr("ERROR: Failed to get ADC1 interface\n");
  141. return -ENODEV;
  142. }
  143. /* Register the ADC driver at "/dev/adc0" */
  144. ret = adc_register(devname, adc);
  145. if (ret < 0)
  146. {
  147. aerr("ERROR: adc_register(%s) failed: %d\n", devname, ret);
  148. return ret;
  149. }
  150. devname[8]++;
  151. #endif
  152. #if defined(CONFIG_STM32H7_ADC3)
  153. /* Configure the pins as analog inputs for the selected channels */
  154. for (i = 0; i < ADC3_NCHANNELS; i++)
  155. {
  156. if (g_adc3_pinlist[i] != 0)
  157. {
  158. stm32_configgpio(g_adc3_pinlist[i]);
  159. }
  160. }
  161. /* Call stm32_adcinitialize() to get an instance of the ADC interface */
  162. adc = stm32h7_adc_initialize(3, g_adc3_chanlist, ADC3_NCHANNELS);
  163. if (adc == NULL)
  164. {
  165. aerr("ERROR: Failed to get ADC3 interface\n");
  166. return -ENODEV;
  167. }
  168. /* Register the ADC driver at "/dev/adc0 or 1" */
  169. ret = adc_register(devname, adc);
  170. if (ret < 0)
  171. {
  172. aerr("ERROR: adc_register(%s) failed: %d\n", devname, ret);
  173. return ret;
  174. }
  175. #endif
  176. #if defined(CONFIG_STM32H7_ADC1) || defined(CONFIG_STM32H7_ADC3)
  177. /* Now we are initialized */
  178. initialized = true;
  179. }
  180. return OK;
  181. #else
  182. return -ENOSYS;
  183. #endif
  184. }
  185. #endif /* CONFIG_STM32H7_ADC1 || CONFIG_STM32H7_ADC2 || CONFIG_STM32H7_ADC3 */
  186. #endif /* CONFIG_ADC */