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