Subversion Repositories EDIS_Ignition

Rev

Rev 12 | Rev 14 | 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 */
12 mjames 45
 
2 mjames 46
/* USER CODE END PM */
47
 
48
/* Private variables ---------------------------------------------------------*/
13 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
/**
13 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
 
12 mjames 202
// dont do this   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;
13 mjames 216
      displayOff = lastTick + 30000;
2 mjames 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
    }
13 mjames 237
    cc_display(0, intensity, 0);
2 mjames 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
12 mjames 253
        comp_pres = 100000;
11 mjames 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
12 mjames 262
      int rpm = 1000;
11 mjames 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
/**
13 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
13 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
13 mjames 369
  */
370
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
371
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
2 mjames 372
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
373
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
374
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
375
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
376
 
377
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
378
  {
379
    Error_Handler();
380
  }
381
}
382
 
383
/**
13 mjames 384
  * @brief CAN Initialization Function
385
  * @param None
386
  * @retval None
387
  */
2 mjames 388
static void MX_CAN_Init(void)
389
{
390
 
391
  /* USER CODE BEGIN CAN_Init 0 */
392
 
393
  /* USER CODE END CAN_Init 0 */
394
 
395
  /* USER CODE BEGIN CAN_Init 1 */
396
 
397
  /* USER CODE END CAN_Init 1 */
398
  hcan.Instance = CAN1;
399
  hcan.Init.Prescaler = 18;
400
  hcan.Init.Mode = CAN_MODE_NORMAL;
401
  hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
402
  hcan.Init.TimeSeg1 = CAN_BS1_3TQ;
403
  hcan.Init.TimeSeg2 = CAN_BS2_4TQ;
404
  hcan.Init.TimeTriggeredMode = DISABLE;
405
  hcan.Init.AutoBusOff = DISABLE;
406
  hcan.Init.AutoWakeUp = DISABLE;
407
  hcan.Init.AutoRetransmission = DISABLE;
408
  hcan.Init.ReceiveFifoLocked = DISABLE;
409
  hcan.Init.TransmitFifoPriority = DISABLE;
410
  if (HAL_CAN_Init(&hcan) != HAL_OK)
411
  {
412
    Error_Handler();
413
  }
414
  /* USER CODE BEGIN CAN_Init 2 */
415
 
416
  /* USER CODE END CAN_Init 2 */
13 mjames 417
 
2 mjames 418
}
419
 
420
/**
13 mjames 421
  * @brief I2C1 Initialization Function
422
  * @param None
423
  * @retval None
424
  */
2 mjames 425
static void MX_I2C1_Init(void)
426
{
427
 
428
  /* USER CODE BEGIN I2C1_Init 0 */
429
 
430
  /* USER CODE END I2C1_Init 0 */
431
 
432
  /* USER CODE BEGIN I2C1_Init 1 */
433
 
434
  /* USER CODE END I2C1_Init 1 */
435
  hi2c1.Instance = I2C1;
436
  hi2c1.Init.ClockSpeed = 100000;
437
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
438
  hi2c1.Init.OwnAddress1 = 0;
439
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
440
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
441
  hi2c1.Init.OwnAddress2 = 0;
442
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
443
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
444
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
445
  {
446
    Error_Handler();
447
  }
448
  /* USER CODE BEGIN I2C1_Init 2 */
449
 
450
  /* USER CODE END I2C1_Init 2 */
13 mjames 451
 
2 mjames 452
}
453
 
454
/**
13 mjames 455
  * @brief IWDG Initialization Function
456
  * @param None
457
  * @retval None
458
  */
