Subversion Repositories EngineBay2

Rev

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

Rev Author Line No. Line
38 mjames 1
/* USER CODE BEGIN Header */
2
/**
3
 ******************************************************************************
4
 * @file           : main.c
5
 * @brief          : Main program body
6
 ******************************************************************************
7
 * @attention
8
 *
9
 * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
10
 * All rights reserved.</center></h2>
11
 *
12
 * This software component is licensed by ST under BSD 3-Clause license,
13
 * the "License"; You may not use this file except in compliance with the
14
 * License. You may obtain a copy of the License at:
15
 *                        opensource.org/licenses/BSD-3-Clause
16
 *
17
 ******************************************************************************
18
 */
19
/* USER CODE END Header */
20
/* Includes ------------------------------------------------------------------*/
21
#include "main.h"
22
 
23
/* Private includes ----------------------------------------------------------*/
24
/* USER CODE BEGIN Includes */
49 mjames 25
#include <string.h>
38 mjames 26
#include "libSerial/serial.h"
27
#include "libPLX/plx.h"
50 mjames 28
#include "libPLX/commsLib.h"
38 mjames 29
#include "misc.h"
30
 
48 mjames 31
#include "libIgnTiming/rpm.h"
32
 
38 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 */
46
#define ADC_CHANNELS 7
47
 
39 mjames 48
#define ADC_MAP_CHAN 2
49
 
50
#define ADC_PRESSURE_CHAN 3
51
 
52
#define ADC_REF_CHAN 5
53
 
54
#define ADC_TEMP_CHAN 6
55
 
38 mjames 56
// wait for about 1 second to decide whether or not starter is on
57
 
58
#define STARTER_LIMIT 10
59
 
60
/* USER CODE END PM */
61
 
62
/* Private variables ---------------------------------------------------------*/
46 mjames 63
ADC_HandleTypeDef hadc1;
38 mjames 64
DMA_HandleTypeDef hdma_adc1;
65
 
66
CAN_HandleTypeDef hcan;
67
 
68
SPI_HandleTypeDef hspi1;
69
 
70
TIM_HandleTypeDef htim2;
71
TIM_HandleTypeDef htim3;
72
TIM_HandleTypeDef htim4;
73
 
74
UART_HandleTypeDef huart1;
75
 
76
/* USER CODE BEGIN PV */
77
 
78
volatile char TimerFlag = 0;
79
 
80
volatile char NoSerialInCTR = 0; // Missing characters coming in on USART1
81
volatile char NoSerialIn = 0;
82
 
83
// storage for ADC
45 mjames 84
uint16_t ADC_Samples[ADC_CHANNELS] = {[0 ... ADC_CHANNELS - 1] = 0};
38 mjames 85
 
45 mjames 86
uint32_t FILT_Samples[ADC_CHANNELS] = {[0 ... ADC_CHANNELS - 1] = 0}; // filtered ADC samples * Scale
38 mjames 87
 
39 mjames 88
#define NOM_VREF 3.3
89
// initial ADC vref
45 mjames 90
float adc_vref = NOM_VREF;
39 mjames 91
 
92
// internal bandgap voltage reference
45 mjames 93
const float STM32REF = 1.2; // 1.2V typical
39 mjames 94
 
95
// scale factor initially assuming
45 mjames 96
float ADC_Scale = 1 / (Scale * 4096) * NOM_VREF;
39 mjames 97
 
38 mjames 98
unsigned int Coded_RPM = 0;
99
unsigned int Coded_CHT = 0;
100
 
42 mjames 101
uint32_t PowerTempTimer;
38 mjames 102
 
103
uint16_t Starter_Debounce = 0;
104
 
105
/* USER CODE END PV */
106
 
107
/* Private function prototypes -----------------------------------------------*/
108
void SystemClock_Config(void);
109
static void MX_GPIO_Init(void);
110
static void MX_DMA_Init(void);
111
static void MX_ADC1_Init(void);
112
static void MX_CAN_Init(void);
113
static void MX_SPI1_Init(void);
114
static void MX_TIM2_Init(void);
115
static void MX_TIM3_Init(void);
116
static void MX_TIM4_Init(void);
117
static void MX_USART1_UART_Init(void);
118
/* USER CODE BEGIN PFP */
119
 
120
/* USER CODE END PFP */
121
 
122
/* Private user code ---------------------------------------------------------*/
123
/* USER CODE BEGIN 0 */
124
 
45 mjames 125
void plx_sendword(int x)
38 mjames 126
{
45 mjames 127
  PutCharSerial(&uc1, ((x) >> 6) & 0x3F);
128
  PutCharSerial(&uc1, (x)&0x3F);
38 mjames 129
}
130
 
45 mjames 131
void filter_ADC_samples()
38 mjames 132
{
133
  int i;
134
  for (i = 0; i < ADC_CHANNELS; i++)
45 mjames 135
  {
136
    FILT_Samples[i] += (ADC_Samples[i] * Scale - FILT_Samples[i]) / 2;
137
  }
38 mjames 138
}
139
 
