1 /* ----------------------------------------------------------------------------
2  *         SAM Software Package License
3  * ----------------------------------------------------------------------------
4  * Copyright (c) 2011-2012, 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 condition is 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  * \file
32  *
33  * Interface for Serial Peripheral Interface (SPI) controller.
34  *
35  */
36 
37 #ifndef _SPI_
38 #define _SPI_
39 
40 /*----------------------------------------------------------------------------
41  *        Headers
42  *----------------------------------------------------------------------------*/
43 
44 #include "../chip.h"
45 
46 /*----------------------------------------------------------------------------
47  *        Macros
48  *----------------------------------------------------------------------------*/
49 
50 /**
51  *
52  * Here are several macros which should be used when configuring a SPI
53  * peripheral.
54  *
55  * \section spi_configuration_macros SPI Configuration Macros
56  * - \ref SPI_PCS
57  * - \ref SPI_SCBR
58  * - \ref SPI_DLYBS
59  * - \ref SPI_DLYBCT
60  */
61 
62 /** Calculate the PCS field value given the chip select NPCS value */
63 #define SPI_PCS(npcs)       ((~(1 << (npcs)) & 0xF) << 16)
64 
65 /** Calculates the value of the CSR SCBR field given the baudrate and MCK. */
66 #define SPI_SCBR(baudrate, masterClock) ((uint32_t) ((masterClock) / (baudrate)) << 8)
67 
68 /** Calculates the value of the CSR DLYBS field given the desired delay (in ns) */
69 #define SPI_DLYBS(delay, masterClock) ((uint32_t) ((((masterClock) / 1000000) * (delay)) / 1000) << 16)
70 
71 /** Calculates the value of the CSR DLYBCT field given the desired delay (in ns) */
72 #define SPI_DLYBCT(delay, masterClock) ((uint32_t) ((((masterClock) / 1000000) * (delay)) / 32000) << 24)
73 
74 /*------------------------------------------------------------------------------ */
75 
76 #ifdef __cplusplus
77  extern "C" {
78 #endif
79 
80 /*----------------------------------------------------------------------------
81  *        Exported functions
82  *----------------------------------------------------------------------------*/
83 
84 extern void SPI_Enable( Spi* spi ) ;
85 extern void SPI_Disable( Spi* spi ) ;
86 extern void SPI_EnableIt( Spi* spi, uint32_t dwSources ) ;
87 extern void SPI_DisableIt( Spi* spi, uint32_t dwSources ) ;
88 
89 extern void SPI_Configure( Spi* spi, uint32_t dwId, uint32_t dwConfiguration ) ;
90 extern void SPI_ConfigureNPCS( Spi* spi, uint32_t dwNpcs, uint32_t dwConfiguration ) ;
91 
92 extern uint32_t SPI_Read( Spi* spi ) ;
93 extern void SPI_Write( Spi* spi, uint32_t dwNpcs, uint16_t wData ) ;
94 
95 extern uint32_t SPI_GetStatus( Spi* spi ) ;
96 extern uint32_t SPI_IsFinished( Spi* pSpi ) ;
97 
98 #if (defined _SAM3S_) || (defined _SAM3S8_) || (defined _SAM3N_)
99 extern void SPI_PdcEnableTx( Spi* spi ) ;
100 extern void SPI_PdcDisableTx( Spi* spi ) ;
101 extern void SPI_PdcEnableRx( Spi* spi ) ;
102 extern void SPI_PdcDisableRx( Spi* spi ) ;
103 
104 extern void SPI_PdcSetTx( Spi* spi, void* pvTxBuf, uint32_t dwTxCount, void* pvTxNextBuf, uint32_t dwTxNextCount ) ;
105 extern void SPI_PdcSetRx( Spi* spi, void* pvRxBuf, uint32_t dwRxCount, void* pvRxNextBuf, uint32_t dwRxNextCount ) ;
106 
107 extern uint32_t SPI_WriteBuffer( Spi* spi, void* pvBuffer, uint32_t dwLength ) ;
108 
109 extern uint32_t SPI_ReadBuffer( Spi* spi, void* pvBuffer, uint32_t dwLength ) ;
110 #endif /* (defined _SAM3S_) || (defined _SAM3S8_) || (defined _SAM3N_) */
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 #endif /* #ifndef _SPI_ */
117 
118