]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A53_64-bit_UltraScale_MPSoC/RTOSDemo_A53_bsp/psu_cortexa53_0/libsrc/standalone_v5_4/src/uart.c
Update BSP source files for UltraScale Cortex-A53 and Cortex-R5 and Microblaze to...
[freertos] / FreeRTOS / Demo / CORTEX_A53_64-bit_UltraScale_MPSoC / RTOSDemo_A53_bsp / psu_cortexa53_0 / libsrc / standalone_v5_4 / src / uart.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2014 - 2015 Xilinx, Inc. All rights reserved.
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
18 *
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
25 * SOFTWARE.
26 *
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.
30 *
31 ******************************************************************************/
32 /*****************************************************************************/
33 /**
34 * @file uart.c
35 *
36 * This file contains APIs for configuring the UART.
37 *
38 * <pre>
39 * MODIFICATION HISTORY:
40 *
41 * Ver   Who  Date     Changes
42 * ----- ---- -------- ---------------------------------------------------
43 * 5.00  pkp      05/29/14 First release
44 * </pre>
45 *
46 * @note
47 *
48 * None.
49 *
50 ******************************************************************************/
51
52 #include "xil_types.h"
53 #include "xil_assert.h"
54 #include "xil_io.h"
55 #include "xparameters.h"
56
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
62
63 #define MAX_BAUD_ERROR_RATE     3U      /* max % error allowed */
64 #define UART_BAUDRATE   115200U
65 #define CSU_VERSION_REG     0xFFCA0044U
66
67 void Init_Uart(void);
68
69 void Init_Uart(void)
70 {
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
76                                  * rate */
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;
80         u32 PercentError;
81         u32 InputClk;
82         u32 BaudRate = UART_BAUDRATE;
83
84         /* set CD and BDIV */
85
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;
90 #else
91         /* STDIO is not set or axi_uart is being used for STDIO */
92         return;
93 #endif
94 InputClk = 25000000U;
95         /*
96          * Determine the Baud divider. It can be 4to 254.
97          * Loop through all possible combinations
98          */
99         for (IterBAUDDIV = 4U; IterBAUDDIV < 255U; IterBAUDDIV++) {
100
101                 /*
102                  * Calculate the value for BRGR register
103                  */
104                 BRGR_Value = InputClk / (BaudRate * ((u32)IterBAUDDIV + 1U));
105
106                 /*
107                  * Calculate the baud rate from the BRGR value
108                  */
109                 CalcBaudRate = InputClk/ (BRGR_Value * ((u32)IterBAUDDIV + 1U));
110
111                 /*
112                  * Avoid unsigned integer underflow
113                  */
114                 if (BaudRate > CalcBaudRate) {
115                         BaudError = BaudRate - CalcBaudRate;
116                 } else {
117                         BaudError = CalcBaudRate - BaudRate;
118                 }
119
120                 /*
121                  * Find the calculated baud rate closest to requested baud rate.
122                  */
123                 if (Best_Error > BaudError) {
124
125                         Best_BRGR = BRGR_Value;
126                         Best_BAUDDIV = IterBAUDDIV;
127                         Best_Error = BaudError;
128                 }
129         }
130
131         /*
132          * Make sure the best error is not too large.
133          */
134         PercentError = (Best_Error * 100U) / BaudRate;
135         if (((u32)MAX_BAUD_ERROR_RATE) < PercentError) {
136                 return;
137         }
138
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);
142
143     /*
144      * Veloce specific code
145      */
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);
149     }
150
151         /*
152          * 8 data, 1 stop, 0 parity bits
153          * sel_clk=uart_clk=APB clock
154          */
155         Xil_Out32(STDOUT_BASEADDRESS + UART_MR_OFFSET, 0x00000020U);
156
157         /* enable Tx/Rx and reset Tx/Rx data path */
158         Xil_Out32((STDOUT_BASEADDRESS + UART_CR_OFFSET), 0x00000017U);
159
160         return;
161 #endif
162 }