Subversion Repositories DashDisplay

Rev

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