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