00001 /* ---------------------------------------------------------------------------- */ 00002 /* Atmel Microcontroller Software Support */ 00003 /* SAM Software Package License */ 00004 /* ---------------------------------------------------------------------------- */ 00005 /* Copyright (c) 2015, Atmel Corporation */ 00006 /* */ 00007 /* All rights reserved. */ 00008 /* */ 00009 /* Redistribution and use in source and binary forms, with or without */ 00010 /* modification, are permitted provided that the following condition is met: */ 00011 /* */ 00012 /* - Redistributions of source code must retain the above copyright notice, */ 00013 /* this list of conditions and the disclaimer below. */ 00014 /* */ 00015 /* Atmel's name may not be used to endorse or promote products derived from */ 00016 /* this software without specific prior written permission. */ 00017 /* */ 00018 /* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR */ 00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ 00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE */ 00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, */ 00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */ 00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */ 00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */ 00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */ 00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */ 00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 00028 /* ---------------------------------------------------------------------------- */ 00029 00030 /** 00031 * \file 00032 * 00033 * Interface for Serial Peripheral Interface (SPI) controller. 00034 * 00035 */ 00036 00037 #ifndef _SPI_ 00038 #define _SPI_ 00039 00040 /*---------------------------------------------------------------------------- 00041 * Headers 00042 *----------------------------------------------------------------------------*/ 00043 00044 #include "chip.h" 00045 00046 /*---------------------------------------------------------------------------- 00047 * Macros 00048 *----------------------------------------------------------------------------*/ 00049 00050 /** 00051 * 00052 * Here are several macros which should be used when configuring a SPI 00053 * peripheral. 00054 * 00055 * \section spi_configuration_macros SPI Configuration Macros 00056 * - \ref SPI_PCS 00057 * - \ref SPI_SCBR 00058 * - \ref SPI_DLYBS 00059 * - \ref SPI_DLYBCT 00060 */ 00061 00062 /** Calculate the PCS field value given the chip select NPCS value */ 00063 #define SPI_PCS(npcs) SPI_MR_PCS((~(1 << npcs) & 0xF)) 00064 00065 /** Calculates the value of the CSR SCBR field given the baudrate and MCK. */ 00066 #define SPI_SCBR(baudrate, masterClock) \ 00067 SPI_CSR_SCBR((uint32_t)(masterClock / baudrate)) 00068 00069 /** Calculates the value of the CSR DLYBS field given the desired delay (in ns) */ 00070 #define SPI_DLYBS(delay, masterClock) \ 00071 SPI_CSR_DLYBS((uint32_t) (((masterClock / 1000000) * delay) / 1000)+1) 00072 00073 /** Calculates the value of the CSR DLYBCT field given the desired delay (in ns) */ 00074 #define SPI_DLYBCT(delay, masterClock) \ 00075 SPI_CSR_DLYBCT ((uint32_t) (((masterClock / 1000000) * delay) / 32000)+1) 00076 00077 /*------------------------------------------------------------------------------ */ 00078 00079 #ifdef __cplusplus 00080 extern "C" { 00081 #endif 00082 00083 /*---------------------------------------------------------------------------- 00084 * Exported functions 00085 *----------------------------------------------------------------------------*/ 00086 00087 extern void SPI_Enable(Spi *spi); 00088 extern void SPI_Disable(Spi *spi); 00089 00090 extern void SPI_EnableIt(Spi *spi, uint32_t dwSources); 00091 extern void SPI_DisableIt(Spi *spi, uint32_t dwSources); 00092 00093 extern void SPI_Configure(Spi *spi, uint32_t dwId, uint32_t dwConfiguration); 00094 extern void SPI_SetMode(Spi *spi, uint32_t dwConfiguration); 00095 00096 extern void SPI_ChipSelect(Spi *spi, uint8_t cS); 00097 extern void SPI_ReleaseCS(Spi *spi); 00098 00099 extern void SPI_ConfigureNPCS(Spi *spi, uint32_t dwNpcs, 00100 uint32_t dwConfiguration); 00101 extern void SPI_ConfigureCSMode(Spi *spi, uint32_t dwNpcs, 00102 uint32_t bReleaseOnLast); 00103 00104 extern uint32_t SPI_Read(Spi *spi); 00105 extern void SPI_Write(Spi *spi, uint32_t dwNpcs, uint16_t wData); 00106 extern void SPI_WriteLast(Spi *spi, uint32_t dwNpcs, uint16_t wData); 00107 00108 extern uint32_t SPI_GetStatus(Spi *spi); 00109 extern uint32_t SPI_IsFinished(Spi *pSpi); 00110 00111 #ifdef __cplusplus 00112 } 00113 #endif 00114 00115 #endif /* #ifndef _SPI_ */ 00116