XilinxProcessorIPLib/drivers/uartns550/src/xuartns550.c
int XUartNs550_SetBaudRate(XUartNs550 *InstancePtr, u32 BaudRate)
{

	u32 BaudLSB;
	u32 BaudMSB;
	u32 LcrRegister;
	u32 Divisor;
	u32 TargetRate;
	u32 Error;
	u32 PercentError;

	/*
	 * Assert validates the input arguments
	 */
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);

	/*
	 * Determine what the divisor should be to get the specified baud
	 * rater based upon the input clock frequency and a baud clock prescaler
	 * of 16
	 */
	Divisor = ((InstancePtr->InputClockHz +((BaudRate * 16UL)/2)) /
			(BaudRate * 16UL));
	/*
	 * check for too much error between the baud rate that will be generated
	 * using the divisor and the expected baud rate, integer division also
	 * truncates always positive
	 */
	TargetRate = Divisor * BaudRate * 16UL;
	if (TargetRate < InstancePtr->InputClockHz) {
		Error = InstancePtr->InputClockHz - TargetRate;
	} else {
		Error = TargetRate - InstancePtr->InputClockHz;
	}
	/*
	 * Error has total error now compute the percentage multiplied by 100 to
	 * avoid floating point calculations, should be less than 3% as per
	 * RS-232 spec
	 */
	PercentError = (Error * 100UL) / InstancePtr->InputClockHz;
	if (PercentError > XUN_MAX_BAUD_ERROR_RATE) {
		return XST_UART_BAUD_ERROR;

	}

	/*
	 * Get the least significant and most significant bytes of the divisor
	 * so they can be written to 2 byte registers
	 */
	BaudLSB = Divisor & XUN_DIVISOR_BYTE_MASK;
	BaudMSB = (Divisor >> 8) & XUN_DIVISOR_BYTE_MASK;

	/*
	 * Save the baud rate in the instance so that the get baud rate function
	 * won't have to calculate it from the divisor
	 */
	InstancePtr->BaudRate = BaudRate;

	/*
	 * Get the line control register contents and set the divisor latch
	 * access bit so the baud rate can be set
	 */
	LcrRegister = XUartNs550_GetLineControlReg(InstancePtr->BaseAddress);
	XUartNs550_SetLineControlReg(InstancePtr->BaseAddress ,
					LcrRegister | XUN_LCR_DLAB);

	/*
	 * Set the baud Divisors to set rate, the initial write of 0xFF is
	 * to keep the divisor from being 0 which is not recommended as per
	 * the NS16550D spec sheet
	 */
	XUartNs550_WriteReg(InstancePtr->BaseAddress, XUN_DLL_OFFSET, 0xFF);
	XUartNs550_WriteReg(InstancePtr->BaseAddress, XUN_DLM_OFFSET,
				BaudMSB);
	XUartNs550_WriteReg(InstancePtr->BaseAddress, XUN_DLL_OFFSET,
				BaudLSB);

	/*
	 * Clear the Divisor latch access bit, DLAB to allow nornal
	 * operation and write to the line control register
	 */
	XUartNs550_SetLineControlReg(InstancePtr->BaseAddress, LcrRegister);

	return XST_SUCCESS;
}
/****************************************************************************
*
* Sets the baud rate for the specified UART. Checks the input value for
* validity and also verifies that the requested rate can be configured to
* within the 3 percent error range for RS-232 communications. If the provided
* rate is not valid, the current setting is unchanged.
*
* This function is designed to be an internal function only used within the
* XUartNs550 component. It is necessary for initialization and for the user
* available function that sets the data format.
*
* @param	InstancePtr is a pointer to the XUartNs550 instance.
* @param	BaudRate to be set in the hardware.
*
* @return
*		- XST_SUCCESS if everything configures as expected
* 		- XST_UART_BAUD_ERROR if the requested rate is not available
*		because there was too much error due to the input clock
*
* @note		None.
*
*****************************************************************************/
Sets the baud rate for the specified UART. Checks the input value for
validity and also verifies that the requested rate can be configured to
within the 3 percent error range for RS-232 communications. If the provided
rate is not valid, the current setting is unchanged.

