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