adc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. ******************************************************************************
  3. * File Name : ADC.c
  4. * Description : This file provides code for the configuration
  5. * of the ADC instances.
  6. ******************************************************************************
  7. * This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * Copyright (c) 2018 STMicroelectronics International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* Includes ------------------------------------------------------------------*/
  50. #include "adc.h"
  51. #include "gpio.h"
  52. #include "dma.h"
  53. /* USER CODE BEGIN 0 */
  54. /* USER CODE END 0 */
  55. ADC_HandleTypeDef hadc1;
  56. ADC_HandleTypeDef hadc3;
  57. DMA_HandleTypeDef hdma_adc1;
  58. DMA_HandleTypeDef hdma_adc3;
  59. /* ADC1 init function */
  60. void MX_ADC1_Init(void)
  61. {
  62. ADC_ChannelConfTypeDef sConfig;
  63. /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  64. */
  65. hadc1.Instance = ADC1;
  66. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;
  67. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  68. hadc1.Init.ScanConvMode = ENABLE;
  69. hadc1.Init.ContinuousConvMode = ENABLE;
  70. hadc1.Init.DiscontinuousConvMode = DISABLE;
  71. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  72. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  73. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  74. hadc1.Init.NbrOfConversion = 1;
  75. hadc1.Init.DMAContinuousRequests = ENABLE;
  76. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  77. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  78. {
  79. _Error_Handler(__FILE__, __LINE__);
  80. }
  81. /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  82. */
  83. sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
  84. sConfig.Rank = 1;
  85. sConfig.SamplingTime = ADC_SAMPLETIME_112CYCLES;
  86. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  87. {
  88. _Error_Handler(__FILE__, __LINE__);
  89. }
  90. }
  91. /* ADC3 init function */
  92. void MX_ADC3_Init(void)
  93. {
  94. ADC_ChannelConfTypeDef sConfig;
  95. /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  96. */
  97. hadc3.Instance = ADC3;
  98. hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;
  99. hadc3.Init.Resolution = ADC_RESOLUTION_12B;
  100. hadc3.Init.ScanConvMode = ENABLE;
  101. hadc3.Init.ContinuousConvMode = ENABLE;
  102. hadc3.Init.DiscontinuousConvMode = DISABLE;
  103. hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  104. hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  105. hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  106. hadc3.Init.NbrOfConversion = 3;
  107. hadc3.Init.DMAContinuousRequests = ENABLE;
  108. hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  109. if (HAL_ADC_Init(&hadc3) != HAL_OK)
  110. {
  111. _Error_Handler(__FILE__, __LINE__);
  112. }
  113. /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  114. */
  115. sConfig.Channel = ADC_CHANNEL_5;
  116. sConfig.Rank = 1;
  117. sConfig.SamplingTime = ADC_SAMPLETIME_112CYCLES;
  118. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  119. {
  120. _Error_Handler(__FILE__, __LINE__);
  121. }
  122. /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  123. */
  124. sConfig.Channel = ADC_CHANNEL_6;
  125. sConfig.Rank = 2;
  126. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  127. {
  128. _Error_Handler(__FILE__, __LINE__);
  129. }
  130. /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  131. */
  132. sConfig.Channel = ADC_CHANNEL_3;
  133. sConfig.Rank = 3;
  134. sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
  135. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  136. {
  137. _Error_Handler(__FILE__, __LINE__);
  138. }
  139. }
  140. void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
  141. {
  142. GPIO_InitTypeDef GPIO_InitStruct;
  143. if(adcHandle->Instance==ADC1)
  144. {
  145. /* USER CODE BEGIN ADC1_MspInit 0 */
  146. /* USER CODE END ADC1_MspInit 0 */
  147. /* ADC1 clock enable */
  148. __HAL_RCC_ADC1_CLK_ENABLE();
  149. /**ADC1 GPIO Configuration
  150. PA3 ------> ADC1_IN3
  151. */
  152. GPIO_InitStruct.Pin = PT1000_Pin;
  153. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  154. GPIO_InitStruct.Pull = GPIO_NOPULL;
  155. HAL_GPIO_Init(PT1000_GPIO_Port, &GPIO_InitStruct);
  156. /* ADC1 DMA Init */
  157. /* ADC1 Init */
  158. hdma_adc1.Instance = DMA2_Stream4;
  159. hdma_adc1.Init.Channel = DMA_CHANNEL_0;
  160. hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
  161. hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
  162. hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
  163. hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
  164. hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
  165. hdma_adc1.Init.Mode = DMA_CIRCULAR;
  166. hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
  167. hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  168. if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
  169. {
  170. _Error_Handler(__FILE__, __LINE__);
  171. }
  172. __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
  173. /* ADC1 interrupt Init */
  174. HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
  175. HAL_NVIC_EnableIRQ(ADC_IRQn);
  176. /* USER CODE BEGIN ADC1_MspInit 1 */
  177. /* USER CODE END ADC1_MspInit 1 */
  178. }
  179. else if(adcHandle->Instance==ADC3)
  180. {
  181. /* USER CODE BEGIN ADC3_MspInit 0 */
  182. /* USER CODE END ADC3_MspInit 0 */
  183. /* ADC3 clock enable */
  184. __HAL_RCC_ADC3_CLK_ENABLE();
  185. /**ADC3 GPIO Configuration
  186. PF7 ------> ADC3_IN5
  187. PF8 ------> ADC3_IN6
  188. PA3 ------> ADC3_IN3
  189. */
  190. GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8;
  191. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  192. GPIO_InitStruct.Pull = GPIO_NOPULL;
  193. HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
  194. GPIO_InitStruct.Pin = PT1000_Pin;
  195. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  196. GPIO_InitStruct.Pull = GPIO_NOPULL;
  197. HAL_GPIO_Init(PT1000_GPIO_Port, &GPIO_InitStruct);
  198. /* ADC3 DMA Init */
  199. /* ADC3 Init */
  200. hdma_adc3.Instance = DMA2_Stream0;
  201. hdma_adc3.Init.Channel = DMA_CHANNEL_2;
  202. hdma_adc3.Init.Direction = DMA_PERIPH_TO_MEMORY;
  203. hdma_adc3.Init.PeriphInc = DMA_PINC_DISABLE;
  204. hdma_adc3.Init.MemInc = DMA_MINC_ENABLE;
  205. hdma_adc3.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
  206. hdma_adc3.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
  207. hdma_adc3.Init.Mode = DMA_CIRCULAR;
  208. hdma_adc3.Init.Priority = DMA_PRIORITY_LOW;
  209. hdma_adc3.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  210. if (HAL_DMA_Init(&hdma_adc3) != HAL_OK)
  211. {
  212. _Error_Handler(__FILE__, __LINE__);
  213. }
  214. __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc3);
  215. /* ADC3 interrupt Init */
  216. HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
  217. HAL_NVIC_EnableIRQ(ADC_IRQn);
  218. /* USER CODE BEGIN ADC3_MspInit 1 */
  219. /* USER CODE END ADC3_MspInit 1 */
  220. }
  221. }
  222. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
  223. {
  224. if(adcHandle->Instance==ADC1)
  225. {
  226. /* USER CODE BEGIN ADC1_MspDeInit 0 */
  227. /* USER CODE END ADC1_MspDeInit 0 */
  228. /* Peripheral clock disable */
  229. __HAL_RCC_ADC1_CLK_DISABLE();
  230. /**ADC1 GPIO Configuration
  231. PA3 ------> ADC1_IN3
  232. */
  233. HAL_GPIO_DeInit(PT1000_GPIO_Port, PT1000_Pin);
  234. /* ADC1 DMA DeInit */
  235. HAL_DMA_DeInit(adcHandle->DMA_Handle);
  236. /* ADC1 interrupt Deinit */
  237. /* USER CODE BEGIN ADC1:ADC_IRQn disable */
  238. /**
  239. * Uncomment the line below to disable the "ADC_IRQn" interrupt
  240. * Be aware, disabling shared interrupt may affect other IPs
  241. */
  242. /* HAL_NVIC_DisableIRQ(ADC_IRQn); */
  243. /* USER CODE END ADC1:ADC_IRQn disable */
  244. /* USER CODE BEGIN ADC1_MspDeInit 1 */
  245. /* USER CODE END ADC1_MspDeInit 1 */
  246. }
  247. else if(adcHandle->Instance==ADC3)
  248. {
  249. /* USER CODE BEGIN ADC3_MspDeInit 0 */
  250. /* USER CODE END ADC3_MspDeInit 0 */
  251. /* Peripheral clock disable */
  252. __HAL_RCC_ADC3_CLK_DISABLE();
  253. /**ADC3 GPIO Configuration
  254. PF7 ------> ADC3_IN5
  255. PF8 ------> ADC3_IN6
  256. PA3 ------> ADC3_IN3
  257. */
  258. HAL_GPIO_DeInit(GPIOF, GPIO_PIN_7|GPIO_PIN_8);
  259. HAL_GPIO_DeInit(PT1000_GPIO_Port, PT1000_Pin);
  260. /* ADC3 DMA DeInit */
  261. HAL_DMA_DeInit(adcHandle->DMA_Handle);
  262. /* ADC3 interrupt Deinit */
  263. /* USER CODE BEGIN ADC3:ADC_IRQn disable */
  264. /**
  265. * Uncomment the line below to disable the "ADC_IRQn" interrupt
  266. * Be aware, disabling shared interrupt may affect other IPs
  267. */
  268. /* HAL_NVIC_DisableIRQ(ADC_IRQn); */
  269. /* USER CODE END ADC3:ADC_IRQn disable */
  270. /* USER CODE BEGIN ADC3_MspDeInit 1 */
  271. /* USER CODE END ADC3_MspDeInit 1 */
  272. }
  273. }
  274. /* USER CODE BEGIN 1 */
  275. /* USER CODE END 1 */
  276. /**
  277. * @}
  278. */
  279. /**
  280. * @}
  281. */
  282. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/