microblaze 串口学习·1
microblaze 串口学习·1
串口初始化函数1int XUartLite_Initialize(XUartLite *InstancePtr, u16 DeviceId)
/****************************************************************************/
/**
*
* Initialize a XUartLite instance. The receive and transmit FIFOs of the
* UART are not flushed, so the user may want to flush them. The hardware
* device does not have any way to disable the receiver such that any valid
* data may be present in the receive FIFO. This function disables the UART
* interrupt. The baudrate and format of the data are fixed in the hardware
* at hardware build time.
*
* @param InstancePtr is a pointer to the XUartLite instance.
* @param DeviceId is the unique id of the device controlled by this
* XUartLite instance. Passing in a device id associates the
* generic XUartLite instance to a specific device, as chosen by
* the caller or application developer.
*
* @return
* - XST_SUCCESS if everything starts up as expected.
* - XST_DEVICE_NOT_FOUND if the device is not found in the
* configuration table.
*
* @note None.
*
*****************************************************************************/
int XUartLite_Initialize(XUartLite *InstancePtr, u16 DeviceId)
{
XUartLite_Config *ConfigPtr;
/*
* Assert validates the input arguments
*/
Xil_AssertNonvoid(InstancePtr != NULL);
/*
* Lookup the device configuration in the configuration table. Use this
* configuration info when initializing this component.
*/
ConfigPtr = XUartLite_LookupConfig(DeviceId);
if (ConfigPtr == (XUartLite_Config *)NULL) {
return XST_DEVICE_NOT_FOUND;
}
return XUartLite_CfgInitialize(InstancePtr, ConfigPtr,
ConfigPtr->RegBaseAddr);
}
#1 串口初始化函数的参数1 名为XUartLite的结构体指针
/**
* The XUartLite driver instance data. The user is required to allocate a
* variable of this type for every UART Lite device in the system. A pointer
* to a variable of this type is then passed to the driver API functions.
*/
typedef struct {
XUartLite_Stats Stats; /* Component Statistics */
UINTPTR RegBaseAddress; /* Base address of registers */
u32 IsReady; /* Device is initialized and ready */
XUartLite_Buffer SendBuffer;
XUartLite_Buffer ReceiveBuffer;
XUartLite_Handler RecvHandler;
void *RecvCallBackRef; /* Callback ref for recv handler */
XUartLite_Handler SendHandler;
void *SendCallBackRef; /* Callback ref for send handler */
} XUartLite;
1-1 函数参数1 结构体指针 XUartLite_Stats Stats;
结构体元素1 外设状态信息
结构体种定义了外设的信息 由XUartLite_Stats定义的结构体
/**
* Statistics for the XUartLite driver
*/
typedef struct {
u32 TransmitInterrupts; /**< Number of transmit interrupts */
u32 ReceiveInterrupts; /**< Number of receive interrupts */
u32 CharactersTransmitted; /**< Number of characters transmitted */
u32 CharactersReceived; /**< Number of characters received */
u32 ReceiveOverrunErrors; /**< Number of receive overruns */
u32 ReceiveParityErrors; /**< Number of receive parity errors */
u32 ReceiveFramingErrors; /**< Number of receive framing errors */
} XUartLite_Stats;
结构体种定义的全部为32bit的整形量,用于指示串口的状态太
1-2 结构体元素2 外设寄存器起始地址 UINTPTR RegBaseAddress;
UINTPTR为整形指针,指示的是外设寄存器的起始地址,这个起始地址可以在vivado的工程中查到
1-3 外设ready标志 device Ready u32 IsReady;
没用到以后再研究
1-4 输出缓冲堆结构体 XUartLite_Buffer SendBuffer;
/**
* The following data type is used to manage the buffers that are handled
* when sending and receiving data in the interrupt mode. It is intended
* for internal use only.
*/
typedef struct {
u8 *NextBytePtr;
unsigned int RequestedBytes;
unsigned int RemainingBytes;
} XUartLite_Buffer;
结构体种第一个元素是一个指针 指向u8的元素。本质上是一个地址。
第二个元素是请求数量,
第三个元素是未处理的请求数量
1-5 输入缓冲结构体 XUartLite_Buffer ReceiveBuffer;
/**
* The following data type is used to manage the buffers that are handled
* when sending and receiving data in the interrupt mode. It is intended
* for internal use only.
*/
typedef struct {
u8 *NextBytePtr;
unsigned int RequestedBytes;
unsigned int RemainingBytes;
} XUartLite_Buffer;
结构体种第一个元素是一个指针 指向u8的元素。本质上是一个地址。
第二个元素是请求数量,
第三个元素是未处理的请求数量
1-6 接收函数指针 XUartLite_Handler RecvHandler;
参数类型由以下typedef定义
这是一个返回值为空,有两个参数的函数的指针
typedef void (*XUartLite_Handler)(void *CallBackRef, unsigned int ByteCount);
第一个参数是一个空指针,看名字应该是指向回调函数,void是一个无类型的指针,可以被任何指针赋值
第二个参数是接收到的数据数量
1-7 接收回调函数void RecvCallBackRef; / Callback ref for recv handler */
一个无类型指针,应该是用于指向接收回调函数
1-8 发送函数指针 XUartLite_Handler SendHandler;
参数类型由以下typedef定义
这是一个返回值为空,有两个参数的函数的指针
typedef void (*XUartLite_Handler)(void *CallBackRef, unsigned int ByteCount);
第一个参数是一个空指针,看名字应该是指向回调函数,void是一个无类型的指针,可以被任何指针赋值
第二个参数是接收到的数据数量
1-9发送回调函数指针 void *SendCallBackRef;
一个无类型指针,应该是用于指向发送回调函数
2 串口初始化函数的参数2 第2个参数是外设ID
是一个16位的整形量,通常情况下是一个常数,这个常数由vivado生成外设硬件资源后,导出hardwareConfig时应该就已经确定了。
可以在vitis生成的工程中在xparameters.h文件种可以找到外设的ID
看文件的话这个ID就是看同类型的外设在总线上挂了多少个,如果外设都只有一个的话。目前生成的工程ID都是0,当由两个同类型外设的时候,才会出现不同的id。
3 总结
总结,在初始化的过程中,需要向初始化函数提供两个参数。
第一个参数有大量的功能性参数可供配置或查询,对于同类的外设来说,大家需要配置的参数类型都是一样的。
第二个参数实际上决定的是配置过程中的地址偏移,因为实际上所有的外设都是挂在AXI总线上执行的,对于CPU来说所有对于外设的操作都可以看作是对特定地址reg的读写操作。
- 点赞
- 收藏
- 关注作者
评论(0)