Subversion Repositories DashDisplay

Rev

Rev 23 | 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 "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.  
  48. /* USER CODE END Includes */
  49.  
  50. /* Private variables ---------------------------------------------------------*/
  51. SPI_HandleTypeDef hspi1;
  52.  
  53. UART_HandleTypeDef huart1;
  54. UART_HandleTypeDef huart2;
  55. UART_HandleTypeDef huart3;
  56.  
  57. /* USER CODE BEGIN PV */
  58. /* Private variables ---------------------------------------------------------*/
  59. #define MAXRDG 32
  60.  
  61. /* timeout when the ignition is switched off */
  62. #define IGNITION_OFF_TIMEOUT 30000UL
  63.  
  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 };
  72.  
  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.  
  78. /* Virtual address defined by the user: 0xFFFF value is prohibited */
  79. uint16_t VirtAddVarTab[NumbOfVar] = {0x1111,0x2222 }  ;
  80.  
  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;
  89.  
  90. uint32_t  Latch_Timer = IGNITION_OFF_TIMEOUT;
  91.  
  92.  
  93. /* USER CODE END PV */
  94.  
  95. /* Private function prototypes -----------------------------------------------*/
  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);
  102. static void MX_USART3_UART_Init(void);
  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 */
  111. void _init(void)
  112. {
  113.  
  114. }
  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;
  121.         int rc;
  122.         select_display(dial); // pick the display we are using
  123.         int ItemIndex = dial_pos[dial]/4;
  124.  
  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.         {
  133.                 ItemIndex = PLXItems-1;
  134.                 dial_pos[dial] = (PLXItems-1)*4;
  135.         }
  136.  
  137.  
  138.  
  139.         // check for item suppression
  140.         if(ItemIndex == suppress)
  141.         {
  142.                 dial1[dial] = -1;
  143.                 OldObservation[dial] = -1;
  144.                 OldObservationIndex[dial] = -1;
  145.  
  146.                 clearDisplay();
  147.                 display();
  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;
  162.  
  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.  
  172.                 if (Observation < PLX_MAX_OBS)
  173.                 {
  174.                         if (Observation != OldObservation[dial]
  175.                                         || ObservationIndex != OldObservationIndex[dial])
  176.                         {
  177.  
  178.                 dial_timer[dial] = DialTimeout;
  179.  
  180.                                 dial1[dial] = -1;
  181.                                 clearDisplay();
  182.                                 dial_draw_scale(
  183.                                                 DisplayInfo[Observation].Low,
  184.                                                 DisplayInfo[Observation].High,
  185.                                             12, 1,DisplayInfo[Observation].TickScale);
  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++)
  191.                                 {
  192.                                         buff[i] = msg[i];
  193.                                 }
  194.                                 if (ObservationIndex > 0 && i<len)
  195.                                 {
  196.                                         buff[i++] = ObservationIndex + '1';
  197.                                 }
  198.  
  199.                                 print_large_string(buff, 64-i*4, 48, i); // this prints spaces for \0 at end of string
  200.  
  201.                                 // print suffix if present.
  202.                                 font_gotoxy(15,4);
  203.                                 int i=0;
  204.                                 while(DisplayInfo[Observation].suffix[i])
  205.                                 {
  206.                                         font_putchar(DisplayInfo[Observation].suffix[i++]);
  207.                                 }
  208.  
  209.                                 OldObservation[dial] = Observation;
  210.                                 OldObservationIndex[dial] = ObservationIndex;
  211.                                 //
  212.                                 display();
  213.  
  214.                         }
  215.                         else
  216.                         {
  217.                                 // check for timer timeout on consistent timer
  218.                                 if(dial_timer[dial])
  219.                                 {
  220.                                         dial_timer[dial]--;
  221.  
  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.                                                 {
  228.                                                         //__disable_irq();
  229.                                                         HAL_FLASH_Unlock();
  230.  
  231.                                                         rc = EE_WriteVariable(VirtAddVarTab[dial],dial_pos[dial]);
  232.                                                         HAL_FLASH_Lock();
  233.                                                         //__enable_irq();
  234.                                                 }
  235.                                         }
  236.                                 }
  237.  
  238.                         }
  239.  
  240.                         double max_rdg;
  241.                         double min_rdg;
  242.                         double cur_rdg;
  243.                         int int_rdg;
  244.                         int int_max;
  245.                         int int_min;
  246.  
  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);
  253.  
  254.                         int dp_pos;  // where to print the decimal place
  255.                         float scale = 1.0;
  256.                         switch (DisplayInfo[Observation].DP)
  257.                         {
  258.                         case 0:
  259.                                 scale = 1.0;
  260.                                 dp_pos = 100;
  261.                                 break;
  262.                         case 1:
  263.                                 scale = 10.0;
  264.                                 dp_pos = 1;
  265.                                 break;
  266.                         case 2:
  267.                                 scale = 100.0;
  268.                                 dp_pos = 2;
  269.                                 break;
  270.                         }
  271.                         int_rdg = (int) (cur_rdg * scale);
  272.                         int_max = (int) (max_rdg * scale);
  273.                         int_min = (int) (min_rdg * scale);
  274.  
  275.                         cur_rdg -= DisplayInfo[Observation].Low;
  276.                         cur_rdg = 100 * cur_rdg
  277.                                         / (DisplayInfo[Observation].High
  278.                                                         - DisplayInfo[Observation].Low);
  279.  
  280.                         dial0[dial] = (int) cur_rdg  ;
  281.  
  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
  290.                         print_digits(30, 30, 5, dp_pos, int_rdg);
  291.                         font_gotoxy(0,0);
  292.                         font_digits(5,dp_pos,int_min);
  293.  
  294.                         font_gotoxy(0,1);
  295.                         font_puts("Min");
  296.  
  297.                         font_gotoxy(15,0);
  298.                         font_digits(5,dp_pos,int_max);
  299.                         font_gotoxy(18,1);
  300.                         font_puts("Max");
  301.  
  302.                         dial1[dial] = dial0[dial];
  303.  
  304.                         display();
  305.  
  306.                 }
  307.         }
  308.         return ItemIndex;
  309. }
  310. /* USER CODE END 0 */
  311.  
  312. int main(void)
  313. {
  314.  
  315.   /* USER CODE BEGIN 1 */
  316.  
  317.         GPIO_InitTypeDef GPIO_InitStruct;
  318.  
  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.  
  324.   /* USER CODE END 1 */
  325.  
  326.   /* MCU Configuration----------------------------------------------------------*/
  327.  
  328.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  329.   HAL_Init();
  330.  
  331.   /* Configure the system clock */
  332.   SystemClock_Config();
  333.  
  334.   /* Initialize all configured peripherals */
  335.   MX_GPIO_Init();
  336.   MX_SPI1_Init();
  337.   MX_USART2_UART_Init();
  338.   MX_USART1_UART_Init();
  339.   MX_USART3_UART_Init();
  340.  
  341.   /* USER CODE BEGIN 2 */
  342.  
  343.  
  344.  
  345.         /* Turn on USART1 IRQ */
  346.         HAL_NVIC_SetPriority(USART1_IRQn, 2, 0);
  347.         HAL_NVIC_EnableIRQ(USART1_IRQn);
  348.  
  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.  
  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.  
  378.         InitSwitches();
  379.  
  380.         int i;
  381.         uint16_t rc;
  382.         for(i=0;i<2;i++)
  383.         {
  384.           uint16_t val;
  385.  
  386.           uint16_t rc =  EE_ReadVariable(VirtAddVarTab[i], &val);
  387.  
  388.           if (rc == 0)
  389.           {
  390.                   dial_pos[i] = val;
  391.           }
  392.           else
  393.           {
  394.                   break;
  395.           }
  396.         }
  397.  
  398.  
  399.  
  400.         ap_init(); // set up the approximate math library
  401.  
  402.         int disp;
  403.  
  404.         ssd1306_begin(1, 0);
  405.         dial_origin(64, 60);
  406.         dial_size(60);
  407.  
  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);
  411.  
  412.         for (disp = 0; disp < 2; disp++)
  413.         {
  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");
  419.  
  420.                 dial_draw_scale(0, 10, 12, 5,1);
  421.                 char  buffer[] = "Display  ";
  422.                 buffer[8] = disp+'1';
  423.                 print_large_string(buffer, 20,30, 9);
  424.  
  425.                 display();
  426.  
  427.         }
  428.  
  429.  
  430.  
  431.   /* USER CODE END 2 */
  432.  
  433.   /* Infinite loop */
  434.   /* USER CODE BEGIN WHILE */
  435.         uint32_t Ticks = HAL_GetTick() + 100;
  436.  
  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.  
  451.     uint32_t timeout = 0;  //
  452.         // PLX decoder protocols
  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.         {
  464. // poll switche
  465.                 HandleSwitches();
  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);
  474.  
  475.                 }
  476.  
  477.  
  478.                 uint16_t cc = SerialCharsReceived(&uc1);
  479.                 int chr;
  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.                 }
  493.                 for (chr = 0; chr < cc; chr++)
  494.                 {
  495.                         char c = GetCharSerial(&uc1);
  496.                         timeout = 0;
  497.                         PutCharSerial(&uc3,c);
  498.  
  499.                         if (c == PLX_Start) // at any time if the start byte appears, reset the pointers
  500.                         {
  501.                                 PLXPtr = 0;    // reset the pointer
  502.                                 PLXPacket = 1;
  503.                         }
  504.                         else if (c == PLX_Stop)
  505.                         {
  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
  529.                                     int suppress = DisplayCurrent(0,-1);
  530.                                         DisplayCurrent(1, suppress);
  531.                                 }
  532.                                 PLXPtr = 0;
  533.                                 PLXPacket = 0;
  534.                         }
  535.                         else if (c > PLX_Stop) // illegal char, restart reading
  536.                         {
  537.                                 PLXPacket = 0;
  538.                                 PLXPtr = 0;
  539.                         }
  540.                         else if (PLXPtr < sizeof(Data.Bytes))
  541.                         {
  542.                                 Data.Bytes[PLXPtr++] = c;
  543.                         }
  544.                 }
  545.  
  546.                 HAL_Delay(1);
  547.         }
  548.   /* USER CODE END WHILE */
  549.  
  550.   /* USER CODE BEGIN 3 */
  551.  
  552.  
  553.   /* USER CODE END 3 */
  554.  
  555. }
  556.  
  557. /** System Clock Configuration
  558. */
  559. void SystemClock_Config(void)
  560. {
  561.  
  562.   RCC_OscInitTypeDef RCC_OscInitStruct;
  563.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  564.  
  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.   }
  575.  
  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.   }
  586.  
  587.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  588.  
  589.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  590.  
  591.   /* SysTick_IRQn interrupt configuration */
  592.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  593. }
  594.  
  595. /* SPI1 init function */
  596. static void MX_SPI1_Init(void)
  597. {
  598.  
  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.   }
  615.  
  616. }
  617.  
  618. /* USART1 init function */
  619. static void MX_USART1_UART_Init(void)
  620. {
  621.  
  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.   }
  634.  
  635. }
  636.  
  637. /* USART2 init function */
  638. static void MX_USART2_UART_Init(void)
  639. {
  640.  
  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.   }
  653.  
  654. }
  655.  
  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.  
  675. /** Configure pins as
  676.         * Analog
  677.         * Input
  678.         * Output
  679.         * EVENT_OUT
  680.         * EXTI
  681. */
  682. static void MX_GPIO_Init(void)
  683. {
  684.  
  685.   GPIO_InitTypeDef GPIO_InitStruct;
  686.  
  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();
  692.  
  693.   /*Configure GPIO pin Output Level */
  694.   HAL_GPIO_WritePin(SPI_NSS1_GPIO_Port, SPI_NSS1_Pin, GPIO_PIN_SET);
  695.  
  696.   /*Configure GPIO pin Output Level */
  697.   HAL_GPIO_WritePin(GPIOA, SPI1CD_Pin|BT_BUTTON_Pin, GPIO_PIN_RESET);
  698.  
  699.   /*Configure GPIO pin Output Level */
  700.   HAL_GPIO_WritePin(GPIOC, SPI_RESET_Pin|POWER_LATCH_Pin|USB_PWR_Pin, GPIO_PIN_RESET);
  701.  
  702.   /*Configure GPIO pin Output Level */
  703.   HAL_GPIO_WritePin(SPI_NSS2_GPIO_Port, SPI_NSS2_Pin, GPIO_PIN_SET);
  704.  
  705.   /*Configure GPIO pins : SPI_NSS1_Pin SPI1CD_Pin BT_BUTTON_Pin */
  706.   GPIO_InitStruct.Pin = SPI_NSS1_Pin|SPI1CD_Pin|BT_BUTTON_Pin;
  707.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  708.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  709.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  710.  
  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;
  713.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  714.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  715.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  716.  
  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);
  722.  
  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);
  728.  
  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.  
  735. }
  736.  
  737. /* USER CODE BEGIN 4 */
  738.  
  739. /* USER CODE END 4 */
  740.  
  741. /**
  742.   * @brief  This function is executed in case of error occurrence.
  743.   * @param  None
  744.   * @retval None
  745.   */
  746. void Error_Handler(void)
  747. {
  748.   /* USER CODE BEGIN Error_Handler */
  749. /* User can add his own implementation to report the HAL error return state */
  750. while (1)
  751. {
  752. }
  753.   /* USER CODE END Error_Handler */
  754. }
  755.  
  756. #ifdef USE_FULL_ASSERT
  757.  
  758. /**
  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.    */
  765. void assert_failed(uint8_t* file, uint32_t line)
  766. {
  767.   /* USER CODE BEGIN 6 */
  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) */
  770.   /* USER CODE END 6 */
  771.  
  772. }
  773.  
  774. #endif
  775.  
  776. /**
  777.   * @}
  778.   */
  779.  
  780. /**
  781.   * @}
  782. */
  783.  
  784. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  785.