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/] [spi/] [spi.c] - Blame information for rev 580

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

powered by: WebSVN 2.1.0

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