古色古香的网站模板,项目建设备案网站,企业网络营销成功案例,纪检网站建设动态主题文章目录 1 实验任务2 系统框图3 软件设计 1 实验任务
本实验使用中断方式实现UART串口数据的连续发送。
2 系统框图
参见6.1。
3 软件设计
注意事项#xff1a;
系统上电、程序下载后#xff0c;此时TX FIFO虽然为空#xff0c;但并不会触发空中断#xff1b;空中断… 文章目录 1 实验任务2 系统框图3 软件设计 1 实验任务
本实验使用中断方式实现UART串口数据的连续发送。
2 系统框图
参见6.1。
3 软件设计
注意事项
系统上电、程序下载后此时TX FIFO虽然为空但并不会触发空中断空中断的触发前提是FIFO中有数据且被读空即FIFO中的最后一个数据被读出This event is triggered whenever the final word is removed from the transmit FIFOXUartPs_Send函数 1函数功能发送数据到TX FIFO2函数实现 1禁用TX FIFO空中断和TX FIFO满中断2设置缓冲区参数包括请求发送的字节数、剩余待发送的字节数和指向下一个要发送的字节的指针3调用XUartPs_SendBuffer函数该函数完成数据发送功能并返回实际发送的字节数4接收XUartPs_SendBuffer函数的返回值并返回 XUartPs_SendBuffer函数 1函数功能以轮询或中断驱动模式将缓冲区数据发送到TX FIFO2函数实现 1发送数据到TX FIFO如果TX FIFO未满且缓冲区中还有数据未发送则将数据写入TX FIFOOtherwise put bytes into the TX FIFO unil it is full, or all of the data has been put into the FIFO2更新缓冲区指针和剩余字节数3使能TX FIFO空中断注意如果接收中断已启用则启用TX FIFO空中断这也是为何在UartInit函数中使能看似无关的XUARTPS_IXR_RXOVR中断的原因 疑问1为什么在启用接收中断的前提下才能启用TX FIFO空中断疑问2为什么仅启用了TX FIFO空中断TX FIFO满中断在XUartPs_Send也被禁用了为何未被重新启用 4返回实际发送的字节数 程序设计思路 1在程序下载后假设上一次数据已发送完毕SendComplete置12将每一轮的首次数据发送放在while循环中这样TxBuffer中的数据才能一轮一轮的循环发送起来刚开始把首次数据发送放在while循环外边在第一轮的最后一次数据发送完毕并触发空中断后SendComplete置1然后就没有然后了…。
/************************** Include Files ***********************************/
#include xparameters.h
#include xuartps.h
#include xscugic.h
#include xil_exception.h
#include stdio.h
#include sleep.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 256 // 发送缓冲区大小
#define FIFO_TRIGGER_LEVEL 32 // FIFO触发阈值
#define RECV_TIMEOUT 4 // 接收超时时间单位波特率时钟周期/************************** Function Prototypes *****************************/
s32 c(XUartPs *UartPsInstPtr, XUartPsFormat* UartFormatPtr);
s32 SetupInterruptSystem(XScuGic *IntcInstPtr, XUartPs *UartPsInstPtr);
void UartIntrHandler(void *CallBackRef);/************************** Variable Definitions ****************************/
XUartPs UartInst;
XScuGic IntcInst;u8 TxBuffer[BUFFER_SIZE] { 0 }; // 接收缓冲区int RxDataLength 0; // 接收到的数据长度XUartPsFormat UartFormat {XUARTPS_DFT_BAUDRATE, // 115200XUARTPS_FORMAT_8_BITS,XUARTPS_FORMAT_NO_PARITY,XUARTPS_FORMAT_1_STOP_BIT
};// 发送状态
u32 TotalBytesSent; // 已发送的字节数
int SendComplete; // 发送完成标志
/************************** Function Implementation *************************/int main()
{//s32 Status;u32 BytesSent;//for (int i 0; i BUFFER_SIZE; i) {TxBuffer[i] (u8)i; // 填充从 0 开始的递增数}// 初始化UARTStatus UartPsInit(UartInst, UartFormat);if (Status ! XST_SUCCESS) {return XST_FAILURE;}// 设置中断系统Status SetupInterruptSystem(IntcInst, UartInst);if (Status ! XST_SUCCESS) {return XST_FAILURE;}// 主循环SendComplete 1;while(1){if (SendComplete 1) {TotalBytesSent 0;SendComplete 0;sleep(3);// 启动第一次发送BytesSent XUartPs_Send(UartInst, TxBuffer, BUFFER_SIZE);TotalBytesSent BytesSent;}}//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_TXEMPTY);//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_TXEMPTY) ! (u32)0) {XUartPs_WriteReg(UartInstPtr-Config.BaseAddress, XUARTPS_ISR_OFFSET, XUARTPS_IXR_TXEMPTY);if (TotalBytesSent BUFFER_SIZE) {// 继续发送剩余的数据u32 BytesSent XUartPs_Send(UartInstPtr, TxBuffer[TotalBytesSent], BUFFER_SIZE - TotalBytesSent);TotalBytesSent BytesSent;}else {// 所有数据已发送完毕SendComplete 1;
// xil_printf(Config file sent successfully in polled mode!\n);}}//return;
}