Subversion Repositories EngineBay2

Rev

Rev 11 | Rev 13 | 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) 2016 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.  
  58. // with a dwell angle of 45 degrees , 4 cylinders and a maximum RPM of 5000
  59. // 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 .
  60. // the TIM2 counter counts in 10uS increments,
  61.  
  62. #define BREAKER_MIN (RPM_COUNT_RATE/300)
  63.  
  64.  
  65. volatile char TimerFlag = 0;
  66.  
  67. volatile char NoSerialInCTR = 0; // Missing characters coming in on USART1
  68. volatile char NoSerialIn = 0;
  69.  
  70. // storage for ADC
  71. long ADC_Samples[6];
  72.  
  73. // Rev counter processing from original RevCounter Project
  74. unsigned int RPM_Diff = 0;
  75. unsigned int RPM_Count_Latch = 0;
  76. // accumulators
  77. unsigned int RPM_Pulsecount = 0;
  78. unsigned int RPM_FilteredWidth = 0;
  79.  
  80. unsigned int Coded_RPM = 0;
  81. unsigned int Coded_CHT = 0;
  82.  
  83. /* USER CODE END PV */
  84.  
  85. /* Private function prototypes -----------------------------------------------*/
  86. void SystemClock_Config(void);
  87. void Error_Handler(void);
  88. static void MX_GPIO_Init(void);
  89. static void MX_DMA_Init(void);
  90. static void MX_ADC_Init(void);
  91. static void MX_SPI1_Init(void);
  92. static void MX_TIM2_Init(void);
  93. static void MX_TIM6_Init(void);
  94. static void MX_USART1_UART_Init(void);
  95. static void MX_USART2_UART_Init(void);
  96.  
  97. void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
  98.                
  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 ConfigureDMA(void) {
  108.         hdma_adc.Instance = DMA1_Channel1;
  109.         hdma_adc.Init.Direction = DMA_PERIPH_TO_MEMORY;
  110.         hdma_adc.Init.PeriphInc = DMA_PINC_DISABLE;
  111.         hdma_adc.Init.MemInc = DMA_MINC_ENABLE;
  112.         hdma_adc.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  113.         hdma_adc.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
  114.         hdma_adc.Init.Mode = DMA_CIRCULAR; // make the DMA loop automatically
  115.         hdma_adc.Init.Priority = DMA_PRIORITY_LOW;
  116.         HAL_DMA_Init(&hdma_adc);
  117.         __HAL_LINKDMA(&hadc, DMA_Handle, hdma_adc);
  118.  
  119. }
  120.  
  121. void plx_sendword(int x) {
  122.         PutCharSerial(&uc1, ((x) >> 6) & 0x3F);
  123.         PutCharSerial(&uc1, (x) & 0x3F);
  124. }
  125.  
  126. void ProcessRPM(int instance) {
  127. // compute the timer values
  128. // snapshot timers
  129.         unsigned long RPM_Pulsewidth;
  130.         unsigned long RPM_Count_Val;
  131.         __disable_irq(); // copy the counter value
  132.         RPM_Count_Val = RPM_Count;
  133.         __enable_irq();
  134. // do calculations
  135. // if there is only one entry, cannot get difference
  136.         if (RPM_Count_Latch != RPM_Count_Val) {
  137.                 while (1) {
  138.                         unsigned int base_time;
  139.                         unsigned int new_time;
  140.                         // if we are at N-1, stop.
  141.                         unsigned int next_count = RPM_Count_Latch + 1;
  142.                         if (next_count == RPM_SAMPLES) {
  143.                                 next_count = 0;
  144.                         }
  145.                         if (next_count == RPM_Count_Val) {
  146.                                 break;
  147.                         }
  148.                         base_time = RPM_Time[RPM_Count_Latch];
  149.                         new_time = RPM_Time[next_count];
  150.                         RPM_Count_Latch = next_count;
  151.                         if (new_time > base_time) {
  152.                                 RPM_Pulsewidth = new_time - base_time; // not wrapped
  153.                         } else {
  154.                                 RPM_Pulsewidth = new_time + (~base_time) + 1; // deal with wrapping
  155.                         }
  156.  
  157.                         RPM_Diff += RPM_Pulsewidth;
  158.                         // need to check if this is a long pulse. If it is, keep the answer
  159.                         if (RPM_Pulsewidth > BREAKER_MIN) {
  160.                                 RPM_Pulsecount++; // count one pulse
  161.                                 RPM_FilteredWidth += RPM_Diff; // add its width to the accumulator
  162.                                 RPM_Diff = 0; // reset accumulator of all the narrow widths
  163.                         }
  164.                 }
  165.  
  166.         }
  167.  
  168.         if (RPM_Pulsecount > 0) {
  169.  
  170.                 // now have time for N pulses in clocks
  171.                 // need to scale by 19.55: one unit is 19.55 RPM
  172.                 // 1Hz is 60 RPM
  173.                 Coded_RPM = (30.0 / 19.55 * RPM_Pulsecount * RPM_COUNT_RATE)
  174.                                 / (RPM_FilteredWidth) + 0.5;
  175. #if !defined MY_DEBUG
  176.                 // reset here unless we want to debug
  177.                 RPM_Pulsecount = 0;
  178.                 RPM_FilteredWidth = 0;
  179. #endif
  180.         }
  181.  
  182. // send the current RPM calculation
  183.         plx_sendword(PLX_RPM);
  184.         PutCharSerial(&uc1, instance);
  185.         plx_sendword(Coded_RPM);
  186. }
  187.  
  188. uint8_t CHT_Timer = 0;
  189.  
  190. // this uses a MAX6675 which is a simple 16 bit read
  191. // SPI is configured for 8 bits so I can use an OLED display if I need it
  192. // must wait > 0.22 seconds between conversion attempts as this is the measurement time
  193. //
  194. void ProcessCHT(int instance)
  195. {
  196.         uint8_t buffer[2];
  197.         CHT_Timer++;
  198.         if(CHT_Timer >= 3) // every 300 milliseconds
  199.  
  200.         {
  201.                 CHT_Timer=0;
  202.  
  203.                    HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_RESET);
  204.  
  205.  
  206.                    HAL_Delay(1);
  207.  
  208.                    HAL_SPI_Receive(&hspi1, buffer, 2, 2);
  209.  
  210.  
  211.  
  212.                    uint16_t obs = (buffer[0]<<8)| buffer[1];
  213.  
  214.                    uint8_t  good = (obs & 4)==0;
  215.                    if(good)
  216.                    {
  217.                      Coded_CHT = obs>>5;
  218.                    }
  219.                    else
  220.                    {
  221.                           Coded_CHT= 1000; // signal fail
  222.                    }
  223.         }
  224.  
  225.         plx_sendword(PLX_EGT);
  226.         PutCharSerial(&uc1, instance);
  227.         plx_sendword(Coded_CHT);
  228.      HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_SET);
  229.  
  230. }
  231.  
  232. void ProcessBatteryVoltage(int instance)
  233. {
  234.         plx_sendword(PLX_Volts);
  235.         PutCharSerial(&uc1, instance);
  236.         plx_sendword(ADC_Samples[instance]);
  237.  
  238.  
  239.  
  240. }
  241.  
  242.  
  243. /* USER CODE END 0 */
  244.  
  245. int main(void)
  246. {
  247.  
  248.   /* USER CODE BEGIN 1 */
  249.  
  250.   /* USER CODE END 1 */
  251.  
  252.   /* MCU Configuration----------------------------------------------------------*/
  253.  
  254.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  255.   HAL_Init();
  256.  
  257.   /* Configure the system clock */
  258.   SystemClock_Config();
  259.  
  260.   /* Initialize all configured peripherals */
  261.   MX_GPIO_Init();
  262.   MX_DMA_Init();
  263.   MX_ADC_Init();
  264.   MX_SPI1_Init();
  265.   MX_TIM2_Init();
  266.   MX_TIM6_Init();
  267.   MX_USART1_UART_Init();
  268.   MX_USART2_UART_Init();
  269.  
  270.   /* USER CODE BEGIN 2 */
  271.  
  272.         __HAL_RCC_SPI1_CLK_ENABLE()
  273.         ;   // Temp sensor port
  274.         __HAL_RCC_USART1_CLK_ENABLE()
  275.         ; // PLX comms port
  276.         __HAL_RCC_USART2_CLK_ENABLE()
  277.         ;  // Debug comms port
  278.  
  279.         __HAL_RCC_ADC1_CLK_ENABLE()
  280.         ; // enable the ADC
  281.  
  282.         __HAL_RCC_TIM6_CLK_ENABLE()
  283.         ;
  284.         __HAL_RCC_TIM2_CLK_ENABLE();
  285.  
  286.         ConfigureDMA();
  287.         HAL_ADC_Start_DMA(&hadc, ADC_Samples, 6);
  288.  
  289.         /* setup the USART control blocks */
  290.         init_usart_ctl(&uc1, huart1.Instance);
  291.         init_usart_ctl(&uc2, huart2.Instance);
  292.  
  293.         EnableSerialRxInterrupt(&uc1);
  294.         EnableSerialRxInterrupt(&uc2);
  295.  
  296.         HAL_TIM_Base_Start_IT(&htim6);
  297.         HAL_TIM_Base_Start(&htim2);
  298.  
  299.          __HAL_TIM_ENABLE_IT(&htim2, TIM_IT_CC1);
  300.  
  301.  
  302.   /* USER CODE END 2 */
  303.  
  304.   /* Infinite loop */
  305.   /* USER CODE BEGIN WHILE */
  306.         while (1) {
  307.   /* USER CODE END WHILE */
  308.  
  309.   /* USER CODE BEGIN 3 */
  310.      // check to see if we have any incoming data, copy and append if so, if no data then create our own frames.
  311.                 int c;
  312.                 char send = 0;
  313.  
  314.                 // poll the  input for a stop bit or timeout
  315.                 if(PollSerial(&uc1))
  316.                 {
  317.                   c = GetCharSerial(&uc1);
  318.                   if (c != PLX_Stop)
  319.                   {
  320.                                 PutCharSerial(&uc1,c); // echo all but the stop bit
  321.                   } else { // must be a stop character
  322.                                 send = 1; // start our sending process.
  323.                         }
  324.                 }
  325.  
  326.                 // sort out auto-sending
  327.                 if (TimerFlag)
  328.                 {
  329.                         TimerFlag = 0;
  330.                   if (NoSerialIn)
  331.                   {
  332.                         PutCharSerial(&uc1,PLX_Start);
  333.                         send = 1;
  334.                   }
  335.                 }
  336.                 if (send)
  337.                 {
  338.                   send = 0;
  339.  
  340.                   uint16_t val;
  341.                   val = __HAL_TIM_GET_COUNTER(&htim2);
  342.           PutCharSerial(&uc2,(val&31) + 32);
  343.  
  344.   // send the observations
  345.                  ProcessRPM(0);
  346.                  ProcessCHT(0);
  347.                  ProcessBatteryVoltage(0);
  348.                  ProcessBatteryVoltage(1);
  349.  
  350.                  PutCharSerial(&uc1,PLX_Stop);
  351.                 }
  352.  
  353.         }
  354.   /* USER CODE END 3 */
  355.  
  356. }
  357.  
  358. /** System Clock Configuration
  359. */
  360. void SystemClock_Config(void)
  361. {
  362.  
  363.   RCC_OscInitTypeDef RCC_OscInitStruct;
  364.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  365.  
  366.   __HAL_RCC_PWR_CLK_ENABLE();
  367.  
  368.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  369.  
  370.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  371.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  372.   RCC_OscInitStruct.HSICalibrationValue = 16;
  373.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  374.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  375.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  376.   RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
  377.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  378.   {
  379.     Error_Handler();
  380.   }
  381.  
  382.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  383.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  384.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  385.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  386.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  387.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  388.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  389.   {
  390.     Error_Handler();
  391.   }
  392.  
  393.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  394.  
  395.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  396.  
  397.   /* SysTick_IRQn interrupt configuration */
  398.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  399. }
  400.  
  401. /* ADC init function */
  402. static void MX_ADC_Init(void)
  403. {
  404.  
  405.   ADC_ChannelConfTypeDef sConfig;
  406.  
  407.     /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  408.     */
  409.   hadc.Instance = ADC1;
  410.   hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  411.   hadc.Init.Resolution = ADC_RESOLUTION_12B;
  412.   hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  413.   hadc.Init.ScanConvMode = ADC_SCAN_ENABLE;
  414.   hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  415.   hadc.Init.LowPowerAutoWait = ADC_AUTOWAIT_DISABLE;
  416.   hadc.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE;
  417.   hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
  418.   hadc.Init.ContinuousConvMode = DISABLE;
  419.   hadc.Init.NbrOfConversion = 6;
  420.   hadc.Init.DiscontinuousConvMode = DISABLE;
  421.   hadc.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T6_TRGO;
  422.   hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
  423.   hadc.Init.DMAContinuousRequests = DISABLE;
  424.   if (HAL_ADC_Init(&hadc) != HAL_OK)
  425.   {
  426.     Error_Handler();
  427.   }
  428.  
  429.     /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  430.     */
  431.   sConfig.Channel = ADC_CHANNEL_10;
  432.   sConfig.Rank = 1;
  433.   sConfig.SamplingTime = ADC_SAMPLETIME_4CYCLES;
  434.   if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  435.   {
  436.     Error_Handler();
  437.   }
  438.  
  439.     /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  440.     */
  441.   sConfig.Channel = ADC_CHANNEL_11;
  442.   sConfig.Rank = 2;
  443.   if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  444.   {
  445.     Error_Handler();
  446.   }
  447.  
  448.     /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  449.     */
  450.   sConfig.Channel = ADC_CHANNEL_12;
  451.   sConfig.Rank = 3;
  452.   if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  453.   {
  454.     Error_Handler();
  455.   }
  456.  
  457.     /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  458.     */
  459.   sConfig.Channel = ADC_CHANNEL_13;
  460.   sConfig.Rank = 4;
  461.   if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  462.   {
  463.     Error_Handler();
  464.   }
  465.  
  466.     /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  467.     */
  468.   sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
  469.   sConfig.Rank = 5;
  470.   if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  471.   {
  472.     Error_Handler();
  473.   }
  474.  
  475.     /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  476.     */
  477.   sConfig.Channel = ADC_CHANNEL_VREFINT;
  478.   sConfig.Rank = 6;
  479.   if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  480.   {
  481.     Error_Handler();
  482.   }
  483.  
  484. }
  485.  
  486. /* SPI1 init function */
  487. static void MX_SPI1_Init(void)
  488. {
  489.  
  490.   hspi1.Instance = SPI1;
  491.   hspi1.Init.Mode = SPI_MODE_MASTER;
  492.   hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  493.   hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  494.   hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  495.   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  496.   hspi1.Init.NSS = SPI_NSS_SOFT;
  497.   hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  498.   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  499.   hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  500.   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  501.   hspi1.Init.CRCPolynomial = 10;
  502.   if (HAL_SPI_Init(&hspi1) != HAL_OK)
  503.   {
  504.     Error_Handler();
  505.   }
  506.  
  507. }
  508.  
  509. /* TIM2 init function */
  510. static void MX_TIM2_Init(void)
  511. {
  512.  
  513.   TIM_ClockConfigTypeDef sClockSourceConfig;
  514.   TIM_MasterConfigTypeDef sMasterConfig;
  515.   TIM_IC_InitTypeDef sConfigIC;
  516.   TIM_OC_InitTypeDef sConfigOC;
  517.  
  518.   htim2.Instance = TIM2;
  519.   htim2.Init.Prescaler = 320;
  520.   htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  521.   htim2.Init.Period = 0xFFFF;
  522.   htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  523.   if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  524.   {
  525.     Error_Handler();
  526.   }
  527.  
  528.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  529.   if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  530.   {
  531.     Error_Handler();
  532.   }
  533.  
  534.   if (HAL_TIM_IC_Init(&htim2) != HAL_OK)
  535.   {
  536.     Error_Handler();
  537.   }
  538.  
  539.   if (HAL_TIM_OC_Init(&htim2) != HAL_OK)
  540.   {
  541.     Error_Handler();
  542.   }
  543.  
  544.   if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
  545.   {
  546.     Error_Handler();
  547.   }
  548.  
  549.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  550.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  551.   if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  552.   {
  553.     Error_Handler();
  554.   }
  555.  
  556.   sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
  557.   sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
  558.   sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
  559.   sConfigIC.ICFilter = 0;
  560.   if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
  561.   {
  562.     Error_Handler();
  563.   }
  564.  
  565.   if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2) != HAL_OK)
  566.   {
  567.     Error_Handler();
  568.   }
  569.  
  570.   sConfigOC.OCMode = TIM_OCMODE_TIMING;
  571.   sConfigOC.Pulse = 0;
  572.   sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  573.   sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  574.   if (HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  575.   {
  576.     Error_Handler();
  577.   }
  578.  
  579.   sConfigOC.OCMode = TIM_OCMODE_PWM1;
  580.   if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  581.   {
  582.     Error_Handler();
  583.   }
  584.  
  585.   HAL_TIM_MspPostInit(&htim2);
  586.  
  587. }
  588.  
  589. /* TIM6 init function */
  590. static void MX_TIM6_Init(void)
  591. {
  592.  
  593.   TIM_MasterConfigTypeDef sMasterConfig;
  594.  
  595.   htim6.Instance = TIM6;
  596.   htim6.Init.Prescaler = 3200;
  597.   htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  598.   htim6.Init.Period = 1000;
  599.   if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  600.   {
  601.     Error_Handler();
  602.   }
  603.  
  604.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  605.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  606.   if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
  607.   {
  608.     Error_Handler();
  609.   }
  610.  
  611. }
  612.  
  613. /* USART1 init function */
  614. static void MX_USART1_UART_Init(void)
  615. {
  616.  
  617.   huart1.Instance = USART1;
  618.   huart1.Init.BaudRate = 19200;
  619.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  620.   huart1.Init.StopBits = UART_STOPBITS_1;
  621.   huart1.Init.Parity = UART_PARITY_NONE;
  622.   huart1.Init.Mode = UART_MODE_TX_RX;
  623.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  624.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  625.   if (HAL_UART_Init(&huart1) != HAL_OK)
  626.   {
  627.     Error_Handler();
  628.   }
  629.  
  630. }
  631.  
  632. /* USART2 init function */
  633. static void MX_USART2_UART_Init(void)
  634. {
  635.  
  636.   huart2.Instance = USART2;
  637.   huart2.Init.BaudRate = 115200;
  638.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  639.   huart2.Init.StopBits = UART_STOPBITS_1;
  640.   huart2.Init.Parity = UART_PARITY_NONE;
  641.   huart2.Init.Mode = UART_MODE_TX_RX;
  642.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  643.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  644.   if (HAL_UART_Init(&huart2) != HAL_OK)
  645.   {
  646.     Error_Handler();
  647.   }
  648.  
  649. }
  650.  
  651. /**
  652.   * Enable DMA controller clock
  653.   */
  654. static void MX_DMA_Init(void)
  655. {
  656.   /* DMA controller clock enable */
  657.   __HAL_RCC_DMA1_CLK_ENABLE();
  658.  
  659.   /* DMA interrupt init */
  660.   /* DMA1_Channel1_IRQn interrupt configuration */
  661.   HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  662.   HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  663.  
  664. }
  665.  
  666. /** Configure pins as
  667.         * Analog
  668.         * Input
  669.         * Output
  670.         * EVENT_OUT
  671.         * EXTI
  672.         * Free pins are configured automatically as Analog (this feature is enabled through
  673.         * the Code Generation settings)
  674. */
  675. static void MX_GPIO_Init(void)
  676. {
  677.  
  678.   GPIO_InitTypeDef GPIO_InitStruct;
  679.  
  680.   /* GPIO Ports Clock Enable */
  681.   __HAL_RCC_GPIOC_CLK_ENABLE();
  682.   __HAL_RCC_GPIOH_CLK_ENABLE();
  683.   __HAL_RCC_GPIOA_CLK_ENABLE();
  684.   __HAL_RCC_GPIOB_CLK_ENABLE();
  685.   __HAL_RCC_GPIOD_CLK_ENABLE();
  686.  
  687.   /*Configure GPIO pins : PC13 PC14 PC15 PC6
  688.                            PC7 PC8 PC9 PC10
  689.                            PC11 PC12 */
  690.   GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_6
  691.                           |GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10
  692.                           |GPIO_PIN_11|GPIO_PIN_12;
  693.   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  694.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  695.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  696.  
  697.   /*Configure GPIO pins : PH0 PH1 */
  698.   GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
  699.   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  700.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  701.   HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
  702.  
  703.   /*Configure GPIO pins : PA0 PA1 PA8 PA11
  704.                            PA12 */
  705.   GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_8|GPIO_PIN_11
  706.                           |GPIO_PIN_12;
  707.   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  708.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  709.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  710.  
  711.   /*Configure GPIO pin : LED_Blink_Pin */
  712.   GPIO_InitStruct.Pin = LED_Blink_Pin;
  713.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  714.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  715.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  716.   HAL_GPIO_Init(LED_Blink_GPIO_Port, &GPIO_InitStruct);
  717.  
  718.   /*Configure GPIO pins : SPI_NSS1_Pin SPI1CD_Pin */
  719.   GPIO_InitStruct.Pin = SPI_NSS1_Pin|SPI1CD_Pin;
  720.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  721.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  722.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  723.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  724.  
  725.   /*Configure GPIO pins : SPI_RESET_Pin SPI_NS_Temp_Pin */
  726.   GPIO_InitStruct.Pin = SPI_RESET_Pin|SPI_NS_Temp_Pin;
  727.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  728.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  729.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  730.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  731.  
  732.   /*Configure GPIO pins : PB2 PB12 PB13 PB14
  733.                            PB15 PB4 PB5 PB6
  734.                            PB7 PB8 PB9 */
  735.   GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
  736.                           |GPIO_PIN_15|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6
  737.                           |GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
  738.   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  739.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  740.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  741.  
  742.   /*Configure GPIO pin : PD2 */
  743.   GPIO_InitStruct.Pin = GPIO_PIN_2;
  744.   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  745.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  746.   HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  747.  
  748.   /*Configure GPIO pin Output Level */
  749.   HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin, GPIO_PIN_RESET);
  750.  
  751.   /*Configure GPIO pin Output Level */
  752.   HAL_GPIO_WritePin(SPI_NSS1_GPIO_Port, SPI_NSS1_Pin, GPIO_PIN_SET);
  753.  
  754.   /*Configure GPIO pin Output Level */
  755.   HAL_GPIO_WritePin(SPI1CD_GPIO_Port, SPI1CD_Pin, GPIO_PIN_RESET);
  756.  
  757.   /*Configure GPIO pin Output Level */
  758.   HAL_GPIO_WritePin(SPI_RESET_GPIO_Port, SPI_RESET_Pin, GPIO_PIN_RESET);
  759.  
  760.   /*Configure GPIO pin Output Level */
  761.   HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_SET);
  762.  
  763. }
  764.  
  765. /* USER CODE BEGIN 4 */
  766.  
  767. /* USER CODE END 4 */
  768.  
  769. /**
  770.   * @brief  This function is executed in case of error occurrence.
  771.   * @param  None
  772.   * @retval None
  773.   */
  774. void Error_Handler(void)
  775. {
  776.   /* USER CODE BEGIN Error_Handler */
  777.         /* User can add his own implementation to report the HAL error return state */
  778.         while (1) {
  779.         }
  780.   /* USER CODE END Error_Handler */
  781. }
  782.  
  783. #ifdef USE_FULL_ASSERT
  784.  
  785. /**
  786.    * @brief Reports the name of the source file and the source line number
  787.    * where the assert_param error has occurred.
  788.    * @param file: pointer to the source file name
  789.    * @param line: assert_param error line source number
  790.    * @retval None
  791.    */
  792. void assert_failed(uint8_t* file, uint32_t line)
  793. {
  794.   /* USER CODE BEGIN 6 */
  795.         /* User can add his own implementation to report the file name and line number,
  796.          ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  797.   /* USER CODE END 6 */
  798.  
  799. }
  800.  
  801. #endif
  802.  
  803. /**
  804.   * @}
  805.   */
  806.  
  807. /**
  808.   * @}
  809. */
  810.  
  811. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  812.