1 /* ----------------------------------------------------------------------------
\r
2 * ATMEL Microcontroller Software Support
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2009, 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
33 * Implementation of SPI driver, transfer data through DMA.
\r
40 /*----------------------------------------------------------------------------
\r
42 *----------------------------------------------------------------------------*/
\r
46 /*----------------------------------------------------------------------------
\r
48 *----------------------------------------------------------------------------*/
\r
50 /** An unspecified error has occured.*/
\r
51 #define SPID_ERROR 1
\r
53 /** SPI driver is currently in use.*/
\r
54 #define SPID_ERROR_LOCK 2
\r
56 /*----------------------------------------------------------------------------
\r
58 *----------------------------------------------------------------------------*/
\r
60 /** Calculates the value of the SCBR field of the Chip Select Register given MCK and SPCK.*/
\r
61 #define SPID_CSR_SCBR(mck, spck) SPI_CSR_SCBR((mck) / (spck))
\r
63 /** Calculates the value of the DLYBS field of the Chip Select Register given delay in ns and MCK.*/
\r
64 #define SPID_CSR_DLYBS(mck, delay) SPI_CSR_DLYBS((((delay) * ((mck) / 1000000)) / 1000) + 1)
\r
66 /** Calculates the value of the DLYBCT field of the Chip Select Register given delay in ns and MCK.*/
\r
67 #define SPID_CSR_DLYBCT(mck, delay) SPI_CSR_DLYBCT((((delay) / 32 * ((mck) / 1000000)) / 1000) + 1)
\r
73 /*----------------------------------------------------------------------------
\r
75 *----------------------------------------------------------------------------*/
\r
77 /** SPI transfer complete callback. */
\r
78 typedef void (*SpidCallback)( uint8_t, void* ) ;
\r
80 /** \brief Spi Transfer Request prepared by the application upper layer.
\r
82 * This structure is sent to the SPI_SendCommand function to start the transfer.
\r
83 * At the end of the transfer, the callback is invoked by the interrupt handler.
\r
85 typedef struct _SpidCmd
\r
87 /** Pointer to the Tx data. */
\r
89 /** Tx size in bytes. */
\r
91 /** Pointer to the Rx data. */
\r
93 /** Rx size in bytes. */
\r
95 /** SPI chip select. */
\r
97 /** Callback function invoked at the end of transfer. */
\r
98 SpidCallback callback;
\r
99 /** Callback arguments. */
\r
103 /** Constant structure associated with SPI port. This structure prevents
\r
104 client applications to have access in the same time. */
\r
105 typedef struct _Spid
\r
107 /** Pointer to SPI Hardware registers */
\r
109 /** Current SpiCommand being processed */
\r
110 SpidCmd *pCurrentCommand ;
\r
111 /** Pointer to DMA driver */
\r
113 /** SPI Id as defined in the product datasheet */
\r
115 /** Mutual exclusion semaphore. */
\r
116 volatile int8_t semaphore ;
\r
119 /*----------------------------------------------------------------------------
\r
120 * Exported functions
\r
121 *----------------------------------------------------------------------------*/
\r
123 extern uint32_t SPID_Configure( Spid* pSpid,
\r
129 extern void SPID_ConfigureCS( Spid* pSpid, uint32_t dwCS, uint32_t dwCsr ) ;
\r
131 extern uint32_t SPID_SendCommand( Spid* pSpid, SpidCmd* pCommand ) ;
\r
133 extern void SPID_Handler( Spid* pSpid ) ;
\r
135 extern void SPID_DmaHandler( Spid *pSpid );
\r
137 extern uint32_t SPID_IsBusy( const Spid* pSpid ) ;
\r
143 #endif /* #ifndef _SPI_DMA_ */
\r