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

📄 nor_flash_boot.c

📁 i.MX31 NOR_flash(SPANSION_S71WS256ND0) bootloader src
💻 C
📖 第 1 页 / 共 2 页
字号:
			printf("STATUS_ERROR -  time waiting status ready was exceed\n");
#endif
			break;
		}
		default:
#ifdef DEBUG
			printf("printStatusErr: UNKNOWN STATUS ERROR\n");
#endif
			break;
	}
	return;
}
/*--------------------------------------------------------------------------
*
* FUNCTION NAME:  write_to_flash
*
* DESCRIPTION: Write one item to the flash with waiting status ready
*
* EXTERNAL EFFECTS:  
*
* PARAMETERS: 
*     current_add - flash address to write
*     data        - data to write
*
* RETURNS: On success the function returns OK, else ERR
*
*--------------------------------------------------------------------------*/
int write_to_flash (U16 *current_add, U16 data)
{
	int   ErrCount = 0;


	*((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_PROGRAM; //  write  command 
	*current_add = data;

	if (flash_status(current_add) != STATUS_READY) 
		ErrCount++;

	return ErrCount;
}
/*--------------------------------------------------------------------------
*
* FUNCTION NAME:  flash_reset
*
* DESCRIPTION: Move flash to the read mode
*
* EXTERNAL EFFECTS:  
*
* PARAMETERS: 
*
* RETURNS: 
*
*--------------------------------------------------------------------------*/
void flash_reset ()
{
	flashptr[0] = FC_RESET;

    return ;
}
/*--------------------------------------------------------------------------
*
* FUNCTION NAME:  error_report
*
* DESCRIPTION: Print error message
*
* EXTERNAL EFFECTS:  Print error string
*
* PARAMETERS: 
*         adr             - pointer to the flash address
*         ExpectedData    - the written data
*         ActualData      - the read data
*
* RETURNS: None
*
*--------------------------------------------------------------------------*/
void error_report (U16 *adr, U16 ExpectedData, U16 ActualData)
{
#ifdef DEBUG
  sprintf (out_str, "ERROR IN ADDRESS 0x%08lx,  WROTE 0x%04x, READ 0x%04x \n",
		  (U32)adr, ExpectedData, ActualData); 
  printf(out_str);
#endif
}
/*--------------------------------------------------------------------------
*
* FUNCTION NAME:  flash_status
*
* DESCRIPTION: 
*
* Flash_status utilizes the DQ6, DQ5, and DQ3 polling algorithms    
* described in the flash data book.  It can quickly ascertain the  
* operational status of the flash device, and return an            
* appropriate status code (defined in flash.h)                     
*
*
* EXTERNAL EFFECTS:  Print error string
*
* PARAMETERS: fp - pointer to the flash base address
*
* RETURNS: 
*
*--------------------------------------------------------------------------*/
int flash_status (U16 *fp)
{
	int rc = 0;
	int ready=0;
	int retry;
	U16 data,lastdata;

	retry = ERASE_POLL_LIMIT;

    lastdata = *fp;

    while(!ready && retry-- >= 0)
    {
        data=*fp;
        if((data&0xFFFF)==(lastdata&0xFFFF)) /* test to see if bit6 is NOT toggling */
           {
           ready=1;
           }
        else
           {
           if (data & 0x0020) 
              {
              lastdata=*fp;
              data=*fp;
              if((data&0xFFFF)!=(lastdata&0xFFFF)) /* test to see if bit6 is toggling */
                 {
                 	rc = 1;
                 }
              }
           }   
        lastdata = data;
    }

#ifdef DEBUG    	
    if (rc || ready == 0)
    	printf ("Error wait for flash ready, status = 0x%04x\n", data);
#endif

    return rc;
}

/*--------------------------------------------------------------------------
*
* FUNCTION NAME:  flash_erase_all
*
* DESCRIPTION: Erase flash content
*
* EXTERNAL EFFECTS:  
*
* PARAMETERS:    	
*
* RETURNS: Error count
*
*--------------------------------------------------------------------------*/


int flash_erase_all(void)
{
	int ErrorCount = 0;

	*((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_ERASE_SETUP; //  write  command 
	
	*((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_ALL_SECTORS_ERASE; //  erase all command

	if ((flash_status(flashptr)) != STATUS_READY)
 		ErrorCount++;

	return ErrorCount;	

}

/*--------------------------------------------------------------------------
*
* FUNCTION NAME:  flash_erase_sector
*
* DESCRIPTION: Erase flash sector
*
* EXTERNAL EFFECTS:  
*
* PARAMETERS:    	DestinationAddress - start addr. to be erase
				ByteCount - number of bytes to be erase
*
* RETURNS: Error count
*
*--------------------------------------------------------------------------*/
int flash_sector_erase(U32 DestinationAddress, U32 ByteCount, U8 channel)
{
	U16 *pSector_erase_start;
	U16 *pSector_erase_end;
	U16 *pWalk;

	U32 erase_end_addr = 0;
	U32 SA = 0;
	U32 SectorSize = 0;

	U32 nWalk = 0;
	U32 nSec = 0;

	int ErrorCount = 0;
	

	nWalk = DestinationAddress;
	nSec = FlashBaseAddr;
	
	erase_end_addr = DestinationAddress + (ByteCount - 2);

	pSector_erase_start = (U16 *)DestinationAddress;

	for (SA = 0; SA <262; SA++)
	{
		if ((SA >= 0 && SA <= 3) || (SA >=258 && SA <= 261))
			SectorSize = 32 * 1024;
		else
			SectorSize = 128*1024;

		if ((nWalk >= nSec) && ( nWalk < SectorSize+ nSec) && (nWalk <= erase_end_addr))
		{
			pWalk = (U16 *)nWalk;
			*((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_ERASE_SETUP; //  write  command 
			
			*((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 *)(pWalk)) = FC_SECTOR_ERASE; //  write  command 

			if ((flash_status(pWalk)) != STATUS_READY)
 				ErrorCount++;
	#ifdef DEBUG
			printf("E");	
	#endif

	#ifdef HABTOOLKIT_FLASH_LIB
			{
				int erase_count = 0;
				erase_count = SectorSize;
				HAB_flash_status(FLASH_ERASE, erase_count,channel);		
			}	
	#endif
			nWalk += SectorSize;
		}
		nSec +=SectorSize;		// increment the nSec for each SA

	}

#ifdef DEBUG
			printf("\n");
#endif	

	return ErrorCount;

}




int flash_loader(U32 SourceAddress, U32 DestinationAddress, U32 ByteCount, U8 channel) 
{
	int rc = RET_OK;
	int i = 0;

  FlashBytesSize = FLASH_BYTES_2; // for AMD 16bits

  FlashBaseAddr = FLASH_BASE_ADDR;	

  flashptr = (U16 *)FlashBaseAddr;	// pointer for the target flash address

  ErrorsNum = 0;

#ifdef DEBUG    	
  printf ("After running  test set Set on CPU SW2 3- OFF, 5- OFF to run from flash\n");
#endif

  // issue a reset command
  flash_reset ();

#ifdef DEBUG
  // Check Flash Type
  if ((rc = CheckFlashType()) == RET_ERR)
  {
	printf("Flash Type not support\n");
	return rc;
  }
#endif



 // Erase	
  if (( rc = flash_sector_erase (DestinationAddress, ByteCount, channel)) == RET_ERR)
 {
#ifdef DEBUG
	printf("Flash Erase Error\n");
#endif

#ifdef HABTOOLKIT_FLASH_LIB
	while (1)
		HAB_flash_status(FLASH_ERASE_ERROR, 0, channel);
#endif

	return rc;
 }

// Write
  if (( rc = flash_program (SourceAddress, DestinationAddress, ByteCount,channel)) == RET_ERR)
 {
#ifdef DEBUG
	printf("Flash Write Error\n");
#endif

#ifdef HABTOOLKIT_FLASH_LIB
	while (1)
		HAB_flash_status(FLASH_WRITE_ERROR, 0, channel);
#endif

	return rc;
 }

// verify
  if (( rc = flash_verify (SourceAddress, DestinationAddress, ByteCount,channel)) == RET_ERR)
 {
#ifdef DEBUG
	printf("Flash Verify Error\n");
#endif

#ifdef HABTOOLKIT_FLASH_LIB
	
	while (1)
		HAB_flash_status(FLASH_VERIFY_ERROR, 0, channel);
#endif

	return rc;
 }

#ifdef HABTOOLKIT_FLASH_LIB
	// send 3 OK to ensure that PC received the OK signal
	//for ( i = 0; i < 3; i++)

	while (1)
		HAB_flash_status(FLASH_OK, 0, channel);
#endif
	
  

  return rc;
}

⌨️ 快捷键说明

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