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
34 /// Definitions for SPI peripheral usage.
\r
38 /// -# Enable the SPI pins required by the application (see pio.h).
\r
39 /// -# Configure the SPI using the SPI_Configure function. This enables the
\r
40 /// peripheral clock. The mode register is loaded with the given value.
\r
41 /// -# Configure all the necessary chip selects with SPI_ConfigureNPCS.
\r
42 /// -# Enable the SPI by calling SPI_Enable.
\r
43 /// -# Send/receive data using SPI_Write and SPI_Read. Note that SPI_Read
\r
44 /// must be called after SPI_Write to retrieve the last value read.
\r
45 /// -# Send/receive data using the PDC with the SPI_WriteBuffer and
\r
46 /// SPI_ReadBuffer functions.
\r
47 /// -# Disable the SPI by calling SPI_Disable.
\r
48 //------------------------------------------------------------------------------
\r
53 //------------------------------------------------------------------------------
\r
55 //------------------------------------------------------------------------------
\r
59 //------------------------------------------------------------------------------
\r
61 //------------------------------------------------------------------------------
\r
63 //------------------------------------------------------------------------------
\r
64 /// \page "SPI configuration macros"
\r
65 /// This page lists several macros which should be used when configuring a SPI
\r
74 /// Calculate the PCS field value given the chip select NPCS value
\r
75 #define SPI_PCS(npcs) ((~(1 << npcs) & 0xF) << 16)
\r
77 /// Calculates the value of the CSR SCBR field given the baudrate and MCK.
\r
78 #define SPI_SCBR(baudrate, masterClock) \
\r
79 ((unsigned int) (masterClock / baudrate) << 8)
\r
81 /// Calculates the value of the CSR DLYBS field given the desired delay (in ns)
\r
82 #define SPI_DLYBS(delay, masterClock) \
\r
83 ((unsigned int) (((masterClock / 1000000) * delay) / 1000) << 16)
\r
85 /// Calculates the value of the CSR DLYBCT field given the desired delay (in ns)
\r
86 #define SPI_DLYBCT(delay, masterClock) \
\r
87 ((unsigned int) (((masterClock / 1000000) * delay) / 32000) << 16)
\r
88 //------------------------------------------------------------------------------
\r
90 //------------------------------------------------------------------------------
\r
91 // Exported functions
\r
92 //------------------------------------------------------------------------------
\r
93 extern void SPI_Enable(AT91S_SPI *spi);
\r
94 extern void SPI_Disable(AT91S_SPI *spi);
\r
95 extern void SPI_Configure(AT91S_SPI *spi,
\r
97 unsigned int configuration);
\r
98 extern void SPI_ConfigureNPCS(AT91S_SPI *spi,
\r
100 unsigned int configuration);
\r
101 extern void SPI_Write(AT91S_SPI *spi, unsigned int npcs, unsigned short data);
\r
102 extern unsigned char SPI_WriteBuffer(AT91S_SPI *spi,
\r
104 unsigned int length);
\r
106 extern unsigned char SPI_IsFinished(AT91S_SPI *pSpi);
\r
108 extern unsigned short SPI_Read(AT91S_SPI *spi);
\r
109 extern unsigned char SPI_ReadBuffer(AT91S_SPI *spi,
\r
111 unsigned int length);
\r
113 #endif //#ifndef SPI_H
\r