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

📄 main.c

📁 测试SPI接口
💻 C
字号:
// Include Standard LIB  files
#include "AT91SAM7X-EK.h"
#include "include/AT91SAM7X256.h"
#include "include/lib_AT91SAM7X256.h"

#define AT91_BAUD_RATE				115200
#define	WAIT_TIME					AT91B_MCK
#define AT91C_BASE_SPI				AT91C_BASE_SPI0

#define USART0_INTERRUPT_LEVEL		6
#define PIO_INTERRUPT_LEVEL     	5
#define FIQ_INTERRUPT_LEVEL     	7  // Always high

#define MASTER_CLOCK				48000000
#define MCK							48000000
#define DATAFLASH_CLK				20000000	//

extern void FIQ_init_handler  (void);
extern void pio_c_irq_handler (void);
extern void AT91F_SPI_Handler (void);

extern void AT91F_CfgSPIForDataFlash(void);
extern char SPI_DATAFLASH_Test(char *databuffer);

static const char szembed_header[]=
{
"\n\r================================================"
"\n\r==         Test SPI DATAFLASH Program         =="
"\n\r================================================\n\r"
};

unsigned char	r_flag;
unsigned char	value_write_flag=0x00;
char			value_write[2]={0};
volatile int	loop;

//*----------------------------------------------------------------------------
//* Function Name       : delay
//* Object              : Wait
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions calAT91B_LED    : none
//*----------------------------------------------------------------------------
void delay ( void )
{
//* Set in Volatile for Optimisation
    volatile unsigned int    i ;
//* loop delay
    for ( i = 0 ;(i < WAIT_TIME/50 );i++ ) ;
}

//*--------------------------------------------------------------------------------------
//* Function Name       : wait
//* Object              : Software waiting loop
//* Input Parameters    : none. Waiting time is defined by the global variable LedSpeed.
//* Output Parameters   : none
//*--------------------------------------------------------------------------------------
void wait ( void )
{
	//Wait at least 10 ms 
    for (loop=0; loop<10000; loop++){};
}

//*----------------------------------------------------------------------------
//* Function Name       : Usart_c_irq_handler
//* Object              : C handler interrupt function 
//* Input Parameters    : none
//* Output Parameters   : none
//*----------------------------------------------------------------------------
void Usart_c_irq_handler(void)
{
	char status;
	AT91PS_USART COM0 = AT91C_BASE_US0;
	//* get Usart status register
	status = COM0->US_CSR;
	if ( status & AT91C_US_RXRDY)
	{
	//* Get byte to write to eeprom
	    value_write[0]=AT91F_US_GetChar(COM0);
	    AT91F_US_PutChar(COM0,value_write[0]);
	    value_write_flag=0x01;
	}
	//* Check error
	if ( status & AT91C_US_FRAME)
	{
		AT91F_US_PutChar (COM0, 'F');
	}
	//* Reset the satus bit
	COM0 ->US_CR = AT91C_US_RSTSTA;
}

