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
 
67 mjames 74
const int DialTimeout = 100; // about 10 seconds after twiddle, save the dial position.
18 mjames 75
 
67 mjames 76
nvram_info_t dial_nvram[MAX_DISPLAYS];
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
67 mjames 254
    contexts[i].dial_timer = 1; // timeout immediately when decremented
255
 
256
    cc_check_nvram(i);
60 mjames 257
  }
7 mjames 258
 
50 mjames 259
  /* reset the display timeout, latch on power from accessories */
260
  Latch_Timer = IGNITION_OFF_TIMEOUT;
60 mjames 261
  HAL_GPIO_WritePin(POWER_LATCH_GPIO_Port, POWER_LATCH_Pin, GPIO_PIN_RESET);
16 mjames 262
 
60 mjames 263
  setRmcCallback(&rmc_callback);
264
 
66 mjames 265
  // data timeout
266
  uint32_t timeout = 0; //
267
 
268
  uint32_t nextTick = 0;
269
  uint8_t log = 0;
270
  // PLX decoder protocols
271
  char PLXPacket = 0;
272
  for (i = 0; i < MAXRDG; i++)
273
  {
67 mjames 274
    Info[i].Max = 0;
66 mjames 275
    Info[i].Min = 0xFFF;
67 mjames 276
    Info[i].sum = 0;
277
    Info[i].count = 0;
66 mjames 278
    Info[i].updated = 0;
279
    Info[i].lastUpdated = 0;
280
  }
281
 
282
  int PLXPtr = 0;
283
  int logCount = 0;
284
 
16 mjames 285
  /* USER CODE END 2 */
7 mjames 286
 
16 mjames 287
  /* Infinite loop */
288
  /* USER CODE BEGIN WHILE */
52 mjames 289
  while (1)
