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/] [spi/] [spi.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 "spi.h"
35
 
36
//------------------------------------------------------------------------------
37
//         Exported functions
38
//------------------------------------------------------------------------------
39
//------------------------------------------------------------------------------
40
/// Enables a SPI peripheral
41
/// \param spi  Pointer to an AT91S_SPI instance.
42
//------------------------------------------------------------------------------
43
void SPI_Enable(AT91S_SPI *spi)
44
{
45
    spi->SPI_CR = AT91C_SPI_SPIEN;
46
}
47
 
48
//------------------------------------------------------------------------------
49
/// Disables a SPI peripheral.
50
/// \param spi  Pointer to an AT91S_SPI instance.
51
//------------------------------------------------------------------------------
52
void SPI_Disable(AT91S_SPI *spi)
53
{
54
    spi->SPI_CR = AT91C_SPI_SPIDIS;
55
}
56
 
57
//------------------------------------------------------------------------------
58
/// Configures a SPI peripheral as specified. The configuration can be computed
59
/// using several macros (see "SPI configuration macros") and the constants
60
/// defined in LibV3 (AT91C_SPI_*).
61
/// \param spi  Pointer to an AT91S_SPI instance.
62
/// \param id  Peripheral ID of the SPI.
63
/// \param configuration  Value of the SPI configuration register.
64
//------------------------------------------------------------------------------
65
void SPI_Configure(AT91S_SPI *spi,
66
                          unsigned int id,
67
                          unsigned int configuration)
68
{
69
    AT91C_BASE_PMC->PMC_PCER = 1 << id;
70
    spi->SPI_CR = AT91C_SPI_SPIDIS | AT91C_SPI_SWRST;
71
    spi->SPI_MR = configuration;
72
}
73
 
74
//------------------------------------------------------------------------------
75
/// Configures a chip select of a SPI peripheral. The chip select configuration
76
/// is computed using the definition provided by the LibV3 (AT91C_SPI_*).
77
/// \param spi  Pointer to an AT91S_SPI instance.
78
/// \param npcs  Chip select to configure (1, 2, 3 or 4).
79
/// \param configuration  Desired chip select configuration.
80
//------------------------------------------------------------------------------
81
void SPI_ConfigureNPCS(AT91S_SPI *spi,
82
                              unsigned int npcs,
83
                              unsigned int configuration)
84
{
85
    spi->SPI_CSR[npcs] = configuration;
86
}
87
 
88
//------------------------------------------------------------------------------
89
/// Sends data through a SPI peripheral. If the SPI is configured to use a fixed
90
/// peripheral select, the npcs value is meaningless. Otherwise, it identifies
91
/// the component which shall be addressed.
92
/// \param spi  Pointer to an AT91S_SPI instance.
93
/// \param npcs  Chip select of the component to address (1, 2, 3 or 4).
94
/// \param data  Word of data to send.
95
//------------------------------------------------------------------------------
96
void SPI_Write(AT91S_SPI *spi, unsigned int npcs, unsigned short data)
97
{
98
    // Discard contents of RDR register
99
    //volatile unsigned int discard = spi->SPI_RDR;
100
 
101
    // Send data
102
    while ((spi->SPI_SR & AT91C_SPI_TXEMPTY) == 0);
103
    spi->SPI_TDR = data | SPI_PCS(npcs);
104
    while ((spi->SPI_SR & AT91C_SPI_TDRE) == 0);
105
}
106
 
107
//------------------------------------------------------------------------------
108
/// Sends the contents of buffer through a SPI peripheral, using the PDC to
109
/// take care of the transfer.
110
/// \param spi  Pointer to an AT91S_SPI instance.
111
/// \param buffer  Data buffer to send.
112
/// \param length  Length of the data buffer.
113
//------------------------------------------------------------------------------
114
unsigned char SPI_WriteBuffer(AT91S_SPI *spi,
115
                                     void *buffer,
116
                                     unsigned int length)
117
{
118
    // Check if first bank is free
119
    if (spi->SPI_TCR == 0) {
120
 
121
        spi->SPI_TPR = (unsigned int) buffer;
122
        spi->SPI_TCR = length;
123
        spi->SPI_PTCR = AT91C_PDC_TXTEN;
124
        return 1;
125
    }
126
    // Check if second bank is free
127
    else if (spi->SPI_TNCR == 0) {
128
 
129
        spi->SPI_TNPR = (unsigned int) buffer;
130
        spi->SPI_TNCR = length;
131
        return 1;
132
    }
133
 
134
    // No free banks
135
    return 0;
136
}
137
 
138
//------------------------------------------------------------------------------
139
/// Returns 1 if there is no pending write operation on the SPI; otherwise
140
/// returns 0.
141
/// \param pSpi  Pointer to an AT91S_SPI instance.
142
//------------------------------------------------------------------------------
143
unsigned char SPI_IsFinished(AT91S_SPI *pSpi)
144
{
145
    return ((pSpi->SPI_SR & AT91C_SPI_TXEMPTY) != 0);
146
}
147
 
148
//------------------------------------------------------------------------------
149
/// Reads and returns the last word of data received by a SPI peripheral. This
150
/// method must be called after a successful SPI_Write call.
151
/// \param spi  Pointer to an AT91S_SPI instance.
152
//------------------------------------------------------------------------------
153
unsigned short SPI_Read(AT91S_SPI *spi)
154
{
155
    while ((spi->SPI_SR & AT91C_SPI_RDRF) == 0);
156
    return spi->SPI_RDR & 0xFFFF;
157
}
158
 
159
//------------------------------------------------------------------------------
160
/// Reads data from a SPI peripheral until the provided buffer is filled. This
161
/// method does NOT need to be called after SPI_Write or SPI_WriteBuffer.
162
/// \param spi  Pointer to an AT91S_SPI instance.
163
/// \param buffer  Data buffer to store incoming bytes.
164
/// \param length  Length in bytes of the data buffer.
165
//------------------------------------------------------------------------------
166
unsigned char SPI_ReadBuffer(AT91S_SPI *spi,
167
                                    void *buffer,
168
                                    unsigned int length)
169
{
170
    // Check if the first bank is free
171
    if (spi->SPI_RCR == 0) {
172
 
173
        spi->SPI_RPR = (unsigned int) buffer;
174
        spi->SPI_RCR = length;
175
        spi->SPI_PTCR = AT91C_PDC_RXTEN;
176
        return 1;
177
    }
178
    // Check if second bank is free
179
    else if (spi->SPI_RNCR == 0) {
180
 
181
        spi->SPI_RNPR = (unsigned int) buffer;
182
        spi->SPI_RNCR = length;
183
        return 1;
184
    }
185
 
186
    // No free bank
187
    return 0;
188
}
189
 

powered by: WebSVN 2.1.0

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