Subversion Repositories DashDisplay

Rev

Rev 44 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
29 mjames 2
  ******************************************************************************
3
  * File Name          : main.c
4
  * Description        : Main program body
5
  ******************************************************************************
6
  *
44 mjames 7
  * COPYRIGHT(c) 2018 STMicroelectronics
29 mjames 8
  *
9
  * Redistribution and use in source and binary forms, with or without modification,
10
  * are permitted provided that the following conditions are met:
11
  *   1. Redistributions of source code must retain the above copyright notice,
12
  *      this list of conditions and the following disclaimer.
13
  *   2. Redistributions in binary form must reproduce the above copyright notice,
14
  *      this list of conditions and the following disclaimer in the documentation
15
  *      and/or other materials provided with the distribution.
16
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
17
  *      may be used to endorse or promote products derived from this software
18
  *      without specific prior written permission.
19
  *
20
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
  *
31
  ******************************************************************************
32
  */
2 mjames 33
/* Includes ------------------------------------------------------------------*/
29 mjames 34
#include "stm32l1xx_hal.h"
2 mjames 35
 
36
/* USER CODE BEGIN Includes */
37
#include "ap_math.h"
38
#include "serial.h"
39
#include "SSD1306.h"
7 mjames 40
#include "Font.h"
2 mjames 41
#include "dials.h"
4 mjames 42
#include "switches.h"
2 mjames 43
#include <math.h>
4 mjames 44
#include "plx.h"
20 mjames 45
#include "displayinfo.h"
27 mjames 46
#include "small_printf.h"
30 mjames 47
#include "nvram.h"
2 mjames 48
 
49
/* USER CODE END Includes */
50
 
51
/* Private variables ---------------------------------------------------------*/
52
SPI_HandleTypeDef hspi1;
53
 
44 mjames 54
TIM_HandleTypeDef htim3;
55
TIM_HandleTypeDef htim9;
56
 
3 mjames 57
UART_HandleTypeDef huart1;
2 mjames 58
UART_HandleTypeDef huart2;
23 mjames 59
UART_HandleTypeDef huart3;
2 mjames 60
 
61
/* USER CODE BEGIN PV */
62
/* Private variables ---------------------------------------------------------*/
18 mjames 63
#define MAXRDG 32
2 mjames 64
 
24 mjames 65
/* timeout when the ignition is switched off */
66
#define IGNITION_OFF_TIMEOUT 30000UL
67
 
7 mjames 68
int OldObservation[2] =
30 mjames 69
{ -1, -1 }; // illegal initial value
7 mjames 70
int OldObservationIndex[2] =
30 mjames 71
{ -1, -1 }; // if more than one sensor this will be printed
7 mjames 72
int16_t dial0[2] =
30 mjames 73
{ 0, 0 };
7 mjames 74
int16_t dial1[2] =
30 mjames 75
{ -1, -1 };
14 mjames 76
 
18 mjames 77
uint16_t dial_timer[2] =
30 mjames 78
{ 0, 0 };
18 mjames 79
 
80
static const int DialTimeout = 50; // about 20 seconds after twiddle, save the dial position.
81
 
30 mjames 82
uint16_t dial_nvram[2] __attribute__((section(".NVRAM_Data")));
14 mjames 83
 
7 mjames 84
union
85
{
30 mjames 86
        PLX_SensorInfo Sensor[MAXRDG];
87
        char Bytes[MAXRDG * sizeof(PLX_SensorInfo)];
7 mjames 88
} Data;
89
int Max[MAXRDG];
90
int Min[MAXRDG];
91
int PLXItems;
24 mjames 92
 
27 mjames 93
uint32_t Latch_Timer = IGNITION_OFF_TIMEOUT;
24 mjames 94
 
2 mjames 95
/* USER CODE END PV */
96
 
97
/* Private function prototypes -----------------------------------------------*/
29 mjames 98
void SystemClock_Config(void);
99
void Error_Handler(void);
100
static void MX_GPIO_Init(void);
101
static void MX_SPI1_Init(void);
102
static void MX_USART1_UART_Init(void);
103
static void MX_USART2_UART_Init(void);
104
static void MX_USART3_UART_Init(void);
44 mjames 105
static void MX_TIM3_Init(void);
106
static void MX_TIM9_Init(void);
2 mjames 107
 