60 mjames 290
  {
291
 
292
    /* while ignition is on, keep resetting power latch timer */
293
    if (HAL_GPIO_ReadPin(IGNITION_GPIO_Port, IGNITION_Pin) == GPIO_PIN_RESET)
52 mjames 294
    {
60 mjames 295
      Latch_Timer = HAL_GetTick() + IGNITION_OFF_TIMEOUT;
296
    }
297
    else
298
    {
299
      /* if the ignition has been off for a while, then turn off power */
300
      if (HAL_GetTick() > Latch_Timer)
301
      {
302
        HAL_GPIO_WritePin(POWER_LATCH_GPIO_Port, POWER_LATCH_Pin,
303
                          GPIO_PIN_RESET);
304
      }
305
    }
7 mjames 306
 
66 mjames 307
    // Handle the bluetooth pairing / reset function by pressing both buttons.
308
    if ((push_pos[0] == 1) && (push_pos[1] == 1))
60 mjames 309
    {
66 mjames 310
      HAL_GPIO_WritePin(BT_BUTTON_GPIO_Port, BT_BUTTON_Pin,
311
                        GPIO_PIN_RESET);
60 mjames 312
    }
66 mjames 313
    else
314
    {
315
      HAL_GPIO_WritePin(BT_BUTTON_GPIO_Port, BT_BUTTON_Pin,
316
                        GPIO_PIN_SET);
317
    }
58 mjames 318
 
66 mjames 319
    // poll GPS Position/time on UART4
320
    (void)updateLocation(&loc, &uc4);
321
    if (loc.valid == 'V')
322
      memset(loc.time, '-', 6);
60 mjames 323
 
66 mjames 324
    // if permitted, log data from RMC packet
325
    if (btConnected())
60 mjames 326
    {
66 mjames 327
      // Any RMC data, send it, reset the logger timeout
328
      if (rmc_length)
62 mjames 329
      {
66 mjames 330
        sendString(&uc3, (const char *)rmc_buff, rmc_length);
62 mjames 331
        rmc_length = 0;
332
        nextTick = HAL_GetTick() + LOGGER_INTERVAL;
66 mjames 333
        log = 1;      // send out associated data over Bluetooth because triggered by recieving RMC
334
        logCount = 0; // first sample set this second numbered 0
62 mjames 335
      }
65 mjames 336
 
66 mjames 337
      // Timeout for data logging regularly
338
      if (HAL_GetTick() > nextTick)
62 mjames 339
      {
340
        nextTick = HAL_GetTick() + LOGGER_INTERVAL;
66 mjames 341
        logCount++;
342
        if (logCount > (1000 / LOGGER_INTERVAL))
343
          logCount = 0;
62 mjames 344
        log = 1;
345
      }
346
 
66 mjames 347
      if (log)
60 mjames 348
      {
66 mjames 349
        log = 0;
350
        // Send items  to BT if it is in connected state
351
        for (int i = 0; i < PLXItems; ++i)
352
        {
353
          char outbuff[100];
354
 
355
          int cnt = small_sprintf(outbuff,
356
                                  "$PLLOG,%d,%d,%d,%ld",
357
                                  logCount,
358
                                  Info[i].observation,
359
                                  Info[i].instance,
360
                                  Info[i].count == 0 ? 0 : Info[i].sum / Info[i].count);
361
 
362
          // NMEA style checksum
363
          int ck;
364
          int sum = 0;
365
          for (ck = 1; ck < cnt; ck++)
366
            sum += outbuff[ck];
367
          cnt += small_sprintf(outbuff + cnt, "*%02X\n",
368
                               sum & 0xFF);
369
          sendString(&uc3, outbuff, cnt);
370
        }
60 mjames 371
      }
66 mjames 372
    }
373
 
374
    // determine if we are getting any data from the interface
375
    uint16_t cc = SerialCharsReceived(&uc1);
376
    int chr;
377
    if (cc == 0)
378
    {
379
      timeout++;
380
      if (btConnected() && (timeout % 1000 == 0))
60 mjames 381
      {
66 mjames 382
        const char msg[] = "Timeout\r\n";
383
        sendString(&uc3, msg, sizeof(msg));
60 mjames 384
      }
27 mjames 385
 
66 mjames 386
      if (timeout > 60000)
60 mjames 387
      {
27 mjames 388
 
66 mjames 389
        // do turn off screen
60 mjames 390
      }
66 mjames 391
      // wait for a bit if nothing came in.
392
      HAL_Delay(10);
393
    }
62 mjames 394
 
66 mjames 395
    /// process the observation list
396
    for (chr = 0; chr < cc; chr++)
397
    {
68 mjames 398
      char c = GetCharSerial(&uc1);
66 mjames 399
 
400
      if (c == PLX_Start) // at any time if the start byte appears, reset the pointers
60 mjames 401
      {
66 mjames 402
        PLXPtr = 0; // reset the pointer
403
        PLXPacket = 1;
404
        timeout = 0; // Reset the timer
67 mjames 405
        continue;
66 mjames 406
      }
67 mjames 407
      if (c == PLX_Stop)
66 mjames 408
      {
409
        if (PLXPacket)
410
        {
411
          // we can now decode the selected parameter
412
          PLXItems = PLXPtr / sizeof(PLX_SensorInfo); // total
413
          // saturate the rotary switch position
24 mjames 414
 
66 mjames 415
          // process min/max
416
          for (i = 0; i < PLXItems; i++)
60 mjames 417
          {
66 mjames 418
            Info[i].observation = ConvPLX(Data.Sensor[i].AddrH,
419
                                          Data.Sensor[i].AddrL);
7 mjames 420
 
66 mjames 421
            Info[i].instance = Data.Sensor[i].Instance;
422
            Info[i].data = ConvPLX(Data.Sensor[i].ReadingH,
423
                                   Data.Sensor[i].ReadingL);
424
            if (Info[i].data > Info[i].Max)
60 mjames 425
            {
66 mjames 426
              Info[i].Max = Info[i].data;
60 mjames 427
            }
66 mjames 428
            if (Info[i].data < Info[i].Min)
429
            {
430
              Info[i].Min = Info[i].data;
431
            }
432
            // take an average
433
            Info[i].sum += Info[i].data;
434
            Info[i].count++;
435
            // note the last update time
436
            Info[i].lastUpdated = HAL_GetTick();
67 mjames 437
            Info[i].updated = 1; // it has been updated
60 mjames 438
          }
439
          PLXPtr = 0;
440
          PLXPacket = 0;
67 mjames 441
          break; // something to process 
60 mjames 442
        }
443
      }
67 mjames 444
      if (c > PLX_Stop) // illegal char, restart reading
445
      {
446
        PLXPacket = 0;
447
        PLXPtr = 0;
448
        continue;
449
      }
450
      if (PLXPacket && PLXPtr < sizeof(Data.Bytes))
451
      {
452
        Data.Bytes[PLXPtr++] = c;
453
      }
454
    }
455
    int suppress = -1;
456
    for (i = 0; i < MAX_DISPLAYS; i++)
457
    { // now to display the information
458
      suppress = DisplayCurrent(i, suppress);
459
 
23 mjames 460
 
67 mjames 461
      if (dial_pos[i] < 0)
462
        dial_pos[i] = PLXItems - 1;
463
      if (dial_pos[i] >= PLXItems)
464
        dial_pos[i] = 0;
56 mjames 465
 
67 mjames 466
      int prevPos = contexts[i].knobPos;
467
      if (contexts[i].knobPos >= 0)
468
        contexts[i].knobPos = dial_pos[i];
469
      // if the dial position was changed then reset timer
470
      if (prevPos != contexts[i].knobPos)
471
        contexts[i].dial_timer = DialTimeout;
56 mjames 472
 
67 mjames 473
      cc_check_nvram(i);
474
      if (contexts[i].knobPos >= 0)
475
        dial_pos[i] = contexts[i].knobPos;
60 mjames 476
    }
66 mjames 477
  }
