Subversion Repositories DashDisplay

Rev

Rev 68 | Rev 71 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /* USER CODE BEGIN Header */
  2. /**
  3.  ******************************************************************************
  4.  * @file           : main.c
  5.  * @brief          : Main program body
  6.  ******************************************************************************
  7.  * @attention
  8.  *
  9.  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10.  * All rights reserved.</center></h2>
  11.  *
  12.  * This software component is licensed by ST under BSD 3-Clause license,
  13.  * the "License"; You may not use this file except in compliance with the
  14.  * License. You may obtain a copy of the License at:
  15.  *                        opensource.org/licenses/BSD-3-Clause
  16.  *
  17.  ******************************************************************************
  18.  */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22.  
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25.  
  26. #include "libPLX/plx.h"
  27. #include "libSerial/serial.H"
  28. #include "libSmallPrintf/small_printf.h"
  29. #include "libNMEA/nmea.h"
  30. #include "switches.h"
  31. #include <string.h>
  32.  
  33. /* USER CODE END Includes */
  34.  
  35. /* Private typedef -----------------------------------------------------------*/
  36. /* USER CODE BEGIN PTD */
  37.  
  38. /* USER CODE END PTD */
  39.  
  40. /* Private define ------------------------------------------------------------*/
  41. /* USER CODE BEGIN PD */
  42. /* USER CODE END PD */
  43.  
  44. /* Private macro -------------------------------------------------------------*/
  45. /* USER CODE BEGIN PM */
  46.  
  47. /* USER CODE END PM */
  48.  
  49. /* Private variables ---------------------------------------------------------*/
  50. I2C_HandleTypeDef hi2c1;
  51.  
  52. SPI_HandleTypeDef hspi1;
  53.  
  54. TIM_HandleTypeDef htim2;
  55. TIM_HandleTypeDef htim3;
  56. TIM_HandleTypeDef htim9;
  57.  
  58. UART_HandleTypeDef huart4;
  59. UART_HandleTypeDef huart1;
  60. UART_HandleTypeDef huart2;
  61. UART_HandleTypeDef huart3;
  62.  
  63. /* USER CODE BEGIN PV */
  64. /* Private variables ---------------------------------------------------------*/
  65.  
  66. context_t contexts[MAX_DISPLAYS];
  67.  
  68. ///@brief  timeout when the ignition is switched off
  69. #define IGNITION_OFF_TIMEOUT 30000UL
  70.  
  71. /// @brief 1000mS per logger period, print average per period
  72. #define LOGGER_INTERVAL 1000UL
  73.  
  74. /// @brief  about 10 seconds after twiddle, save the dial position.
  75. const int DialTimeout = 100;
  76.  
  77. /// @brief Data storage for readings
  78. info_t Info[MAXRDG];
  79.  
  80. /// @brief Define a null item
  81. const info_t nullInfo = {.Max = 0,
  82.                          .Min = 0xFFF,
  83.                          .sum = 0,
  84.                          .count = 0,
  85.                          .updated = 0,
  86.                          .lastUpdated = 0,
  87.                          .observation = PLX_MAX_OBS,
  88.                          .instance = PLX_MAX_INST};
  89.  
  90. /// \brief storage for incoming data
  91. data_t Data;
  92.  
  93. uint32_t Latch_Timer = IGNITION_OFF_TIMEOUT;
  94.  
  95. // location for GPS data
  96. Location loc;
  97.  
  98. /* USER CODE END PV */
  99.  
  100. /* Private function prototypes -----------------------------------------------*/
  101. void SystemClock_Config(void);
  102. static void MX_GPIO_Init(void);
  103. static void MX_SPI1_Init(void);
  104. static void MX_USART1_UART_Init(void);
  105. static void MX_USART2_UART_Init(void);
  106. static void MX_USART3_UART_Init(void);
  107. static void MX_TIM3_Init(void);
  108. static void MX_TIM9_Init(void);
  109. static void MX_TIM2_Init(void);
  110. static void MX_UART4_Init(void);
  111. static void MX_I2C1_Init(void);
  112. /* USER CODE BEGIN PFP */
  113.  
  114. // the dial is the switch number we are using.
  115. // suppress is the ItemIndex we wish to suppress on this display
  116. int DisplayCurrent(int dial, int suppress)
  117. {
  118.   if (contexts[dial].knobPos < 0)
  119.     return -1;
  120.   return cc_display(dial, suppress);
  121. }
  122.  
  123. /// \note  HC-05 only accepts : 9600,19200,38400,57600,115200,230400,460800 baud
  124. /// \brief Setup Bluetooth module
  125. void initModule(usart_ctl *ctl, uint32_t baudRate)
  126. {
  127.   char initBuf[30];
  128.   // switch to command mode
  129.   HAL_GPIO_WritePin(BT_RESET_GPIO_Port, BT_RESET_Pin, GPIO_PIN_RESET);
  130.   HAL_Delay(500);
  131.   setBaud(ctl, 38400);
  132.   int initLen = small_sprintf(initBuf, "AT+UART=%lu,0,0\n", baudRate);
  133.   const char buf[] = "AT+RESET\n";
  134.   sendString(ctl, initBuf, initLen);
  135.   HAL_Delay(500);
  136.   initLen = small_sprintf(initBuf, buf);
  137.   sendString(ctl, initBuf, initLen);
  138.  
  139.   TxWaitEmpty(ctl);
  140.  
  141.   // clear the button press
  142.   HAL_GPIO_WritePin(BT_RESET_GPIO_Port, BT_RESET_Pin, GPIO_PIN_SET);
  143.  
  144.   // switch back to normal comms at new baud rate
  145.   setBaud(ctl, baudRate);
  146.   HAL_Delay(100);
  147. }
  148.  
  149. // workspace for RMC data read from GPS module.
  150. uint8_t rmc_buff[80];
  151. volatile uint16_t rmc_length;
  152.  
  153. uint8_t rmc_callback(uint8_t *data, uint16_t length)
  154. {
  155.   rmc_length = length < sizeof(rmc_buff) ? length : sizeof(rmc_buff);
  156.   memcpy(rmc_buff, data, length);
  157.   return 0;
  158. }
  159.  
  160. // check if bluetooth connected
  161. uint8_t btConnected()
  162. {
  163.   return HAL_GPIO_ReadPin(BT_STATE_GPIO_Port, BT_STATE_Pin) == GPIO_PIN_SET;
  164. }
  165.  
  166. /// @brief return true if this slot is unused
  167. /// @param ptr pointer to the slot to
  168. uint8_t isUnused(int index)
  169. {
  170.   if (index < 0 || index > MAXRDG)
  171.     return false;
  172.  
  173.   return Info[index].instance == PLX_MAX_INST && Info[index].observation == PLX_MAX_OBS;
  174. }
  175.  
  176. /// @brief Determine if an entry is currently valid
  177. /// @param index the number of the array entry to display
  178. /// @return true if the entry contains data which is fresh
  179. uint8_t isValid(int index)
  180. {
  181.   if (index < 0 || index > MAXRDG)
  182.     return false;
  183.   if (isUnused(index))
  184.     return false;
  185.  
  186.   uint32_t age = HAL_GetTick() - Info[index].lastUpdated;
  187.  
  188.   if (age > 300)
  189.     return false;
  190.  
  191.   return true;
  192. }
  193.  
  194. /* USER CODE END PFP */
  195.  
  196. /* Private user code ---------------------------------------------------------*/
  197. /* USER CODE BEGIN 0 */
  198.  
  199. /* USER CODE END 0 */
  200.  
  201. /**
  202.  * @brief  The application entry point.
  203.  * @retval int
  204.  */
  205. int main(void)
  206. {
  207.   /* USER CODE BEGIN 1 */
  208.   __HAL_RCC_SPI1_CLK_ENABLE();
  209.   __HAL_RCC_USART1_CLK_ENABLE(); // PLX main port
  210.   __HAL_RCC_USART2_CLK_ENABLE(); // debug port
  211.   __HAL_RCC_USART3_CLK_ENABLE(); // Bluetooth port
  212.   __HAL_RCC_UART4_CLK_ENABLE();  // NMEA0183 port
  213.  
  214.   __HAL_RCC_TIM3_CLK_ENABLE();
  215.  
  216.   __HAL_RCC_TIM9_CLK_ENABLE();
  217.  
  218.   /* USER CODE END 1 */
  219.  
  220.   /* MCU Configuration--------------------------------------------------------*/
  221.  
  222.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  223.   HAL_Init();
  224.  
  225.   /* USER CODE BEGIN Init */
  226.  
  227.   /* USER CODE END Init */
  228.  
  229.   /* Configure the system clock */
  230.   SystemClock_Config();
  231.  
  232.   /* USER CODE BEGIN SysInit */
  233.   // Switch handler called on sysTick interrupt.
  234.   InitSwitches();
  235.  
  236.   /* USER CODE END SysInit */
  237.  
  238.   /* Initialize all configured peripherals */
  239.   MX_GPIO_Init();
  240.   MX_SPI1_Init();
  241.   MX_USART1_UART_Init();
  242.   MX_USART2_UART_Init();
  243.   MX_USART3_UART_Init();
  244.   MX_TIM3_Init();
  245.   MX_TIM9_Init();
  246.   MX_TIM2_Init();
  247.   MX_UART4_Init();
  248.   MX_I2C1_Init();
  249.   /* USER CODE BEGIN 2 */
  250.  
  251.   /* Turn on USART1 IRQ */
  252.   HAL_NVIC_SetPriority(USART1_IRQn, 2, 0);
  253.   HAL_NVIC_EnableIRQ(USART1_IRQn);
  254.  
  255.   /* Turn on USART2 IRQ  */
  256.   HAL_NVIC_SetPriority(USART2_IRQn, 4, 0);
  257.   HAL_NVIC_EnableIRQ(USART2_IRQn);
  258.  
  259.   /* turn on USART3 IRQ */
  260.   HAL_NVIC_SetPriority(USART3_IRQn, 4, 0);
  261.   HAL_NVIC_EnableIRQ(USART3_IRQn);
  262.  
  263.   /* turn on UART4 IRQ */
  264.   HAL_NVIC_SetPriority(UART4_IRQn, 4, 0);
  265.   HAL_NVIC_EnableIRQ(UART4_IRQn);
  266.  
  267.   /* setup the USART control blocks */
  268.   init_usart_ctl(&uc1, &huart1);
  269.   init_usart_ctl(&uc2, &huart2);
  270.   init_usart_ctl(&uc3, &huart3);
  271.   init_usart_ctl(&uc4, &huart4);
  272.  
  273.   EnableSerialRxInterrupt(&uc1);
  274.   EnableSerialRxInterrupt(&uc2);
  275.   EnableSerialRxInterrupt(&uc3);
  276.   EnableSerialRxInterrupt(&uc4);
  277.  
  278.   HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);
  279.  
  280.   HAL_TIM_Encoder_Start(&htim9, TIM_CHANNEL_ALL);
  281.  
  282.   initModule(&uc3, 38400);
  283.  
  284.   // Initialise UART for 4800 baud NMEA
  285.   setBaud(&uc2, 4800);
  286.  
  287.   // Initialuse UART4 for 4800 baud NMEA.
  288.   setBaud(&uc4, 4800);
  289.  
  290.   cc_init();
  291.  
  292.   int i;
  293.   for (i = 0; i < 2; i++)
  294.   {
  295.     dial_pos[i] = 0;            // default to items 0 and 1
  296.     contexts[i].knobPos = -1;   // set the knob position
  297.     contexts[i].dial_timer = 1; // timeout immediately when decremented
  298.  
  299.     cc_check_nvram(i);
  300.   }
  301.  
  302.   /* reset the display timeout, latch on power from accessories */
  303.   Latch_Timer = IGNITION_OFF_TIMEOUT;
  304.   HAL_GPIO_WritePin(POWER_LATCH_GPIO_Port, POWER_LATCH_Pin, GPIO_PIN_RESET);
  305.  
  306.   setRmcCallback(&rmc_callback);
  307.  
  308.   // data timeout
  309.   uint32_t timeout = 0; //
  310.  
  311.   uint32_t nextTick = 0;
  312.   uint8_t log = 0;
  313.   // PLX decoder protocols
  314.   char PLXPacket = 0;
  315.  
  316.  
  317.   for (i = 0; i < MAXRDG; i++)
  318.   {
  319.     Info[i] = nullInfo;
  320.   }
  321.  
  322.   int PLXPtr = 0;
  323.   int logCount = 0;
  324.  
  325.   uint32_t resetCounter; // record time at which both reset buttons were first pressed.
  326.  
  327.   /* USER CODE END 2 */
  328.  
  329.   /* Infinite loop */
  330.   /* USER CODE BEGIN WHILE */
  331.   while (1)
  332.   {
  333.  
  334.     /* while ignition is on, keep resetting power latch timer */
  335.     if (HAL_GPIO_ReadPin(IGNITION_GPIO_Port, IGNITION_Pin) == GPIO_PIN_RESET)
  336.     {
  337.       Latch_Timer = HAL_GetTick() + IGNITION_OFF_TIMEOUT;
  338.     }
  339.     else
  340.     {
  341.       /* if the ignition has been off for a while, then turn off power */
  342.       if (HAL_GetTick() > Latch_Timer)
  343.       {
  344.         HAL_GPIO_WritePin(POWER_LATCH_GPIO_Port, POWER_LATCH_Pin,
  345.                           GPIO_PIN_RESET);
  346.       }
  347.     }
  348.  
  349.     // Handle the bluetooth pairing / reset function by pressing both buttons.
  350.     if ((push_pos[0] == 1) && (push_pos[1] == 1))
  351.     {
  352.       HAL_GPIO_WritePin(BT_BUTTON_GPIO_Port, BT_BUTTON_Pin,
  353.                         GPIO_PIN_RESET);
  354.       if (resetCounter == 0)
  355.         resetCounter = HAL_GetTick();
  356.     }
  357.     else
  358.     {
  359.       HAL_GPIO_WritePin(BT_BUTTON_GPIO_Port, BT_BUTTON_Pin,
  360.                         GPIO_PIN_SET);
  361.  
  362.       if (resetCounter != 0)
  363.       {
  364.         // Held down reset button for 10 seconds, clear NVRAM.
  365.         if ((HAL_GetTick() - resetCounter) > 10000)
  366.         {
  367.           for (i = 0; i < 2; i++)
  368.           {
  369.             dial_pos[i] = 0;            // default to items 0 and 1
  370.             contexts[i].knobPos = -1;   // set the knob position
  371.             contexts[i].dial_timer = 1; // timeout immediately when decremented
  372.           }
  373.           erase_nvram();
  374.         }
  375.         resetCounter = 0;
  376.       }
  377.     }
  378.  
  379.     // poll GPS Position/time on UART4
  380.     (void)updateLocation(&loc, &uc4);
  381.     if (loc.valid == 'V')
  382.       memset(loc.time, '-', 6);
  383.  
  384.     // if permitted, log data from RMC packet
  385.     if (btConnected())
  386.     {
  387.       // Any RMC data, send it, reset the logger timeout
  388.       if (rmc_length)
  389.       {
  390.         sendString(&uc3, (const char *)rmc_buff, rmc_length);
  391.         rmc_length = 0;
  392.         nextTick = HAL_GetTick() + LOGGER_INTERVAL;
  393.         log = 1;      // send out associated data over Bluetooth because triggered by recieving RMC
  394.         logCount = 0; // first sample set this second numbered 0
  395.       }
  396.  
  397.       // Timeout for data logging regularly
  398.       if (HAL_GetTick() > nextTick)
  399.       {
  400.         nextTick = HAL_GetTick() + LOGGER_INTERVAL;
  401.         logCount++;
  402.         if (logCount > (1000 / LOGGER_INTERVAL))
  403.           logCount = 0;
  404.         log = 1;
  405.       }
  406.  
  407.       if (log)
  408.       {
  409.         log = 0;
  410.         // Send items  to BT if it is in connected state
  411.         for (int i = 0; i <  MAXRDG; ++i)
  412.         {
  413.           if(!isValid(i))
  414.             continue;
  415.           char outbuff[100];
  416.  
  417.           int cnt = small_sprintf(outbuff,
  418.                                   "$PLLOG,%d,%d,%d,%ld",
  419.                                   logCount,
  420.                                   Info[i].observation,
  421.                                   Info[i].instance,
  422.                                   Info[i].count == 0 ? 0 : Info[i].sum / Info[i].count);
  423.  
  424.           // NMEA style checksum
  425.           int ck;
  426.           int sum = 0;
  427.           for (ck = 1; ck < cnt; ck++)
  428.             sum += outbuff[ck];
  429.           cnt += small_sprintf(outbuff + cnt, "*%02X\n",
  430.                                sum & 0xFF);
  431.           sendString(&uc3, outbuff, cnt);
  432.         }
  433.       }
  434.     }
  435.  
  436.     // determine if we are getting any data from the interface
  437.     uint16_t cc = SerialCharsReceived(&uc1);
  438.     int chr;
  439.     if (cc == 0)
  440.     {
  441.       timeout++;
  442.       if (btConnected() && (timeout % 1000 == 0))
  443.       {
  444.         const char msg[] = "Timeout\r\n";
  445.         sendString(&uc3, msg, sizeof(msg));
  446.       }
  447.  
  448.       if (timeout > 60000)
  449.       {
  450.  
  451.         // do turn off screen
  452.       }
  453.       // wait for a bit if nothing came in.
  454.       HAL_Delay(10);
  455.     }
  456.  
  457.     /// process the observation list
  458.     for (chr = 0; chr < cc; chr++)
  459.     {
  460.       char c = GetCharSerial(&uc1);
  461.  
  462.       if (c == PLX_Start) // at any time if the start byte appears, reset the pointers
  463.       {
  464.         PLXPtr = 0; // reset the pointer
  465.         PLXPacket = 1;
  466.         timeout = 0; // Reset the timer
  467.         continue;
  468.       }
  469.       if (c == PLX_Stop)
  470.       {
  471.         if (PLXPacket)
  472.         {
  473.           // we can now decode the selected parameter
  474.           int PLXNewItems = PLXPtr / sizeof(PLX_SensorInfo); // total items in last reading batch
  475.  
  476.           // process items
  477.           for (i = 0; i < PLXNewItems; i++)
  478.           {
  479.             // search to see if the item already has a slot in the Info[] array
  480.             // match the observation and instance: if found, update entry
  481.             enum PLX_Observations observation = ConvPLX(Data.Sensor[i].AddrH,
  482.                                                         Data.Sensor[i].AddrL);
  483.  
  484.             char instance = Data.Sensor[i].Instance;
  485.  
  486.             // validate the current item, discard out of range
  487.  
  488.             if ((instance > PLX_MAX_INST) || (observation > PLX_MAX_OBS))
  489.               continue;
  490.  
  491.             // search for the item in the list
  492.             int j;
  493.             for (j = 0; j < MAXRDG; ++j)
  494.             {
  495.               if ((Info[j].observation == observation) && (Info[j].instance == instance))
  496.                 break;
  497.             }
  498.             // fallen off the end of the list of existing items without a match, so j points at next new item
  499.             //
  500.             // Find an unused slot
  501.  
  502.             if (j == MAXRDG)
  503.             {
  504.               int k;
  505.               {
  506.                 for (k = 0; k < MAXRDG; ++k)
  507.                   if (!isValid(k))
  508.                   {
  509.                     j = k; // found a spare slot
  510.                     break;
  511.                   }
  512.               }
  513.               if (k == MAXRDG)
  514.                 continue; // abandon this iteration
  515.             }
  516.  
  517.             // give up if we are going to fall off the end of the array
  518.             if (j > MAXRDG)
  519.               break;
  520.  
  521.             Info[j].observation = observation;
  522.  
  523.             Info[j].instance = instance;
  524.             Info[j].data = ConvPLX(Data.Sensor[j].ReadingH,
  525.                                    Data.Sensor[j].ReadingL);
  526.             if (Info[j].data > Info[j].Max)
  527.             {
  528.               Info[j].Max = Info[j].data;
  529.             }
  530.             if (Info[j].data < Info[j].Min)
  531.             {
  532.               Info[j].Min = Info[j].data;
  533.             }
  534.             // take an average
  535.             Info[j].sum += Info[j].data;
  536.             Info[j].count++;
  537.             // note the last update time
  538.             Info[j].lastUpdated = HAL_GetTick();
  539.             Info[j].updated = 1; // it has been updated
  540.           }
  541.           PLXPtr = 0;
  542.           PLXPacket = 0;
  543.  
  544.           // scan through and invalidate all old items
  545.           for (int i = 0; i < MAXRDG; ++i)
  546.           {
  547.             if (!isValid(i))
  548.               Info[i] = nullInfo;
  549.           }
  550.  
  551.           break; // something to process
  552.         }
  553.       }
  554.       if (c > PLX_Stop) // illegal char, restart reading
  555.       {
  556.         PLXPacket = 0;
  557.         PLXPtr = 0;
  558.         continue;
  559.       }
  560.       if (PLXPacket && PLXPtr < sizeof(Data.Bytes))
  561.       {
  562.         Data.Bytes[PLXPtr++] = c;
  563.       }
  564.     }
  565.     int suppress = -1;
  566.     for (i = 0; i < MAX_DISPLAYS; i++)
  567.     { // now to display the information
  568.       suppress = DisplayCurrent(i, suppress);
  569.  
  570.       if (dial_pos[i] < 0)
  571.         dial_pos[i] = MAXRDG - 1;
  572.       if (dial_pos[i] >= MAXRDG)
  573.         dial_pos[i] = 0;
  574.  
  575.       int prevPos = contexts[i].knobPos;
  576.       if (contexts[i].knobPos >= 0)
  577.         contexts[i].knobPos = dial_pos[i];
  578.       // if the dial position was changed then reset timer
  579.       if (prevPos != contexts[i].knobPos)
  580.         contexts[i].dial_timer = DialTimeout;
  581.  
  582.       cc_check_nvram(i);
  583.       if (contexts[i].knobPos >= 0)
  584.         dial_pos[i] = contexts[i].knobPos;
  585.     }
  586.   }
  587.   /* USER CODE END WHILE */
  588.  
  589.   /* USER CODE BEGIN 3 */
  590.  
  591.   /* USER CODE END 3 */
  592. }
  593.  
  594. /**
  595.  * @brief System Clock Configuration
  596.  * @retval None
  597.  */
  598. void SystemClock_Config(void)
  599. {
  600.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  601.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  602.  
  603.   /** Configure the main internal regulator output voltage
  604.    */
  605.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  606.  
  607.   /** Initializes the RCC Oscillators according to the specified parameters
  608.    * in the RCC_OscInitTypeDef structure.
  609.    */
  610.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  611.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  612.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  613.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  614.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  615.   RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
  616.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  617.   {
  618.     Error_Handler();
  619.   }
  620.  
  621.   /** Initializes the CPU, AHB and APB buses clocks
  622.    */
  623.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  624.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  625.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  626.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  627.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  628.  
  629.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  630.   {
  631.     Error_Handler();
  632.   }
  633. }
  634.  
  635. /**
  636.  * @brief I2C1 Initialization Function
  637.  * @param None
  638.  * @retval None
  639.  */
  640. static void MX_I2C1_Init(void)
  641. {
  642.  
  643.   /* USER CODE BEGIN I2C1_Init 0 */
  644.  
  645.   /* USER CODE END I2C1_Init 0 */
  646.  
  647.   /* USER CODE BEGIN I2C1_Init 1 */
  648.  
  649.   /* USER CODE END I2C1_Init 1 */
  650.   hi2c1.Instance = I2C1;
  651.   hi2c1.Init.ClockSpeed = 100000;
  652.   hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  653.   hi2c1.Init.OwnAddress1 = 0;
  654.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  655.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  656.   hi2c1.Init.OwnAddress2 = 0;
  657.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  658.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  659.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  660.   {
  661.     Error_Handler();
  662.   }
  663.   /* USER CODE BEGIN I2C1_Init 2 */
  664.  
  665.   /* USER CODE END I2C1_Init 2 */
  666. }
  667.  
  668. /**
  669.  * @brief SPI1 Initialization Function
  670.  * @param None
  671.  * @retval None
  672.  */
  673. static void MX_SPI1_Init(void)
  674. {
  675.  
  676.   /* USER CODE BEGIN SPI1_Init 0 */
  677.  
  678.   /* USER CODE END SPI1_Init 0 */
  679.  
  680.   /* USER CODE BEGIN SPI1_Init 1 */
  681.  
  682.   /* USER CODE END SPI1_Init 1 */
  683.   /* SPI1 parameter configuration*/
  684.   hspi1.Instance = SPI1;
  685.   hspi1.Init.Mode = SPI_MODE_MASTER;
  686.   hspi1.Init.Direction = SPI_DIRECTION_1LINE;
  687.   hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  688.   hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  689.   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  690.   hspi1.Init.NSS = SPI_NSS_SOFT;
  691.   hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  692.   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  693.   hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  694.   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  695.   hspi1.Init.CRCPolynomial = 10;
  696.   if (HAL_SPI_Init(&hspi1) != HAL_OK)
  697.   {
  698.     Error_Handler();
  699.   }
  700.   /* USER CODE BEGIN SPI1_Init 2 */
  701.  
  702.   /* USER CODE END SPI1_Init 2 */
  703. }
  704.  
  705. /**
  706.  * @brief TIM2 Initialization Function
  707.  * @param None
  708.  * @retval None
  709.  */
  710. static void MX_TIM2_Init(void)
  711. {
  712.  
  713.   /* USER CODE BEGIN TIM2_Init 0 */
  714.  
  715.   /* USER CODE END TIM2_Init 0 */
  716.  
  717.   TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  718.   TIM_MasterConfigTypeDef sMasterConfig = {0};
  719.  
  720.   /* USER CODE BEGIN TIM2_Init 1 */
  721.  
  722.   /* USER CODE END TIM2_Init 1 */
  723.   htim2.Instance = TIM2;
  724.   htim2.Init.Prescaler = 0;
  725.   htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  726.   htim2.Init.Period = 65535;
  727.   htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  728.   htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  729.   if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  730.   {
  731.     Error_Handler();
  732.   }
  733.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  734.   if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  735.   {
  736.     Error_Handler();
  737.   }
  738.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  739.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  740.   if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  741.   {
  742.     Error_Handler();
  743.   }
  744.   /* USER CODE BEGIN TIM2_Init 2 */
  745.  
  746.   /* USER CODE END TIM2_Init 2 */
  747. }
  748.  
  749. /**
  750.  * @brief TIM3 Initialization Function
  751.  * @param None
  752.  * @retval None
  753.  */
  754. static void MX_TIM3_Init(void)
  755. {
  756.  
  757.   /* USER CODE BEGIN TIM3_Init 0 */
  758.  
  759.   /* USER CODE END TIM3_Init 0 */
  760.  
  761.   TIM_Encoder_InitTypeDef sConfig = {0};
  762.   TIM_MasterConfigTypeDef sMasterConfig = {0};
  763.  
  764.   /* USER CODE BEGIN TIM3_Init 1 */
  765.  
  766.   /* USER CODE END TIM3_Init 1 */
  767.   htim3.Instance = TIM3;
  768.   htim3.Init.Prescaler = 0;
  769.   htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  770.   htim3.Init.Period = 65535;
  771.   htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  772.   htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  773.   sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
  774.   sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
  775.   sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
  776.   sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
  777.   sConfig.IC1Filter = 15;
  778.   sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
  779.   sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
  780.   sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
  781.   sConfig.IC2Filter = 15;
  782.   if (HAL_TIM_Encoder_Init(&htim3, &sConfig) != HAL_OK)
  783.   {
  784.     Error_Handler();
  785.   }
  786.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  787.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  788.   if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  789.   {
  790.     Error_Handler();
  791.   }
  792.   /* USER CODE BEGIN TIM3_Init 2 */
  793.  
  794.   /* USER CODE END TIM3_Init 2 */
  795. }
  796.  
  797. /**
  798.  * @brief TIM9 Initialization Function
  799.  * @param None
  800.  * @retval None
  801.  */
  802. static void MX_TIM9_Init(void)
  803. {
  804.  
  805.   /* USER CODE BEGIN TIM9_Init 0 */
  806.  
  807.   /* USER CODE END TIM9_Init 0 */
  808.  
  809.   TIM_Encoder_InitTypeDef sConfig = {0};
  810.   TIM_MasterConfigTypeDef sMasterConfig = {0};
  811.  
  812.   /* USER CODE BEGIN TIM9_Init 1 */
  813.  
  814.   /* USER CODE END TIM9_Init 1 */
  815.   htim9.Instance = TIM9;
  816.   htim9.Init.Prescaler = 0;
  817.   htim9.Init.CounterMode = TIM_COUNTERMODE_UP;
  818.   htim9.Init.Period = 65535;
  819.   htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  820.   htim9.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  821.   sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
  822.   sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
  823.   sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
  824.   sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
  825.   sConfig.IC1Filter = 15;
  826.   sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
  827.   sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
  828.   sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
  829.   sConfig.IC2Filter = 0;
  830.   if (HAL_TIM_Encoder_Init(&htim9, &sConfig) != HAL_OK)
  831.   {
  832.     Error_Handler();
  833.   }
  834.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  835.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  836.   if (HAL_TIMEx_MasterConfigSynchronization(&htim9, &sMasterConfig) != HAL_OK)
  837.   {
  838.     Error_Handler();
  839.   }
  840.   /* USER CODE BEGIN TIM9_Init 2 */
  841.  
  842.   /* USER CODE END TIM9_Init 2 */
  843. }
  844.  
  845. /**
  846.  * @brief UART4 Initialization Function
  847.  * @param None
  848.  * @retval None
  849.  */
  850. static void MX_UART4_Init(void)
  851. {
  852.  
  853.   /* USER CODE BEGIN UART4_Init 0 */
  854.  
  855.   /* USER CODE END UART4_Init 0 */
  856.  
  857.   /* USER CODE BEGIN UART4_Init 1 */
  858.  
  859.   /* USER CODE END UART4_Init 1 */
  860.   huart4.Instance = UART4;
  861.   huart4.Init.BaudRate = 4800;
  862.   huart4.Init.WordLength = UART_WORDLENGTH_8B;
  863.   huart4.Init.StopBits = UART_STOPBITS_1;
  864.   huart4.Init.Parity = UART_PARITY_NONE;
  865.   huart4.Init.Mode = UART_MODE_TX_RX;
  866.   huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  867.   huart4.Init.OverSampling = UART_OVERSAMPLING_16;
  868.   if (HAL_UART_Init(&huart4) != HAL_OK)
  869.   {
  870.     Error_Handler();
  871.   }
  872.   /* USER CODE BEGIN UART4_Init 2 */
  873.  
  874.   /* USER CODE END UART4_Init 2 */
  875. }
  876.  
  877. /**
  878.  * @brief USART1 Initialization Function
  879.  * @param None
  880.  * @retval None
  881.  */
  882. static void MX_USART1_UART_Init(void)
  883. {
  884.  
  885.   /* USER CODE BEGIN USART1_Init 0 */
  886.  
  887.   /* USER CODE END USART1_Init 0 */
  888.  
  889.   /* USER CODE BEGIN USART1_Init 1 */
  890.  
  891.   /* USER CODE END USART1_Init 1 */
  892.   huart1.Instance = USART1;
  893.   huart1.Init.BaudRate = 19200;
  894.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  895.   huart1.Init.StopBits = UART_STOPBITS_1;
  896.   huart1.Init.Parity = UART_PARITY_NONE;
  897.   huart1.Init.Mode = UART_MODE_TX_RX;
  898.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  899.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  900.   if (HAL_UART_Init(&huart1) != HAL_OK)
  901.   {
  902.     Error_Handler();
  903.   }
  904.   /* USER CODE BEGIN USART1_Init 2 */
  905.  
  906.   /* USER CODE END USART1_Init 2 */
  907. }
  908.  
  909. /**
  910.  * @brief USART2 Initialization Function
  911.  * @param None
  912.  * @retval None
  913.  */
  914. static void MX_USART2_UART_Init(void)
  915. {
  916.  
  917.   /* USER CODE BEGIN USART2_Init 0 */
  918.  
  919.   /* USER CODE END USART2_Init 0 */
  920.  
  921.   /* USER CODE BEGIN USART2_Init 1 */
  922.  
  923.   /* USER CODE END USART2_Init 1 */
  924.   huart2.Instance = USART2;
  925.   huart2.Init.BaudRate = 115200;
  926.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  927.   huart2.Init.StopBits = UART_STOPBITS_1;
  928.   huart2.Init.Parity = UART_PARITY_NONE;
  929.   huart2.Init.Mode = UART_MODE_TX_RX;
  930.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  931.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  932.   if (HAL_UART_Init(&huart2) != HAL_OK)
  933.   {
  934.     Error_Handler();
  935.   }
  936.   /* USER CODE BEGIN USART2_Init 2 */
  937.  
  938.   /* USER CODE END USART2_Init 2 */
  939. }
  940.  
  941. /**
  942.  * @brief USART3 Initialization Function
  943.  * @param None
  944.  * @retval None
  945.  */
  946. static void MX_USART3_UART_Init(void)
  947. {
  948.  
  949.   /* USER CODE BEGIN USART3_Init 0 */
  950.  
  951.   /* USER CODE END USART3_Init 0 */
  952.  
  953.   /* USER CODE BEGIN USART3_Init 1 */
  954.  
  955.   /* USER CODE END USART3_Init 1 */
  956.   huart3.Instance = USART3;
  957.   huart3.Init.BaudRate = 19200;
  958.   huart3.Init.WordLength = UART_WORDLENGTH_8B;
  959.   huart3.Init.StopBits = UART_STOPBITS_1;
  960.   huart3.Init.Parity = UART_PARITY_NONE;
  961.   huart3.Init.Mode = UART_MODE_TX_RX;
  962.   huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  963.   huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  964.   if (HAL_UART_Init(&huart3) != HAL_OK)
  965.   {
  966.     Error_Handler();
  967.   }
  968.   /* USER CODE BEGIN USART3_Init 2 */
  969.  
  970.   /* USER CODE END USART3_Init 2 */
  971. }
  972.  
  973. /**
  974.  * @brief GPIO Initialization Function
  975.  * @param None
  976.  * @retval None
  977.  */
  978. static void MX_GPIO_Init(void)
  979. {
  980.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  981.  
  982.   /* GPIO Ports Clock Enable */
  983.   __HAL_RCC_GPIOH_CLK_ENABLE();
  984.   __HAL_RCC_GPIOA_CLK_ENABLE();
  985.   __HAL_RCC_GPIOC_CLK_ENABLE();
  986.   __HAL_RCC_GPIOB_CLK_ENABLE();
  987.  
  988.   /*Configure GPIO pin Output Level */
  989.   HAL_GPIO_WritePin(GPIOA, SPI_NSS1_Pin | BT_RESET_Pin, GPIO_PIN_SET);
  990.  
  991.   /*Configure GPIO pin Output Level */
  992.   HAL_GPIO_WritePin(GPIOA, SPI_CD_Pin | BT_BUTTON_Pin, GPIO_PIN_RESET);
  993.  
  994.   /*Configure GPIO pin Output Level */
  995.   HAL_GPIO_WritePin(GPIOC, SPI_RESET_Pin | POWER_LATCH_Pin | USB_PWR_Pin, GPIO_PIN_RESET);
  996.  
  997.   /*Configure GPIO pin Output Level */
  998.   HAL_GPIO_WritePin(SPI_NSS2_GPIO_Port, SPI_NSS2_Pin, GPIO_PIN_SET);
  999.  
  1000.   /*Configure GPIO pins : SPI_NSS1_Pin SPI_CD_Pin */
  1001.   GPIO_InitStruct.Pin = SPI_NSS1_Pin | SPI_CD_Pin;
  1002.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  1003.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  1004.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  1005.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  1006.  
  1007.   /*Configure GPIO pins : SPI_RESET_Pin SPI_NSS2_Pin POWER_LATCH_Pin USB_PWR_Pin */
  1008.   GPIO_InitStruct.Pin = SPI_RESET_Pin | SPI_NSS2_Pin | POWER_LATCH_Pin | USB_PWR_Pin;
  1009.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  1010.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  1011.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  1012.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  1013.  
  1014.   /*Configure GPIO pins : BT_STATE_Pin SW1_PUSH_Pin SW2_PUSH_Pin */
  1015.   GPIO_InitStruct.Pin = BT_STATE_Pin | SW1_PUSH_Pin | SW2_PUSH_Pin;
  1016.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  1017.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  1018.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  1019.  
  1020.   /*Configure GPIO pin : IGNITION_Pin */
  1021.   GPIO_InitStruct.Pin = IGNITION_Pin;
  1022.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  1023.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  1024.   HAL_GPIO_Init(IGNITION_GPIO_Port, &GPIO_InitStruct);
  1025.  
  1026.   /*Configure GPIO pins : BT_BUTTON_Pin BT_RESET_Pin */
  1027.   GPIO_InitStruct.Pin = BT_BUTTON_Pin | BT_RESET_Pin;
  1028.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  1029.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  1030.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  1031.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  1032. }
  1033.  
  1034. /* USER CODE BEGIN 4 */
  1035.  
  1036. /* USER CODE END 4 */
  1037.  
  1038. /**
  1039.  * @brief  This function is executed in case of error occurrence.
  1040.  * @retval None
  1041.  */
  1042. void Error_Handler(void)
  1043. {
  1044.   /* USER CODE BEGIN Error_Handler_Debug */
  1045.   /* User can add his own implementation to report the HAL error return state */
  1046.  
  1047.   /* USER CODE END Error_Handler_Debug */
  1048. }
  1049.  
  1050. #ifdef USE_FULL_ASSERT
  1051. /**
  1052.  * @brief  Reports the name of the source file and the source line number
  1053.  *         where the assert_param error has occurred.
  1054.  * @param  file: pointer to the source file name
  1055.  * @param  line: assert_param error line source number
  1056.  * @retval None
  1057.  */
  1058. void assert_failed(uint8_t *file, uint32_t line)
  1059. {
  1060.   /* USER CODE BEGIN 6 */
  1061.   /* User can add his own implementation to report the file name and line number,
  1062.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  1063.   /* USER CODE END 6 */
  1064. }
  1065. #endif /* USE_FULL_ASSERT */
  1066.