OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [drivers/] [Atmel/] [at91lib/] [peripherals/] [usart/] [usart.c] - Blame information for rev 608

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 608 jeremybenn
/* ----------------------------------------------------------------------------
2
 *         ATMEL Microcontroller Software Support
3
 * ----------------------------------------------------------------------------
4
 * Copyright (c) 2008, Atmel Corporation
5
 *
6
 * All rights reserved.
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
 * - Redistributions of source code must retain the above copyright notice,
12
 * this list of conditions and the disclaimer below.
13
 *
14
 * Atmel's name may not be used to endorse or promote products derived from
15
 * this software without specific prior written permission.
16
 *
17
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 * ----------------------------------------------------------------------------
28
 */
29
 
30
//------------------------------------------------------------------------------
31
//         Headers
32
//------------------------------------------------------------------------------
33
 
34
#include "usart.h"
35
#include <utility/trace.h>
36
 
37
//------------------------------------------------------------------------------
38
//         Exported functions
39
//------------------------------------------------------------------------------
40
//------------------------------------------------------------------------------
41
/// Configures an USART peripheral with the specified parameters.
42
/// \param usart  Pointer to the USART peripheral to configure.
43
/// \param mode  Desired value for the USART mode register (see the datasheet).
44
/// \param baudrate  Baudrate at which the USART should operate (in Hz).
45
/// \param masterClock  Frequency of the system master clock (in Hz).
46
//------------------------------------------------------------------------------
47
void USART_Configure(AT91S_USART *usart,
48
                            unsigned int mode,
49
                            unsigned int baudrate,
50
                            unsigned int masterClock)
51
{
52
    // Reset and disable receiver & transmitter
53
    usart->US_CR = AT91C_US_RSTRX | AT91C_US_RSTTX
54
                   | AT91C_US_RXDIS | AT91C_US_TXDIS;
55
 
56
    // Configure mode
57
    usart->US_MR = mode;
58
 
59
    // Configure baudrate
60
    // Asynchronous, no oversampling
61
    if (((mode & AT91C_US_SYNC) == 0)
62
        && ((mode & AT91C_US_OVER) == 0)) {
63
 
64
        usart->US_BRGR = (masterClock / baudrate) / 16;
65
    }
66
    // TODO other modes
67
}
68
 
69
//------------------------------------------------------------------------------
70
/// Enables or disables the transmitter of an USART peripheral.
71
/// \param usart  Pointer to an USART peripheral
72
/// \param enabled  If true, the transmitter is enabled; otherwise it is
73
///                 disabled.
74
//------------------------------------------------------------------------------
75
void USART_SetTransmitterEnabled(AT91S_USART *usart,
76
                                        unsigned char enabled)
77
{
78
    if (enabled) {
79
 
80
        usart->US_CR = AT91C_US_TXEN;
81
    }
82
    else {
83
 
84
        usart->US_CR = AT91C_US_TXDIS;
85
    }
86
}
87
 
88
//------------------------------------------------------------------------------
89
/// Enables or disables the receiver of an USART peripheral
90
/// \param usart  Pointer to an USART peripheral
91
/// \param enabled  If true, the receiver is enabled; otherwise it is disabled.
92
//------------------------------------------------------------------------------
93
void USART_SetReceiverEnabled(AT91S_USART *usart,
94
                                     unsigned char enabled)
95
{
96
    if (enabled) {
97
 
98
        usart->US_CR = AT91C_US_RXEN;
99
    }
100
    else {
101
 
102
        usart->US_CR = AT91C_US_RXDIS;
103
    }
104
}
105
 
106
//------------------------------------------------------------------------------
107
/// Sends one packet of data through the specified USART peripheral. This
108
/// function operates synchronously, so it only returns when the data has been
109
/// actually sent.
110
/// \param usart  Pointer to an USART peripheral.
111
/// \param data  Data to send including 9nth bit and sync field if necessary (in
112
///              the same format as the US_THR register in the datasheet).
113
/// \param timeOut  Time out value (0 = no timeout).
114
//------------------------------------------------------------------------------
115
void USART_Write(
116
    AT91S_USART *usart,
117
    unsigned short data,
118
    volatile unsigned int timeOut)
119
{
120
    if (timeOut == 0) {
121
 
122
        while ((usart->US_CSR & AT91C_US_TXEMPTY) == 0);
123
    }
124
    else {
125
 
126
        while ((usart->US_CSR & AT91C_US_TXEMPTY) == 0) {
127
 
128
            if (timeOut == 0) {
129
 
130
                trace_LOG(trace_ERROR, "-E- USART_Write: Timed out.\n\r");
131
                return;
132
            }
133
            timeOut--;
134
        }
135
    }
136
 
137
    usart->US_THR = data;
138
}
139
 