108
/* USER CODE BEGIN PFP */
109
/* Private function prototypes -----------------------------------------------*/
110
 
111
/* USER CODE END PFP */
112
 
113
/* USER CODE BEGIN 0 */
114
/* dummy function */
30 mjames 115
void _init(void)
6 mjames 116
{
2 mjames 117
 
118
}
7 mjames 119
// the dial is the switch number we are using.
120
// suppress is the ItemIndex we wish to suppress on this display
30 mjames 121
int DisplayCurrent(int dial, int suppress)
7 mjames 122
{
30 mjames 123
        char buff[10];
124
        int i;
125
        int rc;
126
        select_display(dial); // pick the display we are using
44 mjames 127
        int ItemIndex = dial_pos[dial] % PLXItems;
14 mjames 128
 
44 mjames 129
#if 0
30 mjames 130
        // wrap around count if dial too far to the right
131
        if (ItemIndex >= PLXItems)
7 mjames 132
        {
30 mjames 133
                dial_pos[dial] = 0;
134
                ItemIndex = 0;
27 mjames 135
        }
30 mjames 136
        if (ItemIndex < 0)
137
        {
138
                ItemIndex = PLXItems - 1;
139
                dial_pos[dial] = (PLXItems - 1) * 4;
140
        }
44 mjames 141
#endif
30 mjames 142
        // check for item suppression
143
        if (ItemIndex == suppress)
7 mjames 144
        {
30 mjames 145
                dial1[dial] = -1;
146
                OldObservation[dial] = -1;
147
                OldObservationIndex[dial] = -1;
2 mjames 148
 
30 mjames 149
                clearDisplay();
150
                display();
151
                return -1; // we suppressed this display
152
        }
153
        // do not try to convert if no items in buffer
154
        if (PLXItems > 0)
155
        {
156
                int DataVal = ConvPLX(Data.Sensor[ItemIndex].ReadingH,
157
                                Data.Sensor[ItemIndex].ReadingL); // data reading
158
                int Observation = ConvPLX(Data.Sensor[ItemIndex].AddrH,
159
                                Data.Sensor[ItemIndex].AddrL);
160
                int ObservationIndex = ConvPLX(0, Data.Sensor[ItemIndex].Instance);
161
                // now to convert the readings and format strings
162
                // find out limits
163
                char * msg;
164
                int len;
27 mjames 165
 
30 mjames 166
                // if the user presses the dial then reset min/max to current value
167
                if (push_pos[dial] == 1)
9 mjames 168
                {
30 mjames 169
                        Max[ItemIndex] = DataVal;
170
                        Min[ItemIndex] = DataVal; // 12 bit max value
9 mjames 171
                }
30 mjames 172
 
173
                if (Observation < PLX_MAX_OBS)
27 mjames 174
                {
30 mjames 175
                        if (Observation != OldObservation[dial]
176
                                        || ObservationIndex != OldObservationIndex[dial])
177
                        {
9 mjames 178
 
30 mjames 179
                                dial_timer[dial] = DialTimeout;
9 mjames 180
 
30 mjames 181
                                dial1[dial] = -1;
182
                                clearDisplay();
183
                                dial_draw_scale(DisplayInfo[Observation].Low,
184
                                                DisplayInfo[Observation].High, 12, 1,
185
                                                DisplayInfo[Observation].TickScale);
14 mjames 186
 
30 mjames 187
                                msg = DisplayInfo[Observation].name;
188
                                len = 7;
189
                                int len1 = ObservationIndex > 0 ? len - 1 : len;
190
                                for (i = 0; i < len1 && msg[i]; i++)
191
                                {
192
                                        buff[i] = msg[i];
193
                                }
194
                                if (ObservationIndex > 0 && i < len)
195
                                {
196
                                        buff[i++] = ObservationIndex + '1';
197
                                }
14 mjames 198
 
30 mjames 199
                                print_large_string(buff, 64 - i * 4, 48, i); // this prints spaces for \0 at end of string
14 mjames 200
 
30 mjames 201
                                // print suffix if present.
202
                                font_gotoxy(15, 4);
203
                                int i = 0;
204
                                while (DisplayInfo[Observation].suffix[i])
205
                                {
206
                                        font_putchar(DisplayInfo[Observation].suffix[i++]);
207
                                }
208
 
209
                                OldObservation[dial] = Observation;
210
                                OldObservationIndex[dial] = ObservationIndex;
211
                                //
212
                                display();
213
 
214
                        }
215
                        else
27 mjames 216
                        {
30 mjames 217
                                // check for timer timeout on consistent timer
218
                                if (dial_timer[dial])
219
                                {
220
                                        dial_timer[dial]--;
2 mjames 221
 
30 mjames 222
                                        if (dial_timer[dial] == 0)
223
                                        {
224
                                                uint16_t curr_val = dial_pos[dial];
225
 
31 mjames 226
                                                uint32_t addr = (uint32_t) (&dial_nvram[dial]);
227
                                            WriteUint16NVRAM(addr, curr_val );
30 mjames 228
 
229
                                        }
230
                                }
7 mjames 231
                        }
30 mjames 232
 
27 mjames 233
                }
2 mjames 234
 
30 mjames 235
                double max_rdg;
236
                double min_rdg;
237
                double cur_rdg;
238
                int int_rdg;
239
                int int_max;
240
                int int_min;
21 mjames 241
 
30 mjames 242
                max_rdg = ConveriMFDRaw2Data(Observation,
243
                                DisplayInfo[Observation].Units, Max[ItemIndex]);
244
                min_rdg = ConveriMFDRaw2Data(Observation,
245
                                DisplayInfo[Observation].Units, Min[ItemIndex]);
246
                cur_rdg = ConveriMFDRaw2Data(Observation,
247
                                DisplayInfo[Observation].Units, DataVal);
18 mjames 248
 
30 mjames 249
                int dp_pos;  // where to print the decimal place
250
                float scale = 1.0;
251
                switch (DisplayInfo[Observation].DP)
252
                {
253
                case 0:
254
                        scale = 1.0;
255
                        dp_pos = 100;
256
                        break;
257
                case 1:
258
                        scale = 10.0;
259
                        dp_pos = 1;
260
                        break;
261
                case 2:
262
                        scale = 100.0;
263
                        dp_pos = 2;
264
                        break;
265
                }
266
                int_rdg = (int) (cur_rdg * scale);
267
                int_max = (int) (max_rdg * scale);
268
                int_min = (int) (min_rdg * scale);
18 mjames 269
 
30 mjames 270
                cur_rdg -= DisplayInfo[Observation].Low;
47 mjames 271
                cur_rdg =  SINE_STEPS * cur_rdg
30 mjames 272
                                                / (DisplayInfo[Observation].High
273
                                                                - DisplayInfo[Observation].Low);
2 mjames 274
 
30 mjames 275
                dial0[dial] = (int) cur_rdg;
2 mjames 276
 
30 mjames 277
                /* old needle un-draw */
278
                if (dial1[dial] >= 0)
279
                {
280
                        dial_draw_needle(dial1[dial]);
281
                }
282
                dial_draw_needle(dial0[dial]);
283
                // print value overlaid by needle
284
                // this is actual reading
285
                print_digits(30, 30, 5, dp_pos, int_rdg);
286
                font_gotoxy(0, 0);
287
                font_digits(5, dp_pos, int_min);
2 mjames 288
 
30 mjames 289
                font_gotoxy(0, 1);
290
                font_puts("Min");
2 mjames 291
 
30 mjames 292
                font_gotoxy(15, 0);
293
                font_digits(5, dp_pos, int_max);
294
                font_gotoxy(18, 1);
295
                font_puts("Max");
2 mjames 296
 
30 mjames 297
                dial1[dial] = dial0[dial];
15 mjames 298
 
30 mjames 299
                display();
2 mjames 300
 
7 mjames 301
        }
30 mjames 302
return ItemIndex;
7 mjames 303
}
304
/* USER CODE END 0 */
2 mjames 305
 
