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