2 mjames 459
static void MX_IWDG_Init(void)
460
{
461
 
462
  /* USER CODE BEGIN IWDG_Init 0 */
463
 
464
  /* USER CODE END IWDG_Init 0 */
465
 
466
  /* USER CODE BEGIN IWDG_Init 1 */
467
 
468
  /* USER CODE END IWDG_Init 1 */
469
  hiwdg.Instance = IWDG;
470
  hiwdg.Init.Prescaler = IWDG_PRESCALER_4;
5 mjames 471
  hiwdg.Init.Reload = 1000;
2 mjames 472
  if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
473
  {
474
    Error_Handler();
475
  }
476
  /* USER CODE BEGIN IWDG_Init 2 */
477
 
478
  /* USER CODE END IWDG_Init 2 */
13 mjames 479
 
2 mjames 480
}
481
 
482
/**
13 mjames 483
  * @brief SPI1 Initialization Function
484
  * @param None
485
  * @retval None
486
  */
2 mjames 487
static void MX_SPI1_Init(void)
488
{
489
 
490
  /* USER CODE BEGIN SPI1_Init 0 */
491
 
492
  /* USER CODE END SPI1_Init 0 */
493
 
494
  /* USER CODE BEGIN SPI1_Init 1 */
495
 
496
  /* USER CODE END SPI1_Init 1 */
497
  /* SPI1 parameter configuration*/
498
  hspi1.Instance = SPI1;
499
  hspi1.Init.Mode = SPI_MODE_MASTER;
500
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
501
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
13 mjames 502
  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
503
  hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
2 mjames 504
  hspi1.Init.NSS = SPI_NSS_SOFT;
505
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
506
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
507
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
508
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
509
  hspi1.Init.CRCPolynomial = 10;
510
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
511
  {
512
    Error_Handler();
513
  }
514
  /* USER CODE BEGIN SPI1_Init 2 */
515
 
516
  /* USER CODE END SPI1_Init 2 */
13 mjames 517
 
2 mjames 518
}
519
 
520
/**
13 mjames 521
  * @brief TIM1 Initialization Function
522
  * @param None
523
  * @retval None
524
  */
2 mjames 525
static void MX_TIM1_Init(void)
526
{
527
 
528
  /* USER CODE BEGIN TIM1_Init 0 */
529
 
530
  /* USER CODE END TIM1_Init 0 */
531
 
532
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
533
  TIM_MasterConfigTypeDef sMasterConfig = {0};
534
  TIM_OC_InitTypeDef sConfigOC = {0};
535
  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
536
 
537
  /* USER CODE BEGIN TIM1_Init 1 */
538
 
539
  /* USER CODE END TIM1_Init 1 */
540
  htim1.Instance = TIM1;
541
  htim1.Init.Prescaler = 71;
542
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
543
  htim1.Init.Period = 65535;
544
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
545
  htim1.Init.RepetitionCounter = 0;
546
  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
547
  if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
548
  {
549
    Error_Handler();
550
  }
551
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
552
  if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
553
  {
554
    Error_Handler();
555
  }
5 mjames 556
  if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
2 mjames 557
  {
558
    Error_Handler();
559
  }
560
  if (HAL_TIM_OnePulse_Init(&htim1, TIM_OPMODE_SINGLE) != HAL_OK)
561
  {
562
    Error_Handler();
563
  }
564
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC1REF;
565
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
566
  if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
567
  {
568
    Error_Handler();
569
  }
5 mjames 570
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
4 mjames 571
  sConfigOC.Pulse = SAW_DELAY;
5 mjames 572
  sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
2 mjames 573
  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
574
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
575
  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
576
  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
5 mjames 577
  if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
2 mjames 578
  {
579
    Error_Handler();
580
  }
581
  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
582
  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
583
  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
584
  sBreakDeadTimeConfig.DeadTime = 0;
585
  sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
586
  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
587
  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
588
  if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
589
  {
590
    Error_Handler();
591
  }
592
  /* USER CODE BEGIN TIM1_Init 2 */
593
 
594
  /* USER CODE END TIM1_Init 2 */
595
  HAL_TIM_MspPostInit(&htim1);
13 mjames 596
 
2 mjames 597
}
598
 
599
/**
13 mjames 600
  * @brief TIM2 Initialization Function
601
  * @param None
602
  * @retval None
603
  */