140
//------------------------------------------------------------------------------
141
/// Sends the contents of a data buffer through the specified USART peripheral.
142
/// This function returns immediately (1 if the buffer has been queued, 0
143
/// otherwise); poll the ENDTX and TXBUFE bits of the USART status register
144
/// to check for the transfer completion.
145
/// \param usart  Pointer to an USART peripheral.
146
/// \param buffer  Pointer to the data buffer to send.
147
/// \param size  Size of the data buffer (in bytes).
148
//------------------------------------------------------------------------------
149
unsigned char USART_WriteBuffer(
150
    AT91S_USART *usart,
151
    void *buffer,
152
    unsigned int size)
153
{
154
    // Check if the first PDC bank is free
155
    if ((usart->US_TCR == 0) && (usart->US_TNCR == 0)) {
156
 
157
        usart->US_TPR = (unsigned int) buffer;
158
        usart->US_TCR = size;
159
        usart->US_PTCR = AT91C_PDC_TXTEN;
160
 
161
        return 1;
162
    }
163
    // Check if the second PDC bank is free
164
    else if (usart->US_TNCR == 0) {
165
 
166
        usart->US_TNPR = (unsigned int) buffer;
167
        usart->US_TNCR = size;
168
 
169
        return 1;
170
    }
171
    else {
172
 
173
        return 0;
174
    }
175
}
176
 
177
//------------------------------------------------------------------------------
178
/// Reads and return a packet of data on the specified USART peripheral. This
179
/// function operates asynchronously, so it waits until some data has been
180
/// received.
181
/// \param usart  Pointer to an USART peripheral.
182
/// \param timeOut  Time out value (0 -> no timeout).
183
//------------------------------------------------------------------------------
184
unsigned short USART_Read(
185
    AT91S_USART *usart,
186
    volatile unsigned int timeOut)
187
{
188
    if (timeOut == 0) {
189
 
190
        while ((usart->US_CSR & AT91C_US_RXRDY) == 0);
191
    }
192
    else {
193
 
194
        while ((usart->US_CSR & AT91C_US_RXRDY) == 0) {
195
 
196
            if (timeOut == 0) {
197
 
198
                trace_LOG(trace_ERROR, "-E- USART_Read: Timed out.\n\r");
199
                return 0;
200
            }
201
            timeOut--;
202
        }
203
    }
204
 
205
    return usart->US_RHR;
206
}
207
 
208
//------------------------------------------------------------------------------
209
/// Reads data from an USART peripheral, filling the provided buffer until it
210
/// becomes full. This function returns immediately with 1 if the buffer has
211
/// been queued for transmission; otherwise 0.
212
/// \param usart  Pointer to an USART peripheral.
213
/// \param buffer  Pointer to the buffer where the received data will be stored.
214
/// \param size  Size of the data buffer (in bytes).
215
//------------------------------------------------------------------------------
216
unsigned char USART_ReadBuffer(AT91S_USART *usart,
217
                                      void *buffer,
218
                                      unsigned int size)
219
{
220
    // Check if the first PDC bank is free
221
    if ((usart->US_RCR == 0) && (usart->US_RNCR == 0)) {
222
 
223
        usart->US_RPR = (unsigned int) buffer;
224
        usart->US_RCR = size;
225
        usart->US_PTCR = AT91C_PDC_RXTEN;
226
 
227
        return 1;
228
    }
229
    // Check if the second PDC bank is free
230
    else if (usart->US_RNCR == 0) {
231
 
232
        usart->US_RNPR = (unsigned int) buffer;
233
        usart->US_RNCR = size;
234
 
235
        return 1;
236
    }
237
    else {
238
 
239
        return 0;
240
    }
241
}
242
 
243
//------------------------------------------------------------------------------
244
/// Returns 1 if some data has been received and can be read from an USART;
245
/// otherwise returns 0.
246
/// \param usart  Pointer to an AT91S_USART instance.
247
//------------------------------------------------------------------------------
248
unsigned char USART_IsDataAvailable(AT91S_USART *usart)
249
{
250
    if ((usart->US_CSR & AT91C_US_RXRDY) != 0) {
251
 
252
        return 1;
253
    }
254
    else {
255
 
256
        return 0;
257
    }
258
}
259
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.