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 * 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 /** Pointer to the Tx data. */ 00092 uint8_t *pTxBuff; 00093 /** Tx size in bytes. */ 00094 uint8_t TxSize; 00095 /** Pointer to the Rx data. */ 00096 uint8_t *pRxBuff; 00097 /** Rx size in bytes. */ 00098 uint16_t RxSize; 00099 /** SPI chip select. */ 00100 uint8_t spiCs; 00101 /** Callback function invoked at the end of transfer. */ 00102 SpidCallback callback; 00103 /** Callback arguments. */ 00104 void *pArgument; 00105 } SpidCmd; 00106 00107 /** Constant structure associated with SPI port. This structure prevents 00108 client applications to have access in the same time. */ 00109 typedef struct _Spid { 00110 /** Pointer to SPI Hardware registers */ 00111 Spi *pSpiHw; 00112 /** Current SpiCommand being processed */ 00113 SpidCmd *pCurrentCommand; 00114 /** Pointer to DMA driver */ 00115 sXdmad *pXdmad; 00116 /** SPI Id as defined in the product datasheet */ 00117 uint8_t spiId; 00118 /** Mutual exclusion semaphore. */ 00119 volatile int8_t semaphore; 00120 } Spid; 00121 00122 /*---------------------------------------------------------------------------- 00123 * Exported functions 00124 *----------------------------------------------------------------------------*/ 00125 00126 extern uint32_t SPID_Configure(Spid *pSpid, 00127 Spi *pSpiHw, 00128 uint8_t spiId, 00129 uint32_t SpiMode, 00130 sXdmad *pXdmad); 00131 00132 extern void SPID_ConfigureCS(Spid *pSpid, uint32_t dwCS, uint32_t dwCsr); 00133 00134 extern uint32_t SPID_SendCommand(Spid *pSpid, SpidCmd *pCommand); 00135 00136 extern void SPID_Handler(Spid *pSpid); 00137 00138 extern void SPID_DmaHandler(Spid *pSpid); 00139 00140 extern uint32_t SPID_IsBusy(const Spid *pSpid); 00141 00142 #ifdef __cplusplus 00143 } 00144 #endif 00145 00146 #endif /* #ifndef _SPI_DMA_ */