2 mjames 604
static void MX_TIM2_Init(void)
605
{
606
 
607
  /* USER CODE BEGIN TIM2_Init 0 */
608
 
609
  /* USER CODE END TIM2_Init 0 */
610
 
611
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
612
  TIM_MasterConfigTypeDef sMasterConfig = {0};
5 mjames 613
  TIM_IC_InitTypeDef sConfigIC = {0};
2 mjames 614
 
615
  /* USER CODE BEGIN TIM2_Init 1 */
616
 
617
  /* USER CODE END TIM2_Init 1 */
618
  htim2.Instance = TIM2;
5 mjames 619
  htim2.Init.Prescaler = 719;
2 mjames 620
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
5 mjames 621
  htim2.Init.Period = 65535;
2 mjames 622
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
623
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
624
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
625
  {
626
    Error_Handler();
627
  }
628
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
629
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
630
  {
631
    Error_Handler();
632
  }
5 mjames 633
  if (HAL_TIM_IC_Init(&htim2) != HAL_OK)
2 mjames 634
  {
635
    Error_Handler();
636
  }
637
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
638
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
639
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
640
  {
641
    Error_Handler();
642
  }
5 mjames 643
  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
644
  sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
645
  sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
646
  sConfigIC.ICFilter = 0;
647
  if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
648
  {
649
    Error_Handler();
650
  }
651
  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
652
  sConfigIC.ICSelection = TIM_ICSELECTION_INDIRECTTI;
653
  if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2) != HAL_OK)
654
  {
655
    Error_Handler();
656
  }
2 mjames 657
  /* USER CODE BEGIN TIM2_Init 2 */
658
 
659
  /* USER CODE END TIM2_Init 2 */
13 mjames 660
 
2 mjames 661
}
662
 
663
/**
13 mjames 664
  * @brief TIM3 Initialization Function
665
  * @param None
666
  * @retval None
667
  */
2 mjames 668
static void MX_TIM3_Init(void)
669
{
670
 
671
  /* USER CODE BEGIN TIM3_Init 0 */
672
 
673
  /* USER CODE END TIM3_Init 0 */
674
 
675
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
676
  TIM_MasterConfigTypeDef sMasterConfig = {0};
677
 
678
  /* USER CODE BEGIN TIM3_Init 1 */
679
 
680
  /* USER CODE END TIM3_Init 1 */
681
  htim3.Instance = TIM3;
682
  htim3.Init.Prescaler = 719;
683
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
684
  htim3.Init.Period = 10000;
685
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
686
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
687
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
688
  {
689
    Error_Handler();
690
  }
691
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
692
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
693
  {
694
    Error_Handler();
695
  }
696
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
697
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
698
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
699
  {
700
    Error_Handler();
701
  }
702
  /* USER CODE BEGIN TIM3_Init 2 */
703
 
704
  /* USER CODE END TIM3_Init 2 */
13 mjames 705
 
2 mjames 706
}
707
 
708
/**
13 mjames 709
  * @brief USART2 Initialization Function
710
  * @param None
711
  * @retval None
712
  */
2 mjames 713
static void MX_USART2_UART_Init(void)
714
{
715
 
716
  /* USER CODE BEGIN USART2_Init 0 */
717
 
718
  /* USER CODE END USART2_Init 0 */
719
 
720
  /* USER CODE BEGIN USART2_Init 1 */
721
 
722
  /* USER CODE END USART2_Init 1 */
723
  huart2.Instance = USART2;
724
  huart2.Init.BaudRate = 19200;
725
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
726
  huart2.Init.StopBits = UART_STOPBITS_1;
727
  huart2.Init.Parity = UART_PARITY_NONE;
728
  huart2.Init.Mode = UART_MODE_TX_RX;
729
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
730
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
731
  if (HAL_UART_Init(&huart2) != HAL_OK)
732
  {
733
    Error_Handler();
734
  }
735
  /* USER CODE BEGIN USART2_Init 2 */
736
 
737
  /* USER CODE END USART2_Init 2 */
13 mjames 738
 
2 mjames 739
}
740
 
