Subversion Repositories EDIS_Ignition

Rev

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