UART 串口 代码
uart_cfg=XUartPs_LookupConfig(uart_id);
XUartPs_CfgInitialize(&uart,uart_cfg,uart_cfg->BaseAddress);
XUartPs_SetBaudRate(&uart,baud_rate); 设置波特率
XUartPs_SetHandler(&uart,(XUartPs_Handler)uart1handler,&uart); UART有多种中断,触发对应的中断
intrmask=XUARTPS_IXR_TOUT;
XUartPs_SetInterruptMask(&uart,intrmask);
XUartPs_SetOperMode(&uart,normal_mode);
XUartPs_SetRecvTimeout(&uart,8); 间隔4*8共32个bit的时间内没有接收到数据 触发
XUartPs_Recv(&uart,recvbuf,32);// 读出32个数据到 recbuf
void XUartPs_SetFifoThreshold(XUartPs *InstancePtr, u8 TriggerLevel) 设置阈值,FIFO内数据达到阈值触发(产生事件)
/* * main.c * * Created on: 2022年2月23日 * Author: lht */ #include "stdio.h" #include "xparameters.h" #include "xscugic.h" #include "xuartps.h" #include "xil_exception.h" #include "xil_printf.h" #include <stdlib.h> #define uart_id XPAR_PS7_UART_1_DEVICE_ID #define baud_rate XUARTPS_DFT_BAUDRATE #define normal_mode XUARTPS_OPER_MODE_NORMAL #define gic_id XPAR_PS7_SCUGIC_0_DEVICE_ID #define uart1_intr XPAR_PS7_UART_1_INTR #define BUFFER_SIZE 1024 unsigned char recvbuf[1024]; u32 RecvCnt; u32 codecnt; //static u16 recvbuf_and[512]; static u8 RecvBuffer[BUFFER_SIZE]; u16 *recvbuf_and=NULL; u16 data_len; u8 sum_check; u8 xor_check; int right=0; /*typedef struct st_type //柔性数组 { int j; u16 recvbuf_and[]; }type_a; */ u32 TotalRecvCnt; u8 *RecvBufferPtr; XUartPs uart; XUartPs_Config *uart_cfg; XScuGic scugic; XScuGic_Config * gic_cfg; void init_uart(); void init_intr(); void uart1handler(void *CallBackRef, u32 Event,u32 EventData); int main(){ printf("hi\n\r"); init_intr(); init_uart(); while(1){ //int i=0; //for(i=0;i<codecnt;i++){ // xil_printf("i:%x\r\n",recvbuf_and[i]); //} } return 0; } void init_uart(){ // XUartPs uart; // XUartPs_Config *uart_cfg; u32 intrmask=0; //RecvBufferPtr=RecvBuffer; uart_cfg=XUartPs_LookupConfig(uart_id); XUartPs_CfgInitialize(&uart,uart_cfg,uart_cfg->BaseAddress); XUartPs_SetBaudRate(&uart,baud_rate); XUartPs_SetHandler(&uart,(XUartPs_Handler)uart1handler,&uart); intrmask=XUARTPS_IXR_TOUT|XUARTPS_IXR_RXOVR; XUartPs_SetInterruptMask(&uart,intrmask); XUartPs_SetOperMode(&uart,normal_mode); XUartPs_SetFifoThreshold(&uart,32); XUartPs_SetRecvTimeout(&uart,8); //XUartPs_Recv(&uart,recvbuf,64);// } void init_intr(){ // XScuGic scugic; // XScuGic_Config * gic_cfg; gic_cfg=XScuGic_LookupConfig(gic_id); XScuGic_CfgInitialize(&scugic,gic_cfg,gic_cfg->CpuBaseAddress); Xil_ExceptionInit(); Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,(Xil_ExceptionHandler)XScuGic_InterruptHandler,&scugic); Xil_ExceptionEnable(); XScuGic_Connect(&scugic,uart1_intr,(Xil_ExceptionHandler)XUartPs_InterruptHandler,&uart); XScuGic_Enable(&scugic,uart1_intr); } void uart1handler(void *CallBackRef, u32 Event,u32 EventData){ //u32 recvcnt; if(Event==XUARTPS_EVENT_RECV_DATA){ /* 清除中断标志 */ XUartPs_WriteReg(uart_cfg->BaseAddress, XUARTPS_ISR_OFFSET, XUARTPS_IXR_RXOVR) ; XUartPs_Recv(&uart, RecvBuffer, 500) ; //设置为最大,这样有多少就会接收多少 //RecvCnt += EventData ; //RecvBufferPtr += EventData; xil_printf("1\r\n"); } else if(Event==XUARTPS_EVENT_RECV_TOUT){ int i; int j; int right=0; recvbuf_and = (u16 *) malloc(codecnt * sizeof(u16)); /* 清除中断标志 */ XUartPs_WriteReg(uart_cfg->BaseAddress, XUARTPS_ISR_OFFSET, XUARTPS_IXR_TOUT) ; XUartPs_Recv(&uart, RecvBuffer, 500) ; //RecvCnt += EventData ; if((RecvBuffer[0]==0x12) && (RecvBuffer[1]==0x34)){ data_len=(((RecvBuffer[2])&0x00ff)<<8|(RecvBuffer[3]&0x00ff)); for(j=2;j<(data_len+6);j++){ sum_check+=RecvBuffer[j]; xor_check^=RecvBuffer[j]; //printf("RecvBuffer[j]=%d\n\r",RecvBuffer[j]); //printf("j=%d\n\r",j); } /* printf("RecvBuffer[1]=%d\n\r",RecvBuffer[1]); printf("RecvBuffer[2]=%d\n\r",RecvBuffer[2]); printf("RecvBuffer[3]=%d\n\r",RecvBuffer[3]); printf("DATA_LEN=%d\n\r",data_len); printf("SUM_CHECK=%d\n\r",sum_check); printf("XOR_CHECK=%d\n\r",xor_check); printf("RecvBuffer[4+data_len]=%d\n\r",RecvBuffer[4+data_len]); printf("RecvBuffer[5+data_len]=%d\n\r",RecvBuffer[5+data_len]); printf("RecvBuffer[6+data_len]=%d\n\r",RecvBuffer[6+data_len]); printf("RecvBuffer[7+data_len]=%d\n\r",RecvBuffer[7+data_len]); */ if((sum_check==RecvBuffer[4+data_len]) && (xor_check==RecvBuffer[5+data_len])){ right=1; printf("right=%d\n\r",right); right=0; if((RecvBuffer[6+data_len]==0x43) && (RecvBuffer[7+data_len]==0x21) ){ codecnt=data_len/2; for(i=2;i<codecnt+2;i++){ recvbuf_and[i-2]=(((RecvBuffer[2*i])&0x00ff)<<8|(RecvBuffer[2*i+1]&0x00ff)); printf("recvbuf_and[i]=%d\n\r",recvbuf_and[i-2]); printf("i=%d\n\r",i-2); } } else{ printf("codecnt=%d\n\r",codecnt); } } else{ printf("right=%d\n\r",right); } } else { for(i=0;i<BUFFER_SIZE;i++){ RecvBuffer[i]=0; } } xil_printf("2:%d\r\n",RecvCnt); xil_printf("2:%d\r\n",data_len); sum_check=0; xor_check=0; RecvCnt=0; data_len=0; //recvcnt=EventData; // if(recvcnt==8 && recvbuf[0]==0x55 && recvbuf[1]==0x55){ // printf("recved frame1\n\r"); //} } }