1 |
577 |
jeremybenn |
/******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
|
2 |
|
|
* File Name : tim.h
|
3 |
|
|
* Author : MCD Application Team
|
4 |
|
|
* Date First Issued : 08/09/2003
|
5 |
|
|
* Description : This file contains all the functions prototypes for the
|
6 |
|
|
* TIM software library.
|
7 |
|
|
********************************************************************************
|
8 |
|
|
* History:
|
9 |
|
|
* 30/11/2004 : V2.0
|
10 |
|
|
* 14/07/2004 : V1.3
|
11 |
|
|
* 01/01/2004 : V1.2
|
12 |
|
|
*******************************************************************************
|
13 |
|
|
THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
|
14 |
|
|
CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
|
15 |
|
|
AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT
|
16 |
|
|
OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
|
17 |
|
|
OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
|
18 |
|
|
CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
|
19 |
|
|
*******************************************************************************/
|
20 |
|
|
|
21 |
|
|
#ifndef __TIM_H
|
22 |
|
|
#define __TIM_H
|
23 |
|
|
|
24 |
|
|
#include "71x_map.h"
|
25 |
|
|
|
26 |
|
|
typedef enum { TIM_EXTERNAL,
|
27 |
|
|
TIM_INTERNAL
|
28 |
|
|
} TIM_Clocks;
|
29 |
|
|
|
30 |
|
|
typedef enum { TIM_RISING,
|
31 |
|
|
TIM_FALLING
|
32 |
|
|
} TIM_Clock_Edges;
|
33 |
|
|
|
34 |
|
|
typedef enum { TIM_CHANNEL_A,
|
35 |
|
|
TIM_CHANNEL_B
|
36 |
|
|
} TIM_Channels;
|
37 |
|
|
|
38 |
|
|
typedef enum { TIM_WITH_IT,
|
39 |
|
|
TIM_WITHOUT_IT
|
40 |
|
|
} TIM_IT_Mode;
|
41 |
|
|
|
42 |
|
|
typedef enum { TIM_TIMING,
|
43 |
|
|
TIM_WAVE
|
44 |
|
|
} TIM_OC_Modes;
|
45 |
|
|
|
46 |
|
|
typedef enum { TIM_HIGH,
|
47 |
|
|
TIM_LOW
|
48 |
|
|
} TIM_Logic_Levels;
|
49 |
|
|
|
50 |
|
|
typedef enum { TIM_START,
|
51 |
|
|
TIM_STOP,
|
52 |
|
|
TIM_CLEAR
|
53 |
|
|
} TIM_CounterOperations;
|
54 |
|
|
|
55 |
|
|
typedef enum { TIM_ICFA = 0x8000,
|
56 |
|
|
TIM_OCFA = 0x4000,
|
57 |
|
|
TIM_TOF = 0x2000,
|
58 |
|
|
TIM_ICFB = 0x1000,
|
59 |
|
|
TIM_OCFB = 0x0800
|
60 |
|
|
} TIM_Flags;
|
61 |
|
|
|
62 |
|
|
typedef struct { u16 Pulse;
|
63 |
|
|
u16 Period;
|
64 |
|
|
} PWMI_parameters;
|
65 |
|
|
|
66 |
|
|
#define TIM_ECKEN_Mask 0x0001
|
67 |
|
|
#define TIM_EXEDG_Mask 0x0002
|
68 |
|
|
|
69 |
|
|
#define TIM_IEDGA_Mask 0x0004
|
70 |
|
|
#define TIM_IEDGB_Mask 0x0008
|
71 |
|
|
|
72 |
|
|
#define TIM_PWM_Mask 0x0010
|
73 |
|
|
|
74 |
|
|
#define TIM_OMP_Mask 0x0020
|
75 |
|
|
|
76 |
|
|
#define TIM_OCAE_Mask 0x0040
|
77 |
|
|
#define TIM_OCBE_Mask 0x0080
|
78 |
|
|
|
79 |
|
|
#define TIM_OLVLA_Mask 0x0100
|
80 |
|
|
#define TIM_OLVLB_Mask 0x0200
|
81 |
|
|
|
82 |
|
|
#define TIM_FOLVA_Mask 0x0400
|
83 |
|
|
#define TIM_FOLVB_Mask 0x0800
|
84 |
|
|
|
85 |
|
|
#define TIM_PWMI_Mask 0x4000
|
86 |
|
|
|
87 |
|
|
#define TIM_EN_Mask 0x8000
|
88 |
|
|
|
89 |
|
|
#define TIM_OCBIE_mask 0x0800
|
90 |
|
|
#define TIM_ICBIE_Mask 0x1000
|
91 |
|
|
#define TIM_TOE_Mask 0x2000
|
92 |
|
|
#define TIM_ICAIE_Mask 0x8000
|
93 |
|
|
#define TIM_OCAIE_mask 0x4000
|
94 |
|
|
|
95 |
|
|
#define TIM_ICA_IT 0x8000 // Input Capture Channel A
|
96 |
|
|
#define TIM_OCA_IT 0x4000 // Output Compare Channel A
|
97 |
|
|
#define TIM_TO_IT 0x2000 // Timer OverFlow
|
98 |
|
|
#define TIM_ICB_IT 0x1000 // Input Capture Channel B
|
99 |
|
|
#define TIM_OCB_IT 0x0800 // Input Capture Channel A
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
/*******************************************************************************
|
103 |
|
|
* Function Name : TIM_Init
|
104 |
|
|
* Description : This routine is used to Initialize the TIM peripheral
|
105 |
|
|
* Input : TIM Timer to Initialize
|
106 |
|
|
* Return : None
|
107 |
|
|
*******************************************************************************/
|
108 |
|
|
void TIM_Init( TIM_TypeDef *TIMx );
|
109 |
|
|
|
110 |
|
|
/*******************************************************************************
|
111 |
|
|
* Function Name : TIM_ClockSourceConfig
|
112 |
|
|
* Description : This routine is used to configure the TIM clock source
|
113 |
|
|
* Input : (1) TIM Timer
|
114 |
|
|
* : (2) TIM_Clocks : Specifies the TIM source clock
|
115 |
|
|
* - TIM_INTERNAL : The TIM is clocked by the APB2 frequency
|
116 |
|
|
* divided by the prescaler value.
|
117 |
|
|
* - TIM_EXTERNAL : The TIM is clocked by an external Clock
|
118 |
|
|
* Return : None
|
119 |
|
|
*******************************************************************************/
|
120 |
|
|
inline void TIM_ClockSourceConfig ( TIM_TypeDef *TIMx, TIM_Clocks Xclock )
|
121 |
|
|
{
|
122 |
|
|
if (Xclock==TIM_EXTERNAL) TIMx->CR1|=TIM_ECKEN_Mask; else TIMx->CR1&=~TIM_ECKEN_Mask;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
/*******************************************************************************
|
126 |
|
|
* Function Name : TIM_ClockSourceValue
|
127 |
|
|
* Description : This routine is used to get the TIM clock source
|
128 |
|
|
* Input : TIM Timer
|
129 |
|
|
* Return : TIM_Clocks : Specifies the TIM source clock
|
130 |
|
|
* - TIM_INTERNAL : The TIM is clocked by the APB2 frequency
|
131 |
|
|
* divided by the prescaler value.
|
132 |
|
|
* - TIM_EXTERNAL : The TIM is clocked by an external Clock
|
133 |
|
|
*******************************************************************************/
|
134 |
|
|
inline TIM_Clocks TIM_ClockSourceValue ( TIM_TypeDef *TIMx )
|
135 |
|
|
{
|
136 |
|
|
return ( TIMx->CR1 & TIM_ECKEN_Mask) == 0 ? TIM_INTERNAL : TIM_EXTERNAL;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
/*******************************************************************************
|
140 |
|
|
* Function Name : TIM_PrescalerConfig
|
141 |
|
|
* Description : This routine is used to configure the TIM prescaler value
|
142 |
|
|
* ( using an internal clock )
|
143 |
|
|
* Input : (1) TIM Timer
|
144 |
|
|
* : (2) Prescaler ( u8 )
|
145 |
|
|
* Return : None
|
146 |
|
|
*******************************************************************************/
|
147 |
|
|
inline void TIM_PrescalerConfig ( TIM_TypeDef *TIMx, u8 Xprescaler )
|
148 |
|
|
{
|
149 |
|
|
TIMx->CR2 = ( TIMx->CR2 & 0xFF00 ) | Xprescaler;
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
/*******************************************************************************
|
153 |
|
|
* Function Name : TIM_PrescalerValue
|
154 |
|
|
* Description : This routine is used to get the TIM prescaler value
|
155 |
|
|
* ( when using using an internal clock )
|
156 |
|
|
* Input : TIM Timer
|
157 |
|
|
* Return : Prescaler ( u8 )
|
158 |
|
|
*******************************************************************************/
|
159 |
|
|
inline u8 TIM_PrescalerValue ( TIM_TypeDef *TIMx )
|
160 |
|
|
{
|
161 |
|
|
return TIMx->CR2 & 0x00FF;
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
/*******************************************************************************
|
165 |
|
|
* Function Name : TIM_ClockLevelConfig
|
166 |
|
|
* Description : This routine is used to configure the TIM clock level
|
167 |
|
|
* ( using an external clock )
|
168 |
|
|
* Input : TIM Timer
|
169 |
|
|
* : TIM_Clock_Edges : Specifies the active adge of the external clock
|
170 |
|
|
* - TIM_RISING : The rising edge
|
171 |
|
|
* - TIM_FALLING : The falling edge
|
172 |
|
|
* Return : None
|
173 |
|
|
*******************************************************************************/
|
174 |
|
|
inline void TIM_ClockLevelConfig ( TIM_TypeDef *TIMx, TIM_Clock_Edges Xedge )
|
175 |
|
|
{
|
176 |
|
|
if (Xedge == TIM_RISING) TIMx->CR1 |= TIM_EXEDG_Mask; else TIMx->CR1 &= ~TIM_EXEDG_Mask;
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
/*******************************************************************************
|
180 |
|
|
* Function Name : TIM_ClockLevelValue
|
181 |
|
|
* Description : This routine is used to get the TIM clock level
|
182 |
|
|
* Input : TIM Timer
|
183 |
|
|
* Output : TIM_Clock_Edges : Specifies the active adge of the external clock
|
184 |
|
|
* - TIM_RISING : The rising edge
|
185 |
|
|
* - TIM_FALLING : The falling edge
|
186 |
|
|
*******************************************************************************/
|
187 |
|
|
inline TIM_Clock_Edges TIM_ClockLevelValue ( TIM_TypeDef *TIMx )
|
188 |
|
|
{
|
189 |
|
|
return ( TIMx->CR1 & TIM_EXEDG_Mask ) == 0 ? TIM_FALLING : TIM_RISING;
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
/*******************************************************************************
|
193 |
|
|
* Function Name : TIM_ICAPModeConfig
|
194 |
|
|
* Description : This routine is used to configure the input capture feature
|
195 |
|
|
* Input : (1) TIM Timer
|
196 |
|
|
* : (2) Input Capture Channel ( Channel_A or Channel_B )
|
197 |
|
|
* : (3) Active Edge : Rising edge or Falling edge.
|
198 |
|
|
* Output : None
|
199 |
|
|
*******************************************************************************/
|
200 |
|
|
void TIM_ICAPModeConfig ( TIM_TypeDef *TIMx,
|
201 |
|
|
TIM_Channels Xchannel,
|
202 |
|
|
TIM_Clock_Edges Xedge );
|
203 |
|
|
|
204 |
|
|
/*******************************************************************************
|
205 |
|
|
* Function Name : TIM_ICAPValue
|
206 |
|
|
* Description : This routine is used to get the Input Capture value
|
207 |
|
|
* Input : (1) TIM Timer
|
208 |
|
|
* : (2) Input Capture Channel ( Channel_A or Channel_B )
|
209 |
|
|
* Output : None
|
210 |
|
|
*******************************************************************************/
|
211 |
|
|
inline u16 TIM_ICAPValue ( TIM_TypeDef *TIMx, TIM_Channels Xchannel )
|
212 |
|
|
{
|
213 |
|
|
return Xchannel == TIM_CHANNEL_A ? TIMx->ICAR : TIMx->ICBR;
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
/*******************************************************************************
|
217 |
|
|
* Function Name : TIM_OCMPModeConfig
|
218 |
|
|
* Description : This routine is used to configure the output compare feature
|
219 |
|
|
* Input : (1) TIM Timer
|
220 |
|
|
* : (2) OCMP Channel ( Channel_A or Channel_B )
|
221 |
|
|
* : (3) Pulse Length
|
222 |
|
|
* : (4) OC_Mode : output wave, or only timing.
|
223 |
|
|
* : (5) Level : Rising edge or Falling edge after the ==
|
224 |
|
|
* Output : None
|
225 |
|
|
*******************************************************************************/
|
226 |
|
|
void TIM_OCMPModeConfig ( TIM_TypeDef *TIMx,
|
227 |
|
|
TIM_Channels Xchannel,
|
228 |
|
|
u16 XpulseLength,
|
229 |
|
|
TIM_OC_Modes Xmode,
|
230 |
|
|
TIM_Logic_Levels Xlevel );
|
231 |
|
|
|
232 |
|
|
/*******************************************************************************
|
233 |
|
|
* Function Name : TIM_OPModeConfig
|
234 |
|
|
* Description : This routine is used to configure the one pulse mode
|
235 |
|
|
* Input : (1) TIM Timer
|
236 |
|
|
* : (3) XpulseLength : Length of the pulse
|
237 |
|
|
* : (4) Level1 : Level during the pulse
|
238 |
|
|
* : (5) Level2 : Level after the pulse
|
239 |
|
|
* : (6) Activation Edge : High or Low on ICAP A
|
240 |
|
|
* Output : None
|
241 |
|
|
*******************************************************************************/
|
242 |
|
|
void TIM_OPModeConfig ( TIM_TypeDef *TIMx,
|
243 |
|
|
u16 XpulseLength,
|
244 |
|
|
TIM_Logic_Levels XLevel1,
|
245 |
|
|
TIM_Logic_Levels XLevel2,
|
246 |
|
|
TIM_Clock_Edges Xedge );
|
247 |
|
|
|
248 |
|
|
/*******************************************************************************
|
249 |
|
|
* Function Name : TIM_PWMOModeConfig
|
250 |
|
|
* Description : This routine is used to configure the PWM in output mode
|
251 |
|
|
* Input : (1) TIM Timer
|
252 |
|
|
* : (2) DutyCycle : u16
|
253 |
|
|
* : (3) Level 1 : During the Duty Cycle
|
254 |
|
|
* : (4) Level 2 : During the after the pulse
|
255 |
|
|
* : (5) Full period : u16
|
256 |
|
|
* Output : None
|
257 |
|
|
*******************************************************************************/
|
258 |
|
|
void TIM_PWMOModeConfig ( TIM_TypeDef *TIMx,
|
259 |
|
|
u16 XDutyCycle,
|
260 |
|
|
TIM_Logic_Levels XLevel1,
|
261 |
|
|
u16 XFullperiod,
|
262 |
|
|
TIM_Logic_Levels XLevel2
|
263 |
|
|
);
|
264 |
|
|
|
265 |
|
|
/*******************************************************************************
|
266 |
|
|
* Function Name : TIM_PWMInputConfig
|
267 |
|
|
* Description : This routine is used to configure the PWM in input mode
|
268 |
|
|
* Input : (1) TIM Timer
|
269 |
|
|
* : (2) First Activation Edge
|
270 |
|
|
* Output : None
|
271 |
|
|
*******************************************************************************/
|
272 |
|
|
void TIM_PWMIModeConfig ( TIM_TypeDef *TIMx, TIM_Clock_Edges Xedge );
|
273 |
|
|
|
274 |
|
|
/*******************************************************************************
|
275 |
|
|
* Function Name : TIM_PWMIValue
|
276 |
|
|
* Description : This routine is used to get the PWMI values
|
277 |
|
|
* Input : (1) TIM Timer
|
278 |
|
|
* Output : PWMI_parameters : - u16 Dyty cycle
|
279 |
|
|
- u16 Full period
|
280 |
|
|
*******************************************************************************/
|
281 |
|
|
PWMI_parameters TIM_PWMIValue (TIM_TypeDef *TIMx );
|
282 |
|
|
|
283 |
|
|
/*******************************************************************************
|
284 |
|
|
* Function Name : TIM_CounterConfig
|
285 |
|
|
* Description : This routine is used to start/stop and clear the selected
|
286 |
|
|
* timer counter
|
287 |
|
|
* Input : (1) TIM Timer
|
288 |
|
|
* : (2) TIM_CounterOperations
|
289 |
|
|
TIM_START Enables or resumes the counter
|
290 |
|
|
* TIM_STOP Stops the TIM counter
|
291 |
|
|
* TIM_CLEAR Set the TIM counter value to FFFCh
|
292 |
|
|
* Output : None
|
293 |
|
|
*******************************************************************************/
|
294 |
|
|
void TIM_CounterConfig ( TIM_TypeDef *TIMx, TIM_CounterOperations Xoperation );
|
295 |
|
|
|
296 |
|
|
/*******************************************************************************
|
297 |
|
|
* Function Name : TIM_ITConfig
|
298 |
|
|
* Description : This routine is used to configure the TIM IT
|
299 |
|
|
* Input : (1) TIM Timer
|
300 |
|
|
* : (2) TIM interrupt
|
301 |
|
|
* : (2) ENABLE / DISABLE
|
302 |
|
|
* Output : None
|
303 |
|
|
*******************************************************************************/
|
304 |
|
|
inline void TIM_ITConfig ( TIM_TypeDef *TIMx, u16 New_IT, FunctionalState NewState )
|
305 |
|
|
{
|
306 |
|
|
if (NewState == ENABLE) TIMx->CR2 |= New_IT; else TIMx->CR2 &= ~New_IT;
|
307 |
|
|
}
|
308 |
|
|
|
309 |
|
|
/*******************************************************************************
|
310 |
|
|
* Function Name : TIM_FlagStatus
|
311 |
|
|
* Description : This routine is used to check whether a Flag is Set.
|
312 |
|
|
* Input : (1) TIM Timer
|
313 |
|
|
* : (2) The TIM FLag
|
314 |
|
|
* Output : Flag NewState
|
315 |
|
|
*******************************************************************************/
|
316 |
|
|
inline FlagStatus TIM_FlagStatus ( TIM_TypeDef *TIMx, TIM_Flags Xflag )
|
317 |
|
|
{
|
318 |
|
|
return (TIMx->SR & Xflag) == 0 ? RESET : SET;
|
319 |
|
|
}
|
320 |
|
|
|
321 |
|
|
/*******************************************************************************
|
322 |
|
|
* Function Name : TIM_FlagClear
|
323 |
|
|
* Description : This routine is used to clear Flags.
|
324 |
|
|
* Input : (1) TIM Timer
|
325 |
|
|
* : (2) The TIM FLag
|
326 |
|
|
* Output : None
|
327 |
|
|
*******************************************************************************/
|
328 |
|
|
inline void TIM_FlagClear ( TIM_TypeDef *TIMx, TIM_Flags Xflag )
|
329 |
|
|
{
|
330 |
|
|
TIMx->SR &= ~Xflag;
|
331 |
|
|
}
|
332 |
|
|
|
333 |
|
|
/*******************************************************************************
|
334 |
|
|
* Function Name : TIM_CounterValue
|
335 |
|
|
* Description : This routine returns the timer counter value.
|
336 |
|
|
* Input : TIM Timer
|
337 |
|
|
* Output : The counter value
|
338 |
|
|
*******************************************************************************/
|
339 |
|
|
inline u16 TIM_CounterValue(TIM_TypeDef *TIMx)
|
340 |
|
|
{
|
341 |
|
|
return TIMx->CNTR;
|
342 |
|
|
}
|
343 |
|
|
#endif // __TIM_H
|
344 |
|
|
|
345 |
|
|
/******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/
|