PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/nuttx/configs/stm3240g-eval/src/up_adc.c

https://gitlab.com/BlueCabbage/NuttX
C | 168 lines | 62 code | 35 blank | 71 comment | 7 complexity | 0aca69db3d180c7cfae5a3b040cd36e0 MD5 | raw file
  1. /************************************************************************************
  2. * configs/stm3240g-eval/src/up_adc.c
  3. * arch/arm/src/board/up_adc.c
  4. *
  5. * Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  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/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 "stm3240g-internal.h"
  48. #ifdef CONFIG_ADC
  49. /************************************************************************************
  50. * 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 STM3240G-EVAL has a 10 Kohm potentiometer RV1 connected to PF9 of
  73. * STM32F407IGH6 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. * Private Functions
  83. ************************************************************************************/
  84. /************************************************************************************
  85. * Public Functions
  86. ************************************************************************************/
  87. /************************************************************************************
  88. * Name: adc_devinit
  89. *
  90. * Description:
  91. * All STM32 architectures must provide the following interface to work with
  92. * examples/adc.
  93. *
  94. ************************************************************************************/
  95. int adc_devinit(void)
  96. {
  97. #ifdef CONFIG_STM32_ADC3
  98. static bool initialized = false;
  99. struct adc_dev_s *adc;
  100. int ret;
  101. int i;
  102. /* Check if we have already initialized */
  103. if (!initialized)
  104. {
  105. /* Configure the pins as analog inputs for the selected channels */
  106. for (i = 0; i < ADC3_NCHANNELS; i++)
  107. {
  108. stm32_configgpio(g_pinlist[i]);
  109. }
  110. /* Call stm32_adcinitialize() to get an instance of the ADC interface */
  111. adc = stm32_adcinitialize(3, g_chanlist, ADC3_NCHANNELS);
  112. if (adc == NULL)
  113. {
  114. adbg("ERROR: Failed to get ADC interface\n");
  115. return -ENODEV;
  116. }
  117. /* Register the ADC driver at "/dev/adc0" */
  118. ret = adc_register("/dev/adc0", adc);
  119. if (ret < 0)
  120. {
  121. adbg("adc_register failed: %d\n", ret);
  122. return ret;
  123. }
  124. /* Now we are initialized */
  125. initialized = true;
  126. }
  127. return OK;
  128. #else
  129. return -ENOSYS;
  130. #endif
  131. }
  132. #endif /* CONFIG_STM32_ADC1 || CONFIG_STM32_ADC2 || CONFIG_STM32_ADC3 */
  133. #endif /* CONFIG_ADC */