29 mjames 306
int main(void)
7 mjames 307
{
2 mjames 308
 
16 mjames 309
  /* USER CODE BEGIN 1 */
2 mjames 310
 
30 mjames 311
GPIO_InitTypeDef GPIO_InitStruct;
2 mjames 312
 
30 mjames 313
__HAL_RCC_SPI1_CLK_ENABLE()
314
;
315
__HAL_RCC_USART1_CLK_ENABLE()
316
; // PLX main port
317
__HAL_RCC_USART2_CLK_ENABLE()
318
; // debug port
319
__HAL_RCC_USART3_CLK_ENABLE ()
320
; // Bluetooth port
23 mjames 321
 
44 mjames 322
__HAL_RCC_TIM3_CLK_ENABLE();
323
 
324
__HAL_RCC_TIM9_CLK_ENABLE();
16 mjames 325
  /* USER CODE END 1 */
2 mjames 326
 
16 mjames 327
  /* MCU Configuration----------------------------------------------------------*/
6 mjames 328
 
16 mjames 329
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
29 mjames 330
  HAL_Init();
2 mjames 331
 
16 mjames 332
  /* Configure the system clock */
29 mjames 333
  SystemClock_Config();
2 mjames 334
 
16 mjames 335
  /* Initialize all configured peripherals */
29 mjames 336
  MX_GPIO_Init();
337
  MX_SPI1_Init();
338
  MX_USART1_UART_Init();
339
  MX_USART2_UART_Init();
340
  MX_USART3_UART_Init();
44 mjames 341
  MX_TIM3_Init();
342
  MX_TIM9_Init();
2 mjames 343
 
16 mjames 344
  /* USER CODE BEGIN 2 */
2 mjames 345
 
30 mjames 346
/* Turn on USART1 IRQ */
347
HAL_NVIC_SetPriority(USART1_IRQn, 2, 0);
348
HAL_NVIC_EnableIRQ(USART1_IRQn);
4 mjames 349
 
30 mjames 350
/* Turn on USART2 IRQ  */
351
HAL_NVIC_SetPriority(USART2_IRQn, 4, 0);
352
HAL_NVIC_EnableIRQ(USART2_IRQn);
2 mjames 353
 
30 mjames 354
/* turn on USART3 IRQ */
355
HAL_NVIC_SetPriority(USART3_IRQn, 4, 0);
356
HAL_NVIC_EnableIRQ(USART3_IRQn);
4 mjames 357
 
30 mjames 358
/* setup the USART control blocks */
359
init_usart_ctl(&uc1, huart1.Instance);
360
init_usart_ctl(&uc2, huart2.Instance);
361
init_usart_ctl(&uc3, huart3.Instance);
23 mjames 362
 
30 mjames 363
EnableSerialRxInterrupt(&uc1);
364
EnableSerialRxInterrupt(&uc2);
365
EnableSerialRxInterrupt(&uc3);
23 mjames 366
 
44 mjames 367
HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);
23 mjames 368
 
