Subversion Repositories EDIS_Ignition

Rev

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