1 /*
2  * Copyright 2021 MindMotion Microelectronics Co., Ltd.
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef __HAL_TIM_BASIC_H__
9 #define __HAL_TIM_BASIC_H__
10 
11 #include "hal_common.h"
12 
13 /*!
14  * @addtogroup TIM_BASIC
15  * @{
16  */
17 
18 /*!
19  * @brief TIM_BASIC driver version number.
20  */
21 #define TIM_BASIC_DRIVER_VERSION 0u /*!< TIM_BASIC_0. */
22 
23 /*!
24  * @addtogroup TIM_BASIC_INT
25  * @brief Enable interrupt.
26  * @{
27  */
28 #define TIM_BASIC_INT_UPDATE_PERIOD           (1u << 0u) /*!< Timer update period interrupt switchers. */
29 /*!
30  * @}
31  */
32 
33 /*!
34  * @addtogroup TIM_BASIC_DMA
35  * @brief Enable DMA.
36  * @{
37  */
38 #define TIM_BASIC_DMA_UPDATE_PERIOD           (1u << 8u) /*!< Timer update period interrupt switchers. */
39 /*!
40  * @}
41  */
42 
43 /*!
44  * @addtogroup TIM_BASIC_INT_STATUS
45  * @brief Check interrupt status.
46  * @{
47  */
48 #define TIM_BASIC_STATUS_UPDATE_PERIOD            (1u << 0u) /*!< Timer update period interrupt flag. */
49 /*!
50  * @}
51  */
52 
53 /*!
54  * @addtogroup TIM_BASIC_SWTRG
55  * @brief Events to be triggered by software.
56  * @{
57  */
58 #define TIM_BASIC_SWTRG_UPDATE_PERIOD            (1u << 0u) /*!< Timer update period trigger. */
59 /*!
60  * @}
61  */
62 
63 /*!
64  * @brief Counter period working mode.
65  */
66 typedef enum
67 {
68     TIM_BASIC_PeriodMode_Continuous = 0u, /*!< The counter would count from zero to indicated period value then generate an update circularly. */
69     TIM_BASIC_PeriodMode_OneTimeRun = 1u, /*!< The counter would count from zero to indicated period value then generate an update then stop. */
70 } TIM_BASIC_PeriodMode_Type;
71 
72 /*!
73  * @brief This type of structure instance is used to keep the settings when calling the @ref TIM_BASIC_Init() to initialize the TIM module time base unit.
74  */
75 typedef struct
76 {
77     uint32_t ClockFreqHz;                   /*!< Frequence of clock source for counter. */
78     uint32_t StepFreqHz;                    /*!< Step length value. Counter's StepFreqHz = ClockSourceFreqHz / (ClockSourceDiv+1). */
79     uint32_t Period;                        /*!< Counter counting period length, from 0 to Period. */
80     bool EnablePreloadPeriod;               /*!< Enable the preload of period value. If enable, it will be updated in next period, otherwise immediately. */
81     TIM_BASIC_PeriodMode_Type PeriodMode;   /*!< Counting working mode. */
82 } TIM_BASIC_Init_Type;
83 
84 /*!
85  * @brief Set the timer's step for indicated TIM module.
86  *
87  * @param TIMx TIM_BASIC instance.
88  * @param init Pointer to the initialization structure. See to @ref TIM_BASIC_Init_Type.
89  * @return 'true' to set the timer's step successfully, 'false' to fail because the invalid StepFreqHz.
90  */
91 bool TIM_BASIC_Init(TIM_BASIC_Type * TIMx, TIM_BASIC_Init_Type * init);
92 
93 /*!
94  * @brief Start counting.
95  *
96  * @param TIMx TIM_BASIC instance.
97  * @return None.
98  */
99 void TIM_BASIC_Start(TIM_BASIC_Type * TIMx);
100 
101 /*!
102  * @brief Stop counting immediately.
103  *
104  * @param TIMx TIM_BASIC instance.
105  * @return None.
106  */
107 void TIM_BASIC_Stop(TIM_BASIC_Type * TIMx);
108 
109 /*!
110  * @brief Get the indicated TIMx Counter value.
111  *
112  * @param TIMx TIM_BASIC_Type instance.
113  * @return The current value of the counter register.
114  */
115 uint32_t TIM_BASIC_GetCounterValue(TIM_BASIC_Type * TIMx);
116 
117 /*!
118  * @brief Enable the TIM_BASIC interrupts.
119  *
120  * @param TIMx TIM_BASIC_Type instance.
121  * @param interrupts Interrupts to be enabled.See to @ref TIM_BASIC_INT.
122  * @param enable 'true' to enable the indicated interrupts or DMAs, 'false' to disable the indicated interrupts or DMAs.
123  * @return None.
124  */
125 void TIM_BASIC_EnableInterrupts(TIM_BASIC_Type *TIMx, uint32_t interrupts, bool enable);
126 
127 /*!
128  * @brief Enable the TIM_BASIC DMAs.
129  *
130  * @param TIMx TIM_BASIC_Type instance.
131  * @param dmas DMA masks to be enabled. See to @ref TIM_BASIC_DMA.
132  * @param enable 'true' to enable the indicated interrupts or DMAs, 'false' to disable the indicated interrupts or DMAs.
133  * @return None.
134  */
135 void TIM_BASIC_EnableDMA(TIM_BASIC_Type *TIMx, uint32_t dmas, bool enable);
136 
137 /*!
138  * @brief Software trigger events to call interrupts or DMAs.
139  *
140  * Can simultaneously trigger multiple events.
141  *
142  * @param TIMx TIM_BASIC instance. See to @ref TIM_BASIC_SWTRG.
143  * @param swtrgs Trigger the events to generate interrupts.
144  */
145 void TIM_BASIC_DoSwTrigger(TIM_BASIC_Type *TIMx, uint32_t swtrgs);
146 
147 /*!
148  * @brief Get the status of the indicated timer.
149  *
150  * @param TIMx TIM_BASIC_Type instance.
151  * @return Interrupt status flags. See to @ref TIM_BASIC_INT_STATUS.
152  */
153 uint32_t TIM_BASIC_GetInterruptStatus(TIM_BASIC_Type * TIMx);
154 
155 /*!
156  * @brief Clear the status of indicated interrupt.
157  *
158  * @param TIMx TIM_BASIC_Type instance.
159  * @param status The required clear flag type. See to @ref TIM_BASIC_INT_STATUS.
160  * @return None.
161  */
162 void TIM_BASIC_ClearInterruptStatus(TIM_BASIC_Type *TIMx, uint32_t status);
163 
164 /*!
165  *@}
166  */
167 #endif /* __HAL_TIM_BASIC_H__ */
168 
169