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

📄 nandflash_test.c

📁 erase 讀寫nand flash
💻 C
字号:
/*
 *  Copyright 2005 by Spectrum Digital Incorporated.
 *  All rights reserved. Property of Spectrum Digital Incorporated.
 *
 *  Not for distribution.
 */

/*
 *  NAND Flash Test on DaVinci EVM
 *
 */

#include "stdio.h"
#include "davincievm_nandflash.h"

#define	MAX_IMAGE_SIZE	(0x80000)
#pragma DATA_SECTION ( nandImage, ".image" )
Uint32 nandImage[MAX_IMAGE_SIZE];

#pragma DATA_SECTION ( nandTestImage, ".testimage" )
Uint32 nandTestImage[MAX_IMAGE_SIZE];

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  emif_init( )                                                       *
 *      Configure pinmux and wait states                                    *
 *                                                                          *
 * ------------------------------------------------------------------------ */
Int16 emif_init( )
{
    Uint32 acfg2 = 0x3FFFFFFC;  // MAX Timeout
    Uint32 acfg3 = 0x3FFFFFFC;  // MAX Timeout
    Uint32 acfg4 = 0x3FFFFFFC;  // MAX Timeout
    Uint32 acfg5 = 0x3FFFFFFC;  // MAX Timeout

#if 0
	/* acfg2 = 0x0824412c; */

    /*------------------------------------------------------------------*
     *  NAND FLASH CHIP TIMEOUT @ 27 MHz                                *
     *                                                                  *
     *  AEMIF.CLK freq   = PLL1/6 = 27/6 = 4.5 MHz                      *
     *  AEMIF.CLK period = 1/4.5 MHz = 222.2 ns                         *
     *                                                                  *
     *------------------------------------------------------------------*/
    acfg2 = 0
        | ( 0 << 31 )           // selectStrobe
        | ( 0 << 30 )           // extWait
        | ( 1 << 26 )           // writeSetup      //  10 ns
        | ( 1 << 20 )           // writeStrobe     //  40 ns
        | ( 1 << 17 )           // writeHold       //  10 ns
        | ( 1 << 13 )           // readSetup       //  10 ns
        | ( 1 << 7 )            // readStrobe      //  60 ns
        | ( 1 << 4 )            // readHold        //  10 ns
        | ( 3 << 2 )            // turnAround      //  ?? ns
        | ( 0 << 0 )            // asyncSize       //  8-bit bus
        ;

    /*------------------------------------------------------------------*
     *  NAND FLASH CHIP TIMEOUT @ 459 MHz                               *
     *                                                                  *
     *  AEMIF.CLK freq   = PLL1/6 = 459/6 = 76.5 MHz                    *
     *  AEMIF.CLK period = 1/76.5 MHz = 13.1 ns                         *
     *                                                                  *
     *------------------------------------------------------------------*/
    acfg2 = 0
        | ( 0 << 31 )           // selectStrobe
        | ( 0 << 30 )           // extWait
        | ( 1 << 26 )           // writeSetup      //  10 ns
        | ( 3 << 20 )           // writeStrobe     //  40 ns
        | ( 1 << 17 )           // writeHold       //  10 ns
        | ( 1 << 13 )           // readSetup       //  10 ns
        | ( 5 << 7 )            // readStrobe      //  60 ns
        | ( 1 << 4 )            // readHold        //  10 ns
        | ( 3 << 2 )            // turnAround      //  ?? ns
        | ( 0 << 0 )            // asyncSize       //  8-bit bus
        ;

    /*------------------------------------------------------------------*
     *  NAND FLASH CHIP TIMEOUT @ 594 MHz                               *
     *                                                                  *
     *  AEMIF.CLK freq   = PLL1/6 = 594/6 = 99 MHz                      *
     *  AEMIF.CLK period = 1/99 MHz = 10.1 ns                           *
     *                                                                  *
     *------------------------------------------------------------------*/
    acfg2 = 0
        | ( 0 << 31 )           // selectStrobe
        | ( 0 << 30 )           // extWait
        | ( 1 << 26 )           // writeSetup      //  10 ns
        | ( 4 << 20 )           // writeStrobe     //  40 ns
        | ( 1 << 17 )           // writeHold       //  10 ns
        | ( 1 << 13 )           // readSetup       //  10 ns
        | ( 6 << 7 )            // readStrobe      //  60 ns
        | ( 1 << 4 )            // readHold        //  10 ns
        | ( 3 << 2 )            // turnAround      //  ?? ns
        | ( 0 << 0 )            // asyncSize       //  8-bit bus
        ;
#endif

    DAVINCIEVM_pin_mux_on( 0x00000C1F, 0 );   // Select AEMIF address lines
    DAVINCIEVM_aemif_init( acfg2, acfg3, acfg4, acfg5 );

    AEMIF_NANDFCR = 0x00000101;

    return 0;
}

