00001 /* ---------------------------------------------------------------------------- 00002 * SAM Software Package License 00003 * ---------------------------------------------------------------------------- 00004 * Copyright (c) 2011, Atmel Corporation 00005 * 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * 00011 * - Redistributions of source code must retain the above copyright notice, 00012 * this list of conditions and the disclaimer below. 00013 * 00014 * Atmel's name may not be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00019 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00020 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 00023 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00024 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00025 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00026 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 * ---------------------------------------------------------------------------- 00028 */ 00029 00030 /** 00031 * \file 00032 * 00033 * Implementation of SPI driver, transfer data through DMA. 00034 * 00035 */ 00036 00037 #ifndef _SPI_DMA_ 00038 #define _SPI_DMA_ 00039 00040 /*---------------------------------------------------------------------------- 00041 * Headers 00042 *----------------------------------------------------------------------------*/ 00043 00044 #include "chip.h" 00045 00046 /*---------------------------------------------------------------------------- 00047 * Definitions 00048 *----------------------------------------------------------------------------*/ 00049 00050 /** An unspecified error has occurred.*/ 00051 #define SPID_ERROR 1 00052 00053 /** SPI driver is currently in use.*/ 00054 #define SPID_ERROR_LOCK 2 00055 00056 /*---------------------------------------------------------------------------- 00057 * Macros 00058 *----------------------------------------------------------------------------*/ 00059 00060 /** Calculates the value of the SCBR field of the Chip Select Register 00061 given MCK and SPCK.*/ 00062 #define SPID_CSR_SCBR(mck, spck) SPI_CSR_SCBR((mck) / (spck)) 00063 00064 /** Calculates the value of the DLYBS field of the Chip Select Register 00065 given delay in ns and MCK.*/ 00066 #define SPID_CSR_DLYBS(mck, delay) SPI_CSR_DLYBS((((delay) * \ 00067 ((mck) / 1000000)) / 1000) + 1) 00068 00069 /** Calculates the value of the DLYBCT field of the Chip Select Register 00070 given delay in ns and MCK.*/ 00071 #define SPID_CSR_DLYBCT(mck, delay) SPI_CSR_DLYBCT((((delay) / 32 * \ 00072 ((mck) / 1000000)) / 1000) + 1) 00073 00074 #ifdef __cplusplus 00075 extern "C" { 00076 #endif 00077 00078 /*---------------------------------------------------------------------------- 00079 * Types 00080 *----------------------------------------------------------------------------*/ 00081 00082 /** SPI transfer complete callback. */ 00083 typedef void (*SpidCallback)( uint8_t, void* ) ; 00084 00085 /** \brief Spi Transfer Request prepared by the application upper layer. 00086 * 00087 * This structure is sent to the SPI_SendCommand function to start the transfer. 00088 * At the end of the transfer, the callback is invoked by the interrupt handler. 00089 */ 00090 typedef struct _SpidCmd 00091 { 00092 /** Pointer to the Tx data. */ 00093 uint8_t *pTxBuff; 00094 /** Tx size in bytes. */ 00095 uint8_t TxSize; 00096 /** Pointer to the Rx data. */ 00097 uint8_t *pRxBuff; 00098 /** Rx size in bytes. */ 00099 uint16_t RxSize; 00100 /** SPI chip select. */ 00101 uint8_t spiCs; 00102 /** Callback function invoked at the end of transfer. */ 00103 SpidCallback callback; 00104 /** Callback arguments. */ 00105 void *pArgument; 00106 } SpidCmd ; 00107 00108 /** Constant structure associated with SPI port. This structure prevents 00109 client applications to have access in the same time. */ 00110 typedef struct _Spid 00111 { 00112 /** Pointer to SPI Hardware registers */ 00113 Spi* pSpiHw ; 00114 /** Current SpiCommand being processed */ 00115 SpidCmd *pCurrentCommand ; 00116 /** Pointer to DMA driver */ 00117 sXdmad* pXdmad; 00118 /** SPI Id as defined in the product datasheet */ 00119 uint8_t spiId ; 00120 /** Mutual exclusion semaphore. */ 00121 volatile int8_t semaphore ; 00122 } Spid ; 00123 00124 /*---------------------------------------------------------------------------- 00125 * Exported functions 00126 *----------------------------------------------------------------------------*/ 00127 00128 extern uint32_t SPID_Configure( Spid* pSpid, 00129 Spi* pSpiHw, 00130 uint8_t spiId, 00131 uint32_t SpiMode, 00132 sXdmad* pXdmad ) ; 00133 00134 extern void SPID_ConfigureCS( Spid* pSpid, uint32_t dwCS, uint32_t dwCsr ) ; 00135 00136 extern uint32_t SPID_SendCommand( Spid* pSpid, SpidCmd* pCommand ) ; 00137 00138 extern void SPID_Handler( Spid* pSpid ) ; 00139 00140 extern void SPID_DmaHandler( Spid *pSpid ); 00141 00142 extern uint32_t SPID_IsBusy( const Spid* pSpid ) ; 00143 00144 #ifdef __cplusplus 00145 } 00146 #endif 00147 00148 #endif /* #ifndef _SPI_DMA_ */