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