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