478
  /* USER CODE END WHILE */
52 mjames 479
 
66 mjames 480
  /* USER CODE BEGIN 3 */
481
 
16 mjames 482
  /* USER CODE END 3 */
2 mjames 483
}
67 mjames 484
 
50 mjames 485
/**
62 mjames 486
 * @brief System Clock Configuration
487
 * @retval None
488
 */
58 mjames 489
void SystemClock_Config(void)
5 mjames 490
{
58 mjames 491
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
492
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
2 mjames 493
 
50 mjames 494
  /** Configure the main internal regulator output voltage
62 mjames 495
   */
29 mjames 496
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
61 mjames 497
 
50 mjames 498
  /** Initializes the RCC Oscillators according to the specified parameters
62 mjames 499
   * in the RCC_OscInitTypeDef structure.
500
   */
44 mjames 501
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
59 mjames 502
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
16 mjames 503
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
44 mjames 504
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
505
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
29 mjames 506
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
58 mjames 507
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
508
  {
509
    Error_Handler();
510
  }
61 mjames 511
 
50 mjames 512
  /** Initializes the CPU, AHB and APB buses clocks
62 mjames 513
   */
514
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
16 mjames 515
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
516
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
29 mjames 517
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
16 mjames 518
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
50 mjames 519
 
58 mjames 520
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
521
  {
522
    Error_Handler();
523
  }
2 mjames 524
}
525
 
50 mjames 526
/**
65 mjames 527
 * @brief I2C1 Initialization Function
528
 * @param None
529
 * @retval None
530
 */
531
static void MX_I2C1_Init(void)
532
{
533
 
534
  /* USER CODE BEGIN I2C1_Init 0 */
535
 
536
  /* USER CODE END I2C1_Init 0 */
537
 
538
  /* USER CODE BEGIN I2C1_Init 1 */
539
 
540
  /* USER CODE END I2C1_Init 1 */
541
  hi2c1.Instance = I2C1;
542
  hi2c1.Init.ClockSpeed = 100000;
543
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
544
  hi2c1.Init.OwnAddress1 = 0;
545
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
546
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
547
  hi2c1.Init.OwnAddress2 = 0;
548
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
549
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
550
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
551
  {
552
    Error_Handler();
553
  }
554
  /* USER CODE BEGIN I2C1_Init 2 */
555
 
556
  /* USER CODE END I2C1_Init 2 */
557
}
558
 
559
/**
62 mjames 560
 * @brief SPI1 Initialization Function
561
 * @param None
562
 * @retval None
563
 */
