PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/configs/stm3220g-eval/src/stm32_adc.c

https://bitbucket.org/hg42/nuttx
C | 163 lines | 63 code | 34 blank | 66 comment | 7 complexity | f116a1fa47094052089deb66163ce2e9 MD5 | raw file
Possible License(s): 0BSD
  1. /************************************************************************************
  2. * configs/stm3220g-eval/src/stm32_adc.c
  3. *
  4. * Copyright (C) 2012, 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ************************************************************************************/
  35. /************************************************************************************
  36. * Included Files
  37. ************************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <errno.h>
  40. #include <debug.h>
  41. #include <nuttx/board.h>
  42. #include <nuttx/analog/adc.h>
  43. #include <arch/board/board.h>
  44. #include "chip.h"
  45. #include "up_arch.h"
  46. #include "stm32_pwm.h"
  47. #include "stm3220g-eval.h"
  48. #ifdef CONFIG_ADC
  49. /************************************************************************************
  50. * Pre-processor Definitions
  51. ************************************************************************************/
  52. /* Configuration ************************************************************/
  53. /* Up to 3 ADC interfaces are supported */
  54. #if STM32_NADC < 3
  55. # undef CONFIG_STM32_ADC3
  56. #endif
  57. #if STM32_NADC < 2
  58. # undef CONFIG_STM32_ADC2
  59. #endif
  60. #if STM32_NADC < 1
  61. # undef CONFIG_STM32_ADC1
  62. #endif
  63. #if defined(CONFIG_STM32_ADC1) || defined(CONFIG_STM32_ADC2) || defined(CONFIG_STM32_ADC3)
  64. #ifndef CONFIG_STM32_ADC3
  65. # warning "Channel information only available for ADC3"
  66. #endif
  67. /* The number of ADC channels in the conversion list */
  68. #define ADC3_NCHANNELS 1
  69. /************************************************************************************
  70. * Private Data
  71. ************************************************************************************/
  72. /* The STM3220G-EVAL has a 10 Kohm potentiometer RV1 connected to PF9 of
  73. * STM32F207IGH6 on the board: TIM14_CH1/FSMC_CD/ADC3_IN7
  74. */
  75. /* Identifying number of each ADC channel: Variable Resistor. */
  76. #ifdef CONFIG_STM32_ADC3
  77. static const uint8_t g_chanlist[ADC3_NCHANNELS] = {7};
  78. /* Configurations of pins used byte each ADC channels */
  79. static const uint32_t g_pinlist[ADC3_NCHANNELS] = {GPIO_ADC3_IN7};
  80. #endif
  81. /************************************************************************************
  82. * Public Functions
  83. ************************************************************************************/
  84. /************************************************************************************
  85. * Name: stm32_adc_setup
  86. *
  87. * Description:
  88. * Initialize ADC and register the ADC driver.
  89. *
  90. ************************************************************************************/
  91. int stm32_adc_setup(void)
  92. {
  93. #ifdef CONFIG_STM32_ADC3
  94. static bool initialized = false;
  95. struct adc_dev_s *adc;
  96. int ret;
  97. int i;
  98. /* Check if we have already initialized */
  99. if (!initialized)
  100. {
  101. /* Configure the pins as analog inputs for the selected channels */
  102. for (i = 0; i < ADC3_NCHANNELS; i++)
  103. {
  104. stm32_configgpio(g_pinlist[i]);
  105. }
  106. /* Call stm32_adcinitialize() to get an instance of the ADC interface */
  107. adc = stm32_adcinitialize(3, g_chanlist, ADC3_NCHANNELS);
  108. if (adc == NULL)
  109. {
  110. aerr("ERROR: Failed to get ADC interface\n");
  111. return -ENODEV;
  112. }
  113. /* Register the ADC driver at "/dev/adc0" */
  114. ret = adc_register("/dev/adc0", adc);
  115. if (ret < 0)
  116. {
  117. aerr("ERROR: adc_register failed: %d\n", ret);
  118. return ret;
  119. }
  120. /* Now we are initialized */
  121. initialized = true;
  122. }
  123. return OK;
  124. #else
  125. return -ENOSYS;
  126. #endif
  127. }
  128. #endif /* CONFIG_STM32_ADC1 || CONFIG_STM32_ADC2 || CONFIG_STM32_ADC3 */
  129. #endif /* CONFIG_ADC */