Rev 5 | Rev 10 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /* USER CODE BEGIN Header */ |
2 | /** |
||
4 | mjames | 3 | ****************************************************************************** |
4 | * @file : main.c |
||
5 | * @brief : Main program body |
||
6 | ****************************************************************************** |
||
7 | * @attention |
||
8 | * |
||
9 | * Copyright (c) 2023 STMicroelectronics. |
||
10 | * All rights reserved. |
||
11 | * |
||
12 | * This software is licensed under terms that can be found in the LICENSE file |
||
13 | * in the root directory of this software component. |
||
14 | * If no LICENSE file comes with this software, it is provided AS-IS. |
||
15 | * |
||
16 | ****************************************************************************** |
||
17 | */ |
||
2 | mjames | 18 | /* USER CODE END Header */ |
19 | /* Includes ------------------------------------------------------------------*/ |
||
20 | #include "main.h" |
||
21 | |||
22 | /* Private includes ----------------------------------------------------------*/ |
||
23 | /* USER CODE BEGIN Includes */ |
||
24 | #include "display.h" |
||
25 | #include "bmp280driver.h" |
||
26 | #include "libMisc/fixI2C.h" |
||
27 | #include "libPlx/plx.h" |
||
28 | #include "libSerial/serial.h" |
||
29 | #include "libIgnTiming/timing.h" |
||
30 | #include "libIgnTiming/edis.h" |
||
31 | /* USER CODE END Includes */ |
||
32 | |||
33 | /* Private typedef -----------------------------------------------------------*/ |
||
34 | /* USER CODE BEGIN PTD */ |
||
35 | |||
36 | /* USER CODE END PTD */ |
||
37 | |||
38 | /* Private define ------------------------------------------------------------*/ |
||
39 | /* USER CODE BEGIN PD */ |
||
40 | /* USER CODE END PD */ |
||
41 | |||
42 | /* Private macro -------------------------------------------------------------*/ |
||
43 | /* USER CODE BEGIN PM */ |
||
3 | mjames | 44 | |
2 | mjames | 45 | /* USER CODE END PM */ |
46 | |||
47 | /* Private variables ---------------------------------------------------------*/ |
||
4 | mjames | 48 | CAN_HandleTypeDef hcan; |
2 | mjames | 49 | |
50 | I2C_HandleTypeDef hi2c1; |
||
51 | |||
52 | IWDG_HandleTypeDef hiwdg; |
||
53 | |||
54 | SPI_HandleTypeDef hspi1; |
||
55 | |||
56 | TIM_HandleTypeDef htim1; |
||
57 | TIM_HandleTypeDef htim2; |
||
58 | TIM_HandleTypeDef htim3; |
||
59 | |||
60 | UART_HandleTypeDef huart2; |
||
61 | |||
62 | /* USER CODE BEGIN PV */ |
||
63 | int const T100MS = 100; |
||
64 | // index for our MAP value (there is maybe another in the system) |
||
65 | char ourMAPindex = 0; |
||
66 | |||
67 | // compensated pressure in mb * 100 |
||
68 | uint32_t comp_pres = 0; |
||
69 | // compensated temperature |
||
70 | int32_t comp_temp = -10000; |
||
5 | mjames | 71 | |
72 | int32_t timing = 0; |
||
2 | mjames | 73 | /* USER CODE END PV */ |
74 | |||
75 | /* Private function prototypes -----------------------------------------------*/ |
||
76 | void SystemClock_Config(void); |
||
77 | static void MX_GPIO_Init(void); |
||
78 | static void MX_CAN_Init(void); |
||
79 | static void MX_I2C1_Init(void); |
||
80 | static void MX_TIM1_Init(void); |
||
81 | static void MX_TIM2_Init(void); |
||
82 | static void MX_SPI1_Init(void); |
||
83 | static void MX_USART2_UART_Init(void); |
||
84 | static void MX_TIM3_Init(void); |
||
85 | static void MX_IWDG_Init(void); |
||
86 | /* USER CODE BEGIN PFP */ |
||
87 | |||
9 | mjames | 88 | // send a PLX_SensorInfo structure to the usart. |
89 | void sendInfo(usart_ctl *uc, PLX_SensorInfo *info) |
||
90 | { |
||
91 | for (int i = 0; i < sizeof(PLX_SensorInfo); ++i) |
||
92 | PutCharSerial(uc, info->bytes[i]); |
||
93 | } |
||
94 | |||
2 | mjames | 95 | void processObservations() |
96 | { |
||
97 | // send MAP |
||
98 | PLX_SensorInfo info; |
||
99 | ConvToPLXInstance(ourMAPindex, &info); |
||
100 | ConvToPLXAddr(PLX_MAP, &info); |
||
101 | ConvToPLXReading(ConveriMFDData2Raw(PLX_MAP, PRESSURE_kPa, comp_pres / 1000.0), &info); |
||
9 | mjames | 102 | sendInfo(&uc2, &info); |
5 | mjames | 103 | |
104 | ConvToPLXInstance(0, &info); |
||
105 | ConvToPLXAddr(PLX_Timing, &info); |
||
106 | ConvToPLXReading(ConveriMFDData2Raw(PLX_Timing, 0, timing / TIMING_SCALE), &info); |
||
9 | mjames | 107 | sendInfo(&uc2, &info ); |
2 | mjames | 108 | } |
109 | |||
3 | mjames | 110 | void triggerSAW() |
111 | { |
||
5 | mjames | 112 | // trigger SAW timer, timer 1##pragma endregion |
113 | |||
4 | mjames | 114 | __HAL_TIM_ENABLE(&htim1); |
3 | mjames | 115 | } |
116 | |||
2 | mjames | 117 | /* USER CODE END PFP */ |
118 | |||
119 | /* Private user code ---------------------------------------------------------*/ |
||
120 | /* USER CODE BEGIN 0 */ |
||
121 | |||
122 | /* USER CODE END 0 */ |
||
123 | |||
124 | /** |
||
4 | mjames | 125 | * @brief The application entry point. |
126 | * @retval int |
||
127 | */ |
||
2 | mjames | 128 | int main(void) |
129 | { |
||
130 | /* USER CODE BEGIN 1 */ |
||
131 | |||
132 | /* USER CODE END 1 */ |
||
133 | |||
134 | /* MCU Configuration--------------------------------------------------------*/ |
||
135 | |||
136 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
||
137 | HAL_Init(); |
||
138 | |||
139 | /* USER CODE BEGIN Init */ |
||
140 | |||
141 | /* USER CODE END Init */ |
||
142 | |||
143 | /* Configure the system clock */ |
||
144 | SystemClock_Config(); |
||
145 | |||
146 | /* USER CODE BEGIN SysInit */ |
||
147 | |||
148 | /* USER CODE END SysInit */ |
||
149 | |||
150 | /* Initialize all configured peripherals */ |
||
151 | MX_GPIO_Init(); |
||
152 | MX_CAN_Init(); |
||
153 | MX_I2C1_Init(); |
||
154 | MX_TIM1_Init(); |
||
155 | MX_TIM2_Init(); |
||
156 | MX_SPI1_Init(); |
||
157 | MX_USART2_UART_Init(); |
||
158 | MX_TIM3_Init(); |
||
159 | MX_IWDG_Init(); |
||
160 | /* USER CODE BEGIN 2 */ |
||
5 | mjames | 161 | |
162 | init_usart_ctl(&uc2, &huart2); |
||
163 | |||
2 | mjames | 164 | cc_init(); |
165 | |||
5 | mjames | 166 | HAL_TIM_Base_MspInit(&htim1); |
167 | |||
4 | mjames | 168 | HAL_TIM_Base_Start(&htim1); |
5 | mjames | 169 | HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_1); |
4 | mjames | 170 | |
5 | mjames | 171 | // initialise all the STMCubeMX stuff |
172 | HAL_TIM_Base_MspInit(&htim2); |
||
173 | // Start the counter |
||
174 | HAL_TIM_Base_Start(&htim2); |
||
175 | // Start the input capture and the rising edge interrupt |
||
176 | HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1); |
||
177 | // Start the input capture and the falling edge interrupt |
||
178 | HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2); |
||
179 | |||
4 | mjames | 180 | __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 5); // delay of 5 uS |
181 | |||
2 | mjames | 182 | HAL_I2C_ClearBusyFlagErrata_2_14_7(&hi2c1); |
183 | MX_I2C1_Init(); |
||
184 | init_bmp(&hi2c1); |
||
185 | uint32_t lastTick = HAL_GetTick(); |
||
186 | |||
187 | uint32_t displayOff = lastTick + 10000; |
||
188 | uint8_t intensity = 2; |
||
4 | mjames | 189 | uint32_t timeout = 0; |
2 | mjames | 190 | uint8_t send = 0; // enable sending our PLX data when non zero |
191 | ResetRxBuffer(&uc2); |
||
192 | |||
193 | // used to store data |
||
194 | PLX_SensorInfo info; |
||
195 | int infoCount = -1; |
||
196 | |||
5 | mjames | 197 | // HAL_IWDG_Init(&hiwdg); |
2 | mjames | 198 | /* USER CODE END 2 */ |
199 | |||
200 | /* Infinite loop */ |
||
201 | /* USER CODE BEGIN WHILE */ |
||
202 | while (1) |
||
203 | { |
||
204 | int button = HAL_GPIO_ReadPin(PUSHBUTTON_GPIO_Port, PUSHBUTTON_Pin) == GPIO_PIN_RESET; |
||
205 | |||
206 | if (button) |
||
207 | { |
||
208 | intensity = 2; |
||
209 | displayOff = lastTick + 10000; |
||
210 | } |
||
211 | |||
212 | switch (intensity) |
||
213 | { |
||
214 | case 2: |
||
215 | if (HAL_GetTick() > displayOff) |
||
216 | { |
||
217 | intensity = 1; |
||
218 | displayOff = lastTick + 60000; |
||
219 | } |
||
220 | |||
221 | break; |
||
222 | case 1: |
||
223 | if (HAL_GetTick() > displayOff) |
||
224 | { |
||
5 | mjames | 225 | intensity = 1; // was 0 |
2 | mjames | 226 | } |
227 | default: |
||
228 | break; |
||
229 | } |
||
230 | cc_display(0, intensity); |
||
231 | |||
5 | mjames | 232 | if (HAL_GetTick() - lastTick > T100MS) |
2 | mjames | 233 | { |
234 | lastTick = HAL_GetTick(); |
||
235 | /* Reading the raw data from sensor */ |
||
236 | struct bmp280_uncomp_data ucomp_data; |
||
237 | uint8_t rslt = bmp280_get_uncomp_data(&ucomp_data, &bmp); |
||
238 | |||
239 | if (rslt == 0) |
||
240 | { |
||
241 | uint8_t rslt2 = bmp280_get_comp_pres_32bit(&comp_pres, ucomp_data.uncomp_press, &bmp); |
||
242 | |||
243 | uint8_t rslt3 = bmp280_get_comp_temp_32bit(&comp_temp, ucomp_data.uncomp_temp, &bmp); |
||
4 | mjames | 244 | if (rslt2 == 0 && rslt3 == 0) |
245 | cc_feed_env(comp_pres, comp_temp); |
||
2 | mjames | 246 | } |
247 | |||
5 | mjames | 248 | // compute RPM value, feed to display |
2 | mjames | 249 | |
5 | mjames | 250 | int rpm = CalculateRPM(); |
251 | if (rpm > 0) |
||
252 | { |
||
253 | cc_feed_rpm(rpm); |
||
254 | // compute timing value, feed to display |
||
255 | timing = mapTiming(rpm, 1000 - comp_pres / 100); |
||
256 | cc_feed_timing(timing); |
||
257 | int microsecs = mapTimingToMicroseconds(timing, 0); |
||
258 | __HAL_TIM_SET_AUTORELOAD(&htim1, microsecs + SAW_DELAY); |
||
259 | } |
||
4 | mjames | 260 | } |
2 | mjames | 261 | // Handle PLX |
262 | // poll the input for a stop bit or timeout |
||
263 | if (PollSerial(&uc2)) |
||
264 | { |
||
5 | mjames | 265 | HAL_IWDG_Refresh(&hiwdg); |
2 | mjames | 266 | timeout = HAL_GetTick() + T100MS * 2; |
267 | char c = GetCharSerial(&uc2); |
||
5 | mjames | 268 | |
2 | mjames | 269 | if (c != PLX_Stop) |
270 | { |
||
271 | PutCharSerial(&uc2, c); // echo all but the stop bit |
||
272 | } |
||
273 | else |
||
274 | { // must be a stop character |
||
275 | send = 1; // start our sending process. |
||
276 | } |
||
277 | // look up the i |
||
278 | if (c == PLX_Start) |
||
279 | { |
||
280 | ourMAPindex = 0; |
||
281 | infoCount = 0; |
||
282 | } |
||
283 | else |
||
284 | { |
||
285 | info.bytes[infoCount++] = c; |
||
286 | // process the sensor info field |
||
287 | if (infoCount == sizeof(PLX_SensorInfo)) |
||
288 | { |
||
289 | infoCount = 0; |
||
290 | int addr = ConvPLXAddr(&info); |
||
291 | if (addr == PLX_MAP) |
||
292 | ourMAPindex = info.Instance + 1; |
||
293 | } |
||
294 | } |
||
295 | |||
296 | if (c == PLX_Stop) |
||
297 | infoCount = -1; |
||
298 | } |
||
299 | |||
300 | // sort out auto-sending |
||
301 | if (HAL_GetTick() > timeout) |
||
302 | { |
||
5 | mjames | 303 | PutCharSerial(&uc2, PLX_Start); |
2 | mjames | 304 | timeout = HAL_GetTick() + T100MS; |
305 | send = 1; |
||
306 | } |
||
307 | |||
308 | if (send) |
||
309 | { |
||
310 | send = 0; |
||
311 | |||
312 | // send the observations |
||
313 | processObservations(); |
||
314 | // |
||
315 | PutCharSerial(&uc2, PLX_Stop); |
||
316 | } |
||
317 | |||
318 | /* USER CODE END WHILE */ |
||
319 | |||
320 | /* USER CODE BEGIN 3 */ |
||
321 | HAL_IWDG_Refresh(&hiwdg); |
||
322 | } |
||
323 | /* USER CODE END 3 */ |
||
324 | } |
||
325 | |||
326 | /** |
||
4 | mjames | 327 | * @brief System Clock Configuration |
328 | * @retval None |
||
329 | */ |
||
2 | mjames | 330 | void SystemClock_Config(void) |
331 | { |
||
332 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
||
333 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
||
334 | |||
335 | /** Initializes the RCC Oscillators according to the specified parameters |
||
4 | mjames | 336 | * in the RCC_OscInitTypeDef structure. |
337 | */ |
||
338 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSE; |
||
2 | mjames | 339 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
340 | RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; |
||
341 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
||
342 | RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
||
343 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
||
344 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
||
345 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; |
||
346 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
||
347 | { |
||
348 | Error_Handler(); |
||
349 | } |
||
350 | |||
351 | /** Initializes the CPU, AHB and APB buses clocks |
||
4 | mjames | 352 | */ |
353 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; |
||
2 | mjames | 354 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
355 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
||
356 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; |
||
357 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
||
358 | |||
359 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) |
||
360 | { |
||
361 | Error_Handler(); |
||
362 | } |
||
363 | } |
||
364 | |||
365 | /** |
||
4 | mjames | 366 | * @brief CAN Initialization Function |
367 | * @param None |
||
368 | * @retval None |
||
369 | */ |
||
2 | mjames | 370 | static void MX_CAN_Init(void) |
371 | { |
||
372 | |||
373 | /* USER CODE BEGIN CAN_Init 0 */ |
||
374 | |||
375 | /* USER CODE END CAN_Init 0 */ |
||
376 | |||
377 | /* USER CODE BEGIN CAN_Init 1 */ |
||
378 | |||
379 | /* USER CODE END CAN_Init 1 */ |
||
380 | hcan.Instance = CAN1; |
||
381 | hcan.Init.Prescaler = 18; |
||
382 | hcan.Init.Mode = CAN_MODE_NORMAL; |
||
383 | hcan.Init.SyncJumpWidth = CAN_SJW_1TQ; |
||
384 | hcan.Init.TimeSeg1 = CAN_BS1_3TQ; |
||
385 | hcan.Init.TimeSeg2 = CAN_BS2_4TQ; |
||
386 | hcan.Init.TimeTriggeredMode = DISABLE; |
||
387 | hcan.Init.AutoBusOff = DISABLE; |
||
388 | hcan.Init.AutoWakeUp = DISABLE; |
||
389 | hcan.Init.AutoRetransmission = DISABLE; |
||
390 | hcan.Init.ReceiveFifoLocked = DISABLE; |
||
391 | hcan.Init.TransmitFifoPriority = DISABLE; |
||
392 | if (HAL_CAN_Init(&hcan) != HAL_OK) |
||
393 | { |
||
394 | Error_Handler(); |
||
395 | } |
||
396 | /* USER CODE BEGIN CAN_Init 2 */ |
||
397 | |||
398 | /* USER CODE END CAN_Init 2 */ |
||
399 | } |
||
400 | |||
401 | /** |
||
4 | mjames | 402 | * @brief I2C1 Initialization Function |
403 | * @param None |
||
404 | * @retval None |
||
405 | */ |
||
2 | mjames | 406 | static void MX_I2C1_Init(void) |
407 | { |
||
408 | |||
409 | /* USER CODE BEGIN I2C1_Init 0 */ |
||
410 | |||
411 | /* USER CODE END I2C1_Init 0 */ |
||
412 | |||
413 | /* USER CODE BEGIN I2C1_Init 1 */ |
||
414 | |||
415 | /* USER CODE END I2C1_Init 1 */ |
||
416 | hi2c1.Instance = I2C1; |
||
417 | hi2c1.Init.ClockSpeed = 100000; |
||
418 | hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; |
||
419 | hi2c1.Init.OwnAddress1 = 0; |
||
420 | hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
||
421 | hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; |
||
422 | hi2c1.Init.OwnAddress2 = 0; |
||
423 | hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; |
||
424 | hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; |
||
425 | if (HAL_I2C_Init(&hi2c1) != HAL_OK) |
||
426 | { |
||
427 | Error_Handler(); |
||
428 | } |
||
429 | /* USER CODE BEGIN I2C1_Init 2 */ |
||
430 | |||
431 | /* USER CODE END I2C1_Init 2 */ |
||
432 | } |
||
433 | |||
434 | /** |
||
4 | mjames | 435 | * @brief IWDG Initialization Function |
436 | * @param None |
||
437 | * @retval None |
||
438 | */ |
||
2 | mjames | 439 | static void MX_IWDG_Init(void) |
440 | { |
||
441 | |||
442 | /* USER CODE BEGIN IWDG_Init 0 */ |
||
443 | |||
444 | /* USER CODE END IWDG_Init 0 */ |
||
445 | |||
446 | /* USER CODE BEGIN IWDG_Init 1 */ |
||
447 | |||
448 | /* USER CODE END IWDG_Init 1 */ |
||
449 | hiwdg.Instance = IWDG; |
||
450 | hiwdg.Init.Prescaler = IWDG_PRESCALER_4; |
||
5 | mjames | 451 | hiwdg.Init.Reload = 1000; |
2 | mjames | 452 | if (HAL_IWDG_Init(&hiwdg) != HAL_OK) |
453 | { |
||
454 | Error_Handler(); |
||
455 | } |
||
456 | /* USER CODE BEGIN IWDG_Init 2 */ |
||
457 | |||
458 | /* USER CODE END IWDG_Init 2 */ |
||
459 | } |
||
460 | |||
461 | /** |
||
4 | mjames | 462 | * @brief SPI1 Initialization Function |
463 | * @param None |
||
464 | * @retval None |
||
465 | */ |
||
2 | mjames | 466 | static void MX_SPI1_Init(void) |
467 | { |
||
468 | |||
469 | /* USER CODE BEGIN SPI1_Init 0 */ |
||
470 | |||
471 | /* USER CODE END SPI1_Init 0 */ |
||
472 | |||
473 | /* USER CODE BEGIN SPI1_Init 1 */ |
||
474 | |||
475 | /* USER CODE END SPI1_Init 1 */ |
||
476 | /* SPI1 parameter configuration*/ |
||
477 | hspi1.Instance = SPI1; |
||
478 | hspi1.Init.Mode = SPI_MODE_MASTER; |
||
479 | hspi1.Init.Direction = SPI_DIRECTION_2LINES; |
||
480 | hspi1.Init.DataSize = SPI_DATASIZE_8BIT; |
||
481 | hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; |
||
482 | hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; |
||
483 | hspi1.Init.NSS = SPI_NSS_SOFT; |
||
484 | hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; |
||
485 | hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; |
||
486 | hspi1.Init.TIMode = SPI_TIMODE_DISABLE; |
||
487 | hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
||
488 | hspi1.Init.CRCPolynomial = 10; |
||
489 | if (HAL_SPI_Init(&hspi1) != HAL_OK) |
||
490 | { |
||
491 | Error_Handler(); |
||
492 | } |
||
493 | /* USER CODE BEGIN SPI1_Init 2 */ |
||
494 | |||
495 | /* USER CODE END SPI1_Init 2 */ |
||
496 | } |
||
497 | |||
498 | /** |
||
4 | mjames | 499 | * @brief TIM1 Initialization Function |
500 | * @param None |
||
501 | * @retval None |
||
502 | */ |
||
2 | mjames | 503 | static void MX_TIM1_Init(void) |
504 | { |
||
505 | |||
506 | /* USER CODE BEGIN TIM1_Init 0 */ |
||
507 | |||
508 | /* USER CODE END TIM1_Init 0 */ |
||
509 | |||
510 | TIM_ClockConfigTypeDef sClockSourceConfig = {0}; |
||
511 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
||
512 | TIM_OC_InitTypeDef sConfigOC = {0}; |
||
513 | TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; |
||
514 | |||
515 | /* USER CODE BEGIN TIM1_Init 1 */ |
||
516 | |||
517 | /* USER CODE END TIM1_Init 1 */ |
||
518 | htim1.Instance = TIM1; |
||
519 | htim1.Init.Prescaler = 71; |
||
520 | htim1.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
521 | htim1.Init.Period = 65535; |
||
522 | htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
523 | htim1.Init.RepetitionCounter = 0; |
||
524 | htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
525 | if (HAL_TIM_Base_Init(&htim1) != HAL_OK) |
||
526 | { |
||
527 | Error_Handler(); |
||
528 | } |
||
529 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; |
||
530 | if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK) |
||
531 | { |
||
532 | Error_Handler(); |
||
533 | } |
||
5 | mjames | 534 | if (HAL_TIM_PWM_Init(&htim1) != HAL_OK) |
2 | mjames | 535 | { |
536 | Error_Handler(); |
||
537 | } |
||
538 | if (HAL_TIM_OnePulse_Init(&htim1, TIM_OPMODE_SINGLE) != HAL_OK) |
||
539 | { |
||
540 | Error_Handler(); |
||
541 | } |
||
542 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC1REF; |
||
543 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
544 | if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK) |
||
545 | { |
||
546 | Error_Handler(); |
||
547 | } |
||
5 | mjames | 548 | sConfigOC.OCMode = TIM_OCMODE_PWM1; |
4 | mjames | 549 | sConfigOC.Pulse = SAW_DELAY; |
5 | mjames | 550 | sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW; |
2 | mjames | 551 | sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; |
552 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
||
553 | sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; |
||
554 | sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; |
||
5 | mjames | 555 | if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) |
2 | mjames | 556 | { |
557 | Error_Handler(); |
||
558 | } |
||
559 | sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; |
||
560 | sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; |
||
561 | sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; |
||
562 | sBreakDeadTimeConfig.DeadTime = 0; |
||
563 | sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; |
||
564 | sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; |
||
565 | sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; |
||
566 | if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK) |
||
567 | { |
||
568 | Error_Handler(); |
||
569 | } |
||
570 | /* USER CODE BEGIN TIM1_Init 2 */ |
||
571 | |||
572 | /* USER CODE END TIM1_Init 2 */ |
||
573 | HAL_TIM_MspPostInit(&htim1); |
||
574 | } |
||
575 | |||
576 | /** |
||
4 | mjames | 577 | * @brief TIM2 Initialization Function |
578 | * @param None |
||
579 | * @retval None |
||
580 | */ |
||
2 | mjames | 581 | static void MX_TIM2_Init(void) |
582 | { |
||
583 | |||
584 | /* USER CODE BEGIN TIM2_Init 0 */ |
||
585 | |||
586 | /* USER CODE END TIM2_Init 0 */ |
||
587 | |||
588 | TIM_ClockConfigTypeDef sClockSourceConfig = {0}; |
||
589 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
||
5 | mjames | 590 | TIM_IC_InitTypeDef sConfigIC = {0}; |
2 | mjames | 591 | |
592 | /* USER CODE BEGIN TIM2_Init 1 */ |
||
593 | |||
594 | /* USER CODE END TIM2_Init 1 */ |
||
595 | htim2.Instance = TIM2; |
||
5 | mjames | 596 | htim2.Init.Prescaler = 719; |
2 | mjames | 597 | htim2.Init.CounterMode = TIM_COUNTERMODE_UP; |
5 | mjames | 598 | htim2.Init.Period = 65535; |
2 | mjames | 599 | htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
600 | htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
601 | if (HAL_TIM_Base_Init(&htim2) != HAL_OK) |
||
602 | { |
||
603 | Error_Handler(); |
||
604 | } |
||
605 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; |
||
606 | if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) |
||
607 | { |
||
608 | Error_Handler(); |
||
609 | } |
||
5 | mjames | 610 | if (HAL_TIM_IC_Init(&htim2) != HAL_OK) |
2 | mjames | 611 | { |
612 | Error_Handler(); |
||
613 | } |
||
614 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; |
||
615 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
616 | if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) |
||
617 | { |
||
618 | Error_Handler(); |
||
619 | } |
||
5 | mjames | 620 | sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING; |
621 | sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI; |
||
622 | sConfigIC.ICPrescaler = TIM_ICPSC_DIV1; |
||
623 | sConfigIC.ICFilter = 0; |
||
624 | if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK) |
||
625 | { |
||
626 | Error_Handler(); |
||
627 | } |
||
628 | sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING; |
||
629 | sConfigIC.ICSelection = TIM_ICSELECTION_INDIRECTTI; |
||
630 | if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2) != HAL_OK) |
||
631 | { |
||
632 | Error_Handler(); |
||
633 | } |
||
2 | mjames | 634 | /* USER CODE BEGIN TIM2_Init 2 */ |
635 | |||
636 | /* USER CODE END TIM2_Init 2 */ |
||
637 | } |
||
638 | |||
639 | /** |
||
4 | mjames | 640 | * @brief TIM3 Initialization Function |
641 | * @param None |
||
642 | * @retval None |
||
643 | */ |
||
2 | mjames | 644 | static void MX_TIM3_Init(void) |
645 | { |
||
646 | |||
647 | /* USER CODE BEGIN TIM3_Init 0 */ |
||
648 | |||
649 | /* USER CODE END TIM3_Init 0 */ |
||
650 | |||
651 | TIM_ClockConfigTypeDef sClockSourceConfig = {0}; |
||
652 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
||
653 | |||
654 | /* USER CODE BEGIN TIM3_Init 1 */ |
||
655 | |||
656 | /* USER CODE END TIM3_Init 1 */ |
||
657 | htim3.Instance = TIM3; |
||
658 | htim3.Init.Prescaler = 719; |
||
659 | htim3.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
660 | htim3.Init.Period = 10000; |
||
661 | htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
662 | htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
663 | if (HAL_TIM_Base_Init(&htim3) != HAL_OK) |
||
664 | { |
||
665 | Error_Handler(); |
||
666 | } |
||
667 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; |
||
668 | if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK) |
||
669 | { |
||
670 | Error_Handler(); |
||
671 | } |
||
672 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
||
673 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
674 | if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) |
||
675 | { |
||
676 | Error_Handler(); |
||
677 | } |
||
678 | /* USER CODE BEGIN TIM3_Init 2 */ |
||
679 | |||
680 | /* USER CODE END TIM3_Init 2 */ |
||
681 | } |
||
682 | |||
683 | /** |
||
4 | mjames | 684 | * @brief USART2 Initialization Function |
685 | * @param None |
||
686 | * @retval None |
||
687 | */ |
||
2 | mjames | 688 | static void MX_USART2_UART_Init(void) |
689 | { |
||
690 | |||
691 | /* USER CODE BEGIN USART2_Init 0 */ |
||
692 | |||
693 | /* USER CODE END USART2_Init 0 */ |
||
694 | |||
695 | /* USER CODE BEGIN USART2_Init 1 */ |
||
696 | |||
697 | /* USER CODE END USART2_Init 1 */ |
||
698 | huart2.Instance = USART2; |
||
699 | huart2.Init.BaudRate = 19200; |
||
700 | huart2.Init.WordLength = UART_WORDLENGTH_8B; |
||
701 | huart2.Init.StopBits = UART_STOPBITS_1; |
||
702 | huart2.Init.Parity = UART_PARITY_NONE; |
||
703 | huart2.Init.Mode = UART_MODE_TX_RX; |
||
704 | huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
705 | huart2.Init.OverSampling = UART_OVERSAMPLING_16; |
||
706 | if (HAL_UART_Init(&huart2) != HAL_OK) |
||
707 | { |
||
708 | Error_Handler(); |
||
709 | } |
||
710 | /* USER CODE BEGIN USART2_Init 2 */ |
||
711 | |||
712 | /* USER CODE END USART2_Init 2 */ |
||
713 | } |
||
714 | |||
715 | /** |
||
4 | mjames | 716 | * @brief GPIO Initialization Function |
717 | * @param None |
||
718 | * @retval None |
||
719 | */ |
||
2 | mjames | 720 | static void MX_GPIO_Init(void) |
721 | { |
||
722 | GPIO_InitTypeDef GPIO_InitStruct = {0}; |
||
723 | |||
724 | /* GPIO Ports Clock Enable */ |
||
725 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
||
726 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
||
727 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
||
728 | |||
729 | /*Configure GPIO pin Output Level */ |
||
4 | mjames | 730 | HAL_GPIO_WritePin(GPIOA, SPI1_NSS_Pin | SPI1_RESET_Pin, GPIO_PIN_RESET); |
2 | mjames | 731 | |
732 | /*Configure GPIO pin Output Level */ |
||
4 | mjames | 733 | HAL_GPIO_WritePin(SPI1_CD_GPIO_Port, SPI1_CD_Pin, GPIO_PIN_RESET); |
2 | mjames | 734 | |
4 | mjames | 735 | /*Configure GPIO pins : SPI1_NSS_Pin SPI1_RESET_Pin */ |
736 | GPIO_InitStruct.Pin = SPI1_NSS_Pin | SPI1_RESET_Pin; |
||
2 | mjames | 737 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
738 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
739 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
740 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
||
741 | |||
4 | mjames | 742 | /*Configure GPIO pin : SPI1_CD_Pin */ |
743 | GPIO_InitStruct.Pin = SPI1_CD_Pin; |
||
2 | mjames | 744 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
745 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
746 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
4 | mjames | 747 | HAL_GPIO_Init(SPI1_CD_GPIO_Port, &GPIO_InitStruct); |
2 | mjames | 748 | |
4 | mjames | 749 | /*Configure GPIO pin : PUSHBUTTON_Pin */ |
750 | GPIO_InitStruct.Pin = PUSHBUTTON_Pin; |
||
2 | mjames | 751 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
752 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
||
4 | mjames | 753 | HAL_GPIO_Init(PUSHBUTTON_GPIO_Port, &GPIO_InitStruct); |
2 | mjames | 754 | |
4 | mjames | 755 | /*Configure GPIO pin : dualSpark_Pin */ |
756 | GPIO_InitStruct.Pin = dualSpark_Pin; |
||
2 | mjames | 757 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
758 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
||
4 | mjames | 759 | HAL_GPIO_Init(dualSpark_GPIO_Port, &GPIO_InitStruct); |
2 | mjames | 760 | } |
761 | |||
762 | /* USER CODE BEGIN 4 */ |
||
763 | |||
764 | /* USER CODE END 4 */ |
||
765 | |||
766 | /** |
||
4 | mjames | 767 | * @brief This function is executed in case of error occurrence. |
768 | * @retval None |
||
769 | */ |
||
2 | mjames | 770 | void Error_Handler(void) |
771 | { |
||
772 | /* USER CODE BEGIN Error_Handler_Debug */ |
||
773 | /* User can add his own implementation to report the HAL error return state */ |
||
774 | __disable_irq(); |
||
775 | while (1) |
||
776 | { |
||
777 | } |
||
778 | /* USER CODE END Error_Handler_Debug */ |
||
779 | } |
||
780 | |||
4 | mjames | 781 | #ifdef USE_FULL_ASSERT |
2 | mjames | 782 | /** |
4 | mjames | 783 | * @brief Reports the name of the source file and the source line number |
784 | * where the assert_param error has occurred. |
||
785 | * @param file: pointer to the source file name |
||
786 | * @param line: assert_param error line source number |
||
787 | * @retval None |
||
788 | */ |
||
2 | mjames | 789 | void assert_failed(uint8_t *file, uint32_t line) |
790 | { |
||
791 | /* USER CODE BEGIN 6 */ |
||
792 | /* User can add his own implementation to report the file name and line number, |
||
793 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
||
794 | /* USER CODE END 6 */ |
||
795 | } |
||
796 | #endif /* USE_FULL_ASSERT */ |