58 mjames 564
static void MX_SPI1_Init(void)
5 mjames 565
{
2 mjames 566
 
50 mjames 567
  /* USER CODE BEGIN SPI1_Init 0 */
568
 
569
  /* USER CODE END SPI1_Init 0 */
570
 
571
  /* USER CODE BEGIN SPI1_Init 1 */
572
 
573
  /* USER CODE END SPI1_Init 1 */
574
  /* SPI1 parameter configuration*/
16 mjames 575
  hspi1.Instance = SPI1;
576
  hspi1.Init.Mode = SPI_MODE_MASTER;
577
  hspi1.Init.Direction = SPI_DIRECTION_1LINE;
578
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
579
  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
580
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
581
  hspi1.Init.NSS = SPI_NSS_SOFT;
50 mjames 582
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
16 mjames 583
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
584
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
585
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
586
  hspi1.Init.CRCPolynomial = 10;
58 mjames 587
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
588
  {
589
    Error_Handler();
590
  }
50 mjames 591
  /* USER CODE BEGIN SPI1_Init 2 */
2 mjames 592
 
50 mjames 593
  /* USER CODE END SPI1_Init 2 */
2 mjames 594
}
595
 
50 mjames 596
/**
62 mjames 597
 * @brief TIM2 Initialization Function
598
 * @param None
599
 * @retval None
600
 */
58 mjames 601
static void MX_TIM2_Init(void)
50 mjames 602
{
603
 
604
  /* USER CODE BEGIN TIM2_Init 0 */
605
 
606
  /* USER CODE END TIM2_Init 0 */
607
 
58 mjames 608
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
609
  TIM_MasterConfigTypeDef sMasterConfig = {0};
50 mjames 610
 
611
  /* USER CODE BEGIN TIM2_Init 1 */
612
 
613
  /* USER CODE END TIM2_Init 1 */
614
  htim2.Instance = TIM2;
615
  htim2.Init.Prescaler = 0;
616
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
617
  htim2.Init.Period = 65535;
618
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
619
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
58 mjames 620
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
621
  {
622
    Error_Handler();
623
  }
50 mjames 624
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
58 mjames 625
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
626
  {
627
    Error_Handler();
628
  }
50 mjames 629
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
630
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
58 mjames 631
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
632
  {
633
    Error_Handler();
634
  }
50 mjames 635
  /* USER CODE BEGIN TIM2_Init 2 */
636
 
637
  /* USER CODE END TIM2_Init 2 */
638
}
639
 
640
/**
62 mjames 641
 * @brief TIM3 Initialization Function
642
 * @param None
643
 * @retval None
644
 */
58 mjames 645
static void MX_TIM3_Init(void)
44 mjames 646
{
647
 
50 mjames 648
  /* USER CODE BEGIN TIM3_Init 0 */
44 mjames 649
 
50 mjames 650
  /* USER CODE END TIM3_Init 0 */
651
 
58 mjames 652
  TIM_Encoder_InitTypeDef sConfig = {0};
653
  TIM_MasterConfigTypeDef sMasterConfig = {0};
50 mjames 654
 
655
  /* USER CODE BEGIN TIM3_Init 1 */
656
 
657
  /* USER CODE END TIM3_Init 1 */
44 mjames 658
  htim3.Instance = TIM3;
659
  htim3.Init.Prescaler = 0;
660
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
50 mjames 661
  htim3.Init.Period = 65535;
662
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
663
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
44 mjames 664
  sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
50 mjames 665
  sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
44 mjames 666
  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
667
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
668
  sConfig.IC1Filter = 15;
50 mjames 669
  sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
44 mjames 670
  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
671
  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
672
  sConfig.IC2Filter = 15;
58 mjames 673
  if (HAL_TIM_Encoder_Init(&htim3, &sConfig) != HAL_OK)
674
  {
675
    Error_Handler();
676
  }
44 mjames 677
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
678
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
58 mjames 679
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
680
  {
681
    Error_Handler();
682
  }
50 mjames 683
  /* USER CODE BEGIN TIM3_Init 2 */
44 mjames 684
 
50 mjames 685
  /* USER CODE END TIM3_Init 2 */
44 mjames 686
}
687
 
