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 05/29/14 First release
50 ******************************************************************************/
52 #include "xil_types.h"
53 #include "xil_assert.h"
55 #include "xparameters.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 3U /* 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
77 u32 Best_BRGR = 0U; /* Best value for baud rate generator */
78 u8 Best_BAUDDIV = 0U; /* Best value for baud divisor */
79 u32 Best_Error = 0xFFFFFFFFU;
82 u32 BaudRate = UART_BAUDRATE;
86 #if (STDOUT_BASEADDRESS == XPAR_XUARTPS_0_BASEADDR)
87 InputClk = XPAR_XUARTPS_0_UART_CLK_FREQ_HZ;
88 #elif (STDOUT_BASEADDRESS == XPAR_XUARTPS_1_BASEADDR)
89 InputClk = XPAR_XUARTPS_1_UART_CLK_FREQ_HZ;
91 /* STDIO is not set or axi_uart is being used for STDIO */
96 * Determine the Baud divider. It can be 4to 254.
97 * Loop through all possible combinations
99 for (IterBAUDDIV = 4U; IterBAUDDIV < 255U; IterBAUDDIV++) {
102 * Calculate the value for BRGR register
104 BRGR_Value = InputClk / (BaudRate * ((u32)IterBAUDDIV + 1U));
107 * Calculate the baud rate from the BRGR value
109 CalcBaudRate = InputClk/ (BRGR_Value * ((u32)IterBAUDDIV + 1U));
112 * Avoid unsigned integer underflow
114 if (BaudRate > CalcBaudRate) {
115 BaudError = BaudRate - CalcBaudRate;
117 BaudError = CalcBaudRate - BaudRate;
121 * Find the calculated baud rate closest to requested baud rate.
123 if (Best_Error > BaudError) {
125 Best_BRGR = BRGR_Value;
126 Best_BAUDDIV = IterBAUDDIV;
127 Best_Error = BaudError;
132 * Make sure the best error is not too large.
134 PercentError = (Best_Error * 100U) / BaudRate;
135 if (((u32)MAX_BAUD_ERROR_RATE) < PercentError) {
139 /* set CD and BDIV */
140 Xil_Out32(STDOUT_BASEADDRESS + UART_BAUDGEN_OFFSET, Best_BRGR);
141 Xil_Out32(STDOUT_BASEADDRESS + UART_BAUDDIV_OFFSET, (u32)Best_BAUDDIV);
144 * Veloce specific code
146 if((Xil_In32(CSU_VERSION_REG) & 0x0000F000U) == 0x00002000U ) {
147 Xil_Out32(STDOUT_BASEADDRESS + UART_BAUDGEN_OFFSET, 2U);
148 Xil_Out32(STDOUT_BASEADDRESS + UART_BAUDDIV_OFFSET, 4U);
152 * 8 data, 1 stop, 0 parity bits
153 * sel_clk=uart_clk=APB clock
155 Xil_Out32(STDOUT_BASEADDRESS + UART_MR_OFFSET, 0x00000020U);
157 /* enable Tx/Rx and reset Tx/Rx data path */
158 Xil_Out32((STDOUT_BASEADDRESS + UART_CR_OFFSET), 0x00000017U);