关于库文件中typedef 结构体指针定义出现了好几种不同的定义方法:
以Hal结构体定义为例:
typedef struct _HAL_Obj_
{
ADC_Handle adcHandle; //!< the ADC handle
CLK_Handle clkHandle; //!< the clock handle
CPU_Handle cpuHandle; //!< the CPU handle
DRV8305_Handle drv8305Handle; //!< the drv8305 interface handle
DRV8305_Obj drv8305; //!< the drv8305 interface object
} HAL_Obj;
//! \brief Defines the HAL handle
//! \details The HAL handle is a pointer to a HAL object. In all HAL functions
//! the HAL handle is passed so that the function knows what peripherals
//! are to be accessed.
//!
typedef struct _HAL_Obj_ *HAL_Handle;
在HAL初始化函数中:
HAL_Handle HAL_init(void *pMemory,const size_t numBytes)
{
uint_least8_t cnt;
HAL_Handle handle;
HAL_Obj *obj;
if(numBytes < sizeof(HAL_Obj))
return((HAL_Handle)NULL);
// assign the handle
handle = (HAL_Handle)pMemory;
// assign the object
obj = (HAL_Obj *)handle;
// initialize the watchdog driver
obj->wdogHandle = WDOG_init((void *)WDOG_BASE_ADDR,sizeof(WDOG_Obj));
// disable watchdog
HAL_disableWdog(handle);
// initialize the ADC
obj->adcHandle = ADC_init((void *)ADC_BASE_ADDR,sizeof(ADC_Obj));
return(handle);
}
HAL_Handle handle;
HAL_Obj *obj;
这两种有什么区别?
handle和obj难道不都是两个结构体指针吗?为什么要定义两个?而且同样是两个,为什么还要采用两种不同的形式?
还有关于GPIO的结构体指针中还出现了其他版本:
第一个版本是FOC的28027的foc的lab例程中的
typedef struct _GPIO_Obj_
{
volatile uint32_t GPACTRL; //!< GPIO A Control Register
volatile uint32_t GPAQSEL1; //!< GPIO A Qualifier Select 1 Register
volatile uint32_t GPAQSEL2; //!< GPIO A Qualifier Select 2 Register
} GPIO_Obj;
//! \brief Defines the general purpose I/O (GPIO) handle
//!
typedef struct _GPIO_Obj_ *GPIO_Handle;
下面一个是F28027的controlSUITE的V2.30库函数中的:
typedef struct _GPIO_Obj_
{
volatile uint32_t GPACTRL; //!< GPIO A Control Register
volatile uint32_t GPAQSEL1; //!< GPIO A Qualifier Select 1 Register
volatile uint32_t GPAQSEL2; //!< GPIO A Qualifier Select 2 Register
} GPIO_Obj;
//! \brief Defines the general purpose I/O (GPIO) handle
//!
typedef struct GPIO_Obj *GPIO_Handle;
这两种定义方式又有什么区别呢?了
typedef struct GPIO_Obj *GPIO_Handle;这么定义是不是应该去掉一个struct,因为GPIO_Obj就相当于struct _GPIO_Obj_ 了。