This function is designed to be an internal function only used within the
XUartNs550 component. It is necessary for initialization and for the user
available function that sets the data format.
Sets the baud rate for the specified UART . Checks the input value for validity and also verifies that the requested rate can be configured to within the 3 percent error range for RS - 232 communications . If the provided rate is not valid the current setting is unchanged . This function is designed to be an internal function only used within the XUartNs550 component . It is necessary for initialization and for the user available function that sets the data format .
int XUartNs550_SetBaudRate(XUartNs550 *InstancePtr, u32 BaudRate)
{
	u32 BaudLSB;
	u32 BaudMSB;
	u32 LcrRegister;
	u32 Divisor;
	u32 TargetRate;
	u32 Error;
	u32 PercentError;
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
	Divisor = ((InstancePtr->InputClockHz +((BaudRate * 16UL)/2)) /
			(BaudRate * 16UL));
	TargetRate = Divisor * BaudRate * 16UL;
	if (TargetRate < InstancePtr->InputClockHz) {
		Error = InstancePtr->InputClockHz - TargetRate;
	} else {
		Error = TargetRate - InstancePtr->InputClockHz;
	}
	PercentError = (Error * 100UL) / InstancePtr->InputClockHz;
	if (PercentError > XUN_MAX_BAUD_ERROR_RATE) {
		return XST_UART_BAUD_ERROR;
	}
	BaudLSB = Divisor & XUN_DIVISOR_BYTE_MASK;
	BaudMSB = (Divisor >> 8) & XUN_DIVISOR_BYTE_MASK;
	InstancePtr->BaudRate = BaudRate;
	LcrRegister = XUartNs550_GetLineControlReg(InstancePtr->BaseAddress);
	XUartNs550_SetLineControlReg(InstancePtr->BaseAddress ,
					LcrRegister | XUN_LCR_DLAB);
	XUartNs550_WriteReg(InstancePtr->BaseAddress, XUN_DLL_OFFSET, 0xFF);
	XUartNs550_WriteReg(InstancePtr->BaseAddress, XUN_DLM_OFFSET,
				BaudMSB);
	XUartNs550_WriteReg(InstancePtr->BaseAddress, XUN_DLL_OFFSET,
				BaudLSB);
	XUartNs550_SetLineControlReg(InstancePtr->BaseAddress, LcrRegister);
	return XST_SUCCESS;
}
int XUartNs550_SetBaudRate ( XUartNs550 * InstancePtr , u32 BaudRate ) { u32 BaudLSB ; u32 BaudMSB ; u32 LcrRegister ; u32 Divisor ; u32 TargetRate ; u32 Error ; u32 PercentError ; Xil_AssertNonvoid ( InstancePtr != NULL ) ; Xil_AssertNonvoid ( InstancePtr -> IsReady == XIL_COMPONENT_IS_READY ) ; Divisor = ( ( InstancePtr -> InputClockHz + ( ( BaudRate * 16UL ) / 2 ) ) / ( BaudRate * 16UL ) ) ; TargetRate = Divisor * BaudRate * 16UL ; if ( TargetRate < InstancePtr -> InputClockHz ) { Error = InstancePtr -> InputClockHz - TargetRate ; } else { Error = TargetRate - InstancePtr -> InputClockHz ; } PercentError = ( Error * 100UL ) / InstancePtr -> InputClockHz ; if ( PercentError > XUN_MAX_BAUD_ERROR_RATE ) { return XST_UART_BAUD_ERROR ; } BaudLSB = Divisor & XUN_DIVISOR_BYTE_MASK ; BaudMSB = ( Divisor >> 8 ) & XUN_DIVISOR_BYTE_MASK ; InstancePtr -> BaudRate = BaudRate ; LcrRegister = XUartNs550_GetLineControlReg ( InstancePtr -> BaseAddress ) ; XUartNs550_SetLineControlReg ( InstancePtr -> BaseAddress , LcrRegister | XUN_LCR_DLAB ) ; XUartNs550_WriteReg ( InstancePtr -> BaseAddress , XUN_DLL_OFFSET , 0xFF ) ; XUartNs550_WriteReg ( InstancePtr -> BaseAddress , XUN_DLM_OFFSET , BaudMSB ) ; XUartNs550_WriteReg ( InstancePtr -> BaseAddress , XUN_DLL_OFFSET , BaudLSB ) ; XUartNs550_SetLineControlReg ( InstancePtr -> BaseAddress , LcrRegister ) ; return XST_SUCCESS ; }
Sets the baud rate for the specified UART.
Sets the baud rate for the specified UART .
/*
	 * Assert validates the input arguments
	 */ /*
	 * Determine what the divisor should be to get the specified baud
	 * rater based upon the input clock frequency and a baud clock prescaler
	 * of 16
	 */ /*
	 * check for too much error between the baud rate that will be generated
	 * using the divisor and the expected baud rate, integer division also
	 * truncates always positive
	 */ /*
	 * Error has total error now compute the percentage multiplied by 100 to
	 * avoid floating point calculations, should be less than 3% as per
	 * RS-232 spec
	 */ /*
	 * Get the least significant and most significant bytes of the divisor
	 * so they can be written to 2 byte registers
	 */ /*
	 * Save the baud rate in the instance so that the get baud rate function
	 * won't have to calculate it from the divisor
	 */ /*
	 * Get the line control register contents and set the divisor latch
	 * access bit so the baud rate can be set
	 */ /*
	 * Set the baud Divisors to set rate, the initial write of 0xFF is
	 * to keep the divisor from being 0 which is not recommended as per
	 * the NS16550D spec sheet
	 */ /*
	 * Clear the Divisor latch access bit, DLAB to allow nornal
	 * operation and write to the line control register
	 */