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