1 /******************************************************************************
2  *  Filename:       uart.c
3  *
4  *  Description:    Driver for the UART peripheral.
5  *
6  *  Copyright (c) 2022 Texas Instruments Incorporated
7  *
8  *  Redistribution and use in source and binary forms, with or without
9  *  modification, are permitted provided that the following conditions are met:
10  *
11  *  1) Redistributions of source code must retain the above copyright notice,
12  *     this list of conditions and the following disclaimer.
13  *
14  *  2) Redistributions in binary form must reproduce the above copyright notice,
15  *     this list of conditions and the following disclaimer in the documentation
16  *     and/or other materials provided with the distribution.
17  *
18  *  3) Neither the name of the copyright holder nor the names of its
19  *     contributors may be used to endorse or promote products derived from this
20  *     software without specific prior written permission.
21  *
22  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  *  POSSIBILITY OF SUCH DAMAGE.
33  *
34  ******************************************************************************/
35 
36 #include "uart.h"
37 
38 //*****************************************************************************
39 //
40 // Sets the configuration of a UART
41 //
42 //*****************************************************************************
UARTConfigSetExpClk(uint32_t base,uint32_t UARTClkFreq,uint32_t baudFreq,uint32_t config)43 void UARTConfigSetExpClk(uint32_t base, uint32_t UARTClkFreq, uint32_t baudFreq, uint32_t config)
44 {
45     uint32_t div;
46 
47     // Check the arguments.
48     ASSERT(baudFreq != 0);
49 
50     // Stop the UART.
51     UARTDisable(base);
52 
53     // Compute the fractional baud rate divider.
54     div = (((UARTClkFreq * 8) / baudFreq) + 1) / 2;
55 
56     // Set the baud rate.
57     HWREG(base + UART_O_IBRD) = div / 64;
58     HWREG(base + UART_O_FBRD) = div % 64;
59 
60     // Set parity, data length, and number of stop bits.
61     HWREG(base + UART_O_LCRH) = config;
62 }
63 
64 //*****************************************************************************
65 //
66 // Disables transmitting and receiving
67 //
68 //*****************************************************************************
UARTDisable(uint32_t base)69 void UARTDisable(uint32_t base)
70 {
71     // Wait for end of TX.
72     while (HWREG(base + UART_O_FR) & UART_FR_BUSY) {}
73 
74     // Disable the FIFO.
75     HWREG(base + UART_O_LCRH) &= ~(UART_LCRH_FEN);
76 
77     // Disable the UART.
78     HWREG(base + UART_O_CTL) &= ~(UART_CTL_UARTEN | UART_CTL_TXE | UART_CTL_RXE);
79 }
80 
81 //*****************************************************************************
82 //
83 // Enable UART
84 //
85 //*****************************************************************************
UARTEnable(uint32_t base)86 void UARTEnable(uint32_t base)
87 {
88     // Enable the UART.
89     HWREG(base + UART_O_CTL) |= (UART_CTL_UARTEN | UART_CTL_TXE | UART_CTL_RXE);
90 }
91 
92 //*****************************************************************************
93 //
94 // Blocks until a character is available, and returns it
95 //
96 //*****************************************************************************
UARTGetChar(uint32_t base)97 uint8_t UARTGetChar(uint32_t base)
98 {
99     // Wait until a char is available.
100     while (HWREG(base + UART_O_FR) & UART_FR_RXFE) {}
101 
102     // Return the character.
103     return (HWREG(base + UART_O_DR));
104 }
105 
106 //*****************************************************************************
107 //
108 // Blocks until there is space in the data register, and writes a byte to it
109 //
110 //*****************************************************************************
UARTPutChar(uint32_t base,uint8_t data)111 void UARTPutChar(uint32_t base, uint8_t data)
112 {
113     // Wait until space is available.
114     while (HWREG(base + UART_O_FR) & UART_FR_TXFF) {}
115 
116     // Send the char.
117     HWREG(base + UART_O_DR) = data;
118 }
119