Subversion Repositories DashDisplay

Rev

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