Subversion Repositories EDIS_Ignition

Rev

Rev 18 | Rev 20 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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