1 | /* ---------------------------------------------------------------------------- */ |
---|
2 | /* Atmel Microcontroller Software Support */ |
---|
3 | /* SAM Software Package License */ |
---|
4 | /* ---------------------------------------------------------------------------- */ |
---|
5 | /* Copyright (c) 2015, Atmel Corporation */ |
---|
6 | /* */ |
---|
7 | /* All rights reserved. */ |
---|
8 | /* */ |
---|
9 | /* Redistribution and use in source and binary forms, with or without */ |
---|
10 | /* modification, are permitted provided that the following condition is met: */ |
---|
11 | /* */ |
---|
12 | /* - Redistributions of source code must retain the above copyright notice, */ |
---|
13 | /* this list of conditions and the disclaimer below. */ |
---|
14 | /* */ |
---|
15 | /* Atmel's name may not be used to endorse or promote products derived from */ |
---|
16 | /* this software without specific prior written permission. */ |
---|
17 | /* */ |
---|
18 | /* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR */ |
---|
19 | /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ |
---|
20 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE */ |
---|
21 | /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, */ |
---|
22 | /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */ |
---|
23 | /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */ |
---|
24 | /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */ |
---|
25 | /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */ |
---|
26 | /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */ |
---|
27 | /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
---|
28 | /* ---------------------------------------------------------------------------- */ |
---|
29 | |
---|
30 | /** |
---|
31 | * \file |
---|
32 | * |
---|
33 | * Implementation of UART (Universal Asynchronous Receiver Transmitter) |
---|
34 | * controller. |
---|
35 | * |
---|
36 | */ |
---|
37 | /*------------------------------------------------------------------------------ |
---|
38 | * Headers |
---|
39 | *----------------------------------------------------------------------------*/ |
---|
40 | #include "chip.h" |
---|
41 | |
---|
42 | #include <assert.h> |
---|
43 | #include <string.h> |
---|
44 | |
---|
45 | /*------------------------------------------------------------------------------ |
---|
46 | * Exported functions |
---|
47 | *----------------------------------------------------------------------------*/ |
---|
48 | |
---|
49 | /** |
---|
50 | * \brief Configures an UART peripheral with the specified parameters. |
---|
51 | * |
---|
52 | * |
---|
53 | * \param uart Pointer to the UART peripheral to configure. |
---|
54 | * \param mode Desired value for the UART mode register (see the datasheet). |
---|
55 | * \param baudrate Baudrate at which the UART should operate (in Hz). |
---|
56 | * \param masterClock Frequency of the system master clock (in Hz). |
---|
57 | */ |
---|
58 | void UART_Configure(Uart *uart, |
---|
59 | uint32_t mode, |
---|
60 | uint32_t baudrate, |
---|
61 | uint32_t masterClock) |
---|
62 | { |
---|
63 | /* Reset and disable receiver & transmitter*/ |
---|
64 | uart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX |
---|
65 | | UART_CR_RXDIS | UART_CR_TXDIS | UART_CR_RSTSTA; |
---|
66 | |
---|
67 | uart->UART_IDR = 0xFFFFFFFF; |
---|
68 | |
---|
69 | /* Configure mode*/ |
---|
70 | uart->UART_MR = mode; |
---|
71 | |
---|
72 | /* Configure baudrate*/ |
---|
73 | uart->UART_BRGR = (masterClock / baudrate) / 16; |
---|
74 | |
---|
75 | uart->UART_CR = UART_CR_TXEN | UART_CR_RXEN; |
---|
76 | |
---|
77 | } |
---|
78 | /** |
---|
79 | * \brief Enables or disables the transmitter of an UART peripheral. |
---|
80 | * |
---|
81 | * |
---|
82 | * \param uart Pointer to an UART peripheral |
---|
83 | * \param enabled If true, the transmitter is enabled; otherwise it is |
---|
84 | * disabled. |
---|
85 | */ |
---|
86 | void UART_SetTransmitterEnabled(Uart *uart, uint8_t enabled) |
---|
87 | { |
---|
88 | if (enabled) |
---|
89 | uart->UART_CR = UART_CR_TXEN; |
---|
90 | else |
---|
91 | uart->UART_CR = UART_CR_TXDIS; |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * \brief Enables or disables the receiver of an UART peripheral |
---|
96 | * |
---|
97 | * |
---|
98 | * \param uart Pointer to an UART peripheral |
---|
99 | * \param enabled If true, the receiver is enabled; otherwise it is disabled. |
---|
100 | */ |
---|
101 | void UART_SetReceiverEnabled(Uart *uart, uint8_t enabled) |
---|
102 | { |
---|
103 | if (enabled) |
---|
104 | uart->UART_CR = UART_CR_RXEN; |
---|
105 | else |
---|
106 | uart->UART_CR = UART_CR_RXDIS; |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * \brief Return 1 if a character can be read in UART |
---|
111 | * \param uart Pointer to an UART peripheral. |
---|
112 | */ |
---|
113 | uint32_t UART_IsRxReady(Uart *uart) |
---|
114 | { |
---|
115 | return (uart->UART_SR & UART_SR_RXRDY); |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * \brief Reads and returns a character from the UART. |
---|
120 | * |
---|
121 | * \note This function is synchronous (i.e. uses polling). |
---|
122 | * \param uart Pointer to an UART peripheral. |
---|
123 | * \return Character received. |
---|
124 | */ |
---|
125 | uint8_t UART_GetChar(Uart *uart) |
---|
126 | { |
---|
127 | while (!UART_IsRxReady(uart)); |
---|
128 | |
---|
129 | return uart->UART_RHR; |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * \brief Return 1 if a character can be send to UART |
---|
134 | * \param uart Pointer to an UART peripheral. |
---|
135 | */ |
---|
136 | uint32_t UART_IsTxReady(Uart *uart) |
---|
137 | { |
---|
138 | return (uart->UART_SR & UART_SR_TXRDY); |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * \brief Return 1 if a character can be send to UART |
---|
143 | * \param uart Pointer to an UART peripheral. |
---|
144 | */ |
---|
145 | static uint32_t UART_IsTxSent(Uart *uart) |
---|
146 | { |
---|
147 | return (uart->UART_SR & UART_SR_TXEMPTY); |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * \brief Sends one packet of data through the specified UART peripheral. This |
---|
152 | * function operates synchronously, so it only returns when the data has been |
---|
153 | * actually sent. |
---|
154 | * |
---|
155 | * \param uart Pointer to an UART peripheral. |
---|
156 | * \param c Character to send |
---|
157 | */ |
---|
158 | void UART_PutChar(Uart *uart, uint8_t c) |
---|
159 | { |
---|
160 | /* Wait for the transmitter to be ready*/ |
---|
161 | while (!UART_IsRxReady(uart) && !UART_IsTxSent(uart)); |
---|
162 | |
---|
163 | /* Send character*/ |
---|
164 | uart->UART_THR = c; |
---|
165 | |
---|
166 | /* Wait for the transfer to complete*/ |
---|
167 | while (!UART_IsTxSent(uart)); |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * \brief Get present status |
---|
172 | * \param uart Pointer to an UART peripheral. |
---|
173 | */ |
---|
174 | uint32_t UART_GetStatus(Uart *uart) |
---|
175 | { |
---|
176 | return uart->UART_SR; |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * \brief Enable interrupt |
---|
181 | * \param uart Pointer to an UART peripheral. |
---|
182 | * \param mode Interrupt mode. |
---|
183 | */ |
---|
184 | void UART_EnableIt(Uart *uart, uint32_t mode) |
---|
185 | { |
---|
186 | uart->UART_IER = mode; |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * \brief Disable interrupt |
---|
191 | * \param uart Pointer to an UART peripheral. |
---|
192 | * \param mode Interrupt mode. |
---|
193 | */ |
---|
194 | void UART_DisableIt(Uart *uart, uint32_t mode) |
---|
195 | { |
---|
196 | uart->UART_IDR = mode; |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * \brief Return interrupt mask |
---|
201 | * \param uart Pointer to an UART peripheral. |
---|
202 | */ |
---|
203 | uint32_t UART_GetItMask(Uart *uart) |
---|
204 | { |
---|
205 | return uart->UART_IMR; |
---|
206 | } |
---|
207 | |
---|
208 | void UART_SendBuffer(Uart *uart, uint8_t *pBuffer, uint32_t BuffLen) |
---|
209 | { |
---|
210 | uint8_t *pData = pBuffer; |
---|
211 | uint32_t Len = 0; |
---|
212 | |
---|
213 | for (Len = 0; Len < BuffLen; Len++) { |
---|
214 | UART_PutChar(uart, *pData); |
---|
215 | pData++; |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | void UART_ReceiveBuffer(Uart *uart, uint8_t *pBuffer, uint32_t BuffLen) |
---|
220 | { |
---|
221 | uint32_t Len = 0; |
---|
222 | |
---|
223 | for (Len = 0; Len < BuffLen; Len++) { |
---|
224 | *pBuffer = UART_GetChar(uart); |
---|
225 | pBuffer++; |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | void UART_CompareConfig(Uart *uart, uint8_t Val1, uint8_t Val2) |
---|
230 | { |
---|
231 | uart->UART_CMPR = (UART_CMPR_VAL1(Val1) | UART_CMPR_VAL2(Val2)); |
---|
232 | } |
---|