| 1 |
608 |
jeremybenn |
/* ----------------------------------------------------------------------------
|
| 2 |
|
|
* ATMEL Microcontroller Software Support
|
| 3 |
|
|
* ----------------------------------------------------------------------------
|
| 4 |
|
|
* Copyright (c) 2008, 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 |
|
|
/// \dir
|
| 32 |
|
|
/// !Purpose
|
| 33 |
|
|
///
|
| 34 |
|
|
/// Definitions for SPI peripheral usage.
|
| 35 |
|
|
///
|
| 36 |
|
|
/// !Usage
|
| 37 |
|
|
///
|
| 38 |
|
|
/// -# Enable the SPI pins required by the application (see pio.h).
|
| 39 |
|
|
/// -# Configure the SPI using the SPI_Configure function. This enables the
|
| 40 |
|
|
/// peripheral clock. The mode register is loaded with the given value.
|
| 41 |
|
|
/// -# Configure all the necessary chip selects with SPI_ConfigureNPCS.
|
| 42 |
|
|
/// -# Enable the SPI by calling SPI_Enable.
|
| 43 |
|
|
/// -# Send/receive data using SPI_Write and SPI_Read. Note that SPI_Read
|
| 44 |
|
|
/// must be called after SPI_Write to retrieve the last value read.
|
| 45 |
|
|
/// -# Send/receive data using the PDC with the SPI_WriteBuffer and
|
| 46 |
|
|
/// SPI_ReadBuffer functions.
|
| 47 |
|
|
/// -# Disable the SPI by calling SPI_Disable.
|
| 48 |
|
|
//------------------------------------------------------------------------------
|
| 49 |
|
|
|
| 50 |
|
|
#ifndef SPI_H
|
| 51 |
|
|
#define SPI_H
|
| 52 |
|
|
|
| 53 |
|
|
//------------------------------------------------------------------------------
|
| 54 |
|
|
// Headers
|
| 55 |
|
|
//------------------------------------------------------------------------------
|
| 56 |
|
|
|
| 57 |
|
|
#include <board.h>
|
| 58 |
|
|
|
| 59 |
|
|
//------------------------------------------------------------------------------
|
| 60 |
|
|
// Definitions
|
| 61 |
|
|
//------------------------------------------------------------------------------
|
| 62 |
|
|
|
| 63 |
|
|
//------------------------------------------------------------------------------
|
| 64 |
|
|
/// \page "SPI configuration macros"
|
| 65 |
|
|
/// This page lists several macros which should be used when configuring a SPI
|
| 66 |
|
|
/// peripheral.
|
| 67 |
|
|
///
|
| 68 |
|
|
/// !Macros
|
| 69 |
|
|
/// - SPI_PCS
|
| 70 |
|
|
/// - SPI_SCBR
|
| 71 |
|
|
/// - SPI_DLYBS
|
| 72 |
|
|
/// - SPI_DLYBCT
|
| 73 |
|
|
|
| 74 |
|
|
/// Calculate the PCS field value given the chip select NPCS value
|
| 75 |
|
|
#define SPI_PCS(npcs) ((~(1 << npcs) & 0xF) << 16)
|
| 76 |
|
|
|
| 77 |
|
|
/// Calculates the value of the CSR SCBR field given the baudrate and MCK.
|
| 78 |
|
|
#define SPI_SCBR(baudrate, masterClock) \
|
| 79 |
|
|
((unsigned int) (masterClock / baudrate) << 8)
|
| 80 |
|
|
|
| 81 |
|
|
/// Calculates the value of the CSR DLYBS field given the desired delay (in ns)
|
| 82 |
|
|
#define SPI_DLYBS(delay, masterClock) \
|
| 83 |
|
|
((unsigned int) (((masterClock / 1000000) * delay) / 1000) << 16)
|
| 84 |
|
|
|
| 85 |
|
|
/// Calculates the value of the CSR DLYBCT field given the desired delay (in ns)
|
| 86 |
|
|
#define SPI_DLYBCT(delay, masterClock) \
|
| 87 |
|
|
((unsigned int) (((masterClock / 1000000) * delay) / 32000) << 16)
|
| 88 |
|
|
//------------------------------------------------------------------------------
|
| 89 |
|
|
|
| 90 |
|
|
//------------------------------------------------------------------------------
|
| 91 |
|
|
// Exported functions
|
| 92 |
|
|
//------------------------------------------------------------------------------
|
| 93 |
|
|
extern void SPI_Enable(AT91S_SPI *spi);
|
| 94 |
|
|
extern void SPI_Disable(AT91S_SPI *spi);
|
| 95 |
|
|
extern void SPI_Configure(AT91S_SPI *spi,
|
| 96 |
|
|
unsigned int id,
|
| 97 |
|
|
unsigned int configuration);
|
| 98 |
|
|
extern void SPI_ConfigureNPCS(AT91S_SPI *spi,
|
| 99 |
|
|
unsigned int npcs,
|
| 100 |
|
|
unsigned int configuration);
|
| 101 |
|
|
extern void SPI_Write(AT91S_SPI *spi, unsigned int npcs, unsigned short data);
|
| 102 |
|
|
extern unsigned char SPI_WriteBuffer(AT91S_SPI *spi,
|
| 103 |
|
|
void *buffer,
|
| 104 |
|
|
unsigned int length);
|
| 105 |
|
|
|
| 106 |
|
|
extern unsigned char SPI_IsFinished(AT91S_SPI *pSpi);
|
| 107 |
|
|
|
| 108 |
|
|
extern unsigned short SPI_Read(AT91S_SPI *spi);
|
| 109 |
|
|
extern unsigned char SPI_ReadBuffer(AT91S_SPI *spi,
|
| 110 |
|
|
void *buffer,
|
| 111 |
|
|
unsigned int length);
|
| 112 |
|
|
|
| 113 |
|
|
#endif //#ifndef SPI_H
|
| 114 |
|
|
|