39 mjames 140
/****!
141
 * @brief this reads the reference voltage within the STM32L151
142
 * Powers up reference voltage and temperature sensor, waits 3mS  and takes reading
143
 * Requires that the ADC be powered up
144
 */
145
 
45 mjames 146
void CalibrateADC(void)
39 mjames 147
{
45 mjames 148
  float adc_val = FILT_Samples[ADC_REF_CHAN]; // as set up in device config
39 mjames 149
 
45 mjames 150
  float adc_vref = STM32REF * (4096.0 * Scale) / adc_val; // the estimate for checking
39 mjames 151
 
45 mjames 152
  ADC_Scale = 1 / (Scale * 4096) * adc_vref;
39 mjames 153
}
154
 
49 mjames 155
void ProcessRPM(void)
38 mjames 156
{
48 mjames 157
  static unsigned int Coded_RPM = 0;
158
  int32_t rpm = CalculateRPM();
159
  if (rpm >= 0)
160
    Coded_RPM = rpm / 19.55;
46 mjames 161
 
45 mjames 162
  // send the current RPM *calculation
163
  plx_sendword(PLX_RPM);
50 mjames 164
  PutCharSerial(&uc1, libPLXgetNextInstance(PLX_RPM));
45 mjames 165
  plx_sendword(Coded_RPM / Scale);
38 mjames 166
}
167
 
168
// this uses a MAX6675 which is a simple 16 bit read
169
// SPI is configured for 8 bits so I can use an OLED display if I need it
170
// must wait > 0.22 seconds between conversion attempts as this is the measurement time
171
//
172
 
173
FunctionalState CHT_Enable = ENABLE;
174
 
175
#define CORR 3
176
 
45 mjames 177
uint16_t Temp_Observations[NUM_SPI_TEMP_SENS] = {[0 ... NUM_SPI_TEMP_SENS - 1] = 0};
38 mjames 178
 
42 mjames 179
/// \param item The array index to send
180
/// \param type the code to use for this observation
49 mjames 181
void ProcessTemp(char item, enum PLX_Observations type)
38 mjames 182
{
42 mjames 183
  if (item > NUM_SPI_TEMP_SENS)
184
    return;
45 mjames 185
  plx_sendword(type);
50 mjames 186
  PutCharSerial(&uc1, libPLXgetNextInstance(type));
45 mjames 187
  plx_sendword(Temp_Observations[(int)item]);
38 mjames 188
}
189
 
42 mjames 190
/// \brief Reset the temperature chip select system
191
void resetTempCS(void)
192
{
45 mjames 193
  HAL_GPIO_WritePin(SPI_CS_D_GPIO_Port, SPI_CS_D_Pin, GPIO_PIN_SET);
194
  HAL_GPIO_WritePin(SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin,
195
                    GPIO_PIN_SET);
42 mjames 196
 
45 mjames 197
  for (int i = 0; i < 8; i++)
198
  {
199
    HAL_GPIO_WritePin(SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin,
200
                      GPIO_PIN_RESET);
201
    HAL_GPIO_WritePin(SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin,
202
                      GPIO_PIN_SET);
203
  }
42 mjames 204
 
45 mjames 205
  // prepare for selecting next pin
206
  HAL_GPIO_WritePin(SPI_CS_D_GPIO_Port, SPI_CS_D_Pin, GPIO_PIN_RESET);
42 mjames 207
}
208
 
209
void nextTempCS(void)
210
{
45 mjames 211
  HAL_GPIO_WritePin(SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin,
212
                    GPIO_PIN_RESET);
213
  HAL_GPIO_WritePin(SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin,
214
                    GPIO_PIN_SET);
215
  HAL_GPIO_WritePin(SPI_CS_D_GPIO_Port, SPI_CS_D_Pin, GPIO_PIN_SET);
42 mjames 216
}
217
 
45 mjames 218
void EnableTempSensors(FunctionalState state)
38 mjames 219
 
