网站建设和美工湘潭网站建设
文章目录
- 1 实验任务
- 2 系统框图
- 3 软件设计
1 实验任务
本实验任务是使用PS端UART控制器,完成串口中断数据环回的功能。
2 系统框图
3 软件设计
注意事项:
- 同时使能XUARTPS_IXR_RXOVR 和XUARTPS_IXR_TOUT,以支持不定长数据接收;
- 对于XUartPs_Recv函数
- (1) 禁用所有中断
- (2) 使用XUartPs_ReceiveBuffer函数从RX FIFO中读取数据
- (3) 恢复中断使能
- (4) 返回实际读取的个数;
- 接收超时是个好东西,哈哈哈。
/************************** Include Files ***********************************/
#include "xparameters.h"
#include "xuartps.h"
#include "xscugic.h"
#include "xil_exception.h"
#include "stdio.h"/************************** Constant Definitions ****************************/
#define UART_DEVICE_ID XPAR_XUARTPS_0_DEVICE_ID
#define INTC_DEVICE_ID XPAR_SCUGIC_SINGLE_DEVICE_ID
#define UART_INTR_ID XPAR_XUARTPS_1_INTR#define BUFFER_SIZE 64 // 接收缓冲区大小
#define FIFO_TRIGGER_LEVEL 32 // FIFO触发阈值
#define RECV_TIMEOUT 4 // 接收超时时间(单位:波特率时钟周期)/************************** Function Prototypes *****************************/
s32 UartPsInit(XUartPs *UartPsInstPtr, XUartPsFormat* UartFormatPtr);
s32 SetupInterruptSystem(XScuGic *IntcInstPtr, XUartPs *UartPsInstPtr);
void UartIntrHandler(void *CallBackRef);/************************** Variable Definitions ****************************/
XUartPs UartInst;
XScuGic IntcInst;u8 RxBuffer[BUFFER_SIZE] = { 0 }; // 接收缓冲区int RxDataLength = 0; // 接收到的数据长度XUartPsFormat UartFormat = {XUARTPS_DFT_BAUDRATE, // 115200XUARTPS_FORMAT_8_BITS,XUARTPS_FORMAT_NO_PARITY,XUARTPS_FORMAT_1_STOP_BIT
};
/************************** Function Implementation *************************/int main()
{//s32 Status;// 初始化UARTStatus = UartPsInit(&UartInst, &UartFormat);if (Status != XST_SUCCESS) {return XST_FAILURE;}// 设置中断系统Status = SetupInterruptSystem(&IntcInst, &UartInst);if (Status != XST_SUCCESS) {return XST_FAILURE;}// 主循环while(1){;}//return 0;
}s32 UartPsInit(XUartPs *UartInstPtr, XUartPsFormat* UartFormatPtr)
{//s32 Status;XUartPs_Config *UartConfigPtr;// 查找UART配置UartConfigPtr = XUartPs_LookupConfig(UART_DEVICE_ID);if(NULL == UartConfigPtr){return XST_FAILURE;}// 初始化UARTStatus = XUartPs_CfgInitialize(UartInstPtr, UartConfigPtr, UartConfigPtr->BaseAddress);if (Status != XST_SUCCESS) {return XST_FAILURE;}// 设置UART数据格式XUartPs_SetDataFormat(UartInstPtr, UartFormatPtr);// 设置UART操作模式XUartPs_SetOperMode(UartInstPtr, XUARTPS_OPER_MODE_NORMAL);// 设置接收FIFO触发阈值XUartPs_SetFifoThreshold(UartInstPtr, FIFO_TRIGGER_LEVEL);// 设置接收超时XUartPs_SetRecvTimeout(UartInstPtr, RECV_TIMEOUT);// 设置中断掩码,使能FIFO触发中断和接收超时中断XUartPs_SetInterruptMask(UartInstPtr, XUARTPS_IXR_RXOVR | XUARTPS_IXR_TOUT);//return XST_SUCCESS;
}s32 SetupInterruptSystem(XScuGic *IntcInstPtr, XUartPs *UartInstPtr)
{//s32 Status;XScuGic_Config *IntcConfigPtr;// 初始化中断控制器GICIntcConfigPtr = XScuGic_LookupConfig(INTC_DEVICE_ID);if (NULL == IntcConfigPtr){return XST_FAILURE;}Status = XScuGic_CfgInitialize(IntcInstPtr, IntcConfigPtr, IntcConfigPtr->CpuBaseAddress);if (Status != XST_SUCCESS){return XST_FAILURE;}// 注册异常处理程序Xil_ExceptionInit();Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler)XScuGic_InterruptHandler, IntcInstPtr);Xil_ExceptionEnable();// 连接UART中断处理程序XScuGic_Connect(IntcInstPtr, UART_INTR_ID, (Xil_InterruptHandler)UartIntrHandler, (void *)UartInstPtr);// 使能UART中断XScuGic_Enable(IntcInstPtr, UART_INTR_ID);//return XST_SUCCESS;
}void UartIntrHandler(void *CallBackRef)
{//XUartPs* UartInstPtr = (XUartPs*)CallBackRef;u32 IsrStatus;// 读取中断状态IsrStatus = XUartPs_ReadReg(UartInstPtr->Config.BaseAddress, XUARTPS_IMR_OFFSET);IsrStatus &= XUartPs_ReadReg(UartInstPtr->Config.BaseAddress, XUARTPS_ISR_OFFSET);// 处理FIFO触发中断if ((IsrStatus & (u32)XUARTPS_IXR_RXOVR) != (u32)0) {// 读取FIFO中的数据RxDataLength = XUartPs_Recv(UartInstPtr, RxBuffer, BUFFER_SIZE);// 清除中断状态XUartPs_WriteReg(UartInstPtr->Config.BaseAddress, XUARTPS_ISR_OFFSET, XUARTPS_IXR_RXOVR);//printf("Rx Trigger, Receive %u Bytes.\n", RxDataLength);}// 处理接收超时中断if ((IsrStatus & (u32)XUARTPS_IXR_TOUT) != (u32)0) {// 读取FIFO中剩余的数据RxDataLength = XUartPs_Recv(UartInstPtr, RxBuffer, BUFFER_SIZE);// 清除中断状态XUartPs_WriteReg(UartInstPtr->Config.BaseAddress, XUARTPS_ISR_OFFSET, XUARTPS_IXR_TOUT);//printf("Rx Timeout, Receive %u Bytes.\n", RxDataLength);}// 如果有数据接收到if (RxDataLength > 0) {// 处理接收到的数据XUartPs_Send(UartInstPtr, RxBuffer, RxDataLength);//RxDataLength = 0;}//return;
}