Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

# spi.c

    00001 /*! \file spi.c \brief SPI Interface Driver for AT91SAM7S. */
    00002 //*****************************************************************************
    00003 //
    00004 // File Name    : 'spi.c'
    00005 // Title        : SPI Interface Driver for AT91SAM7S
    00006 // Author       : Pascal Stang - Copyright (C) 2006
    00007 // Created      : 2006.02.01
    00008 // Revised      : 2006.02.01
    00009 // Version      : 0.1
    00010 // Target MCU   : ARM processors
    00011 // Editor Tabs  : 4
    00012 //
    00013 // NOTE: This code is currently below version 1.0, and therefore is considered
    00014 // to be lacking in some functionality or documentation, or may not be fully
    00015 // tested.  Nonetheless, you can expect most functions to work.
    00016 //
    00017 // This code is distributed under the GNU Public License
    00018 //      which can be found at http://www.gnu.org/licenses/gpl.txt
    00019 //
    00020 //*****************************************************************************
    00021
    00022 #include "at91sam7s64.h"
    00023 #include "global.h"
    00024 #include "spi.h"
    00025
    00026 #ifndef SPI_SCKDIV
    00027 #define SPI_SCKDIV      2
    00028 #endif
    00029
    00030 void spiInit(void)
    00031 {
    00032     // enable clock to SPI interface
    00033     AT91C_BASE_PMC->PMC_PCER = (1<<AT91C_ID_SPI);
    00034
    00035     // setup PIO pins for SPI bus
    00036     *AT91C_PIOA_ASR   = AT91C_PA12_MISO|AT91C_PA13_MOSI|AT91C_PA14_SPCK;    // assign pins to SPI interface
    00037     *AT91C_PIOA_PDR   = AT91C_PA12_MISO|AT91C_PA13_MOSI|AT91C_PA14_SPCK;
    00038     *AT91C_PIOA_PPUER = AT91C_PA12_MISO|AT91C_PA13_MOSI|AT91C_PA14_SPCK;    // set pullups
    00039     // setup PIO pins for SPI chip selects
    00040     //AT91C_BASE_PIOA->PIO_ASR = AT91C_PA11_NPCS0|AT91C_PA31_NPCS1;
    00041     //AT91C_BASE_PIOA->PIO_PDR = AT91C_PA11_NPCS0|AT91C_PA31_NPCS1;
    00042     //AT91C_BASE_PIOA->PIO_OER = AT91C_PA11_NPCS0|AT91C_PA31_NPCS1;
    00043
    00044     // reset and enable SPI
    00045     *AT91C_SPI_CR = AT91C_SPI_SPIEN | AT91C_SPI_SWRST;
    00046     *AT91C_SPI_CR = AT91C_SPI_SPIEN;
    00047
    00048     // set master mode with:
    00049     //  - SPI master
    00050     //  - no mode fault
    00051     //  - variable peripheral chip select
    00052     *AT91C_SPI_MR = AT91C_SPI_MODFDIS | AT91C_SPI_PS_VARIABLE | AT91C_SPI_MSTR;
    00053 //  *AT91C_SPI_MR = AT91C_SPI_MODFDIS | AT91C_SPI_PS_FIXED | AT91C_SPI_MSTR | (0x0E<<16);
    00054
    00055     // setup data transfer format and rate for device 0-3 => 8bits, CPOL=0, NCPHA=1
    00056     AT91C_SPI_CSR[0] = AT91C_SPI_NCPHA|AT91C_SPI_BITS_8|(SPI_SCKDIV<<8);
    00057     AT91C_SPI_CSR[1] = AT91C_SPI_NCPHA|AT91C_SPI_BITS_8|(SPI_SCKDIV<<8);
    00058     AT91C_SPI_CSR[2] = AT91C_SPI_NCPHA|AT91C_SPI_BITS_8|(SPI_SCKDIV<<8);
    00059     AT91C_SPI_CSR[3] = AT91C_SPI_NCPHA|AT91C_SPI_BITS_8|(SPI_SCKDIV<<8);
    00060 }
    00061
    00062 u08 spiTransferByte(u08 data)
    00063 {
    00064     // wait for transmit completion/ready
    00065     while(!(*AT91C_SPI_SR & AT91C_SPI_TDRE));
    00066     // write data to be transmitted
    00067     *AT91C_SPI_TDR = data;
    00068     // wait for completion
    00069     while(!(*AT91C_SPI_SR & AT91C_SPI_RDRF));
    00070     // return received data
    00071     return *AT91C_SPI_RDR;
    00072 }
    00073
    00074 u16 spiTransfer16(u16 data)
    00075 {
    00076     // wait for transmit completion/ready
    00077     while(!(*AT91C_SPI_SR & AT91C_SPI_TDRE));
    00078     // write data to be transmitted
    00079     *AT91C_SPI_TDR = data;
    00080     // wait for completion
    00081     while(!(*AT91C_SPI_SR & AT91C_SPI_RDRF));
    00082     // return received data
    00083     return *AT91C_SPI_RDR;
    00084 }
    00085
    00086 u32 spiTransfer32(u32 data)
    00087 {
    00088     u32 rxdata;
    00089
    00090     // wait for transmit completion/ready
    00091     while(!(*AT91C_SPI_SR & AT91C_SPI_TDRE));
    00092     // write data to be transmitted
    00093     *AT91C_SPI_TDR = data>>16;
    00094     // wait for completion
    00095     while(!(*AT91C_SPI_SR & AT91C_SPI_RDRF));
    00096     // get received data
    00097     rxdata = (*AT91C_SPI_RDR&0xFFFF)<<16;
    00098     // write data to be transmitted
    00099     *AT91C_SPI_TDR = data;
    00100     // wait for completion
    00101     while(!(*AT91C_SPI_SR & AT91C_SPI_RDRF));
    00102     // get received data
    00103     rxdata |= (*AT91C_SPI_RDR&0xFFFF);
    00104     // return received data
    00105     return rxdata;
    00106 }
    00107
    00108 /*
    00109 void spiSetClockDiv(u08 clockdiv)
    00110 {
    00111     //SPCCR = clockdiv;
    00112 }
    00113
    00114 void spiSend(u16 data)
    00115 {
    00116     // write data to be transmitted
    00117     *AT91C_SPI_TDR = data;
    00118 }
    00119 */