220
{
221
  GPIO_InitTypeDef GPIO_InitStruct;
222
 
223
  CHT_Enable = state;
224
 
225
  /* enable SPI in live mode : assume it and its GPIOs are already initialised in SPI mode */
226
  if (state == ENABLE)
45 mjames 227
  {
228
    HAL_GPIO_WritePin(ENA_AUX_5V_GPIO_Port, ENA_AUX_5V_Pin, GPIO_PIN_SET);
38 mjames 229
 
45 mjames 230
    resetTempCS();
42 mjames 231
 
45 mjames 232
    /* put the SPI pins back into SPI AF mode */
233
    GPIO_InitStruct.Pin = SPI1_MOSI_Pin | SPI1_MISO_Pin | SPI1_SCK_Pin;
234
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
235
    GPIO_InitStruct.Pull = GPIO_NOPULL;
236
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
237
    HAL_GPIO_Init(SPI1_SCK_GPIO_Port, &GPIO_InitStruct);
238
  }
38 mjames 239
  else
45 mjames 240
  {
241
    /*  Power down the SPI interface taking signals all low */
242
    HAL_GPIO_WritePin(ENA_AUX_5V_GPIO_Port, ENA_AUX_5V_Pin, GPIO_PIN_RESET);
38 mjames 243
 
45 mjames 244
    HAL_GPIO_WritePin(SPI1_SCK_GPIO_Port,
245
                      SPI1_MOSI_Pin | SPI1_MISO_Pin | SPI1_SCK_Pin,
246
                      GPIO_PIN_RESET);
38 mjames 247
 
45 mjames 248
    /* put the SPI pins back into GPIO mode */
249
    GPIO_InitStruct.Pin = SPI1_MOSI_Pin | SPI1_MISO_Pin | SPI1_SCK_Pin;
250
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
251
    GPIO_InitStruct.Pull = GPIO_NOPULL;
252
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
253
    HAL_GPIO_Init(SPI1_SCK_GPIO_Port, &GPIO_InitStruct);
254
  }
38 mjames 255
}
256
 
257
// 1023 is 20.00 volts.
49 mjames 258
/// \param item - used to lookup the index of the local reading
259
void ProcessBatteryVoltage(int item)
38 mjames 260
{
49 mjames 261
  float reading = FILT_Samples[item] * ADC_Scale;
38 mjames 262
  reading = reading * 7.8125; // real voltage
45 mjames 263
  reading = reading * 51.15;  // PLC scaling =  1023/20
38 mjames 264
 
45 mjames 265
  plx_sendword(PLX_Volts);
50 mjames 266
  PutCharSerial(&uc1, libPLXgetNextInstance(PLX_Volts));
45 mjames 267
  plx_sendword((uint16_t)reading);
38 mjames 268
}
269
 
49 mjames 270
void ProcessCPUTemperature(void)
38 mjames 271
{
45 mjames 272
  // this is defined in the STM32F103 reference manual . #
39 mjames 273
  // V25 = 1.43 volts
274
  // Avg_slope = 4.3mV /degree C
275
  // temperature = {(V25 - VSENSE) / Avg_Slope} + 25
38 mjames 276
 
277
  /* get the ADC reading corresponding to ADC channel 16 after turning on the ADC */
278
 
39 mjames 279
  float temp_val = FILT_Samples[ADC_TEMP_CHAN] * ADC_Scale;
38 mjames 280
  /* renormalise temperature value to account for different ADC Vref  : normalise to that which we would get for a 3000mV reference */
45 mjames 281
  temp_val = (1.43 - temp_val) / 4.3e-3 + 25;
38 mjames 282
 
45 mjames 283
  int32_t result = temp_val;
38 mjames 284
 
45 mjames 285
  //  int32_t result = 800 * ((int32_t) temp_val - TS_CAL30);
286
  //  result = result / (TS_CAL110 - TS_CAL30) + 300;
39 mjames 287
 
45 mjames 288
  plx_sendword(PLX_FluidTemp);
50 mjames 289
  PutCharSerial(&uc1, libPLXgetNextInstance(PLX_FluidTemp));
45 mjames 290
  plx_sendword(result);
38 mjames 291
}
292
 
293
// the MAP sensor is giving us a reading of
294
// 4.6 volts for 1019mB or 2.27 volts at the ADC input (resistive divider by 2.016)
295
// I believe the sensor reads  4.5V at 1000kPa and 0.5V at  0kPa
296
// Calibration is a bit off
297
// Real   Displayed
298
// 989    968
299
// 994.1    986
300
// 992.3  984
301
 
49 mjames 302
void ProcessMAP(void)
38 mjames 303
{
45 mjames 304
  // Using ADC_Samples[3] as the MAP input
39 mjames 305
  float reading = FILT_Samples[ADC_MAP_CHAN] * ADC_Scale;
45 mjames 306
  reading = reading * 2.016; // real voltage
38 mjames 307
  // values computed from slope / intercept of map.ods
45 mjames 308
  // reading = (reading) * 56.23 + 743.2; // do not assume 0.5 volt offset : reading from 0 to 4.5 instead of 0.5 to 4.5
38 mjames 309
  // using a pressure gauge.
45 mjames 310
  reading = (reading)*150 + 326;
38 mjames 311
 
45 mjames 312
  plx_sendword(PLX_MAP);
50 mjames 313
  PutCharSerial(&uc1, libPLXgetNextInstance(PLX_MAP));
45 mjames 314
  plx_sendword((uint16_t)reading);
38 mjames 315
}
316
 
317
// the Oil pressi sensor is giving us a reading of
318
// 4.5 volts for 100 PSI or  2.25 volts at the ADC input (resistive divider by 2.016)
319
// I believe the sensor reads  4.5V at 100PSI and 0.5V at  0PSI
320
// an observation of 1024 is 200PSI, so observation of 512 is 100 PSI.
321
 
