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
 
56 mjames 70
nvram_info_t dial_nvram[MAX_DISPLAYS] __attribute__((section(".NVRAM_Data")));
14 mjames 71
 
56 mjames 72
info_t Info[MAXRDG];
73
 
74
/// \brief storage for incoming data
50 mjames 75
data_t Data;
56 mjames 76
 
7 mjames 77
int PLXItems;
24 mjames 78
 
27 mjames 79
uint32_t Latch_Timer = IGNITION_OFF_TIMEOUT;
24 mjames 80
 
2 mjames 81
/* USER CODE END PV */
82
 
83
/* Private function prototypes -----------------------------------------------*/
52 mjames 84
void
85
SystemClock_Config (void);
86
static void
87
MX_GPIO_Init (void);
88
static void
89
MX_SPI1_Init (void);
90
static void
91
MX_USART1_UART_Init (void);
92
static void
93
MX_USART2_UART_Init (void);
94
static void
95
MX_USART3_UART_Init (void);
96
static void
97
MX_TIM3_Init (void);
98
static void
99
MX_TIM9_Init (void);
100
static void
101
MX_TIM2_Init (void);
2 mjames 102
/* USER CODE BEGIN PFP */
103
 
7 mjames 104
// the dial is the switch number we are using.
105
// suppress is the ItemIndex we wish to suppress on this display
52 mjames 106
int
107
DisplayCurrent (int dial, int suppress)
7 mjames 108
{
56 mjames 109
  int itemIndex = dial_pos[dial] % PLXItems;
110
  if (itemIndex < 0)
50 mjames 111
    return -1;
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
 
56 mjames 133
/// \note this code doesnt work so it leaves speed as 9600.
134
/// \brief Setup Bluetooth module
53 mjames 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
 
56 mjames 239
  initModule (&uc3, 9600);
53 mjames 240
 
52 mjames 241
  cc_init ();
23 mjames 242
 
50 mjames 243
  int i;
244
  for (i = 0; i < 2; i++)
52 mjames 245
    {
56 mjames 246
      dial_pos[i] = -1; // default to items 0 and 1
52 mjames 247
    }
7 mjames 248
 
50 mjames 249
  /* reset the display timeout, latch on power from accessories */
250
  Latch_Timer = IGNITION_OFF_TIMEOUT;
52 mjames 251
  HAL_GPIO_WritePin (POWER_LATCH_GPIO_Port, POWER_LATCH_Pin, GPIO_PIN_RESET);
16 mjames 252
 
253
  /* USER CODE END 2 */
7 mjames 254
 
16 mjames 255
  /* Infinite loop */
256
  /* USER CODE BEGIN WHILE */
52 mjames 257
  while (1)
258
    {
7 mjames 259
 
52 mjames 260
      /* while ignition is on, keep resetting power latch timer */
261
      if (HAL_GPIO_ReadPin (IGNITION_GPIO_Port, IGNITION_Pin) == GPIO_PIN_RESET)
262
        {
263
          Latch_Timer = HAL_GetTick () + IGNITION_OFF_TIMEOUT;
264
        }
265
      else
266
        {
267
          /* if the ignition has been off for a while, then turn off power */
268
          if (HAL_GetTick () > Latch_Timer)
269
            {
270
              HAL_GPIO_WritePin (POWER_LATCH_GPIO_Port, POWER_LATCH_Pin,
271
                                 GPIO_PIN_RESET);
272
            }
273
        }
27 mjames 274
 
52 mjames 275
      uint32_t timeout = 0;  //
27 mjames 276
 
52 mjames 277
      uint32_t nextTick = HAL_GetTick () + LOGGER_INTERVAL;
278
      uint8_t log = 0;
279
      // PLX decoder protocols
280
      char PLXPacket = 0;
281
      for (i = 0; i < MAXRDG; i++)
282
        {
56 mjames 283
          Info[i].Max = 0;
284
          Info[i].Min = 0xFFF; // 12 bit max value
52 mjames 285
        }
27 mjames 286
 
52 mjames 287
      int PLXPtr = 0;
24 mjames 288
 
52 mjames 289
      while (1)
290
        {
53 mjames 291
          // Handle the bluetooth pairing / reset function by pressing both buttons.
52 mjames 292
          if ((push_pos[0] == 1) && (push_pos[1] == 1))
293
            {
294
              HAL_GPIO_WritePin (BT_BUTTON_GPIO_Port, BT_BUTTON_Pin,
295
                                 GPIO_PIN_RESET);
296
            }
297
          else
298
            {
299
              HAL_GPIO_WritePin (BT_BUTTON_GPIO_Port, BT_BUTTON_Pin,
300
                                 GPIO_PIN_SET);
301
            }
7 mjames 302
 
52 mjames 303
          uint16_t cc = SerialCharsReceived (&uc1);
304
          int chr;
305
          if (cc == 0)
306
            {
307
              timeout++;
308
              if (timeout % 1000 == 0)
309
                {
310
                  const char msg[] = "Timeout\r\n";
53 mjames 311
                  sendString (&uc3, msg, sizeof(msg));
38 mjames 312
 
52 mjames 313
                }
38 mjames 314
 
52 mjames 315
              if (timeout > 60000)
316
                {
7 mjames 317
 
52 mjames 318
                  // do turn off screen
319
                }
27 mjames 320
 
52 mjames 321
            }
322
          for (chr = 0; chr < cc; chr++)
323
            {
324
              char c = GetCharSerial (&uc1);
7 mjames 325
 
52 mjames 326
              if (c == PLX_Start) // at any time if the start byte appears, reset the pointers
327
                {
328
                  PLXPtr = 0;    // reset the pointer
329
                  PLXPacket = 1;
330
                  timeout = 0;    // Reset the timer
331
                  if (HAL_GetTick () > nextTick)
332
                    {
333
                      nextTick = HAL_GetTick () + LOGGER_INTERVAL;
334
                      log = 1;
335
                    }
336
                  else
337
                    log = 0;
338
                }
339
              else if (c == PLX_Stop)
340
                {
341
                  if (PLXPacket)
342
                    {
343
                      // we can now decode the selected parameter
344
                      PLXItems = PLXPtr / sizeof(PLX_SensorInfo); // total
345
                      // saturate the rotary switch position
9 mjames 346
 
52 mjames 347
                      int DataVal;
348
                      // process min/max
349
                      for (i = 0; i < PLXItems; i++)
350
                        {
56 mjames 351
                          Info[i].observation = ConvPLX (Data.Sensor[i].AddrH,
352
                                                         Data.Sensor[i].AddrL);
353
                          Info[i].instance = Data.Sensor[i].Instance;
354
                          Info[i].data = ConvPLX (Data.Sensor[i].ReadingH,
355
                                                  Data.Sensor[i].ReadingL);
356
                          if (Info[i].data > Info[i].Max)
357
                            {
358
                              Info[i].Max = Info[i].data;
359
                            }
360
                          if (Info[i].data < Info[i].Min)
361
                            {
362
                              Info[i].Min = Info[i].data;
363
                            }
23 mjames 364
 
56 mjames 365
                          // Send item to BT
366
 
52 mjames 367
                          if (log)
368
                            {
56 mjames 369
 
52 mjames 370
                              char outbuff[100];
30 mjames 371
 
53 mjames 372
                              int cnt = small_sprintf (outbuff, "$LOG,%d,%d,%d",
56 mjames 373
                                                       Info[i].observation,
374
                                                       Info[i].instance,
375
                                                       Info[i].data);
4 mjames 376
 
53 mjames 377
                              //checksum
52 mjames 378
                              int ck;
379
                              int sum = 0;
53 mjames 380
                              for (ck = 1; ck < cnt; ck++)
52 mjames 381
                                sum += outbuff[ck];
53 mjames 382
                              cnt += small_sprintf (outbuff + cnt, "*%02X\n",
383
                                                    sum & 0xFF);
384
                              sendString (&uc3, outbuff, cnt);
50 mjames 385
 
52 mjames 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);
56 mjames 409
 
410
          dial_pos[0] = cc_check_nvram (0, dial_pos[0]);
411
          dial_pos[1] = cc_check_nvram (1, dial_pos[1]);
412
 
52 mjames 413
        }
414
 
415
      /* USER CODE END WHILE */
416
 
417
      /* USER CODE BEGIN 3 */
418
    }
16 mjames 419
  /* USER CODE END 3 */
2 mjames 420
}
421
 
50 mjames 422
/**
52 mjames 423
 * @brief System Clock Configuration
424
 * @retval None
425
 */
426
void
427
SystemClock_Config (void)
5 mjames 428
{
52 mjames 429
  RCC_OscInitTypeDef RCC_OscInitStruct =
430
    { 0 };
431
  RCC_ClkInitTypeDef RCC_ClkInitStruct =
432
    { 0 };
2 mjames 433
 
50 mjames 434
  /** Configure the main internal regulator output voltage
52 mjames 435
   */
29 mjames 436
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
50 mjames 437
  /** Initializes the RCC Oscillators according to the specified parameters
52 mjames 438
   * in the RCC_OscInitTypeDef structure.
439
   */
44 mjames 440
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
441
  RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
16 mjames 442
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
44 mjames 443
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
444
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
29 mjames 445
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
52 mjames 446
  if (HAL_RCC_OscConfig (&RCC_OscInitStruct) != HAL_OK)
447
    {
448
      Error_Handler ();
449
    }
50 mjames 450
  /** Initializes the CPU, AHB and APB buses clocks
52 mjames 451
   */
452
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
453
      | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
16 mjames 454
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
455
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
29 mjames 456
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
16 mjames 457
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
50 mjames 458
 
52 mjames 459
  if (HAL_RCC_ClockConfig (&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
460
    {
461
      Error_Handler ();
462
    }
2 mjames 463
}
464
 
50 mjames 465
/**
52 mjames 466
 * @brief SPI1 Initialization Function
467
 * @param None
468
 * @retval None
469
 */
470
static void
471
MX_SPI1_Init (void)
5 mjames 472
{
2 mjames 473
 
50 mjames 474
  /* USER CODE BEGIN SPI1_Init 0 */
475
 
476
  /* USER CODE END SPI1_Init 0 */
477
 
478
  /* USER CODE BEGIN SPI1_Init 1 */
479
 
480
  /* USER CODE END SPI1_Init 1 */
481
  /* SPI1 parameter configuration*/
16 mjames 482
  hspi1.Instance = SPI1;
483
  hspi1.Init.Mode = SPI_MODE_MASTER;
484
  hspi1.Init.Direction = SPI_DIRECTION_1LINE;
485
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
486
  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
487
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
488
  hspi1.Init.NSS = SPI_NSS_SOFT;
50 mjames 489
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
16 mjames 490
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
491
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
492
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
493
  hspi1.Init.CRCPolynomial = 10;
52 mjames 494
  if (HAL_SPI_Init (&hspi1) != HAL_OK)
495
    {
496
      Error_Handler ();
497
    }
50 mjames 498
  /* USER CODE BEGIN SPI1_Init 2 */
2 mjames 499
 
50 mjames 500
  /* USER CODE END SPI1_Init 2 */
501
 
2 mjames 502
}
503
 
50 mjames 504
/**
52 mjames 505
 * @brief TIM2 Initialization Function
506
 * @param None
507
 * @retval None
508
 */
509
static void
510
MX_TIM2_Init (void)
50 mjames 511
{
512
 
513
  /* USER CODE BEGIN TIM2_Init 0 */
514
 
515
  /* USER CODE END TIM2_Init 0 */
516
 
52 mjames 517
  TIM_ClockConfigTypeDef sClockSourceConfig =
518
    { 0 };
519
  TIM_MasterConfigTypeDef sMasterConfig =
520
    { 0 };
50 mjames 521
 
522
  /* USER CODE BEGIN TIM2_Init 1 */
523
 
524
  /* USER CODE END TIM2_Init 1 */
525
  htim2.Instance = TIM2;
526
  htim2.Init.Prescaler = 0;
527
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
528
  htim2.Init.Period = 65535;
529
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
530
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
52 mjames 531
  if (HAL_TIM_Base_Init (&htim2) != HAL_OK)
532
    {
533
      Error_Handler ();
534
    }
50 mjames 535
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
52 mjames 536
  if (HAL_TIM_ConfigClockSource (&htim2, &sClockSourceConfig) != HAL_OK)
537
    {
538
      Error_Handler ();
539
    }
50 mjames 540
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
541
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
52 mjames 542
  if (HAL_TIMEx_MasterConfigSynchronization (&htim2, &sMasterConfig) != HAL_OK)
543
    {
544
      Error_Handler ();
545
    }
50 mjames 546
  /* USER CODE BEGIN TIM2_Init 2 */
547
 
548
  /* USER CODE END TIM2_Init 2 */
549
 
550
}
551
 
552
/**
52 mjames 553
 * @brief TIM3 Initialization Function
554
 * @param None
555
 * @retval None
556
 */
557
static void
558
MX_TIM3_Init (void)
44 mjames 559
{
560
 
50 mjames 561
  /* USER CODE BEGIN TIM3_Init 0 */
44 mjames 562
 
50 mjames 563
  /* USER CODE END TIM3_Init 0 */
564
 
52 mjames 565
  TIM_Encoder_InitTypeDef sConfig =
566
    { 0 };
567
  TIM_MasterConfigTypeDef sMasterConfig =
568
    { 0 };
50 mjames 569
 
570
  /* USER CODE BEGIN TIM3_Init 1 */
571
 
572
  /* USER CODE END TIM3_Init 1 */
44 mjames 573
  htim3.Instance = TIM3;
574
  htim3.Init.Prescaler = 0;
575
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
50 mjames 576
  htim3.Init.Period = 65535;
577
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
578
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
44 mjames 579
  sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
50 mjames 580
  sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
44 mjames 581
  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
582
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
583
  sConfig.IC1Filter = 15;
50 mjames 584
  sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
44 mjames 585
  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
586
  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
587
  sConfig.IC2Filter = 15;
52 mjames 588
  if (HAL_TIM_Encoder_Init (&htim3, &sConfig) != HAL_OK)
589
    {
590
      Error_Handler ();
591
    }
44 mjames 592
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
593
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
52 mjames 594
  if (HAL_TIMEx_MasterConfigSynchronization (&htim3, &sMasterConfig) != HAL_OK)
595
    {
596
      Error_Handler ();
597
    }
50 mjames 598
  /* USER CODE BEGIN TIM3_Init 2 */
44 mjames 599
 
50 mjames 600
  /* USER CODE END TIM3_Init 2 */
601
 
44 mjames 602
}
603
 
50 mjames 604
/**
52 mjames 605
 * @brief TIM9 Initialization Function
606
 * @param None
607
 * @retval None
608
 */
609
static void
610
MX_TIM9_Init (void)
44 mjames 611
{
612
 
50 mjames 613
  /* USER CODE BEGIN TIM9_Init 0 */
44 mjames 614
 
50 mjames 615
  /* USER CODE END TIM9_Init 0 */
616
 
52 mjames 617
  TIM_Encoder_InitTypeDef sConfig =
618
    { 0 };
619
  TIM_MasterConfigTypeDef sMasterConfig =
620
    { 0 };
50 mjames 621
 
622
  /* USER CODE BEGIN TIM9_Init 1 */
623
 
624
  /* USER CODE END TIM9_Init 1 */
44 mjames 625
  htim9.Instance = TIM9;
626
  htim9.Init.Prescaler = 0;
627
  htim9.Init.CounterMode = TIM_COUNTERMODE_UP;
50 mjames 628
  htim9.Init.Period = 65535;
629
  htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
630
  htim9.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
44 mjames 631
  sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
50 mjames 632
  sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
44 mjames 633
  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
634
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
635
  sConfig.IC1Filter = 15;
50 mjames 636
  sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
44 mjames 637
  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
638
  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
50 mjames 639
  sConfig.IC2Filter = 0;
52 mjames 640
  if (HAL_TIM_Encoder_Init (&htim9, &sConfig) != HAL_OK)
641
    {
642
      Error_Handler ();
643
    }
44 mjames 644
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
645
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
52 mjames 646
  if (HAL_TIMEx_MasterConfigSynchronization (&htim9, &sMasterConfig) != HAL_OK)
647
    {
648
      Error_Handler ();
649
    }
50 mjames 650
  /* USER CODE BEGIN TIM9_Init 2 */
44 mjames 651
 
50 mjames 652
  /* USER CODE END TIM9_Init 2 */
653
 
44 mjames 654
}
655
 
50 mjames 656
/**
52 mjames 657
 * @brief USART1 Initialization Function
658
 * @param None
659
 * @retval None
660
 */
661
static void
662
MX_USART1_UART_Init (void)
5 mjames 663
{
3 mjames 664
 
50 mjames 665
  /* USER CODE BEGIN USART1_Init 0 */
666
 
667
  /* USER CODE END USART1_Init 0 */
668
 
669
  /* USER CODE BEGIN USART1_Init 1 */
670
 
671
  /* USER CODE END USART1_Init 1 */
16 mjames 672
  huart1.Instance = USART1;
673
  huart1.Init.BaudRate = 19200;
674
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
44 mjames 675
  huart1.Init.StopBits = UART_STOPBITS_1;
16 mjames 676
  huart1.Init.Parity = UART_PARITY_NONE;
677
  huart1.Init.Mode = UART_MODE_TX_RX;
678
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
679
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
52 mjames 680
  if (HAL_UART_Init (&huart1) != HAL_OK)
681
    {
682
      Error_Handler ();
683
    }
50 mjames 684
  /* USER CODE BEGIN USART1_Init 2 */
3 mjames 685
 
50 mjames 686
  /* USER CODE END USART1_Init 2 */
687
 
3 mjames 688
}
689
 
50 mjames 690
/**
52 mjames 691
 * @brief USART2 Initialization Function
692
 * @param None
693
 * @retval None
694
 */
695
static void
696
MX_USART2_UART_Init (void)
5 mjames 697
{
2 mjames 698
 
50 mjames 699
  /* USER CODE BEGIN USART2_Init 0 */
700
 
701
  /* USER CODE END USART2_Init 0 */
702
 
703
  /* USER CODE BEGIN USART2_Init 1 */
704
 
705
  /* USER CODE END USART2_Init 1 */
16 mjames 706
  huart2.Instance = USART2;
707
  huart2.Init.BaudRate = 115200;
708
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
709
  huart2.Init.StopBits = UART_STOPBITS_1;
710
  huart2.Init.Parity = UART_PARITY_NONE;
711
  huart2.Init.Mode = UART_MODE_TX_RX;
712
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
713
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
52 mjames 714
  if (HAL_UART_Init (&huart2) != HAL_OK)
715
    {
716
      Error_Handler ();
717
    }
50 mjames 718
  /* USER CODE BEGIN USART2_Init 2 */
2 mjames 719
 
50 mjames 720
  /* USER CODE END USART2_Init 2 */
721
 
2 mjames 722
}
723
 
50 mjames 724
/**
52 mjames 725
 * @brief USART3 Initialization Function
726
 * @param None
727
 * @retval None
728
 */
729
static void
730
MX_USART3_UART_Init (void)
23 mjames 731
{
732
 
50 mjames 733
  /* USER CODE BEGIN USART3_Init 0 */
734
 
735
  /* USER CODE END USART3_Init 0 */
736
 
737
  /* USER CODE BEGIN USART3_Init 1 */
738
 
739
  /* USER CODE END USART3_Init 1 */
23 mjames 740
  huart3.Instance = USART3;
52 mjames 741
  huart3.Init.BaudRate = 9600;
23 mjames 742
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
50 mjames 743
  huart3.Init.StopBits = UART_STOPBITS_1;
44 mjames 744
  huart3.Init.Parity = UART_PARITY_NONE;
23 mjames 745
  huart3.Init.Mode = UART_MODE_TX_RX;
746
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
747
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
52 mjames 748
  if (HAL_UART_Init (&huart3) != HAL_OK)
749
    {
750
      Error_Handler ();
751
    }
50 mjames 752
  /* USER CODE BEGIN USART3_Init 2 */
23 mjames 753
 
50 mjames 754
  /* USER CODE END USART3_Init 2 */
755
 
23 mjames 756
}
757
 
50 mjames 758
/**
52 mjames 759
 * @brief GPIO Initialization Function
760
 * @param None
761
 * @retval None
762
 */
763
static void
764
MX_GPIO_Init (void)
5 mjames 765
{
52 mjames 766
  GPIO_InitTypeDef GPIO_InitStruct =
767
    { 0 };
2 mjames 768
 
16 mjames 769
  /* GPIO Ports Clock Enable */
29 mjames 770
  __HAL_RCC_GPIOH_CLK_ENABLE();
771
  __HAL_RCC_GPIOA_CLK_ENABLE();
772
  __HAL_RCC_GPIOC_CLK_ENABLE();
773
  __HAL_RCC_GPIOB_CLK_ENABLE();
2 mjames 774
 
16 mjames 775
  /*Configure GPIO pin Output Level */
52 mjames 776
  HAL_GPIO_WritePin (SPI_NSS1_GPIO_Port, SPI_NSS1_Pin, GPIO_PIN_SET);
2 mjames 777
 
16 mjames 778
  /*Configure GPIO pin Output Level */
52 mjames 779
  HAL_GPIO_WritePin (GPIOA, SPI_CD_Pin | BT_BUTTON_Pin, GPIO_PIN_RESET);
2 mjames 780
 
50 mjames 781
  /*Configure GPIO pin Output Level */
52 mjames 782
  HAL_GPIO_WritePin (GPIOC, SPI_RESET_Pin | POWER_LATCH_Pin | USB_PWR_Pin,
783
                     GPIO_PIN_RESET);
50 mjames 784
 
785
  /*Configure GPIO pin Output Level */
52 mjames 786
  HAL_GPIO_WritePin (SPI_NSS2_GPIO_Port, SPI_NSS2_Pin, GPIO_PIN_SET);
50 mjames 787
 
788
  /*Configure GPIO pins : SPI_NSS1_Pin SPI_CD_Pin */
52 mjames 789
  GPIO_InitStruct.Pin = SPI_NSS1_Pin | SPI_CD_Pin;
16 mjames 790
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
29 mjames 791
  GPIO_InitStruct.Pull = GPIO_NOPULL;
16 mjames 792
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
52 mjames 793
  HAL_GPIO_Init (GPIOA, &GPIO_InitStruct);
2 mjames 794
 
24 mjames 795
  /*Configure GPIO pins : SPI_RESET_Pin SPI_NSS2_Pin POWER_LATCH_Pin USB_PWR_Pin */
52 mjames 796
  GPIO_InitStruct.Pin = SPI_RESET_Pin | SPI_NSS2_Pin | POWER_LATCH_Pin
797
      | USB_PWR_Pin;
16 mjames 798
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
29 mjames 799
  GPIO_InitStruct.Pull = GPIO_NOPULL;
16 mjames 800
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
52 mjames 801
  HAL_GPIO_Init (GPIOC, &GPIO_InitStruct);
2 mjames 802
 
44 mjames 803
  /*Configure GPIO pins : SW1_PUSH_Pin SW2_PUSH_Pin */
52 mjames 804
  GPIO_InitStruct.Pin = SW1_PUSH_Pin | SW2_PUSH_Pin;
16 mjames 805
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
32 mjames 806
  GPIO_InitStruct.Pull = GPIO_PULLUP;
52 mjames 807
  HAL_GPIO_Init (GPIOB, &GPIO_InitStruct);
5 mjames 808
 
32 mjames 809
  /*Configure GPIO pin : IGNITION_Pin */
810
  GPIO_InitStruct.Pin = IGNITION_Pin;
811
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
812
  GPIO_InitStruct.Pull = GPIO_NOPULL;
52 mjames 813
  HAL_GPIO_Init (IGNITION_GPIO_Port, &GPIO_InitStruct);
32 mjames 814
 
37 mjames 815
  /*Configure GPIO pin : BT_BUTTON_Pin */
816
  GPIO_InitStruct.Pin = BT_BUTTON_Pin;
817
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
818
  GPIO_InitStruct.Pull = GPIO_NOPULL;
819
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
52 mjames 820
  HAL_GPIO_Init (BT_BUTTON_GPIO_Port, &GPIO_InitStruct);
37 mjames 821
 
2 mjames 822
}
823
 
824
/* USER CODE BEGIN 4 */
825
 
826
/* USER CODE END 4 */
827
 
5 mjames 828
/**
52 mjames 829
 * @brief  This function is executed in case of error occurrence.
830
 * @retval None
831
 */
832
void
833
Error_Handler (void)
5 mjames 834
{
50 mjames 835
  /* USER CODE BEGIN Error_Handler_Debug */
836
  /* User can add his own implementation to report the HAL error return state */
837
 
838
  /* USER CODE END Error_Handler_Debug */
30 mjames 839
}
5 mjames 840
 
50 mjames 841
#ifdef  USE_FULL_ASSERT
2 mjames 842
/**
50 mjames 843
  * @brief  Reports the name of the source file and the source line number
844
  *         where the assert_param error has occurred.
845
  * @param  file: pointer to the source file name
846
  * @param  line: assert_param error line source number
847
  * @retval None
848
  */
849
void assert_failed(uint8_t *file, uint32_t line)
29 mjames 850
{
851
  /* USER CODE BEGIN 6 */
50 mjames 852
  /* User can add his own implementation to report the file name and line number,
853
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
29 mjames 854
  /* USER CODE END 6 */
855
}
50 mjames 856
#endif /* USE_FULL_ASSERT */
2 mjames 857
 
858
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/