ZYNQ UARTLITE学习小结

经过一天的踩坑和学习,大概理解了ZYNQ 中PS调用UARTLITE的IP核的用法。
记录一下坑点

  1. 最好用xil_printf打印,用printf打印有时候打印不出来。调试的时候都没有打印,弄的我怀疑人生。
  2. 轮询模式和中断模式的应用中,假如发送开启了中断模式,那么接收也必须开启中断模式,接收不能用轮询的模式。
    基本上都是参考xilinx的例程来的,只是在调试的过程中,尝试了一下发送中断,接收轮询的方式,结果踩到坑里,很久也没出来。
    轮询模式代码如下:
	Status = XUartLite_Initialize(UartLiteInstPtr, UartLiteDeviceId);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	Status = XUartLite_SelfTest(UartLiteInstPtr);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
		SendBuffer[Index] = Index;
		RecvBuffer[Index] = 0;
	}

	XUartLite_Send(UartLiteInstPtr, SendBuffer, TEST_BUFFER_SIZE);


	usleep(1000);
	xil_printf("So%d,%d ",TotalSentCount,cnttest);
	cnttest=0;
	usleep(1000);
	while (1) {
		ReceivedCount += XUartLite_Recv(UartLiteInstPtr,
							   RecvBuffer + ReceivedCount,
							   TEST_BUFFER_SIZE - ReceivedCount);
		if (ReceivedCount == TEST_BUFFER_SIZE) {
			for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
				if(RecvBuffer[Index] == SendBuffer[Index]) {
					if(Index == TEST_BUFFER_SIZE-1)
						xil_printf("rcv successful,%d\r\n ",ReceivedCount);
				}
				else
					xil_printf("rcv[%d] fail\r\n",Index);
			}
			ReceivedCount = 0;
		}
	}

中断模式的代码如下:

Status = XUartLite_Initialize(UartLiteInstPtr, UartLiteDeviceId);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	Status = XUartLite_SelfTest(UartLiteInstPtr);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	Status = UartLiteSetupIntrSystem(IntcInstancePtr,
					 UartLiteInstPtr,
					 UartLiteIntrId);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	XUartLite_SetSendHandler(UartLiteInstPtr, UartLiteSendHandler,
							 UartLiteInstPtr);
	XUartLite_SetRecvHandler(UartLiteInstPtr, UartLiteRecvHandler,
							 UartLiteInstPtr);
	XUartLite_EnableInterrupt(UartLiteInstPtr);


	for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
		SendBuffer[Index] = Index;
		RecvBuffer[Index] = 0;
	}

	XUartLite_Recv(UartLiteInstPtr, RecvBuffer, TEST_BUFFER_SIZE*2);
	XUartLite_Send(UartLiteInstPtr, SendBuffer, TEST_BUFFER_SIZE);

	while ((TotalSentCount != TEST_BUFFER_SIZE)) {
	}
	usleep(1000);
	xil_printf("So%d,%d ",TotalSentCount,cnttest);
	cnttest=0;
	usleep(1000);
	while (1) {

		sleep(1);
		xil_printf("R%d,%d\r\n ",ReceivedCount,cnttest);
		if (ReceivedCount == TEST_BUFFER_SIZE*2) {
			XUartLite_Recv(UartLiteInstPtr, RecvBuffer, TEST_BUFFER_SIZE*2);
			ReceivedCount = 0;
		}
	}

你可能感兴趣的:(ZYNQ,学习)