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

📄 nor_flash_boot.c

📁 i.MX31 NOR_flash(SPANSION_S71WS256ND0) bootloader src
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "tortola_memory_map_defines.h"
#include "prototype.h"
#include "nor_flash.h"
#include "MX31_HABTools.h"

#define TEST_SIZE 16

#define FLASH_BASE_ADDR    CS0_BASE_ADDR
#define FLASH_SIZE_IN_BYTE (32*1024*1024)  //0x02000000 // 16M * 16 bit

#define SECTOR_SIZE_IN_BYTE 0x200000  

#define MAX_TEST_ERROR_NUM 5

static U16 *flashptr; 

static int   ErrorsNum;
static char  out_str[0x100];
static char  TestName[] = "NOR FLASH BURST";
U32  sector_address_prev=0xffffffff;
U32 FlashBaseAddr;
U32 FlashLastAddr;

U32 FlashLength;
int led_idx=0;

U32 FlashBytesSize;
U16 *source_of_data; /* address of source data */

int  write_to_flash (U16 *current_add, U16 data);
int flash_erase_sector_Null (void);
void printStatusErr (int StatusCode);
int CheckFlashType (void); 
void flash_reset ();
void error_report (U16 *adr, U16 ExpectedData, U16 ActualData);
int flash_status (U16 *fp);
int burst_boot_code (void);
int burst_led_blinkcode_to_flash () ;

U32 boot_code [] =
{
//    Reset_Handler
//    Init
0xe321f0d3, // MSR CPSR_c,#0xd3   
//0xe59f10a0, // LDR r1,[pc,#160] ; [0xac] =
//0xe3a02802, // MOV r2,#0x20000   
//0xe5812000, // .. STR r2,[r1,#0]  
0xe1a00000, // NOP    
0xe1a00000, // NOP    
0xe1a00000, // NOP    
0xe1a00000, // NOP    
0xe1a00000, // NOP    
0xe1a00000, // NOP    
0xe1a00000, // NOP    
0xe1a00000, // NOP    
0xe1a00000, // NOP    
0xe1a00000, // NOP    
0xe1a00000, // NOP    
0xe1a00000, // NOP    


/*
0xe59f1098, // LDR r1,[pc,#152] ; [0xb0] =
0xe59f2098, // .. LDR r2,[pc,#152] ; [0xb4]
0xe5812000, // .. STR r2,[r1,#0]  
0xe59f1094, // LDR r1,[pc,#148] ; [0xb8] =
0xe59f2094, // .. LDR r2,[pc,#148] ; [0xbc]
0xe5812000, // .. STR r2,[r1,#0]  
0xe59f1090, // LDR r1,[pc,#144] ; [0xc0] =
0xe59f2090, // .. LDR r2,[pc,#144] ; [0xc4]
0xe5812000, // .. STR r2,[r1,#0]  
*/
0xe59f108c, // LDR r1,[pc,#140] ; [0xc8] =
0xe59f208c, // .. LDR r2,[pc,#140] ; [0xcc]
0xe5812000, // .. STR r2,[r1,#0]  
0xe59f1088, // LDR r1,[pc,#136] ; [0xd0] =
0xe59f2088, // .. LDR r2,[pc,#136] ; [0xd4]
0xe5812000, // .. STR r2,[r1,#0]  
0xe59f1084, // LDR r1,[pc,#132] ; [0xd8] =
0xe59f206c, // .. LDR r2,[pc,#108] ; [0xc4]
0xe5812000, // .. STR r2,[r1,#0]  
0xe59f007c, // LDR r0,[pc,#124] ; [0xdc] =
0xe3e01701, // MVN r1,#0x40000   
0xe3a020c0, // .. MOV r2,#0xc0  
//    main_loop
0xe3a03803, // MOV r3,#0x30000   
//    wait_loop1
0xe2433001, // SUB r3,r3,#1   
0xe3530000, // CMP r3,#0   
0x1afffffc, // BNE wait_loop1 ; 0x68 Section
//0xe5801000, // STR r1,[r0,#0]  
0xE1C020B0, // .. STRH r2,[r0,#0] 
0xe1a00000, // NOP    
0xe1a00000, // NOP    
0xe3a03803, // MOV r3,#0x30000   
//    wait_loop2
0xe2433001, // SUB r3,r3,#1   
0xe3530000, // CMP r3,#0   
0x1afffffc, // BNE wait_loop2 ; 0x84 Section
//0xe5802000, // .. STR r2,[r0,#0]  
0xE1C020B2, // .. STRH r2,[r0,#2]  
0xe1a00000, // NOP    
0xe1a00000, // NOP    
0xeafffff0, // B main_loop ; 0x64 Section
0xe1a00000, // NOP    
0xe1a00000, // NOP    
0xe1a00000, // NOP    
//  data_sction
0x50030030,
0xb8002000,
0x0000ca13,
0xb8002004,
0x22252d21,
0xb8002008,
0x22220a00,
0xb8002040,
0x0000ce43,
0xb8002044,
0x22252521,
0xb8002048,
0xb4000004
};

