Subversion Repositories DashDisplay

Rev

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