49 mjames 322
void ProcessOilPress(void)
38 mjames 323
{
45 mjames 324
  // Using ADC_Samples[2] as the MAP input
39 mjames 325
  float reading = FILT_Samples[ADC_PRESSURE_CHAN] * ADC_Scale;
45 mjames 326
  reading = reading * 2.00;            // real voltage
327
  reading = (reading - 0.5) * 512 / 4; // this is 1023 * 100/200
38 mjames 328
 
45 mjames 329
  plx_sendword(PLX_FluidPressure);
50 mjames 330
  PutCharSerial(&uc1, libPLXgetNextInstance(PLX_FluidPressure));
45 mjames 331
  plx_sendword((uint16_t)reading);
38 mjames 332
}
333
 
49 mjames 334
void ProcessTiming(void)
38 mjames 335
{
45 mjames 336
  plx_sendword(PLX_Timing);
50 mjames 337
  PutCharSerial(&uc1, libPLXgetNextInstance(PLX_Timing));
45 mjames 338
  plx_sendword(64 - 15); // make it negative
38 mjames 339
}
340
 
50 mjames 341
void libPLXcallbackResetSerialTimeout()
342
{
343
  resetSerialTimeout();
344
  TimerFlag = 0;
345
}
346
 
347
void libPLXcallbackSendUserData()
348
{
349
  // send the observations
350
  ProcessRPM();
351
  ProcessTemp(0, PLX_X_CHT);
352
  ProcessTemp(1, PLX_X_CHT);
353
  ProcessTemp(2, PLX_AIT);
354
  ProcessTemp(3, PLX_AIT);
355
  ProcessBatteryVoltage(0); // Batt 1
356
  ProcessBatteryVoltage(1); // Batt 2
357
  ProcessCPUTemperature();  //  built in temperature sensor
358
 
359
  ProcessMAP();
360
  ProcessOilPress();
361
 
362
  PutCharSerial(&uc1, PLX_Stop);
363
}
38 mjames 364
/* USER CODE END 0 */
365
 
366
/**
46 mjames 367
 * @brief  The application entry point.
368
 * @retval int
369
 */
38 mjames 370
int main(void)
371
{
372
  /* USER CODE BEGIN 1 */
373
 
374
  /* USER CODE END 1 */
375
 
376
  /* MCU Configuration--------------------------------------------------------*/
377
 
378
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
379
  HAL_Init();
380
 
381
  /* USER CODE BEGIN Init */
382
 
383
  /* USER CODE END Init */
384
 
385
  /* Configure the system clock */
386
  SystemClock_Config();
387
 
388
  /* USER CODE BEGIN SysInit */
389
 
390
  /* USER CODE END SysInit */
391
 
392
  /* Initialize all configured peripherals */
393
  MX_GPIO_Init();
394
  MX_DMA_Init();
395
  MX_ADC1_Init();
396
  MX_CAN_Init();
397
  MX_SPI1_Init();
398
  MX_TIM2_Init();
399
  MX_TIM3_Init();
400
  MX_TIM4_Init();
401
  MX_USART1_UART_Init();
402
  /* USER CODE BEGIN 2 */
45 mjames 403
  HAL_MspInit();
38 mjames 404
 
405
  // Not using HAL USART code
45 mjames 406
  __HAL_RCC_USART1_CLK_ENABLE(); // PLX comms port
38 mjames 407
  /* setup the USART control blocks */
45 mjames 408
  init_usart_ctl(&uc1, &huart1);
38 mjames 409
 
45 mjames 410
  EnableSerialRxInterrupt(&uc1);
38 mjames 411
 
45 mjames 412
  HAL_SPI_MspInit(&hspi1);
38 mjames 413
 
45 mjames 414
  HAL_ADC_MspInit(&hadc1);
38 mjames 415
 
45 mjames 416
  HAL_ADC_Start_DMA(&hadc1, (uint32_t *)ADC_Samples, ADC_CHANNELS);
38 mjames 417
 
45 mjames 418
  HAL_ADC_Start_IT(&hadc1);
38 mjames 419
 
45 mjames 420
  HAL_TIM_Base_MspInit(&htim4);
421
  HAL_TIM_Base_Start_IT(&htim4);
38 mjames 422
 
423
  // initialise all the STMCubeMX stuff
45 mjames 424
  HAL_TIM_Base_MspInit(&htim2);
38 mjames 425
  // Start the counter
45 mjames 426
  HAL_TIM_Base_Start(&htim2);
41 mjames 427
  // Start the input capture and the rising edge interrupt
45 mjames 428
  HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
41 mjames 429
  // Start the input capture and the falling edge interrupt
45 mjames 430
  HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2);
38 mjames 431
 
45 mjames 432
  HAL_TIM_Base_MspInit(&htim3);
