Subversion Repositories DashDisplay

Rev

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

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