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