北京可太冷了,说好要写一段废话,发生了不少事情,好与丧的都有,丧的占大多数。话不能多说,自己消化就好,多说就泄露了天机。
在使用ARM Cortex-A9的中断系统时,阅读到了一段配置定时器的程序,其中包含的一行程序引起了我的注意。
XTmrCtr_SetOptions(&TMRInst, 0, XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION);//第三个变量选择了模式选项对寄存器进行了配置
这行程序是对定时器的 Control/Status 寄存器XTC_TCSR_OFFSET进行配置,一般来说配置寄存器是通过修改寄存器的某1bit的值 0,1 来对这一位代表的功能进行设置,或者直接写入一个字节8bit的数来直接将一个寄存器配置完成。
但是这行程序是使用了一个封装更好,更易读的方式对寄存器进行配置的,使用了Xilinx提供的XTmrCtr_SetOptions()函数对定时器进行模式配置。可以看到函数的第三个变量通过选择option的形式,选择了定时器的不同功能,也就是设置了定时器控制寄存器的不同位的功能,以一个更易读的方式配置了定时器的工作方式,使得程序更易读。下面对此函数的底层进行分析,来看看它是如何工作的。
首先此函数有3个变量,首先要理解这三个变量的含义。
/*****************************************************************************
* @param InstancePtr is a pointer to the XTmrCtr instance.
* @param TmrCtrNumber is the timer counter of the device to operate on.
* Each device may contain multiple timer counters. The timer
* number is a zero based number with a range of
* 0 - (XTC_DEVICE_TIMER_COUNT - 1).
* @param Options contains the desired options to be set or cleared.
* Setting the option to '1' enables the option, clearing the to
* '0' disables the option. The options are bit masks such that
* multiple options may be set or cleared. The options are
* described in xtmrctr.h.
******************************************************************************/
void XTmrCtr_SetOptions(XTmrCtr * InstancePtr, u8 TmrCtrNumber, u32 Options)
Xilinx的函数不仅提供了文档,而且在每个函数都详细备注了函数的功能与变量的意义,这点很棒,就不需要详细解释了,大致含义是InstancePtr是定时器实例,TmrCtrNumber是在有多个定时器的时候指示是第几个定时器,Options是用来设置选项的,当选择了一个选项,该bit就会被置1,未被选择的就被置0.
要理解函数是如何工作的,就需要查看这个配置函数实现的文件xtmrctr_options.c,将这个文件的代码贴到最后以便查阅。
文件提供了两个函数接口,一个是配置定时器的控制寄存器函数XTmrCtr_SetOptions,另一个是获得寄存器当前状态的XTmrCtr_GetOptions,两个函数实现的功能类似,只需要看懂一个就能理解另一个。
函数实现如图下,实现核心配置功能的代码是for循环后面。
******************************************************************************/
void XTmrCtr_SetOptions(XTmrCtr * InstancePtr, u8 TmrCtrNumber, u32 Options)
{
u32 CounterControlReg = 0;
u32 Index;
Xil_AssertVoid(InstancePtr != NULL);
Xil_AssertVoid(TmrCtrNumber < XTC_DEVICE_TIMER_COUNT);
Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
/*
* Loop through the Options table, turning the enable on or off
* depending on whether the bit is set in the incoming Options flag.
*/
for (Index = 0; Index < XTC_NUM_OPTIONS; Index++) {
if (Options & OptionsTable[Index].Option) {
/*
* Turn the option on
*/
CounterControlReg |= OptionsTable[Index].Mask;
}
else {
/*
* Turn the option off
*/
CounterControlReg &= ~OptionsTable[Index].Mask;
}
}
/*
* Write out the updated value to the actual register
*/
XTmrCtr_WriteReg(InstancePtr->BaseAddress, TmrCtrNumber,
XTC_TCSR_OFFSET, CounterControlReg);
}
XTC_NUM_OPTIONS的定义在前面有,代表的是有几个可以配置的bit,进行for循环,对每个bit进行配置。而OptionsTable的值在xtmrctr.h和xtmrctr_l.h中有定义,例如option的定义
/**
* Used to configure the timer counter device.
*
* XTC_CASCADE_MODE_OPTION Enables the Cascade Mode only valid for TCSRO.
* XTC_ENABLE_ALL_OPTION Enables all timer counters at once.
* XTC_DOWN_COUNT_OPTION Configures the timer counter to count down from
* start value, the default is to count up.
* XTC_CAPTURE_MODE_OPTION Configures the timer to capture the timer
* counter value when the external capture line is
* asserted. The default mode is compare mode.
* XTC_INT_MODE_OPTION Enables the timer counter interrupt output.
* XTC_AUTO_RELOAD_OPTION In compare mode, configures the timer counter to
* reload from the compare value. The default mode
* causes the timer counter to hold when the
* compare value is hit.
* In capture mode, configures the timer counter to
* not hold the previous capture value if a new
* event occurs. The default mode cause the timer
* counter to hold the capture value until
* recognized.
* XTC_EXT_COMPARE_OPTION Enables the external compare output signal.
*
*/
#define XTC_CASCADE_MODE_OPTION 0x00000080UL
#define XTC_ENABLE_ALL_OPTION 0x00000040UL
#define XTC_DOWN_COUNT_OPTION 0x00000020UL
#define XTC_CAPTURE_MODE_OPTION 0x00000010UL
#define XTC_INT_MODE_OPTION 0x00000008UL
#define XTC_AUTO_RELOAD_OPTION 0x00000004UL
#define XTC_EXT_COMPARE_OPTION 0x00000002UL
就假设按照最初调用函数,要配置XTC_INT_MODE_OPTION选项,即开启定时器中断,那么函数的第三个变量Options就会传进来0x00000008UL。对for循环进行分析
index控制配置OptionsTable第几个值。当index从0加到3时
if (Options & OptionsTable[Index].Option) //Options为XTC_INT_MODE_OPTION,OptionsTable[Index].Option与Options相同
if条件内两个变量相与,满足,执行下一句。
CounterControlReg |= OptionsTable[Index].Mask;
当满足时,假设CounterControlReg = 0x00000000
CounterControlReg =0x00000000 | XTC_CSR_ENABLE_INT_MASK(0x00000040)
CounterControlReg 的值就变为了0x00000040。开启定时器中断功能
若没满足,执行
CounterControlReg &= ~OptionsTable[Index].Mask;
假设CounterControlReg = 0x00000040
CounterControlReg =0x00000040 & ~XTC_CSR_ENABLE_INT_MASK(0xFFFFFFBF)
CounterControlReg 的值就变为了0x00000000。只改变了某一bit而其他位并未修改。关闭了定时器中断功能。
最终通过XTmrCtr_WriteReg函数将CounterControlReg 的值写入寄存器中,实现了对定时器控制寄存器的控制。
在获取控制寄存器的函数XTmrCtr_GetOptions()是同理的,理解了set就能理解get。
回头再看代码调用使用的方式
XTmrCtr_SetOptions(&TMRInst, 0, XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION);
在第三个变量通过选择不同的常量OPTION,通过选择的方式开启或者关闭了某项功能,程序可读性极佳,也易于调用者使用,可以不用再了解控制寄存器的不用比特的含义来直接进行控制,封装的也很好。
嗯,就到这里吧,读程序和写解释完全不一样,十几分钟能读完的想说明白可废了老大劲,后面还要多学习。
/***************************** Include Files *********************************/
#include "xtmrctr.h"
#include "xtmrctr_i.h"
/************************** Variable Definitions *****************************/
/*
* The following data type maps an option to a register mask such that getting
* and setting the options may be table driven.
*/
typedef struct {
u32 Option;
u32 Mask;
} Mapping;
/*
* Create the table which contains options which are to be processed to get/set
* the options. These options are table driven to allow easy maintenance and
* expansion of the options.
*/
static Mapping OptionsTable[] = {
{XTC_CASCADE_MODE_OPTION, XTC_CSR_CASC_MASK},
{XTC_ENABLE_ALL_OPTION, XTC_CSR_ENABLE_ALL_MASK},
{XTC_DOWN_COUNT_OPTION, XTC_CSR_DOWN_COUNT_MASK},
{XTC_CAPTURE_MODE_OPTION, XTC_CSR_CAPTURE_MODE_MASK |
XTC_CSR_EXT_CAPTURE_MASK},
{XTC_INT_MODE_OPTION, XTC_CSR_ENABLE_INT_MASK},
{XTC_AUTO_RELOAD_OPTION, XTC_CSR_AUTO_RELOAD_MASK},
{XTC_EXT_COMPARE_OPTION, XTC_CSR_EXT_GENERATE_MASK}
};
/* Create a constant for the number of entries in the table */
#define XTC_NUM_OPTIONS (sizeof(OptionsTable) / sizeof(Mapping))
/*****************************************************************************/
/**
*
* Enables the specified options for the specified timer counter. This function
* sets the options without regard to the current options of the driver. To
* prevent a loss of the current options, the user should call
* XTmrCtr_GetOptions() prior to this function and modify the retrieved options
* to pass into this function to prevent loss of the current options.
*
* @param InstancePtr is a pointer to the XTmrCtr instance.
* @param TmrCtrNumber is the timer counter of the device to operate on.
* Each device may contain multiple timer counters. The timer
* number is a zero based number with a range of
* 0 - (XTC_DEVICE_TIMER_COUNT - 1).
* @param Options contains the desired options to be set or cleared.
* Setting the option to '1' enables the option, clearing the to
* '0' disables the option. The options are bit masks such that
* multiple options may be set or cleared. The options are
* described in xtmrctr.h.
*
* @return None.
*
* @note None.
*
******************************************************************************/
void XTmrCtr_SetOptions(XTmrCtr * InstancePtr, u8 TmrCtrNumber, u32 Options)
{
u32 CounterControlReg = 0;
u32 Index;
Xil_AssertVoid(InstancePtr != NULL);
Xil_AssertVoid(TmrCtrNumber < XTC_DEVICE_TIMER_COUNT);
Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
/*
* Loop through the Options table, turning the enable on or off
* depending on whether the bit is set in the incoming Options flag.
*/
for (Index = 0; Index < XTC_NUM_OPTIONS; Index++) {
if (Options & OptionsTable[Index].Option) {
/*
* Turn the option on
*/
CounterControlReg |= OptionsTable[Index].Mask;
}
else {
/*
* Turn the option off
*/
CounterControlReg &= ~OptionsTable[Index].Mask;
}
}
/*
* Write out the updated value to the actual register
*/
XTmrCtr_WriteReg(InstancePtr->BaseAddress, TmrCtrNumber,
XTC_TCSR_OFFSET, CounterControlReg);
}
/*****************************************************************************/
/**
*
* Get the options for the specified timer counter.
*
* @param InstancePtr is a pointer to the XTmrCtr instance.
* @param TmrCtrNumber is the timer counter of the device to operate on
* Each device may contain multiple timer counters. The timer
* number is a zero based number with a range of
* 0 - (XTC_DEVICE_TIMER_COUNT - 1).
*
* @return
*
* The currently set options. An option which is set to a '1' is enabled and
* set to a '0' is disabled. The options are bit masks such that multiple
* options may be set or cleared. The options are described in xtmrctr.h.
*
* @note None.
*
******************************************************************************/
u32 XTmrCtr_GetOptions(XTmrCtr * InstancePtr, u8 TmrCtrNumber)
{
u32 Options = 0;
u32 CounterControlReg;
u32 Index;
Xil_AssertNonvoid(InstancePtr != NULL);
Xil_AssertNonvoid(TmrCtrNumber < XTC_DEVICE_TIMER_COUNT);
Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
/*
* Read the current contents of the control status register to allow
* the current options to be determined
*/
CounterControlReg = XTmrCtr_ReadReg(InstancePtr->BaseAddress,
TmrCtrNumber, XTC_TCSR_OFFSET);
/*
* Loop through the Options table, turning the enable on or off
* depending on whether the bit is set in the current register settings.
*/
for (Index = 0; Index < XTC_NUM_OPTIONS; Index++) {
if (CounterControlReg & OptionsTable[Index].Mask) {
Options |= OptionsTable[Index].Option; /* turn it on */
}
else {
Options &= ~OptionsTable[Index].Option; /* turn it off */
}
}
return Options;
}
/** @} */