PageRenderTime 25ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/doc/STM32F10x_StdPeriph_Examples/ADC/3ADCs_DMA/main.c

https://bitbucket.org/philpem/stm32_spl
C | 280 lines | 122 code | 41 blank | 117 comment | 2 complexity | c8a7a76b329954b558e70597a9fddf8b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /**
  2. ******************************************************************************
  3. * @file ADC/3ADCs_DMA/main.c
  4. * @author MCD Application Team
  5. * @version V3.5.0
  6. * @date 08-April-2011
  7. * @brief Main program body
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  19. ******************************************************************************
  20. */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "stm32f10x.h"
  23. /** @addtogroup STM32F10x_StdPeriph_Examples
  24. * @{
  25. */
  26. /** @addtogroup ADC_3ADCs_DMA
  27. * @{
  28. */
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* Private define ------------------------------------------------------------*/
  31. #define ADC1_DR_Address ((uint32_t)0x4001244C)
  32. #define ADC3_DR_Address ((uint32_t)0x40013C4C)
  33. /* Private macro -------------------------------------------------------------*/
  34. /* Private variables ---------------------------------------------------------*/
  35. ADC_InitTypeDef ADC_InitStructure;
  36. DMA_InitTypeDef DMA_InitStructure;
  37. __IO uint16_t ADC1ConvertedValue = 0, ADC3ConvertedValue = 0;
  38. /* Private function prototypes -----------------------------------------------*/
  39. void RCC_Configuration(void);
  40. void GPIO_Configuration(void);
  41. void NVIC_Configuration(void);
  42. /* Private functions ---------------------------------------------------------*/
  43. /**
  44. * @brief Main program
  45. * @param None
  46. * @retval None
  47. */
  48. int main(void)
  49. {
  50. /*!< At this stage the microcontroller clock setting is already configured,
  51. this is done through SystemInit() function which is called from startup
  52. file (startup_stm32f10x_xx.s) before to branch to application main.
  53. To reconfigure the default setting of SystemInit() function, refer to
  54. system_stm32f10x.c file
  55. */
  56. /* System clocks configuration ---------------------------------------------*/
  57. RCC_Configuration();
  58. /* NVIC configuration ------------------------------------------------------*/
  59. NVIC_Configuration();
  60. /* GPIO configuration ------------------------------------------------------*/
  61. GPIO_Configuration();
  62. /* DMA1 channel1 configuration ----------------------------------------------*/
  63. DMA_DeInit(DMA1_Channel1);
  64. DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
  65. DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC1ConvertedValue;
  66. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  67. DMA_InitStructure.DMA_BufferSize = 1;
  68. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  69. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
  70. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  71. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  72. DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  73. DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  74. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  75. DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  76. /* Enable DMA1 channel1 */
  77. DMA_Cmd(DMA1_Channel1, ENABLE);
  78. /* DMA2 channel5 configuration ----------------------------------------------*/
  79. DMA_DeInit(DMA2_Channel5);
  80. DMA_InitStructure.DMA_PeripheralBaseAddr = ADC3_DR_Address;
  81. DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC3ConvertedValue;
  82. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  83. DMA_InitStructure.DMA_BufferSize = 1;
  84. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  85. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
  86. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  87. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  88. DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  89. DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  90. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  91. DMA_Init(DMA2_Channel5, &DMA_InitStructure);
  92. /* Enable DMA2 channel5 */
  93. DMA_Cmd(DMA2_Channel5, ENABLE);
  94. /* ADC1 configuration ------------------------------------------------------*/
  95. ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  96. ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  97. ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  98. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  99. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  100. ADC_InitStructure.ADC_NbrOfChannel = 1;
  101. ADC_Init(ADC1, &ADC_InitStructure);
  102. /* ADC1 regular channels configuration */
  103. ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_28Cycles5);
  104. /* Enable ADC1 DMA */
  105. ADC_DMACmd(ADC1, ENABLE);
  106. /* ADC2 configuration ------------------------------------------------------*/
  107. ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  108. ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  109. ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  110. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  111. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  112. ADC_InitStructure.ADC_NbrOfChannel = 1;
  113. ADC_Init(ADC2, &ADC_InitStructure);
  114. /* ADC2 regular channels configuration */
  115. ADC_RegularChannelConfig(ADC2, ADC_Channel_13, 1, ADC_SampleTime_28Cycles5);
  116. /* Enable ADC2 EOC interrupt */
  117. ADC_ITConfig(ADC2, ADC_IT_EOC, ENABLE);
  118. /* ADC3 configuration ------------------------------------------------------*/
  119. ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  120. ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  121. ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  122. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  123. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  124. ADC_InitStructure.ADC_NbrOfChannel = 1;
  125. ADC_Init(ADC3, &ADC_InitStructure);
  126. /* ADC3 regular channel14 configuration */
  127. ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 1, ADC_SampleTime_28Cycles5);
  128. /* Enable ADC3 DMA */
  129. ADC_DMACmd(ADC3, ENABLE);
  130. /* Enable ADC1 */
  131. ADC_Cmd(ADC1, ENABLE);
  132. /* Enable ADC1 reset calibration register */
  133. ADC_ResetCalibration(ADC1);
  134. /* Check the end of ADC1 reset calibration register */
  135. while(ADC_GetResetCalibrationStatus(ADC1));
  136. /* Start ADC1 calibration */
  137. ADC_StartCalibration(ADC1);
  138. /* Check the end of ADC1 calibration */
  139. while(ADC_GetCalibrationStatus(ADC1));
  140. /* Enable ADC2 */
  141. ADC_Cmd(ADC2, ENABLE);
  142. /* Enable ADC2 reset calibration register */
  143. ADC_ResetCalibration(ADC2);
  144. /* Check the end of ADC2 reset calibration register */
  145. while(ADC_GetResetCalibrationStatus(ADC2));
  146. /* Start ADC2 calibration */
  147. ADC_StartCalibration(ADC2);
  148. /* Check the end of ADC2 calibration */
  149. while(ADC_GetCalibrationStatus(ADC2));
  150. /* Enable ADC3 */
  151. ADC_Cmd(ADC3, ENABLE);
  152. /* Enable ADC3 reset calibration register */
  153. ADC_ResetCalibration(ADC3);
  154. /* Check the end of ADC3 reset calibration register */
  155. while(ADC_GetResetCalibrationStatus(ADC3));
  156. /* Start ADC3 calibration */
  157. ADC_StartCalibration(ADC3);
  158. /* Check the end of ADC3 calibration */
  159. while(ADC_GetCalibrationStatus(ADC3));
  160. /* Start ADC1 Software Conversion */
  161. ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  162. /* Start ADC2 Software Conversion */
  163. ADC_SoftwareStartConvCmd(ADC2, ENABLE);
  164. /* Start ADC3 Software Conversion */
  165. ADC_SoftwareStartConvCmd(ADC3, ENABLE);
  166. while (1)
  167. {
  168. }
  169. }
  170. /**
  171. * @brief Configures the different system clocks.
  172. * @param None
  173. * @retval None
  174. */
  175. void RCC_Configuration(void)
  176. {
  177. /* ADCCLK = PCLK2/4 */
  178. RCC_ADCCLKConfig(RCC_PCLK2_Div4);
  179. /* Enable peripheral clocks ------------------------------------------------*/
  180. /* Enable DMA1 and DMA2 clocks */
  181. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 | RCC_AHBPeriph_DMA2, ENABLE);
  182. /* Enable ADC1, ADC2, ADC3 and GPIOC clocks */
  183. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 |
  184. RCC_APB2Periph_ADC3 | RCC_APB2Periph_GPIOC, ENABLE);
  185. }
  186. /**
  187. * @brief Configures the different GPIO ports.
  188. * @param None
  189. * @retval None
  190. */
  191. void GPIO_Configuration(void)
  192. {
  193. GPIO_InitTypeDef GPIO_InitStructure;
  194. /* Configure PC.02, PC.03 and PC.04 (ADC Channel12, ADC Channel13 and
  195. ADC Channel14) as analog inputs */
  196. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
  197. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  198. GPIO_Init(GPIOC, &GPIO_InitStructure);
  199. }
  200. /**
  201. * @brief Configures Vector Table base location.
  202. * @param None
  203. * @retval None
  204. */
  205. void NVIC_Configuration(void)
  206. {
  207. NVIC_InitTypeDef NVIC_InitStructure;
  208. /* Configure and enable ADC interrupt */
  209. NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
  210. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  211. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  212. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  213. NVIC_Init(&NVIC_InitStructure);
  214. }
  215. #ifdef USE_FULL_ASSERT
  216. /**
  217. * @brief Reports the name of the source file and the source line number
  218. * where the assert_param error has occurred.
  219. * @param file: pointer to the source file name
  220. * @param line: assert_param error line source number
  221. * @retval None
  222. */
  223. void assert_failed(uint8_t* file, uint32_t line)
  224. {
  225. /* User can add his own implementation to report the file name and line number,
  226. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  227. /* Infinite loop */
  228. while (1)
  229. {
  230. }
  231. }
  232. #endif
  233. /**
  234. * @}
  235. */
  236. /**
  237. * @}
  238. */
  239. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/