44 mjames 369
HAL_TIM_Encoder_Start(&htim9, TIM_CHANNEL_ALL);
370
 
30 mjames 371
InitSwitches();
23 mjames 372
 
30 mjames 373
int i;
374
uint16_t rc;
375
for (i = 0; i < 2; i++)
376
{
377
        dial_pos[i] = dial_nvram[i];
378
}
2 mjames 379
 
30 mjames 380
ap_init(); // set up the approximate math library
23 mjames 381
 
30 mjames 382
int disp;
7 mjames 383
 
30 mjames 384
ssd1306_begin(1, 0);
385
dial_origin(64, 60);
386
dial_size(60);
16 mjames 387
 
30 mjames 388
/* reset the display timeout, latch on power from accessories */
389
Latch_Timer = IGNITION_OFF_TIMEOUT;
390
HAL_GPIO_WritePin(POWER_LATCH_GPIO_Port, POWER_LATCH_Pin, GPIO_PIN_RESET);
17 mjames 391
 
30 mjames 392
for (disp = 0; disp < 2; disp++)
393
{
394
        select_display(disp);
395
        clearDisplay();
396
        dim(0);
397
        //font_puts(
398
        //              "Hello world !!\rThis text is a test of the text rendering library in a 5*7 font");
18 mjames 399
 
30 mjames 400
        dial_draw_scale(0, 10, 12, 5, 1);
401
        char buffer[] = "Display  ";
402
        buffer[8] = disp + '1';
403
        print_large_string(buffer, 20, 30, 9);
18 mjames 404
 
30 mjames 405
        display();
7 mjames 406
 
30 mjames 407
}
7 mjames 408
 
16 mjames 409
  /* USER CODE END 2 */
7 mjames 410
 
16 mjames 411
  /* Infinite loop */
412
  /* USER CODE BEGIN WHILE */
