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