analogin_device.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2016, STMicroelectronics
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "mbed_assert.h"
  29. #include "analogin_api.h"
  30. #if DEVICE_ANALOGIN
  31. #include "mbed_wait_api.h"
  32. #include "cmsis.h"
  33. #include "pinmap.h"
  34. #include "mbed_error.h"
  35. #include "PeripheralPins.h"
  36. void analogin_init(analogin_t *obj, PinName pin)
  37. {
  38. uint32_t function = (uint32_t)NC;
  39. // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
  40. // are described in PinNames.h and PeripheralPins.c
  41. // Pin value must be between 0xF0 and 0xFF
  42. if ((pin < 0xF0) || (pin >= 0x100)) {
  43. // Normal channels
  44. // Get the peripheral name from the pin and assign it to the object
  45. obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC);
  46. // Get the functions (adc channel) from the pin and assign it to the object
  47. function = pinmap_function(pin, PinMap_ADC);
  48. // Configure GPIO
  49. pinmap_pinout(pin, PinMap_ADC);
  50. } else {
  51. // Internal channels
  52. obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC_Internal);
  53. function = pinmap_function(pin, PinMap_ADC_Internal);
  54. // No GPIO configuration for internal channels
  55. }
  56. MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
  57. MBED_ASSERT(function != (uint32_t)NC);
  58. obj->channel = STM_PIN_CHANNEL(function);
  59. // Save pin number for the read function
  60. obj->pin = pin;
  61. // Configure ADC object structures
  62. obj->handle.State = HAL_ADC_STATE_RESET;
  63. obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2; // Asynchronous clock mode, input ADC clock
  64. obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
  65. obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  66. obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
  67. obj->handle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example).
  68. obj->handle.Init.LowPowerAutoWait = DISABLE;
  69. obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
  70. obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
  71. obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
  72. obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
  73. obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Software start to trig the 1st conversion manually, without external event
  74. obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  75. obj->handle.Init.DMAContinuousRequests = DISABLE;
  76. obj->handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; // DR register is overwritten with the last conversion result in case of overrun
  77. obj->handle.Init.OversamplingMode = DISABLE; // No oversampling
  78. // Enable ADC clock
  79. __HAL_RCC_ADC_CLK_ENABLE();
  80. __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);
  81. if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
  82. error("Cannot initialize ADC");
  83. }
  84. // ADC calibration is done only once
  85. if (!HAL_ADCEx_Calibration_GetValue(&obj->handle, ADC_SINGLE_ENDED)) {
  86. HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED);
  87. }
  88. }
  89. uint16_t adc_read(analogin_t *obj)
  90. {
  91. ADC_ChannelConfTypeDef sConfig = {0};
  92. // Configure ADC channel
  93. sConfig.Rank = ADC_REGULAR_RANK_1;
  94. sConfig.SamplingTime = ADC_SAMPLETIME_47CYCLES_5; // default value (1.5 us for 80MHz clock)
  95. sConfig.SingleDiff = ADC_SINGLE_ENDED;
  96. sConfig.OffsetNumber = ADC_OFFSET_NONE;
  97. sConfig.Offset = 0;
  98. switch (obj->channel) {
  99. case 0:
  100. sConfig.Channel = ADC_CHANNEL_VREFINT;
  101. sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5; // Minimum ADC sampling time when reading the internal reference voltage is 4us
  102. break;
  103. case 1:
  104. sConfig.Channel = ADC_CHANNEL_1;
  105. break;
  106. case 2:
  107. sConfig.Channel = ADC_CHANNEL_2;
  108. break;
  109. case 3:
  110. sConfig.Channel = ADC_CHANNEL_3;
  111. break;
  112. case 4:
  113. sConfig.Channel = ADC_CHANNEL_4;
  114. break;
  115. case 5:
  116. sConfig.Channel = ADC_CHANNEL_5;
  117. break;
  118. case 6:
  119. sConfig.Channel = ADC_CHANNEL_6;
  120. break;
  121. case 7:
  122. sConfig.Channel = ADC_CHANNEL_7;
  123. break;
  124. case 8:
  125. sConfig.Channel = ADC_CHANNEL_8;
  126. break;
  127. case 9:
  128. sConfig.Channel = ADC_CHANNEL_9;
  129. break;
  130. case 10:
  131. sConfig.Channel = ADC_CHANNEL_10;
  132. break;
  133. case 11:
  134. sConfig.Channel = ADC_CHANNEL_11;
  135. break;
  136. case 12:
  137. sConfig.Channel = ADC_CHANNEL_12;
  138. break;
  139. case 13:
  140. sConfig.Channel = ADC_CHANNEL_13;
  141. break;
  142. case 14:
  143. sConfig.Channel = ADC_CHANNEL_14;
  144. break;
  145. case 15:
  146. sConfig.Channel = ADC_CHANNEL_15;
  147. break;
  148. case 16:
  149. sConfig.Channel = ADC_CHANNEL_16;
  150. break;
  151. case 17:
  152. sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
  153. sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5; // Minimum ADC sampling time when reading the temperature is 5us
  154. break;
  155. case 18:
  156. sConfig.Channel = ADC_CHANNEL_VBAT;
  157. sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5; // Minimum ADC sampling time when reading the VBAT is 12us
  158. break;
  159. default:
  160. return 0;
  161. }
  162. HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
  163. HAL_ADC_Start(&obj->handle); // Start conversion
  164. // Wait end of conversion and get value
  165. if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
  166. return (uint16_t)HAL_ADC_GetValue(&obj->handle);
  167. } else {
  168. return 0;
  169. }
  170. }
  171. #endif