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