從驅動接口宏定義抽出來
第一個數據結構
// Driver functions:驅動函數
CYG_ADC_FUNCTIONS( lpc2xxx_adc_funs,
lpc2xxx_adc_enable,
lpc2xxx_adc_disable,
lpc2xxx_adc_set_rate );
//doc中的解釋說明
The macro CYG_ADC_FUNCTIONS() instantiates a function table called example_adc_funs and populates it with the ADC driver functions
//宏定義原型
#define CYG_ADC_FUNCTIONS( __name, __enable, __disable, __set_rate ) \
cyg_adc_functions __name = \
{ \
.enable = __enable, \
.disable = __disable, \
.set_rate = __set_rate \
};
//adc函數定義原型,一個結構體包含了三個函數指針
struct cyg_adc_functions
{
void (*enable)( cyg_adc_channel *chan );
void (*disable)( cyg_adc_channel *chan );
void (*set_rate)( cyg_adc_channel *chan, cyg_uint32 rate );
};
/******************************************************************/
宏展開的結果:定義了一個cyg_adc_funtions包含了函數指針的結構體
cyg_adc_functions lpc2xxx_adc_funs{
.enable = lpc2xxx_adc_enable,
.disable = lpc2xxx_adc_disable,
.set_rate = lpc2xxx_adc_set_rate
}
/********************************************************************/
//ADC信息的私有數據
static lpc2xxx_adc_info lpc2xxx_adc_info0 =
{
.base = CYGARC_HAL_LPC2XXX_REG_AD_BASE,
.vector = CYGNUM_HAL_INTERRUPT_TIMER1,
.intprio = CYGNUM_DEVS_ADC_ARM_LPC24XX_INTPRIO,
.int_handle = 0,
#if CYGPKG_DEVS_ADC_ARM_LPC24XX_DEBUG_LEVEL > 0
.isr_cnt = 0,
#endif
.chan_mask = 0
};
//ADC驅動的總結構,包含了上述的兩部分驅動函數和私有數據
CYG_ADC_DEVICE( lpc2xxx_adc_device, //名字
&lpc2xxx_adc_funs, //驅動函數
&lpc2xxx_adc_info0, //驅動信息私有數據
CYGNUM_DEVS_ADC_ARM_LPC24XX_DEFAULT_RATE); //默認采樣率
宏定義
#define CYG_ADC_DEVICE(__name, __funs, __dev_priv, __rate ) \
cyg_adc_device __name = \
{ \
.funs = __funs, \
.dev_priv = __dev_priv, \
.config.rate = __rate, \
.init = false \
};
/********************************************************************************************/
展開宏定義的結果
cyg_adc_device lpc2xxx_adc_device = \
{ \
.funs = lpc2xxx_adc_funs, , \
.dev_priv = lpc2xxx_adc_info0,
.config.rate =CYGNUM_DEVS_ADC_ARM_LPC24XX_DEFAULT_RATE,
.init = false
};
/**********************************************************************************************/
//ADC0~7驅動的注冊
LPC2XXX_ADC_CHANNEL(0~7)
//原型
#define LPC2XXX_ADC_CHANNEL( __chan ) \
CYG_ADC_CHANNEL( lpc2xxx_adc_channel##__chan, \
__chan, \
CYGDAT_DEVS_ADC_ARM_LPC24XX_CHANNEL##__chan##_BUFSIZE, \
&lpc2xxx_adc_device ); \
\
DEVTAB_ENTRY( lpc2xxx_adc_channel##__chan##_device, \
CYGDAT_DEVS_ADC_ARM_LPC24XX_CHANNEL##__chan##_NAME, \
0, \
&cyg_io_adc_devio, \
lpc2xxx_adc_init, \
lpc2xxx_adc_lookup, \
&lpc2xxx_adc_channel##__chan );
(1)第一步展開,將"##__chan"和"##_chan##"替換為__chan的數值,假設為0
CYG_ADC_CHANNEL( lpc2xxx_adc_channel0, \
0, \
CYGDAT_DEVS_ADC_ARM_LPC24XX_CHANNEL0_BUFSIZE, \
&lpc2xxx_adc_device ); \
\
DEVTAB_ENTRY( lpc2xxx_adc_channel0_device, \
CYGDAT_DEVS_ADC_ARM_LPC24XX_CHANNEL0_NAME, \
0, \
&cyg_io_adc_devio, \
lpc2xxx_adc_init, \
lpc2xxx_adc_lookup, \
&lpc2xxx_adc_channel0 );
//CYG_ADC_CHANNEL宏定義原型
#define CYG_ADC_CHANNEL( __name, __channel, __bufsize, __device )
static cyg_adc_sample_t __name##_buf[__bufsize];
cyg_adc_channel __name =
{
.channel = __channel, \
.buf = __name##_buf, \
.len = __bufsize, \
.put = 0, \
.get = 0, \
.device = __device, \
.init = false \
};
(2-1)CYG_ADC_CHANNLE再次展開后的結果:
static cyg_adc_sample_t lpc2xxx_adc_channel0_buf[CYGDAT_DEVS_ADC_ARM_LPC24XX_CHANNEL0_BUFSIZE]; \
cyg_adc_channel lpc2xxx_adc_channel0 = \
{ \
.channel = 0, \
.buf = lpc2xxx_adc_channel0_buf, \
.len = CYGDAT_DEVS_ADC_ARM_LPC24XX_CHANNEL0_BUFSIZE, \
.put = 0, \
.get = 0, \
.device = &lpc2xxx_adc_device, \
.init = false \
};
(2-2)DEVTAB_ENTRY宏定義的原型
#define DEVTAB_ENTRY(_l,_name,_dep_name,_handlers,_init,_lookup,_priv) \
CHAR_DEVTAB_ENTRY(_l,_name,_dep_name,_handlers,_init,_lookup,_priv)
#define CHAR_DEVTAB_ENTRY(_l,_name,_dep_name,_handlers,_init,_lookup,_priv)
cyg_devtab_entry_t _l CYG_HAL_TABLE_ENTRY(devtab) = {
_name,
_dep_name,
_handlers,
_init,
_lookup,
_priv,
CYG_DEVTAB_STATUS_CHAR
};
展開結果
cyg_devtab_entry_t _l CYG_HAL_TABLE_ENTRY(devtab) = {
lpc2xxx_adc_channel0_device,
CYGDAT_DEVS_ADC_ARM_LPC24XX_CHANNEL0_NAME,
0,
lpc2xxx_adc_init,
lpc2xxx_adc_lookup,
&lpc2xxx_adc_channel0,
CYG_DEVTAB_STATUS_CHAR
};
CYG_HAL_TABLE_ENTRY()原型
#define CYG_HAL_TABLE_ENTRY( _name ) \
CYGBLD_ATTRIB_SECTION(".ecos.table." __xstring(_name) ".data")
CYGBLD_ATTRIB_USED
展開結果
CYGBLD_ATTRIB_SECTION(".ecos.table.devtab.data")
CYGBLD_ATTRIB_USED
CYGBLD_ATTRIB_SECTION原型
#define CYGBLD_ATTRIB_SECTION(__sect__) __attribute__((section (__sect__)))
展開結果
__attribute__((section (".ecos.table.devtab.data")))
展開CYGBLD_ATTRIB_USED
__attribute__((used))
CYG_HAL_TABLE_ENTRY()的展開結果為
__attribute__((section (".ecos.table.devtab.data")))
__attribute__((used))
DEVTAB_ENTRY的展開結果為
cyg_devtab_entry_t _l __attribute__((section (".ecos.table.devtab.data"))) __attribute__((used)) = {
lpc2xxx_adc_channel0_device,
CYGDAT_DEVS_ADC_ARM_LPC24XX_CHANNEL0_NAME,
0,
lpc2xxx_adc_init,
lpc2xxx_adc_lookup,
&lpc2xxx_adc_channel0,
CYG_DEVTAB_STATUS_CHAR
};
總結LPC2XXX_ADC_CHANNEL(0)的宏定義結果為
static cyg_adc_sample_t lpc2xxx_adc_channel0_buf[CYGDAT_DEVS_ADC_ARM_LPC24XX_CHANNEL0_BUFSIZE];
cyg_adc_channel lpc2xxx_adc_channel0 =
{
.channel = 0,
.buf = lpc2xxx_adc_channel0_buf,
.len = CYGDAT_DEVS_ADC_ARM_LPC24XX_CHANNEL0_BUFSIZE,
.put = 0,
.get = 0,
.device = &lpc2xxx_adc_device,
.init = false
};
cyg_devtab_entry_t _l __attribute__((section (".ecos.table.devtab.data"))) __attribute__((used)) = {
lpc2xxx_adc_channel0_device,
CYGDAT_DEVS_ADC_ARM_LPC24XX_CHANNEL0_NAME,
0,
lpc2xxx_adc_init,
lpc2xxx_adc_lookup,
&lpc2xxx_adc_channel0,
CYG_DEVTAB_STATUS_CHAR
};
因為公社的版本太窄了,導致很多行都亂了,所以把一些還行符刪掉了~
看人家老外做的程序,專業~看不懂~
我要是分析的不對的,大家指正 |