1 |
577 |
jeremybenn |
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
|
2 |
|
|
* File Name : 75x_tb.c
|
3 |
|
|
* Author : MCD Application Team
|
4 |
|
|
* Date First Issued : 03/10/2006
|
5 |
|
|
* Description : This file provides all the TB software functions.
|
6 |
|
|
********************************************************************************
|
7 |
|
|
* History:
|
8 |
|
|
* 07/17/2006 : V1.0
|
9 |
|
|
* 03/10/2006 : V0.1
|
10 |
|
|
********************************************************************************
|
11 |
|
|
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
|
12 |
|
|
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
|
13 |
|
|
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
|
14 |
|
|
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
|
15 |
|
|
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
|
16 |
|
|
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
|
17 |
|
|
*******************************************************************************/
|
18 |
|
|
|
19 |
|
|
/* Includes ------------------------------------------------------------------*/
|
20 |
|
|
#include "75x_tb.h"
|
21 |
|
|
#include "75x_mrcc.h"
|
22 |
|
|
|
23 |
|
|
/* Private typedef -----------------------------------------------------------*/
|
24 |
|
|
/* Private define ------------------------------------------------------------*/
|
25 |
|
|
/* Private macro -------------------------------------------------------------*/
|
26 |
|
|
/* Private variables ---------------------------------------------------------*/
|
27 |
|
|
#define TB_IT_Enable_Mask 0x7FFF
|
28 |
|
|
#define TB_IT_Clear_Mask 0x7FFF
|
29 |
|
|
#define TB_IC_Enable 0x0004
|
30 |
|
|
#define TB_ICPolarity_Set 0x0008
|
31 |
|
|
#define TB_ICPolarity_Reset 0xFFF7
|
32 |
|
|
#define TB_UFS_Reset 0xFFFE
|
33 |
|
|
#define TB_UFS_Set 0x0001
|
34 |
|
|
|
35 |
|
|
/* TB debug state */
|
36 |
|
|
#define TB_DBGC_Set 0x0400
|
37 |
|
|
#define TB_DBGC_Reset 0xFB7F
|
38 |
|
|
|
39 |
|
|
/* TB counter state */
|
40 |
|
|
#define TB_COUNTER_Reset 0x0002
|
41 |
|
|
#define TB_COUNTER_Start 0x0004
|
42 |
|
|
#define TB_COUNTER_Stop 0xFFFB
|
43 |
|
|
|
44 |
|
|
#define TB_SMS_EXTCLK_Set 0x0008
|
45 |
|
|
#define TB_SMS_RESETCLK_Set 0x0000
|
46 |
|
|
|
47 |
|
|
/* TB Slave Mode Enable Set/Reset value */
|
48 |
|
|
#define TB_SME_Reset 0x731B
|
49 |
|
|
#define TB_SME_Set 0x0004
|
50 |
|
|
|
51 |
|
|
/* TB Trigger Selection value */
|
52 |
|
|
#define TB_TS_IC1_Set 0x0200
|
53 |
|
|
|
54 |
|
|
/* TB SCR Masks bit */
|
55 |
|
|
#define TB_SlaveModeSelection_Mask 0x7307
|
56 |
|
|
#define TB_TriggerSelection_Mask 0x701F
|
57 |
|
|
|
58 |
|
|
/* Reset Register Masks */
|
59 |
|
|
#define TB_Prescaler_Reset_Mask 0x0000
|
60 |
|
|
#define TB_CounterMode_Mask 0xFF8F
|
61 |
|
|
#define TB_AutoReload_Reset_Mask 0xFFFF
|
62 |
|
|
|
63 |
|
|
/* Private function prototypes -----------------------------------------------*/
|
64 |
|
|
/* Private functions ---------------------------------------------------------*/
|
65 |
|
|
|
66 |
|
|
/******************************************************************************
|
67 |
|
|
* Function Name : TB_DeInit
|
68 |
|
|
* Description : Deinitializes the TB peripheral registers to their default
|
69 |
|
|
* reset values.
|
70 |
|
|
* Input : None
|
71 |
|
|
* Output : None
|
72 |
|
|
* Return : None
|
73 |
|
|
*******************************************************************************/
|
74 |
|
|
void TB_DeInit(void)
|
75 |
|
|
{
|
76 |
|
|
/* Enters and exits the TB peripheral to and from reset */
|
77 |
|
|
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TB,ENABLE);
|
78 |
|
|
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TB,DISABLE);
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
/*******************************************************************************
|
82 |
|
|
* Function Name : TB_Init
|
83 |
|
|
* Description : Initializes TB peripheral according to the specified
|
84 |
|
|
* parameters in the TB_InitStruct.
|
85 |
|
|
* Input : TB_InitStruct: pointer to a TB_InitTypeDef structure that
|
86 |
|
|
* contains the configuration information for the TB peripheral.
|
87 |
|
|
* Output : None
|
88 |
|
|
* Return : None
|
89 |
|
|
*******************************************************************************/
|
90 |
|
|
void TB_Init(TB_InitTypeDef* TB_InitStruct)
|
91 |
|
|
{
|
92 |
|
|
/* Set the TB prescaler value */
|
93 |
|
|
TB->PSC = TB_InitStruct->TB_Prescaler;
|
94 |
|
|
|
95 |
|
|
/* Set the TB period value */
|
96 |
|
|
TB->ARR = TB_InitStruct->TB_AutoReload;
|
97 |
|
|
|
98 |
|
|
/* Set the corresponding counter mode */
|
99 |
|
|
TB->CR = (TB->CR & TB_CounterMode_Mask) | TB_InitStruct->TB_CounterMode;
|
100 |
|
|
|
101 |
|
|
/* Set the corresponding clock source */
|
102 |
|
|
if(TB_InitStruct->TB_ClockSource == TB_ClockSource_CKRTC)
|
103 |
|
|
{
|
104 |
|
|
TB->SCR &= TB_SME_Reset & TB_SlaveModeSelection_Mask & TB_TriggerSelection_Mask;
|
105 |
|
|
TB->SCR |= TB_SMS_EXTCLK_Set | TB_SME_Set | TB_TS_IC1_Set;
|
106 |
|
|
}
|
107 |
|
|
else
|
108 |
|
|
{
|
109 |
|
|
TB->SCR &= TB_SME_Reset & TB_SlaveModeSelection_Mask & TB_TriggerSelection_Mask;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
if(TB_InitStruct->TB_Mode == TB_Mode_IC)
|
113 |
|
|
{
|
114 |
|
|
/* Set the corresponding value in TB SCR register */
|
115 |
|
|
TB->SCR &= TB_SME_Reset & TB_SlaveModeSelection_Mask & TB_TriggerSelection_Mask;
|
116 |
|
|
TB->SCR |= TB_SMS_RESETCLK_Set | TB_SME_Set | TB_TS_IC1_Set;
|
117 |
|
|
|
118 |
|
|
/* Set the IC1 enable bit */
|
119 |
|
|
TB->IMCR |= TB_IC_Enable;
|
120 |
|
|
|
121 |
|
|
/* Set the input signal polarity */
|
122 |
|
|
if (TB_InitStruct->TB_ICAPolarity == TB_ICAPolarity_Falling)
|
123 |
|
|
{
|
124 |
|
|
TB->IMCR |= TB_ICPolarity_Set;
|
125 |
|
|
}
|
126 |
|
|
else
|
127 |
|
|
{
|
128 |
|
|
TB->IMCR &= TB_ICPolarity_Reset;
|
129 |
|
|
}
|
130 |
|
|
}
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
/*******************************************************************************
|
134 |
|
|
* Function Name : TB_StructInit
|
135 |
|
|
* Description : Fills each TB_InitStruct member with its default value
|
136 |
|
|
* Input : TB_InitStruct : pointer to a TB_InitTypeDef structure which
|
137 |
|
|
* will be initialized.
|
138 |
|
|
* Output : None
|
139 |
|
|
* Return : None
|
140 |
|
|
*******************************************************************************/
|
141 |
|
|
void TB_StructInit(TB_InitTypeDef *TB_InitStruct)
|
142 |
|
|
{
|
143 |
|
|
TB_InitStruct->TB_Mode = TB_Mode_Timing;
|
144 |
|
|
TB_InitStruct->TB_ClockSource = TB_ClockSource_CKTIM;
|
145 |
|
|
TB_InitStruct->TB_CounterMode = TB_CounterMode_Up;
|
146 |
|
|
TB_InitStruct->TB_ICAPolarity = TB_ICAPolarity_Rising;
|
147 |
|
|
TB_InitStruct->TB_Prescaler = TB_Prescaler_Reset_Mask;
|
148 |
|
|
TB_InitStruct->TB_AutoReload = TB_AutoReload_Reset_Mask;
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
/*******************************************************************************
|
152 |
|
|
* Function Name : TB_Cmd
|
153 |
|
|
* Description : Enables or disables the TB peripheral.
|
154 |
|
|
* Input : Newstate: new state of the TB peripheral. This parameter can
|
155 |
|
|
* be: ENABLE or DISABLE.
|
156 |
|
|
* Output : None
|
157 |
|
|
* Return : None
|
158 |
|
|
*******************************************************************************/
|
159 |
|
|
void TB_Cmd(FunctionalState Newstate)
|
160 |
|
|
{
|
161 |
|
|
if(Newstate == ENABLE)
|
162 |
|
|
{
|
163 |
|
|
TB->CR |= TB_COUNTER_Start;
|
164 |
|
|
}
|
165 |
|
|
else
|
166 |
|
|
{
|
167 |
|
|
TB->CR &= TB_COUNTER_Stop;
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
/*******************************************************************************
|
172 |
|
|
* Function Name : TB_ITConfig
|
173 |
|
|
* Description : Enables or disables the specified TB interrupt.
|
174 |
|
|
* Input : - TB_IT: specifies the TB interrupt sources to be enabled or
|
175 |
|
|
* disabled.
|
176 |
|
|
* This parameter can be any combination of the following values:
|
177 |
|
|
* - TB_IT_Update: TB Update interrupt
|
178 |
|
|
* - TB_IT_GlobalUpdate: TB Global Update interrupt
|
179 |
|
|
* - TB_IT_IC: TB Input Capture interrupt
|
180 |
|
|
* - Newstate: new state of the specified TB interrupts.
|
181 |
|
|
* This parameter can be: ENABLE or DISABLE.
|
182 |
|
|
* Output : None
|
183 |
|
|
* Return : None
|
184 |
|
|
*******************************************************************************/
|
185 |
|
|
void TB_ITConfig(u16 TB_IT, FunctionalState Newstate)
|
186 |
|
|
{
|
187 |
|
|
u16 TB_IT_Enable = 0;
|
188 |
|
|
|
189 |
|
|
TB_IT_Enable = TB_IT & TB_IT_Enable_Mask;
|
190 |
|
|
|
191 |
|
|
if(Newstate == ENABLE)
|
192 |
|
|
{
|
193 |
|
|
/* Update interrupt global source: overflow/undeflow, counter reset operation
|
194 |
|
|
or slave mode controller in reset mode */
|
195 |
|
|
if ((TB_IT & TB_IT_GlobalUpdate) == TB_IT_GlobalUpdate)
|
196 |
|
|
{
|
197 |
|
|
TB->CR &= TB_UFS_Reset;
|
198 |
|
|
}
|
199 |
|
|
/* Update interrupt source: counter overflow/underflow */
|
200 |
|
|
else if ((TB_IT & TB_IT_Update) == TB_IT_Update)
|
201 |
|
|
{
|
202 |
|
|
TB->CR |= TB_UFS_Set;
|
203 |
|
|
}
|
204 |
|
|
/* Select and enable the interrupts requests */
|
205 |
|
|
TB->RSR |= TB_IT_Enable;
|
206 |
|
|
TB->RER |= TB_IT_Enable;
|
207 |
|
|
}
|
208 |
|
|
/* Disable the interrupts requests */
|
209 |
|
|
else
|
210 |
|
|
{
|
211 |
|
|
TB->RSR &= ~TB_IT_Enable;
|
212 |
|
|
TB->RER &= ~TB_IT_Enable;
|
213 |
|
|
}
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
/*******************************************************************************
|
217 |
|
|
* Function Name : TB_SetPrescaler
|
218 |
|
|
* Description : Sets the TB Prescaler value.
|
219 |
|
|
* Input : Prescaler: specifies the TB Prescaler value.
|
220 |
|
|
* Output : None
|
221 |
|
|
* Return : None
|
222 |
|
|
*******************************************************************************/
|
223 |
|
|
void TB_SetPrescaler(u16 Prescaler)
|
224 |
|
|
{
|
225 |
|
|
/* Sets the prescaler value */
|
226 |
|
|
TB->PSC = Prescaler;
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
/*******************************************************************************
|
230 |
|
|
* Function Name : TB_ResetCounter
|
231 |
|
|
* Description : Re-intializes the counter and generates an update of the
|
232 |
|
|
* registers.
|
233 |
|
|
* Input : None
|
234 |
|
|
* Output : None
|
235 |
|
|
* Return : None
|
236 |
|
|
*******************************************************************************/
|
237 |
|
|
void TB_ResetCounter(void)
|
238 |
|
|
{
|
239 |
|
|
/* Re-intializes TB counter */
|
240 |
|
|
TB->CR |= TB_COUNTER_Reset;
|
241 |
|
|
}
|
242 |
|
|
|
243 |
|
|
/*******************************************************************************
|
244 |
|
|
* Function Name : TB_DebugCmd
|
245 |
|
|
* Description : Enables or disables TB peripheral Debug control.
|
246 |
|
|
* Input : Newstate: new state of the TB Debug control.
|
247 |
|
|
* This parameter can be: ENABLE or DISABLE.
|
248 |
|
|
* Output : None
|
249 |
|
|
* Return : None
|
250 |
|
|
*******************************************************************************/
|
251 |
|
|
void TB_DebugCmd(FunctionalState Newstate)
|
252 |
|
|
{
|
253 |
|
|
if(Newstate == ENABLE)
|
254 |
|
|
{
|
255 |
|
|
TB->CR |= TB_DBGC_Set;
|
256 |
|
|
}
|
257 |
|
|
else
|
258 |
|
|
{
|
259 |
|
|
TB->CR &= TB_DBGC_Reset;
|
260 |
|
|
}
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
/*******************************************************************************
|
264 |
|
|
* Function Name : TB_CounterModeConfig
|
265 |
|
|
* Description : Configures the TB Counter Mode.
|
266 |
|
|
* Input : TB_CounterMode: specifies the TB counter mode to be used.
|
267 |
|
|
* This parameter can be one of the following values:
|
268 |
|
|
* - TB_CounterMode_Up: TB Up Counting Mode
|
269 |
|
|
* - TB_CounterMode_Down: TB Down Counting Mode
|
270 |
|
|
* - TB_CounterMode_CenterAligned: TB Center Aligned Mode
|
271 |
|
|
* Output : None
|
272 |
|
|
* Return : None
|
273 |
|
|
*******************************************************************************/
|
274 |
|
|
void TB_CounterModeConfig(u16 TB_CounterMode)
|
275 |
|
|
{
|
276 |
|
|
/* Counter mode configuration */
|
277 |
|
|
TB->CR &= TB_CounterMode_Mask;
|
278 |
|
|
TB->CR |= TB_CounterMode;
|
279 |
|
|
}
|
280 |
|
|
|
281 |
|
|
/*******************************************************************************
|
282 |
|
|
* Function Name : TB_SLaveModeConfig
|
283 |
|
|
* Description : Configures the TB slave Mode.
|
284 |
|
|
* Input : TB_SMSMode: specifies the TB slave mode to be used.
|
285 |
|
|
* This parameter can be one of the following values:
|
286 |
|
|
* - TB_SMSMode_Trigger: The counter starts at a rising
|
287 |
|
|
* edge of the trigger
|
288 |
|
|
* - TB_SMSMode_Gated: The counter clock is enabled when
|
289 |
|
|
* trigger signal is high
|
290 |
|
|
* - TB_SMSMode_External: The rising edge of selected trigger
|
291 |
|
|
* clocks the counter
|
292 |
|
|
* - TB_SMSMode_Reset: The rising edge of the selected
|
293 |
|
|
* trigger signal resets the counter
|
294 |
|
|
* Output : None
|
295 |
|
|
* Return : None
|
296 |
|
|
*******************************************************************************/
|
297 |
|
|
void TB_SLaveModeConfig(u16 TB_SMSMode)
|
298 |
|
|
{
|
299 |
|
|
TB->SCR &= TB_SME_Reset & TB_SlaveModeSelection_Mask & TB_TriggerSelection_Mask;
|
300 |
|
|
TB->SCR |= TB_SME_Set | TB_SMSMode | TB_TS_IC1_Set;
|
301 |
|
|
}
|
302 |
|
|
/*******************************************************************************
|
303 |
|
|
* Function Name : TB_GetCounter
|
304 |
|
|
* Description : Gets the TB Counter value.
|
305 |
|
|
* Input : None
|
306 |
|
|
* Output : None
|
307 |
|
|
* Return : The TB counter register value.
|
308 |
|
|
*******************************************************************************/
|
309 |
|
|
u16 TB_GetCounter(void)
|
310 |
|
|
{
|
311 |
|
|
return TB->CNT;
|
312 |
|
|
}
|
313 |
|
|
|
314 |
|
|
/*******************************************************************************
|
315 |
|
|
* Function Name : TB_GetICAP1
|
316 |
|
|
* Description : Gets the TB Input capture value.
|
317 |
|
|
* Input : None
|
318 |
|
|
* Output : None
|
319 |
|
|
* Return : The TB ICR1 register value.
|
320 |
|
|
*******************************************************************************/
|
321 |
|
|
u16 TB_GetICAP1(void)
|
322 |
|
|
{
|
323 |
|
|
return TB->ICR1;
|
324 |
|
|
}
|
325 |
|
|
|
326 |
|
|
/*******************************************************************************
|
327 |
|
|
* Function Name : TB_SetCounter
|
328 |
|
|
* Description : Sets the TB Counter value.
|
329 |
|
|
* Input : Counter: specifies the TB Counter value.
|
330 |
|
|
* Output : None
|
331 |
|
|
* Return : None
|
332 |
|
|
*******************************************************************************/
|
333 |
|
|
void TB_SetCounter(u16 Counter)
|
334 |
|
|
{
|
335 |
|
|
TB->CNT = Counter;
|
336 |
|
|
}
|
337 |
|
|
|
338 |
|
|
/*******************************************************************************
|
339 |
|
|
* Function Name : TB_GetFlagStatus
|
340 |
|
|
* Description : Checks whether the specified TB flag is set or not.
|
341 |
|
|
* Input : TB_FLAG: specifies the flag to check.
|
342 |
|
|
* This parameter can be one of the following values:
|
343 |
|
|
* - TB_FLAG_IC: TB Input Capture flag
|
344 |
|
|
* - TB_FLAG_Update: TB update flag
|
345 |
|
|
* Output : None
|
346 |
|
|
* Return : The new state of the TB_FLAG (SET or RESET).
|
347 |
|
|
*******************************************************************************/
|
348 |
|
|
FlagStatus TB_GetFlagStatus(u16 TB_FLAG)
|
349 |
|
|
{
|
350 |
|
|
if((TB->ISR & TB_FLAG) != RESET )
|
351 |
|
|
{
|
352 |
|
|
return SET;
|
353 |
|
|
}
|
354 |
|
|
else
|
355 |
|
|
{
|
356 |
|
|
return RESET;
|
357 |
|
|
}
|
358 |
|
|
}
|
359 |
|
|
|
360 |
|
|
/*******************************************************************************
|
361 |
|
|
* Function Name : TB_ClearFlag
|
362 |
|
|
* Description : Clears the TB’s pending flags.
|
363 |
|
|
* Input : TB_FLAG: specifies the flag to clear.
|
364 |
|
|
* This parameter can be any combination of the following values:
|
365 |
|
|
* - TB_FLAG_IC: TB Input Capture flag
|
366 |
|
|
* - TB_FLAG_Update: TB update flag
|
367 |
|
|
* Output : None
|
368 |
|
|
* Return : None
|
369 |
|
|
*******************************************************************************/
|
370 |
|
|
void TB_ClearFlag(u16 TB_FLAG)
|
371 |
|
|
{
|
372 |
|
|
/* Clears the flags */
|
373 |
|
|
TB->ISR &= ~TB_FLAG;
|
374 |
|
|
}
|
375 |
|
|
|
376 |
|
|
/*******************************************************************************
|
377 |
|
|
* Function Name : TB_GetITStatus
|
378 |
|
|
* Description : Checks whether the specified TB interrupt has occurred or not.
|
379 |
|
|
* Input : TB_IT: specifies the interrupt to check.
|
380 |
|
|
* This parameter can be one of the following values:
|
381 |
|
|
* - TB_IT_Update: TB Update interrupt
|
382 |
|
|
* - TB_IT_GlobalUpdate: TB Global Update interrupt
|
383 |
|
|
* - TB_IT_IC: TB Input Capture interrupt
|
384 |
|
|
* Output : None
|
385 |
|
|
* Return : The new state of the TB_IT (SET or RESET).
|
386 |
|
|
*******************************************************************************/
|
387 |
|
|
ITStatus TB_GetITStatus(u16 TB_IT)
|
388 |
|
|
{
|
389 |
|
|
u16 TB_IT_Check = 0;
|
390 |
|
|
|
391 |
|
|
/* Calculates the pending bits to be checked */
|
392 |
|
|
TB_IT_Check = TB_IT & TB_IT_Clear_Mask;
|
393 |
|
|
|
394 |
|
|
if((TB->ISR & TB_IT_Check) != RESET )
|
395 |
|
|
{
|
396 |
|
|
return SET;
|
397 |
|
|
}
|
398 |
|
|
else
|
399 |
|
|
{
|
400 |
|
|
return RESET;
|
401 |
|
|
}
|
402 |
|
|
}
|
403 |
|
|
|
404 |
|
|
/*******************************************************************************
|
405 |
|
|
* Function Name : TB_ClearITPendingBit
|
406 |
|
|
* Description : Clears the TB's interrupt pending bits.
|
407 |
|
|
* Input : TB_IT: specifies the interrupt pending bit to clear.
|
408 |
|
|
* This parameter can be any combination of the following values:
|
409 |
|
|
* - TB_IT_Update: TB Update interrupt
|
410 |
|
|
* - TB_IT_GlobalUpdate: TB Global Update interrupt
|
411 |
|
|
* - TB_IT_IC: TB Input Capture interrupt
|
412 |
|
|
* Output : None
|
413 |
|
|
* Return : None
|
414 |
|
|
*******************************************************************************/
|
415 |
|
|
void TB_ClearITPendingBit(u16 TB_IT)
|
416 |
|
|
{
|
417 |
|
|
u16 TB_IT_Clear = 0;
|
418 |
|
|
|
419 |
|
|
/* Calculates the pending bits to be cleared */
|
420 |
|
|
TB_IT_Clear = TB_IT & TB_IT_Clear_Mask;
|
421 |
|
|
|
422 |
|
|
/* Clears the pending bits */
|
423 |
|
|
TB->ISR &= ~TB_IT_Clear;
|
424 |
|
|
}
|
425 |
|
|
/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/
|