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

📄 main.c

📁 这是ATMEL公司
💻 C
字号:
//*----------------------------------------------------------------------------
//*         ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name           : main.c
//* Object              : main application written in C
//* Creation            : Hi   11/18/2002
//*
//*----------------------------------------------------------------------------
#include "AT91RM9200.h"
#include "lib_AT91RM9200.h"
#include "FLASH16x4.h"

#include <stdio.h>
#include <string.h>

//* Defines for the AT49BV1614 Flash test
#define AT91C_SECTOR_TO_TEST         39
#define AT91C_TEST_PATTERN           0xCAFE
#define AT91C_FIRST_SECTOR_TO_ERASE  38
#define AT91C_LAST_SECTOR_TO_ERASE   39


//* Interrupt Handlers
extern void AT91F_ST_ASM_HANDLER(void);
//* function in init.c
extern void AT91F_DBGU_Printk(char *buffer);

//* Flash structures 
AT91S_FLASH_INFO Flash16x4Info;	 
AT91S_FLASH16x4 Flash16x4;

//* Message buffer
char MsgBuffer[256];

//* system timer counter
unsigned int StTick = 0;


//*----------------------------------------------------------------------------
//* \fn    AT91F_GetTickCount
//* \brief This function returns the value of the system timer
//* This function is for demonstration purpose only
//*----------------------------------------------------------------------------
unsigned int AT91F_GetTickCount(void)
{
	return(StTick);
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_ST_HANDLER
//* \brief This function is invoked by main
//* This function is for demonstration purpose only
//*----------------------------------------------------------------------------
void AT91F_ST_HANDLER(void)
{
volatile int StStatus;
	// Read the system timer status register
	StStatus = *(AT91C_ST_SR);
	StTick++;
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_InitFlash
//* \brief This function performs low level HW initialization
//*----------------------------------------------------------------------------
void AT91F_InitFlash()
{
	//* Setup MEMC to support CS0=Flash
	AT91C_BASE_EBI->EBI_CSA |= AT91C_EBI_CS0A_SMC;
	AT91C_BASE_EBI->EBI_CFGR = (AT91C_EBI_DBPUC & 0x00) | (AT91C_EBI_EBSEN & 0x00);

	//* Setup Flash
	AT91C_BASE_SMC2->SMC2_CSR[0] = (AT91C_SMC2_NWS & 0x4)  | AT91C_SMC2_WSEN |
	                               (AT91C_SMC2_TDF & 0x200)| AT91C_SMC2_BAT  |
	                                AT91C_SMC2_DBW_16;
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_DisplayFlashId
//* \brief This function reads and prints the flash manufacturer and device code
//*----------------------------------------------------------------------------
void AT91F_DisplayFlashId(unsigned int FlashId)
{
	sprintf( MsgBuffer, "\n\r Manufacturer Code: 0x%X, Device Code: 0x%X", 
	                     (FlashId & 0x0000FFFF), (FlashId & 0xFFFF0000) >> 16);
	AT91F_DBGU_Printk(MsgBuffer); 
}



//*----------------------------------------------------------------------------
//* \fn    AT91F_TestWriteFlash
//* \brief This function tests the write command
//* 
//*----------------------------------------------------------------------------
void AT91F_TestWriteFlash(unsigned int SectorToTest, unsigned short TestPattern)
{
	unsigned short *pAddress    = (unsigned short*)(Flash16x4.pFlashInfo->start[SectorToTest]);
	unsigned short *pEndAddress;
	
	pEndAddress = pAddress + (MAIN_SECT_SIZE / sizeof(unsigned short));
	
	sprintf(MsgBuffer, "\n\r Write Sector[%d], Base address = 0x%X , Pattern = 0x%X", SectorToTest, (unsigned int)pAddress, TestPattern);
	AT91F_DBGU_Printk(MsgBuffer); 

	if (SectorToTest >= CFG_MAX_FLASH_SECT)
	{
		sprintf(MsgBuffer, "\n\r Unavailable Sector ");
		AT91F_DBGU_Printk(MsgBuffer); 
		return;
	}
	
	//* Write the sector 'SectorToTest' with the pattern 'TestPattern'
	while(pAddress < pEndAddress)
		if (AT91F_FLASH16x4Program(&Flash16x4, (void *)pAddress++, TestPattern) != AT91C_FLASH_READY)
			break;
	//* Test the result of the write 
	if (pAddress < pEndAddress)
	{
		sprintf(MsgBuffer, "\n\r Error Program: 0x%X", (unsigned int)pAddress);		
		AT91F_DBGU_Printk(MsgBuffer);
		return;
	}
	else
	{
		sprintf(MsgBuffer, "\n\r Program sector[%d] OK", SectorToTest);
		AT91F_DBGU_Printk(MsgBuffer);
	}
	
	//* Read the sector 'SectorToTest' and verify the pattern
	pAddress = (unsigned short*)(Flash16x4.pFlashInfo->start[SectorToTest]);
	while(pAddress < pEndAddress)
		if (*pAddress++ != TestPattern)
			break;
		
	if (pAddress < pEndAddress)
		sprintf(MsgBuffer, "\n\r Read Error: 0x%x", (unsigned int)pAddress);
	else
		sprintf(MsgBuffer, "\n\r Read OK : Data[%X]= 0x%X, Data[%X]= 0x%X",
		                    (unsigned int)Flash16x4.pFlashInfo->start[SectorToTest],
		                    *((unsigned short *)Flash16x4.pFlashInfo->start[SectorToTest]),
		                    (unsigned int)(pAddress-sizeof(unsigned short)),
		                    *(pAddress-sizeof(unsigned short)));
	AT91F_DBGU_Printk(MsgBuffer);
}



//*----------------------------------------------------------------------------
//* \fn    AT91F_TestEraseFlash
//* \brief This function tests the erase command
//* 
//*----------------------------------------------------------------------------
void AT91F_TestEraseFlash(unsigned int FirstSector, unsigned int LastSector)
{
	unsigned short *pStartAddress = (unsigned short*)(Flash16x4.pFlashInfo->start[FirstSector]);
	unsigned short *pEndAddress   = (unsigned short*)(Flash16x4.pFlashInfo->start[LastSector]);

	sprintf(MsgBuffer, "\n\r\n\r Erase Sector %d [%X] to Sector %d [%X]",
	                         (unsigned int)FirstSector, (unsigned int) pStartAddress,
	                         (unsigned int)LastSector,  (unsigned int) pEndAddress);
	AT91F_DBGU_Printk(MsgBuffer); 

	if ((FirstSector >= CFG_MAX_FLASH_SECT) || (LastSector  >= CFG_MAX_FLASH_SECT))
	{
		sprintf(MsgBuffer, "\n\r Unavailable Sector ");
		AT91F_DBGU_Printk(MsgBuffer); 
		return;
	}
	
	AT91F_FLASH16x4SectorErase(&Flash16x4, FirstSector, LastSector, 0);
	while(pStartAddress < pEndAddress)
	{
		if (*pStartAddress != 0xFFFF)
			break;
		pStartAddress++;
	}
	if(pStartAddress < pEndAddress)
		sprintf(MsgBuffer, "\n\r Erase Error: 0x%X", (unsigned int)pStartAddress);
	else
		sprintf(MsgBuffer, "\n\r Erase OK: Data[%X]= 0x%X, Data[%X]= 0x%X",
		                     (unsigned int)Flash16x4.pFlashInfo->start[FirstSector],
		                     *((unsigned short*)(Flash16x4.pFlashInfo->start[FirstSector])),
		                     (unsigned int)Flash16x4.pFlashInfo->start[LastSector],
		                     *((unsigned short*)(Flash16x4.pFlashInfo->start[LastSector])));		
	AT91F_DBGU_Printk(MsgBuffer);		
}



//*----------------------------------------------------------------------------
//* \fn    main
//* \brief 
//* 
//*----------------------------------------------------------------------------
int main()
{

	AT91F_DBGU_Printk("\n\n\r-I- ======================================\n\r");
	AT91F_DBGU_Printk("-I- AT91RM9200 Flash AT49BV1614 Test\n\r");
	AT91F_DBGU_Printk("-I- --------------------------------------\n\r");

	//* System Timer initialization
	AT91F_ST_SetPeriodInterval(AT91C_BASE_ST, AT91C_ST_PITS);
	AT91F_ST_EnableIt(AT91C_BASE_ST, AT91C_ST_PITS);
	AT91F_AIC_ConfigureIt (
							AT91C_BASE_AIC,                        // AIC base address
							AT91C_ID_SYS,                          // System peripheral ID
							AT91C_AIC_PRIOR_HIGHEST,               // Max priority
							AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE,   // Level sensitive
							AT91F_ST_ASM_HANDLER );						
	//* Enable ST interrupt
	AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_SYS);
	
	//* Configure EBI to support flash 16x4 on NCS0
	AT91F_InitFlash(); 

	AT91F_FLASH16x4Open(&Flash16x4 , &Flash16x4Info, (unsigned short *)0x10000000);
	Flash16x4Info.InitFlashInfo = AT91F_Init16x4Flash;
	Flash16x4Info.InitFlashInfo(Flash16x4.pFlashInfo, (unsigned int)Flash16x4.baseAddress);
	Flash16x4Info.flash_id = AT91F_FLASH16x4ProductId(&Flash16x4);

	AT91F_DisplayFlashId(Flash16x4Info.flash_id);
	
	AT91F_TestWriteFlash(AT91C_SECTOR_TO_TEST, AT91C_TEST_PATTERN);
	
	AT91F_TestEraseFlash(AT91C_FIRST_SECTOR_TO_ERASE, AT91C_LAST_SECTOR_TO_ERASE);

	while(1);
}

⌨️ 快捷键说明

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