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