30 mjames 413
uint32_t Ticks = HAL_GetTick() + 100;
7 mjames 414
 
30 mjames 415
/* while ignition is on, keep resetting power latch timer */
416
if (HAL_GPIO_ReadPin(IGNITION_GPIO_Port, IGNITION_Pin) == GPIO_PIN_RESET)
417
{
418
        Latch_Timer = HAL_GetTick() + IGNITION_OFF_TIMEOUT;
419
}
420
else
421
{
422
        /* if the ignition has been off for a while, then turn off power */
423
        if (HAL_GetTick() > Latch_Timer)
24 mjames 424
        {
30 mjames 425
                HAL_GPIO_WritePin(POWER_LATCH_GPIO_Port, POWER_LATCH_Pin,
426
                                GPIO_PIN_RESET);
24 mjames 427
        }
30 mjames 428
}
27 mjames 429
 
30 mjames 430
uint32_t timeout = 0;  //
431
// PLX decoder protocols
432
char PLXPacket = 0;
433
for (i = 0; i < MAXRDG; i++)
434
{
435
        Max[i] = 0;
436
        Min[i] = 0xFFF; // 12 bit max value
437
}
27 mjames 438
 
30 mjames 439
int PLXPtr = 0;
27 mjames 440
 
30 mjames 441
while (1)
442
{
27 mjames 443
// poll switche
30 mjames 444
        HandleSwitches();
27 mjames 445
// Handle the bluetooth pairing function by pressing both buttons.
30 mjames 446
        if ((push_pos[0] == 1) && (push_pos[1] == 1))
24 mjames 447
        {
30 mjames 448
                HAL_GPIO_WritePin(BT_BUTTON_GPIO_Port, BT_BUTTON_Pin, GPIO_PIN_SET);
24 mjames 449
        }
30 mjames 450
        else
27 mjames 451
        {
30 mjames 452
                HAL_GPIO_WritePin(BT_BUTTON_GPIO_Port, BT_BUTTON_Pin, GPIO_PIN_RESET);
24 mjames 453
 
7 mjames 454
        }
455
 
30 mjames 456
        uint16_t cc = SerialCharsReceived(&uc1);
457
        int chr;
458
        if (cc == 0)
27 mjames 459
        {
30 mjames 460
                timeout++;
461
                if (timeout % 1000 == 0)
462
                {
38 mjames 463
                        const char msg[] = "No data\r\n";
464
                        char * p = msg;
465
                        while(*p)
466
                        {
467
                                PutCharSerial(&uc2,*p++);
468
                        }
469
 
30 mjames 470
                }
38 mjames 471
 
30 mjames 472
                if (timeout > 60000)
473
                {
474
                        // do turn off screen
475
                }
7 mjames 476
 
27 mjames 477
        }
30 mjames 478
        for (chr = 0; chr < cc; chr++)
7 mjames 479
        {
30 mjames 480
                char c = GetCharSerial(&uc1);
481
                timeout = 0;
27 mjames 482
 
30 mjames 483
                if (c == PLX_Start) // at any time if the start byte appears, reset the pointers
23 mjames 484
                {
30 mjames 485
                        PLXPtr = 0;    // reset the pointer
486
                        PLXPacket = 1;
487
                }
488
                else if (c == PLX_Stop)
489
                {
490
                        if (PLXPacket)
491
                        {
492
                                // we can now decode the selected parameter
493
                                PLXItems = PLXPtr / sizeof(PLX_SensorInfo); // total
494
                                // saturate the rotary switch position
7 mjames 495
 
30 mjames 496
                                int DataVal;
497
                                // process min/max
498
                                for (i = 0; i < PLXItems; i++)
499
                                {
500
                                        // Send item to BT
501
                                        uint16_t addr = ConvPLX(Data.Sensor[i].AddrH,
502
                                                        Data.Sensor[i].AddrL);
503
                                        uint8_t inst = Data.Sensor[i].Instance;
504
                                        uint16_t reading = ConvPLX(Data.Sensor[i].ReadingH,
505
                                                        Data.Sensor[i].ReadingL);
9 mjames 506
 
30 mjames 507
                                        char outbuff[100];
37 mjames 508
                                        int cnt = small_sprintf(outbuff, "%d,%d,%d\n\r", addr, inst,
30 mjames 509
                                                        reading);
37 mjames 510
                                        int ck=0;
511
                                        while(outbuff[ck] && ck < 100)
23 mjames 512
 
30 mjames 513
                                        {
38 mjames 514
                                                PutCharSerial(&uc2, outbuff[ck++]);
30 mjames 515
                                        }
516
                                        DataVal = ConvPLX(Data.Sensor[i].ReadingH,
517
                                                        Data.Sensor[i].ReadingL);
518
                                        if (DataVal > Max[i])
519
                                        {
520
                                                Max[i] = DataVal;
521
                                        }
522
                                        if (DataVal < Min[i])
523
                                        {
524
                                                Min[i] = DataVal;
525
                                        }
526
                                }
527
 
528
                                // now to display the information
529
                                int suppress = DisplayCurrent(0, -1);
530
                                DisplayCurrent(1, suppress);
23 mjames 531
                        }
30 mjames 532
                        PLXPtr = 0;
533
                        PLXPacket = 0;
6 mjames 534
                }
30 mjames 535
                else if (c > PLX_Stop) // illegal char, restart reading
536
                {
537
                        PLXPacket = 0;
538
                        PLXPtr = 0;
539
                }
540
                else if (PLXPtr < sizeof(Data.Bytes))
541
                {
542
                        Data.Bytes[PLXPtr++] = c;
543
                }
27 mjames 544
        }
4 mjames 545
 
30 mjames 546
        HAL_Delay(1);
547
}
16 mjames 548
  /* USER CODE END WHILE */
