Subversion Repositories EngineBay2

Rev

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