741
/**
13 mjames 742
  * @brief GPIO Initialization Function
743
  * @param None
744
  * @retval None
745
  */
2 mjames 746
static void MX_GPIO_Init(void)
747
{
748
  GPIO_InitTypeDef GPIO_InitStruct = {0};
749
 
750
  /* GPIO Ports Clock Enable */
751
  __HAL_RCC_GPIOD_CLK_ENABLE();
752
  __HAL_RCC_GPIOA_CLK_ENABLE();
753
  __HAL_RCC_GPIOB_CLK_ENABLE();
754
 
755
  /*Configure GPIO pin Output Level */
13 mjames 756
  HAL_GPIO_WritePin(GPIOA, SPI1_NSS_Pin|SPI1_RESET_Pin, GPIO_PIN_RESET);
2 mjames 757
 
758
  /*Configure GPIO pin Output Level */
4 mjames 759
  HAL_GPIO_WritePin(SPI1_CD_GPIO_Port, SPI1_CD_Pin, GPIO_PIN_RESET);
2 mjames 760
 
4 mjames 761
  /*Configure GPIO pins : SPI1_NSS_Pin SPI1_RESET_Pin */
13 mjames 762
  GPIO_InitStruct.Pin = SPI1_NSS_Pin|SPI1_RESET_Pin;
2 mjames 763
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
764
  GPIO_InitStruct.Pull = GPIO_NOPULL;
765
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
766
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
767
 
4 mjames 768
  /*Configure GPIO pin : SPI1_CD_Pin */
769
  GPIO_InitStruct.Pin = SPI1_CD_Pin;
2 mjames 770
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
771
  GPIO_InitStruct.Pull = GPIO_NOPULL;
772
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
4 mjames 773
  HAL_GPIO_Init(SPI1_CD_GPIO_Port, &GPIO_InitStruct);
2 mjames 774
 
4 mjames 775
  /*Configure GPIO pin : PUSHBUTTON_Pin */
776
  GPIO_InitStruct.Pin = PUSHBUTTON_Pin;
2 mjames 777
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
778
  GPIO_InitStruct.Pull = GPIO_PULLUP;
4 mjames 779
  HAL_GPIO_Init(PUSHBUTTON_GPIO_Port, &GPIO_InitStruct);
2 mjames 780
 
4 mjames 781
  /*Configure GPIO pin : dualSpark_Pin */
782
  GPIO_InitStruct.Pin = dualSpark_Pin;
2 mjames 783
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
784
  GPIO_InitStruct.Pull = GPIO_PULLUP;
4 mjames 785
  HAL_GPIO_Init(dualSpark_GPIO_Port, &GPIO_InitStruct);
13 mjames 786
 
2 mjames 787
}
788
 
789
/* USER CODE BEGIN 4 */
790
 
791
/* USER CODE END 4 */
792
 
793
/**
13 mjames 794
  * @brief  This function is executed in case of error occurrence.
795
  * @retval None
796
  */
2 mjames 797
void Error_Handler(void)
798
{
799
  /* USER CODE BEGIN Error_Handler_Debug */
800
  /* User can add his own implementation to report the HAL error return state */
801
  __disable_irq();
802
  while (1)
803
  {
804
  }
805
  /* USER CODE END Error_Handler_Debug */
806
}
807
 
13 mjames 808
#ifdef  USE_FULL_ASSERT
2 mjames 809
/**
13 mjames 810
  * @brief  Reports the name of the source file and the source line number
811
  *         where the assert_param error has occurred.
812
  * @param  file: pointer to the source file name
813
  * @param  line: assert_param error line source number
814
  * @retval None
815
  */
2 mjames 816
void assert_failed(uint8_t *file, uint32_t line)
817
{
818
  /* USER CODE BEGIN 6 */
819
  /* User can add his own implementation to report the file name and line number,
820
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
821
  /* USER CODE END 6 */
822
}
823
#endif /* USE_FULL_ASSERT */