This repository has been archived on 2025-04-02. You can view files and clone it, but cannot push or open issues or pull requests.
controller-hart/User/system/bsp/tims.h

129 lines
4.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @file tims.h
* @brief Header file for TIMS module.
*
* This file contains the declarations and definitions for the TIMS module.
* TIMS stands for Timer System and provides functionality related to timers.
*
* @author xxx
* @date 2024-01-16 22:23:43
* @copyright Copyright (c) 2024 by xxx, All Rights Reserved.
*/
#ifndef __TIMS_H__
#define __TIMS_H__
#include "lib.h"
/**
Timer overflow time calculation formula
Tout = ((ARR + 1)*(PSC + 1)) / Tclk
Given Tclk as 84MHz, we need Tout to be 200ms or 200000us. Let's assume PSC is 839, substituting it into the formula gives ARR = 19999.
With these calculated values, both ARR and PSC are within the range of 0 to 65535, so we can use this parameter set.
*/
#define ENABLE_TIM_COUNT(TIMx) \
do \
{ \
LL_TIM_EnableCounter(TIMx); \
} while (__LINE__ == -1);
#define RESET_TIM_COUNT(TIMx) \
do \
{ \
LL_TIM_DisableCounter(TIMx); \
LL_TIM_SetCounter(TIMx, 0); \
LL_TIM_EnableCounter(TIMx); \
} while (__LINE__ == -1);
/**
* @brief Enables the specified TIMx.
* @param TIMx TIM instance.
*/
#define ENABLE_TIM(TIMx) \
do \
{ \
LL_TIM_EnableCounter(TIMx); \
LL_TIM_EnableIT_UPDATE(TIMx); \
} while (__LINE__ == -1);
/**
* @brief Checks if the specified TIMx is enabled.
* @param TIMx TIM instance.
* @retval The new state of TIMx (1 or 0).
*/
#define IS_ENABLE_TIM(TIMx) LL_TIM_IsEnabledIT_UPDATE(TIMx)
/**
* @brief Disables the specified TIMx.
* @param TIMx TIM instance.
*/
#define DISABLE_TIM(TIMx) \
do \
{ \
LL_TIM_DisableCounter(TIMx); \
LL_TIM_DisableIT_UPDATE(TIMx); \
} while (__LINE__ == -1);
#define ENABLE_TIM_ARR_RELOAD(TIMx) LL_TIM_EnableARRPreload(TIMx)
#define DISABLE_TIM_ARR_RELOAD(TIMx) LL_TIM_DisableARRPreload(TIMx)
/**
* @brief Checks if the specified TIMx interrupt flag is set.
* @param TIMx TIM instance.
* @retval The new state of the specified TIMx interrupt flag (1 or 0).
*/
#define IS_TIM_IT_FLAG(TIMx) (LL_TIM_IsActiveFlag_UPDATE(TIMx) == 1)
/**
* @brief TIM interrupt handler.
* @param TIMx TIM instance.
*/
#define TIM_IRQ_HANDLER(TIMx) \
do \
{ \
if (LL_TIM_IsActiveFlag_CC1(TIMx) == SET) \
{ \
if (LL_TIM_IsEnabledIT_CC1(TIMx) == SET) \
{ \
LL_TIM_ClearFlag_CC1(TIMx); \
} \
} \
if (LL_TIM_IsActiveFlag_CC2(TIMx) == SET) \
{ \
if (LL_TIM_IsEnabledIT_CC2(TIMx) == SET) \
{ \
LL_TIM_ClearFlag_CC2(TIMx); \
} \
} \
if (LL_TIM_IsActiveFlag_CC3(TIMx) == SET) \
{ \
if (LL_TIM_IsEnabledIT_CC3(TIMx) == SET) \
{ \
LL_TIM_ClearFlag_CC3(TIMx); \
} \
} \
if (LL_TIM_IsActiveFlag_CC4(TIMx) == SET) \
{ \
if (LL_TIM_IsEnabledIT_CC4(TIMx) == SET) \
{ \
LL_TIM_ClearFlag_CC4(TIMx); \
} \
} \
if (LL_TIM_IsActiveFlag_UPDATE(TIMx) == SET) \
{ \
if (LL_TIM_IsEnabledIT_UPDATE(TIMx) == SET) \
{ \
LL_TIM_ClearFlag_UPDATE(TIMx); \
} \
} \
} while (__LINE__ == -1)
/**
* @brief 获取定时器周期时间(单位:毫秒)
*
* 获取指定定时器TIMx的周期时间以秒为单位。
*
* @param TIMx 定时器指针,指向需要查询的定时器
*
* @return 返回定时器周期时间(单位:毫秒)
*/
#define TIM_CYCLE(TIMx) ((LL_TIM_GetAutoReload(TIMx) + 1) * 0.1)
#endif ///< __TIMS_H__