2 mjames 549
 
16 mjames 550
  /* USER CODE BEGIN 3 */
6 mjames 551
 
16 mjames 552
  /* USER CODE END 3 */
553
 
2 mjames 554
}
555
 
556
/** System Clock Configuration
29 mjames 557
*/
558
void SystemClock_Config(void)
5 mjames 559
{
2 mjames 560
 
16 mjames 561
  RCC_OscInitTypeDef RCC_OscInitStruct;
562
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
2 mjames 563
 
29 mjames 564
  __HAL_RCC_PWR_CLK_ENABLE();
565
 
566
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
567
 
44 mjames 568
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
569
  RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
16 mjames 570
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
44 mjames 571
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
572
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
29 mjames 573
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
574
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
575
  {
576
    Error_Handler();
577
  }
2 mjames 578
 
29 mjames 579
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
580
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
16 mjames 581
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
582
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
29 mjames 583
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
16 mjames 584
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
29 mjames 585
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
586
  {
587
    Error_Handler();
588
  }
2 mjames 589
 
29 mjames 590
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
2 mjames 591
 
29 mjames 592
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
2 mjames 593
 
16 mjames 594
  /* SysTick_IRQn interrupt configuration */
29 mjames 595
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
2 mjames 596
}
597
 
598
/* SPI1 init function */
29 mjames 599
static void MX_SPI1_Init(void)
5 mjames 600
{
2 mjames 601
 
16 mjames 602
  hspi1.Instance = SPI1;
603
  hspi1.Init.Mode = SPI_MODE_MASTER;
604
  hspi1.Init.Direction = SPI_DIRECTION_1LINE;
605
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
606
  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
607
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
608
  hspi1.Init.NSS = SPI_NSS_SOFT;
29 mjames 609
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
16 mjames 610
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
611
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
612
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
613
  hspi1.Init.CRCPolynomial = 10;
29 mjames 614
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
615
  {
616
    Error_Handler();
617
  }
2 mjames 618
 
619
}
620
 
44 mjames 621
/* TIM3 init function */
622
static void MX_TIM3_Init(void)
623
{
624
 
625
  TIM_Encoder_InitTypeDef sConfig;
626
  TIM_MasterConfigTypeDef sMasterConfig;
627
 
628
  htim3.Instance = TIM3;
629
  htim3.Init.Prescaler = 0;
630
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
631
  htim3.Init.Period = 0xffff;
632
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4;
633
  sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
634
  sConfig.IC1Polarity = TIM_ICPOLARITY_FALLING;
635
  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
636
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
637
  sConfig.IC1Filter = 15;
638
  sConfig.IC2Polarity = TIM_ICPOLARITY_FALLING;
639
  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
640
  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
641
  sConfig.IC2Filter = 15;
642
  if (HAL_TIM_Encoder_Init(&htim3, &sConfig) != HAL_OK)
643
  {
644
    Error_Handler();
645
  }
646
 
647
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
648
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
649
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
650
  {
651
    Error_Handler();
652
  }
653
 
654
}
655
 
