Subversion Repositories EngineBay2

Rev

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

  1. /**
  2.  ******************************************************************************
  3.  * File Name          : main.c
  4.  * Description        : Main program body
  5.  ******************************************************************************
  6.  *
  7.  * COPYRIGHT(c) 2017 STMicroelectronics
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without modification,
  10.  * are permitted provided that the following conditions are met:
  11.  *   1. Redistributions of source code must retain the above copyright notice,
  12.  *      this list of conditions and the following disclaimer.
  13.  *   2. Redistributions in binary form must reproduce the above copyright notice,
  14.  *      this list of conditions and the following disclaimer in the documentation
  15.  *      and/or other materials provided with the distribution.
  16.  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  17.  *      may be used to endorse or promote products derived from this software
  18.  *      without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23.  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  *
  31.  ******************************************************************************
  32.  */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32l1xx_hal.h"
  35.  
  36. /* USER CODE BEGIN Includes */
  37. #include "serial.h"
  38. #include "plx.h"
  39. #include "misc.h"
  40. /* USER CODE END Includes */
  41.  
  42. /* Private variables ---------------------------------------------------------*/
  43. ADC_HandleTypeDef hadc;
  44. DMA_HandleTypeDef hdma_adc;
  45.  
  46. SPI_HandleTypeDef hspi1;
  47.  
  48. TIM_HandleTypeDef htim2;
  49. TIM_HandleTypeDef htim6;
  50.  
  51. UART_HandleTypeDef huart1;
  52. UART_HandleTypeDef huart2;
  53.  
  54. /* USER CODE BEGIN PV */
  55. /* Private variables ---------------------------------------------------------*/
  56.  
  57. // with a dwell angle of 45 degrees , 4 cylinders and a maximum RPM of 5000
  58. // freq = 5000/60 * 2 = 166Hz. Because the breaker might bounce , we accept the first pulse longer than 1/300 of a second as being a proper closure .
  59. // the TIM2 counter counts in 10uS increments,
  60. #define BREAKER_MIN (RPM_COUNT_RATE/300)
  61.  
  62. volatile char TimerFlag = 0;
  63.  
  64. volatile char NoSerialInCTR = 0; // Missing characters coming in on USART1
  65. volatile char NoSerialIn = 0;
  66.  
  67. // storage for ADC
  68. uint16_t ADC_Samples[6];
  69.  
  70. #define Scale 1024.0
  71. const float ADC_Scale = 3.3 / (Scale * 4096.0); // convert to a voltage
  72.  
  73. uint32_t FILT_Samples[6]; // filtered ADC samples * 1024
  74. // Rev counter processing from original RevCounter Project
  75. unsigned int RPM_Diff = 0;
  76. unsigned int RPM_Count_Latch = 0;
  77. // accumulators
  78. unsigned int RPM_Pulsecount = 0;
  79. unsigned int RPM_FilteredWidth = 0;
  80.  
  81. unsigned int Coded_RPM = 0;
  82. unsigned int Coded_CHT = 0;
  83.  
  84. uint32_t Power_CHT_Timer;
  85.  
  86. /* USER CODE END PV */
  87.  
  88. /* Private function prototypes -----------------------------------------------*/
  89. void SystemClock_Config(void);
  90. void Error_Handler(void);
  91. static void MX_GPIO_Init(void);
  92. static void MX_DMA_Init(void);
  93. static void MX_ADC_Init(void);
  94. static void MX_SPI1_Init(void);
  95. static void MX_TIM2_Init(void);
  96. static void MX_TIM6_Init(void);
  97. static void MX_USART2_UART_Init(void);
  98. static void MX_USART1_UART_Init(void);
  99.  
  100. /* USER CODE BEGIN PFP */
  101. /* Private function prototypes -----------------------------------------------*/
  102.  
  103. /* USER CODE END PFP */
  104.  
  105. /* USER CODE BEGIN 0 */
  106.  
  107. void plx_sendword(int x)
  108. {
  109.         PutCharSerial(&uc1, ((x) >> 6) & 0x3F);
  110.         PutCharSerial(&uc1, (x) & 0x3F);
  111. }
  112.  
  113. void init_ADC_filter()
  114. {
  115.         int i;
  116.         for (i = 0; i < 6; i++)
  117.         {
  118.                 FILT_Samples[i] = 0;
  119.         }
  120. }
  121.  
  122. void filter_ADC_samples()
  123. {
  124.         int i;
  125.         for (i = 0; i < 6; i++)
  126.         {
  127.                 FILT_Samples[i] += (ADC_Samples[i] * Scale - FILT_Samples[i]) / 2;
  128.         }
  129. }
  130.  
  131. void ProcessRPM(int instance)
  132. {
  133. // compute the timer values
  134. // snapshot timers
  135.         unsigned long RPM_Pulsewidth;
  136.         unsigned long RPM_Count_Val;
  137.         __disable_irq(); // copy the counter value
  138.         RPM_Count_Val = RPM_Count;
  139.         __enable_irq();
  140. // do calculations
  141. // if there is only one entry, cannot get difference
  142.         if (RPM_Count_Latch != RPM_Count_Val)
  143.         {
  144.                 while (1)
  145.                 {
  146.                         unsigned int base_time;
  147.                         unsigned int new_time;
  148.                         // if we are at N-1, stop.
  149.                         unsigned int next_count = RPM_Count_Latch + 1;
  150.                         if (next_count == RPM_SAMPLES)
  151.                         {
  152.                                 next_count = 0;
  153.                         }
  154.                         if (next_count == RPM_Count_Val)
  155.                         {
  156.                                 break;
  157.                         }
  158.                         base_time = RPM_Time[RPM_Count_Latch];
  159.                         new_time = RPM_Time[next_count];
  160.                         RPM_Count_Latch = next_count;
  161.                         if (new_time > base_time)
  162.                         {
  163.                                 RPM_Pulsewidth = new_time - base_time; // not wrapped
  164.                         }
  165.                         else
  166.                         {
  167.                                 RPM_Pulsewidth = new_time - base_time + 65536; // deal with wrapping
  168.                         }
  169.  
  170.                         RPM_Diff += RPM_Pulsewidth;
  171.                         // need to check if this is a long pulse. If it is, keep the answer
  172.                         if (RPM_Pulsewidth > BREAKER_MIN)
  173.                         {
  174.                                 RPM_Pulsecount++; // count one pulse
  175.                                 RPM_FilteredWidth += RPM_Diff; // add its width to the accumulator
  176.                                 RPM_Diff = 0; // reset accumulator of all the narrow widths
  177.                         }
  178.                 }
  179.  
  180.         }
  181.  
  182.         if (RPM_Pulsecount > 0)
  183.         {
  184.                 // now have time for N pulses in clocks
  185.                 // need to scale by 19.55: one unit is 19.55 RPM
  186.                 // 1Hz is 60 RPM
  187.                 float new_RPM = (30.0 / 19.55 * RPM_Pulsecount * RPM_COUNT_RATE)
  188.                                 / (RPM_FilteredWidth) + 0.5;
  189.  
  190.                 Coded_RPM += (new_RPM * Scale - Coded_RPM) / 4;
  191.  
  192. #if !defined MY_DEBUG
  193.                 // reset here unless we want to debug
  194.                 RPM_Pulsecount = 0;
  195.                 RPM_FilteredWidth = 0;
  196. #endif
  197.         }
  198.  
  199. // send the current RPM *calculation
  200.         plx_sendword(PLX_RPM);
  201.         PutCharSerial(&uc1, instance);
  202.         plx_sendword(Coded_RPM / Scale);
  203. }
  204.  
  205. // this uses a MAX6675 which is a simple 16 bit read
  206. // SPI is configured for 8 bits so I can use an OLED display if I need it
  207. // must wait > 0.22 seconds between conversion attempts as this is the measurement time
  208. //
  209.  
  210. GPIO_PinState CHT_Enable;
  211.  
  212. uint8_t CHT_Timer[2] =
  213. { 0, 0 }; // two temperature readings
  214. uint8_t CHT_Observations[2] =
  215. { 0, 0 };
  216.  
  217. void ProcessCHT(int instance)
  218. {
  219.         uint8_t buffer[2];
  220.         if (instance > 2)
  221.                 return;
  222.         CHT_Timer[instance]++;
  223.         if ((CHT_Enable == GPIO_PIN_SET) && (CHT_Timer[instance] >= 3)) // every 300 milliseconds
  224.         {
  225.  
  226.                 CHT_Timer[instance] = 0;
  227.  
  228.                 uint16_t Pin = (instance == 0) ? SPI_NS_Temp_Pin : SPI_NS_Temp2_Pin;
  229.  
  230.                 HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, Pin, GPIO_PIN_RESET);
  231.  
  232.                 HAL_SPI_Receive(&hspi1, buffer, 2, 2);
  233.  
  234.                 HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, Pin, GPIO_PIN_SET);
  235.  
  236.                 uint16_t obs = (buffer[0] << 8) | buffer[1];
  237.  
  238.                 uint8_t good = (obs & 4) == 0;
  239.                 if (good)
  240.                 {
  241.                         CHT_Observations[instance] = obs >> 5;
  242.                 }
  243.                 else
  244.                 {
  245.                         CHT_Observations[instance] = 1024; // signal fail
  246.                 }
  247.         }
  248.  
  249.         plx_sendword(PLX_X_CHT);
  250.         PutCharSerial(&uc1, instance);
  251.         plx_sendword(CHT_Observations[instance]);
  252.  
  253. }
  254.  
  255. void EnableCHT(GPIO_PinState state)
  256. {
  257.  
  258.         CHT_Enable = state;
  259.         HAL_GPIO_WritePin(ENA_AUX_5V_GPIO_Port, ENA_AUX_5V_Pin, state);
  260. }
  261.  
  262. // 1023 is 20.00 volts.
  263. void ProcessBatteryVoltage(int instance)
  264. {
  265.         float reading = FILT_Samples[instance] * ADC_Scale;
  266.         reading = reading * 7.8125; // real voltage
  267.         reading = reading * 51.15; // 1023/20
  268.  
  269.         plx_sendword(PLX_Volts);
  270.         PutCharSerial(&uc1, instance);
  271.         plx_sendword((uint16_t) reading);
  272.  
  273. }
  274.  
  275. /****!
  276.  * @brief this reads the reference voltage within the STM32L151
  277.  * Powers up reference voltage and temperature sensor, waits 3mS  and takes reading
  278.  * Requires that the ADC be powered up
  279.  */
  280.  
  281. uint32_t ADC_VREF_MV = 3300;           // 3.300V typical
  282. const uint16_t STM32REF_MV = 1224;           // 1.224V typical
  283.  
  284. void CalibrateADC(void)
  285. {
  286.         uint32_t adc_val = FILT_Samples[6];       // as set up in device config
  287.         ADC_VREF_MV = (STM32REF_MV * 4096) / adc_val;
  288. }
  289.  
  290. void ProcessCPUTemperature(int instance)
  291. {
  292.         int32_t temp_val;
  293.         uint16_t TS_CAL30 = *(uint16_t *) 0x1FF8007A; /* ADC reading for temperature sensor at 30 degrees C with Vref = 3000mV */
  294.         uint16_t TS_CAL110 = *(uint16_t *) 0x1FF8007E; /* ADC reading for temperature sensor at 110 degrees C with Vref = 3000mV */
  295.         /* get the ADC reading corresponding to ADC channel 16 after turning on the ADC */
  296.  
  297.         temp_val = FILT_Samples[5];
  298.  
  299.         /* renormalise temperature value to account for different ADC Vref  : normalise to that which we would get for a 3000mV reference */
  300.         temp_val = temp_val * ADC_VREF_MV / 3000UL;
  301.  
  302.         int32_t result = 800 * ((int32_t) temp_val - TS_CAL30);
  303.         result = result / (TS_CAL110 - TS_CAL30) + 300;
  304.  
  305.         if (result < 0)
  306.         {
  307.                 result = 0;
  308.         }
  309.         plx_sendword(PLX_FluidTemp);
  310.         PutCharSerial(&uc1, instance);
  311.         plx_sendword(result / 10);
  312.  
  313. }
  314.  
  315. // the MAP sensor is giving us a reading of
  316. // 4.6 volts for 1019mB or 2.27 volts at the ADC input (resistive divider by 2.016)
  317. // I believe the sensor reads  4.5V at 1000kPa and 0.5V at  0kPa
  318.  
  319. void ProcessMAP(int instance)
  320. {
  321. // Using ADC_Samples[3] as the MAP input
  322.         float reading = FILT_Samples[3] * ADC_Scale;
  323.         reading = reading * 2.016;      // real voltage
  324.         reading = (reading) * 1000 / 4.5; // do not assume 0.5 volt offset : reading from 0 to 4.5 instead of 0.5 to 4.5
  325.         plx_sendword(PLX_MAP);
  326.         PutCharSerial(&uc1, instance);
  327.         plx_sendword((uint16_t) reading);
  328.  
  329. }
  330.  
  331. // the Oil pressi sensor is giving us a reading of
  332. // 4.5 volts for 100 PSI or  2.25 volts at the ADC input (resistive divider by 2.016)
  333. // I believe the sensor reads  4.5V at 100PSI and 0.5V at  0PSI
  334. // an observation of 1024 is 200PSI, so observation of 512 is 100 PSI.
  335.  
  336. void ProcessOilPress(int instance)
  337. {
  338. // Using ADC_Samples[2] as the MAP input
  339.         float reading = FILT_Samples[2] * ADC_Scale;
  340.         reading = reading * 2.00; // real voltage
  341.         reading = (reading - 0.5) * 512 / 4;  // this is 1023 * 100/200
  342.  
  343.         plx_sendword(PLX_FluidPressure);
  344.         PutCharSerial(&uc1, instance);
  345.         plx_sendword((uint16_t) reading);
  346.  
  347. }
  348.  
  349. void ProcessTiming(int instance)
  350. {
  351.         plx_sendword(PLX_Timing);
  352.         PutCharSerial(&uc1, instance);
  353.         plx_sendword(64 - 15); // make it negative
  354. }
  355.  
  356. /* USER CODE END 0 */
  357.  
  358. int main(void)
  359. {
  360.  
  361.         /* USER CODE BEGIN 1 */
  362.  
  363.         /* USER CODE END 1 */
  364.  
  365.         /* MCU Configuration----------------------------------------------------------*/
  366.  
  367.         /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  368.         HAL_Init();
  369.  
  370.         /* Configure the system clock */
  371.         SystemClock_Config();
  372.  
  373.         /* Initialize all configured peripherals */
  374.         MX_GPIO_Init();
  375.         MX_DMA_Init();
  376.         MX_ADC_Init();
  377.         MX_SPI1_Init();
  378.         MX_TIM2_Init();
  379.         MX_TIM6_Init();
  380.         MX_USART2_UART_Init();
  381.         MX_USART1_UART_Init();
  382.  
  383.         /* USER CODE BEGIN 2 */
  384.         HAL_MspInit();
  385.  
  386. // Not using HAL USART code
  387.         __HAL_RCC_USART1_CLK_ENABLE()
  388.         ; // PLX comms port
  389.         __HAL_RCC_USART2_CLK_ENABLE()
  390.         ;  // Debug comms port
  391.         /* setup the USART control blocks */
  392.         init_usart_ctl(&uc1, huart1.Instance);
  393.         init_usart_ctl(&uc2, huart2.Instance);
  394.  
  395.         EnableSerialRxInterrupt(&uc1);
  396.         EnableSerialRxInterrupt(&uc2);
  397.  
  398.         HAL_SPI_MspInit(&hspi1);
  399.  
  400.         HAL_ADC_MspInit(&hadc);
  401.  
  402.         HAL_ADC_Start_DMA(&hadc, ADC_Samples, 6);
  403.  
  404.         HAL_ADC_Start_IT(&hadc);
  405.  
  406.         HAL_TIM_Base_MspInit(&htim6);
  407.         HAL_TIM_Base_Start_IT(&htim6);
  408.  
  409. // initialise all the STMCubeMX stuff
  410.         HAL_TIM_Base_MspInit(&htim2);
  411. // Start the counter
  412.         HAL_TIM_Base_Start(&htim2);
  413. // Start the input capture and the interrupt
  414.         HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
  415.  
  416.         init_ADC_filter();
  417.  
  418.         uint32_t Ticks = HAL_GetTick() + 100;
  419.         int CalCounter = 0;
  420.  
  421.         Power_CHT_Timer = HAL_GetTick() + 10000; /* wait 10 seconds before powering up the CHT sensor */
  422.  
  423.         /* USER CODE END 2 */
  424.  
  425.         /* Infinite loop */
  426.         /* USER CODE BEGIN WHILE */
  427.         while (1)
  428.         {
  429.                 /* USER CODE END WHILE */
  430.  
  431.                 /* USER CODE BEGIN 3 */
  432.  
  433.                 if (HAL_GetTick() > Ticks)
  434.                 {
  435.                         Ticks += 100;
  436.                         filter_ADC_samples();
  437.                         // delay to calibrate ADC
  438.                         if (CalCounter < 500)
  439.                         {
  440.                                 CalCounter += 100;
  441.                         }
  442.  
  443.                         if (CalCounter == 400)
  444.                         {
  445.                                 CalibrateADC();
  446.                         }
  447.                 }
  448.                 /* when the starter motor is on then power down the CHT sensors as they seem to fail */
  449.  
  450.                 if (HAL_GPIO_ReadPin(STARTER_ON_GPIO_Port, STARTER_ON_Pin)
  451.                                 == GPIO_PIN_RESET)
  452.                 {
  453.                         EnableCHT(GPIO_PIN_RESET);
  454.                         Power_CHT_Timer = HAL_GetTick() + 500;
  455.                 }
  456.                 else
  457.                 /* if the Power_CHT_Timer is set then wait for it to timeout, then power up CHT */
  458.                 {
  459.                         if ((Power_CHT_Timer > 0) && (HAL_GetTick() > Power_CHT_Timer))
  460.                         {
  461.                                 EnableCHT(GPIO_PIN_SET);
  462.                                 Power_CHT_Timer = 0;
  463.                         }
  464.                 }
  465.  
  466.                 // check to see if we have any incoming data, copy and append if so, if no data then create our own frames.
  467.                 int c;
  468.                 char send = 0;
  469.  
  470.                 // poll the  input for a stop bit or timeout
  471.                 if (PollSerial(&uc1))
  472.                 {
  473.                         c = GetCharSerial(&uc1);
  474.                         if (c != PLX_Stop)
  475.                         {
  476.                                 PutCharSerial(&uc1, c); // echo all but the stop bit
  477.                         }
  478.                         else
  479.                         { // must be a stop character
  480.                                 send = 1; // start our sending process.
  481.                         }
  482.                 }
  483.  
  484.                 // sort out auto-sending
  485.                 if (TimerFlag)
  486.                 {
  487.                         TimerFlag = 0;
  488.                         if (NoSerialIn)
  489.                         {
  490.                                 PutCharSerial(&uc1, PLX_Start);
  491.                                 send = 1;
  492.                         }
  493.                 }
  494.                 if (send)
  495.                 {
  496.                         send = 0;
  497.  
  498.                         uint16_t val;
  499.                         val = __HAL_TIM_GET_COMPARE(&htim2,TIM_CHANNEL_1);
  500.                         PutCharSerial(&uc2, (val & 31) + 32);
  501.  
  502.                         // send the observations
  503.                         ProcessRPM(0);
  504.                         ProcessCHT(0);
  505.                         //      ProcessCHT(1);
  506.                         ProcessBatteryVoltage(0); // Batt 1
  507.                         ProcessBatteryVoltage(1); // Batt 2
  508.                         ProcessCPUTemperature(0); //  built in temperature sensor
  509.  
  510.                         ProcessMAP(0);
  511.                         ProcessOilPress(0);
  512.  
  513.                         PutCharSerial(&uc1, PLX_Stop);
  514.                 }
  515.         }
  516.         /* USER CODE END 3 */
  517. }
  518. /** System Clock Configuration
  519.  */
  520. void SystemClock_Config(void)
  521. {
  522.  
  523.         RCC_OscInitTypeDef RCC_OscInitStruct;
  524.         RCC_ClkInitTypeDef RCC_ClkInitStruct;
  525.  
  526.         __HAL_RCC_PWR_CLK_ENABLE()
  527.         ;
  528.  
  529.         __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  530.  
  531.         RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  532.         RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  533.         RCC_OscInitStruct.HSICalibrationValue = 16;
  534.         RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  535.         RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  536.         RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  537.         RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
  538.         if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  539.         {
  540.                 Error_Handler();
  541.         }
  542.  
  543.         RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  544.                         | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  545.         RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  546.         RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  547.         RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  548.         RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  549.         if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  550.         {
  551.                 Error_Handler();
  552.         }
  553.  
  554.         HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
  555.  
  556.         HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  557.  
  558.         /* SysTick_IRQn interrupt configuration */
  559.         HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  560. }
  561.  
  562. /* ADC init function */
  563. static void MX_ADC_Init(void)
  564. {
  565.  
  566.         ADC_ChannelConfTypeDef sConfig;
  567.  
  568.         /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  569.          */
  570.         hadc.Instance = ADC1;
  571.         hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  572.         hadc.Init.Resolution = ADC_RESOLUTION_12B;
  573.         hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  574.         hadc.Init.ScanConvMode = ADC_SCAN_ENABLE;
  575.         hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  576.         hadc.Init.LowPowerAutoWait = ADC_AUTOWAIT_DISABLE;
  577.         hadc.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE;
  578.         hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
  579.         hadc.Init.ContinuousConvMode = DISABLE;
  580.         hadc.Init.NbrOfConversion = 6;
  581.         hadc.Init.DiscontinuousConvMode = DISABLE;
  582.         hadc.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T6_TRGO;
  583.         hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
  584.         hadc.Init.DMAContinuousRequests = ENABLE;
  585.         if (HAL_ADC_Init(&hadc) != HAL_OK)
  586.         {
  587.                 Error_Handler();
  588.         }
  589.  
  590.         /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  591.          */
  592.         sConfig.Channel = ADC_CHANNEL_10;
  593.         sConfig.Rank = 1;
  594.         sConfig.SamplingTime = ADC_SAMPLETIME_384CYCLES;
  595.         if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  596.         {
  597.                 Error_Handler();
  598.         }
  599.  
  600.         /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  601.          */
  602.         sConfig.Channel = ADC_CHANNEL_11;
  603.         sConfig.Rank = 2;
  604.         if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  605.         {
  606.                 Error_Handler();
  607.         }
  608.  
  609.         /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  610.          */
  611.         sConfig.Channel = ADC_CHANNEL_12;
  612.         sConfig.Rank = 3;
  613.         if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  614.         {
  615.                 Error_Handler();
  616.         }
  617.  
  618.         /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  619.          */
  620.         sConfig.Channel = ADC_CHANNEL_13;
  621.         sConfig.Rank = 4;
  622.         if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  623.         {
  624.                 Error_Handler();
  625.         }
  626.  
  627.         /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  628.          */
  629.         sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
  630.         sConfig.Rank = 5;
  631.         if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  632.         {
  633.                 Error_Handler();
  634.         }
  635.  
  636.         /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  637.          */
  638.         sConfig.Channel = ADC_CHANNEL_VREFINT;
  639.         sConfig.Rank = 6;
  640.         if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  641.         {
  642.                 Error_Handler();
  643.         }
  644.  
  645. }
  646.  
  647. /* SPI1 init function */
  648. static void MX_SPI1_Init(void)
  649. {
  650.  
  651.         hspi1.Instance = SPI1;
  652.         hspi1.Init.Mode = SPI_MODE_MASTER;
  653.         hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  654.         hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  655.         hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  656.         hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  657.         hspi1.Init.NSS = SPI_NSS_SOFT;
  658.         hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  659.         hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  660.         hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  661.         hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  662.         hspi1.Init.CRCPolynomial = 10;
  663.         if (HAL_SPI_Init(&hspi1) != HAL_OK)
  664.         {
  665.                 Error_Handler();
  666.         }
  667.  
  668. }
  669.  
  670. /* TIM2 init function */
  671. static void MX_TIM2_Init(void)
  672. {
  673.  
  674.         TIM_ClockConfigTypeDef sClockSourceConfig;
  675.         TIM_MasterConfigTypeDef sMasterConfig;
  676.         TIM_IC_InitTypeDef sConfigIC;
  677.  
  678.         htim2.Instance = TIM2;
  679.         htim2.Init.Prescaler = 320;
  680.         htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  681.         htim2.Init.Period = 65535;
  682.         htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  683.         if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  684.         {
  685.                 Error_Handler();
  686.         }
  687.  
  688.         sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  689.         if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  690.         {
  691.                 Error_Handler();
  692.         }
  693.  
  694.         if (HAL_TIM_IC_Init(&htim2) != HAL_OK)
  695.         {
  696.                 Error_Handler();
  697.         }
  698.  
  699.         sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  700.         sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  701.         if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  702.         {
  703.                 Error_Handler();
  704.         }
  705.  
  706.         sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
  707.         sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
  708.         sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
  709.         sConfigIC.ICFilter = 0;
  710.         if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
  711.         {
  712.                 Error_Handler();
  713.         }
  714.  
  715. }
  716.  
  717. /* TIM6 init function */
  718. static void MX_TIM6_Init(void)
  719. {
  720.  
  721.         TIM_MasterConfigTypeDef sMasterConfig;
  722.  
  723.         htim6.Instance = TIM6;
  724.         htim6.Init.Prescaler = 320;
  725.         htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  726.         htim6.Init.Period = 9999;
  727.         if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  728.         {
  729.                 Error_Handler();
  730.         }
  731.  
  732.         sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  733.         sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  734.         if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
  735.         {
  736.                 Error_Handler();
  737.         }
  738.  
  739. }
  740.  
  741. /* USART1 init function */
  742. static void MX_USART1_UART_Init(void)
  743. {
  744.  
  745.         huart1.Instance = USART1;
  746.         huart1.Init.BaudRate = 19200;
  747.         huart1.Init.WordLength = UART_WORDLENGTH_8B;
  748.         huart1.Init.StopBits = UART_STOPBITS_1;
  749.         huart1.Init.Parity = UART_PARITY_NONE;
  750.         huart1.Init.Mode = UART_MODE_TX_RX;
  751.         huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  752.         huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  753.         if (HAL_UART_Init(&huart1) != HAL_OK)
  754.         {
  755.                 Error_Handler();
  756.         }
  757.  
  758. }
  759.  
  760. /* USART2 init function */
  761. static void MX_USART2_UART_Init(void)
  762. {
  763.  
  764.         huart2.Instance = USART2;
  765.         huart2.Init.BaudRate = 115200;
  766.         huart2.Init.WordLength = UART_WORDLENGTH_8B;
  767.         huart2.Init.StopBits = UART_STOPBITS_1;
  768.         huart2.Init.Parity = UART_PARITY_NONE;
  769.         huart2.Init.Mode = UART_MODE_TX_RX;
  770.         huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  771.         huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  772.         if (HAL_UART_Init(&huart2) != HAL_OK)
  773.         {
  774.                 Error_Handler();
  775.         }
  776.  
  777. }
  778.  
  779. /**
  780.  * Enable DMA controller clock
  781.  */
  782. static void MX_DMA_Init(void)
  783. {
  784.         /* DMA controller clock enable */
  785.         __HAL_RCC_DMA1_CLK_ENABLE()
  786.         ;
  787.  
  788.         /* DMA interrupt init */
  789.         /* DMA1_Channel1_IRQn interrupt configuration */
  790.         HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  791.         HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  792.  
  793. }
  794.  
  795. /** Configure pins as
  796.  * Analog
  797.  * Input
  798.  * Output
  799.  * EVENT_OUT
  800.  * EXTI
  801.  * Free pins are configured automatically as Analog (this feature is enabled through
  802.  * the Code Generation settings)
  803.  */
  804. static void MX_GPIO_Init(void)
  805. {
  806.  
  807.         GPIO_InitTypeDef GPIO_InitStruct;
  808.  
  809.         /* GPIO Ports Clock Enable */
  810.         __HAL_RCC_GPIOC_CLK_ENABLE()
  811.         ;
  812.         __HAL_RCC_GPIOH_CLK_ENABLE()
  813.         ;
  814.         __HAL_RCC_GPIOA_CLK_ENABLE()
  815.         ;
  816.         __HAL_RCC_GPIOB_CLK_ENABLE()
  817.         ;
  818.         __HAL_RCC_GPIOD_CLK_ENABLE()
  819.         ;
  820.  
  821.         /*Configure GPIO pins : PC13 PC14 PC15 PC6
  822.          PC7 PC8 PC9 PC11
  823.          PC12 */
  824.         GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15 | GPIO_PIN_6
  825.                         | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_11 | GPIO_PIN_12;
  826.         GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  827.         GPIO_InitStruct.Pull = GPIO_NOPULL;
  828.         HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  829.  
  830.         /*Configure GPIO pins : PH0 PH1 */
  831.         GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
  832.         GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  833.         GPIO_InitStruct.Pull = GPIO_NOPULL;
  834.         HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
  835.  
  836.         /*Configure GPIO pins : PA0 PA1 PA8 PA11
  837.          PA12 */
  838.         GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_8 | GPIO_PIN_11
  839.                         | GPIO_PIN_12;
  840.         GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  841.         GPIO_InitStruct.Pull = GPIO_NOPULL;
  842.         HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  843.  
  844.         /*Configure GPIO pin : LED_Blink_Pin */
  845.         GPIO_InitStruct.Pin = LED_Blink_Pin;
  846.         GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  847.         GPIO_InitStruct.Pull = GPIO_NOPULL;
  848.         GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  849.         HAL_GPIO_Init(LED_Blink_GPIO_Port, &GPIO_InitStruct);
  850.  
  851.         /*Configure GPIO pins : SPI_NSS1_Pin SPI1CD_Pin */
  852.         GPIO_InitStruct.Pin = SPI_NSS1_Pin | SPI1CD_Pin;
  853.         GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  854.         GPIO_InitStruct.Pull = GPIO_NOPULL;
  855.         GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  856.         HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  857.  
  858.         /*Configure GPIO pins : SPI_RESET_Pin SPI_NS_Temp_Pin SPI_NS_Temp2_Pin ENA_AUX_5V_Pin */
  859.         GPIO_InitStruct.Pin = SPI_RESET_Pin | SPI_NS_Temp_Pin | SPI_NS_Temp2_Pin
  860.                         | ENA_AUX_5V_Pin;
  861.         GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  862.         GPIO_InitStruct.Pull = GPIO_NOPULL;
  863.         GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  864.         HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  865.  
  866.         /*Configure GPIO pins : PB11 PB12 PB13 PB14
  867.          PB15 PB3 PB4 PB5
  868.          PB6 PB7 PB8 PB9 */
  869.         GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14
  870.                         | GPIO_PIN_15 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6
  871.                         | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
  872.         GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  873.         GPIO_InitStruct.Pull = GPIO_NOPULL;
  874.         HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  875.  
  876.         /*Configure GPIO pin : STARTER_ON_Pin */
  877.         GPIO_InitStruct.Pin = STARTER_ON_Pin;
  878.         GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  879.         GPIO_InitStruct.Pull = GPIO_NOPULL;
  880.         HAL_GPIO_Init(STARTER_ON_GPIO_Port, &GPIO_InitStruct);
  881.  
  882.         /*Configure GPIO pin : PD2 */
  883.         GPIO_InitStruct.Pin = GPIO_PIN_2;
  884.         GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  885.         GPIO_InitStruct.Pull = GPIO_NOPULL;
  886.         HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  887.  
  888.         /*Configure GPIO pin Output Level */
  889.         HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin, GPIO_PIN_RESET);
  890.  
  891.         /*Configure GPIO pin Output Level */
  892.         HAL_GPIO_WritePin(SPI_NSS1_GPIO_Port, SPI_NSS1_Pin, GPIO_PIN_SET);
  893.  
  894.         /*Configure GPIO pin Output Level */
  895.         HAL_GPIO_WritePin(SPI1CD_GPIO_Port, SPI1CD_Pin, GPIO_PIN_RESET);
  896.  
  897.         /*Configure GPIO pin Output Level */
  898.         HAL_GPIO_WritePin(GPIOB, SPI_RESET_Pin | SPI_NS_Temp2_Pin | ENA_AUX_5V_Pin,
  899.                         GPIO_PIN_RESET);
  900.  
  901.         /*Configure GPIO pin Output Level */
  902.         HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_SET);
  903.  
  904. }
  905.  
  906. /* USER CODE BEGIN 4 */
  907.  
  908. /* USER CODE END 4 */
  909.  
  910. /**
  911.  * @brief  This function is executed in case of error occurrence.
  912.  * @param  None
  913.  * @retval None
  914.  */
  915. void Error_Handler(void)
  916. {
  917.         /* USER CODE BEGIN Error_Handler */
  918.         /* User can add his own implementation to report the HAL error return state */
  919.         while (1)
  920.         {
  921.         }
  922.         /* USER CODE END Error_Handler */
  923. }
  924.  
  925. #ifdef USE_FULL_ASSERT
  926.  
  927. /**
  928.  * @brief Reports the name of the source file and the source line number
  929.  * where the assert_param error has occurred.
  930.  * @param file: pointer to the source file name
  931.  * @param line: assert_param error line source number
  932.  * @retval None
  933.  */
  934. void assert_failed(uint8_t* file, uint32_t line)
  935. {
  936.         /* USER CODE BEGIN 6 */
  937.         /* User can add his own implementation to report the file name and line number,
  938.          ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  939.         /* USER CODE END 6 */
  940.  
  941. }
  942.  
  943. #endif
  944.  
  945. /**
  946.  * @}
  947.  */
  948.  
  949. /**
  950.  * @}
  951.  */
  952.  
  953. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  954.