1 /* ----------------------------------------------------------------------------
\r
2 * ATMEL Microcontroller Software Support
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2008, Atmel Corporation
\r
6 * All rights reserved.
\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
11 * - Redistributions of source code must retain the above copyright notice,
\r
12 * this list of conditions and the disclaimer below.
\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
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
30 //------------------------------------------------------------------------------
\r
32 //------------------------------------------------------------------------------
\r
36 //------------------------------------------------------------------------------
\r
37 // Exported functions
\r
38 //------------------------------------------------------------------------------
\r
39 //------------------------------------------------------------------------------
\r
40 /// Enables a SPI peripheral
\r
41 /// \param spi Pointer to an AT91S_SPI instance.
\r
42 //------------------------------------------------------------------------------
\r
43 void SPI_Enable(AT91S_SPI *spi)
\r
45 spi->SPI_CR = AT91C_SPI_SPIEN;
\r
48 //------------------------------------------------------------------------------
\r
49 /// Disables a SPI peripheral.
\r
50 /// \param spi Pointer to an AT91S_SPI instance.
\r
51 //------------------------------------------------------------------------------
\r
52 void SPI_Disable(AT91S_SPI *spi)
\r
54 spi->SPI_CR = AT91C_SPI_SPIDIS;
\r
57 //------------------------------------------------------------------------------
\r
58 /// Configures a SPI peripheral as specified. The configuration can be computed
\r
59 /// using several macros (see "SPI configuration macros") and the constants
\r
60 /// defined in LibV3 (AT91C_SPI_*).
\r
61 /// \param spi Pointer to an AT91S_SPI instance.
\r
62 /// \param id Peripheral ID of the SPI.
\r
63 /// \param configuration Value of the SPI configuration register.
\r
64 //------------------------------------------------------------------------------
\r
65 void SPI_Configure(AT91S_SPI *spi,
\r
67 unsigned int configuration)
\r
69 AT91C_BASE_PMC->PMC_PCER = 1 << id;
\r
70 spi->SPI_CR = AT91C_SPI_SPIDIS | AT91C_SPI_SWRST;
\r
71 spi->SPI_MR = configuration;
\r
74 //------------------------------------------------------------------------------
\r
75 /// Configures a chip select of a SPI peripheral. The chip select configuration
\r
76 /// is computed using the definition provided by the LibV3 (AT91C_SPI_*).
\r
77 /// \param spi Pointer to an AT91S_SPI instance.
\r
78 /// \param npcs Chip select to configure (1, 2, 3 or 4).
\r
79 /// \param configuration Desired chip select configuration.
\r
80 //------------------------------------------------------------------------------
\r
81 void SPI_ConfigureNPCS(AT91S_SPI *spi,
\r
83 unsigned int configuration)
\r
85 spi->SPI_CSR[npcs] = configuration;
\r
88 //------------------------------------------------------------------------------
\r
89 /// Sends data through a SPI peripheral. If the SPI is configured to use a fixed
\r
90 /// peripheral select, the npcs value is meaningless. Otherwise, it identifies
\r
91 /// the component which shall be addressed.
\r
92 /// \param spi Pointer to an AT91S_SPI instance.
\r
93 /// \param npcs Chip select of the component to address (1, 2, 3 or 4).
\r
94 /// \param data Word of data to send.
\r
95 //------------------------------------------------------------------------------
\r
96 void SPI_Write(AT91S_SPI *spi, unsigned int npcs, unsigned short data)
\r
98 // Discard contents of RDR register
\r
99 //volatile unsigned int discard = spi->SPI_RDR;
\r
102 while ((spi->SPI_SR & AT91C_SPI_TXEMPTY) == 0);
\r
103 spi->SPI_TDR = data | SPI_PCS(npcs);
\r
104 while ((spi->SPI_SR & AT91C_SPI_TDRE) == 0);
\r
107 //------------------------------------------------------------------------------
\r
108 /// Sends the contents of buffer through a SPI peripheral, using the PDC to
\r
109 /// take care of the transfer.
\r
110 /// \param spi Pointer to an AT91S_SPI instance.
\r
111 /// \param buffer Data buffer to send.
\r
112 /// \param length Length of the data buffer.
\r
113 //------------------------------------------------------------------------------
\r
114 unsigned char SPI_WriteBuffer(AT91S_SPI *spi,
\r
116 unsigned int length)
\r
118 // Check if first bank is free
\r
119 if (spi->SPI_TCR == 0) {
\r
121 spi->SPI_TPR = (unsigned int) buffer;
\r
122 spi->SPI_TCR = length;
\r
123 spi->SPI_PTCR = AT91C_PDC_TXTEN;
\r
126 // Check if second bank is free
\r
127 else if (spi->SPI_TNCR == 0) {
\r
129 spi->SPI_TNPR = (unsigned int) buffer;
\r
130 spi->SPI_TNCR = length;
\r
138 //------------------------------------------------------------------------------
\r
139 /// Returns 1 if there is no pending write operation on the SPI; otherwise
\r
141 /// \param pSpi Pointer to an AT91S_SPI instance.
\r
142 //------------------------------------------------------------------------------
\r
143 unsigned char SPI_IsFinished(AT91S_SPI *pSpi)
\r
145 return ((pSpi->SPI_SR & AT91C_SPI_TXEMPTY) != 0);
\r
148 //------------------------------------------------------------------------------
\r
149 /// Reads and returns the last word of data received by a SPI peripheral. This
\r
150 /// method must be called after a successful SPI_Write call.
\r
151 /// \param spi Pointer to an AT91S_SPI instance.
\r
152 //------------------------------------------------------------------------------
\r
153 unsigned short SPI_Read(AT91S_SPI *spi)
\r
155 while ((spi->SPI_SR & AT91C_SPI_RDRF) == 0);
\r
156 return spi->SPI_RDR & 0xFFFF;
\r
159 //------------------------------------------------------------------------------
\r
160 /// Reads data from a SPI peripheral until the provided buffer is filled. This
\r
161 /// method does NOT need to be called after SPI_Write or SPI_WriteBuffer.
\r
162 /// \param spi Pointer to an AT91S_SPI instance.
\r
163 /// \param buffer Data buffer to store incoming bytes.
\r
164 /// \param length Length in bytes of the data buffer.
\r
165 //------------------------------------------------------------------------------
\r
166 unsigned char SPI_ReadBuffer(AT91S_SPI *spi,
\r
168 unsigned int length)
\r
170 // Check if the first bank is free
\r
171 if (spi->SPI_RCR == 0) {
\r
173 spi->SPI_RPR = (unsigned int) buffer;
\r
174 spi->SPI_RCR = length;
\r
175 spi->SPI_PTCR = AT91C_PDC_RXTEN;
\r
178 // Check if second bank is free
\r
179 else if (spi->SPI_RNCR == 0) {
\r
181 spi->SPI_RNPR = (unsigned int) buffer;
\r
182 spi->SPI_RNCR = length;
\r