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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [CORTEX_AT91SAM3U256_IAR/] [AT91Lib/] [peripherals/] [usart/] [usart.c] - Blame information for rev 590

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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