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

📄 norwriter.c

📁 davinci的NANDFLASH烧写程序
💻 C
字号:
/* --------------------------------------------------------------------------
    FILE        : norwriter.c 				                             	 	        
    PURPOSE     : NOR writer main program
    PROJECT     : DaVinci CCS NOR Flashing Utility
    AUTHOR      : Daniel Allred
    DATE	    : 04-Jun-2007  
 
    HISTORY
 	     v1.00 - DJA - 04-Jun-2007
 	        Completion (with support for DM6441 and DM6441_LV)
 	     
 ----------------------------------------------------------------------------- */

#include "stdio.h"
#include "nor.h"
#include "util.h"
#include "norwriter.h"

#pragma DATA_SECTION(DDRStart,".ddr_mem");
VUint32 DDRStart;

#pragma DATA_SECTION(NORStart,".aemif_mem");
VUint32 NORStart;


/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  norwriter( )                                                           *
 *                                                                          *
 * ------------------------------------------------------------------------ */
Uint32 norwriter()
{
	NOR_BOOT  gNorBoot;
    PNOR_INFO pNorInfo;

    Bool    bUseUBL = FALSE;
	FILE	*fPtr;
	Uint8	*ramPtr;
	Int32	fileSize = 0;
	Int8	fileName[256];
    Int8    answer[24];
    Uint32   baseAddress = 0, blockSize = 0, blockAddr = 0;


	printf( "Starting DV_NORWriter.\r\n");

    // Initialize NOR Flash
    pNorInfo = NOR_Open((Uint32)&NORStart);
    if (pNorInfo == NULL)
	{
        printf( "\tERROR: NOR Initialization failed.\r\n" );
        return E_FAIL;
    }

    // Ask if a UBL will be used at start of NOR flash
    printf("Do you wish to use a UBL in the NOR flash?\r\n");
	scanf("%c", answer);
    bUseUBL =  ( (answer[0] == 'y') || (answer[0] == 'Y') );

    // Set base address to start putting data at
    baseAddress = pNorInfo->flashBase;

    if (bUseUBL)
    {
	    printf("Enter the UBL file Name\n");
    	scanf("%s", fileName);
	    fflush(stdin);

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

	    // Initialize the pointer
	    fileSize = 0;

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

	    // Setup pointer in RAM
	    ramPtr = (Uint8 *) ubl_alloc_mem(fileSize);

	    if(fileSize == 0)
	    {
		    printf("\tERROR: File read failed.. Closing program.\r\n");
		    fclose (fPtr);
		    return E_FAIL;
	    }

	    fseek(fPtr,0,SEEK_SET);

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

	    fclose (fPtr);

       	// Erasing the Flash
	    if ( NOR_Erase(pNorInfo, baseAddress, fileSize) != E_PASS )
        {
    		printf("\tERROR: Erasing NOR failed.\r\n", fileName);
	    	return E_FAIL;
        }
	        
	    // Write the actual application to the flash
	    if ( NOR_WriteBytes(pNorInfo, baseAddress, fileSize, (Uint32)ramPtr ) != E_PASS )
        {
    		printf("\tERROR: Writing NOR failed.\r\n", fileName);
	    	return E_FAIL;
        }

        // Get block size and base of block where UBL was written
        NOR_GetBlockInfo(pNorInfo,baseAddress+fileSize,&blockSize,&blockAddr);

        // Calculate the new base address at which the U-boot or App will now start
        baseAddress =  (blockAddr + blockSize);
    }

    // Read the file from host
	printf("Enter the U-boot or application file name:\r\n");
	scanf("%s", fileName);
	fflush(stdin);

	// Open an File from the hard drive
	fPtr = fopen(fileName, "rb");
	if(fPtr == NULL)
	{
		printf("\tERROR: File %s open failed.\r\n", fileName);
		return E_FAIL;
	}

	// Initialize the pointer
	fileSize = 0;

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

    // Check to make sure image was read correctly
    if(fileSize == 0)
	{
		printf("\tERROR: File read failed.. Closing program.\r\n");
		fclose (fPtr);
		return E_FAIL;
	}
    // Check to make sure the app image will fit 
	else if ( fileSize > (pNorInfo->flashSize - (bUseUBL?(blockSize + sizeof(gNorBoot)):0)) )
    {

       	printf("\tERROR: File too big.. Closing program.\r\n");
		fclose (fPtr);
    }

    if (bUseUBL)
    {
        // Get the entry point and load addresses
	    printf("Enter the U-boot or application entry point: \n");
	    scanf("%s", answer);
        gNorBoot.entryPoint = strtoul(answer, NULL, 16);
	    fflush(stdin);
        if ( (gNorBoot.entryPoint < RAM_START_ADDR) || (gNorBoot.entryPoint > RAM_END_ADDR) )
        {
            printf("\tWARNING: Entry point not in acceptable range - using default 0x81080000.\r\n");
            gNorBoot.entryPoint = 0x81080000;
        }

        printf("Enter the U-boot or application load address: \r\n");
	    scanf("%s", answer);
	    gNorBoot.ldAddress = strtoul(answer, NULL, 16);
        if ( (gNorBoot.ldAddress < RAM_START_ADDR) || (gNorBoot.ldAddress > RAM_END_ADDR) )
        {
            printf("\tWARNING: Load address not in acceptable range - using default 0x81080000.\r\n");
            gNorBoot.ldAddress = 0x81080000;
        }

	    gNorBoot.magicNum = UBL_MAGIC_BIN_IMG;
        gNorBoot.appSize = fileSize;
    }

    // Setup pointer in RAM
	ramPtr = (Uint8 *) ubl_alloc_mem(fileSize);

	fseek(fPtr,0,SEEK_SET);

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

	fclose (fPtr);

    if (NOR_Erase( pNorInfo, baseAddress, (fileSize + (bUseUBL?sizeof(gNorBoot):0)) ) != E_PASS)
    {
        printf("\tERROR: Erasing NOR failed.\r\n");
	    return E_FAIL;
    }

	// Write the NOR_BOOT header to the flash
    if (bUseUBL) 
        if (NOR_WriteBytes( pNorInfo, baseAddress, sizeof(gNorBoot), (Uint32) &gNorBoot) != E_PASS)
        {
            printf("\tERROR: Writing NOR failed.\r\n");
	    	return E_FAIL;
        }

	// Write the application data to the flash
	if (NOR_WriteBytes( pNorInfo, baseAddress + (bUseUBL?sizeof(gNorBoot):0), fileSize, (Uint32)ramPtr) != E_PASS)
    {
        printf("\tERROR: Writing NOR failed.\r\n");
    	return E_FAIL;
    }


	return E_PASS;
}

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  nandflash_test( )                                                       *
 *                                                                          *
 * ------------------------------------------------------------------------ */
