]> git.sur5r.net Git - freertos/blob - Demo/Common/drivers/Atmel/at91lib/peripherals/usart/usart.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / Common / drivers / Atmel / at91lib / peripherals / usart / usart.c
1 /* ----------------------------------------------------------------------------\r
2  *         ATMEL Microcontroller Software Support \r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2008, 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 //         Headers\r
32 //------------------------------------------------------------------------------\r
33 \r
34 #include "usart.h"\r
35 #include <utility/trace.h>\r
36 \r
37 //------------------------------------------------------------------------------\r
38 //         Exported functions\r
39 //------------------------------------------------------------------------------\r
40 //------------------------------------------------------------------------------\r
41 /// Configures an USART peripheral with the specified parameters.\r
42 /// \param usart  Pointer to the USART peripheral to configure.\r
43 /// \param mode  Desired value for the USART mode register (see the datasheet).\r
44 /// \param baudrate  Baudrate at which the USART should operate (in Hz).\r
45 /// \param masterClock  Frequency of the system master clock (in Hz).\r
46 //------------------------------------------------------------------------------\r
47 void USART_Configure(AT91S_USART *usart,\r
48                             unsigned int mode,\r
49                             unsigned int baudrate,\r
50                             unsigned int masterClock)\r
51 {\r
52     // Reset and disable receiver & transmitter\r
53     usart->US_CR = AT91C_US_RSTRX | AT91C_US_RSTTX\r
54                    | AT91C_US_RXDIS | AT91C_US_TXDIS;\r
55 \r
56     // Configure mode\r
57     usart->US_MR = mode;\r
58 \r
59     // Configure baudrate\r
60     // Asynchronous, no oversampling\r
61     if (((mode & AT91C_US_SYNC) == 0)\r
62         && ((mode & AT91C_US_OVER) == 0)) {\r
63     \r
64         usart->US_BRGR = (masterClock / baudrate) / 16;\r
65     }\r
66     // TODO other modes\r
67 }\r
68 \r
69 //------------------------------------------------------------------------------\r
70 /// Enables or disables the transmitter of an USART peripheral.\r
71 /// \param usart  Pointer to an USART peripheral\r
72 /// \param enabled  If true, the transmitter is enabled; otherwise it is\r
73 ///                 disabled.\r
74 //------------------------------------------------------------------------------\r
75 void USART_SetTransmitterEnabled(AT91S_USART *usart,\r
76                                         unsigned char enabled)\r
77 {\r
78     if (enabled) {\r
79 \r
80         usart->US_CR = AT91C_US_TXEN;\r
81     }\r
82     else {\r
83 \r
84         usart->US_CR = AT91C_US_TXDIS;\r
85     }\r
86 }\r
87 \r
88 //------------------------------------------------------------------------------\r
89 /// Enables or disables the receiver of an USART peripheral\r
90 /// \param usart  Pointer to an USART peripheral\r
91 /// \param enabled  If true, the receiver is enabled; otherwise it is disabled.\r
92 //------------------------------------------------------------------------------\r
93 void USART_SetReceiverEnabled(AT91S_USART *usart,\r
94                                      unsigned char enabled)\r
95 {\r
96     if (enabled) {\r
97 \r
98         usart->US_CR = AT91C_US_RXEN;\r
99     }\r
100     else {\r
101 \r
102         usart->US_CR = AT91C_US_RXDIS;\r
103     }\r
104 }\r
105 \r
106 //------------------------------------------------------------------------------\r
107 /// Sends one packet of data through the specified USART peripheral. This\r
108 /// function operates synchronously, so it only returns when the data has been\r
109 /// actually sent.\r
110 /// \param usart  Pointer to an USART peripheral.\r
111 /// \param data  Data to send including 9nth bit and sync field if necessary (in\r
112 ///              the same format as the US_THR register in the datasheet).\r
113 /// \param timeOut  Time out value (0 = no timeout).\r
114 //------------------------------------------------------------------------------\r
115 void USART_Write(\r
116     AT91S_USART *usart,\r
117     unsigned short data,\r
118     volatile unsigned int timeOut)\r
119 {\r
120     if (timeOut == 0) {\r
121 \r
122         while ((usart->US_CSR & AT91C_US_TXEMPTY) == 0);\r
123     }\r
124     else {\r
125 \r
126         while ((usart->US_CSR & AT91C_US_TXEMPTY) == 0) {\r
127 \r
128             if (timeOut == 0) {\r
129 \r
130                 trace_LOG(trace_ERROR, "-E- USART_Write: Timed out.\n\r");\r
131                 return;\r
132             }\r
133             timeOut--;\r
134         }\r
135     }\r
136 \r
137     usart->US_THR = data;\r
138 }\r
139 \r
140 //------------------------------------------------------------------------------\r
141 /// Sends the contents of a data buffer through the specified USART peripheral.\r
142 /// This function returns immediately (1 if the buffer has been queued, 0\r
143 /// otherwise); poll the ENDTX and TXBUFE bits of the USART status register\r
144 /// to check for the transfer completion.\r
145 /// \param usart  Pointer to an USART peripheral.\r
146 /// \param buffer  Pointer to the data buffer to send.\r
147 /// \param size  Size of the data buffer (in bytes).\r
148 //------------------------------------------------------------------------------\r
149 unsigned char USART_WriteBuffer(\r
150     AT91S_USART *usart,\r
151     void *buffer,\r
152     unsigned int size)\r
153 {\r
154     // Check if the first PDC bank is free\r
155     if ((usart->US_TCR == 0) && (usart->US_TNCR == 0)) {\r
156 \r
157         usart->US_TPR = (unsigned int) buffer;\r
158         usart->US_TCR = size;\r
159         usart->US_PTCR = AT91C_PDC_TXTEN;\r
160 \r
161         return 1;\r
162     }\r
163     // Check if the second PDC bank is free\r
164     else if (usart->US_TNCR == 0) {\r
165 \r
166         usart->US_TNPR = (unsigned int) buffer;\r
167         usart->US_TNCR = size;\r
168 \r
169         return 1;\r
170     }\r
171     else {\r
172 \r
173         return 0;\r
174     }\r
175 }\r
176 \r
177 //------------------------------------------------------------------------------\r
178 /// Reads and return a packet of data on the specified USART peripheral. This\r
179 /// function operates asynchronously, so it waits until some data has been\r
180 /// received.\r
181 /// \param usart  Pointer to an USART peripheral.\r
182 /// \param timeOut  Time out value (0 -> no timeout).\r
183 //------------------------------------------------------------------------------\r
184 unsigned short USART_Read(\r
185     AT91S_USART *usart,\r
186     volatile unsigned int timeOut)\r
187 {\r
188     if (timeOut == 0) {\r
189 \r
190         while ((usart->US_CSR & AT91C_US_RXRDY) == 0);\r
191     }\r
192     else {\r
193 \r
194         while ((usart->US_CSR & AT91C_US_RXRDY) == 0) {\r
195 \r
196             if (timeOut == 0) {\r
197 \r
198                 trace_LOG(trace_ERROR, "-E- USART_Read: Timed out.\n\r");\r
199                 return 0;\r
200             }\r
201             timeOut--;\r
202         }\r
203     }\r
204 \r
205     return usart->US_RHR;\r
206 }\r
207 \r
208 //------------------------------------------------------------------------------\r
209 /// Reads data from an USART peripheral, filling the provided buffer until it\r
210 /// becomes full. This function returns immediately with 1 if the buffer has\r
211 /// been queued for transmission; otherwise 0.\r
212 /// \param usart  Pointer to an USART peripheral.\r
213 /// \param buffer  Pointer to the buffer where the received data will be stored.\r
214 /// \param size  Size of the data buffer (in bytes).\r
215 //------------------------------------------------------------------------------\r
216 unsigned char USART_ReadBuffer(AT91S_USART *usart,\r
217                                       void *buffer,\r
218                                       unsigned int size)\r
219 {\r
220     // Check if the first PDC bank is free\r
221     if ((usart->US_RCR == 0) && (usart->US_RNCR == 0)) {\r
222 \r
223         usart->US_RPR = (unsigned int) buffer;\r
224         usart->US_RCR = size;\r
225         usart->US_PTCR = AT91C_PDC_RXTEN;\r
226 \r
227         return 1;\r
228     }\r
229     // Check if the second PDC bank is free\r
230     else if (usart->US_RNCR == 0) {\r
231 \r
232         usart->US_RNPR = (unsigned int) buffer;\r
233         usart->US_RNCR = size;\r
234 \r
235         return 1;\r
236     }\r
237     else {\r
238 \r
239         return 0;\r
240     }\r
241 }\r
242 \r
243 //------------------------------------------------------------------------------\r
244 /// Returns 1 if some data has been received and can be read from an USART;\r
245 /// otherwise returns 0.\r
246 /// \param usart  Pointer to an AT91S_USART instance.\r
247 //------------------------------------------------------------------------------\r
248 unsigned char USART_IsDataAvailable(AT91S_USART *usart)\r
249 {\r
250     if ((usart->US_CSR & AT91C_US_RXRDY) != 0) {\r
251 \r
252         return 1;\r
253     }\r
254     else {\r
255 \r
256         return 0;\r
257     }\r
258 }\r
259 \r