656
/* TIM9 init function */
657
static void MX_TIM9_Init(void)
658
{
659
 
660
  TIM_Encoder_InitTypeDef sConfig;
661
  TIM_MasterConfigTypeDef sMasterConfig;
662
 
663
  htim9.Instance = TIM9;
664
  htim9.Init.Prescaler = 0;
665
  htim9.Init.CounterMode = TIM_COUNTERMODE_UP;
666
  htim9.Init.Period = 0xffff;
667
  htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4;
668
  sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
669
  sConfig.IC1Polarity = TIM_ICPOLARITY_FALLING;
670
  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
671
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
672
  sConfig.IC1Filter = 15;
673
  sConfig.IC2Polarity = TIM_ICPOLARITY_FALLING;
674
  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
675
  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
676
  sConfig.IC2Filter = 15;
677
  if (HAL_TIM_Encoder_Init(&htim9, &sConfig) != HAL_OK)
678
  {
679
    Error_Handler();
680
  }
681
 
682
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
683
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
684
  if (HAL_TIMEx_MasterConfigSynchronization(&htim9, &sMasterConfig) != HAL_OK)
685
  {
686
    Error_Handler();
687
  }
688
 
689
}
690
 
3 mjames 691
/* USART1 init function */
29 mjames 692
static void MX_USART1_UART_Init(void)
5 mjames 693
{
3 mjames 694
 
16 mjames 695
  huart1.Instance = USART1;
696
  huart1.Init.BaudRate = 19200;
697
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
44 mjames 698
  huart1.Init.StopBits = UART_STOPBITS_1;
16 mjames 699
  huart1.Init.Parity = UART_PARITY_NONE;
700
  huart1.Init.Mode = UART_MODE_TX_RX;
701
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
702
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
29 mjames 703
  if (HAL_UART_Init(&huart1) != HAL_OK)
704
  {
705
    Error_Handler();
706
  }
3 mjames 707
 
708
}
709
 
2 mjames 710
/* USART2 init function */
29 mjames 711
static void MX_USART2_UART_Init(void)
5 mjames 712
{
2 mjames 713
 
16 mjames 714
  huart2.Instance = USART2;
715
  huart2.Init.BaudRate = 115200;
716
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
717
  huart2.Init.StopBits = UART_STOPBITS_1;
718
  huart2.Init.Parity = UART_PARITY_NONE;
719
  huart2.Init.Mode = UART_MODE_TX_RX;
720
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
721
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
29 mjames 722
  if (HAL_UART_Init(&huart2) != HAL_OK)
723
  {
724
    Error_Handler();
725
  }
2 mjames 726
 
727
}
728
 
23 mjames 729
/* USART3 init function */
29 mjames 730
static void MX_USART3_UART_Init(void)
23 mjames 731
{
732
 
733
  huart3.Instance = USART3;
734
  huart3.Init.BaudRate = 19200;
735
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
44 mjames 736
  huart3.Init.StopBits = UART_STOPBITS_2;
737
  huart3.Init.Parity = UART_PARITY_NONE;
23 mjames 738
  huart3.Init.Mode = UART_MODE_TX_RX;
739
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
740
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
29 mjames 741
  if (HAL_UART_Init(&huart3) != HAL_OK)
742
  {
743
    Error_Handler();
744
  }
23 mjames 745
 
746
}
747
 
