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

📄 nandflash_test.c

📁 DM6467 Bootloader源码
💻 C
字号:
/*
 *  Copyright 2007 by Spectrum Digital Incorporated.
 *  All rights reserved. Property of Spectrum Digital Incorporated.
 */

/*
 *  NAND Flash Test
 *
 */

#include "stdio.h"
#include "davincihd_nandflash.h"

/*
 *  NAND Flash Large Pages ( 2kb )
 */
static Uint8 rx[2048 + 64];
static Uint8 tx[2048 + 64];

#define APP_MAGIC_NUM		0xA1ACED11
#define ENTRY_OFFSET		0x81080000
#define LOAD_ADDRESS		ENTRY_OFFSET

/* ------------------------------------------------------------------------ *
 *  nandflash_test( )                                                       *
 * ------------------------------------------------------------------------ */
Int16 nandflash_test( )
{
    Int32 i, j = 0;
    Uint32* p32;

    /* Blocks */
    Uint32 nblocks;
    Uint32 pages_per_block;

    /* Pages */
    Uint32 npages;
	Uint32 ubl_hdr_start_page;
	Uint32 app_start_page;
	Uint32 num_of_pages;
    Uint32 page_size;

    /* Errors */
    Int16 errors;
    Int32 bad_pages = 0;
    Int32 bad_pages_limit;

	Int8	fileName[256];
	FILE	*fPtr;
	Uint32 fileSize;


    /* Test Case */
    /* ---------------------------------------------------------------- *
     *  Initialize NAND Flash                                           *
     * ---------------------------------------------------------------- */
    DAVINCIHD_NANDFLASH_init( );

    /* ---------------------------------------------------------------- *
     *  Get NAND Memory Organization                                    *
     * ---------------------------------------------------------------- */
    if ( ( npages = NAND_PAGES ) == 0 )
    {
        printf( "  >> Error: Cannot find NAND Flash pages.\n" );
        return 1;
    }

    page_size       = NAND_PAGESIZE;
    pages_per_block = NAND_PAGES_PER_BLOCK;
    nblocks         = npages / pages_per_block;
    bad_pages_limit = 1;

    printf( "  %d MBytes\n", ( npages * page_size ) >> 20 );
    printf( "  %d Blocks\n", nblocks );
    printf( "  %d Pages\n", npages );

    /* ---------------------------------------------------------------- *
     *  Erase                                                           *
     * ---------------------------------------------------------------- */
    printf( "  Erasing Flash [%d-%d] blocks\n", 0, 1024 );

    bad_pages = 0;

    /* Cycle through NAND Flash Blocks */
    for ( i = 0 ; i < 1024 ; i++ )
    {
        errors = DAVINCIHD_NANDFLASH_erase( i, 1 );

/*        if ( errors )
        {
            if ( errors == NAND_ERR_TIMEOUT )
            {
                printf( "  >> Error: Timeout occured.\n" );
                return errors;
            }
            else
            {
                printf( "  >> Found bad block #%d: %d\n", ++bad_pages, i );
                return errors;
            }
        }*/
    }

    bad_pages = 0;

	printf ("Enter the U-Boot Binary file name\n");
	scanf ("%s", fileName);

	fPtr = fopen(fileName, "rb");
	if(fPtr == NULL)
	{
		printf("File %s Open failed\n", fileName);
		return -1;
	}
	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);
	fclose (fPtr);
	num_of_pages = (fileSize/NAND_PAGESIZE) + 1;

    /* ---------------------------------------------------------------- *
     *  Write the UBL Header here                                                           *
     * ---------------------------------------------------------------- */
	ubl_hdr_start_page = 64;

    /* Iterate through all pages */
    for ( i = ubl_hdr_start_page ; i < ubl_hdr_start_page + 1 ; i++ )
    {
        /* Create write pattern */
        p32 = ( Uint32* )tx;
        *p32++ = APP_MAGIC_NUM;
		*p32++ = ENTRY_OFFSET;
		*p32++ = num_of_pages;
		*p32++ = 0x00000001;
		*p32++ = 0x00000001;
		*p32++ = LOAD_ADDRESS;

        /* Write to Flash */
        errors = DAVINCIHD_NANDFLASH_writePage( ( Uint32 )tx, i, 1 );

/*        if ( errors )
        {
            printf( "  Found bad page #%d: [%d]\n", ++bad_pages, i );
            return bad_pages;
        }*/
    }

    /* Iterate through all pages */
	fPtr = fopen(fileName, "rb");
	fileSize = 0;
	app_start_page = 65;

	/* ---------------------------------------------------------------- *
     *  Write                                                           *
     * ---------------------------------------------------------------- */
    printf( "  Writing Flash [%d-%d] pages\n", app_start_page, \
    		(app_start_page + num_of_pages) - 1 );

	for ( i = app_start_page ; i < app_start_page + num_of_pages ; i++ )
    {
        /* Create write pattern */
        p32 = ( Uint32* )tx;

		fileSize = fread (p32, 1, NAND_PAGESIZE, fPtr);

		//printf ("Size of File = %d\n", fileSize);
		printf ("Writing to page %d\n", app_start_page + j++);

        /* Write to Flash */
        errors = DAVINCIHD_NANDFLASH_writePage( ( Uint32 )tx, i, 1 );

/*        if ( errors )
        {
            printf( "  Found bad page #%d: [%d]\n", ++bad_pages, i );
            return bad_pages;
        }*/

		//p32 += NAND_PAGESIZE;
    }
	fclose (fPtr);

    bad_pages = 0;

    return 0;
}

⌨️ 快捷键说明

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