1 | /** |
---|
2 | ****************************************************************************** |
---|
3 | * @file adc.c |
---|
4 | * @brief This file provides code for the configuration |
---|
5 | * of the ADC instances. |
---|
6 | ****************************************************************************** |
---|
7 | * @attention |
---|
8 | * |
---|
9 | * <h2><center>© Copyright (c) 2021 STMicroelectronics. |
---|
10 | * All rights reserved.</center></h2> |
---|
11 | * |
---|
12 | * This software component is licensed by ST under BSD 3-Clause license, |
---|
13 | * the "License"; You may not use this file except in compliance with the |
---|
14 | * License. You may obtain a copy of the License at: |
---|
15 | * opensource.org/licenses/BSD-3-Clause |
---|
16 | * |
---|
17 | ****************************************************************************** |
---|
18 | */ |
---|
19 | |
---|
20 | /* Includes ------------------------------------------------------------------*/ |
---|
21 | #include "adc.h" |
---|
22 | |
---|
23 | /* USER CODE BEGIN 0 */ |
---|
24 | |
---|
25 | /* USER CODE END 0 */ |
---|
26 | |
---|
27 | ADC_HandleTypeDef hadc1; |
---|
28 | DMA_HandleTypeDef hdma_adc1; |
---|
29 | |
---|
30 | /* ADC1 init function */ |
---|
31 | void MX_ADC1_Init(void) |
---|
32 | { |
---|
33 | |
---|
34 | /* USER CODE BEGIN ADC1_Init 0 */ |
---|
35 | |
---|
36 | /* USER CODE END ADC1_Init 0 */ |
---|
37 | |
---|
38 | ADC_ChannelConfTypeDef sConfig = {0}; |
---|
39 | |
---|
40 | /* USER CODE BEGIN ADC1_Init 1 */ |
---|
41 | |
---|
42 | /* USER CODE END ADC1_Init 1 */ |
---|
43 | /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) |
---|
44 | */ |
---|
45 | hadc1.Instance = ADC1; |
---|
46 | hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV8; |
---|
47 | hadc1.Init.Resolution = ADC_RESOLUTION_12B; |
---|
48 | hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
---|
49 | hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE; |
---|
50 | hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV; |
---|
51 | hadc1.Init.LowPowerAutoWait = DISABLE; |
---|
52 | hadc1.Init.LowPowerAutoPowerOff = DISABLE; |
---|
53 | hadc1.Init.ContinuousConvMode = ENABLE; |
---|
54 | hadc1.Init.NbrOfConversion = 4; |
---|
55 | hadc1.Init.DiscontinuousConvMode = DISABLE; |
---|
56 | hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; |
---|
57 | hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; |
---|
58 | hadc1.Init.DMAContinuousRequests = ENABLE; |
---|
59 | hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED; |
---|
60 | hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_160CYCLES_5; |
---|
61 | hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_160CYCLES_5; |
---|
62 | hadc1.Init.OversamplingMode = ENABLE; |
---|
63 | hadc1.Init.Oversampling.Ratio = ADC_OVERSAMPLING_RATIO_16; |
---|
64 | hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_NONE; |
---|
65 | hadc1.Init.Oversampling.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER; |
---|
66 | hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_LOW; |
---|
67 | if (HAL_ADC_Init(&hadc1) != HAL_OK) |
---|
68 | { |
---|
69 | Error_Handler(); |
---|
70 | } |
---|
71 | /** Configure Regular Channel |
---|
72 | */ |
---|
73 | sConfig.Channel = ADC_CHANNEL_5; |
---|
74 | sConfig.Rank = ADC_REGULAR_RANK_1; |
---|
75 | sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1; |
---|
76 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
---|
77 | { |
---|
78 | Error_Handler(); |
---|
79 | } |
---|
80 | /** Configure Regular Channel |
---|
81 | */ |
---|
82 | sConfig.Channel = ADC_CHANNEL_6; |
---|
83 | sConfig.Rank = ADC_REGULAR_RANK_2; |
---|
84 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
---|
85 | { |
---|
86 | Error_Handler(); |
---|
87 | } |
---|
88 | /** Configure Regular Channel |
---|
89 | */ |
---|
90 | sConfig.Channel = ADC_CHANNEL_7; |
---|
91 | sConfig.Rank = ADC_REGULAR_RANK_3; |
---|
92 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
---|
93 | { |
---|
94 | Error_Handler(); |
---|
95 | } |
---|
96 | /** Configure Regular Channel |
---|
97 | */ |
---|
98 | sConfig.Channel = ADC_CHANNEL_8; |
---|
99 | sConfig.Rank = ADC_REGULAR_RANK_4; |
---|
100 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
---|
101 | { |
---|
102 | Error_Handler(); |
---|
103 | } |
---|
104 | /* USER CODE BEGIN ADC1_Init 2 */ |
---|
105 | |
---|
106 | /* USER CODE END ADC1_Init 2 */ |
---|
107 | |
---|
108 | } |
---|
109 | |
---|
110 | void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) |
---|
111 | { |
---|
112 | |
---|
113 | GPIO_InitTypeDef GPIO_InitStruct = {0}; |
---|
114 | if(adcHandle->Instance==ADC1) |
---|
115 | { |
---|
116 | /* USER CODE BEGIN ADC1_MspInit 0 */ |
---|
117 | |
---|
118 | /* USER CODE END ADC1_MspInit 0 */ |
---|
119 | /* ADC1 clock enable */ |
---|
120 | __HAL_RCC_ADC_CLK_ENABLE(); |
---|
121 | |
---|
122 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
---|
123 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
---|
124 | /**ADC1 GPIO Configuration |
---|
125 | PA5 ------> ADC1_IN5 |
---|
126 | PA6 ------> ADC1_IN6 |
---|
127 | PA7 ------> ADC1_IN7 |
---|
128 | PB0 ------> ADC1_IN8 |
---|
129 | */ |
---|
130 | GPIO_InitStruct.Pin = ADC1_IN5_I_SENSE_AVG_Pin|ADC1_IN6_UIN_SENSE_Pin|ADC1_IN7_TEMP_SENSE_Pin; |
---|
131 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
---|
132 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
---|
133 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
---|
134 | |
---|
135 | GPIO_InitStruct.Pin = GPIO_PIN_0; |
---|
136 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
---|
137 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
---|
138 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
---|
139 | |
---|
140 | /* ADC1 DMA Init */ |
---|
141 | /* ADC1 Init */ |
---|
142 | hdma_adc1.Instance = DMA1_Channel1; |
---|
143 | hdma_adc1.Init.Request = DMA_REQUEST_ADC1; |
---|
144 | hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY; |
---|
145 | hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE; |
---|
146 | hdma_adc1.Init.MemInc = DMA_MINC_ENABLE; |
---|
147 | hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; |
---|
148 | hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; |
---|
149 | hdma_adc1.Init.Mode = DMA_CIRCULAR; |
---|
150 | hdma_adc1.Init.Priority = DMA_PRIORITY_LOW; |
---|
151 | if (HAL_DMA_Init(&hdma_adc1) != HAL_OK) |
---|
152 | { |
---|
153 | Error_Handler(); |
---|
154 | } |
---|
155 | |
---|
156 | __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1); |
---|
157 | |
---|
158 | /* ADC1 interrupt Init */ |
---|
159 | HAL_NVIC_SetPriority(ADC1_COMP_IRQn, 0, 0); |
---|
160 | HAL_NVIC_EnableIRQ(ADC1_COMP_IRQn); |
---|
161 | /* USER CODE BEGIN ADC1_MspInit 1 */ |
---|
162 | |
---|
163 | /* USER CODE END ADC1_MspInit 1 */ |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) |
---|
168 | { |
---|
169 | |
---|
170 | if(adcHandle->Instance==ADC1) |
---|
171 | { |
---|
172 | /* USER CODE BEGIN ADC1_MspDeInit 0 */ |
---|
173 | |
---|
174 | /* USER CODE END ADC1_MspDeInit 0 */ |
---|
175 | /* Peripheral clock disable */ |
---|
176 | __HAL_RCC_ADC_CLK_DISABLE(); |
---|
177 | |
---|
178 | /**ADC1 GPIO Configuration |
---|
179 | PA5 ------> ADC1_IN5 |
---|
180 | PA6 ------> ADC1_IN6 |
---|
181 | PA7 ------> ADC1_IN7 |
---|
182 | PB0 ------> ADC1_IN8 |
---|
183 | */ |
---|
184 | HAL_GPIO_DeInit(GPIOA, ADC1_IN5_I_SENSE_AVG_Pin|ADC1_IN6_UIN_SENSE_Pin|ADC1_IN7_TEMP_SENSE_Pin); |
---|
185 | |
---|
186 | HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0); |
---|
187 | |
---|
188 | /* ADC1 DMA DeInit */ |
---|
189 | HAL_DMA_DeInit(adcHandle->DMA_Handle); |
---|
190 | |
---|
191 | /* ADC1 interrupt Deinit */ |
---|
192 | HAL_NVIC_DisableIRQ(ADC1_COMP_IRQn); |
---|
193 | /* USER CODE BEGIN ADC1_MspDeInit 1 */ |
---|
194 | |
---|
195 | /* USER CODE END ADC1_MspDeInit 1 */ |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | /* USER CODE BEGIN 1 */ |
---|
200 | |
---|
201 | /* USER CODE END 1 */ |
---|
202 | |
---|
203 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
---|