typedef struct {
	Uint32		magicNum;
	Uint32		entryPt;
	Uint32		nPages;
	Uint32		blockNo;
	Uint32		pageNo;
} nandBootHdr;

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  nandflash_test( )                                                       *
 *                                                                          *
 * ------------------------------------------------------------------------ */
Int16 nandflash_test( )
{
    Uint32 nblocks;
    Uint32 npages;
    Uint32 nbytes;

	FILE	*fPtr;
	Uint8	*ramPtr;
	Int32	fileSize = 0;
	Int32	offset = 0;
	Int32	origOffset = 0;

	Int16	retcode = 0;

	Int8	fileName[256];
	Int8	answer[256];
	Uint8	gErase;
	Uint32	*buffer;
	Uint32	entryOffset = 0;
	Uint32  loadAddress = 0;

	printf( "Starting NAND Flash Writer\n", offset);
	printf( "Note: First page of the block will be used to store boot header\n\n");

	/* ---------------------------------------------------------------- *
     *  Initialize EMIF Controller                                      *
     * ---------------------------------------------------------------- */
	emif_init();

    /* ---------------------------------------------------------------- *
     *  Initialize NAND Flash                                           *
     * ---------------------------------------------------------------- */
    NANDFLASH_init( );

    /* Determine the total number of pages */
    npages = NANDFLASH_getTotalPages( );

    if ( npages == 0 )
    {
        printf( "     Error: NO pages found\n" );
        return 1;
    }
    else
    {
        nblocks = npages >> NANDFLASH_PAGES_PER_BLOCK_POW2;
        nbytes = npages * NANDFLASH_PAGESIZE;
        printf( "     Total blocks: %d\n", nblocks );
        printf( "     Total pages:  %d\n", npages );
        printf( "     Total bytes:  %d\n", nbytes );
    }

	/* ---------------------------------------------------------------- *
     *  Read Application                                                *
	 * ---------------------------------------------------------------- */
	printf("Enter the File Name\n");
	scanf("%s", fileName);

	printf("Burn Block Offset \n");
	scanf("%s", answer);

	fflush(stdin);

	printf("Do a global Erase \n");
	scanf("%c", &gErase);

	offset = strtol(answer, NULL, 16);
	origOffset = offset;
	if((offset < 1) || (offset > nblocks))
	{
		printf("Wrong Offset - Use 1 to 0x%x\n", nblocks);
		printf("Note values are treated as hex & offset should be in multiples of %d Bytes\n",
												(NANDFLASH_PAGESIZE * NANDFLASH_PAGES_PER_BLOCK));
		return -1;
	}

	memset(answer, 0, 256);

	/* Check if we are writing the application or the boot loader */
	if(offset > 5)
	{
		printf("Enter application entry point\n");
		scanf("%s", answer);
		entryOffset = strtoul(answer, NULL, 16);

		fflush(stdin);
		printf("Enter application load Address\n");
		scanf("%s", answer);
		loadAddress = strtoul(answer, NULL, 16);
	}
	else
	{
		entryOffset = 0x20;
	}

	/* Clear the memory */
	memset(nandImage, 0, NANDFLASH_PAGESIZE);

	// Open an File from the hard drive
	fPtr = fopen(fileName, "rb");
	if(fPtr == NULL)
	{
		printf("File %s Open failed\n", fileName);
		return -1;
	}

	// Initialize the pointer
	ramPtr = (Uint8 *) (&nandImage[NANDFLASH_PAGESIZE/4]);
	fileSize = 0;

	// Read file size
	fseek(fPtr,0,SEEK_END);
	fileSize = ftell(fPtr);

	if(fileSize == 0)
	{
		printf("File read failed.. Closing APP\n");
		fclose (fPtr);
		return -1;
	}

	fseek(fPtr,0,SEEK_SET);

	if(offset < 5)
	{
		/* Its a UBL - So ignore first 256 bytes */
		fread(ramPtr, 1, 256, fPtr);
		fileSize -= 0x100;
	}

	if (fileSize != fread(ramPtr, 1, fileSize, fPtr))
	{
		printf("Warning !!! File Size mismatch\n");
	}

	fclose (fPtr);
//while(1);
	/* ---------------------------------------------------------------- *
     *  Un-Protect                                                      *
	 * ---------------------------------------------------------------- */
	printf( "     Un-Protect Flash [%d-%d] blocks\n", 0, nblocks );
	NANDFLASH_unProtect(0, (nblocks - 1) * NANDFLASH_PAGESIZE * NANDFLASH_PAGES_PER_BLOCK);

    /* ---------------------------------------------------------------- *
     *  Erase                                                           *
	 * ---------------------------------------------------------------- */
	if(gErase == 'Y' || gErase == 'y')
	{
		printf( "     Erasing Flash [%d-%d] blocks\n", 0, nblocks );
	    retcode = NANDFLASH_erase( 0, nblocks );
		nblocks = (fileSize/(NANDFLASH_PAGESIZE * NANDFLASH_PAGES_PER_BLOCK)) + 1;
	}
	else
	{
		nblocks = (fileSize/(NANDFLASH_PAGESIZE * NANDFLASH_PAGES_PER_BLOCK)) + 1;
		printf( "     Erasing Flash [%d-%d] blocks\n", offset, (offset + nblocks - 1) );
	    retcode = NANDFLASH_erase( (offset * NANDFLASH_PAGES_PER_BLOCK * NANDFLASH_PAGESIZE),
	    						   nblocks );
	}

    if ( retcode != 0 )
    {
        printf( "     Error: Erasing code %d\n", retcode );
        return retcode | 0x8000;
    }

    /* ---------------------------------------------------------------- *
     *  Write                                                           *
	 * ---------------------------------------------------------------- */
    printf( "     Writing Flash \n");
	buffer = (Uint32 *)nandImage;

	buffer[1] = entryOffset;											/* Entry Point */
	buffer[2] = (fileSize + NANDFLASH_PAGESIZE/2)/NANDFLASH_PAGESIZE;	/* Number of Pages */
	buffer[3] = offset;													/* Block Number */
	buffer[4] = 1;														/* Page Number */
	if(buffer[5] = loadAddress)											/* Load Address */
	{
		buffer[0] = 0xA1ACED11;											/* Magic Number */
	}
	else
	{
		buffer[0] = 0xA1ACED00;											/* Magic Number */
	}

	ramPtr = (Uint8 *)nandImage;

	do {
	    /* Write to Flash */
	    retcode |= NANDFLASH_writePage( (Uint32)ramPtr,
	    								(offset * NANDFLASH_PAGES_PER_BLOCK * NANDFLASH_PAGESIZE),
	    								NANDFLASH_PAGES_PER_BLOCK );

	    /* Check errors */
	    if ( retcode != 0 )
	    {
	        printf( "     Error: Writing code %d\n", retcode );
	        return retcode | 0x8000;
	    }

		/* Reduce the no. of blocks */
		nblocks--;
		ramPtr +=  NANDFLASH_PAGES_PER_BLOCK * NANDFLASH_PAGESIZE;
		offset++;

	} while (nblocks > 0);


	/* ---------------------------------------------------------------- *
     *  Read                                                            *
	 * ---------------------------------------------------------------- */
    printf( "     Reading Flash \n");

	offset = origOffset;
	nblocks = (fileSize/(NANDFLASH_PAGESIZE * NANDFLASH_PAGES_PER_BLOCK)) + 1;

	ramPtr = (Uint8 *)nandTestImage;

	do
	{
		retcode = NANDFLASH_readPage( (offset * NANDFLASH_PAGES_PER_BLOCK * NANDFLASH_PAGESIZE),
									 (Uint32)ramPtr,
									 NANDFLASH_PAGES_PER_BLOCK);

	    /* Check errors */
	    if ( retcode != 0 )
	    {
	        printf( "     Error: Reading code %d\n", retcode );
	        return retcode | 0x8000;
	    }

		/* Reduce the no. of blocks */
		nblocks--;
		ramPtr +=  NANDFLASH_PAGES_PER_BLOCK * NANDFLASH_PAGESIZE;
		offset++;

	} while (nblocks > 0);

	nbytes = (fileSize + NANDFLASH_PAGESIZE)/4 + 1;

	do
	{
		nbytes --;

		if(nandTestImage[nbytes] != nandImage[nbytes])
		{
			printf("    Compare failed @ 0x%x - Try some other block\n", nbytes);
			return NANDFLASH_STATUS_ERROR;
		}

	} while (nbytes > 0);

    return 0;
}

⌨️ 快捷键说明

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