38 mjames 433
  __HAL_TIM_ENABLE_IT(&htim3, TIM_IT_UPDATE);
45 mjames 434
  uint32_t Ticks = HAL_GetTick() + 100;
38 mjames 435
  int CalCounter = 0;
436
 
45 mjames 437
  PowerTempTimer = HAL_GetTick() + 1000; /* wait 10 seconds before powering up the CHT sensor */
38 mjames 438
 
45 mjames 439
  ResetRxBuffer(&uc1);
49 mjames 440
 
50 mjames 441
  resetPLX();
38 mjames 442
  /* USER CODE END 2 */
443
 
444
  /* Infinite loop */
445
  /* USER CODE BEGIN WHILE */
446
  while (1)
45 mjames 447
  {
38 mjames 448
    /* USER CODE END WHILE */
449
 
450
    /* USER CODE BEGIN 3 */
451
 
45 mjames 452
    if (HAL_GetTick() > Ticks)
453
    {
454
      Ticks += 100;
455
      filter_ADC_samples();
456
      // delay to calibrate ADC
457
      if (CalCounter < 1000)
458
      {
459
        CalCounter += 100;
460
      }
38 mjames 461
 
45 mjames 462
      if (CalCounter == 900)
463
      {
464
        CalibrateADC();
465
      }
466
    }
467
    /* when the starter motor is on then power down the CHT sensors as they seem to fail */
38 mjames 468
 
45 mjames 469
    if (HAL_GPIO_ReadPin(STARTER_ON_GPIO_Port, STARTER_ON_Pin) == GPIO_PIN_RESET)
470
    {
471
      if (Starter_Debounce < STARTER_LIMIT)
472
      {
473
        Starter_Debounce++;
474
      }
475
    }
476
    else
477
    {
478
      if (Starter_Debounce > 0)
479
      {
480
        Starter_Debounce--;
481
      }
482
    }
38 mjames 483
 
45 mjames 484
    if (Starter_Debounce == STARTER_LIMIT)
485
    {
486
      EnableTempSensors(DISABLE);
487
      PowerTempTimer = HAL_GetTick() + 1000;
488
    }
489
    else
490
    /* if the PowerTempTimer is set then wait for it to timeout, then power up CHT */
491
    {
492
      if ((PowerTempTimer > 0) && (HAL_GetTick() > PowerTempTimer))
493
      {
494
        EnableTempSensors(ENABLE);
495
        PowerTempTimer = 0;
496
      }
497
    }
38 mjames 498
 
45 mjames 499
    // check to see if we have any incoming data, copy and append if so, if no data then create our own frames.
38 mjames 500
 
50 mjames 501
    // poll the input data and produce automatic output if the timer expires and no serial input data
502
    libPLXpollData(&uc1, TimerFlag && NoSerialIn);
45 mjames 503
  }
38 mjames 504
 
505
  /* USER CODE END 3 */
506
}
507
 
508
/**
46 mjames 509
 * @brief System Clock Configuration
510
 * @retval None
511
 */
38 mjames 512
void SystemClock_Config(void)
513
{
514
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
515
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
516
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
517
 
518
  /** Initializes the RCC Oscillators according to the specified parameters
46 mjames 519
   * in the RCC_OscInitTypeDef structure.
520
   */
38 mjames 521
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
522
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
523
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
524
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
525
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
526
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
527
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
528
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
529
  {
530
    Error_Handler();
531
  }
45 mjames 532
 
38 mjames 533
  /** Initializes the CPU, AHB and APB buses clocks
46 mjames 534
   */
535
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
38 mjames 536
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
537
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
538
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
539
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
540
 
541
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
542
  {
543
    Error_Handler();
544
  }
545
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
546
  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
547
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
548
  {
549
    Error_Handler();
550
  }
551
}
552
 
553
/**
46 mjames 554
 * @brief ADC1 Initialization Function
555
 * @param None
556
 * @retval None
557
 */
