]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R5_UltraScale_MPSoC/RTOSDemo_R5_bsp/psu_cortexr5_0/libsrc/standalone_v5_4/src/uart.c
Update some more standard demos for use on 64-bit architectures.
[freertos] / FreeRTOS / Demo / CORTEX_R5_UltraScale_MPSoC / RTOSDemo_R5_bsp / psu_cortexr5_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  02/20/14 First release
44 * </pre>
45 *
46 * @note
47 *
48 * None.
49 *
50 ******************************************************************************/
51
52 #include "xil_types.h"
53 #include "xparameters.h"
54 #include "xil_assert.h"
55 #include "xil_io.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     0x00000003U     /* 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 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;
79         u32 PercentError;
80         u32 InputClk;
81    u32 BaudRate = UART_BAUDRATE;
82
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;
87 #else
88         /* STDIO is not set or axi_uart is being used for STDIO */
89         return;
90 #endif
91 InputClk = 25000000U;
92         /*
93          * Determine the Baud divider. It can be 4to 254.
94          * Loop through all possible combinations
95          */
96         for (IterBAUDDIV = 4U; IterBAUDDIV < 255U; IterBAUDDIV++) {
97
98                 /*
99                  * Calculate the value for BRGR register
100                  */
101                 BRGR_Value = InputClk / (BaudRate * ((u32)IterBAUDDIV + 0x00000001U));
102
103                 /*
104                  * Calculate the baud rate from the BRGR value
105                  */
106                 CalcBaudRate = InputClk/ (BRGR_Value * ((u32)IterBAUDDIV + 0x00000001U));
107
108                 /*
109                  * Avoid unsigned integer underflow
110                  */
111                 if (BaudRate > CalcBaudRate) {
112                         BaudError = BaudRate - CalcBaudRate;
113                 } else {
114                         BaudError = CalcBaudRate - BaudRate;
115                 }
116
117                 /*
118                  * Find the calculated baud rate closest to requested baud rate.
119                  */
120                 if (Best_Error > BaudError) {
121
122                         Best_BRGR = BRGR_Value;
123                         Best_BAUDDIV = IterBAUDDIV;
124                         Best_Error = BaudError;
125
126                 }
127         }
128
129         /*
130          * Make sure the best error is not too large.
131          */
132         PercentError = (Best_Error * 100U) / BaudRate;
133         if (MAX_BAUD_ERROR_RATE < PercentError) {
134                 return;
135         }
136
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);
140
141     /*
142      * Veloce specific code
143      */
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);
147     }
148
149         /*
150          * 8 data, 1 stop, 0 parity bits
151          * sel_clk=uart_clk=APB clock
152          */
153         Xil_Out32(STDOUT_BASEADDRESS + UART_MR_OFFSET, 0x00000020U);
154
155         /* enable Tx/Rx and reset Tx/Rx data path */
156         Xil_Out32((STDOUT_BASEADDRESS + UART_CR_OFFSET), 0x00000017U);
157
158         return;
159 #endif
160 }