⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi.c

📁 IAR5.2下 AT91SAM9260 ARM 对 MCP2515 控制源化码
💻 C
字号:
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support 
 * ----------------------------------------------------------------------------
 * Copyright (c) 2008, Atmel Corporation
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ----------------------------------------------------------------------------
 */

//------------------------------------------------------------------------------
//         Headers
//------------------------------------------------------------------------------

#include "spi.h"
#include <pio/pio.h>
#include <aic/aic.h>
#include <pdc/pdc.h>

#define SPI0_INTERRUPT_LEVEL        7  	// 定义Spi的中断优先级为最高优先级

unsigned char r_flag = 1;

//------------------------------------------------------------------------------
//         Exported functions
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
/// Enables a SPI peripheral
/// \param spi  Pointer to an AT91S_SPI instance.
//------------------------------------------------------------------------------
void SPI_Enable(AT91S_SPI *spi)
{
    spi->SPI_CR = AT91C_SPI_SPIEN;
}

//------------------------------------------------------------------------------
/// Disables a SPI peripheral.
/// \param spi  Pointer to an AT91S_SPI instance.
//------------------------------------------------------------------------------
void SPI_Disable(AT91S_SPI *spi)
{
    spi->SPI_CR = AT91C_SPI_SPIDIS;
}

//------------------------------------------------------------------------------
/// Configures a SPI peripheral as specified. The configuration can be computed
/// using several macros (see "SPI configuration macros") and the constants
/// defined in LibV3 (AT91C_SPI_*).
/// \param spi  Pointer to an AT91S_SPI instance.
/// \param id  Peripheral ID of the SPI.
/// \param configuration  Value of the SPI configuration register.
//------------------------------------------------------------------------------
void SPI_Configure(AT91S_SPI *spi,
                   unsigned int id,
                   unsigned int configuration)
{
    AT91C_BASE_PMC->PMC_PCER = 1 << id;                 // 配制 SPI 电源时钟
    spi->SPI_CR = AT91C_SPI_SPIDIS | AT91C_SPI_SWRST;   // Disable and reset the SPI
    spi->SPI_MR = configuration;
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_SPI_CfgCs
//* \brief Configure SPI chip select register
//*----------------------------------------------------------------------------
void SPI_CfgCs (  AT91PS_SPI pSPI,     	// pointer to a SPI controller
	                int cs,     	// SPI cs number (0 to 3)
 	                int val)   	//  chip select register
{
	//* Write to the CSR register
	*(pSPI->SPI_CSR + cs) = val;
}


//------------------------------------------------------------------------------
/// Configures a chip select of a SPI peripheral. The chip select configuration
/// is computed using the definition provided by the LibV3 (AT91C_SPI_*).
/// \param spi  Pointer to an AT91S_SPI instance.
/// \param npcs  Chip select to configure (1, 2, 3 or 4).
/// \param configuration  Desired chip select configuration.
//------------------------------------------------------------------------------
void SPI_ConfigureNPCS(AT91S_SPI *spi,
                       unsigned int npcs,
                       unsigned int configuration)
{
    spi->SPI_CSR[npcs] = configuration;
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_SPI_CfgPCS
//* \brief Switch to the correct PCS of SPI Mode Register : Fixed Peripheral Selected
//*----------------------------------------------------------------------------
void SPI_CfgPCS (AT91PS_SPI pSPI, 			// pointer to a SPI controller
	         char PCS_Device) 			// PCS of the Device
{	
 	//* Write to the MR register
	pSPI->SPI_MR &= 0xFFF0FFFF;
	pSPI->SPI_MR |= ( (PCS_Device<<16) & AT91C_SPI_PCS );
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_SPI_EnableIt
//* \brief Enable SPI interrupt
//*----------------------------------------------------------------------------
void SPI_EnableIt (unsigned int flag)   // IT to be enabled
{
	//* Write to the IER register
	AT91C_BASE_SPI0->SPI_IER = flag;
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_SPI_DisableIt
//* \brief Disable SPI interrupt
//*----------------------------------------------------------------------------
void SPI_DisableIt (unsigned int flag) // IT to be disabled
{
	//* Write to the IDR register
	AT91C_BASE_SPI0->SPI_IDR = flag;
}


//------------------------------------------------------------------------------
/// Sends data through a SPI peripheral. If the SPI is configured to use a fixed
/// peripheral select, the npcs value is meaningless. Otherwise, it identifies
/// the component which shall be addressed.
/// \param spi  Pointer to an AT91S_SPI instance.
/// \param npcs  Chip select of the component to address (0, 1, 2 or 3).
/// \param data  Word of data to send.
//------------------------------------------------------------------------------
void SPI_Write(AT91S_SPI *spi, unsigned int npcs, unsigned short data)
{
    char NPCS[4] = {0x0E,0x0D,0xB0,0x07};
    // Discard contents of RDR register
    //volatile unsigned int discard = spi->SPI_RDR;

    // Send data
    while ((spi->SPI_SR & AT91C_SPI_TXEMPTY) == 0);
    spi->SPI_TDR = data | (NPCS[npcs]<<16);
    while ((spi->SPI_SR & AT91C_SPI_TDRE) == 0);
}

//------------------------------------------------------------------------------
/// Sends the contents of buffer through a SPI peripheral, using the PDC to
/// take care of the transfer.
/// \param spi  Pointer to an AT91S_SPI instance.
/// \param buffer  Data buffer to send.
/// \param length  Length of the data buffer.
//------------------------------------------------------------------------------
unsigned char SPI_WriteBuffer(AT91S_SPI *spi,
                              void *buffer,
                              unsigned int length)
{
    // Check if first bank is free
    if (spi->SPI_TCR == 0) 
    {
        spi->SPI_TPR = (unsigned int) buffer;
        spi->SPI_TCR = length;
        spi->SPI_PTCR = AT91C_PDC_TXTEN;
        return 1;
    }
    // Check if second bank is free
    else if (spi->SPI_TNCR == 0) 
    {
        spi->SPI_TNPR = (unsigned int) buffer;
        spi->SPI_TNCR = length;
        return 1;
    }   
    // No free banks
    return 0;
}

//------------------------------------------------------------------------------
/// Returns 1 if there is no pending write operation on the SPI; otherwise
/// returns 0.
/// \param pSpi  Pointer to an AT91S_SPI instance.
//------------------------------------------------------------------------------
unsigned char SPI_IsFinished(AT91S_SPI *pSpi)
{
    return ((pSpi->SPI_SR & AT91C_SPI_TXEMPTY) != 0);
}

//------------------------------------------------------------------------------
/// Reads and returns the last word of data received by a SPI peripheral. This
/// method must be called after a successful SPI_Write call.
/// \param spi  Pointer to an AT91S_SPI instance.
//------------------------------------------------------------------------------
unsigned short SPI_Read(AT91S_SPI *spi)
{
    while ((spi->SPI_SR & AT91C_SPI_RDRF) == 0);
    return spi->SPI_RDR & 0xFFFF;
}

//------------------------------------------------------------------------------
/// Reads data from a SPI peripheral until the provided buffer is filled. This
/// method does NOT need to be called after SPI_Write or SPI_WriteBuffer.
/// \param spi  Pointer to an AT91S_SPI instance.
/// \param buffer  Data buffer to store incoming bytes.
/// \param length  Length in bytes of the data buffer.
//------------------------------------------------------------------------------
unsigned char SPI_ReadBuffer(AT91S_SPI *spi,
                             void *buffer,
                             unsigned int length)
{
    // Check if the first bank is free
    if (spi->SPI_RCR == 0) 
    {
        spi->SPI_RPR = (unsigned int) buffer;
        spi->SPI_RCR = length;
        spi->SPI_PTCR = AT91C_PDC_RXTEN;
        return 1;
    }
    // Check if second bank is free
    else if (spi->SPI_RNCR == 0) 
    {
        spi->SPI_RNPR = (unsigned int) buffer;
        spi->SPI_RNCR = length;
        return 1;
    }
    // No free bank
    return 0;
}


//*****************************************************************************
//* 函数名称:AT91F_DataFlashHandler
//* 函数功能:SPI0 中断处理程序
//* 入口参数:status: SPI0 中断状态
//* 返回值 :无
//*****************************************************************************
void SPI0_Handler(unsigned int status)
{
    //* If End of Receive Transfer interrupt occurred
    if (( status & AT91C_SPI_RXBUFF))
    {
        //* Disable the Transmit Interrupt
        AT91C_BASE_SPI0->SPI_IDR  = AT91C_SPI_RXBUFF;
        AT91C_BASE_SPI0->SPI_PTCR = AT91C_PDC_TXTDIS + AT91C_PDC_RXTDIS;
        r_flag = 0;
        return;  
    }          		
    AT91C_BASE_SPI0->SPI_PTCR = AT91C_PDC_TXTDIS + AT91C_PDC_RXTDIS;
    AT91C_BASE_SPI0->SPI_IDR = status;
}


//*****************************************************************************
//* 函数名称:AT91F_SPI_Handler
//* 函数功能:SPI0 中断入口
//* 入口参数:无
//* 返回值 :无
//*****************************************************************************
void AT91F_SPI0_Handler(void)
{
    unsigned int status = 0;
    
    AIC_DisableIT(AT91C_ID_SPI0);
    
    status = AT91C_BASE_SPI0->SPI_SR;
    status &= AT91C_BASE_SPI0->SPI_IMR;
    
    SPI0_Handler(status);
    // 允许SPI0中断
    AIC_EnableIT(AT91C_ID_SPI0);
}


void init_SPI0(void)
{
    long long int mod;
  
    //* Configure SPI in Master Mode with No CS selected !!!
    SPI_Configure(AT91C_BASE_SPI0, AT91C_ID_SPI0, AT91C_SPI_MSTR | AT91C_SPI_MODFDIS | AT91C_SPI_PCS);
    
    //* Configure SPI CS0 for MCP2515 see the datasheet!
    mod = (AT91C_SPI_DLYBCT & 0x80) | (AT91C_SPI_DLYBS & 0x100000) | AT91C_SPI_NCPHA | (0x30 << 8);
    SPI_CfgCs(AT91C_BASE_SPI0,0, mod);

    //* 配制 PIO 为 SPI0 驱动
    PIO_CfgPeriph(AT91C_BASE_PIOA, 				// PIO controller base address
		  ((unsigned int) AT91C_PA1_SPI0_MOSI)  |
		  ((unsigned int) AT91C_PA3_SPI0_NPCS0) |
		  ((unsigned int) AT91C_PA0_SPI0_MISO)  |
		  ((unsigned int) AT91C_PA2_SPI0_SPCK), 	// Peripheral A
	          0); 						// Peripheral B
    
    AT91F_PDC_Open(AT91C_BASE_PDC_SPI0);			// 打开 SPI0 PDC
    // 配制 SPI0 中断
    AIC_ConfigureIt(AT91C_BASE_AIC,
		    AT91C_ID_SPI0,
	            SPI0_INTERRUPT_LEVEL,
	            AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,
		    AT91F_SPI0_Handler);
    
    SPI_Enable(AT91C_BASE_SPI0);			        // 使能 SPI0
    SPI_EnableIt(0x1<<6);		                        //允许接收缓冲区满中断---RXBUFF
    AIC_EnableIT(AT91C_ID_SPI0);			        // 使能 SPI0 中断
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -