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

📄 norflash.c

📁 (1)个人针对atmel提供的运行于CPU内13k空间的ram的romboot修改而成。 (2)提供了IRQ和FIQ的测试工作; (3)提供了对外部sdram的数据总线和地址总线的测试工作 (3
💻 C
字号:
//*----------------------------------------------------------------------------
//*      GRENTECH INC., LTD
//*----------------------------------------------------------------------------

//*----------------------------------------------------------------------------
//* File Name           : norflash.c
//* Object              : 
//*
//* 1.0 28/0207  : Creation, yanyuan gr1660
//*----------------------------------------------------------------------------
#include <config.h>

#ifdef INCLUDE_NOR_FLASH_SUPPORT

#include <norflash.h>

int read_nor_flash(unsigned long addr_src, unsigned long size, char *addr_dest)
{
#if 0
    *addr_dest = *((volatile unsigned short *)(NOR_FLASH_BASE_ADDRESS + addr_src));
#endif

    return NOR_FLASH_OK;
}

int write_nor_flash (unsigned long addr_dest, unsigned int addr_src, unsigned int size)
{
    
    return NOR_FLASH_OK;
}

int erase_nor_flash_sector(unsigned int sector_num, unsigned int sector_count)
{
    unsigned int sector_offset_address = 0
    unsigned int i = 0;		

    if ((sector_num + sector_count) > 255)
    {
        printf("Invalid sector number.\r\n");
	 return NOR_FLASH_ERROR;	
    }

    for (; sector_count > 0; sector_count--, sector_num++)
    {
        sector_offset_address = NOR_FLASH_SECTOR_SIZE_IN_BYTES * sector_num;

        *((volatile unsigned short *)NOR_FLASH_BASE_ADDRESS + 0x555) = 0xAA;
        *((volatile unsigned short *)NOR_FLASH_BASE_ADDRESS + 0x2AA) = 0x55;
        *((volatile unsigned short *)NOR_FLASH_BASE_ADDRESS + 0x555) = 0x80;
        *((volatile unsigned short *)NOR_FLASH_BASE_ADDRESS + 0x555) = 0xAA;
        *((volatile unsigned short *)NOR_FLASH_BASE_ADDRESS + 0x2AA) = 0x55;
        *((volatile unsigned short *)(NOR_FLASH_BASE_ADDRESS + sector_offset_address)) = 0x30;

        for (;;)
        {
            if (*((volatile unsigned short *)(NOR_FLASH_BASE_ADDRESS+sector_offset_address))  == (unsigned short)0xFFFF)
	         break;
        }		
    }

    return NOR_FLASH_OK;
}

#if 0

/*-----------------------------------------------------------------------------
Function:   nor_flash_cmd_erase_chip

Overview:   

Input:      None

Output:     None

Return:     None
-----------------------------------------------------------------------------*/
LOCAL STATUS nor_flash_cmd_erase_chip()
{ 
    UINT32 i = 0;
	
    *((volatile UINT16 *)ROM_BASE_ADRS+0x555) = 0xAA;
    *((volatile UINT16 *)ROM_BASE_ADRS+0x2AA) = 0x55;
    *((volatile UINT16 *)ROM_BASE_ADRS+0x555) = 0x80;
    *((volatile UINT16 *)ROM_BASE_ADRS+0x555) = 0xAA;
    *((volatile UINT16 *)ROM_BASE_ADRS+0x2AA) = 0x55;
    *((volatile UINT16 *)ROM_BASE_ADRS+0x555) = 0x10;
	
#if 0

    taskDelay (sysClkRateGet () * 512);

#else

    for (i = 0; i < 513; i++)
    {
        if (*((volatile UINT16 *)ROM_BASE_ADRS)  == (UINT16)0xFFFF)
	     break;
		
	 taskDelay (sysClkRateGet());	
	 
	 printf(".");

    }
	
    printf("\r\n");	
	
    if (i == 10)
        printf("Erase chip failed!\r\n");	
    else	
        printf("Erase chip done!\r\n");
	
#endif

    return OK;
}


/*-----------------------------------------------------------------------------
Function:   flasherasesecotor

Overview:   

Input:      None

Output:     None

Return:     None
-----------------------------------------------------------------------------*/
LOCAL STATUS nor_flash_cmd_erase_sector(char *pEraseSectorCmd)
{ 
#if 1
    UINT32 sector = 0;
    UINT32 sectoraddr = 0;
    UINT32 i = 0;	 
		
    if (sscanf(pEraseSectorCmd, "%*s %*s %d", &sector) == EOF)
    {
       return (ERROR);			 
    }

#if 0	
    if ((sector > 34) || (sector < 0))
    {
        printf("Sector Number error!\n");
        return ERROR;
    }
    else if (sector > 3)
    {
        sectoraddr = 0x08000 * (sector - 3);
    }
    else
    {
        switch (sector)
	     {
			 	case 0:
					sectoraddr = 0x00000;
					break;
				case 1:
					sectoraddr = 0x02000;
					break;
				case 2:
					sectoraddr = 0x03000; 
					break;
				case 3:
					sectoraddr = 0x04000;  
					break;
				default:
					break;
	     }
    }
#else

    if (sector > 255)
    {
        printf("Invalid sector number.\r\n");
	 return ERROR;	
    }

    sectoraddr = NOR_FLASH_SECTOR_SIZE_IN_BYTES * sector;
	
#endif
 
    *((volatile UINT16 *)ROM_BASE_ADRS+0x555) = 0xAA;
    *((volatile UINT16 *)ROM_BASE_ADRS+0x2AA) = 0x55;
    *((volatile UINT16 *)ROM_BASE_ADRS+0x555) = 0x80;
    *((volatile UINT16 *)ROM_BASE_ADRS+0x555) = 0xAA;
    *((volatile UINT16 *)ROM_BASE_ADRS+0x2AA) = 0x55;
    *((volatile UINT16 *)(ROM_BASE_ADRS+sectoraddr)) = 0x30;

    for (i = 0; i < 5; i++)
    {
        if (*((volatile UINT16 *)(ROM_BASE_ADRS+sectoraddr))  == (UINT16)0xFFFF)
	     break;
	 taskDelay (sysClkRateGet());	
	 printf(".");

    }

    printf("\r\n");
	
    if (i == 10)
        printf("Erase sector %d failed!\r\n", sector);	
    else	
        printf("Erase sector %d done!\r\n", sector);
	
    #endif
    return OK;
}

/*-----------------------------------------------------------------------------
Function:   nor_flash_cmd_identifier

Overview:   

Input:      None

Output:     None

Return:     None
-----------------------------------------------------------------------------*/

LOCAL STATUS nor_flash_cmd_identifier()
{ 
    UINT16 ManufacturerID = 0;
    UINT16 DeviceID[3];
	
#if 0

    if (nor_flash_identify_chip(&ManufacturerID, DeviceID) != OK)
    {
        return ERROR;
    }

    nor_flash_reset();
	
    printf("Manufacturer ID : 0x%x\n", ManufacturerID);
    printf("DeviceID[0] : 0x%x\n", DeviceID[0]);	
    printf("DeviceID[1] : 0x%x\n", DeviceID[1]);
    printf("DeviceID[2] : 0x%x\n", DeviceID[2]);
	
#else

    *((volatile UINT16 *)ROM_BASE_ADRS + 0x555) = 0xAA;
    *((volatile UINT16 *)ROM_BASE_ADRS + 0x2AA) = 0x55;
    *((volatile UINT16 *)ROM_BASE_ADRS + 0x555) = 0x90;
    ManufacturerID = *((volatile UINT16 *)ROM_BASE_ADRS + 0x000);
    printf("Manufacturer ID : 0x%x\n", ManufacturerID);	
	
    *((volatile UINT16 *)ROM_BASE_ADRS + 0x555) = 0xAA;
    *((volatile UINT16 *)ROM_BASE_ADRS + 0x2AA) = 0x55;
    *((volatile UINT16 *)ROM_BASE_ADRS + 0x555) = 0x90;
    DeviceID[0] = *((volatile UINT16 *)ROM_BASE_ADRS + 0x001);	
    DeviceID[1] = *((volatile UINT16 *)ROM_BASE_ADRS + 0x00E);
    DeviceID[2] = *((volatile UINT16 *)ROM_BASE_ADRS + 0x00F);
    printf("DeviceID : 0x%x 0x%x 0x%x\n", DeviceID[0], DeviceID[1], DeviceID[2]);
	
    /* Issue reset command to exit autoselect mode */	
    *((volatile UINT16 *)ROM_BASE_ADRS + 0x000) = 0xF0;   
    
	
#endif

    return OK;
}


/*-----------------------------------------------------------------------------
Function:   nor_flash_cmd_reset

Overview:   

Input:      None

Output:     None

Return:     None
-----------------------------------------------------------------------------*/
LOCAL STATUS nor_flash_cmd_reset()
{ 

    /* Issue reset command to exit autoselect mode */	
    *((volatile UINT16 *)ROM_BASE_ADRS + 0x000) = 0xF0;  
	
    return OK;
}


/*-----------------------------------------------------------------------------
Function:   nor_flash_cmd_program

Overview:   

Input:      None

Output:     None

Return:     None
-----------------------------------------------------------------------------*/
LOCAL STATUS nor_flash_cmd_program(char *pWriteCommand)
{ 
    UINT32 address = 0;
    UINT32 data = 0;
		
    if (sscanf(pWriteCommand, "%*s %*s %d %d", &address, &data) == EOF)
    {
       return (ERROR);			 
    }

    printf("Flash program address: 0x%x\n", address);
    printf("Flash program data: 0x%x\n", data);
	
#if 0

    if (nor_flash_program(address, (UINT8 *)&data, sizeof(data)) != OK)
    {
        return ERROR;
    }
	
#else

    *((volatile UINT16 *)ROM_BASE_ADRS + 0x555) = 0xAA;
    *((volatile UINT16*)ROM_BASE_ADRS + 0x2AA) = 0x55;
    *((volatile UINT16 *)ROM_BASE_ADRS + 0x555) = 0xA0;		
    *((volatile UINT16 *)(ROM_BASE_ADRS + address)) = data;
	
#endif
    return OK;
}
/*-----------------------------------------------------------------------------
Function:   nor_flash_cmd_read

Overview:   

Input:      None

Output:     None

Return:     None
-----------------------------------------------------------------------------*/
LOCAL STATUS nor_flash_cmd_read(char *pReadCommand)
{ 
    UINT32 address = 0;
    UINT16 data = 0;

    if (sscanf(pReadCommand, "%*s %*s %d", &address) == EOF)
    {
       return (ERROR);			 
    }
	
#if 0

    if (nor_flash_read((UINT8 *)&data, address, sizeof(data)) != OK)
   	{
   	    return ERROR;
   	}
	
#else		

    data = *((volatile UINT16 *)(ROM_BASE_ADRS + address));

    printf("Flash addr: 0x%x\n", address);
    printf("Flash data: 0x%x\n", data);
	
#endif		
    return OK;
}

/*-----------------------------------------------------------------------------
Function:   FLASHDebugCommand

Overview:   

Input:      None

Output:     None

Return:     None
-----------------------------------------------------------------------------*/
LOCAL STATUS nor_flash_cmd(char *pString)
{
    char flashcommad[10];
		
    if (sscanf(pString, "%*s %s", flashcommad) == EOF)
    {
       printf("Missing flash command!\n"); 
       return (ERROR);			 
    }
		
    if (strncmp(flashcommad, "eall", 4) == 0)
    {
        if (nor_flash_cmd_erase_chip() != OK)
        {
            printf ("Erase flash chip fail!\n");
            return (ERROR);	
        }				
        printf("Erase finished!\n");
    }
    else if (strncmp(flashcommad, "es", 2) == 0)
    {
        if (nor_flash_cmd_erase_sector(pString) != OK)
        {
            printf ("Error in erasing flash sector!\n");
            return (ERROR);						
        }				
    }
    else if (strncmp(flashcommad, "pg", 2) == 0)
    {
        if (nor_flash_cmd_program(pString) != OK)
        {
            printf ("Error in writing flash!\n");
            return (ERROR);						
        }				
    }  
    else if (strncmp(flashcommad, "rd", 2) == 0)
    {
        if (nor_flash_cmd_read(pString) != OK)
        {
            printf ("Error in reading flash!\n");
            return (ERROR);						
        }				
    }
    else if (strncmp(flashcommad, "id", 2) == 0)
    {
        if (nor_flash_cmd_identifier() != OK)
        {
            printf ("Error in reading flash id!\n");
            return (ERROR);						
        }				
    }
    else if (strncmp(flashcommad, "reset", 5) == 0)
    {
        nor_flash_cmd_reset();
    }	
   
    return (OK);		
}

#endif

#endif /*INCLUDE_NOR_FLASH_SUPPORT*/

void norflash_c_kill_warning()
{}

⌨️ 快捷键说明

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