1 /******************************************************************************
3 * Copyright (C) 2014 - 2015 Xilinx, Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
31 ******************************************************************************/
32 /*****************************************************************************/
36 * This file contains APIs for configuring the UART.
39 * MODIFICATION HISTORY:
41 * Ver Who Date Changes
42 * ----- ---- -------- ---------------------------------------------------
43 * 5.00 pkp 02/20/14 First release
50 ******************************************************************************/
52 #include "xil_types.h"
53 #include "xparameters.h"
54 #include "xil_assert.h"
57 /* Register offsets */
58 #define UART_CR_OFFSET 0x00000000U
59 #define UART_MR_OFFSET 0x00000004U
60 #define UART_BAUDGEN_OFFSET 0x00000018U
61 #define UART_BAUDDIV_OFFSET 0x00000034U
63 #define MAX_BAUD_ERROR_RATE 0x00000003U /* max % error allowed */
64 #define UART_BAUDRATE 115200U
65 #define CSU_VERSION_REG 0xFFCA0044U
71 #ifdef STDOUT_BASEADDRESS
72 u8 IterBAUDDIV; /* Iterator for available baud divisor values */
73 u32 BRGR_Value; /* Calculated value for baud rate generator */
74 u32 CalcBaudRate; /* Calculated baud rate */
75 u32 BaudError; /* Diff between calculated and requested baud rate */
76 u32 Best_BRGR = 0U; /* Best value for baud rate generator */
77 u8 Best_BAUDDIV = 0U; /* Best value for baud divisor */
78 u32 Best_Error = 0xFFFFFFFFU;
81 u32 BaudRate = UART_BAUDRATE;
83 #if (STDOUT_BASEADDRESS == XPAR_XUARTPS_0_BASEADDR)
84 InputClk = XPAR_XUARTPS_0_UART_CLK_FREQ_HZ;
85 #elif (STDOUT_BASEADDRESS == XPAR_XUARTPS_1_BASEADDR)
86 InputClk = XPAR_XUARTPS_1_UART_CLK_FREQ_HZ;
88 /* STDIO is not set or axi_uart is being used for STDIO */
93 * Determine the Baud divider. It can be 4to 254.
94 * Loop through all possible combinations
96 for (IterBAUDDIV = 4U; IterBAUDDIV < 255U; IterBAUDDIV++) {
99 * Calculate the value for BRGR register
101 BRGR_Value = InputClk / (BaudRate * ((u32)IterBAUDDIV + 0x00000001U));
104 * Calculate the baud rate from the BRGR value
106 CalcBaudRate = InputClk/ (BRGR_Value * ((u32)IterBAUDDIV + 0x00000001U));
109 * Avoid unsigned integer underflow
111 if (BaudRate > CalcBaudRate) {
112 BaudError = BaudRate - CalcBaudRate;
114 BaudError = CalcBaudRate - BaudRate;
118 * Find the calculated baud rate closest to requested baud rate.
120 if (Best_Error > BaudError) {
122 Best_BRGR = BRGR_Value;
123 Best_BAUDDIV = IterBAUDDIV;
124 Best_Error = BaudError;
130 * Make sure the best error is not too large.
132 PercentError = (Best_Error * 100U) / BaudRate;
133 if (MAX_BAUD_ERROR_RATE < PercentError) {
137 /* set CD and BDIV */
138 Xil_Out32(STDOUT_BASEADDRESS + UART_BAUDGEN_OFFSET, Best_BRGR);
139 Xil_Out32(STDOUT_BASEADDRESS + UART_BAUDDIV_OFFSET, (u32)Best_BAUDDIV);
142 * Veloce specific code
144 if((Xil_In32(CSU_VERSION_REG) & 0x0000F000U) == 0x00002000U ) {
145 Xil_Out32(STDOUT_BASEADDRESS + UART_BAUDGEN_OFFSET, 0x00000002U);
146 Xil_Out32(STDOUT_BASEADDRESS + UART_BAUDDIV_OFFSET, 0x00000004U);
150 * 8 data, 1 stop, 0 parity bits
151 * sel_clk=uart_clk=APB clock
153 Xil_Out32(STDOUT_BASEADDRESS + UART_MR_OFFSET, 0x00000020U);
155 /* enable Tx/Rx and reset Tx/Rx data path */
156 Xil_Out32((STDOUT_BASEADDRESS + UART_CR_OFFSET), 0x00000017U);