Rev 196 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
196 | killagreg | 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** |
1 | ingob | 2 | * File Name : 91x_tim.c |
3 | * Author : MCD Application Team |
||
196 | killagreg | 4 | * Version : V2.1 |
5 | * Date : 12/22/2008 |
||
6 | * Description : This file provides all the TIM firmware functions. |
||
1 | ingob | 7 | ******************************************************************************** |
8 | * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH |
||
9 | * CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS |
||
10 | * A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT |
||
11 | * OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT |
||
12 | * OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION |
||
13 | * CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
||
14 | *******************************************************************************/ |
||
15 | |||
16 | /* Includes ------------------------------------------------------------------*/ |
||
17 | #include "91x_tim.h" |
||
18 | |||
19 | /* Include of other module interface headers ---------------------------------*/ |
||
20 | /* Local includes ------------------------------------------------------------*/ |
||
21 | /* Private typedef -----------------------------------------------------------*/ |
||
22 | /* Private define ------------------------------------------------------------*/ |
||
23 | |||
24 | /* TIM Bits Masks */ |
||
25 | |||
26 | #define TIM_PWM_MASK 0x0010 |
||
27 | #define TIM_OPM_MASK 0x0020 |
||
28 | #define TIM_OC1_ENABLE_MASK 0x0040 |
||
29 | #define TIM_OC1_DISABLE_MASK 0xFFBF |
||
30 | #define TIM_OC2_ENABLE_MASK 0x0080 |
||
31 | #define TIM_OC2_DISABLE_MASK 0xFF7F |
||
32 | |||
33 | #define TIM_OLVL1_SET_MASK 0x0100 |
||
34 | #define TIM_OLVL1_RESET_MASK 0xFEFF |
||
35 | |||
36 | #define TIM_OLVL2_SET_MASK 0x0200 |
||
37 | #define TIM_OLVL2_RESET_MASK 0xFDFF |
||
38 | |||
39 | #define TIM_ENABLE_MASK 0x8000 |
||
40 | #define TIM_DISABLE_MASK 0x7FFF |
||
41 | |||
42 | #define TIM_DMA_CLEAR_MASK 0xCFFF |
||
43 | |||
44 | /* Private macro -------------------------------------------------------------*/ |
||
45 | /* Private variables ---------------------------------------------------------*/ |
||
46 | /* Private function prototypes -----------------------------------------------*/ |
||
47 | /* Interface functions -------------------------------------------------------*/ |
||
48 | /* Private functions ---------------------------------------------------------*/ |
||
49 | /******************************************************************************* |
||
50 | * Function Name : TIM_DeInit |
||
51 | * Description : Initializes TIM peripheral control and registers to their |
||
52 | * : default reset values. |
||
53 | * Input : TIMx: where x can be from 0 to 3 to select the TIM |
||
54 | * peripheral. |
||
55 | * Output : None |
||
56 | * Return : None |
||
57 | *******************************************************************************/ |
||
58 | void TIM_DeInit(TIM_TypeDef *TIMx) |
||
59 | { |
||
60 | if((TIMx == TIM0)||(TIMx == TIM1)) |
||
61 | { |
||
62 | SCU_APBPeriphReset(__TIM01, DISABLE); /* TIM0 & TIM1 Reset's off */ |
||
63 | } |
||
64 | else |
||
65 | { |
||
66 | SCU_APBPeriphReset(__TIM23, DISABLE); /* TIM2 & TIM3 Reset's off */ |
||
67 | } |
||
68 | |||
69 | /* Set all the TIMx registers to thier default values */ |
||
70 | TIMx->OC1R = 0x8000; |
||
71 | TIMx->OC2R = 0x8000; |
||
72 | TIMx->CR1 = 0x0; |
||
73 | TIMx->CR2 = 0x1; |
||
74 | TIMx->CNTR = 0x1234; |
||
75 | TIMx->SR = 0x0; |
||
76 | } |
||
77 | |||
78 | /******************************************************************************* |
||
79 | * Function Name : TIM_StructInit |
||
80 | * Description : Fills in a TIM_InitTypeDef structure with the reset value of |
||
81 | * each parameter. |
||
82 | * Input : TIM_InitStruct : pointer to a TIM_InitTypeDef structure |
||
83 | which will be initialized. |
||
84 | * Output : None |
||
85 | * Return : None. |
||
86 | *******************************************************************************/ |
||
87 | void TIM_StructInit(TIM_InitTypeDef *TIM_InitStruct) |
||
88 | { |
||
89 | TIM_InitStruct->TIM_Mode = 0x0000; |
||
90 | TIM_InitStruct->TIM_OC1_Modes = 0x0000; |
||
91 | TIM_InitStruct->TIM_OC2_Modes = 0x0000; |
||
92 | TIM_InitStruct->TIM_Clock_Source = 0x0000; |
||
93 | TIM_InitStruct->TIM_Clock_Edge = 0x0000; |
||
94 | TIM_InitStruct->TIM_OPM_INPUT_Edge = 0x0000; |
||
95 | TIM_InitStruct->TIM_ICAP1_Edge = 0x0000; |
||
96 | TIM_InitStruct->TIM_ICAP2_Edge = 0x0000; |
||
97 | TIM_InitStruct->TIM_Prescaler = 0x0000; |
||
98 | TIM_InitStruct->TIM_Pulse_Level_1 = 0x0000; |
||
99 | TIM_InitStruct->TIM_Pulse_Level_2 = 0x0000; |
||
100 | TIM_InitStruct->TIM_Period_Level = 0x0000; |
||
101 | TIM_InitStruct->TIM_Pulse_Length_1 = 0x0000; |
||
102 | TIM_InitStruct->TIM_Pulse_Length_2 = 0x0000; |
||
103 | TIM_InitStruct->TIM_Full_Period = 0x0000; |
||
104 | } |
||
105 | |||
106 | /******************************************************************************* |
||
107 | * Function Name : TIM_Init |
||
108 | * Description : Initializes TIM peripheral according to the specified |
||
109 | * parameters in the TIM_InitTypeDef structure. |
||
110 | * Input1 : TIMx: where x can be from 0 to 3 to select the TIM |
||
111 | * peripheral. |
||
112 | * Input2 : TIM_InitStruct: pointer to a TIM_InitTypeDef structure that |
||
113 | * contains the configuration information for the specified |
||
114 | * TIM peripheral. |
||
115 | * Output : None |
||
116 | * Return : None |
||
117 | *******************************************************************************/ |
||
118 | |||
119 | void TIM_Init(TIM_TypeDef *TIMx, TIM_InitTypeDef *TIM_InitStruct) |
||
120 | { |
||
121 | /***************************** Clock configuration ****************************/ |
||
122 | |||
123 | if (TIM_InitStruct->TIM_Clock_Source == TIM_CLK_APB) |
||
124 | { |
||
125 | /* APB clock */ |
||
126 | TIMx->CR1 &= TIM_CLK_APB; |
||
127 | } |
||
128 | else |
||
129 | { |
||
196 | killagreg | 130 | /* External clock */ |
1 | ingob | 131 | TIMx->CR1 |= TIM_CLK_EXTERNAL; |
132 | if (TIM_InitStruct->TIM_Clock_Edge == TIM_CLK_EDGE_RISING) |
||
133 | { |
||
134 | /* Clock rising edge */ |
||
135 | TIMx->CR1 |= TIM_CLK_EDGE_RISING; |
||
136 | } |
||
137 | else |
||
138 | { |
||
139 | /* Clock falling edge */ |
||
140 | TIMx->CR1 &= TIM_CLK_EDGE_FALLING; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /************************** Prescaler configuration ***************************/ |
||
145 | |||
146 | TIMx->CR2 =( TIMx->CR2 & 0xFF00 )|TIM_InitStruct->TIM_Prescaler ; |
||
147 | |||
148 | /********************************** TIM Modes *********************************/ |
||
149 | |||
150 | switch ( TIM_InitStruct->TIM_Mode) |
||
151 | { |
||
152 | /******************************* PWM Input mode *******************************/ |
||
153 | |||
154 | case TIM_PWMI: |
||
155 | |||
156 | /* Set the PWMI Bit */ |
||
157 | TIMx->CR1 |= TIM_PWMI; |
||
158 | |||
159 | /* Set the first edge Level */ |
||
160 | if ( TIM_InitStruct->TIM_ICAP1_Edge == TIM_ICAP1_EDGE_RISING) |
||
161 | { |
||
162 | TIMx->CR1 |= TIM_ICAP1_EDGE_RISING; |
||
163 | } |
||
164 | else |
||
165 | { |
||
166 | TIMx->CR1 &= TIM_ICAP1_EDGE_FALLING; |
||
167 | } |
||
168 | |||
169 | /* Set the Second edge Level ( Opposite of the first level ) */ |
||
170 | if ( TIM_InitStruct->TIM_ICAP1_Edge == TIM_ICAP1_EDGE_RISING) |
||
171 | { |
||
172 | TIMx->CR1 &= TIM_ICAP2_EDGE_FALLING; |
||
173 | } |
||
174 | else |
||
175 | { |
||
176 | TIMx->CR1 |= TIM_ICAP2_EDGE_RISING; |
||
177 | } |
||
178 | |||
179 | break; |
||
180 | |||
181 | /************************** Output compare channel 1 **************************/ |
||
182 | |||
183 | case TIM_OCM_CHANNEL_1: |
||
184 | |||
185 | if (TIM_InitStruct->TIM_Pulse_Level_1 == TIM_HIGH) |
||
186 | { |
||
187 | TIMx->CR1 |= TIM_OLVL1_SET_MASK; |
||
188 | } |
||
189 | else |
||
190 | { |
||
191 | TIMx->CR1 &= TIM_OLVL1_RESET_MASK; |
||
192 | } |
||
193 | |||
194 | TIMx->OC1R = TIM_InitStruct->TIM_Pulse_Length_1; |
||
195 | |||
196 | if (TIM_InitStruct->TIM_OC1_Modes == TIM_TIMING) |
||
197 | { |
||
198 | TIMx->CR1 &= TIM_OC1_DISABLE_MASK; |
||
199 | } |
||
200 | else |
||
201 | { |
||
202 | TIMx->CR1 |= TIM_OC1_ENABLE_MASK; |
||
203 | } |
||
204 | |||
205 | break; |
||
206 | |||
207 | /************************** Output compare channel 2 **************************/ |
||
208 | |||
209 | case TIM_OCM_CHANNEL_2: |
||
210 | |||
211 | if (TIM_InitStruct->TIM_Pulse_Level_2 == TIM_HIGH) |
||
212 | { |
||
213 | TIMx->CR1 |= TIM_OLVL2_SET_MASK; |
||
214 | } |
||
215 | else |
||
216 | { |
||
217 | TIMx->CR1 &= TIM_OLVL2_RESET_MASK; |
||
218 | } |
||
219 | |||
220 | TIMx->OC2R = TIM_InitStruct->TIM_Pulse_Length_2; |
||
221 | |||
222 | if (TIM_InitStruct->TIM_OC2_Modes == TIM_TIMING) |
||
223 | { |
||
224 | TIMx->CR1 &= TIM_OC2_DISABLE_MASK; |
||
225 | } |
||
226 | else |
||
227 | { |
||
228 | TIMx->CR1 |= TIM_OC2_ENABLE_MASK; |
||
229 | } |
||
230 | |||
231 | break; |
||
232 | |||
233 | /************************ Output compare channel 1 & 2 ************************/ |
||
234 | |||
235 | case TIM_OCM_CHANNEL_12: |
||
236 | |||
237 | TIMx->OC2R = TIM_InitStruct->TIM_Pulse_Length_2; |
||
238 | TIMx->OC1R = TIM_InitStruct->TIM_Pulse_Length_1; |
||
239 | |||
240 | if (TIM_InitStruct->TIM_OC2_Modes == TIM_TIMING) |
||
241 | { |
||
242 | TIMx->CR1 &= TIM_OC2_DISABLE_MASK; |
||
243 | } |
||
244 | else |
||
245 | { |
||
246 | TIMx->CR1 |= TIM_OC2_ENABLE_MASK; |
||
247 | } |
||
248 | |||
249 | if (TIM_InitStruct->TIM_OC1_Modes == TIM_TIMING) |
||
250 | { |
||
251 | TIMx->CR1 &= TIM_OC1_DISABLE_MASK; |
||
252 | } |
||
253 | else |
||
254 | { |
||
255 | TIMx->CR1 |= TIM_OC1_ENABLE_MASK; |
||
256 | } |
||
257 | |||
258 | if (TIM_InitStruct->TIM_Pulse_Level_1 == TIM_HIGH) |
||
259 | { |
||
260 | TIMx->CR1 |= TIM_OLVL1_SET_MASK; |
||
261 | } |
||
262 | else |
||
263 | { |
||
264 | TIMx->CR1 &= TIM_OLVL1_RESET_MASK; |
||
265 | } |
||
266 | |||
267 | if (TIM_InitStruct->TIM_Pulse_Level_2 == TIM_HIGH) |
||
268 | { |
||
269 | TIMx->CR1 |= TIM_OLVL2_SET_MASK; |
||
270 | } |
||
271 | else |
||
272 | { |
||
273 | TIMx->CR1 &= TIM_OLVL2_RESET_MASK; |
||
274 | } |
||
275 | |||
276 | break; |
||
277 | |||
278 | /********************************** PWM mode **********************************/ |
||
279 | |||
280 | case TIM_PWM: |
||
281 | |||
282 | /* Set the Level During the pulse */ |
||
283 | if ( TIM_InitStruct->TIM_Pulse_Level_1 == TIM_HIGH) |
||
284 | { |
||
285 | TIMx->CR1 |= TIM_OLVL2_SET_MASK; |
||
286 | } |
||
287 | else |
||
288 | { |
||
289 | TIMx->CR1 &= TIM_OLVL2_RESET_MASK; |
||
290 | } |
||
291 | |||
292 | /* Set the Level after the pulse */ |
||
293 | if (TIM_InitStruct->TIM_Period_Level == TIM_HIGH) |
||
294 | { |
||
295 | TIMx->CR1 |= TIM_OLVL1_SET_MASK; |
||
296 | } |
||
297 | else |
||
298 | { |
||
299 | TIMx->CR1 &= TIM_OLVL1_RESET_MASK; |
||
300 | } |
||
301 | |||
302 | /* Set the OCAE */ |
||
303 | TIMx->CR1 |= TIM_OC1_ENABLE_MASK; |
||
304 | |||
305 | /* Set the PWM Bit */ |
||
306 | TIMx->CR1 |= TIM_PWM_MASK; |
||
307 | |||
308 | /* Set the Duty Cycle value */ |
||
309 | |||
310 | TIMx->OC1R = TIM_InitStruct->TIM_Pulse_Length_1 ; |
||
311 | |||
312 | /* Set the Full Period */ |
||
313 | |||
314 | TIMx->OC2R = TIM_InitStruct->TIM_Full_Period ; |
||
315 | |||
316 | break; |
||
317 | |||
318 | /******************************* One pulse mode *******************************/ |
||
319 | |||
320 | case TIM_OPM: |
||
321 | |||
322 | /* Set the Level During the pulse */ |
||
323 | if (TIM_InitStruct->TIM_Pulse_Level_1 == TIM_HIGH) |
||
324 | { |
||
325 | TIMx->CR1 |= TIM_OLVL2_SET_MASK; |
||
326 | } |
||
327 | |||
328 | /* Set the Level after the pulse */ |
||
329 | if (TIM_InitStruct->TIM_Period_Level == TIM_HIGH) |
||
330 | { |
||
331 | TIMx->CR1 |= TIM_OLVL1_SET_MASK; |
||
332 | } |
||
333 | |||
334 | /* Set the Activation Edge on the ICAP 1 */ |
||
335 | if (TIM_InitStruct->TIM_OPM_INPUT_Edge == TIM_OPM_EDGE_RISING) |
||
336 | { |
||
337 | TIMx->CR1 |= TIM_OPM_EDGE_RISING; |
||
338 | } |
||
339 | |||
340 | /* Set the Output Compare Function */ |
||
341 | TIMx->CR1 |= TIM_OC1_ENABLE_MASK; |
||
342 | |||
343 | /* Set the One pulse mode */ |
||
344 | TIMx->CR1 |= TIM_OPM_MASK; |
||
345 | |||
346 | /* Set the Pulse length */ |
||
347 | TIMx->OC1R = TIM_InitStruct->TIM_Pulse_Length_1; |
||
348 | |||
349 | break; |
||
350 | |||
351 | /*************************** Input capture channel 1 **************************/ |
||
352 | |||
353 | case TIM_ICAP_CHANNEL_1: |
||
354 | |||
355 | if (TIM_InitStruct->TIM_ICAP1_Edge == TIM_ICAP1_EDGE_RISING) |
||
356 | { |
||
357 | TIMx->CR1 |= TIM_ICAP1_EDGE_RISING; |
||
358 | } |
||
359 | else |
||
360 | { |
||
361 | TIMx->CR1 &= TIM_ICAP1_EDGE_FALLING; |
||
362 | } |
||
363 | |||
364 | break; |
||
365 | |||
366 | /*************************** Input capture channel 2 **************************/ |
||
367 | |||
368 | case TIM_ICAP_CHANNEL_2: |
||
369 | |||
370 | if (TIM_InitStruct->TIM_ICAP2_Edge == TIM_ICAP2_EDGE_RISING) |
||
371 | { |
||
372 | TIMx->CR1 |= TIM_ICAP2_EDGE_RISING; |
||
373 | } |
||
374 | else |
||
375 | { |
||
376 | TIMx->CR1 &= TIM_ICAP2_EDGE_FALLING; |
||
377 | } |
||
378 | |||
379 | break; |
||
380 | |||
381 | /************************* Input capture channel 1 & 2 ************************/ |
||
382 | |||
383 | case TIM_ICAP_CHANNEL_12: |
||
384 | if (TIM_InitStruct->TIM_ICAP2_Edge == TIM_ICAP2_EDGE_RISING) |
||
385 | { |
||
386 | TIMx->CR1 |= TIM_ICAP2_EDGE_RISING; |
||
387 | } |
||
388 | else |
||
389 | { |
||
390 | TIMx->CR1 &= TIM_ICAP2_EDGE_FALLING; |
||
391 | } |
||
392 | |||
393 | if (TIM_InitStruct->TIM_ICAP1_Edge == TIM_ICAP1_EDGE_RISING) |
||
394 | { |
||
395 | TIMx->CR1 |= TIM_ICAP1_EDGE_RISING; |
||
396 | } |
||
397 | else |
||
398 | { |
||
399 | TIMx->CR1 &= TIM_ICAP1_EDGE_FALLING; |
||
400 | } |
||
401 | |||
402 | break; |
||
403 | |||
404 | default: |
||
405 | break; |
||
406 | } |
||
407 | } |
||
408 | |||
409 | /******************************************************************************* |
||
410 | * Function Name : TIM_CounterCmd |
||
411 | * Description : Enables or disables TIMx Counter peripheral. |
||
412 | * Input1 : TIMx: where x can be from 0 to 3 to select the TIM |
||
413 | * peripheral. |
||
414 | * Input2 : TIM_operation: specifies the new state of the TIMx Counter. |
||
415 | * This parameter can be one of the following values: |
||
416 | * - TIM_START: Start the timer counter. |
||
417 | * - TIM_STOP : Stop the timer counter. |
||
418 | * - TIM_CLEAR: Clear the timer counter. |
||
419 | * Output : None |
||
420 | * Return : None |
||
421 | *******************************************************************************/ |
||
422 | void TIM_CounterCmd(TIM_TypeDef *TIMx, TIM_CounterOperations TIM_operation) |
||
423 | { |
||
424 | switch (TIM_operation) |
||
425 | { |
||
426 | case TIM_START: |
||
427 | TIMx->CR1 |= TIM_ENABLE_MASK; |
||
428 | break; |
||
429 | |||
430 | case TIM_STOP: |
||
431 | TIMx->CR1 &= TIM_DISABLE_MASK; |
||
432 | break; |
||
433 | |||
434 | case TIM_CLEAR: |
||
435 | TIMx->CNTR = 0x1234; |
||
436 | break; |
||
437 | |||
438 | default: |
||
439 | break; |
||
440 | } |
||
441 | } |
||
442 | |||
443 | /******************************************************************************* |
||
444 | * Function Name : TIM_PrescalerConfig |
||
445 | * Description : This routine is used to configure the TIMx prescaler value |
||
446 | * (when using the APB clock). |
||
447 | * Input1 : TIMx: where x can be from 0 to 3 to select the TIM |
||
448 | * peripheral. |
||
449 | * Input2 : TIM_Prescaler: specifies the prescaler value. This parameter |
||
450 | * can be a value from 0x0 to 0xFF. |
||
451 | * Output : None |
||
452 | * Return : None |
||
453 | *******************************************************************************/ |
||
454 | void TIM_PrescalerConfig(TIM_TypeDef *TIMx, u8 TIM_Prescaler) |
||
455 | { |
||
456 | TIMx->CR2 &= 0xFF00; |
||
457 | TIMx->CR2 |= TIM_Prescaler; |
||
458 | |||
459 | } |
||
460 | /******************************************************************************* |
||
461 | * Function Name : TIM_GetPrescalerValue |
||
462 | * Description : This routine is used to get the TIMx prescaler value |
||
463 | * (when using the APB clock). |
||
464 | * Input : TIMx: where x can be from 0 to 3 to select the TIM |
||
465 | * peripheral. |
||
466 | * Output : None |
||
467 | * Return : The prescaler value. |
||
468 | *******************************************************************************/ |
||
469 | u8 TIM_GetPrescalerValue(TIM_TypeDef *TIMx) |
||
470 | { |
||
471 | return TIMx->CR2 & 0x00FF; |
||
472 | } |
||
473 | |||
474 | /******************************************************************************* |
||
475 | * Function Name : TIM_GetCounterValue |
||
476 | * Description : This routine is used to get the TIMx counter value. |
||
477 | * Input : TIMx: where x can be from 0 to 3 to select the TIM |
||
478 | * peripheral. |
||
479 | * Output : None |
||
480 | * Return : The counter value. |
||
481 | *******************************************************************************/ |
||
482 | u16 TIM_GetCounterValue(TIM_TypeDef *TIMx) |
||
483 | { |
||
484 | return TIMx->CNTR; |
||
485 | } |
||
486 | |||
487 | /******************************************************************************* |
||
488 | * Function Name : TIM_GetICAP1Value |
||
489 | * Description : This routine is used to get the Input Capture 1 value. |
||
490 | * Input : TIMx: where x can be from 0 to 3 to select the TIM |
||
491 | * peripheral. |
||
492 | * Output : None |
||
493 | * Return : The Input Capture 1 value. |
||
494 | *******************************************************************************/ |
||
495 | u16 TIM_GetICAP1Value(TIM_TypeDef *TIMx) |
||
496 | { |
||
497 | return TIMx->IC1R; |
||
498 | } |
||
499 | |||
500 | /******************************************************************************* |
||
501 | * Function Name : TIM_GetICAP2Value |
||
502 | * Description : This routine is used to get the Input Capture 2 value. |
||
503 | * Input : TIMx: where x can be from 0 to 3 to select the TIM |
||
504 | * peripheral. |
||
505 | * Output : None |
||
506 | * Return : The Input Capture 2 value. |
||
507 | *******************************************************************************/ |
||
508 | u16 TIM_GetICAP2Value(TIM_TypeDef *TIMx) |
||
509 | { |
||
510 | return TIMx->IC2R; |
||
511 | } |
||
512 | |||
513 | /******************************************************************************* |
||
514 | * Function Name : TIM_SetPulse |
||
515 | * Description : This routine is used to set the pulse value. |
||
516 | * Input1 : TIMx: where x can be from 0 to 3 to select the TIM |
||
517 | * peripheral. |
||
518 | * Input2 : TIM_Channel: specifies the needed channel. |
||
519 | * This parameter can be one of the following values: |
||
520 | * - TIM_PWM_OC1_Channel: PWM/Output Compare 1 Channel |
||
521 | * - TIM_OC2_Channel : Output Compare 2 Channel |
||
522 | * Input3 : TIM_Pulse: specifies the new pulse value. |
||
523 | * Output : None |
||
524 | * Return : None |
||
525 | *******************************************************************************/ |
||
526 | void TIM_SetPulse(TIM_TypeDef *TIMx,u16 TIM_Channel ,u16 TIM_Pulse) |
||
527 | { |
||
528 | if (TIM_Channel == TIM_PWM_OC1_Channel) |
||
529 | { |
||
530 | TIMx->OC1R = TIM_Pulse; |
||
531 | } |
||
532 | else |
||
533 | { |
||
534 | TIMx->OC2R = TIM_Pulse; |
||
535 | } |
||
536 | } |
||
537 | /******************************************************************************* |
||
538 | * Function Name : TIM_GetFlagStatus |
||
539 | * Description : Checks whether the specified TIMx flag is set or not. |
||
540 | * Input1 : TIMx: where x can be from 0 to 3 to select the TIM |
||
541 | * peripheral. |
||
542 | * Input2 : TIM_Flag: specifies the flag to check. |
||
543 | * This parameter can be one of the following values: |
||
544 | * - TIM_FLAG_IC1: Input Capture Channel 1 Flag |
||
545 | * - TIM_FLAG_IC2: Input Capture Channel 2 Flag |
||
546 | * - TIM_FLAG_TO : Timer Overflow Flag |
||
547 | * - TIM_FLAG_OC1: Output Compare Channel 1 Flag |
||
548 | * - TIM_FLAG_OC2: Output Compare Channel 2 Flag |
||
549 | * Output : None |
||
550 | * Return : The NewState of the TIM_Flag (SET or RESET). |
||
551 | *******************************************************************************/ |
||
552 | FlagStatus TIM_GetFlagStatus(TIM_TypeDef *TIMx, u16 TIM_Flag) |
||
553 | { |
||
554 | if((TIMx->SR & TIM_Flag) == RESET) |
||
555 | { |
||
556 | return RESET; |
||
557 | } |
||
558 | else |
||
559 | { |
||
560 | return SET; |
||
561 | } |
||
562 | } |
||
563 | |||
564 | /******************************************************************************* |
||
565 | * Function Name : TIM_ClearFlag |
||
566 | * Description : Clears the TIM Flag passed as a parameter. |
||
567 | * Input1 : TIMx: where x can be from 0 to 3 to select the TIM |
||
568 | * peripheral. |
||
569 | * Input2 : TIM_Flag: specifies the flag to clear. |
||
570 | * This parameter can be one of the following values: |
||
571 | * - TIM_FLAG_IC1: Input Capture Channel 1 Flag |
||
572 | * - TIM_FLAG_IC2: Input Capture Channel 2 Flag |
||
573 | * - TIM_FLAG_TO : Timer Overflow Flag |
||
574 | * - TIM_FLAG_OC1: Output Compare Channel 1 Flag |
||
575 | * - TIM_FLAG_OC2: Output Compare Channel 2 Flag |
||
576 | * Output : None |
||
577 | * Return : None |
||
578 | *******************************************************************************/ |
||
579 | void TIM_ClearFlag(TIM_TypeDef *TIMx, u16 TIM_Flag) |
||
580 | { |
||
581 | /* Clear TIM_Flag */ |
||
582 | TIMx->SR &= ~TIM_Flag; |
||
583 | } |
||
584 | |||
585 | /******************************************************************************* |
||
586 | * Function Name : TIM_GetPWMIPulse |
||
587 | * Description : This routine is used to get the Pulse value in PWMI Mode. |
||
588 | * Input : TIMx: where x can be from 0 to 3 to select the TIM |
||
589 | * peripheral. |
||
590 | * Output : None |
||
591 | * Return : The pulse value. |
||
592 | *******************************************************************************/ |
||
593 | u16 TIM_GetPWMIPulse(TIM_TypeDef *TIMx) |
||
594 | { |
||
595 | return TIMx->IC2R; |
||
596 | } |
||
597 | |||
598 | /******************************************************************************* |
||
599 | * Function Name : TIM_GetPWMIPeriod |
||
600 | * Description : This routine is used to get the Period value in PWMI Mode. |
||
601 | * Input : TIMx: where x can be from 0 to 3 to select the TIM |
||
602 | * peripheral. |
||
603 | * Output : None |
||
604 | * Return : The period value. |
||
605 | *******************************************************************************/ |
||
606 | u16 TIM_GetPWMIPeriod(TIM_TypeDef *TIMx) |
||
607 | { |
||
608 | return TIMx->IC1R; |
||
609 | } |
||
610 | |||
611 | /******************************************************************************* |
||
612 | * Function Name : TIM_ITConfig |
||
613 | * Description : Configures the Timer interrupt source. |
||
614 | * Input1 : TIMx: where x can be from 0 to 3 to select the TIM |
||
615 | * peripheral. |
||
616 | * Input2 : TIM_IT: specifies the TIM interrupt source to be enabled. |
||
617 | * This parameter can be one of the following values: |
||
618 | * - TIM_IT_IC1: Input Capture 1 Interrupt source. |
||
619 | * - TIM_IT_OC1: Output Compare 1 Interrupt source. |
||
620 | * - TIM_IT_TO : Timer Overflow Interrupt source. |
||
621 | * - TIM_IT_IC2: Input Capture 2 Interrupt source. |
||
622 | * - TIM_IT_OC2: Output Compare 2 Interrupt source. |
||
623 | * Input3 : TIM_Newstate: specifies the new state of the TIMx IT. |
||
624 | * This parameter can be one of the following values: |
||
625 | * - ENABLE : Enable the needed interrupt. |
||
626 | * - DISABLE: Disable the needed interrupt. |
||
627 | * Output : None |
||
628 | * Return : None |
||
629 | *******************************************************************************/ |
||
630 | void TIM_ITConfig(TIM_TypeDef *TIMx, u16 TIM_IT, FunctionalState TIM_Newstate) |
||
631 | { |
||
632 | if(TIM_Newstate == ENABLE) |
||
633 | { |
||
634 | TIMx->CR2 = (TIMx->CR2 & 0x00FF) | TIM_IT; |
||
635 | } |
||
636 | else |
||
637 | { |
||
638 | TIMx->CR2 &= ~TIM_IT; |
||
639 | } |
||
640 | } |
||
641 | |||
642 | /******************************************************************************* |
||
643 | * Function Name : TIM_DMAConfig |
||
644 | * Description : Configures the Timer DMA source. |
||
645 | * Input1 : TIMx: where x can be from 0 to 3 to select the TIM |
||
646 | * peripheral. |
||
647 | * Input2 : TIM_DMA_Souces: specifies the TIM DMA source to be selected. |
||
648 | * This parameter can be one of the following values: |
||
649 | * - TIM_DMA_IC1: Input Capture 1 DMA source. |
||
650 | * - TIM_DMA_OCA1 Output Compare 1 DMA source. |
||
651 | * - TIM_DMA_TO: Timer Overflow DMA source. |
||
652 | * - TIM_DMA_IC2: Input Capture 2 DMA source. |
||
653 | * - TIM_DMA_OC2: Output Compare 2 DMA source. |
||
654 | * Output : None |
||
655 | * Return : None |
||
656 | *******************************************************************************/ |
||
657 | void TIM_DMAConfig(TIM_TypeDef *TIMx, u16 TIM_DMA_Sources) |
||
658 | { |
||
659 | /* Reset the DMAS[1:0] bits */ |
||
660 | TIMx->CR1 &= TIM_DMA_CLEAR_MASK; |
||
661 | /* Set the DMAS[1:0] bits according to TIM_DMA_Sources parameter */ |
||
662 | TIMx->CR1 |= TIM_DMA_Sources; |
||
663 | } |
||
664 | |||
665 | /******************************************************************************* |
||
666 | * Function Name : TIM_DMACmd |
||
667 | * Description : Enables or disables TIMx DMA peripheral. |
||
668 | * Input1 : TIMx: where x can be from 0 to 3 to select the TIM |
||
669 | * peripheral. |
||
670 | * Input2 : TIM_Newstate: new state of the TIMx DMA peripheral |
||
671 | * This parameter can be one of the following values: |
||
672 | * - ENABLE : Enable the TIMx DMA. |
||
673 | * - DISABLE: Disable the TIMx DMA. |
||
674 | * Output : None |
||
675 | * Return : None |
||
676 | *******************************************************************************/ |
||
677 | void TIM_DMACmd(TIM_TypeDef *TIMx, FunctionalState TIM_Newstate) |
||
678 | { |
||
679 | if (TIM_Newstate == ENABLE) |
||
680 | { |
||
681 | TIMx->CR2 |= TIM_DMA_ENABLE; |
||
682 | } |
||
683 | else |
||
684 | { |
||
685 | TIMx->CR2 &= TIM_DMA_DISABLE; |
||
686 | } |
||
687 | } |
||
196 | killagreg | 688 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ |