Subversion Repositories DashDisplay

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
50 mjames 1
/* USER CODE BEGIN Header */
2 mjames 2
/**
52 mjames 3
 ******************************************************************************
4
 * @file           : main.c
5
 * @brief          : Main program body
6
 ******************************************************************************
7
 * @attention
8
 *
9
 * <h2><center>&copy; Copyright (c) 2020 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
 */
50 mjames 19
/* USER CODE END Header */
2 mjames 20
/* Includes ------------------------------------------------------------------*/
50 mjames 21
#include "main.h"
2 mjames 22
 
50 mjames 23
/* Private includes ----------------------------------------------------------*/
2 mjames 24
/* USER CODE BEGIN Includes */
50 mjames 25
 
26
#include "libPLX/plx.h"
27
#include "libSerial/serial.H"
28
#include "libSmallPrintf/small_printf.h"
4 mjames 29
#include "switches.h"
2 mjames 30
 
31
/* USER CODE END Includes */
32
 
50 mjames 33
/* Private typedef -----------------------------------------------------------*/
34
/* USER CODE BEGIN PTD */
35
 
36
/* USER CODE END PTD */
37
 
38
/* Private define ------------------------------------------------------------*/
39
/* USER CODE BEGIN PD */
40
/* USER CODE END PD */
41
 
42
/* Private macro -------------------------------------------------------------*/
43
/* USER CODE BEGIN PM */
44
 
45
/* USER CODE END PM */
46
 
2 mjames 47
/* Private variables ---------------------------------------------------------*/
48
SPI_HandleTypeDef hspi1;
49
 
50 mjames 50
TIM_HandleTypeDef htim2;
44 mjames 51
TIM_HandleTypeDef htim3;
52
TIM_HandleTypeDef htim9;
53
 
3 mjames 54
UART_HandleTypeDef huart1;
2 mjames 55
UART_HandleTypeDef huart2;
23 mjames 56
UART_HandleTypeDef huart3;
2 mjames 57
 
58
/* USER CODE BEGIN PV */
59
/* Private variables ---------------------------------------------------------*/
60
 
50 mjames 61
context_t contexts[MAX_DISPLAYS];
62
 
24 mjames 63
/* timeout when the ignition is switched off */
64
#define IGNITION_OFF_TIMEOUT 30000UL
65
 
52 mjames 66
#define LOGGER_INTERVAL 500UL
14 mjames 67
 
50 mjames 68
const int DialTimeout = 50; // about 20 seconds after twiddle, save the dial position.
18 mjames 69
 
50 mjames 70
uint16_t dial_nvram[MAX_DISPLAYS] __attribute__((section(".NVRAM_Data")));
14 mjames 71
 
50 mjames 72
data_t Data;
7 mjames 73
int Max[MAXRDG];
74
int Min[MAXRDG];
75
int PLXItems;
24 mjames 76
 
27 mjames 77
uint32_t Latch_Timer = IGNITION_OFF_TIMEOUT;
24 mjames 78
 
2 mjames 79
/* USER CODE END PV */
80
 
81
/* Private function prototypes -----------------------------------------------*/
52 mjames 82
void
83
SystemClock_Config (void);
84
static void
85
MX_GPIO_Init (void);
86
static void
87
MX_SPI1_Init (void);
88
static void
89
MX_USART1_UART_Init (void);
90
static void
91
MX_USART2_UART_Init (void);
92
static void
93
MX_USART3_UART_Init (void);
94
static void
95
MX_TIM3_Init (void);
96
static void
97
MX_TIM9_Init (void);
98
static void
99
MX_TIM2_Init (void);
2 mjames 100
/* USER CODE BEGIN PFP */
101
 
7 mjames 102
// the dial is the switch number we are using.
103
// suppress is the ItemIndex we wish to suppress on this display
52 mjames 104
int
105
DisplayCurrent (int dial, int suppress)
7 mjames 106
{
50 mjames 107
  if (PLXItems == 0)
108
    return -1;
14 mjames 109
 
50 mjames 110
  int itemIndex = dial_pos[dial] % PLXItems;
2 mjames 111
 
52 mjames 112
  return cc_display (dial, itemIndex, suppress);
50 mjames 113
}
30 mjames 114
 