7 mjames 748
/** Configure pins as
29 mjames 749
        * Analog
750
        * Input
751
        * Output
752
        * EVENT_OUT
753
        * EXTI
754
*/
755
static void MX_GPIO_Init(void)
5 mjames 756
{
2 mjames 757
 
16 mjames 758
  GPIO_InitTypeDef GPIO_InitStruct;
2 mjames 759
 
16 mjames 760
  /* GPIO Ports Clock Enable */
29 mjames 761
  __HAL_RCC_GPIOH_CLK_ENABLE();
762
  __HAL_RCC_GPIOA_CLK_ENABLE();
763
  __HAL_RCC_GPIOC_CLK_ENABLE();
764
  __HAL_RCC_GPIOB_CLK_ENABLE();
2 mjames 765
 
16 mjames 766
  /*Configure GPIO pin Output Level */
30 mjames 767
  HAL_GPIO_WritePin(GPIOA, SPI_NSS1_Pin|SPI1CD_Pin|BT_BUTTON_Pin, GPIO_PIN_RESET);
2 mjames 768
 
16 mjames 769
  /*Configure GPIO pin Output Level */
29 mjames 770
  HAL_GPIO_WritePin(GPIOC, SPI_RESET_Pin|SPI_NSS2_Pin|POWER_LATCH_Pin|USB_PWR_Pin, GPIO_PIN_RESET);
2 mjames 771
 
37 mjames 772
  /*Configure GPIO pins : SPI_NSS1_Pin SPI1CD_Pin */
773
  GPIO_InitStruct.Pin = SPI_NSS1_Pin|SPI1CD_Pin;
16 mjames 774
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
29 mjames 775
  GPIO_InitStruct.Pull = GPIO_NOPULL;
16 mjames 776
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
29 mjames 777
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
2 mjames 778
 
24 mjames 779
  /*Configure GPIO pins : SPI_RESET_Pin SPI_NSS2_Pin POWER_LATCH_Pin USB_PWR_Pin */
29 mjames 780
  GPIO_InitStruct.Pin = SPI_RESET_Pin|SPI_NSS2_Pin|POWER_LATCH_Pin|USB_PWR_Pin;
16 mjames 781
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
29 mjames 782
  GPIO_InitStruct.Pull = GPIO_NOPULL;
16 mjames 783
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
29 mjames 784
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
2 mjames 785
 
44 mjames 786
  /*Configure GPIO pins : SW1_PUSH_Pin SW2_PUSH_Pin */
787
  GPIO_InitStruct.Pin = SW1_PUSH_Pin|SW2_PUSH_Pin;
16 mjames 788
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
32 mjames 789
  GPIO_InitStruct.Pull = GPIO_PULLUP;
29 mjames 790
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
5 mjames 791
 
32 mjames 792
  /*Configure GPIO pin : IGNITION_Pin */
793
  GPIO_InitStruct.Pin = IGNITION_Pin;
794
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
795
  GPIO_InitStruct.Pull = GPIO_NOPULL;
796
  HAL_GPIO_Init(IGNITION_GPIO_Port, &GPIO_InitStruct);
797
 
37 mjames 798
  /*Configure GPIO pin : BT_BUTTON_Pin */
799
  GPIO_InitStruct.Pin = BT_BUTTON_Pin;
800
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
801
  GPIO_InitStruct.Pull = GPIO_NOPULL;
802
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
803
  HAL_GPIO_Init(BT_BUTTON_GPIO_Port, &GPIO_InitStruct);
804
 
2 mjames 805
}
806
 
807
/* USER CODE BEGIN 4 */
808
 
809
/* USER CODE END 4 */
810
 
5 mjames 811
/**
29 mjames 812
  * @brief  This function is executed in case of error occurrence.
813
  * @param  None
814
  * @retval None
815
  */
816
void Error_Handler(void)
5 mjames 817
{
16 mjames 818
  /* USER CODE BEGIN Error_Handler */
30 mjames 819
/* User can add his own implementation to report the HAL error return state */
820
while (1)
821
{
822
}
29 mjames 823
  /* USER CODE END Error_Handler */
5 mjames 824
}
825
 
2 mjames 826
#ifdef USE_FULL_ASSERT
827
 
828
/**
29 mjames 829
   * @brief Reports the name of the source file and the source line number
830
   * where the assert_param error has occurred.
831
   * @param file: pointer to the source file name
832
   * @param line: assert_param error line source number
833
   * @retval None
834
   */
2 mjames 835
void assert_failed(uint8_t* file, uint32_t line)
29 mjames 836
{
837
  /* USER CODE BEGIN 6 */
30 mjames 838
/* User can add his own implementation to report the file name and line number,
839
 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
29 mjames 840
  /* USER CODE END 6 */
2 mjames 841
 
29 mjames 842
}
2 mjames 843
 
844
#endif
845
 
846
/**
29 mjames 847
  * @}
848
  */
2 mjames 849
 
850
/**
29 mjames 851
  * @}
852
*/
2 mjames 853
 
854
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/