50 mjames 688
/**
62 mjames 689
 * @brief TIM9 Initialization Function
690
 * @param None
691
 * @retval None
692
 */
58 mjames 693
static void MX_TIM9_Init(void)
44 mjames 694
{
695
 
50 mjames 696
  /* USER CODE BEGIN TIM9_Init 0 */
44 mjames 697
 
50 mjames 698
  /* USER CODE END TIM9_Init 0 */
699
 
58 mjames 700
  TIM_Encoder_InitTypeDef sConfig = {0};
701
  TIM_MasterConfigTypeDef sMasterConfig = {0};
50 mjames 702
 
703
  /* USER CODE BEGIN TIM9_Init 1 */
704
 
705
  /* USER CODE END TIM9_Init 1 */
44 mjames 706
  htim9.Instance = TIM9;
707
  htim9.Init.Prescaler = 0;
708
  htim9.Init.CounterMode = TIM_COUNTERMODE_UP;
50 mjames 709
  htim9.Init.Period = 65535;
710
  htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
711
  htim9.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
44 mjames 712
  sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
50 mjames 713
  sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
44 mjames 714
  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
715
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
716
  sConfig.IC1Filter = 15;
50 mjames 717
  sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
44 mjames 718
  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
719
  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
50 mjames 720
  sConfig.IC2Filter = 0;
58 mjames 721
  if (HAL_TIM_Encoder_Init(&htim9, &sConfig) != HAL_OK)
722
  {
723
    Error_Handler();
724
  }
44 mjames 725
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
726
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
58 mjames 727
  if (HAL_TIMEx_MasterConfigSynchronization(&htim9, &sMasterConfig) != HAL_OK)
728
  {
729
    Error_Handler();
730
  }
50 mjames 731
  /* USER CODE BEGIN TIM9_Init 2 */
44 mjames 732
 
50 mjames 733
  /* USER CODE END TIM9_Init 2 */
60 mjames 734
}
50 mjames 735
 
60 mjames 736
/**
62 mjames 737
 * @brief UART4 Initialization Function
738
 * @param None
739
 * @retval None
740
 */
60 mjames 741
static void MX_UART4_Init(void)
742
{
743
 
744
  /* USER CODE BEGIN UART4_Init 0 */
745
 
746
  /* USER CODE END UART4_Init 0 */
747
 
748
  /* USER CODE BEGIN UART4_Init 1 */
749
 
750
  /* USER CODE END UART4_Init 1 */
751
  huart4.Instance = UART4;
752
  huart4.Init.BaudRate = 4800;
753
  huart4.Init.WordLength = UART_WORDLENGTH_8B;
754
  huart4.Init.StopBits = UART_STOPBITS_1;
755
  huart4.Init.Parity = UART_PARITY_NONE;
756
  huart4.Init.Mode = UART_MODE_TX_RX;
757
  huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
758
  huart4.Init.OverSampling = UART_OVERSAMPLING_16;
759
  if (HAL_UART_Init(&huart4) != HAL_OK)
760
  {
761
    Error_Handler();
762
  }
763
  /* USER CODE BEGIN UART4_Init 2 */
764
 
765
  /* USER CODE END UART4_Init 2 */
44 mjames 766
}
767
 
50 mjames 768
/**
62 mjames 769
 * @brief USART1 Initialization Function
770
 * @param None
771
 * @retval None
772
 */
58 mjames 773
static void MX_USART1_UART_Init(void)
5 mjames 774
{
3 mjames 775
 
50 mjames 776
  /* USER CODE BEGIN USART1_Init 0 */
777
 
778
  /* USER CODE END USART1_Init 0 */
779
 
780
  /* USER CODE BEGIN USART1_Init 1 */
781
 
782
  /* USER CODE END USART1_Init 1 */
16 mjames 783
  huart1.Instance = USART1;
784
  huart1.Init.BaudRate = 19200;
785
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
44 mjames 786
  huart1.Init.StopBits = UART_STOPBITS_1;
16 mjames 787
  huart1.Init.Parity = UART_PARITY_NONE;
788
  huart1.Init.Mode = UART_MODE_TX_RX;
789
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
790
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
58 mjames 791
  if (HAL_UART_Init(&huart1) != HAL_OK)
792
  {
793
    Error_Handler();
794
  }
50 mjames 795
  /* USER CODE BEGIN USART1_Init 2 */
3 mjames 796
 
50 mjames 797
  /* USER CODE END USART1_Init 2 */
3 mjames 798
}
799
 