53 mjames 115
void
116
setBaud (usart_ctl *ctl, uint32_t baud)
117
{
118
  ctl->handle->Init.BaudRate = baud;
119
  __disable_irq ();
120
  HAL_UART_Init (ctl->handle);
121
  __enable_irq ();
122
}
123
 
124
void
125
sendString (usart_ctl *ctl, char *string, int length)
126
{
127
  int i;
128
  for (i = 0; i < length; i++)
129
    PutCharSerial (ctl, string[i]);
130
 
131
}
132
 
133
 
134
// this code doesnt work so it leaves speed as 9600.
135
void
136
initModule (usart_ctl *ctl, uint32_t baudRate)
137
{
138
  char initBuf[30];
139
  // switch to command mode
140
  HAL_GPIO_WritePin (BT_BUTTON_GPIO_Port, BT_BUTTON_Pin, GPIO_PIN_RESET);
141
  HAL_Delay (500);
142
  int initLen = small_sprintf (initBuf, "AT+UART=%d,1,2\n", baudRate);
143
  setBaud (ctl, 38400);
144
  sendString (ctl, initBuf, initLen);
145
  TxWaitEmpty (ctl);
146
  // switch back to normal comms at new baud rate
147
 
148
  HAL_GPIO_WritePin (BT_BUTTON_GPIO_Port, BT_BUTTON_Pin, GPIO_PIN_SET);
149
  setBaud (ctl, baudRate);
150
  HAL_Delay (100);
151
 
152
}
153
 
50 mjames 154
/* USER CODE END PFP */
14 mjames 155
 
50 mjames 156
/* Private user code ---------------------------------------------------------*/
157
/* USER CODE BEGIN 0 */
14 mjames 158
 
7 mjames 159
/* USER CODE END 0 */
2 mjames 160
 
50 mjames 161
/**
52 mjames 162
 * @brief  The application entry point.
163
 * @retval int
164
 */
