Rev 44 | Rev 46 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 44 | Rev 45 | ||
|---|---|---|---|
| Line 51... | Line 51... | ||
| 51 | 51 | ||
| 52 | // with a dwell angle of 45 degrees , 4 cylinders and a maximum RPM of 5000 |
52 | // with a dwell angle of 45 degrees , 4 cylinders and a maximum RPM of 5000 |
| 53 | // freq = 5000/60 * 2 = 166Hz. |
53 | // freq = 5000/60 * 2 = 166Hz. |
| 54 | // the TIM2 counter counts in 10uS increments, |
54 | // the TIM2 counter counts in 10uS increments, |
| 55 | // TODO this is wrong algo. Accept FIRST pulse, skip shorter pulses |
55 | // TODO this is wrong algo. Accept FIRST pulse, skip shorter pulses |
| 56 | // Accept the first pulse with over 2.5mS (1/400 sec) duration as the closure |
56 | // Accept the first pulse with over 1ms (1/1000 sec) duration as the closure |
| 57 | #define BREAKER_MIN (RPM_COUNT_RATE/400) |
57 | #define BREAKER_MIN (RPM_COUNT_RATE / 1000) |
| 58 | 58 | ||
| 59 | #define RPM_AVERAGE 4 |
59 | #define RPM_AVERAGE 4 |
| 60 | 60 | ||
| 61 | // wait for about 1 second to decide whether or not starter is on |
61 | // wait for about 1 second to decide whether or not starter is on |
| 62 | 62 | ||
| 63 | #define STARTER_LIMIT 10 |
63 | #define STARTER_LIMIT 10 |
| 64 | 64 | ||
| 65 | - | ||
| 66 | /* USER CODE END PM */ |
65 | /* USER CODE END PM */ |
| 67 | 66 | ||
| 68 | /* Private variables ---------------------------------------------------------*/ |
67 | /* Private variables ---------------------------------------------------------*/ |
| 69 | ADC_HandleTypeDef hadc1; |
68 | ADC_HandleTypeDef hadc1; |
| 70 | DMA_HandleTypeDef hdma_adc1; |
69 | DMA_HandleTypeDef hdma_adc1; |
| 71 | 70 | ||
| 72 | CAN_HandleTypeDef hcan; |
71 | CAN_HandleTypeDef hcan; |
| 73 | 72 | ||
| 74 | SPI_HandleTypeDef hspi1; |
73 | SPI_HandleTypeDef hspi1; |
| Line 79... | Line 78... | ||
| 79 | 78 | ||
| 80 | UART_HandleTypeDef huart1; |
79 | UART_HandleTypeDef huart1; |
| 81 | 80 | ||
| 82 | /* USER CODE BEGIN PV */ |
81 | /* USER CODE BEGIN PV */ |
| 83 | 82 | ||
| 84 | - | ||
| 85 | volatile char TimerFlag = 0; |
83 | volatile char TimerFlag = 0; |
| 86 | 84 | ||
| 87 | volatile char NoSerialInCTR = 0; // Missing characters coming in on USART1 |
85 | volatile char NoSerialInCTR = 0; // Missing characters coming in on USART1 |
| 88 | volatile char NoSerialIn = 0; |
86 | volatile char NoSerialIn = 0; |
| 89 | 87 | ||
| 90 | // scale for filtered samples |
88 | // scale for filtered samples |
| 91 | #define Scale 1024.0 |
89 | #define Scale 1024.0 |
| 92 | 90 | ||
| 93 | // storage for ADC |
91 | // storage for ADC |
| 94 | uint16_t ADC_Samples[ADC_CHANNELS] = { [0 ... ADC_CHANNELS-1] = 0 }; |
92 | uint16_t ADC_Samples[ADC_CHANNELS] = {[0 ... ADC_CHANNELS - 1] = 0}; |
| 95 | - | ||
| 96 | uint32_t FILT_Samples[ADC_CHANNELS] = { [0 ... ADC_CHANNELS-1] = 0 }; // filtered ADC samples * Scale |
- | |
| 97 | 93 | ||
| - | 94 | uint32_t FILT_Samples[ADC_CHANNELS] = {[0 ... ADC_CHANNELS - 1] = 0}; // filtered ADC samples * Scale |
|
| 98 | 95 | ||
| 99 | #define NOM_VREF 3.3 |
96 | #define NOM_VREF 3.3 |
| 100 | // initial ADC vref |
97 | // initial ADC vref |
| 101 | float adc_vref = NOM_VREF; |
98 | float adc_vref = NOM_VREF; |
| 102 | 99 | ||
| 103 | // internal bandgap voltage reference |
100 | // internal bandgap voltage reference |
| 104 | const float STM32REF = 1.2; // 1.2V typical |
101 | const float STM32REF = 1.2; // 1.2V typical |
| 105 | 102 | ||
| 106 | // scale factor initially assuming |
103 | // scale factor initially assuming |
| 107 | float ADC_Scale = 1/(Scale * 4096) * NOM_VREF ; |
104 | float ADC_Scale = 1 / (Scale * 4096) * NOM_VREF; |
| 108 | 105 | ||
| 109 | // Rev counter processing from original RevCounter Project |
106 | // Rev counter processing from original RevCounter Project |
| 110 | uint16_t RPM_Diff = 0; |
107 | uint16_t RPM_Diff = 0; |
| 111 | uint16_t RPM_Count_Latch = 0; |
108 | uint16_t RPM_Count_Latch = 0; |
| 112 | // accumulators |
109 | // accumulators |
| Line 143... | Line 140... | ||
| 143 | /* USER CODE END PFP */ |
140 | /* USER CODE END PFP */ |
| 144 | 141 | ||
| 145 | /* Private user code ---------------------------------------------------------*/ |
142 | /* Private user code ---------------------------------------------------------*/ |
| 146 | /* USER CODE BEGIN 0 */ |
143 | /* USER CODE BEGIN 0 */ |
| 147 | 144 | ||
| 148 | void |
- | |
| 149 | plx_sendword (int x) |
145 | void plx_sendword(int x) |
| 150 | { |
146 | { |
| 151 | PutCharSerial (&uc1, ((x) >> 6) & 0x3F); |
147 | PutCharSerial(&uc1, ((x) >> 6) & 0x3F); |
| 152 | PutCharSerial (&uc1, (x) & 0x3F); |
148 | PutCharSerial(&uc1, (x)&0x3F); |
| 153 | } |
149 | } |
| 154 | 150 | ||
| 155 | void |
- | |
| 156 | filter_ADC_samples () |
151 | void filter_ADC_samples() |
| 157 | { |
152 | { |
| 158 | int i; |
153 | int i; |
| 159 | for (i = 0; i < ADC_CHANNELS; i++) |
154 | for (i = 0; i < ADC_CHANNELS; i++) |
| 160 | { |
155 | { |
| 161 | FILT_Samples[i] += (ADC_Samples[i] * Scale - FILT_Samples[i]) / 2; |
156 | FILT_Samples[i] += (ADC_Samples[i] * Scale - FILT_Samples[i]) / 2; |
| 162 | } |
157 | } |
| 163 | } |
158 | } |
| 164 | 159 | ||
| 165 | - | ||
| 166 | /****! |
160 | /****! |
| 167 | * @brief this reads the reference voltage within the STM32L151 |
161 | * @brief this reads the reference voltage within the STM32L151 |
| 168 | * Powers up reference voltage and temperature sensor, waits 3mS and takes reading |
162 | * Powers up reference voltage and temperature sensor, waits 3mS and takes reading |
| 169 | * Requires that the ADC be powered up |
163 | * Requires that the ADC be powered up |
| 170 | */ |
164 | */ |
| 171 | 165 | ||
| 172 | - | ||
| 173 | void |
- | |
| 174 | CalibrateADC (void) |
166 | void CalibrateADC(void) |
| 175 | { |
167 | { |
| 176 | float adc_val = FILT_Samples[ADC_REF_CHAN] ; // as set up in device config |
168 | float adc_val = FILT_Samples[ADC_REF_CHAN]; // as set up in device config |
| 177 | - | ||
| 178 | float adc_vref = STM32REF * ( 4096.0 * Scale)/ adc_val; // the estimate for checking |
- | |
| 179 | - | ||
| 180 | ADC_Scale = 1/(Scale * 4096) * adc_vref ; |
- | |
| 181 | 169 | ||
| - | 170 | float adc_vref = STM32REF * (4096.0 * Scale) / adc_val; // the estimate for checking |
|
| 182 | 171 | ||
| - | 172 | ADC_Scale = 1 / (Scale * 4096) * adc_vref; |
|
| 183 | } |
173 | } |
| 184 | 174 | ||
| 185 | - | ||
| 186 | - | ||
| 187 | void |
- | |
| 188 | ProcessRPM (int instance) |
175 | void ProcessRPM(int instance) |
| 189 | { |
176 | { |
| 190 | // compute the timer values |
177 | // compute the timer values |
| 191 | // snapshot timers |
178 | // snapshot timers |
| 192 | unsigned short RPM_Pulsewidth; |
179 | unsigned short RPM_Pulsewidth; |
| 193 | // current RPM pulse next slot index |
180 | // current RPM pulse next slot index |
| 194 | unsigned short RPM_Count_Val; |
181 | unsigned short RPM_Count_Val; |
| 195 | __disable_irq (); // copy the counter value |
182 | __disable_irq(); // copy the counter value |
| 196 | RPM_Count_Val = RPM_Count; |
183 | RPM_Count_Val = RPM_Count; |
| 197 | __enable_irq (); |
184 | __enable_irq(); |
| 198 | // do calculations |
185 | // do calculations |
| 199 | // if there is only one entry, cannot get difference |
186 | // if there is only one entry, cannot get difference |
| 200 | if (RPM_Count_Latch != RPM_Count_Val) |
187 | if (RPM_Count_Latch != RPM_Count_Val) |
| - | 188 | { |
|
| - | 189 | while (1) |
|
| 201 | { |
190 | { |
| 202 | while (1) |
- | |
| 203 | { |
- | |
| 204 | unsigned int base_time; |
191 | unsigned int base_time; |
| 205 | unsigned int new_time; |
192 | unsigned int new_time; |
| 206 | // if we are at N-1, stop. |
193 | // if we are at N-1, stop. |
| 207 | unsigned int next_count = (RPM_Count_Latch + 1) % RPM_SAMPLES; |
194 | unsigned int next_count = (RPM_Count_Latch + 1) % RPM_SAMPLES; |
| 208 | if (next_count == RPM_Count_Val) |
195 | if (next_count == RPM_Count_Val) |
| 209 | { |
196 | { |
| 210 | break; // completed loop |
197 | break; // completed loop |
| 211 | } |
198 | } |
| 212 | char pulse_level = RPM_Level[RPM_Count_Latch]; |
199 | char pulse_level = RPM_Level[RPM_Count_Latch]; |
| 213 | base_time = RPM_Time[RPM_Count_Latch]; |
200 | base_time = RPM_Time[RPM_Count_Latch]; |
| 214 | new_time = RPM_Time[next_count]; |
201 | new_time = RPM_Time[next_count]; |
| 215 | RPM_Count_Latch = next_count; |
202 | RPM_Count_Latch = next_count; |
| 216 | 203 | ||
| 217 | RPM_Pulsewidth = new_time - base_time; // not wrapped |
204 | RPM_Pulsewidth = new_time - base_time; // not wrapped |
| 218 | 205 | ||
| 219 | // if the pulse was low, |
206 | // if the pulse was low, |
| 220 | if (pulse_level == 0 && RPM_Pulsewidth > BREAKER_MIN) |
207 | if (pulse_level == 0 && RPM_Pulsewidth > BREAKER_MIN) |
| 221 | { |
208 | { |
| 222 | 209 | ||
| 223 | RPM_Diff = new_time - last_dwell_end; |
210 | RPM_Diff = new_time - last_dwell_end; |
| 224 | 211 | ||
| 225 | RPM_Period[RPM_Period_Ptr] = RPM_Diff; |
212 | RPM_Period[RPM_Period_Ptr] = RPM_Diff; |
| 226 | RPM_Period_Ptr = (RPM_Period_Ptr + 1) % RPM_AVERAGE; |
213 | RPM_Period_Ptr = (RPM_Period_Ptr + 1) % RPM_AVERAGE; |
| 227 | if (RPM_Pulsecount < RPM_AVERAGE) |
214 | if (RPM_Pulsecount < RPM_AVERAGE) |
| 228 | RPM_Pulsecount++; // count one pulse |
215 | RPM_Pulsecount++; // count one pulse |
| 229 | last_dwell_end = new_time; |
216 | last_dwell_end = new_time; |
| 230 | - | ||
| 231 | } |
217 | } |
| 232 | } |
- | |
| 233 | - | ||
| 234 | } |
218 | } |
| - | 219 | } |
|
| 235 | 220 | ||
| 236 | if (RPM_Pulsecount == RPM_AVERAGE) |
221 | if (RPM_Pulsecount == RPM_AVERAGE) |
| 237 | { |
222 | { |
| 238 | // now have time for N pulses in clocks |
223 | // now have time for N pulses in clocks |
| 239 | // need to scale by 19.55: one unit is 19.55 RPM |
224 | // need to scale by 19.55: one unit is 19.55 RPM |
| 240 | // 1Hz is 30 RPM |
225 | // 1Hz is 30 RPM |
| 241 | int i; |
226 | int i; |
| 242 | RPM_FilteredWidth = 0; |
227 | RPM_FilteredWidth = 0; |
| 243 | for (i = 0; i < RPM_AVERAGE; i++) |
228 | for (i = 0; i < RPM_AVERAGE; i++) |
| 244 | RPM_FilteredWidth += RPM_Period[i]; |
229 | RPM_FilteredWidth += RPM_Period[i]; |
| 245 | 230 | ||
| 246 | Coded_RPM = (Scale * 30.0 * RPM_AVERAGE * RPM_COUNT_RATE) |
231 | Coded_RPM = (Scale * 30.0 * RPM_AVERAGE * RPM_COUNT_RATE) / (19.55 * RPM_FilteredWidth); |
| 247 | / (19.55 * RPM_FilteredWidth); |
- | |
| 248 | 232 | ||
| 249 | #if !defined MY_DEBUG |
233 | #if !defined MY_DEBUG |
| 250 | // reset here unless we want to debug |
234 | // reset here unless we want to debug |
| 251 | RPM_Pulsecount = 0; |
235 | RPM_Pulsecount = 0; |
| 252 | RPM_FilteredWidth = 0; |
236 | RPM_FilteredWidth = 0; |
| 253 | #endif |
237 | #endif |
| 254 | } |
238 | } |
| 255 | 239 | ||
| 256 | // send the current RPM *calculation |
240 | // send the current RPM *calculation |
| 257 | plx_sendword (PLX_RPM); |
241 | plx_sendword(PLX_RPM); |
| 258 | PutCharSerial (&uc1, instance); |
242 | PutCharSerial(&uc1, instance); |
| 259 | plx_sendword (Coded_RPM / Scale); |
243 | plx_sendword(Coded_RPM / Scale); |
| 260 | } |
244 | } |
| 261 | 245 | ||
| 262 | // this uses a MAX6675 which is a simple 16 bit read |
246 | // this uses a MAX6675 which is a simple 16 bit read |
| 263 | // SPI is configured for 8 bits so I can use an OLED display if I need it |
247 | // SPI is configured for 8 bits so I can use an OLED display if I need it |
| 264 | // must wait > 0.22 seconds between conversion attempts as this is the measurement time |
248 | // must wait > 0.22 seconds between conversion attempts as this is the measurement time |
| Line 266... | Line 250... | ||
| 266 | 250 | ||
| 267 | FunctionalState CHT_Enable = ENABLE; |
251 | FunctionalState CHT_Enable = ENABLE; |
| 268 | 252 | ||
| 269 | #define CORR 3 |
253 | #define CORR 3 |
| 270 | 254 | ||
| 271 | uint16_t Temp_Observations[NUM_SPI_TEMP_SENS] = { [0 ... NUM_SPI_TEMP_SENS-1] = 0 }; |
255 | uint16_t Temp_Observations[NUM_SPI_TEMP_SENS] = {[0 ... NUM_SPI_TEMP_SENS - 1] = 0}; |
| 272 | - | ||
| 273 | 256 | ||
| 274 | /// \param item The array index to send |
257 | /// \param item The array index to send |
| 275 | /// \param instance The instance to send over the bus |
258 | /// \param instance The instance to send over the bus |
| 276 | /// \param type the code to use for this observation |
259 | /// \param type the code to use for this observation |
| 277 | void |
- | |
| 278 | ProcessTemp (char item, int instance, enum PLX_Observations type) |
260 | void ProcessTemp(char item, int instance, enum PLX_Observations type) |
| 279 | { |
261 | { |
| 280 | if (item > NUM_SPI_TEMP_SENS) |
262 | if (item > NUM_SPI_TEMP_SENS) |
| 281 | return; |
263 | return; |
| 282 | plx_sendword (type); |
264 | plx_sendword(type); |
| 283 | PutCharSerial (&uc1, instance); |
265 | PutCharSerial(&uc1, instance); |
| 284 | plx_sendword (Temp_Observations[item]); |
266 | plx_sendword(Temp_Observations[(int)item]); |
| 285 | - | ||
| 286 | } |
267 | } |
| 287 | 268 | ||
| 288 | - | ||
| 289 | /// \brief Reset the temperature chip select system |
269 | /// \brief Reset the temperature chip select system |
| 290 | void resetTempCS(void) |
270 | void resetTempCS(void) |
| 291 | { |
271 | { |
| 292 | HAL_GPIO_WritePin (SPI_CS_D_GPIO_Port, SPI_CS_D_Pin, GPIO_PIN_SET); |
272 | HAL_GPIO_WritePin(SPI_CS_D_GPIO_Port, SPI_CS_D_Pin, GPIO_PIN_SET); |
| 293 | HAL_GPIO_WritePin (SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin, |
273 | HAL_GPIO_WritePin(SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin, |
| 294 | GPIO_PIN_SET); |
- | |
| 295 | - | ||
| 296 | for (int i = 0 ; i < 8; i++) |
274 | GPIO_PIN_SET); |
| 297 | { |
- | |
| 298 | HAL_GPIO_WritePin (SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin, |
- | |
| 299 | GPIO_PIN_RESET); |
- | |
| 300 | HAL_GPIO_WritePin (SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin, |
- | |
| 301 | GPIO_PIN_SET); |
- | |
| 302 | } |
- | |
| 303 | 275 | ||
| - | 276 | for (int i = 0; i < 8; i++) |
|
| - | 277 | { |
|
| - | 278 | HAL_GPIO_WritePin(SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin, |
|
| 304 | // prepare for selecting next pin |
279 | GPIO_PIN_RESET); |
| 305 | HAL_GPIO_WritePin (SPI_CS_D_GPIO_Port, SPI_CS_D_Pin, GPIO_PIN_RESET); |
280 | HAL_GPIO_WritePin(SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin, |
| - | 281 | GPIO_PIN_SET); |
|
| - | 282 | } |
|
| 306 | 283 | ||
| - | 284 | // prepare for selecting next pin |
|
| - | 285 | HAL_GPIO_WritePin(SPI_CS_D_GPIO_Port, SPI_CS_D_Pin, GPIO_PIN_RESET); |
|
| 307 | } |
286 | } |
| 308 | 287 | ||
| 309 | void nextTempCS(void) |
288 | void nextTempCS(void) |
| 310 | { |
289 | { |
| 311 | HAL_GPIO_WritePin (SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin, |
290 | HAL_GPIO_WritePin(SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin, |
| 312 | GPIO_PIN_RESET); |
291 | GPIO_PIN_RESET); |
| 313 | HAL_GPIO_WritePin (SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin, |
292 | HAL_GPIO_WritePin(SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin, |
| 314 | GPIO_PIN_SET); |
293 | GPIO_PIN_SET); |
| 315 | HAL_GPIO_WritePin (SPI_CS_D_GPIO_Port, SPI_CS_D_Pin, GPIO_PIN_SET); |
294 | HAL_GPIO_WritePin(SPI_CS_D_GPIO_Port, SPI_CS_D_Pin, GPIO_PIN_SET); |
| 316 | } |
295 | } |
| 317 | 296 | ||
| 318 | void |
- | |
| 319 | EnableTempSensors (FunctionalState state) |
297 | void EnableTempSensors(FunctionalState state) |
| 320 | 298 | ||
| 321 | { |
299 | { |
| 322 | GPIO_InitTypeDef GPIO_InitStruct; |
300 | GPIO_InitTypeDef GPIO_InitStruct; |
| 323 | 301 | ||
| 324 | CHT_Enable = state; |
302 | CHT_Enable = state; |
| 325 | 303 | ||
| 326 | /* enable SPI in live mode : assume it and its GPIOs are already initialised in SPI mode */ |
304 | /* enable SPI in live mode : assume it and its GPIOs are already initialised in SPI mode */ |
| 327 | if (state == ENABLE) |
305 | if (state == ENABLE) |
| 328 | { |
306 | { |
| 329 | HAL_GPIO_WritePin (ENA_AUX_5V_GPIO_Port, ENA_AUX_5V_Pin, GPIO_PIN_SET); |
307 | HAL_GPIO_WritePin(ENA_AUX_5V_GPIO_Port, ENA_AUX_5V_Pin, GPIO_PIN_SET); |
| 330 | - | ||
| 331 | resetTempCS(); |
- | |
| 332 | 308 | ||
| 333 | /* put the SPI pins back into SPI AF mode */ |
- | |
| 334 | GPIO_InitStruct.Pin = SPI1_MOSI_Pin | SPI1_MISO_Pin | SPI1_SCK_Pin; |
- | |
| 335 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
- | |
| 336 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
309 | resetTempCS(); |
| 337 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
- | |
| 338 | HAL_GPIO_Init (SPI1_SCK_GPIO_Port, &GPIO_InitStruct); |
- | |
| 339 | 310 | ||
| - | 311 | /* put the SPI pins back into SPI AF mode */ |
|
| - | 312 | GPIO_InitStruct.Pin = SPI1_MOSI_Pin | SPI1_MISO_Pin | SPI1_SCK_Pin; |
|
| - | 313 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
|
| - | 314 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
|
| - | 315 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
|
| - | 316 | HAL_GPIO_Init(SPI1_SCK_GPIO_Port, &GPIO_InitStruct); |
|
| 340 | } |
317 | } |
| 341 | else |
318 | else |
| 342 | { |
319 | { |
| 343 | /* Power down the SPI interface taking signals all low */ |
320 | /* Power down the SPI interface taking signals all low */ |
| 344 | HAL_GPIO_WritePin (ENA_AUX_5V_GPIO_Port, ENA_AUX_5V_Pin, GPIO_PIN_RESET); |
321 | HAL_GPIO_WritePin(ENA_AUX_5V_GPIO_Port, ENA_AUX_5V_Pin, GPIO_PIN_RESET); |
| 345 | - | ||
| 346 | HAL_GPIO_WritePin (SPI1_SCK_GPIO_Port, |
- | |
| 347 | SPI1_MOSI_Pin | SPI1_MISO_Pin | SPI1_SCK_Pin, |
- | |
| 348 | GPIO_PIN_RESET); |
- | |
| 349 | - | ||
| 350 | /* put the SPI pins back into GPIO mode */ |
- | |
| 351 | GPIO_InitStruct.Pin = SPI1_MOSI_Pin | SPI1_MISO_Pin | SPI1_SCK_Pin; |
- | |
| 352 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
- | |
| 353 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
- | |
| 354 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
- | |
| 355 | HAL_GPIO_Init (SPI1_SCK_GPIO_Port, &GPIO_InitStruct); |
- | |
| 356 | - | ||
| 357 | } |
- | |
| 358 | 322 | ||
| - | 323 | HAL_GPIO_WritePin(SPI1_SCK_GPIO_Port, |
|
| - | 324 | SPI1_MOSI_Pin | SPI1_MISO_Pin | SPI1_SCK_Pin, |
|
| - | 325 | GPIO_PIN_RESET); |
|
| - | 326 | ||
| - | 327 | /* put the SPI pins back into GPIO mode */ |
|
| - | 328 | GPIO_InitStruct.Pin = SPI1_MOSI_Pin | SPI1_MISO_Pin | SPI1_SCK_Pin; |
|
| - | 329 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
|
| - | 330 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
|
| - | 331 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
|
| - | 332 | HAL_GPIO_Init(SPI1_SCK_GPIO_Port, &GPIO_InitStruct); |
|
| - | 333 | } |
|
| 359 | } |
334 | } |
| 360 | 335 | ||
| 361 | // 1023 is 20.00 volts. |
336 | // 1023 is 20.00 volts. |
| 362 | void |
- | |
| 363 | ProcessBatteryVoltage (int instance) |
337 | void ProcessBatteryVoltage(int instance) |
| 364 | { |
338 | { |
| 365 | float reading = FILT_Samples[instance] * ADC_Scale; |
339 | float reading = FILT_Samples[instance] * ADC_Scale; |
| 366 | reading = reading * 7.8125; // real voltage |
340 | reading = reading * 7.8125; // real voltage |
| 367 | reading = reading * 51.15; // PLC scaling = 1023/20 |
341 | reading = reading * 51.15; // PLC scaling = 1023/20 |
| 368 | - | ||
| 369 | plx_sendword (PLX_Volts); |
- | |
| 370 | PutCharSerial (&uc1, instance); |
- | |
| 371 | plx_sendword ((uint16_t) reading); |
- | |
| 372 | 342 | ||
| - | 343 | plx_sendword(PLX_Volts); |
|
| - | 344 | PutCharSerial(&uc1, instance); |
|
| - | 345 | plx_sendword((uint16_t)reading); |
|
| 373 | } |
346 | } |
| 374 | 347 | ||
| 375 | - | ||
| 376 | void |
- | |
| 377 | ProcessCPUTemperature (int instance) |
348 | void ProcessCPUTemperature(int instance) |
| 378 | { |
349 | { |
| 379 | // this is defined in the STM32F103 reference manual . # |
350 | // this is defined in the STM32F103 reference manual . # |
| 380 | // V25 = 1.43 volts |
351 | // V25 = 1.43 volts |
| 381 | // Avg_slope = 4.3mV /degree C |
352 | // Avg_slope = 4.3mV /degree C |
| 382 | // temperature = {(V25 - VSENSE) / Avg_Slope} + 25 |
353 | // temperature = {(V25 - VSENSE) / Avg_Slope} + 25 |
| 383 | 354 | ||
| 384 | /* get the ADC reading corresponding to ADC channel 16 after turning on the ADC */ |
355 | /* get the ADC reading corresponding to ADC channel 16 after turning on the ADC */ |
| 385 | 356 | ||
| 386 | float temp_val = FILT_Samples[ADC_TEMP_CHAN] * ADC_Scale; |
357 | float temp_val = FILT_Samples[ADC_TEMP_CHAN] * ADC_Scale; |
| 387 | /* renormalise temperature value to account for different ADC Vref : normalise to that which we would get for a 3000mV reference */ |
358 | /* renormalise temperature value to account for different ADC Vref : normalise to that which we would get for a 3000mV reference */ |
| 388 | temp_val = (1.43- temp_val) / 4.3e-3 + 25; |
359 | temp_val = (1.43 - temp_val) / 4.3e-3 + 25; |
| 389 | - | ||
| 390 | int32_t result = temp_val ; |
- | |
| 391 | 360 | ||
| 392 | // int32_t result = 800 * ((int32_t) temp_val - TS_CAL30); |
361 | int32_t result = temp_val; |
| 393 | // result = result / (TS_CAL110 - TS_CAL30) + 300; |
- | |
| 394 | 362 | ||
| - | 363 | // int32_t result = 800 * ((int32_t) temp_val - TS_CAL30); |
|
| - | 364 | // result = result / (TS_CAL110 - TS_CAL30) + 300; |
|
| 395 | 365 | ||
| 396 | plx_sendword (PLX_FluidTemp); |
366 | plx_sendword(PLX_FluidTemp); |
| 397 | PutCharSerial (&uc1, instance); |
367 | PutCharSerial(&uc1, instance); |
| 398 | plx_sendword (result); |
368 | plx_sendword(result); |
| 399 | - | ||
| 400 | } |
369 | } |
| 401 | 370 | ||
| 402 | // the MAP sensor is giving us a reading of |
371 | // the MAP sensor is giving us a reading of |
| 403 | // 4.6 volts for 1019mB or 2.27 volts at the ADC input (resistive divider by 2.016) |
372 | // 4.6 volts for 1019mB or 2.27 volts at the ADC input (resistive divider by 2.016) |
| 404 | // I believe the sensor reads 4.5V at 1000kPa and 0.5V at 0kPa |
373 | // I believe the sensor reads 4.5V at 1000kPa and 0.5V at 0kPa |
| Line 406... | Line 375... | ||
| 406 | // Real Displayed |
375 | // Real Displayed |
| 407 | // 989 968 |
376 | // 989 968 |
| 408 | // 994.1 986 |
377 | // 994.1 986 |
| 409 | // 992.3 984 |
378 | // 992.3 984 |
| 410 | 379 | ||
| 411 | void |
- | |
| 412 | ProcessMAP (int instance) |
380 | void ProcessMAP(int instance) |
| 413 | { |
381 | { |
| 414 | // Using ADC_Samples[3] as the MAP input |
382 | // Using ADC_Samples[3] as the MAP input |
| 415 | float reading = FILT_Samples[ADC_MAP_CHAN] * ADC_Scale; |
383 | float reading = FILT_Samples[ADC_MAP_CHAN] * ADC_Scale; |
| 416 | reading = reading * 2.016; // real voltage |
384 | reading = reading * 2.016; // real voltage |
| 417 | // values computed from slope / intercept of map.ods |
385 | // values computed from slope / intercept of map.ods |
| 418 | //reading = (reading) * 56.23 + 743.2; // do not assume 0.5 volt offset : reading from 0 to 4.5 instead of 0.5 to 4.5 |
386 | // reading = (reading) * 56.23 + 743.2; // do not assume 0.5 volt offset : reading from 0 to 4.5 instead of 0.5 to 4.5 |
| 419 | // using a pressure gauge. |
387 | // using a pressure gauge. |
| 420 | reading = (reading) * 150 + 326; |
388 | reading = (reading)*150 + 326; |
| 421 | - | ||
| 422 | plx_sendword (PLX_MAP); |
- | |
| 423 | PutCharSerial (&uc1, instance); |
- | |
| 424 | plx_sendword ((uint16_t) reading); |
- | |
| 425 | 389 | ||
| - | 390 | plx_sendword(PLX_MAP); |
|
| - | 391 | PutCharSerial(&uc1, instance); |
|
| - | 392 | plx_sendword((uint16_t)reading); |
|
| 426 | } |
393 | } |
| 427 | 394 | ||
| 428 | // the Oil pressi sensor is giving us a reading of |
395 | // the Oil pressi sensor is giving us a reading of |
| 429 | // 4.5 volts for 100 PSI or 2.25 volts at the ADC input (resistive divider by 2.016) |
396 | // 4.5 volts for 100 PSI or 2.25 volts at the ADC input (resistive divider by 2.016) |
| 430 | // I believe the sensor reads 4.5V at 100PSI and 0.5V at 0PSI |
397 | // I believe the sensor reads 4.5V at 100PSI and 0.5V at 0PSI |
| 431 | // an observation of 1024 is 200PSI, so observation of 512 is 100 PSI. |
398 | // an observation of 1024 is 200PSI, so observation of 512 is 100 PSI. |
| 432 | 399 | ||
| 433 | void |
- | |
| 434 | ProcessOilPress (int instance) |
400 | void ProcessOilPress(int instance) |
| 435 | { |
401 | { |
| 436 | // Using ADC_Samples[2] as the MAP input |
402 | // Using ADC_Samples[2] as the MAP input |
| 437 | float reading = FILT_Samples[ADC_PRESSURE_CHAN] * ADC_Scale; |
403 | float reading = FILT_Samples[ADC_PRESSURE_CHAN] * ADC_Scale; |
| 438 | reading = reading * 2.00; // real voltage |
404 | reading = reading * 2.00; // real voltage |
| 439 | reading = (reading - 0.5) * 512 / 4; // this is 1023 * 100/200 |
405 | reading = (reading - 0.5) * 512 / 4; // this is 1023 * 100/200 |
| 440 | - | ||
| 441 | plx_sendword (PLX_FluidPressure); |
- | |
| 442 | PutCharSerial (&uc1, instance); |
- | |
| 443 | plx_sendword ((uint16_t) reading); |
- | |
| 444 | 406 | ||
| - | 407 | plx_sendword(PLX_FluidPressure); |
|
| - | 408 | PutCharSerial(&uc1, instance); |
|
| - | 409 | plx_sendword((uint16_t)reading); |
|
| 445 | } |
410 | } |
| 446 | 411 | ||
| 447 | void |
- | |
| 448 | ProcessTiming (int instance) |
412 | void ProcessTiming(int instance) |
| 449 | { |
413 | { |
| 450 | plx_sendword (PLX_Timing); |
414 | plx_sendword(PLX_Timing); |
| 451 | PutCharSerial (&uc1, instance); |
415 | PutCharSerial(&uc1, instance); |
| 452 | plx_sendword (64 - 15); // make it negative |
416 | plx_sendword(64 - 15); // make it negative |
| 453 | } |
417 | } |
| 454 | 418 | ||
| 455 | /* USER CODE END 0 */ |
419 | /* USER CODE END 0 */ |
| 456 | 420 | ||
| 457 | /** |
421 | /** |
| Line 489... | Line 453... | ||
| 489 | MX_TIM2_Init(); |
453 | MX_TIM2_Init(); |
| 490 | MX_TIM3_Init(); |
454 | MX_TIM3_Init(); |
| 491 | MX_TIM4_Init(); |
455 | MX_TIM4_Init(); |
| 492 | MX_USART1_UART_Init(); |
456 | MX_USART1_UART_Init(); |
| 493 | /* USER CODE BEGIN 2 */ |
457 | /* USER CODE BEGIN 2 */ |
| 494 | HAL_MspInit (); |
458 | HAL_MspInit(); |
| 495 | 459 | ||
| 496 | // Not using HAL USART code |
460 | // Not using HAL USART code |
| 497 | __HAL_RCC_USART1_CLK_ENABLE() |
461 | __HAL_RCC_USART1_CLK_ENABLE(); // PLX comms port |
| 498 | ; // PLX comms port |
- | |
| 499 | /* setup the USART control blocks */ |
462 | /* setup the USART control blocks */ |
| 500 | init_usart_ctl (&uc1, &huart1); |
463 | init_usart_ctl(&uc1, &huart1); |
| 501 | 464 | ||
| 502 | EnableSerialRxInterrupt (&uc1); |
465 | EnableSerialRxInterrupt(&uc1); |
| 503 | 466 | ||
| 504 | HAL_SPI_MspInit (&hspi1); |
467 | HAL_SPI_MspInit(&hspi1); |
| 505 | 468 | ||
| 506 | HAL_ADC_MspInit (&hadc1); |
469 | HAL_ADC_MspInit(&hadc1); |
| 507 | 470 | ||
| 508 | HAL_ADC_Start_DMA (&hadc1, (uint32_t *)ADC_Samples, ADC_CHANNELS); |
471 | HAL_ADC_Start_DMA(&hadc1, (uint32_t *)ADC_Samples, ADC_CHANNELS); |
| 509 | 472 | ||
| 510 | HAL_ADC_Start_IT (&hadc1); |
473 | HAL_ADC_Start_IT(&hadc1); |
| 511 | 474 | ||
| 512 | HAL_TIM_Base_MspInit (&htim4); |
475 | HAL_TIM_Base_MspInit(&htim4); |
| 513 | HAL_TIM_Base_Start_IT (&htim4); |
476 | HAL_TIM_Base_Start_IT(&htim4); |
| 514 | 477 | ||
| 515 | // initialise all the STMCubeMX stuff |
478 | // initialise all the STMCubeMX stuff |
| 516 | HAL_TIM_Base_MspInit (&htim2); |
479 | HAL_TIM_Base_MspInit(&htim2); |
| 517 | // Start the counter |
480 | // Start the counter |
| 518 | HAL_TIM_Base_Start (&htim2); |
481 | HAL_TIM_Base_Start(&htim2); |
| 519 | // Start the input capture and the rising edge interrupt |
482 | // Start the input capture and the rising edge interrupt |
| 520 | HAL_TIM_IC_Start_IT (&htim2, TIM_CHANNEL_1); |
483 | HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1); |
| 521 | // Start the input capture and the falling edge interrupt |
484 | // Start the input capture and the falling edge interrupt |
| 522 | HAL_TIM_IC_Start_IT (&htim2, TIM_CHANNEL_2); |
485 | HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2); |
| 523 | 486 | ||
| 524 | HAL_TIM_Base_MspInit (&htim3); |
487 | HAL_TIM_Base_MspInit(&htim3); |
| 525 | __HAL_TIM_ENABLE_IT(&htim3, TIM_IT_UPDATE); |
488 | __HAL_TIM_ENABLE_IT(&htim3, TIM_IT_UPDATE); |
| 526 | uint32_t Ticks = HAL_GetTick () + 100; |
489 | uint32_t Ticks = HAL_GetTick() + 100; |
| 527 | int CalCounter = 0; |
490 | int CalCounter = 0; |
| 528 | 491 | ||
| 529 | PowerTempTimer = HAL_GetTick () + 1000; /* wait 10 seconds before powering up the CHT sensor */ |
492 | PowerTempTimer = HAL_GetTick() + 1000; /* wait 10 seconds before powering up the CHT sensor */ |
| 530 | 493 | ||
| - | 494 | ResetRxBuffer(&uc1); |
|
| 531 | /* USER CODE END 2 */ |
495 | /* USER CODE END 2 */ |
| 532 | 496 | ||
| 533 | /* Infinite loop */ |
497 | /* Infinite loop */ |
| 534 | /* USER CODE BEGIN WHILE */ |
498 | /* USER CODE BEGIN WHILE */ |
| 535 | while (1) |
499 | while (1) |
| 536 | { |
500 | { |
| 537 | /* USER CODE END WHILE */ |
501 | /* USER CODE END WHILE */ |
| 538 | 502 | ||
| 539 | /* USER CODE BEGIN 3 */ |
503 | /* USER CODE BEGIN 3 */ |
| 540 | 504 | ||
| 541 | if (HAL_GetTick () > Ticks) |
505 | if (HAL_GetTick() > Ticks) |
| 542 | { |
506 | { |
| 543 | Ticks += 100; |
507 | Ticks += 100; |
| 544 | filter_ADC_samples (); |
508 | filter_ADC_samples(); |
| 545 | // delay to calibrate ADC |
509 | // delay to calibrate ADC |
| 546 | if (CalCounter < 1000) |
510 | if (CalCounter < 1000) |
| 547 | { |
511 | { |
| 548 | CalCounter += 100; |
512 | CalCounter += 100; |
| 549 | } |
513 | } |
| 550 | 514 | ||
| 551 | if (CalCounter == 900) |
515 | if (CalCounter == 900) |
| 552 | { |
516 | { |
| 553 | CalibrateADC (); |
517 | CalibrateADC(); |
| 554 | } |
- | |
| 555 | } |
- | |
| 556 | /* when the starter motor is on then power down the CHT sensors as they seem to fail */ |
- | |
| 557 | - | ||
| 558 | if (HAL_GPIO_ReadPin (STARTER_ON_GPIO_Port, STARTER_ON_Pin) |
- | |
| 559 | == GPIO_PIN_RESET) |
- | |
| 560 | { |
- | |
| 561 | if (Starter_Debounce < STARTER_LIMIT) |
- | |
| 562 | { |
- | |
| 563 | Starter_Debounce++; |
- | |
| 564 | } |
518 | } |
| 565 | } |
- | |
| 566 | else |
- | |
| 567 | { |
- | |
| 568 | if (Starter_Debounce > 0) |
- | |
| 569 | { |
- | |
| 570 | Starter_Debounce--; |
- | |
| 571 | } |
519 | } |
| 572 | } |
- | |
| 573 | - | ||
| 574 | if (Starter_Debounce == STARTER_LIMIT) |
- | |
| 575 | { |
- | |
| 576 | EnableTempSensors (DISABLE); |
- | |
| 577 | PowerTempTimer = HAL_GetTick () + 1000; |
- | |
| 578 | } |
- | |
| 579 | else |
- | |
| 580 | /* if the PowerTempTimer is set then wait for it to timeout, then power up CHT */ |
520 | /* when the starter motor is on then power down the CHT sensors as they seem to fail */ |
| 581 | { |
- | |
| 582 | if ((PowerTempTimer > 0) && (HAL_GetTick () > PowerTempTimer)) |
- | |
| 583 | { |
- | |
| 584 | EnableTempSensors (ENABLE); |
- | |
| 585 | PowerTempTimer = 0; |
- | |
| 586 | } |
- | |
| 587 | } |
- | |
| 588 | - | ||
| 589 | // check to see if we have any incoming data, copy and append if so, if no data then create our own frames. |
- | |
| 590 | int c; |
- | |
| 591 | char send = 0; |
- | |
| 592 | - | ||
| 593 | // poll the input for a stop bit or timeout |
- | |
| 594 | if (PollSerial (&uc1)) |
- | |
| 595 | { |
- | |
| 596 | resetSerialTimeout (); |
- | |
| 597 | c = GetCharSerial (&uc1); |
- | |
| 598 | if (c != PLX_Stop) |
- | |
| 599 | { |
- | |
| 600 | PutCharSerial (&uc1, c); // echo all but the stop bit |
- | |
| 601 | } |
- | |
| 602 | else |
- | |
| 603 | { // must be a stop character |
- | |
| 604 | send = 1; // start our sending process. |
- | |
| 605 | } |
- | |
| 606 | } |
- | |
| 607 | - | ||
| 608 | // sort out auto-sending |
- | |
| 609 | if (TimerFlag) |
- | |
| 610 | { |
- | |
| 611 | TimerFlag = 0; |
- | |
| 612 | if (NoSerialIn) |
- | |
| 613 | { |
- | |
| 614 | PutCharSerial (&uc1, PLX_Start); |
- | |
| 615 | send = 1; |
- | |
| 616 | } |
- | |
| 617 | } |
- | |
| 618 | if (send) |
- | |
| 619 | { |
- | |
| 620 | send = 0; |
- | |
| 621 | - | ||
| 622 | // send the observations |
- | |
| 623 | ProcessRPM (0); |
- | |
| 624 | ProcessTemp (0,0,PLX_X_CHT); |
- | |
| 625 | ProcessTemp (1,1,PLX_X_CHT); |
- | |
| 626 | ProcessTemp (2,0,PLX_AIT); |
- | |
| 627 | ProcessTemp (3,1,PLX_AIT); |
- | |
| 628 | ProcessBatteryVoltage (0); // Batt 1 |
- | |
| 629 | ProcessBatteryVoltage (1); // Batt 2 |
- | |
| 630 | ProcessCPUTemperature (0); // built in temperature sensor |
- | |
| 631 | 521 | ||
| - | 522 | if (HAL_GPIO_ReadPin(STARTER_ON_GPIO_Port, STARTER_ON_Pin) == GPIO_PIN_RESET) |
|
| - | 523 | { |
|
| - | 524 | if (Starter_Debounce < STARTER_LIMIT) |
|
| - | 525 | { |
|
| - | 526 | Starter_Debounce++; |
|
| - | 527 | } |
|
| - | 528 | } |
|
| 632 | ProcessMAP (0); |
529 | else |
| - | 530 | { |
|
| - | 531 | if (Starter_Debounce > 0) |
|
| - | 532 | { |
|
| 633 | ProcessOilPress (0); |
533 | Starter_Debounce--; |
| - | 534 | } |
|
| - | 535 | } |
|
| 634 | 536 | ||
| - | 537 | if (Starter_Debounce == STARTER_LIMIT) |
|
| - | 538 | { |
|
| 635 | PutCharSerial (&uc1, PLX_Stop); |
539 | EnableTempSensors(DISABLE); |
| - | 540 | PowerTempTimer = HAL_GetTick() + 1000; |
|
| 636 | } |
541 | } |
| - | 542 | else |
|
| - | 543 | /* if the PowerTempTimer is set then wait for it to timeout, then power up CHT */ |
|
| - | 544 | { |
|
| - | 545 | if ((PowerTempTimer > 0) && (HAL_GetTick() > PowerTempTimer)) |
|
| - | 546 | { |
|
| - | 547 | EnableTempSensors(ENABLE); |
|
| - | 548 | PowerTempTimer = 0; |
|
| - | 549 | } |
|
| 637 | } |
550 | } |
| 638 | 551 | ||
| - | 552 | // check to see if we have any incoming data, copy and append if so, if no data then create our own frames. |
|
| - | 553 | int c; |
|
| - | 554 | char send = 0; |
|
| - | 555 | ||
| - | 556 | // poll the input for a stop bit or timeout |
|
| - | 557 | if (PollSerial(&uc1)) |
|
| - | 558 | { |
|
| - | 559 | resetSerialTimeout(); |
|
| - | 560 | c = GetCharSerial(&uc1); |
|
| - | 561 | if (c != PLX_Stop) |
|
| - | 562 | { |
|
| - | 563 | PutCharSerial(&uc1, c); // echo all but the stop bit |
|
| - | 564 | } |
|
| - | 565 | else |
|
| - | 566 | { // must be a stop character |
|
| - | 567 | send = 1; // start our sending process. |
|
| - | 568 | } |
|
| - | 569 | } |
|
| - | 570 | ||
| - | 571 | // sort out auto-sending |
|
| - | 572 | if (TimerFlag) |
|
| - | 573 | { |
|
| - | 574 | TimerFlag = 0; |
|
| - | 575 | if (NoSerialIn) |
|
| - | 576 | { |
|
| - | 577 | PutCharSerial(&uc1, PLX_Start); |
|
| - | 578 | send = 1; |
|
| - | 579 | } |
|
| - | 580 | } |
|
| - | 581 | if (send) |
|
| - | 582 | { |
|
| - | 583 | send = 0; |
|
| - | 584 | ||
| - | 585 | // send the observations |
|
| - | 586 | ProcessRPM(0); |
|
| - | 587 | ProcessTemp(0, 0, PLX_X_CHT); |
|
| - | 588 | ProcessTemp(1, 1, PLX_X_CHT); |
|
| - | 589 | ProcessTemp(2, 0, PLX_AIT); |
|
| - | 590 | ProcessTemp(3, 1, PLX_AIT); |
|
| - | 591 | ProcessBatteryVoltage(0); // Batt 1 |
|
| - | 592 | ProcessBatteryVoltage(1); // Batt 2 |
|
| - | 593 | ProcessCPUTemperature(0); // built in temperature sensor |
|
| - | 594 | ||
| - | 595 | ProcessMAP(0); |
|
| - | 596 | ProcessOilPress(0); |
|
| - | 597 | ||
| - | 598 | PutCharSerial(&uc1, PLX_Stop); |
|
| - | 599 | } |
|
| - | 600 | } |
|
| 639 | 601 | ||
| 640 | /* USER CODE END 3 */ |
602 | /* USER CODE END 3 */ |
| 641 | } |
603 | } |
| 642 | 604 | ||
| 643 | /** |
605 | /** |
| Line 662... | Line 624... | ||
| 662 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; |
624 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; |
| 663 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
625 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
| 664 | { |
626 | { |
| 665 | Error_Handler(); |
627 | Error_Handler(); |
| 666 | } |
628 | } |
| - | 629 | ||
| 667 | /** Initializes the CPU, AHB and APB buses clocks |
630 | /** Initializes the CPU, AHB and APB buses clocks |
| 668 | */ |
631 | */ |
| 669 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
632 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
| 670 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
633 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
| 671 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
634 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| Line 700... | Line 663... | ||
| 700 | ADC_ChannelConfTypeDef sConfig = {0}; |
663 | ADC_ChannelConfTypeDef sConfig = {0}; |
| 701 | 664 | ||
| 702 | /* USER CODE BEGIN ADC1_Init 1 */ |
665 | /* USER CODE BEGIN ADC1_Init 1 */ |
| 703 | 666 | ||
| 704 | /* USER CODE END ADC1_Init 1 */ |
667 | /* USER CODE END ADC1_Init 1 */ |
| - | 668 | ||
| 705 | /** Common config |
669 | /** Common config |
| 706 | */ |
670 | */ |
| 707 | hadc1.Instance = ADC1; |
671 | hadc1.Instance = ADC1; |
| 708 | hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE; |
672 | hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE; |
| 709 | hadc1.Init.ContinuousConvMode = DISABLE; |
673 | hadc1.Init.ContinuousConvMode = DISABLE; |
| Line 713... | Line 677... | ||
| 713 | hadc1.Init.NbrOfConversion = 7; |
677 | hadc1.Init.NbrOfConversion = 7; |
| 714 | if (HAL_ADC_Init(&hadc1) != HAL_OK) |
678 | if (HAL_ADC_Init(&hadc1) != HAL_OK) |
| 715 | { |
679 | { |
| 716 | Error_Handler(); |
680 | Error_Handler(); |
| 717 | } |
681 | } |
| - | 682 | ||
| 718 | /** Configure Regular Channel |
683 | /** Configure Regular Channel |
| 719 | */ |
684 | */ |
| 720 | sConfig.Channel = ADC_CHANNEL_0; |
685 | sConfig.Channel = ADC_CHANNEL_0; |
| 721 | sConfig.Rank = ADC_REGULAR_RANK_1; |
686 | sConfig.Rank = ADC_REGULAR_RANK_1; |
| 722 | sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5; |
687 | sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5; |
| 723 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
688 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
| 724 | { |
689 | { |
| 725 | Error_Handler(); |
690 | Error_Handler(); |
| 726 | } |
691 | } |
| - | 692 | ||
| 727 | /** Configure Regular Channel |
693 | /** Configure Regular Channel |
| 728 | */ |
694 | */ |
| 729 | sConfig.Channel = ADC_CHANNEL_1; |
695 | sConfig.Channel = ADC_CHANNEL_1; |
| 730 | sConfig.Rank = ADC_REGULAR_RANK_2; |
696 | sConfig.Rank = ADC_REGULAR_RANK_2; |
| 731 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
697 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
| 732 | { |
698 | { |
| 733 | Error_Handler(); |
699 | Error_Handler(); |
| 734 | } |
700 | } |
| - | 701 | ||
| 735 | /** Configure Regular Channel |
702 | /** Configure Regular Channel |
| 736 | */ |
703 | */ |
| 737 | sConfig.Channel = ADC_CHANNEL_2; |
704 | sConfig.Channel = ADC_CHANNEL_2; |
| 738 | sConfig.Rank = ADC_REGULAR_RANK_3; |
705 | sConfig.Rank = ADC_REGULAR_RANK_3; |
| 739 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
706 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
| 740 | { |
707 | { |
| 741 | Error_Handler(); |
708 | Error_Handler(); |
| 742 | } |
709 | } |
| - | 710 | ||
| 743 | /** Configure Regular Channel |
711 | /** Configure Regular Channel |
| 744 | */ |
712 | */ |
| 745 | sConfig.Channel = ADC_CHANNEL_3; |
713 | sConfig.Channel = ADC_CHANNEL_3; |
| 746 | sConfig.Rank = ADC_REGULAR_RANK_4; |
714 | sConfig.Rank = ADC_REGULAR_RANK_4; |
| 747 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
715 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
| 748 | { |
716 | { |
| 749 | Error_Handler(); |
717 | Error_Handler(); |
| 750 | } |
718 | } |
| - | 719 | ||
| 751 | /** Configure Regular Channel |
720 | /** Configure Regular Channel |
| 752 | */ |
721 | */ |
| 753 | sConfig.Channel = ADC_CHANNEL_4; |
722 | sConfig.Channel = ADC_CHANNEL_4; |
| 754 | sConfig.Rank = ADC_REGULAR_RANK_5; |
723 | sConfig.Rank = ADC_REGULAR_RANK_5; |
| 755 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
724 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
| 756 | { |
725 | { |
| 757 | Error_Handler(); |
726 | Error_Handler(); |
| 758 | } |
727 | } |
| - | 728 | ||
| 759 | /** Configure Regular Channel |
729 | /** Configure Regular Channel |
| 760 | */ |
730 | */ |
| 761 | sConfig.Channel = ADC_CHANNEL_VREFINT; |
731 | sConfig.Channel = ADC_CHANNEL_VREFINT; |
| 762 | sConfig.Rank = ADC_REGULAR_RANK_6; |
732 | sConfig.Rank = ADC_REGULAR_RANK_6; |
| 763 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
733 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
| 764 | { |
734 | { |
| 765 | Error_Handler(); |
735 | Error_Handler(); |
| 766 | } |
736 | } |
| - | 737 | ||
| 767 | /** Configure Regular Channel |
738 | /** Configure Regular Channel |
| 768 | */ |
739 | */ |
| 769 | sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; |
740 | sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; |
| 770 | sConfig.Rank = ADC_REGULAR_RANK_7; |
741 | sConfig.Rank = ADC_REGULAR_RANK_7; |
| 771 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
742 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
| Line 1126... | Line 1097... | ||
| 1126 | * @retval None |
1097 | * @retval None |
| 1127 | */ |
1098 | */ |
| 1128 | void Error_Handler(void) |
1099 | void Error_Handler(void) |
| 1129 | { |
1100 | { |
| 1130 | /* USER CODE BEGIN Error_Handler_Debug */ |
1101 | /* USER CODE BEGIN Error_Handler_Debug */ |
| 1131 | /* User can add his own implementation to report the HAL error return state */ |
1102 | /* User can add his own implementation to report the HAL error return state */ |
| 1132 | 1103 | ||
| 1133 | /* USER CODE END Error_Handler_Debug */ |
1104 | /* USER CODE END Error_Handler_Debug */ |
| 1134 | } |
1105 | } |
| 1135 | 1106 | ||
| 1136 | #ifdef USE_FULL_ASSERT |
1107 | #ifdef USE_FULL_ASSERT |
| Line 1147... | Line 1118... | ||
| 1147 | /* User can add his own implementation to report the file name and line number, |
1118 | /* User can add his own implementation to report the file name and line number, |
| 1148 | tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
1119 | tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
| 1149 | /* USER CODE END 6 */ |
1120 | /* USER CODE END 6 */ |
| 1150 | } |
1121 | } |
| 1151 | #endif /* USE_FULL_ASSERT */ |
1122 | #endif /* USE_FULL_ASSERT */ |
| 1152 | - | ||
| 1153 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
- | |