int boot_code_size = sizeof(boot_code);
/*--------------------------------------------------------------------------
*
* FUNCTION NAME:  addr_march_test
*
* DESCRIPTION: Address march test
*
* EXTERNAL EFFECTS:  None
*
* PARAMETERS: None
*
* RETURNS: Count of error if there was an error and 0 if there wasn't
*
*--------------------------------------------------------------------------*/
int burst_boot_code (void)
{
	int   rc = RET_OK, ErrCount = 0;
	U16 data, data1;
	int   j, iend;
	U16 *pboot_code;


    iend = boot_code_size/FlashBytesSize;
    
#ifdef DEBUG   
    printf("Burst boot code to flash...\r\n");
#endif

    pboot_code = (U16 *)boot_code;
	for (j = 0; j < iend; j++)
	{
        data = pboot_code[j];
		if ((write_to_flash(&flashptr[j], data)) > 0)
			ErrCount++;
		if (ErrorsNum + ErrCount >= MAX_TEST_ERROR_NUM)
			break;
	}
	
	//verify
	for (j = 0; j < iend; j++)
	{
		if (ErrorsNum + ErrCount >= MAX_TEST_ERROR_NUM)
			break;
        data = pboot_code[j];
		data1 = flashptr[j];
		  if (data1 != data)
		{ 
			ErrCount++;
			error_report(&flashptr[j], data, data1);
		}
	}
    ErrorsNum += ErrCount;

    return ErrCount;
}

int flash_program(U32 SourceAddress, U32 DestinationAddress, U32 ByteCount, U8 channel)
{
	U32 count = 0 ;	
	U32 i = 0;
	U16 *pSrc_ptr;
	U16 *pDest_ptr;

	pSrc_ptr = (U16 *)SourceAddress;
	pDest_ptr = (U16 *)DestinationAddress;
	count = ByteCount/FlashBytesSize;		//number of count that to be program into flash

	for ( i = 0; i < count ; i++)
	{
		if ((write_to_flash(&pDest_ptr[i], pSrc_ptr[i]) > 0))
		{
			return RET_ERR;
		}

	#ifdef DEBUG
		if (!( i % 0x4000))
			printf("P");
	#endif
	

	#ifdef HABTOOLKIT_FLASH_LIB
		if (!( i % 0x4000))
		{
			HAB_flash_status(FLASH_WRITE, 4000,channel);
		}
	#endif
		
	}

	#ifdef DEBUG
		printf("\n");
	#endif
	
	
	return RET_OK;
}

int flash_verify(U32 SourceAddress, U32 DestinationAddress, U32 ByteCount, U8 channel)
{
	U32 count = 0 ;	
	U32 i = 0;
	U16 *pSrc_ptr;
	U16 *pDest_ptr;

	pSrc_ptr = (U16 *)SourceAddress;
	pDest_ptr = (U16 *)DestinationAddress;
	count = ByteCount/FlashBytesSize;		//number of count that to be program into flash

	for ( i = 0; i < count ; i++)
	{
		if (pDest_ptr[i] != pSrc_ptr[i])
		{
			return RET_ERR;
		}

		
	#ifdef DEBUG
		if (!( i % 0x4000))
			printf("V");
	#endif

	#ifdef HABTOOLKIT_FLASH_LIB
		if (!( i % 0x4000))
			HAB_flash_status(FLASH_VERIFY, 4000, channel);
	#endif
	
	}

	#ifdef DEBUG
		printf("\n");
	#endif
	
	return RET_OK;	
}


/*--------------------------------------------------------------------------
*
* FUNCTION NAME:  CheckFlashType
*
* DESCRIPTION:
*   Check if the flash Manufacture code is 0x01 
*
* EXTERNAL EFFECTS:  None
*
* PARAMETERS: 
*		
*
* RETURNS: Always OK currently
*   OK if Flash Manufacture code is 0x01 , else ERR 
*   
*--------------------------------------------------------------------------*/
int CheckFlashType (void) 
{  
	int rc = RET_OK;
	U16 dat;

	*((U16 *)(FlashBaseAddr + FA_WORD_UNLOCK_1)) = FC_UNLOCK1; //  write unlock cycle 1
	*((U16 *)(FlashBaseAddr + FA_WORD_UNLOCK_2)) = FC_UNLOCK2; // write unlock cycle 2 
	*((U16 *)(FlashBaseAddr + FA_WORD_COMMAND)) = FC_DEV_ID; //  write autoselect command 

	
  dat = flashptr[0];  

  if (dat != FLASH_MANUFACTURE_ID)
  {
  	rc = RET_ERR;
#ifdef DEBUG
    printf ("Invalid Manufacturer ID read 0x%04x instead 0x%04x\n", dat, FLASH_DEV_ID)  	;
#endif

  }

  dat = flashptr[1];  
  if (dat != FLASH_DEV_ID)
  {
  	rc = RET_ERR;
#ifdef DEBUG
    printf ("Invalid Device ID read 0x%04x instead 0x%04x\n", dat, FLASH_DEV_ID)  	;
#endif
  }

   flash_reset ();
   
  return rc;  
}  
/*--------------------------------------------------------------------------
*
* FUNCTION NAME:  printStatusErr
*
* DESCRIPTION: Print error string by the given status error code
*
*
* EXTERNAL EFFECTS:  Print error string
*
* PARAMETERS: StatusCode - Flash status error code
*
* RETURNS: 
*
*--------------------------------------------------------------------------*/
void printStatusErr (int StatusCode)
{

	switch (StatusCode)
	{
		case STATUS_ERSUSP:
		{
#ifdef DEBUG
			printf("STATUS_ERSUSP -  erase-suspend\n");
#endif
			break;
		}
		case STATUS_TIMEOUT:
		{
#ifdef DEBUG
			printf("STATUS_TIMEOUT -  timeout\n");
#endif
			break;
		}
		case STATUS_BUSY:
		{
#ifdef DEBUG
			printf("STATUS_BUSY\n");
#endif
			break;
		}
		case STATUS_ERROR:
		{
#ifdef DEBUG

⌨️ 快捷键说明

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