165
int
166
main (void)
7 mjames 167
{
16 mjames 168
  /* USER CODE BEGIN 1 */
50 mjames 169
  __HAL_RCC_SPI1_CLK_ENABLE()
170
  ;
171
  __HAL_RCC_USART1_CLK_ENABLE()
172
  ; // PLX main port
173
  __HAL_RCC_USART2_CLK_ENABLE()
174
  ; // debug port
175
  __HAL_RCC_USART3_CLK_ENABLE ()
176
  ; // Bluetooth port
2 mjames 177
 
50 mjames 178
  __HAL_RCC_TIM3_CLK_ENABLE();
2 mjames 179
 
50 mjames 180
  __HAL_RCC_TIM9_CLK_ENABLE();
23 mjames 181
 
16 mjames 182
  /* USER CODE END 1 */
2 mjames 183
 
50 mjames 184
  /* MCU Configuration--------------------------------------------------------*/
6 mjames 185
 
16 mjames 186
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
52 mjames 187
  HAL_Init ();
2 mjames 188
 
50 mjames 189
  /* USER CODE BEGIN Init */
190
 
191
  /* USER CODE END Init */
192
 
16 mjames 193
  /* Configure the system clock */
52 mjames 194
  SystemClock_Config ();
2 mjames 195
 
50 mjames 196
  /* USER CODE BEGIN SysInit */
197
 
198
  /* USER CODE END SysInit */
199
 
16 mjames 200
  /* Initialize all configured peripherals */
52 mjames 201
  MX_GPIO_Init ();
202
  MX_SPI1_Init ();
203
  MX_USART1_UART_Init ();
204
  MX_USART2_UART_Init ();
205
  MX_USART3_UART_Init ();
206
  MX_TIM3_Init ();
207
  MX_TIM9_Init ();
208
  MX_TIM2_Init ();
16 mjames 209
  /* USER CODE BEGIN 2 */
2 mjames 210
 
50 mjames 211
  /* Turn on USART1 IRQ */
52 mjames 212
  HAL_NVIC_SetPriority (USART1_IRQn, 2, 0);
213
  HAL_NVIC_EnableIRQ (USART1_IRQn);
4 mjames 214
 
50 mjames 215
  /* Turn on USART2 IRQ  */
52 mjames 216
  HAL_NVIC_SetPriority (USART2_IRQn, 4, 0);
217
  HAL_NVIC_EnableIRQ (USART2_IRQn);
2 mjames 218
 
50 mjames 219
  /* turn on USART3 IRQ */
52 mjames 220
  HAL_NVIC_SetPriority (USART3_IRQn, 4, 0);
221
  HAL_NVIC_EnableIRQ (USART3_IRQn);
4 mjames 222
 
50 mjames 223
  /* setup the USART control blocks */
53 mjames 224
  init_usart_ctl (&uc1, &huart1);
225
  init_usart_ctl (&uc2, &huart2);
226
  init_usart_ctl (&uc3, &huart3);
23 mjames 227
 
52 mjames 228
  EnableSerialRxInterrupt (&uc1);
229
  EnableSerialRxInterrupt (&uc2);
230
  EnableSerialRxInterrupt (&uc3);
23 mjames 231
 
52 mjames 232
  HAL_TIM_Encoder_Start (&htim3, TIM_CHANNEL_ALL);
23 mjames 233
 
52 mjames 234
  HAL_TIM_Encoder_Start (&htim9, TIM_CHANNEL_ALL);
44 mjames 235
 
50 mjames 236
  // Switch handler called on sysTick interrupt.
52 mjames 237
  InitSwitches ();
2 mjames 238
 
53 mjames 239
  initModule(&uc3,9600);
240
 
241
 
52 mjames 242
  cc_init ();
23 mjames 243
 
50 mjames 244
  int i;
245
  for (i = 0; i < 2; i++)
52 mjames 246
    {
247
      dial_pos[i] = dial_nvram[i];
248
    }
7 mjames 249
 
50 mjames 250
  /* reset the display timeout, latch on power from accessories */
251
  Latch_Timer = IGNITION_OFF_TIMEOUT;
52 mjames 252
  HAL_GPIO_WritePin (POWER_LATCH_GPIO_Port, POWER_LATCH_Pin, GPIO_PIN_RESET);
16 mjames 253
 
254
  /* USER CODE END 2 */
7 mjames 255
 
16 mjames 256
  /* Infinite loop */
257
  /* USER CODE BEGIN WHILE */
52 mjames 258
  while (1)
259
    {
7 mjames 260
 
52 mjames 261
      /* while ignition is on, keep resetting power latch timer */
262
      if (HAL_GPIO_ReadPin (IGNITION_GPIO_Port, IGNITION_Pin) == GPIO_PIN_RESET)
263
        {
264
          Latch_Timer = HAL_GetTick () + IGNITION_OFF_TIMEOUT;
265
        }
266
      else
267
        {
268
          /* if the ignition has been off for a while, then turn off power */
269
          if (HAL_GetTick () > Latch_Timer)
270
            {
271
              HAL_GPIO_WritePin (POWER_LATCH_GPIO_Port, POWER_LATCH_Pin,
272
                                 GPIO_PIN_RESET);
273
            }
274
        }
27 mjames 275
 
52 mjames 276
      uint32_t timeout = 0;  //
27 mjames 277
 
52 mjames 278
      uint32_t nextTick = HAL_GetTick () + LOGGER_INTERVAL;
279
      uint8_t log = 0;
280
      // PLX decoder protocols
281
      char PLXPacket = 0;
282
      for (i = 0; i < MAXRDG; i++)
283
        {
284
          Max[i] = 0;
285
          Min[i] = 0xFFF; // 12 bit max value
286
        }
27 mjames 287
 
52 mjames 288
      int PLXPtr = 0;
24 mjames 289
 
52 mjames 290
      while (1)
291
        {
53 mjames 292
          // Handle the bluetooth pairing / reset function by pressing both buttons.
52 mjames 293
          if ((push_pos[0] == 1) && (push_pos[1] == 1))
294
            {
295
              HAL_GPIO_WritePin (BT_BUTTON_GPIO_Port, BT_BUTTON_Pin,
296
                                 GPIO_PIN_RESET);
297
            }
298
          else
299
            {
300
              HAL_GPIO_WritePin (BT_BUTTON_GPIO_Port, BT_BUTTON_Pin,
301
                                 GPIO_PIN_SET);
302
            }
7 mjames 303
 
52 mjames 304
          uint16_t cc = SerialCharsReceived (&uc1);
305
          int chr;
306
          if (cc == 0)
307
            {
308
              timeout++;
309
              if (timeout % 1000 == 0)
310
                {
311
                  const char msg[] = "Timeout\r\n";
53 mjames 312
                  sendString (&uc3, msg, sizeof(msg));
38 mjames 313
 
52 mjames 314
                }
38 mjames 315
 
52 mjames 316
              if (timeout > 60000)
317
                {
7 mjames 318
 
52 mjames 319
                  // do turn off screen
320
                }
27 mjames 321
 
52 mjames 322
            }
323
          for (chr = 0; chr < cc; chr++)
324
            {
325
              char c = GetCharSerial (&uc1);
7 mjames 326
 
52 mjames 327
              if (c == PLX_Start) // at any time if the start byte appears, reset the pointers
328
                {
329
                  PLXPtr = 0;    // reset the pointer
330
                  PLXPacket = 1;
331
                  timeout = 0;    // Reset the timer
332
                  if (HAL_GetTick () > nextTick)
333
                    {
334
                      nextTick = HAL_GetTick () + LOGGER_INTERVAL;
335
                      log = 1;
336
                    }
337
                  else
338
                    log = 0;
339
                }
340
              else if (c == PLX_Stop)
341
                {
342
                  if (PLXPacket)
343
                    {
344
                      // we can now decode the selected parameter
345
                      PLXItems = PLXPtr / sizeof(PLX_SensorInfo); // total
346
                      // saturate the rotary switch position
9 mjames 347
 
52 mjames 348
                      int DataVal;
349
                      // process min/max
350
                      for (i = 0; i < PLXItems; i++)
351
                        {
23 mjames 352
 
52 mjames 353
                          if (log)
354
                            {
53 mjames 355
                              // Send item to BT
356
                              uint16_t addr = ConvPLX (Data.Sensor[i].AddrH,
357
                                                       Data.Sensor[i].AddrL);
358
                              uint8_t inst = Data.Sensor[i].Instance;
359
                              uint16_t reading = ConvPLX (
360
                                  Data.Sensor[i].ReadingH,
361
                                  Data.Sensor[i].ReadingL);
52 mjames 362
                              char outbuff[100];
30 mjames 363
 
53 mjames 364
                              int cnt = small_sprintf (outbuff, "$LOG,%d,%d,%d",
365
                                                       addr, inst, reading);
4 mjames 366
 
53 mjames 367
                              //checksum
52 mjames 368
                              int ck;
369
                              int sum = 0;
53 mjames 370
                              for (ck = 1; ck < cnt; ck++)
52 mjames 371
                                sum += outbuff[ck];
53 mjames 372
                              cnt += small_sprintf (outbuff + cnt, "*%02X\n",
373
                                                    sum & 0xFF);
374
                              sendString (&uc3, outbuff, cnt);
50 mjames 375
 
52 mjames 376
                            }
377
                          DataVal = ConvPLX (Data.Sensor[i].ReadingH,
378
                                             Data.Sensor[i].ReadingL);
379
                          if (DataVal > Max[i])
380
                            {
381
                              Max[i] = DataVal;
382
                            }
383
                          if (DataVal < Min[i])
384
                            {
385
                              Min[i] = DataVal;
386
                            }
387
                        }
50 mjames 388
 
52 mjames 389
                      // now to display the information
390
                      int suppress = DisplayCurrent (0, -1);
391
                      DisplayCurrent (1, suppress);
392
                    }
393
                  PLXPtr = 0;
394
                  PLXPacket = 0;
395
                }
396
              else if (c > PLX_Stop) // illegal char, restart reading
397
                {
398
                  PLXPacket = 0;
399
                  PLXPtr = 0;
400
                }
401
              else if (PLXPacket && PLXPtr < sizeof(Data.Bytes))
402
                {
403
                  Data.Bytes[PLXPtr++] = c;
404
                }
405
 
406
            }
407
 
408
          HAL_Delay (1);
409
        }
410
 
411
      /* USER CODE END WHILE */
412
 
413
      /* USER CODE BEGIN 3 */
414
    }
16 mjames 415
  /* USER CODE END 3 */
2 mjames 416
}
417
 