/*Uint32 nandflash_test()
{
    Uint32 numPages;
    Uint32 numBytes;

	NAND_BOOT gNandBoot;

	FILE	*fPtr;
	Uint8	*ramPtr;
	Int32	fileSize = 0;
	Int8	fileName[256];

	// Setup pointer in RAM (alloc 16MB space)
	ramPtr = (Uint8 *) ubl_alloc_mem(MAX_IMAGE_SIZE);

	printf( "Starting LC_NANDWriter.\r\n");

    // Initialize NAND Flash
    if (NAND_Init() != E_PASS)
	{
        printf( "     Error: NAND Initialization failed.\n" );
        return E_FAIL;
    }

    // Determine the total number of pages
    numPages = gNandInfo.numBlocks * gNandInfo.pagesPerBlock;
    numBytes = gNandInfo.bytesPerPage * numPages;
    printf( "     Total blocks: %d\n", gNandInfo.numBlocks );
    printf( "     Total pages:  %d\n", numPages );
    printf( "     Total bytes:  %d\n", numBytes );

	// Read the file from host
	printf("Enter the File Name\n");
	scanf("%s", fileName);
	fflush(stdin);

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

	// Initialize the pointer
	fileSize = 0;

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

	// Setup pointer in RAM
	ramPtr = (Uint8 *) ubl_alloc_mem(fileSize);

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

	fseek(fPtr,0,SEEK_SET);

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

	fclose (fPtr);

	numPages = 0;
	while ( (numPages * gNandInfo.bytesPerPage)  < (fileSize) )
	{
		numPages++;
	}

	gNandBoot.magicNum = 0;
	gNandBoot.block = 1;
	gNandBoot.page = 0;
	gNandBoot.numPage = numPages;
	gNandBoot.entryPoint = 0;
	gNandBoot.ldAddress = 0;

	if (NAND_WriteHeaderAndData(&gNandBoot,ramPtr) != E_PASS)
	{
		printf("Warning! Write Failed\n");
		return E_FAIL;
	}

	return E_PASS;
}*/


/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  main( )                                                                 *
 *                                                                          *
 * ------------------------------------------------------------------------ */
void main( void )
{
    int status;

	// Init memory alloc pointer
	set_current_mem_loc(0);

	// Execute the NAND test
    status = norwriter();

    /* Check for test fail */
    if (status != E_PASS)
    {
        /* Print error message */
        printf( "\tNOR flashing failed!\r\n");
    }
    else
    {
        /* Print error message */
        printf( "\tNOR boot preparation was successful!\r\n" );
    }
}

⌨️ 快捷键说明

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