50 mjames 800
/**
62 mjames 801
 * @brief USART2 Initialization Function
802
 * @param None
803
 * @retval None
804
 */
58 mjames 805
static void MX_USART2_UART_Init(void)
5 mjames 806
{
2 mjames 807
 
50 mjames 808
  /* USER CODE BEGIN USART2_Init 0 */
809
 
810
  /* USER CODE END USART2_Init 0 */
811
 
812
  /* USER CODE BEGIN USART2_Init 1 */
813
 
814
  /* USER CODE END USART2_Init 1 */
16 mjames 815
  huart2.Instance = USART2;
816
  huart2.Init.BaudRate = 115200;
817
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
818
  huart2.Init.StopBits = UART_STOPBITS_1;
819
  huart2.Init.Parity = UART_PARITY_NONE;
820
  huart2.Init.Mode = UART_MODE_TX_RX;
821
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
822
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
58 mjames 823
  if (HAL_UART_Init(&huart2) != HAL_OK)
824
  {
825
    Error_Handler();
826
  }
50 mjames 827
  /* USER CODE BEGIN USART2_Init 2 */
2 mjames 828
 
50 mjames 829
  /* USER CODE END USART2_Init 2 */
2 mjames 830
}
831
 
50 mjames 832
/**
62 mjames 833
 * @brief USART3 Initialization Function
834
 * @param None
835
 * @retval None
836
 */
58 mjames 837
static void MX_USART3_UART_Init(void)
23 mjames 838
{
839
 
50 mjames 840
  /* USER CODE BEGIN USART3_Init 0 */
841
 
842
  /* USER CODE END USART3_Init 0 */
843
 
844
  /* USER CODE BEGIN USART3_Init 1 */
845
 
846
  /* USER CODE END USART3_Init 1 */
23 mjames 847
  huart3.Instance = USART3;
58 mjames 848
  huart3.Init.BaudRate = 19200;
23 mjames 849
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
50 mjames 850
  huart3.Init.StopBits = UART_STOPBITS_1;
44 mjames 851
  huart3.Init.Parity = UART_PARITY_NONE;
23 mjames 852
  huart3.Init.Mode = UART_MODE_TX_RX;
853
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
854
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
58 mjames 855
  if (HAL_UART_Init(&huart3) != HAL_OK)
856
  {
857
    Error_Handler();
858
  }
50 mjames 859
  /* USER CODE BEGIN USART3_Init 2 */
23 mjames 860
 
50 mjames 861
  /* USER CODE END USART3_Init 2 */
23 mjames 862
}
863
 
50 mjames 864
/**
62 mjames 865
 * @brief GPIO Initialization Function
866
 * @param None
867
 * @retval None
868
 */
