| /**  | 
|   ******************************************************************************  | 
|   * File Name          : TIM.c  | 
|   * Description        : This file provides code for the configuration  | 
|   *                      of the TIM instances.  | 
|   ******************************************************************************  | 
|   * @attention  | 
|   *  | 
|   * <h2><center>© Copyright (c) 2021 STMicroelectronics.  | 
|   * All rights reserved.</center></h2>  | 
|   *  | 
|   * This software component is licensed by ST under Ultimate Liberty license  | 
|   * SLA0044, the "License"; You may not use this file except in compliance with  | 
|   * the License. You may obtain a copy of the License at:  | 
|   *                             www.st.com/SLA0044  | 
|   *  | 
|   ******************************************************************************  | 
|   */  | 
|   | 
| /* Includes ------------------------------------------------------------------*/  | 
| #include "tim.h"  | 
| #include "board_common.h"  | 
| /* USER CODE BEGIN 0 */  | 
|   | 
| /* USER CODE END 0 */  | 
|   | 
| TIM_HandleTypeDef htim2;  | 
|   | 
| /* TIM2 init function */  | 
| void Board_TIM2Init(void)  | 
| {  | 
|     TIM_ClockConfigTypeDef sClockSourceConfig = {0};  | 
|     TIM_MasterConfigTypeDef sMasterConfig = {0};  | 
|   | 
|     htim2.Instance = TIM2;  | 
|     htim2.Init.Prescaler = 72 -1; /* 72MHz */  | 
|     htim2.Init.CounterMode = TIM_COUNTERMODE_UP;  | 
|     htim2.Init.Period = 1000 -1;  | 
|     htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;  | 
|     htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;  | 
|     if (HAL_TIM_Base_Init(&htim2) != HAL_OK)  | 
|     {  | 
|         Error_Handler();  | 
|     }  | 
|     sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;  | 
|     if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)  | 
|     {  | 
|         Error_Handler();  | 
|     }  | 
|     sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;  | 
|     sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;  | 
|     if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)  | 
|     {  | 
|         Error_Handler();  | 
|     }  | 
|   | 
|     while (HAL_TIM_Base_Start_IT(&htim2) != HAL_OK);  | 
|       | 
|     //while (HAL_TIM_Base_Start(&htim2) != HAL_OK);  | 
|       | 
| }  | 
|   | 
| void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)  | 
| {  | 
|   | 
|     if(tim_baseHandle->Instance==TIM2)  | 
|     {  | 
|         /* USER CODE BEGIN TIM2_MspInit 0 */  | 
|   | 
|         /* USER CODE END TIM2_MspInit 0 */  | 
|         /* TIM2 clock enable */  | 
|         __HAL_RCC_TIM2_CLK_ENABLE();  | 
|   | 
|         /* TIM2 interrupt Init */  | 
|         HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);  | 
|         HAL_NVIC_EnableIRQ(TIM2_IRQn);  | 
|     }  | 
| }  | 
|   | 
| void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)  | 
| {  | 
|   | 
|     if(tim_baseHandle->Instance==TIM2)  | 
|     {  | 
|         /* USER CODE BEGIN TIM2_MspDeInit 0 */  | 
|   | 
|         /* USER CODE END TIM2_MspDeInit 0 */  | 
|         /* Peripheral clock disable */  | 
|         __HAL_RCC_TIM2_CLK_DISABLE();  | 
|           | 
|         /* TIM2 interrupt Deinit */  | 
|         HAL_NVIC_DisableIRQ(TIM2_IRQn);  | 
|     }  | 
| }   | 
|   | 
| #define HEARTBEAT_PERIOD 1000//1260  | 
| static void SystemRunLed(void)  | 
| {  | 
|     static uint8_t u8Phase = 0;  | 
|     static uint32_t u32Timeout = 0;  | 
|     static bool bOneTimeFlag = false;  | 
|       | 
|     if (!bOneTimeFlag)  | 
|     {  | 
|         u32Timeout = jiffies+60;  | 
|         bOneTimeFlag = true;  | 
|         Board_TurnLed(LED_RUN, TURN_ON);  | 
|     }  | 
|       | 
|     switch (u8Phase)  | 
|     {  | 
|         case 0:  | 
|             if (Timeout_Happened(u32Timeout)) /* turn on 70ms */  | 
|             {  | 
|                 Board_TurnLed(LED_RUN, TURN_OFF);  | 
|                 u32Timeout = jiffies+(HEARTBEAT_PERIOD/4-60);  | 
|                 u8Phase++;  | 
|             }  | 
|             break;  | 
|         case 1:  | 
|             if (Timeout_Happened(u32Timeout)) /* turn off (HEARTBEAT_PERIOD/4-70)ms */  | 
|             {  | 
|                 Board_TurnLed(LED_RUN, TURN_ON);  | 
|                 u32Timeout = jiffies+60;  | 
|                 u8Phase++;  | 
|             }  | 
|             break;  | 
|         case 2:              | 
|             if (Timeout_Happened(u32Timeout)) /* turn on 70ms */  | 
|             {  | 
|                 Board_TurnLed(LED_RUN, TURN_OFF);  | 
|                 u32Timeout = jiffies+(HEARTBEAT_PERIOD-HEARTBEAT_PERIOD/4-70);  | 
|                 u8Phase++;  | 
|             }  | 
|           | 
|             break;  | 
|         case 3:  | 
|             if (Timeout_Happened(u32Timeout)) /* turn off (HEARTBEAT_PERIOD-HEARTBEAT_PERIOD/4-70)ms */  | 
|             {  | 
|                 Board_TurnLed(LED_RUN, TURN_ON);                  | 
|                 u32Timeout = jiffies+60;  | 
|                 u8Phase = 0;  | 
|             }  | 
|             break;  | 
|         default:  | 
|             break;  | 
|     }  | 
| }  | 
|   | 
| volatile uint32_t jiffies = 0;  | 
| void msTimingDelay_Decrement(void)  | 
| {      | 
|     jiffies++;  /* 1ms tick */  | 
| }  | 
|   | 
| /* TIM2¶¨Ê±Öжϴ¦Àí */  | 
| void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)  | 
| {  | 
|     if (htim->Instance == TIM1)   | 
|     {  | 
|         HAL_IncTick();  | 
|     }  | 
|       | 
|     if (htim->Instance == TIM2)   | 
|     {  | 
|         msTimingDelay_Decrement();  | 
|         ADC_Sample();  | 
|         SystemRunLed();  | 
|     }  | 
| }  | 
|   | 
|   | 
| /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/  |