38 mjames 558
static void MX_ADC1_Init(void)
559
{
560
 
561
  /* USER CODE BEGIN ADC1_Init 0 */
562
 
563
  /* USER CODE END ADC1_Init 0 */
564
 
565
  ADC_ChannelConfTypeDef sConfig = {0};
566
 
567
  /* USER CODE BEGIN ADC1_Init 1 */
568
 
569
  /* USER CODE END ADC1_Init 1 */
45 mjames 570
 
38 mjames 571
  /** Common config
46 mjames 572
   */
38 mjames 573
  hadc1.Instance = ADC1;
574
  hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
575
  hadc1.Init.ContinuousConvMode = DISABLE;
576
  hadc1.Init.DiscontinuousConvMode = DISABLE;
577
  hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T3_TRGO;
578
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
579
  hadc1.Init.NbrOfConversion = 7;
580
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
581
  {
582
    Error_Handler();
583
  }
45 mjames 584
 
38 mjames 585
  /** Configure Regular Channel
46 mjames 586
   */
38 mjames 587
  sConfig.Channel = ADC_CHANNEL_0;
588
  sConfig.Rank = ADC_REGULAR_RANK_1;
39 mjames 589
  sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
38 mjames 590
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
591
  {
592
    Error_Handler();
593
  }
45 mjames 594
 
38 mjames 595
  /** Configure Regular Channel
46 mjames 596
   */
38 mjames 597
  sConfig.Channel = ADC_CHANNEL_1;
598
  sConfig.Rank = ADC_REGULAR_RANK_2;
599
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
600
  {
601
    Error_Handler();
602
  }
45 mjames 603
 
38 mjames 604
  /** Configure Regular Channel
46 mjames 605
   */
38 mjames 606
  sConfig.Channel = ADC_CHANNEL_2;
607
  sConfig.Rank = ADC_REGULAR_RANK_3;
608
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
609
  {
610
    Error_Handler();
611
  }
45 mjames 612
 
38 mjames 613
  /** Configure Regular Channel
46 mjames 614
   */
38 mjames 615
  sConfig.Channel = ADC_CHANNEL_3;
616
  sConfig.Rank = ADC_REGULAR_RANK_4;
617
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
618
  {
619
    Error_Handler();
620
  }
45 mjames 621
 
38 mjames 622
  /** Configure Regular Channel
46 mjames 623
   */
39 mjames 624
  sConfig.Channel = ADC_CHANNEL_4;
38 mjames 625
  sConfig.Rank = ADC_REGULAR_RANK_5;
626
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
627
  {
628
    Error_Handler();
629
  }
45 mjames 630
 
38 mjames 631
  /** Configure Regular Channel
46 mjames 632
   */
38 mjames 633
  sConfig.Channel = ADC_CHANNEL_VREFINT;
634
  sConfig.Rank = ADC_REGULAR_RANK_6;
635
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
636
  {
637
    Error_Handler();
638
  }
45 mjames 639
 
38 mjames 640
  /** Configure Regular Channel
46 mjames 641
   */
39 mjames 642
  sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
38 mjames 643
  sConfig.Rank = ADC_REGULAR_RANK_7;
644
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
645
  {
646
    Error_Handler();
647
  }
648
  /* USER CODE BEGIN ADC1_Init 2 */
649
 
650
  /* USER CODE END ADC1_Init 2 */
651
}
652
 
653
/**
46 mjames 654
 * @brief CAN Initialization Function
655
 * @param None
656
 * @retval None
657
 */
38 mjames 658
static void MX_CAN_Init(void)
659
{
660
 
661
  /* USER CODE BEGIN CAN_Init 0 */
662
 
663
  /* USER CODE END CAN_Init 0 */
664
 
665
  /* USER CODE BEGIN CAN_Init 1 */
666
 
667
  /* USER CODE END CAN_Init 1 */
668
  hcan.Instance = CAN1;
669
  hcan.Init.Prescaler = 16;
670
  hcan.Init.Mode = CAN_MODE_NORMAL;
671
  hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
672
  hcan.Init.TimeSeg1 = CAN_BS1_1TQ;
673
  hcan.Init.TimeSeg2 = CAN_BS2_1TQ;
674
  hcan.Init.TimeTriggeredMode = DISABLE;
675
  hcan.Init.AutoBusOff = DISABLE;
676
  hcan.Init.AutoWakeUp = DISABLE;
677
  hcan.Init.AutoRetransmission = DISABLE;
678
  hcan.Init.ReceiveFifoLocked = DISABLE;
679
  hcan.Init.TransmitFifoPriority = DISABLE;
680
  if (HAL_CAN_Init(&hcan) != HAL_OK)
681
  {
682
    Error_Handler();
683
  }
684
  /* USER CODE BEGIN CAN_Init 2 */
685
 
686
  /* USER CODE END CAN_Init 2 */
687
}
688
 
689
/**
46 mjames 690
 * @brief SPI1 Initialization Function
691
 * @param None
692
 * @retval None
693
 */
38 mjames 694
static void MX_SPI1_Init(void)
695
{
696
 
697
  /* USER CODE BEGIN SPI1_Init 0 */
698
 
699
  /* USER CODE END SPI1_Init 0 */
700
 
701
  /* USER CODE BEGIN SPI1_Init 1 */
702
 
703
  /* USER CODE END SPI1_Init 1 */
704
  /* SPI1 parameter configuration*/
705
  hspi1.Instance = SPI1;
706
  hspi1.Init.Mode = SPI_MODE_MASTER;
707
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
708
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
709
  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
710
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
711
  hspi1.Init.NSS = SPI_NSS_SOFT;
41 mjames 712
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
38 mjames 713
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
714
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
715
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
716
  hspi1.Init.CRCPolynomial = 10;
717
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
718
  {
719
    Error_Handler();
720
  }
721
  /* USER CODE BEGIN SPI1_Init 2 */
722
 
723
  /* USER CODE END SPI1_Init 2 */
724
}
725
 