50 mjames 418
/**
52 mjames 419
 * @brief System Clock Configuration
420
 * @retval None
421
 */
422
void
423
SystemClock_Config (void)
5 mjames 424
{
52 mjames 425
  RCC_OscInitTypeDef RCC_OscInitStruct =
426
    { 0 };
427
  RCC_ClkInitTypeDef RCC_ClkInitStruct =
428
    { 0 };
2 mjames 429
 
50 mjames 430
  /** Configure the main internal regulator output voltage
52 mjames 431
   */
29 mjames 432
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
50 mjames 433
  /** Initializes the RCC Oscillators according to the specified parameters
52 mjames 434
   * in the RCC_OscInitTypeDef structure.
435
   */
44 mjames 436
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
437
  RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
16 mjames 438
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
44 mjames 439
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
440
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
29 mjames 441
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
52 mjames 442
  if (HAL_RCC_OscConfig (&RCC_OscInitStruct) != HAL_OK)
443
    {
444
      Error_Handler ();
445
    }
50 mjames 446
  /** Initializes the CPU, AHB and APB buses clocks
52 mjames 447
   */
448
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
449
      | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
16 mjames 450
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
451
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
29 mjames 452
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
16 mjames 453
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
50 mjames 454
 
52 mjames 455
  if (HAL_RCC_ClockConfig (&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
456
    {
457
      Error_Handler ();
458
    }
2 mjames 459
}
460
 
50 mjames 461
/**
52 mjames 462
 * @brief SPI1 Initialization Function
463
 * @param None
464
 * @retval None
465
 */
466
static void
467
MX_SPI1_Init (void)
5 mjames 468
{
2 mjames 469
 
50 mjames 470
  /* USER CODE BEGIN SPI1_Init 0 */
471
 
472
  /* USER CODE END SPI1_Init 0 */
473
 
474
  /* USER CODE BEGIN SPI1_Init 1 */
475
 
476
  /* USER CODE END SPI1_Init 1 */
477
  /* SPI1 parameter configuration*/
16 mjames 478
  hspi1.Instance = SPI1;
479
  hspi1.Init.Mode = SPI_MODE_MASTER;
480
  hspi1.Init.Direction = SPI_DIRECTION_1LINE;
481
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
482
  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
483
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
484
  hspi1.Init.NSS = SPI_NSS_SOFT;
50 mjames 485
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
16 mjames 486
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
487
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
488
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
489
  hspi1.Init.CRCPolynomial = 10;
52 mjames 490
  if (HAL_SPI_Init (&hspi1) != HAL_OK)
491
    {
492
      Error_Handler ();
493
    }
50 mjames 494
  /* USER CODE BEGIN SPI1_Init 2 */
2 mjames 495
 
50 mjames 496
  /* USER CODE END SPI1_Init 2 */
497
 
2 mjames 498
}
499
 
50 mjames 500
/**
52 mjames 501
 * @brief TIM2 Initialization Function
502
 * @param None
503
 * @retval None
504
 */
505
static void
506
MX_TIM2_Init (void)
50 mjames 507
{
508
 
509
  /* USER CODE BEGIN TIM2_Init 0 */
510
 
511
  /* USER CODE END TIM2_Init 0 */
512
 
52 mjames 513
  TIM_ClockConfigTypeDef sClockSourceConfig =
514
    { 0 };
515
  TIM_MasterConfigTypeDef sMasterConfig =
516
    { 0 };
50 mjames 517
 
518
  /* USER CODE BEGIN TIM2_Init 1 */
519
 
520
  /* USER CODE END TIM2_Init 1 */
521
  htim2.Instance = TIM2;
522
  htim2.Init.Prescaler = 0;
523
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
524
  htim2.Init.Period = 65535;
525
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
526
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
52 mjames 527
  if (HAL_TIM_Base_Init (&htim2) != HAL_OK)
528
    {
529
      Error_Handler ();
530
    }
50 mjames 531
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
52 mjames 532
  if (HAL_TIM_ConfigClockSource (&htim2, &sClockSourceConfig) != HAL_OK)
533
    {
534
      Error_Handler ();
535
    }
50 mjames 536
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
537
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
52 mjames 538
  if (HAL_TIMEx_MasterConfigSynchronization (&htim2, &sMasterConfig) != HAL_OK)
539
    {
540
      Error_Handler ();
541
    }
50 mjames 542
  /* USER CODE BEGIN TIM2_Init 2 */
543
 
544
  /* USER CODE END TIM2_Init 2 */
545
 
546
}
547
 
548
/**
52 mjames 549
 * @brief TIM3 Initialization Function
550
 * @param None
551
 * @retval None
552
 */
553
static void
554
MX_TIM3_Init (void)
44 mjames 555
{
556
 
50 mjames 557
  /* USER CODE BEGIN TIM3_Init 0 */
44 mjames 558
 
50 mjames 559
  /* USER CODE END TIM3_Init 0 */
560
 
52 mjames 561
  TIM_Encoder_InitTypeDef sConfig =
562
    { 0 };
563
  TIM_MasterConfigTypeDef sMasterConfig =
564
    { 0 };
50 mjames 565
 
566
  /* USER CODE BEGIN TIM3_Init 1 */
567
 
568
  /* USER CODE END TIM3_Init 1 */
44 mjames 569
  htim3.Instance = TIM3;
570
  htim3.Init.Prescaler = 0;
571
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
50 mjames 572
  htim3.Init.Period = 65535;
573
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
574
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
44 mjames 575
  sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
50 mjames 576
  sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
44 mjames 577
  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
578
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
579
  sConfig.IC1Filter = 15;
50 mjames 580
  sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
44 mjames 581
  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
582
  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
583
  sConfig.IC2Filter = 15;
52 mjames 584
  if (HAL_TIM_Encoder_Init (&htim3, &sConfig) != HAL_OK)
585
    {
586
      Error_Handler ();
587
    }
44 mjames 588
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
589
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
52 mjames 590
  if (HAL_TIMEx_MasterConfigSynchronization (&htim3, &sMasterConfig) != HAL_OK)
591
    {
592
      Error_Handler ();
593
    }
50 mjames 594
  /* USER CODE BEGIN TIM3_Init 2 */
44 mjames 595
 
50 mjames 596
  /* USER CODE END TIM3_Init 2 */
597
 
44 mjames 598
}
599
 
50 mjames 600
/**
52 mjames 601
 * @brief TIM9 Initialization Function
602
 * @param None
603
 * @retval None
604
 */
605
static void
606
MX_TIM9_Init (void)
44 mjames 607
{
608
 
50 mjames 609
  /* USER CODE BEGIN TIM9_Init 0 */
44 mjames 610
 
50 mjames 611
  /* USER CODE END TIM9_Init 0 */
612
 
52 mjames 613
  TIM_Encoder_InitTypeDef sConfig =
614
    { 0 };
615
  TIM_MasterConfigTypeDef sMasterConfig =
616
    { 0 };
50 mjames 617
 
618
  /* USER CODE BEGIN TIM9_Init 1 */
619
 
620
  /* USER CODE END TIM9_Init 1 */
44 mjames 621
  htim9.Instance = TIM9;
622
  htim9.Init.Prescaler = 0;
623
  htim9.Init.CounterMode = TIM_COUNTERMODE_UP;
50 mjames 624
  htim9.Init.Period = 65535;
625
  htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
626
  htim9.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
44 mjames 627
  sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
50 mjames 628
  sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
44 mjames 629
  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
630
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
631
  sConfig.IC1Filter = 15;
50 mjames 632
  sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
44 mjames 633
  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
634
  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
50 mjames 635
  sConfig.IC2Filter = 0;
52 mjames 636
  if (HAL_TIM_Encoder_Init (&htim9, &sConfig) != HAL_OK)
637
    {
638
      Error_Handler ();
639
    }
44 mjames 640
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
641
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
52 mjames 642
  if (HAL_TIMEx_MasterConfigSynchronization (&htim9, &sMasterConfig) != HAL_OK)
643
    {
644
      Error_Handler ();
645
    }
50 mjames 646
  /* USER CODE BEGIN TIM9_Init 2 */
44 mjames 647
 
50 mjames 648
  /* USER CODE END TIM9_Init 2 */
649
 
44 mjames 650
}
651
 
50 mjames 652
/**
52 mjames 653
 * @brief USART1 Initialization Function
654
 * @param None
655
 * @retval None
656
 */
657
static void
658
MX_USART1_UART_Init (void)
5 mjames 659
{
3 mjames 660
 
50 mjames 661
  /* USER CODE BEGIN USART1_Init 0 */
662
 
663
  /* USER CODE END USART1_Init 0 */
664
 
665
  /* USER CODE BEGIN USART1_Init 1 */
666
 
667
  /* USER CODE END USART1_Init 1 */
16 mjames 668
  huart1.Instance = USART1;
669
  huart1.Init.BaudRate = 19200;
670
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
44 mjames 671
  huart1.Init.StopBits = UART_STOPBITS_1;
16 mjames 672
  huart1.Init.Parity = UART_PARITY_NONE;
673
  huart1.Init.Mode = UART_MODE_TX_RX;
674
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
675
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
52 mjames 676
  if (HAL_UART_Init (&huart1) != HAL_OK)
677
    {
678
      Error_Handler ();
679
    }
50 mjames 680
  /* USER CODE BEGIN USART1_Init 2 */
3 mjames 681
 
50 mjames 682
  /* USER CODE END USART1_Init 2 */
683
 
3 mjames 684
}
685
 
50 mjames 686
/**
52 mjames 687
 * @brief USART2 Initialization Function
688
 * @param None
689
 * @retval None
690
 */
691
static void
692
MX_USART2_UART_Init (void)
5 mjames 693
{
2 mjames 694
 
50 mjames 695
  /* USER CODE BEGIN USART2_Init 0 */
696
 
697
  /* USER CODE END USART2_Init 0 */
698
 
699
  /* USER CODE BEGIN USART2_Init 1 */
700
 
701
  /* USER CODE END USART2_Init 1 */
16 mjames 702
  huart2.Instance = USART2;
703
  huart2.Init.BaudRate = 115200;
704
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
705
  huart2.Init.StopBits = UART_STOPBITS_1;
706
  huart2.Init.Parity = UART_PARITY_NONE;
707
  huart2.Init.Mode = UART_MODE_TX_RX;
708
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
709
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
52 mjames 710
  if (HAL_UART_Init (&huart2) != HAL_OK)
711
    {
712
      Error_Handler ();
713
    }
50 mjames 714
  /* USER CODE BEGIN USART2_Init 2 */
2 mjames 715
 
50 mjames 716
  /* USER CODE END USART2_Init 2 */
717
 
2 mjames 718
}
719
 
50 mjames 720
/**
52 mjames 721
 * @brief USART3 Initialization Function
722
 * @param None
723
 * @retval None
724
 */
725
static void
726
MX_USART3_UART_Init (void)
23 mjames 727
{
728
 
50 mjames 729
  /* USER CODE BEGIN USART3_Init 0 */
730
 
731
  /* USER CODE END USART3_Init 0 */
732
 
733
  /* USER CODE BEGIN USART3_Init 1 */
734
 
735
  /* USER CODE END USART3_Init 1 */
23 mjames 736
  huart3.Instance = USART3;
52 mjames 737
  huart3.Init.BaudRate = 9600;
23 mjames 738
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
50 mjames 739
  huart3.Init.StopBits = UART_STOPBITS_1;
44 mjames 740
  huart3.Init.Parity = UART_PARITY_NONE;
23 mjames 741
  huart3.Init.Mode = UART_MODE_TX_RX;
742
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
743
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
52 mjames 744
  if (HAL_UART_Init (&huart3) != HAL_OK)
745
    {
746
      Error_Handler ();
747
    }
50 mjames 748
  /* USER CODE BEGIN USART3_Init 2 */
23 mjames 749
 
50 mjames 750
  /* USER CODE END USART3_Init 2 */
751
 
23 mjames 752
}
753
 
50 mjames 754
/**
52 mjames 755
 * @brief GPIO Initialization Function
756
 * @param None
757
 * @retval None
758
 */
759
static void
760
MX_GPIO_Init (void)
5 mjames 761
{
52 mjames 762
  GPIO_InitTypeDef GPIO_InitStruct =
763
    { 0 };
2 mjames 764
 
16 mjames 765
  /* GPIO Ports Clock Enable */
29 mjames 766
  __HAL_RCC_GPIOH_CLK_ENABLE();
767
  __HAL_RCC_GPIOA_CLK_ENABLE();
768
  __HAL_RCC_GPIOC_CLK_ENABLE();
769
  __HAL_RCC_GPIOB_CLK_ENABLE();
2 mjames 770
 
16 mjames 771
  /*Configure GPIO pin Output Level */
52 mjames 772
  HAL_GPIO_WritePin (SPI_NSS1_GPIO_Port, SPI_NSS1_Pin, GPIO_PIN_SET);
2 mjames 773
 
16 mjames 774
  /*Configure GPIO pin Output Level */
52 mjames 775
  HAL_GPIO_WritePin (GPIOA, SPI_CD_Pin | BT_BUTTON_Pin, GPIO_PIN_RESET);
2 mjames 776
 
50 mjames 777
  /*Configure GPIO pin Output Level */
52 mjames 778
  HAL_GPIO_WritePin (GPIOC, SPI_RESET_Pin | POWER_LATCH_Pin | USB_PWR_Pin,
779
                     GPIO_PIN_RESET);
50 mjames 780
 
781
  /*Configure GPIO pin Output Level */
52 mjames 782
  HAL_GPIO_WritePin (SPI_NSS2_GPIO_Port, SPI_NSS2_Pin, GPIO_PIN_SET);
50 mjames 783
 
784
  /*Configure GPIO pins : SPI_NSS1_Pin SPI_CD_Pin */
52 mjames 785
  GPIO_InitStruct.Pin = SPI_NSS1_Pin | SPI_CD_Pin;
16 mjames 786
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
29 mjames 787
  GPIO_InitStruct.Pull = GPIO_NOPULL;
16 mjames 788
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
52 mjames 789
  HAL_GPIO_Init (GPIOA, &GPIO_InitStruct);
2 mjames 790
 
24 mjames 791
  /*Configure GPIO pins : SPI_RESET_Pin SPI_NSS2_Pin POWER_LATCH_Pin USB_PWR_Pin */
52 mjames 792
  GPIO_InitStruct.Pin = SPI_RESET_Pin | SPI_NSS2_Pin | POWER_LATCH_Pin
793
      | USB_PWR_Pin;
16 mjames 794
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
29 mjames 795
  GPIO_InitStruct.Pull = GPIO_NOPULL;
16 mjames 796
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
52 mjames 797
  HAL_GPIO_Init (GPIOC, &GPIO_InitStruct);
2 mjames 798
 
44 mjames 799
  /*Configure GPIO pins : SW1_PUSH_Pin SW2_PUSH_Pin */
52 mjames 800
  GPIO_InitStruct.Pin = SW1_PUSH_Pin | SW2_PUSH_Pin;
16 mjames 801
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
32 mjames 802
  GPIO_InitStruct.Pull = GPIO_PULLUP;
52 mjames 803
  HAL_GPIO_Init (GPIOB, &GPIO_InitStruct);
5 mjames 804
 
32 mjames 805
  /*Configure GPIO pin : IGNITION_Pin */
806
  GPIO_InitStruct.Pin = IGNITION_Pin;
807
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
808
  GPIO_InitStruct.Pull = GPIO_NOPULL;
52 mjames 809
  HAL_GPIO_Init (IGNITION_GPIO_Port, &GPIO_InitStruct);
32 mjames 810
 
37 mjames 811
  /*Configure GPIO pin : BT_BUTTON_Pin */
812
  GPIO_InitStruct.Pin = BT_BUTTON_Pin;
813
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
814
  GPIO_InitStruct.Pull = GPIO_NOPULL;
815
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
52 mjames 816
  HAL_GPIO_Init (BT_BUTTON_GPIO_Port, &GPIO_InitStruct);
37 mjames 817
 
2 mjames 818
}
819
 
820
/* USER CODE BEGIN 4 */
821
 
822
/* USER CODE END 4 */
823
 
5 mjames 824
/**
52 mjames 825
 * @brief  This function is executed in case of error occurrence.
826
 * @retval None
827
 */
828
void
829
Error_Handler (void)
5 mjames 830
{
50 mjames 831
  /* USER CODE BEGIN Error_Handler_Debug */
832
  /* User can add his own implementation to report the HAL error return state */
833
 
834
  /* USER CODE END Error_Handler_Debug */
30 mjames 835
}
5 mjames 836
 
50 mjames 837
#ifdef  USE_FULL_ASSERT
2 mjames 838
/**
50 mjames 839
  * @brief  Reports the name of the source file and the source line number
840
  *         where the assert_param error has occurred.
841
  * @param  file: pointer to the source file name
842
  * @param  line: assert_param error line source number
843
  * @retval None
844
  */
845
void assert_failed(uint8_t *file, uint32_t line)
29 mjames 846
{
847
  /* USER CODE BEGIN 6 */
50 mjames 848
  /* User can add his own implementation to report the file name and line number,
849
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
29 mjames 850
  /* USER CODE END 6 */
851
}
50 mjames 852
#endif /* USE_FULL_ASSERT */
2 mjames 853
 
854
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/