Rev 16 | Rev 18 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /** |
2 | ****************************************************************************** |
||
3 | * File Name : main.c |
||
4 | * Description : Main program body |
||
5 | ****************************************************************************** |
||
6 | * |
||
7 | * COPYRIGHT(c) 2016 STMicroelectronics |
||
8 | * |
||
9 | * Redistribution and use in source and binary forms, with or without modification, |
||
10 | * are permitted provided that the following conditions are met: |
||
11 | * 1. Redistributions of source code must retain the above copyright notice, |
||
12 | * this list of conditions and the following disclaimer. |
||
13 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
||
14 | * this list of conditions and the following disclaimer in the documentation |
||
15 | * and/or other materials provided with the distribution. |
||
16 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
||
17 | * may be used to endorse or promote products derived from this software |
||
18 | * without specific prior written permission. |
||
19 | * |
||
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
||
26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||
27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
||
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
30 | * |
||
31 | ****************************************************************************** |
||
32 | */ |
||
33 | /* Includes ------------------------------------------------------------------*/ |
||
34 | #include "stm32l1xx_hal.h" |
||
35 | |||
36 | /* USER CODE BEGIN Includes */ |
||
7 | mjames | 37 | #include "serial.h" |
9 | mjames | 38 | #include "plx.h" |
39 | #include "misc.h" |
||
2 | mjames | 40 | /* USER CODE END Includes */ |
41 | |||
42 | /* Private variables ---------------------------------------------------------*/ |
||
43 | ADC_HandleTypeDef hadc; |
||
6 | mjames | 44 | DMA_HandleTypeDef hdma_adc; |
2 | mjames | 45 | |
46 | SPI_HandleTypeDef hspi1; |
||
47 | |||
48 | TIM_HandleTypeDef htim2; |
||
49 | TIM_HandleTypeDef htim6; |
||
50 | |||
51 | UART_HandleTypeDef huart1; |
||
6 | mjames | 52 | UART_HandleTypeDef huart2; |
2 | mjames | 53 | |
54 | /* USER CODE BEGIN PV */ |
||
55 | /* Private variables ---------------------------------------------------------*/ |
||
56 | |||
8 | mjames | 57 | |
9 | mjames | 58 | // with a dwell angle of 45 degrees , 4 cylinders and a maximum RPM of 5000 |
59 | // freq = 5000/60 * 2 = 166Hz. Because the breaker might bounce , we accept the first pulse longer than 1/300 of a second as being a proper closure . |
||
60 | // the TIM2 counter counts in 10uS increments, |
||
61 | |||
62 | #define BREAKER_MIN (RPM_COUNT_RATE/300) |
||
63 | |||
64 | |||
65 | volatile char TimerFlag = 0; |
||
66 | |||
67 | volatile char NoSerialInCTR = 0; // Missing characters coming in on USART1 |
||
68 | volatile char NoSerialIn = 0; |
||
69 | |||
8 | mjames | 70 | // storage for ADC |
14 | mjames | 71 | uint16_t ADC_Samples[6]; |
8 | mjames | 72 | |
17 | mjames | 73 | #define Scale 1024.0 |
74 | const float ADC_Scale = 3.3 / (Scale * 4096.0); // convert to a voltage |
||
75 | |||
76 | uint32_t FILT_Samples[6]; // filtered ADC samples * 1024 |
||
9 | mjames | 77 | // Rev counter processing from original RevCounter Project |
78 | unsigned int RPM_Diff = 0; |
||
79 | unsigned int RPM_Count_Latch = 0; |
||
80 | // accumulators |
||
81 | unsigned int RPM_Pulsecount = 0; |
||
82 | unsigned int RPM_FilteredWidth = 0; |
||
83 | |||
84 | unsigned int Coded_RPM = 0; |
||
85 | unsigned int Coded_CHT = 0; |
||
86 | |||
2 | mjames | 87 | /* USER CODE END PV */ |
88 | |||
89 | /* Private function prototypes -----------------------------------------------*/ |
||
90 | void SystemClock_Config(void); |
||
91 | void Error_Handler(void); |
||
92 | static void MX_GPIO_Init(void); |
||
6 | mjames | 93 | static void MX_DMA_Init(void); |
2 | mjames | 94 | static void MX_ADC_Init(void); |
95 | static void MX_SPI1_Init(void); |
||
96 | static void MX_TIM2_Init(void); |
||
97 | static void MX_TIM6_Init(void); |
||
13 | mjames | 98 | static void MX_USART2_UART_Init(void); |
2 | mjames | 99 | static void MX_USART1_UART_Init(void); |
100 | |||
101 | /* USER CODE BEGIN PFP */ |
||
102 | /* Private function prototypes -----------------------------------------------*/ |
||
103 | |||
9 | mjames | 104 | /* USER CODE END PFP */ |
7 | mjames | 105 | |
9 | mjames | 106 | /* USER CODE BEGIN 0 */ |
7 | mjames | 107 | |
9 | mjames | 108 | void plx_sendword(int x) { |
109 | PutCharSerial(&uc1, ((x) >> 6) & 0x3F); |
||
110 | PutCharSerial(&uc1, (x) & 0x3F); |
||
111 | } |
||
2 | mjames | 112 | |
17 | mjames | 113 | void init_ADC_filter() |
114 | { |
||
115 | int i; |
||
116 | for(i=0;i<6;i++) |
||
117 | { |
||
118 | FILT_Samples[i] = 0; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | void filter_ADC_samples() |
||
123 | { |
||
124 | int i; |
||
125 | for(i=0;i<6;i++) |
||
126 | { |
||
127 | FILT_Samples[i] += (ADC_Samples[i] * Scale - FILT_Samples[i]) / 2; |
||
128 | } |
||
129 | |||
130 | |||
131 | |||
132 | } |
||
133 | |||
134 | |||
9 | mjames | 135 | void ProcessRPM(int instance) { |
136 | // compute the timer values |
||
137 | // snapshot timers |
||
138 | unsigned long RPM_Pulsewidth; |
||
139 | unsigned long RPM_Count_Val; |
||
140 | __disable_irq(); // copy the counter value |
||
141 | RPM_Count_Val = RPM_Count; |
||
142 | __enable_irq(); |
||
143 | // do calculations |
||
144 | // if there is only one entry, cannot get difference |
||
145 | if (RPM_Count_Latch != RPM_Count_Val) { |
||
146 | while (1) { |
||
147 | unsigned int base_time; |
||
148 | unsigned int new_time; |
||
149 | // if we are at N-1, stop. |
||
150 | unsigned int next_count = RPM_Count_Latch + 1; |
||
151 | if (next_count == RPM_SAMPLES) { |
||
152 | next_count = 0; |
||
153 | } |
||
154 | if (next_count == RPM_Count_Val) { |
||
155 | break; |
||
156 | } |
||
157 | base_time = RPM_Time[RPM_Count_Latch]; |
||
158 | new_time = RPM_Time[next_count]; |
||
159 | RPM_Count_Latch = next_count; |
||
160 | if (new_time > base_time) { |
||
161 | RPM_Pulsewidth = new_time - base_time; // not wrapped |
||
162 | } else { |
||
13 | mjames | 163 | RPM_Pulsewidth = new_time - base_time + 65536; // deal with wrapping |
9 | mjames | 164 | } |
2 | mjames | 165 | |
9 | mjames | 166 | RPM_Diff += RPM_Pulsewidth; |
167 | // need to check if this is a long pulse. If it is, keep the answer |
||
168 | if (RPM_Pulsewidth > BREAKER_MIN) { |
||
169 | RPM_Pulsecount++; // count one pulse |
||
170 | RPM_FilteredWidth += RPM_Diff; // add its width to the accumulator |
||
171 | RPM_Diff = 0; // reset accumulator of all the narrow widths |
||
172 | } |
||
173 | } |
||
174 | |||
175 | } |
||
176 | |||
177 | if (RPM_Pulsecount > 0) { |
||
178 | |||
17 | mjames | 179 | |
180 | |||
181 | |||
9 | mjames | 182 | // now have time for N pulses in clocks |
183 | // need to scale by 19.55: one unit is 19.55 RPM |
||
184 | // 1Hz is 60 RPM |
||
17 | mjames | 185 | float new_RPM = (30.0 / 19.55 * RPM_Pulsecount * RPM_COUNT_RATE) |
186 | / (RPM_FilteredWidth) + 0.5; |
||
187 | |||
188 | Coded_RPM += (new_RPM * Scale - Coded_RPM)/4; |
||
189 | |||
9 | mjames | 190 | #if !defined MY_DEBUG |
191 | // reset here unless we want to debug |
||
192 | RPM_Pulsecount = 0; |
||
193 | RPM_FilteredWidth = 0; |
||
194 | #endif |
||
195 | } |
||
196 | |||
17 | mjames | 197 | // send the current RPM *calculation |
9 | mjames | 198 | plx_sendword(PLX_RPM); |
199 | PutCharSerial(&uc1, instance); |
||
17 | mjames | 200 | plx_sendword(Coded_RPM/Scale); |
9 | mjames | 201 | } |
202 | |||
11 | mjames | 203 | uint8_t CHT_Timer = 0; |
9 | mjames | 204 | |
205 | // this uses a MAX6675 which is a simple 16 bit read |
||
206 | // SPI is configured for 8 bits so I can use an OLED display if I need it |
||
11 | mjames | 207 | // must wait > 0.22 seconds between conversion attempts as this is the measurement time |
208 | // |
||
9 | mjames | 209 | void ProcessCHT(int instance) |
210 | { |
||
211 | uint8_t buffer[2]; |
||
11 | mjames | 212 | CHT_Timer++; |
213 | if(CHT_Timer >= 3) // every 300 milliseconds |
||
214 | |||
215 | { |
||
216 | CHT_Timer=0; |
||
217 | |||
9 | mjames | 218 | HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_RESET); |
219 | |||
220 | HAL_SPI_Receive(&hspi1, buffer, 2, 2); |
||
221 | |||
17 | mjames | 222 | HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_SET); |
9 | mjames | 223 | |
224 | |||
225 | uint16_t obs = (buffer[0]<<8)| buffer[1]; |
||
226 | |||
227 | uint8_t good = (obs & 4)==0; |
||
228 | if(good) |
||
229 | { |
||
10 | mjames | 230 | Coded_CHT = obs>>5; |
9 | mjames | 231 | } |
10 | mjames | 232 | else |
233 | { |
||
17 | mjames | 234 | Coded_CHT= 1024; // signal fail |
10 | mjames | 235 | } |
11 | mjames | 236 | } |
237 | |||
16 | mjames | 238 | plx_sendword(PLX_X_CHT); |
9 | mjames | 239 | PutCharSerial(&uc1, instance); |
240 | plx_sendword(Coded_CHT); |
||
241 | |||
242 | } |
||
243 | |||
17 | mjames | 244 | // 1023 is 20.00 volts. |
12 | mjames | 245 | void ProcessBatteryVoltage(int instance) |
246 | { |
||
17 | mjames | 247 | float reading = FILT_Samples[instance] * ADC_Scale ; |
248 | reading = reading * 7.8125; // real voltage |
||
249 | reading = reading * 51.15 ; // 1023/20 |
||
250 | |||
251 | |||
12 | mjames | 252 | plx_sendword(PLX_Volts); |
253 | PutCharSerial(&uc1, instance); |
||
17 | mjames | 254 | plx_sendword((uint16_t)reading); |
12 | mjames | 255 | |
256 | |||
257 | |||
258 | } |
||
259 | |||
17 | mjames | 260 | // the MAP sensor is giving us a reading of |
261 | // 4.6 volts for 1019mB or 2.27 volts at the ADC input (resistive divider by 2.016) |
||
262 | // I believe the sensor reads 4.5V at 1000kPa and 0.5V at 0kPa |
||
12 | mjames | 263 | |
17 | mjames | 264 | void ProcessMAP(int instance) |
265 | { |
||
266 | // Using ADC_Samples[3] as the MAP input |
||
267 | float reading = FILT_Samples[3] * ADC_Scale; |
||
268 | reading = reading * 2.016; // real voltage |
||
269 | reading = (reading-0.5) * 1000/ 4; |
||
270 | |||
271 | plx_sendword(PLX_MAP); |
||
272 | PutCharSerial(&uc1, instance); |
||
273 | plx_sendword((uint16_t)reading); |
||
274 | |||
275 | |||
276 | |||
277 | } |
||
278 | |||
279 | // the Oil pressi sensor is giving us a reading of |
||
280 | // 4.5 volts for 100 PSI or 2.25 volts at the ADC input (resistive divider by 2.016) |
||
281 | // I believe the sensor reads 4.5V at 100PSI and 0.5V at 0PSI |
||
282 | // an observation of 1024 is 200PSI, so observation of 512 is 100 PSI. |
||
283 | |||
284 | void ProcessOilPress(int instance) |
||
285 | { |
||
286 | // Using ADC_Samples[2] as the MAP input |
||
287 | float reading = FILT_Samples[2] * ADC_Scale ; |
||
288 | reading = reading * 2.00 ; // real voltage |
||
289 | reading = (reading-0.5) * 512 / 4; // this is 1023 * 100/200 |
||
290 | |||
291 | plx_sendword(PLX_FluidPressure); |
||
292 | PutCharSerial(&uc1, instance); |
||
293 | plx_sendword((uint16_t)reading); |
||
294 | |||
295 | |||
296 | |||
297 | } |
||
298 | |||
299 | |||
300 | |||
16 | mjames | 301 | void ProcessTiming(int instance) |
302 | { |
||
303 | plx_sendword(PLX_Timing); |
||
304 | PutCharSerial(&uc1, instance); |
||
305 | plx_sendword(64-15); // make it negative |
||
306 | } |
||
307 | |||
17 | mjames | 308 | |
309 | |||
2 | mjames | 310 | /* USER CODE END 0 */ |
311 | |||
312 | int main(void) |
||
313 | { |
||
314 | |||
315 | /* USER CODE BEGIN 1 */ |
||
316 | |||
317 | /* USER CODE END 1 */ |
||
318 | |||
319 | /* MCU Configuration----------------------------------------------------------*/ |
||
320 | |||
321 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
||
322 | HAL_Init(); |
||
323 | |||
324 | /* Configure the system clock */ |
||
325 | SystemClock_Config(); |
||
326 | |||
327 | /* Initialize all configured peripherals */ |
||
328 | MX_GPIO_Init(); |
||
6 | mjames | 329 | MX_DMA_Init(); |
2 | mjames | 330 | MX_ADC_Init(); |
331 | MX_SPI1_Init(); |
||
332 | MX_TIM2_Init(); |
||
333 | MX_TIM6_Init(); |
||
13 | mjames | 334 | MX_USART2_UART_Init(); |
2 | mjames | 335 | MX_USART1_UART_Init(); |
336 | |||
337 | /* USER CODE BEGIN 2 */ |
||
13 | mjames | 338 | HAL_MspInit(); |
2 | mjames | 339 | |
13 | mjames | 340 | // Not using HAL USART code |
9 | mjames | 341 | __HAL_RCC_USART1_CLK_ENABLE() |
342 | ; // PLX comms port |
||
343 | __HAL_RCC_USART2_CLK_ENABLE() |
||
344 | ; // Debug comms port |
||
7 | mjames | 345 | /* setup the USART control blocks */ |
346 | init_usart_ctl(&uc1, huart1.Instance); |
||
347 | init_usart_ctl(&uc2, huart2.Instance); |
||
348 | |||
349 | EnableSerialRxInterrupt(&uc1); |
||
350 | EnableSerialRxInterrupt(&uc2); |
||
351 | |||
13 | mjames | 352 | |
353 | HAL_SPI_MspInit(&hspi1); |
||
354 | |||
355 | HAL_ADC_MspInit(&hadc); |
||
14 | mjames | 356 | |
13 | mjames | 357 | HAL_ADC_Start_DMA(&hadc, ADC_Samples, 6); |
358 | |||
14 | mjames | 359 | HAL_ADC_Start_IT(&hadc); |
13 | mjames | 360 | |
361 | HAL_TIM_Base_MspInit(&htim6); |
||
9 | mjames | 362 | HAL_TIM_Base_Start_IT(&htim6); |
13 | mjames | 363 | |
364 | // initialise all the STMCubeMX stuff |
||
365 | HAL_TIM_Base_MspInit(&htim2); |
||
366 | // Start the counter |
||
12 | mjames | 367 | HAL_TIM_Base_Start(&htim2); |
13 | mjames | 368 | // Start the input capture and the interrupt |
369 | HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1); |
||
8 | mjames | 370 | |
17 | mjames | 371 | init_ADC_filter(); |
7 | mjames | 372 | |
2 | mjames | 373 | /* USER CODE END 2 */ |
374 | |||
375 | /* Infinite loop */ |
||
376 | /* USER CODE BEGIN WHILE */ |
||
9 | mjames | 377 | while (1) { |
2 | mjames | 378 | /* USER CODE END WHILE */ |
379 | |||
380 | /* USER CODE BEGIN 3 */ |
||
9 | mjames | 381 | // check to see if we have any incoming data, copy and append if so, if no data then create our own frames. |
382 | int c; |
||
383 | char send = 0; |
||
2 | mjames | 384 | |
9 | mjames | 385 | // poll the input for a stop bit or timeout |
386 | if(PollSerial(&uc1)) |
||
387 | { |
||
388 | c = GetCharSerial(&uc1); |
||
389 | if (c != PLX_Stop) |
||
390 | { |
||
391 | PutCharSerial(&uc1,c); // echo all but the stop bit |
||
392 | } else { // must be a stop character |
||
393 | send = 1; // start our sending process. |
||
394 | } |
||
395 | } |
||
396 | |||
397 | // sort out auto-sending |
||
398 | if (TimerFlag) |
||
399 | { |
||
10 | mjames | 400 | TimerFlag = 0; |
9 | mjames | 401 | if (NoSerialIn) |
402 | { |
||
403 | PutCharSerial(&uc1,PLX_Start); |
||
404 | send = 1; |
||
405 | } |
||
406 | } |
||
407 | if (send) |
||
408 | { |
||
409 | send = 0; |
||
410 | |||
12 | mjames | 411 | uint16_t val; |
13 | mjames | 412 | val = __HAL_TIM_GET_COMPARE(&htim2,TIM_CHANNEL_1); |
12 | mjames | 413 | PutCharSerial(&uc2,(val&31) + 32); |
414 | |||
13 | mjames | 415 | |
416 | |||
12 | mjames | 417 | // send the observations |
17 | mjames | 418 | filter_ADC_samples(); |
9 | mjames | 419 | ProcessRPM(0); |
420 | ProcessCHT(0); |
||
17 | mjames | 421 | ProcessBatteryVoltage(0); // Batt 1 |
422 | ProcessBatteryVoltage(1); // Batt 2 |
||
423 | // ProcessBatteryVoltage(2); // oil pressure |
||
424 | // ProcessBatteryVoltage(3); // MAP |
||
425 | ProcessBatteryVoltage(4); // temperature sensor |
||
426 | // ProcessBatteryVoltage(5); // vrefint |
||
427 | ProcessMAP(0); |
||
428 | ProcessOilPress(0); |
||
16 | mjames | 429 | ProcessTiming(0); |
430 | |||
9 | mjames | 431 | PutCharSerial(&uc1,PLX_Stop); |
432 | } |
||
433 | |||
434 | } |
||
2 | mjames | 435 | /* USER CODE END 3 */ |
436 | |||
437 | } |
||
438 | |||
439 | /** System Clock Configuration |
||
440 | */ |
||
441 | void SystemClock_Config(void) |
||
442 | { |
||
443 | |||
444 | RCC_OscInitTypeDef RCC_OscInitStruct; |
||
445 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
||
446 | |||
447 | __HAL_RCC_PWR_CLK_ENABLE(); |
||
448 | |||
449 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
||
450 | |||
451 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
||
452 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
||
453 | RCC_OscInitStruct.HSICalibrationValue = 16; |
||
454 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
||
455 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
||
456 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6; |
||
457 | RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3; |
||
458 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
||
459 | { |
||
460 | Error_Handler(); |
||
461 | } |
||
462 | |||
463 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
||
464 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
||
465 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
||
466 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
||
467 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
||
468 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
||
469 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) |
||
470 | { |
||
471 | Error_Handler(); |
||
472 | } |
||
473 | |||
474 | HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); |
||
475 | |||
476 | HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); |
||
477 | |||
478 | /* SysTick_IRQn interrupt configuration */ |
||
479 | HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); |
||
480 | } |
||
481 | |||
482 | /* ADC init function */ |
||
483 | static void MX_ADC_Init(void) |
||
484 | { |
||
485 | |||
486 | ADC_ChannelConfTypeDef sConfig; |
||
487 | |||
488 | /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) |
||
489 | */ |
||
490 | hadc.Instance = ADC1; |
||
491 | hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; |
||
492 | hadc.Init.Resolution = ADC_RESOLUTION_12B; |
||
493 | hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
||
494 | hadc.Init.ScanConvMode = ADC_SCAN_ENABLE; |
||
495 | hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV; |
||
496 | hadc.Init.LowPowerAutoWait = ADC_AUTOWAIT_DISABLE; |
||
497 | hadc.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE; |
||
498 | hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A; |
||
499 | hadc.Init.ContinuousConvMode = DISABLE; |
||
500 | hadc.Init.NbrOfConversion = 6; |
||
501 | hadc.Init.DiscontinuousConvMode = DISABLE; |
||
502 | hadc.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T6_TRGO; |
||
503 | hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; |
||
14 | mjames | 504 | hadc.Init.DMAContinuousRequests = ENABLE; |
2 | mjames | 505 | if (HAL_ADC_Init(&hadc) != HAL_OK) |
506 | { |
||
507 | Error_Handler(); |
||
508 | } |
||
509 | |||
510 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
511 | */ |
||
6 | mjames | 512 | sConfig.Channel = ADC_CHANNEL_10; |
2 | mjames | 513 | sConfig.Rank = 1; |
17 | mjames | 514 | sConfig.SamplingTime = ADC_SAMPLETIME_384CYCLES; |
2 | mjames | 515 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
516 | { |
||
517 | Error_Handler(); |
||
518 | } |
||
519 | |||
520 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
521 | */ |
||
6 | mjames | 522 | sConfig.Channel = ADC_CHANNEL_11; |
2 | mjames | 523 | sConfig.Rank = 2; |
524 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
525 | { |
||
526 | Error_Handler(); |
||
527 | } |
||
528 | |||
529 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
530 | */ |
||
6 | mjames | 531 | sConfig.Channel = ADC_CHANNEL_12; |
2 | mjames | 532 | sConfig.Rank = 3; |
533 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
534 | { |
||
535 | Error_Handler(); |
||
536 | } |
||
537 | |||
538 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
539 | */ |
||
6 | mjames | 540 | sConfig.Channel = ADC_CHANNEL_13; |
2 | mjames | 541 | sConfig.Rank = 4; |
542 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
543 | { |
||
544 | Error_Handler(); |
||
545 | } |
||
546 | |||
547 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
548 | */ |
||
549 | sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; |
||
550 | sConfig.Rank = 5; |
||
551 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
552 | { |
||
553 | Error_Handler(); |
||
554 | } |
||
555 | |||
556 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
557 | */ |
||
558 | sConfig.Channel = ADC_CHANNEL_VREFINT; |
||
559 | sConfig.Rank = 6; |
||
560 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
561 | { |
||
562 | Error_Handler(); |
||
563 | } |
||
564 | |||
565 | } |
||
566 | |||
567 | /* SPI1 init function */ |
||
568 | static void MX_SPI1_Init(void) |
||
569 | { |
||
570 | |||
571 | hspi1.Instance = SPI1; |
||
572 | hspi1.Init.Mode = SPI_MODE_MASTER; |
||
3 | mjames | 573 | hspi1.Init.Direction = SPI_DIRECTION_2LINES; |
2 | mjames | 574 | hspi1.Init.DataSize = SPI_DATASIZE_8BIT; |
575 | hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; |
||
576 | hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; |
||
577 | hspi1.Init.NSS = SPI_NSS_SOFT; |
||
10 | mjames | 578 | hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; |
2 | mjames | 579 | hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; |
580 | hspi1.Init.TIMode = SPI_TIMODE_DISABLE; |
||
581 | hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
||
582 | hspi1.Init.CRCPolynomial = 10; |
||
583 | if (HAL_SPI_Init(&hspi1) != HAL_OK) |
||
584 | { |
||
585 | Error_Handler(); |
||
586 | } |
||
587 | |||
588 | } |
||
589 | |||
590 | /* TIM2 init function */ |
||
591 | static void MX_TIM2_Init(void) |
||
592 | { |
||
593 | |||
12 | mjames | 594 | TIM_ClockConfigTypeDef sClockSourceConfig; |
2 | mjames | 595 | TIM_MasterConfigTypeDef sMasterConfig; |
596 | TIM_IC_InitTypeDef sConfigIC; |
||
597 | |||
598 | htim2.Instance = TIM2; |
||
599 | htim2.Init.Prescaler = 320; |
||
600 | htim2.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
13 | mjames | 601 | htim2.Init.Period = 65535; |
2 | mjames | 602 | htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
12 | mjames | 603 | if (HAL_TIM_Base_Init(&htim2) != HAL_OK) |
604 | { |
||
605 | Error_Handler(); |
||
606 | } |
||
607 | |||
608 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; |
||
609 | if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) |
||
610 | { |
||
611 | Error_Handler(); |
||
612 | } |
||
613 | |||
2 | mjames | 614 | if (HAL_TIM_IC_Init(&htim2) != HAL_OK) |
615 | { |
||
616 | Error_Handler(); |
||
617 | } |
||
618 | |||
13 | mjames | 619 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; |
2 | mjames | 620 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
621 | if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) |
||
622 | { |
||
623 | Error_Handler(); |
||
624 | } |
||
625 | |||
626 | sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING; |
||
627 | sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI; |
||
628 | sConfigIC.ICPrescaler = TIM_ICPSC_DIV1; |
||
629 | sConfigIC.ICFilter = 0; |
||
630 | if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK) |
||
631 | { |
||
632 | Error_Handler(); |
||
633 | } |
||
634 | |||
635 | } |
||
636 | |||
637 | /* TIM6 init function */ |
||
638 | static void MX_TIM6_Init(void) |
||
639 | { |
||
640 | |||
641 | TIM_MasterConfigTypeDef sMasterConfig; |
||
642 | |||
643 | htim6.Instance = TIM6; |
||
13 | mjames | 644 | htim6.Init.Prescaler = 320; |
2 | mjames | 645 | htim6.Init.CounterMode = TIM_COUNTERMODE_UP; |
13 | mjames | 646 | htim6.Init.Period = 9999; |
2 | mjames | 647 | if (HAL_TIM_Base_Init(&htim6) != HAL_OK) |
648 | { |
||
649 | Error_Handler(); |
||
650 | } |
||
651 | |||
652 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; |
||
653 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
654 | if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK) |
||
655 | { |
||
656 | Error_Handler(); |
||
657 | } |
||
658 | |||
659 | } |
||
660 | |||
661 | /* USART1 init function */ |
||
662 | static void MX_USART1_UART_Init(void) |
||
663 | { |
||
664 | |||
665 | huart1.Instance = USART1; |
||
666 | huart1.Init.BaudRate = 19200; |
||
667 | huart1.Init.WordLength = UART_WORDLENGTH_8B; |
||
668 | huart1.Init.StopBits = UART_STOPBITS_1; |
||
669 | huart1.Init.Parity = UART_PARITY_NONE; |
||
670 | huart1.Init.Mode = UART_MODE_TX_RX; |
||
671 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
672 | huart1.Init.OverSampling = UART_OVERSAMPLING_16; |
||
673 | if (HAL_UART_Init(&huart1) != HAL_OK) |
||
674 | { |
||
675 | Error_Handler(); |
||
676 | } |
||
677 | |||
678 | } |
||
679 | |||
6 | mjames | 680 | /* USART2 init function */ |
681 | static void MX_USART2_UART_Init(void) |
||
682 | { |
||
683 | |||
684 | huart2.Instance = USART2; |
||
685 | huart2.Init.BaudRate = 115200; |
||
686 | huart2.Init.WordLength = UART_WORDLENGTH_8B; |
||
687 | huart2.Init.StopBits = UART_STOPBITS_1; |
||
688 | huart2.Init.Parity = UART_PARITY_NONE; |
||
689 | huart2.Init.Mode = UART_MODE_TX_RX; |
||
690 | huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
691 | huart2.Init.OverSampling = UART_OVERSAMPLING_16; |
||
692 | if (HAL_UART_Init(&huart2) != HAL_OK) |
||
693 | { |
||
694 | Error_Handler(); |
||
695 | } |
||
696 | |||
697 | } |
||
698 | |||
699 | /** |
||
700 | * Enable DMA controller clock |
||
701 | */ |
||
702 | static void MX_DMA_Init(void) |
||
703 | { |
||
704 | /* DMA controller clock enable */ |
||
705 | __HAL_RCC_DMA1_CLK_ENABLE(); |
||
706 | |||
707 | /* DMA interrupt init */ |
||
708 | /* DMA1_Channel1_IRQn interrupt configuration */ |
||
709 | HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); |
||
710 | HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); |
||
711 | |||
712 | } |
||
713 | |||
2 | mjames | 714 | /** Configure pins as |
715 | * Analog |
||
716 | * Input |
||
717 | * Output |
||
718 | * EVENT_OUT |
||
719 | * EXTI |
||
5 | mjames | 720 | * Free pins are configured automatically as Analog (this feature is enabled through |
721 | * the Code Generation settings) |
||
2 | mjames | 722 | */ |
723 | static void MX_GPIO_Init(void) |
||
724 | { |
||
725 | |||
726 | GPIO_InitTypeDef GPIO_InitStruct; |
||
727 | |||
728 | /* GPIO Ports Clock Enable */ |
||
5 | mjames | 729 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
730 | __HAL_RCC_GPIOH_CLK_ENABLE(); |
||
2 | mjames | 731 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
732 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
||
5 | mjames | 733 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
2 | mjames | 734 | |
6 | mjames | 735 | /*Configure GPIO pins : PC13 PC14 PC15 PC6 |
5 | mjames | 736 | PC7 PC8 PC9 PC10 |
737 | PC11 PC12 */ |
||
6 | mjames | 738 | GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_6 |
5 | mjames | 739 | |GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10 |
740 | |GPIO_PIN_11|GPIO_PIN_12; |
||
741 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
||
742 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
743 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
||
2 | mjames | 744 | |
5 | mjames | 745 | /*Configure GPIO pins : PH0 PH1 */ |
746 | GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; |
||
747 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
||
748 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
749 | HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); |
||
3 | mjames | 750 | |
6 | mjames | 751 | /*Configure GPIO pins : PA0 PA1 PA8 PA11 |
7 | mjames | 752 | PA12 */ |
6 | mjames | 753 | GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_8|GPIO_PIN_11 |
7 | mjames | 754 | |GPIO_PIN_12; |
6 | mjames | 755 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
756 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
757 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
||
758 | |||
7 | mjames | 759 | /*Configure GPIO pin : LED_Blink_Pin */ |
760 | GPIO_InitStruct.Pin = LED_Blink_Pin; |
||
2 | mjames | 761 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
762 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
7 | mjames | 763 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
764 | HAL_GPIO_Init(LED_Blink_GPIO_Port, &GPIO_InitStruct); |
||
2 | mjames | 765 | |
3 | mjames | 766 | /*Configure GPIO pins : SPI_NSS1_Pin SPI1CD_Pin */ |
767 | GPIO_InitStruct.Pin = SPI_NSS1_Pin|SPI1CD_Pin; |
||
768 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
||
769 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
770 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
771 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
||
772 | |||
7 | mjames | 773 | /*Configure GPIO pins : SPI_RESET_Pin SPI_NS_Temp_Pin */ |
774 | GPIO_InitStruct.Pin = SPI_RESET_Pin|SPI_NS_Temp_Pin; |
||
3 | mjames | 775 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
776 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
777 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
7 | mjames | 778 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
3 | mjames | 779 | |
13 | mjames | 780 | /*Configure GPIO pins : PB2 PB10 PB11 PB12 |
781 | PB13 PB14 PB15 PB3 |
||
782 | PB4 PB5 PB6 PB7 |
||
783 | PB8 PB9 */ |
||
784 | GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12 |
||
785 | |GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_3 |
||
786 | |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7 |
||
787 | |GPIO_PIN_8|GPIO_PIN_9; |
||
5 | mjames | 788 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
789 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
790 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
||
791 | |||
792 | /*Configure GPIO pin : PD2 */ |
||
793 | GPIO_InitStruct.Pin = GPIO_PIN_2; |
||
794 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
||
795 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
796 | HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); |
||
797 | |||
798 | /*Configure GPIO pin Output Level */ |
||
7 | mjames | 799 | HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin, GPIO_PIN_RESET); |
5 | mjames | 800 | |
801 | /*Configure GPIO pin Output Level */ |
||
7 | mjames | 802 | HAL_GPIO_WritePin(SPI_NSS1_GPIO_Port, SPI_NSS1_Pin, GPIO_PIN_SET); |
5 | mjames | 803 | |
804 | /*Configure GPIO pin Output Level */ |
||
7 | mjames | 805 | HAL_GPIO_WritePin(SPI1CD_GPIO_Port, SPI1CD_Pin, GPIO_PIN_RESET); |
806 | |||
807 | /*Configure GPIO pin Output Level */ |
||
5 | mjames | 808 | HAL_GPIO_WritePin(SPI_RESET_GPIO_Port, SPI_RESET_Pin, GPIO_PIN_RESET); |
809 | |||
7 | mjames | 810 | /*Configure GPIO pin Output Level */ |
811 | HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_SET); |
||
812 | |||
2 | mjames | 813 | } |
814 | |||
815 | /* USER CODE BEGIN 4 */ |
||
816 | |||
817 | /* USER CODE END 4 */ |
||
818 | |||
819 | /** |
||
820 | * @brief This function is executed in case of error occurrence. |
||
821 | * @param None |
||
822 | * @retval None |
||
823 | */ |
||
824 | void Error_Handler(void) |
||
825 | { |
||
826 | /* USER CODE BEGIN Error_Handler */ |
||
9 | mjames | 827 | /* User can add his own implementation to report the HAL error return state */ |
828 | while (1) { |
||
829 | } |
||
2 | mjames | 830 | /* USER CODE END Error_Handler */ |
831 | } |
||
832 | |||
833 | #ifdef USE_FULL_ASSERT |
||
834 | |||
835 | /** |
||
836 | * @brief Reports the name of the source file and the source line number |
||
837 | * where the assert_param error has occurred. |
||
838 | * @param file: pointer to the source file name |
||
839 | * @param line: assert_param error line source number |
||
840 | * @retval None |
||
841 | */ |
||
842 | void assert_failed(uint8_t* file, uint32_t line) |
||
843 | { |
||
844 | /* USER CODE BEGIN 6 */ |
||
9 | mjames | 845 | /* User can add his own implementation to report the file name and line number, |
846 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
||
2 | mjames | 847 | /* USER CODE END 6 */ |
848 | |||
849 | } |
||
850 | |||
851 | #endif |
||
852 | |||
853 | /** |
||
854 | * @} |
||
855 | */ |
||
856 | |||
857 | /** |
||
858 | * @} |
||
859 | */ |
||
860 | |||
861 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |