XilinxProcessorIPLib/drivers/uartpsv/src/xuartpsv.c
s32 XUartPsv_SetBaudRate(XUartPsv *InstancePtr, u32 BaudRate)
{
	u16 BAUDIDIV_Value;	/* Value for integer baud rate divisor */
	u8 BAUDFDIV_Value;	/* Value for fractional baud rate divisor */
	u32 BAUDDIV_Value;
	u32 CalcBaudRate;	/* Calculated baud rate */
	u32 BaudError;		/* Diff between calculated and requested
						 * baud rate
						 */
	u16 Best_BAUDIDIV = 0U;	/* Best value for integer baud rate
				 * divisor */
	u8 Best_BAUDFDIV = 0U;	/* Best value for fractional baud rate
				 * divisor
				 */
	u32 Best_Error = 0xFFFFFFFFU;
	u32 PercentError;
	u32 InputClk;
	u32 Temp;

	/* Asserts validate the input arguments */
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
	Xil_AssertNonvoid(BaudRate <= (u32)XUARTPSV_MAX_RATE);
	Xil_AssertNonvoid(BaudRate >= (u32)XUARTPSV_MIN_RATE);

	/*
	 * Make sure the baud rate is not impossible by large.
	 * Fastest possible baud rate is Input Clock / 16.
	 */
	if ((BaudRate * 16U) > InstancePtr->Config.InputClockHz) {
		return (s32)XST_UART_BAUD_ERROR;
	}

	InputClk = InstancePtr->Config.InputClockHz;

	/*
	 * Baud rate divisor BAUDDIV = (FUARTCLK/(16xBaud rate))
	 * where FUARTCLK is the UART reference clock frequency
	 * The BAUDDIV is comprised of the integer value (BAUD DIVINT)
	 * and the fractional value (BAUD DIVFRAC)
	 */

	/* Calculate the baud divisor integer value */
	BAUDIDIV_Value = (u16)(InputClk / (BaudRate * 16U));
	BAUDFDIV_Value = (u8)(InputClk % (BaudRate * 16U));

	Best_BAUDIDIV = BAUDIDIV_Value;
	Best_BAUDFDIV = BAUDFDIV_Value;

	if (BAUDFDIV_Value != 0U) {
		/*
		 * Determine the fractional Baud rate divider.
		 * It can be 0 to 63.
		 * Loop through all possible combinations
		 */
		for (BAUDFDIV_Value = 1; BAUDFDIV_Value < 64U;
				BAUDFDIV_Value++) {

			/*
			 * Multiply BAUDDIV_Value with 64 to avoid
			 * fractional values
			 */
			BAUDDIV_Value = (64U * (u32)BAUDIDIV_Value)+BAUDFDIV_Value;

			/*
			 * Calculate the baud rate with BAUDDIV_Value divided
			 * by 64
			 */
			CalcBaudRate = (InputClk / (16U * BAUDDIV_Value)) * 64U;

			/* Avoid unsigned integer underflow */
			if (BaudRate > CalcBaudRate) {
				BaudError = BaudRate - CalcBaudRate;
			}
			else {
				BaudError = CalcBaudRate - BaudRate;
			}

			/*
			 * Find the calculated baud rate closest to requested
			 * baud rate. For the same 'Best_Error', take the maximum
			 * 'BAUDFDIV_Value'.
			 */
			if (Best_Error >= BaudError) {
				Best_BAUDIDIV = BAUDIDIV_Value;
				Best_BAUDFDIV = BAUDFDIV_Value;
				Best_Error = BaudError;
			}
		}

		/* Make sure the best error is not too large. */
		PercentError = (Best_Error * 100U) / BaudRate;
		if (XUARTPSV_MAX_BAUD_ERROR_RATE < PercentError) {
			return (s32)XST_UART_BAUD_ERROR;
		}
	}

	/* Disable the UART */
	XUartPsv_DisableUart(InstancePtr);

	/*
	 * Wait for the end of transmission or reception of the current
	 * character
	 */

	/* Program baud divisor registers */
	XUartPsv_WriteReg(InstancePtr->Config.BaseAddress,
			XUARTPSV_UARTIBRD_OFFSET, Best_BAUDIDIV);
	XUartPsv_WriteReg(InstancePtr->Config.BaseAddress,
			XUARTPSV_UARTFBRD_OFFSET, Best_BAUDFDIV);

	/* As per TRM, do write of LCR after writing to baud rate registers */
	Temp = XUartPsv_ReadReg(InstancePtr->Config.BaseAddress, XUARTPSV_UARTLCR_OFFSET);
	XUartPsv_WriteReg(InstancePtr->Config.BaseAddress,
			XUARTPSV_UARTLCR_OFFSET, Temp);

	/* Enable TX and RX */

	/* Enable the UART */
	XUartPsv_EnableUart(InstancePtr);
	InstancePtr->BaudRate = BaudRate;

	return (s32)XST_SUCCESS;
}
/*****************************************************************************/
/**
*
* Sets the baud rate for the device. Checks the input value for
* validity and also verifies that the requested rate can be configured to
* within the maximum error range specified by XUARTPSV_MAX_BAUD_ERROR_RATE.
* If the provided rate is not possible, the current setting is unchanged.
*
* @param	InstancePtr is a pointer to the XUartPsv instance
* @param	BaudRate to be set
*
* @return
*		- XST_SUCCESS if everything configured as expected
*		- XST_UART_BAUD_ERROR if the requested rate is not available
*	  	  because there was too much error
*
* @note 	None.
*
******************************************************************************/
Sets the baud rate for the device. Checks the input value for
validity and also verifies that the requested rate can be configured to
within the maximum error range specified by XUARTPSV_MAX_BAUD_ERROR_RATE.
If the provided rate is not possible, the current setting is unchanged.
Sets the baud rate for the device . Checks the input value for validity and also verifies that the requested rate can be configured to within the maximum error range specified by XUARTPSV_MAX_BAUD_ERROR_RATE . If the provided rate is not possible the current setting is unchanged .
s32 XUartPsv_SetBaudRate(XUartPsv *InstancePtr, u32 BaudRate)
{
	u16 BAUDIDIV_Value;	
	u8 BAUDFDIV_Value;	
	u32 BAUDDIV_Value;
	u32 CalcBaudRate;	
	u32 BaudError;		
	u16 Best_BAUDIDIV = 0U;	
	u8 Best_BAUDFDIV = 0U;	
	u32 Best_Error = 0xFFFFFFFFU;
	u32 PercentError;
	u32 InputClk;
	u32 Temp;
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
	Xil_AssertNonvoid(BaudRate <= (u32)XUARTPSV_MAX_RATE);
	Xil_AssertNonvoid(BaudRate >= (u32)XUARTPSV_MIN_RATE);
	if ((BaudRate * 16U) > InstancePtr->Config.InputClockHz) {
		return (s32)XST_UART_BAUD_ERROR;
	}
	InputClk = InstancePtr->Config.InputClockHz;
	BAUDIDIV_Value = (u16)(InputClk / (BaudRate * 16U));
	BAUDFDIV_Value = (u8)(InputClk % (BaudRate * 16U));
	Best_BAUDIDIV = BAUDIDIV_Value;
	Best_BAUDFDIV = BAUDFDIV_Value;
	if (BAUDFDIV_Value != 0U) {
		for (BAUDFDIV_Value = 1; BAUDFDIV_Value < 64U;
				BAUDFDIV_Value++) {
			BAUDDIV_Value = (64U * (u32)BAUDIDIV_Value)+BAUDFDIV_Value;
			CalcBaudRate = (InputClk / (16U * BAUDDIV_Value)) * 64U;
			if (BaudRate > CalcBaudRate) {
				BaudError = BaudRate - CalcBaudRate;
			}
			else {
				BaudError = CalcBaudRate - BaudRate;
			}
			if (Best_Error >= BaudError) {
				Best_BAUDIDIV = BAUDIDIV_Value;
				Best_BAUDFDIV = BAUDFDIV_Value;
				Best_Error = BaudError;
			}
		}
		PercentError = (Best_Error * 100U) / BaudRate;
		if (XUARTPSV_MAX_BAUD_ERROR_RATE < PercentError) {
			return (s32)XST_UART_BAUD_ERROR;
		}
	}
	XUartPsv_DisableUart(InstancePtr);
	XUartPsv_WriteReg(InstancePtr->Config.BaseAddress,
			XUARTPSV_UARTIBRD_OFFSET, Best_BAUDIDIV);
	XUartPsv_WriteReg(InstancePtr->Config.BaseAddress,
			XUARTPSV_UARTFBRD_OFFSET, Best_BAUDFDIV);
	Temp = XUartPsv_ReadReg(InstancePtr->Config.BaseAddress, XUARTPSV_UARTLCR_OFFSET);
	XUartPsv_WriteReg(InstancePtr->Config.BaseAddress,
			XUARTPSV_UARTLCR_OFFSET, Temp);
	XUartPsv_EnableUart(InstancePtr);
	InstancePtr->BaudRate = BaudRate;
	return (s32)XST_SUCCESS;
}
s32 XUartPsv_SetBaudRate ( XUartPsv * InstancePtr , u32 BaudRate ) { u16 BAUDIDIV_Value ; u8 BAUDFDIV_Value ; u32 BAUDDIV_Value ; u32 CalcBaudRate ; u32 BaudError ; u16 Best_BAUDIDIV = 0U ; u8 Best_BAUDFDIV = 0U ; u32 Best_Error = 0xFFFFFFFFU ; u32 PercentError ; u32 InputClk ; u32 Temp ; Xil_AssertNonvoid ( InstancePtr != NULL ) ; Xil_AssertNonvoid ( InstancePtr -> IsReady == XIL_COMPONENT_IS_READY ) ; Xil_AssertNonvoid ( BaudRate <= ( u32 ) XUARTPSV_MAX_RATE ) ; Xil_AssertNonvoid ( BaudRate >= ( u32 ) XUARTPSV_MIN_RATE ) ; if ( ( BaudRate * 16U ) > InstancePtr -> Config . InputClockHz ) { return ( s32 ) XST_UART_BAUD_ERROR ; } InputClk = InstancePtr -> Config . InputClockHz ; BAUDIDIV_Value = ( u16 ) ( InputClk / ( BaudRate * 16U ) ) ; BAUDFDIV_Value = ( u8 ) ( InputClk % ( BaudRate * 16U ) ) ; Best_BAUDIDIV = BAUDIDIV_Value ; Best_BAUDFDIV = BAUDFDIV_Value ; if ( BAUDFDIV_Value != 0U ) { for ( BAUDFDIV_Value = 1 ; BAUDFDIV_Value < 64U ; BAUDFDIV_Value ++ ) { BAUDDIV_Value = ( 64U * ( u32 ) BAUDIDIV_Value ) + BAUDFDIV_Value ; CalcBaudRate = ( InputClk / ( 16U * BAUDDIV_Value ) ) * 64U ; if ( BaudRate > CalcBaudRate ) { BaudError = BaudRate - CalcBaudRate ; } else { BaudError = CalcBaudRate - BaudRate ; } if ( Best_Error >= BaudError ) { Best_BAUDIDIV = BAUDIDIV_Value ; Best_BAUDFDIV = BAUDFDIV_Value ; Best_Error = BaudError ; } } PercentError = ( Best_Error * 100U ) / BaudRate ; if ( XUARTPSV_MAX_BAUD_ERROR_RATE < PercentError ) { return ( s32 ) XST_UART_BAUD_ERROR ; } } XUartPsv_DisableUart ( InstancePtr ) ; XUartPsv_WriteReg ( InstancePtr -> Config . BaseAddress , XUARTPSV_UARTIBRD_OFFSET , Best_BAUDIDIV ) ; XUartPsv_WriteReg ( InstancePtr -> Config . BaseAddress , XUARTPSV_UARTFBRD_OFFSET , Best_BAUDFDIV ) ; Temp = XUartPsv_ReadReg ( InstancePtr -> Config . BaseAddress , XUARTPSV_UARTLCR_OFFSET ) ; XUartPsv_WriteReg ( InstancePtr -> Config . BaseAddress , XUARTPSV_UARTLCR_OFFSET , Temp ) ; XUartPsv_EnableUart ( InstancePtr ) ; InstancePtr -> BaudRate = BaudRate ; return ( s32 ) XST_SUCCESS ; }
Sets the baud rate for the device.
Sets the baud rate for the device .
/* Value for integer baud rate divisor */ /* Value for fractional baud rate divisor */ /* Calculated baud rate */ /* Diff between calculated and requested
						 * baud rate
						 */ /* Best value for integer baud rate
				 * divisor */ /* Best value for fractional baud rate
				 * divisor
				 */ /* Asserts validate the input arguments */ /*
	 * Make sure the baud rate is not impossible by large.
	 * Fastest possible baud rate is Input Clock / 16.
	 */ /*
	 * Baud rate divisor BAUDDIV = (FUARTCLK/(16xBaud rate))
	 * where FUARTCLK is the UART reference clock frequency
	 * The BAUDDIV is comprised of the integer value (BAUD DIVINT)
	 * and the fractional value (BAUD DIVFRAC)
	 */ /* Calculate the baud divisor integer value */ /*
		 * Determine the fractional Baud rate divider.
		 * It can be 0 to 63.
		 * Loop through all possible combinations
		 */ /*
			 * Multiply BAUDDIV_Value with 64 to avoid
			 * fractional values
			 */ /*
			 * Calculate the baud rate with BAUDDIV_Value divided
			 * by 64
			 */ /* Avoid unsigned integer underflow */ /*
			 * Find the calculated baud rate closest to requested
			 * baud rate. For the same 'Best_Error', take the maximum
			 * 'BAUDFDIV_Value'.
			 */ /* Make sure the best error is not too large. */ /* Disable the UART */ /*
	 * Wait for the end of transmission or reception of the current
	 * character
	 */ /* Program baud divisor registers */ /* As per TRM, do write of LCR after writing to baud rate registers */ /* Enable TX and RX */ /* Enable the UART */