58 mjames 869
static void MX_GPIO_Init(void)
5 mjames 870
{
58 mjames 871
  GPIO_InitTypeDef GPIO_InitStruct = {0};
2 mjames 872
 
16 mjames 873
  /* GPIO Ports Clock Enable */
29 mjames 874
  __HAL_RCC_GPIOH_CLK_ENABLE();
875
  __HAL_RCC_GPIOA_CLK_ENABLE();
876
  __HAL_RCC_GPIOC_CLK_ENABLE();
877
  __HAL_RCC_GPIOB_CLK_ENABLE();
2 mjames 878
 
16 mjames 879
  /*Configure GPIO pin Output Level */
58 mjames 880
  HAL_GPIO_WritePin(SPI_NSS1_GPIO_Port, SPI_NSS1_Pin, GPIO_PIN_SET);
2 mjames 881
 
16 mjames 882
  /*Configure GPIO pin Output Level */
62 mjames 883
  HAL_GPIO_WritePin(GPIOA, SPI_CD_Pin | BT_BUTTON_Pin, GPIO_PIN_RESET);
2 mjames 884
 
50 mjames 885
  /*Configure GPIO pin Output Level */
62 mjames 886
  HAL_GPIO_WritePin(GPIOC, SPI_RESET_Pin | POWER_LATCH_Pin | USB_PWR_Pin, GPIO_PIN_RESET);
50 mjames 887
 
888
  /*Configure GPIO pin Output Level */
58 mjames 889
  HAL_GPIO_WritePin(SPI_NSS2_GPIO_Port, SPI_NSS2_Pin, GPIO_PIN_SET);
50 mjames 890
 
891
  /*Configure GPIO pins : SPI_NSS1_Pin SPI_CD_Pin */
62 mjames 892
  GPIO_InitStruct.Pin = SPI_NSS1_Pin | SPI_CD_Pin;
16 mjames 893
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
29 mjames 894
  GPIO_InitStruct.Pull = GPIO_NOPULL;
16 mjames 895
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
58 mjames 896
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
2 mjames 897
 
24 mjames 898
  /*Configure GPIO pins : SPI_RESET_Pin SPI_NSS2_Pin POWER_LATCH_Pin USB_PWR_Pin */
62 mjames 899
  GPIO_InitStruct.Pin = SPI_RESET_Pin | SPI_NSS2_Pin | POWER_LATCH_Pin | USB_PWR_Pin;
16 mjames 900
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
29 mjames 901
  GPIO_InitStruct.Pull = GPIO_NOPULL;
16 mjames 902
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
58 mjames 903
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
2 mjames 904
 
61 mjames 905
  /*Configure GPIO pins : BT_STATE_Pin SW1_PUSH_Pin SW2_PUSH_Pin */
62 mjames 906
  GPIO_InitStruct.Pin = BT_STATE_Pin | SW1_PUSH_Pin | SW2_PUSH_Pin;
16 mjames 907
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
32 mjames 908
  GPIO_InitStruct.Pull = GPIO_PULLUP;
58 mjames 909
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
5 mjames 910
 
32 mjames 911
  /*Configure GPIO pin : IGNITION_Pin */
912
  GPIO_InitStruct.Pin = IGNITION_Pin;
913
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
914
  GPIO_InitStruct.Pull = GPIO_NOPULL;
58 mjames 915
  HAL_GPIO_Init(IGNITION_GPIO_Port, &GPIO_InitStruct);
32 mjames 916
 
37 mjames 917
  /*Configure GPIO pin : BT_BUTTON_Pin */
918
  GPIO_InitStruct.Pin = BT_BUTTON_Pin;
919
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
920
  GPIO_InitStruct.Pull = GPIO_NOPULL;
921
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
58 mjames 922
  HAL_GPIO_Init(BT_BUTTON_GPIO_Port, &GPIO_InitStruct);
2 mjames 923
}
924
 
925
/* USER CODE BEGIN 4 */
926
 
927
/* USER CODE END 4 */
928
 
5 mjames 929
/**
62 mjames 930
 * @brief  This function is executed in case of error occurrence.
931
 * @retval None
932
 */
58 mjames 933
void Error_Handler(void)
5 mjames 934
{
50 mjames 935
  /* USER CODE BEGIN Error_Handler_Debug */
936
  /* User can add his own implementation to report the HAL error return state */
937
 
938
  /* USER CODE END Error_Handler_Debug */
30 mjames 939
}
5 mjames 940
 
62 mjames 941
#ifdef USE_FULL_ASSERT
2 mjames 942
/**
62 mjames 943
 * @brief  Reports the name of the source file and the source line number
944
 *         where the assert_param error has occurred.
945
 * @param  file: pointer to the source file name
946
 * @param  line: assert_param error line source number
947
 * @retval None
948
 */
50 mjames 949
void assert_failed(uint8_t *file, uint32_t line)
29 mjames 950
{
951
  /* USER CODE BEGIN 6 */
50 mjames 952
  /* User can add his own implementation to report the file name and line number,
953
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
29 mjames 954
  /* USER CODE END 6 */
955
}
50 mjames 956
#endif /* USE_FULL_ASSERT */