]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/drivers/Atmel/at91lib/peripherals/spi/spi.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / Common / drivers / Atmel / at91lib / peripherals / spi / spi.c
1 /* ----------------------------------------------------------------------------\r
2  *         ATMEL Microcontroller Software Support \r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2008, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\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
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\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
16  *\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
28  */\r
29 \r
30 //------------------------------------------------------------------------------\r
31 //         Headers\r
32 //------------------------------------------------------------------------------\r
33 \r
34 #include "spi.h"\r
35 \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
44 {\r
45     spi->SPI_CR = AT91C_SPI_SPIEN;\r
46 }\r
47 \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
53 {\r
54     spi->SPI_CR = AT91C_SPI_SPIDIS;\r
55 }\r
56 \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
66                           unsigned int id,\r
67                           unsigned int configuration)\r
68 {\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
72 }\r
73 \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
82                               unsigned int npcs,\r
83                               unsigned int configuration)\r
84 {\r
85     spi->SPI_CSR[npcs] = configuration;\r
86 }\r
87 \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
97 {\r
98     // Discard contents of RDR register\r
99     //volatile unsigned int discard = spi->SPI_RDR;\r
100 \r
101     // Send data\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
105 }\r
106 \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
115                                      void *buffer,\r
116                                      unsigned int length)\r
117 {\r
118     // Check if first bank is free\r
119     if (spi->SPI_TCR == 0) {\r
120 \r
121         spi->SPI_TPR = (unsigned int) buffer;\r
122         spi->SPI_TCR = length;\r
123         spi->SPI_PTCR = AT91C_PDC_TXTEN;\r
124         return 1;\r
125     }\r
126     // Check if second bank is free\r
127     else if (spi->SPI_TNCR == 0) {\r
128 \r
129         spi->SPI_TNPR = (unsigned int) buffer;\r
130         spi->SPI_TNCR = length;\r
131         return 1;\r
132     }\r
133       \r
134     // No free banks\r
135     return 0;\r
136 }\r
137 \r
138 //------------------------------------------------------------------------------\r
139 /// Returns 1 if there is no pending write operation on the SPI; otherwise\r
140 /// returns 0.\r
141 /// \param pSpi  Pointer to an AT91S_SPI instance.\r
142 //------------------------------------------------------------------------------\r
143 unsigned char SPI_IsFinished(AT91S_SPI *pSpi)\r
144 {\r
145     return ((pSpi->SPI_SR & AT91C_SPI_TXEMPTY) != 0);\r
146 }\r
147 \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
154 {\r
155     while ((spi->SPI_SR & AT91C_SPI_RDRF) == 0);\r
156     return spi->SPI_RDR & 0xFFFF;\r
157 }\r
158 \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
167                                     void *buffer,\r
168                                     unsigned int length)\r
169 {\r
170     // Check if the first bank is free\r
171     if (spi->SPI_RCR == 0) {\r
172 \r
173         spi->SPI_RPR = (unsigned int) buffer;\r
174         spi->SPI_RCR = length;\r
175         spi->SPI_PTCR = AT91C_PDC_RXTEN;\r
176         return 1;\r
177     }\r
178     // Check if second bank is free\r
179     else if (spi->SPI_RNCR == 0) {\r
180 \r
181         spi->SPI_RNPR = (unsigned int) buffer;\r
182         spi->SPI_RNCR = length;\r
183         return 1;\r
184     }\r
185 \r
186     // No free bank\r
187     return 0;\r
188 }\r
189 \r