]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo_bsp/ps7_cortexa9_0/libsrc/standalone_v4_1/src/uart.c
Remove obsolete MPU demos.
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo_bsp / ps7_cortexa9_0 / libsrc / standalone_v4_1 / src / uart.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2010 - 2014 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 * 1.00a sdm  08/02/10 Initial version
44 * </pre>
45 *
46 * @note
47 *
48 * None.
49 *
50 ******************************************************************************/
51
52 #include "xparameters.h"
53 #include "xil_types.h"
54 #include "xil_assert.h"
55 #include "xil_io.h"
56
57 /* Register offsets */
58 #define UART_CR_OFFSET          0x00
59 #define UART_MR_OFFSET          0x04
60 #define UART_BAUDGEN_OFFSET     0x18
61 #define UART_BAUDDIV_OFFSET     0x34
62
63 #define MAX_BAUD_ERROR_RATE     3       /* max % error allowed */
64 #define UART_BAUDRATE   115200
65
66 void Init_Uart(void);
67
68 void Init_Uart(void)
69 {
70 #ifdef STDOUT_BASEADDRESS
71         u8 IterBAUDDIV;         /* Iterator for available baud divisor values */
72         u32 BRGR_Value;         /* Calculated value for baud rate generator */
73         u32 CalcBaudRate;       /* Calculated baud rate */
74         u32 BaudError;          /* Diff between calculated and requested baud
75                                  * rate */
76         u32 Best_BRGR = 0;      /* Best value for baud rate generator */
77         u8 Best_BAUDDIV = 0;    /* Best value for baud divisor */
78         u32 Best_Error = 0xFFFFFFFF;
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
92         /*
93          * Determine the Baud divider. It can be 4to 254.
94          * Loop through all possible combinations
95          */
96         for (IterBAUDDIV = 4; IterBAUDDIV < 255; IterBAUDDIV++) {
97
98                 /*
99                  * Calculate the value for BRGR register
100                  */
101                 BRGR_Value = InputClk / (BaudRate * (IterBAUDDIV + 1));
102
103                 /*
104                  * Calculate the baud rate from the BRGR value
105                  */
106                 CalcBaudRate = InputClk/ (BRGR_Value * (IterBAUDDIV + 1));
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          * Make sure the best error is not too large.
130          */
131         PercentError = (Best_Error * 100) / BaudRate;
132         if (MAX_BAUD_ERROR_RATE < PercentError) {
133                 return;
134         }
135
136         /* set CD and BDIV */
137         Xil_Out32(STDOUT_BASEADDRESS + UART_BAUDGEN_OFFSET, Best_BRGR);
138         Xil_Out32(STDOUT_BASEADDRESS + UART_BAUDDIV_OFFSET, Best_BAUDDIV);
139
140         /*
141          * 8 data, 1 stop, 0 parity bits
142          * sel_clk=uart_clk=APB clock
143          */
144         Xil_Out32(STDOUT_BASEADDRESS + UART_MR_OFFSET, 0x20);
145
146         /* enable Tx/Rx and reset Tx/Rx data path */
147         Xil_Out32((STDOUT_BASEADDRESS + UART_CR_OFFSET), 0x17);
148
149         return;
150 #endif
151 }