zynq 的uart(ps)

初始化

int serial_init(void)
{
	int Status;
	XUartPs_Config *Config;

	Config = XUartPs_LookupConfig(UART_DEVICE_ID);
	if (NULL == Config) {
		return XST_FAILURE;
	}
	Status = XUartPs_CfgInitialize(&Uart_PS, Config, Config->BaseAddress);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	/* Use Normal mode. */
	XUartPs_SetOperMode(&Uart_PS, XUARTPS_OPER_MODE_NORMAL);
	/* Set uart mode Baud Rate 115200, 8bits, no parity, 1 stop bit */
	XUartPs_SetDataFormat(&Uart_PS, &UartFormat) ;
	/*Set receiver FIFO interrupt trigger level, here set to 1*/

#if 0         // 不使用中断
	XUartPs_SetFifoThreshold(&Uart_PS,1) ;
	/* Enable the receive FIFO trigger level interrupt and empty interrupt for the device */
	XUartPs_SetInterruptMask(&Uart_PS,XUARTPS_IXR_RXOVR|XUARTPS_IXR_TXEMPTY);

	SetupInterruptSystem(&IntcInstPtr, &Uart_PS, UART_INT_IRQ_ID) ;
#endif

	return XST_SUCCESS;
}

发送和接收

int Uart_Send(u8 *sendbuf, int length)
{
	int SentCount = 0;

	while (SentCount < length) {
		SentCount += XUartPs_Send(&Uart_PS, &sendbuf[SentCount], 1);
	}

	return SentCount;
}

#define MAX_BUF_LEN		64
static char _uartBuf[MAX_BUF_LEN + 4];


u32 serial_send(u8 *pBuf, u32 len)
{
	 return XUartPs_Send(&Uart_PS, pBuf, len);
}

u32 serial_get(u8 *pBuf, int len)
{
	return XUartPs_Recv(&Uart_PS, pBuf, len);
}

void serial_puts(const char *fmt, ...)
{
	// Generate formatted string.
	s16 count;
	va_list vArgList;
	va_start(vArgList, fmt);
	count = vsnprintf(_uartBuf, MAX_BUF_LEN, fmt, vArgList);
	va_end(vArgList);

	if (MAX_BUF_LEN < count)
	{
		count = MAX_BUF_LEN;
	}

	serial_send(_uartBuf, count);
}

需要的宏

#define UART_DEVICE_ID      XPAR_XUARTPS_0_DEVICE_ID
#define INTC_DEVICE_ID		XPAR_SCUGIC_SINGLE_DEVICE_ID
#define UART_INT_IRQ_ID		XPAR_XUARTPS_1_INTR

静态变量

const XUartPsFormat UartFormat =
{
		115200,
		XUARTPS_FORMAT_8_BITS,
		XUARTPS_FORMAT_NO_PARITY,
		XUARTPS_FORMAT_1_STOP_BIT
};


XUartPs Uart_PS;		/* Instance of the UART Device */
XScuGic IntcInstPtr ;

猜你喜欢

转载自blog.csdn.net/qq_21353001/article/details/89645206