Subversion Repositories DashDisplay

Rev

Rev 24 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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