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