//*----------------------------------------------------------------------------
//* Function Name       : Usart_init
//* Object              : USART initialization
//* Input Parameters    : none
//* Output Parameters   : TRUE
//*----------------------------------------------------------------------------
void Usart_init ( void )
{
	AT91PS_USART COM0 = AT91C_BASE_US0;
   	// First, enable the clock of the USART
	AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1 << AT91C_ID_US0 ) ;
	//* Configure PIO controllers to periph mode
	AT91F_PIO_CfgPeriph( AT91C_BASE_PIOA,
 		((unsigned int) AT91C_PA0_RXD0    ) |
 		((unsigned int) AT91C_PA1_TXD0    ) |
 		((unsigned int) AT91C_PA3_RTS0    ) |
 		((unsigned int) AT91C_PA4_CTS0    ), // Peripheral A
 		0); // Peripheral B
	// Usart Configure
	AT91F_US_Configure (COM0, AT91B_MCK, AT91C_US_ASYNC_MODE, AT91_BAUD_RATE, 0);
	// Enable usart
	COM0->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
	//* Enable USART IT error and RXRDY
    AT91F_US_EnableIt(COM0, AT91C_US_FRAME | AT91C_US_RXRDY);
   	//* open Usart 0 interrupt
	AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC,
							AT91C_ID_US0,
							USART0_INTERRUPT_LEVEL,
							AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,
							Usart_c_irq_handler);
	AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_US0);
 	AT91F_US_SendFrame(COM0,(char *)szembed_header,sizeof(szembed_header),0,0);
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_SPI_CfgSPI
//* \brief Config SPI IP
//*----------------------------------------------------------------------------
void  AT91F_SPI_CfgSPI(void)
{
	//* Reset the SPI0
	AT91F_SPI_Reset(AT91C_BASE_SPI0);
    //* Configure SPI0 in Master Mode--Fixed peripher----Mode detection disenable---NPCS0
	AT91F_SPI_CfgMode(AT91C_BASE_SPI0, AT91C_SPI_MSTR | AT91C_SPI_MODFDIS | (0xE<<16));
    //* Configure SPI CS0 for DataFlash AT45DB021B 8 bite
	AT91F_SPI_CfgCs(AT91C_BASE_SPI0,0, AT91C_SPI_CPOL | (AT91C_SPI_DLYBS & 0x100000) |((MCK/ (DATAFLASH_CLK)) << 8));
    //* Enable the SPI
    AT91F_SPI_Enable(AT91C_BASE_SPI0);
}

//*----------------------------------------------------------------------------
//* Function Name       : main
//* Object              : Main interrupt function
//* Input Parameters    : none
//* Output Parameters   : TRUE
//*----------------------------------------------------------------------------
int main( void )
{
	AT91PS_USART COM0 = AT91C_BASE_US0;
    Usart_init();
    // Enable User Reset and set its minimal assertion to 960 us
    AT91C_BASE_RSTC->RSTC_RMR = AT91C_RSTC_URSTEN | (0x4<<8) | (unsigned int)(0xA5<<24);
	//open port
    AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1 << AT91C_ID_PIOB );
    AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1 << AT91C_ID_PIOA );
    // Init SPI 
	//  \brief Configure PIO controllers to drive SPI signals
	AT91F_SPI0_CfgPIO();
    //  \brief Enable Peripheral clock in PMC for  SPI	
	AT91F_SPI0_CfgPMC();
    //  \brief Config SPI IP
	AT91F_SPI_CfgSPI();

    AT91F_PDC_Open(AT91C_BASE_PDC_SPI0);

    AT91F_CfgSPIForDataFlash();

    // Configure SPI interrupt
	AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
			      AT91C_ID_SPI0,
			      AT91C_AIC_PRIOR_HIGHEST,
			      AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,
			      AT91F_SPI_Handler);
						
	// Enable SPI interrupt
	AT91F_AIC_EnableIt(AT91C_BASE_AIC,AT91C_ID_SPI0);

    //test dataflash
    while(1)
    {
    	if(value_write_flag==0x01)
    	{
    		value_write[1]=SPI_DATAFLASH_Test(value_write);
    		if ((value_write[1]!= 0xff)&&(value_write[1]!= 0x00))
			{
				AT91F_US_PutChar(COM0,'<');
				wait();
				AT91F_US_PutChar(COM0,value_write[1]);
				wait();
				AT91F_US_PutChar(COM0,'>');
				wait();
				AT91F_US_PutChar(COM0,'\n');
				wait();
				AT91F_US_PutChar(COM0,'\r');
				wait();
			}
			else
			{
				AT91F_US_PutChar(COM0,'<');
				wait();
				AT91F_US_PutChar(COM0,'E');
				wait();
				AT91F_US_PutChar(COM0,'R');
				wait();
				AT91F_US_PutChar(COM0,'R');
				wait();
				AT91F_US_PutChar(COM0,'O');
				wait();
				AT91F_US_PutChar(COM0,'R');
				wait();
				AT91F_US_PutChar(COM0,'>');
				wait();
				AT91F_US_PutChar(COM0,'\n');
				wait();
				AT91F_US_PutChar(COM0,'\r');
				wait();
			}
    		value_write_flag=0x00;
		}
	}
}

⌨️ 快捷键说明

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