726
/**
46 mjames 727
 * @brief TIM2 Initialization Function
728
 * @param None
729
 * @retval None
730
 */
38 mjames 731
static void MX_TIM2_Init(void)
732
{
733
 
734
  /* USER CODE BEGIN TIM2_Init 0 */
735
 
736
  /* USER CODE END TIM2_Init 0 */
737
 
738
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
739
  TIM_MasterConfigTypeDef sMasterConfig = {0};
740
  TIM_IC_InitTypeDef sConfigIC = {0};
741
 
742
  /* USER CODE BEGIN TIM2_Init 1 */
743
 
744
  /* USER CODE END TIM2_Init 1 */
745
  htim2.Instance = TIM2;
41 mjames 746
  htim2.Init.Prescaler = 719;
38 mjames 747
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
748
  htim2.Init.Period = 65535;
749
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
750
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
751
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
752
  {
753
    Error_Handler();
754
  }
755
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
756
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
757
  {
758
    Error_Handler();
759
  }
760
  if (HAL_TIM_IC_Init(&htim2) != HAL_OK)
761
  {
762
    Error_Handler();
763
  }
764
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
765
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
766
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
767
  {
768
    Error_Handler();
769
  }
770
  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
771
  sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
772
  sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
773
  sConfigIC.ICFilter = 15;
774
  if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
775
  {
776
    Error_Handler();
777
  }
41 mjames 778
  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
779
  sConfigIC.ICSelection = TIM_ICSELECTION_INDIRECTTI;
780
  sConfigIC.ICFilter = 0;
781
  if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2) != HAL_OK)
782
  {
783
    Error_Handler();
784
  }
38 mjames 785
  /* USER CODE BEGIN TIM2_Init 2 */
786
 
787
  /* USER CODE END TIM2_Init 2 */
788
}
789
 
790
/**
46 mjames 791
 * @brief TIM3 Initialization Function
792
 * @param None
793
 * @retval None
794
 */
