Christina Quast | 637ace9 | 2014-11-28 13:28:37 +0100 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------------
|
| 2 | * ATMEL Microcontroller Software Support
|
| 3 | * ----------------------------------------------------------------------------
|
| 4 | * Copyright (c) 2009, 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 conditions are 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 | * Implementation of SPI PDC driver.
|
| 34 | *
|
| 35 | */
|
| 36 |
|
| 37 | #ifndef _SPI_PDC_
|
| 38 | #define _SPI_PDC_
|
| 39 |
|
| 40 | /*----------------------------------------------------------------------------
|
| 41 | * Headers
|
| 42 | *----------------------------------------------------------------------------*/
|
| 43 | #include "chip.h"
|
| 44 |
|
| 45 | /*----------------------------------------------------------------------------
|
| 46 | * Definitions
|
| 47 | *----------------------------------------------------------------------------*/
|
| 48 |
|
| 49 | /** An unspecified error has occured.*/
|
| 50 | #define SPID_ERROR 1
|
| 51 |
|
| 52 | /** SPI driver is currently in use.*/
|
| 53 | #define SPID_ERROR_LOCK 2
|
| 54 |
|
| 55 | /*----------------------------------------------------------------------------
|
| 56 | * Macros
|
| 57 | *----------------------------------------------------------------------------*/
|
| 58 |
|
| 59 | /** Calculates the value of the SCBR field of the Chip Select Register given MCK and SPCK.*/
|
| 60 | #define SPID_CSR_SCBR(mck, spck) (SPI_CSR_SCBR(((mck) / (spck))) )
|
| 61 |
|
| 62 | /** Calculates the value of the DLYBS field of the Chip Select Register given delay in ns and MCK.*/
|
| 63 | #define SPID_CSR_DLYBS(mck, delay) ( SPI_CSR_DLYBS(((((delay) * ((mck) / 1000000)) / 1000) + 1)) )
|
| 64 |
|
| 65 | /** Calculates the value of the DLYBCT field of the Chip Select Register given delay in ns and MCK.*/
|
| 66 | #define SPID_CSR_DLYBCT(mck, delay) ( SPI_CSR_DLYBCT((((delay) / 32 * ((mck) / 1000000)) / 1000) + 1) )
|
| 67 |
|
| 68 | #ifdef __cplusplus
|
| 69 | extern "C" {
|
| 70 | #endif
|
| 71 |
|
| 72 | /*----------------------------------------------------------------------------
|
| 73 | * Types
|
| 74 | *----------------------------------------------------------------------------*/
|
| 75 |
|
| 76 | /** SPI transfer complete callback. */
|
| 77 | typedef void (*SpidCallback)( uint8_t, void* ) ;
|
| 78 |
|
| 79 | /** \brief Spi Transfer Request prepared by the application upper layer.
|
| 80 | *
|
| 81 | * This structure is sent to the SPI_SendCommand function to start the transfer.
|
| 82 | * At the end of the transfer, the callback is invoked by the interrupt handler.
|
| 83 | */
|
| 84 | typedef struct _SpidCmd
|
| 85 | {
|
| 86 | /** Pointer to the command data. */
|
| 87 | uint8_t *pCmd;
|
| 88 | /** Command size in bytes. */
|
| 89 | uint8_t cmdSize;
|
| 90 | /** Pointer to the data to be sent. */
|
| 91 | uint8_t *pData;
|
| 92 | /** Data size in bytes. */
|
| 93 | unsigned short dataSize;
|
| 94 | /** SPI chip select. */
|
| 95 | uint8_t spiCs;
|
| 96 | /** Callback function invoked at the end of transfer. */
|
| 97 | SpidCallback callback;
|
| 98 | /** Callback arguments. */
|
| 99 | void *pArgument;
|
| 100 | } SpidCmd ;
|
| 101 |
|
| 102 | /** Constant structure associated with SPI port. This structure prevents
|
| 103 | client applications to have access in the same time. */
|
| 104 | typedef struct _Spid
|
| 105 | {
|
| 106 | /** Pointer to SPI Hardware registers */
|
| 107 | Spi* pSpiHw ;
|
| 108 | /** SPI Id as defined in the product datasheet */
|
| 109 | char spiId ;
|
| 110 | /** Current SpiCommand being processed */
|
| 111 | SpidCmd *pCurrentCommand ;
|
| 112 | /** Mutual exclusion semaphore. */
|
| 113 | volatile char semaphore ;
|
| 114 | } Spid ;
|
| 115 |
|
| 116 | /*----------------------------------------------------------------------------
|
| 117 | * Exported functions
|
| 118 | *----------------------------------------------------------------------------*/
|
| 119 |
|
| 120 | extern uint32_t SPID_Configure( Spid* pSpid, Spi* pSpiHw, uint8_t spiId ) ;
|
| 121 |
|
| 122 | extern void SPID_ConfigureCS( Spid* pSpid, uint32_t dwCS, uint32_t dwCsr ) ;
|
| 123 |
|
| 124 | extern uint32_t SPID_SendCommand( Spid* pSpid, SpidCmd* pCommand ) ;
|
| 125 |
|
| 126 | extern void SPID_Handler( Spid* pSpid ) ;
|
| 127 |
|
| 128 | extern uint32_t SPID_IsBusy( const Spid* pSpid ) ;
|
| 129 |
|
| 130 | #ifdef __cplusplus
|
| 131 | }
|
| 132 | #endif
|
| 133 |
|
| 134 | #endif /* #ifndef _SPI_PDC_ */
|
| 135 |
|