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