38 mjames 795
static void MX_TIM3_Init(void)
796
{
797
 
798
  /* USER CODE BEGIN TIM3_Init 0 */
799
 
800
  /* USER CODE END TIM3_Init 0 */
801
 
802
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
803
  TIM_MasterConfigTypeDef sMasterConfig = {0};
804
  TIM_OC_InitTypeDef sConfigOC = {0};
805
 
806
  /* USER CODE BEGIN TIM3_Init 1 */
807
 
808
  /* USER CODE END TIM3_Init 1 */
809
  htim3.Instance = TIM3;
41 mjames 810
  htim3.Init.Prescaler = 719;
38 mjames 811
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
41 mjames 812
  htim3.Init.Period = 199;
38 mjames 813
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
814
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
815
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
816
  {
817
    Error_Handler();
818
  }
819
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
820
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
821
  {
822
    Error_Handler();
823
  }
824
  if (HAL_TIM_OC_Init(&htim3) != HAL_OK)
825
  {
826
    Error_Handler();
827
  }
828
  if (HAL_TIM_OnePulse_Init(&htim3, TIM_OPMODE_SINGLE) != HAL_OK)
829
  {
830
    Error_Handler();
831
  }
832
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC1;
833
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
834
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
835
  {
836
    Error_Handler();
837
  }
838
  sConfigOC.OCMode = TIM_OCMODE_TIMING;
41 mjames 839
  sConfigOC.Pulse = 198;
38 mjames 840
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
841
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
842
  if (HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
843
  {
844
    Error_Handler();
845
  }
846
  /* USER CODE BEGIN TIM3_Init 2 */
847
 
848
  /* USER CODE END TIM3_Init 2 */
849
}
850
 
851
/**
46 mjames 852
 * @brief TIM4 Initialization Function
853
 * @param None
854
 * @retval None
855
 */
38 mjames 856
static void MX_TIM4_Init(void)
857
{
858
 
859
  /* USER CODE BEGIN TIM4_Init 0 */
860
 
861
  /* USER CODE END TIM4_Init 0 */
862
 
863
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
864
  TIM_MasterConfigTypeDef sMasterConfig = {0};
865
 
866
  /* USER CODE BEGIN TIM4_Init 1 */
867
 
868
  /* USER CODE END TIM4_Init 1 */
869
  htim4.Instance = TIM4;
41 mjames 870
  htim4.Init.Prescaler = 719;
38 mjames 871
  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
872
  htim4.Init.Period = 9999;
873
  htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
874
  htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
875
  if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
876
  {
877
    Error_Handler();
878
  }
879
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
880
  if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
881
  {
882
    Error_Handler();
883
  }
884
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
885
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
886
  if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
887
  {
888
    Error_Handler();
889
  }
890
  /* USER CODE BEGIN TIM4_Init 2 */
891
 
892
  /* USER CODE END TIM4_Init 2 */
893
}
894
 
895
/**
46 mjames 896
 * @brief USART1 Initialization Function
897
 * @param None
898
 * @retval None
899
 */
38 mjames 900
static void MX_USART1_UART_Init(void)
901
{
902
 
903
  /* USER CODE BEGIN USART1_Init 0 */
904
 
905
  /* USER CODE END USART1_Init 0 */
906
 
907
  /* USER CODE BEGIN USART1_Init 1 */
908
 
909
  /* USER CODE END USART1_Init 1 */
910
  huart1.Instance = USART1;
911
  huart1.Init.BaudRate = 19200;
912
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
913
  huart1.Init.StopBits = UART_STOPBITS_1;
914
  huart1.Init.Parity = UART_PARITY_NONE;
915
  huart1.Init.Mode = UART_MODE_TX_RX;
916
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
917
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
918
  if (HAL_UART_Init(&huart1) != HAL_OK)
919
  {
920
    Error_Handler();
921
  }
922
  /* USER CODE BEGIN USART1_Init 2 */
923
 
924
  /* USER CODE END USART1_Init 2 */
925
}
926
 
927
/**
46 mjames 928
 * Enable DMA controller clock
929
 */
38 mjames 930
static void MX_DMA_Init(void)
931
{
932
 
933
  /* DMA controller clock enable */
934
  __HAL_RCC_DMA1_CLK_ENABLE();
935
 
936
  /* DMA interrupt init */
937
  /* DMA1_Channel1_IRQn interrupt configuration */
938
  HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
939
  HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
940
}
941
 
942
/**
46 mjames 943
 * @brief GPIO Initialization Function
944
 * @param None
945
 * @retval None
946
 */
38 mjames 947
static void MX_GPIO_Init(void)
948
{
949
  GPIO_InitTypeDef GPIO_InitStruct = {0};
950
 
951
  /* GPIO Ports Clock Enable */
952
  __HAL_RCC_GPIOC_CLK_ENABLE();
953
  __HAL_RCC_GPIOD_CLK_ENABLE();
954
  __HAL_RCC_GPIOA_CLK_ENABLE();
955
  __HAL_RCC_GPIOB_CLK_ENABLE();
956
 
957
  /*Configure GPIO pin Output Level */
958
  HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin, GPIO_PIN_RESET);
959
 
960
  /*Configure GPIO pin Output Level */
46 mjames 961
  HAL_GPIO_WritePin(GPIOB, SPI_CS_Clk_Pin | SPI_CS_D_Pin | ENA_AUX_5V_Pin, GPIO_PIN_RESET);
38 mjames 962
 
963
  /*Configure GPIO pin : LED_Blink_Pin */
964
  GPIO_InitStruct.Pin = LED_Blink_Pin;
965
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
966
  GPIO_InitStruct.Pull = GPIO_NOPULL;
967
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
968
  HAL_GPIO_Init(LED_Blink_GPIO_Port, &GPIO_InitStruct);
969
 
43 mjames 970
  /*Configure GPIO pins : SPI_CS_Clk_Pin SPI_CS_D_Pin ENA_AUX_5V_Pin */
46 mjames 971
  GPIO_InitStruct.Pin = SPI_CS_Clk_Pin | SPI_CS_D_Pin | ENA_AUX_5V_Pin;
38 mjames 972
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
973
  GPIO_InitStruct.Pull = GPIO_NOPULL;
974
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
975
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
976
 
977
  /*Configure GPIO pin : STARTER_ON_Pin */
978
  GPIO_InitStruct.Pin = STARTER_ON_Pin;
979
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
980
  GPIO_InitStruct.Pull = GPIO_NOPULL;
981
  HAL_GPIO_Init(STARTER_ON_GPIO_Port, &GPIO_InitStruct);
982
}
983
 
984
/* USER CODE BEGIN 4 */
985
 
986
/* USER CODE END 4 */
987
 
988
/**
46 mjames 989
 * @brief  This function is executed in case of error occurrence.
990
 * @retval None
991
 */
38 mjames 992
void Error_Handler(void)
993
{
994
  /* USER CODE BEGIN Error_Handler_Debug */
45 mjames 995
  /* User can add his own implementation to report the HAL error return state */
38 mjames 996
 
997
  /* USER CODE END Error_Handler_Debug */
998
}
999
 
46 mjames 1000
#ifdef USE_FULL_ASSERT
38 mjames 1001
/**
46 mjames 1002
 * @brief  Reports the name of the source file and the source line number
1003
 *         where the assert_param error has occurred.
1004
 * @param  file: pointer to the source file name
1005
 * @param  line: assert_param error line source number
1006
 * @retval None
1007
 */
38 mjames 1008
void assert_failed(uint8_t *file, uint32_t line)
1009
{
1010
  /* USER CODE BEGIN 6 */
1011
  /* User can add his own implementation to report the file name and line number,
1012
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
1013
  /* USER CODE END 6 */
1014
}
1015
#endif /* USE_FULL_ASSERT */