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

📄 i28fxxxj3.c

📁 inter Nor Flash 驱动
💻 C
📖 第 1 页 / 共 3 页
字号:
 *
 ***************************************************************************/
TMPL_Status TMPL_GetBlockAddress ( UINT16 blocknum, 
                                   UINT32 *address )
{

	TMPL_Status stat;

	if ( blocknum < TMPL_TOTAL_NUMBLOCKS )
	{
		*address = TMPL_BASE_FLASH_ADDRESS + ( blocknum * TMPL_BLOCK_NUMBYTES );
	}
	else
	{
		stat.Result = StatBadBlock;
		return( stat );  
	}

	stat.Result = StatCompleted;

	return( stat );

}


/****************************************************************************
 *
 * TMPL_LockBlock
 *
 * Description:   
 *
 *    This procedure is called to lock the specified block on the flash
 *    device.  See the flash device datasheet for specific details on this 
 *    command.
 *
 * Parameters:
 *
 *    IN      blocknum - the block number on the device.
 *
 *    IN      returnSR - flag to indicate whether the device status register
 *                       value should be returned by this function.
 *
 * Returns:   
 *
 *    TMPL_Status - includes function return status defined by enum 
 *                  TMPL_CommandStat and optionally the flash device 
 *                  status register value.
 *
 * Assumptions:
 *
 *    NONE
 *
 ***************************************************************************/
TMPL_Status TMPL_LockBlock ( UINT16 blocknum, 
                             UINT8  returnSR )
{

	TMPL_Status stat;
	UINT32      blockaddr;
   
	stat = TMPL_GetBlockAddress( blocknum, &blockaddr );

	if ( stat.Result != StatCompleted )
	{
		return( stat );
	}

	if ( returnSR )
	{
		TMPL_ClearStatus();
	}

	TMPL_WriteF( blockaddr, TMPL_CONFIG_SETUP );

	TMPL_WriteF( blockaddr, TMPL_LOCK_BIT_SET );

	if ( !TMPL_WaitUntilReady( TMPL_PROGRAM_TIMEOUT ) )
	{
		stat.Result = StatTimeout;
	}
	else
	{
		stat.Result = StatCompleted;
	}

	if ( returnSR )
	{
		stat.SR = TMPL_ReadStatus();
	}

	/* return device to read array mode */
	TMPL_WriteF( TMPL_BASE_FLASH_ADDRESS, TMPL_READ_ARRAY );

	return( stat ); 

}


/****************************************************************************
 *
 * TMPL_LockProtection
 *
 * Description:   
 *
 *    This procedure is called to program the protection register user lock
 *    bit on the flash device.  See the flash device datasheet for specific
 *    details on this command.
 *
 * Parameters:
 *
 *    IN      returnSR - flag to indicate whether the device status register
 *                       value should be returned by this function.
 *
 * Returns:   
 *
 *    TMPL_Status - includes function return status defined by enum
 *                  command_stat and optionally the flash device status
 *                  register value.
 *
 * Assumptions:
 *
 *    NONE
 *
 ***************************************************************************/
TMPL_Status TMPL_LockProtection ( UINT8 returnSR )
{

	UINT32      baseadd;
	UINT32      location;
	UINT32      address;
	TMPL_Status stat;

	location = TMPL_PROTECTION_LOCK_LOCATION;

	baseadd = TMPL_BASE_FLASH_ADDRESS;

	address = TMPL_OTP_BASE + TMPL_BASE_FLASH_ADDRESS;

	address += ( location * sizeof(TMPL_FDATA) );

	if ( returnSR )
	{
		TMPL_ClearStatus();
	}

	TMPL_WriteF(baseadd, TMPL_OTP_PROGRAM );

	TMPL_WriteF(address, TMPL_OTP_LOCK);

	if ( !TMPL_WaitUntilReady( TMPL_PROGRAM_TIMEOUT ) )
	{
		stat.Result = StatTimeout;
	}
	else
	{
		stat.Result = StatCompleted;
	}

	if ( returnSR )
	{
		stat.SR = TMPL_ReadStatus();
	}

	/* return device to read array mode */
	TMPL_WriteF(TMPL_BASE_FLASH_ADDRESS, TMPL_READ_ARRAY );

	return( stat );

}


/****************************************************************************
 *
 * TMPL_PageMode 
 *
 * Description:   
 *
 *    This procedure is called to enable or disable page mode read to
 *    the device.
 *
 * Parameters:
 *
 *    IN    enable - flag "1" to indicate the page mode is anable and 
 *                   flag "0" to indicate the page mode is disable to 
 *                   the device.
 *
 * Returns:   
 *
 *    TMPL_Status - includes function return status defined by enum 
 *                  TMPL_CommandStat and optionally the flash device 
 *                  status register value.
 *
 * Assumptions:
 *
 *    NONE 
 *
 ***************************************************************************/
TMPL_Status TMPL_PageMode ( UINT16 enable )
{

	TMPL_Status stat;
	UINT32      address; /* contains the address to load the 
	                        Read Configuration Register with */
	if ( enable )
	{
		address = TMPL_PAGE_READ_MODE;
	}
	else
	{
		address = TMPL_STD_READ_MODE;
	}

#if ( X_8 || X_16 )
		address = address << 1;  /* shift so address is on A16..A1 */
#else
		address = address << 2;  /* shift so address is on A16..A1 */
#endif
	address += TMPL_BASE_FLASH_ADDRESS; /* offset by base flash mem */ 

	/* write Read Configuration command */
	TMPL_WriteF( TMPL_BASE_FLASH_ADDRESS, TMPL_CONFIG_SETUP );
	TMPL_WriteF( address, TMPL_SET_READ_CONFIG );

	stat.Result = StatCompleted;

	return( stat );

}	


#if X_16
/****************************************************************************
 *
 * TMPL_ProgramFlashBuffered
 *
 * Description:   
 *
 *    This procedure is called to program the flash device at the specified 
 *    starting address contiguously with the specified buffer data.  See
 *    the flash device datasheet for specific details on the write to buffer 
 *    command.
 *
 * Parameters:
 *
 *    IN      address  - the flash address to be programmed.
 *
 *    IN      buffer   - the buffer containing data to be programmed.
 *
 *    IN      numbytes - the number of bytes of data contained in the buffer.
 *
 *    IN      returnSR - flag to indicate whether the device status register
 *                       value should be returned by this function.
 *
 * Returns:   
 *
 *    TMPL_Status - includes function return status defined by enum
 *                  TMPL_CommandStat and optionally the flash device
 *                  status register value.
 *
 * Assumptions:
 *
 *    NONE 
 *
 ***************************************************************************/
TMPL_Status TMPL_ProgramFlashBuffered ( UINT32    address, 
                                        UINT8_PTR buffer, 
                                        UINT32    numbytes,
                                        UINT8     returnSR )
{

   TMPL_Status    stat;
   TMPL_FDATA_PTR fptr;
   TMPL_FDATA     writedata;
   UINT16         numitems; 
   UINT32         cmndaddress; 
   UINT32         numwritebytes;
   UINT32         byteswritten;

   cmndaddress = address;

   if ( ( address + numbytes ) > TMPL_TOTAL_SIZE )
   {
      stat.Result = StatBadAddress;

      return( stat );
   }

   if ( cmndaddress & 0x01 )
   {
      cmndaddress --;
   }

   if ( !TMPL_WaitUntilReady( TMPL_PROGRAM_TIMEOUT ) )
   {
      stat.Result = StatTimeout;
      if ( returnSR )
      {
         stat.SR = TMPL_ReadStatus();
      }

      return( stat );
   }
   else
   {

      if ( returnSR )
      {
         TMPL_ClearStatus();
      }


      /* if (start address is not TMPL_BUFFER_SIZE-byte aligned ) */

      if ( ( address % TMPL_BUFFER_SIZE ) != 0 )
      {
         /* if ( buffer size is > TMPL_BUFFER_SIZE ) or 
            ( buffer crosses block boundary ) */
         if ( ( numbytes > TMPL_BUFFER_SIZE ) ||
		      ( ( address & 0x1 ) && ( numbytes >= TMPL_BUFFER_SIZE ) ) ||
              ( ( address + numbytes -1 ) > ( address | TMPL_BLOCK_MASK) ) 
            )
		 {
		    /* write partial buffer */
		    numwritebytes = ( TMPL_BUFFER_SIZE - ( address % TMPL_BUFFER_SIZE ) );
         }
		 else
		 {
		    /* write all remaining bytes */
		    numwritebytes = numbytes;
		 }

         byteswritten = numwritebytes;

		 TMPL_WriteF( cmndaddress, TMPL_WRITE_TO_BUFFER );
	
		 numitems = numwritebytes / sizeof(TMPL_FDATA);

		 if ( ( ( numwritebytes % sizeof(TMPL_FDATA) ) != 0 ) ||
		        ( ( numwritebytes > 0x01 ) && ( address & 0x01 ) ) )
		 {
	        numitems++;
		 }

		 TMPL_WriteF( cmndaddress, numitems-1 );
	
		 if ( numwritebytes > 0 ) /* while more data to write */ 
		 {
		    while ( numwritebytes > 0 ) /* if more bytes still to write */ 
			{
			   if ( ( address & 0x1 ) != 0 ) /* if destination address is odd */ 
			   {
                      address--;
#if (BIG_ENDIAN_ARCHITECTURE)
					  writedata = (TMPL_FDATA) *buffer;
					  writedata |= 0xff00;
#else /* little endian */ 
					  writedata = *((TMPL_FDATA_PTR)buffer);
					  writedata = ( writedata << 8 ) | 0x00ff;
#endif
					  numwritebytes--;
					  buffer++;
			   }
			   else /* destination address is even */ 
			   {
#if BIG_ENDIAN_ARCHITECTURE
				      /* grab first byte */	
					  writedata = (TMPL_FDATA)( *buffer );
					  writedata = ( ( writedata << 8 ) & 0xff00 );
					  /* grab second byte */	
					  writedata = writedata | ( (TMPL_FDATA) *(buffer+1) );
#else /* little endian architecture */
					  /* grab 2 bytes */
					  writedata = *( (TMPL_FDATA_PTR)buffer ); 
#endif /* BIG_ENDIAN_ARCHITECTURE */ 

					  if ( numwritebytes == 1 ) 
					  {
#if BIG_ENDIAN_ARCHITECTURE
					     writedata |= 0x00ff;
#else
						 writedata |= 0xff00;
#endif
						 numwritebytes--;
					  }
					  else
					  {
					     numwritebytes -= sizeof(TMPL_FDATA);
					  }

					  buffer += sizeof(TMPL_FDATA);
			   }

			   fptr = TMPL_GetFptr(address);
			   *fptr = writedata; 
			   address += sizeof(TMPL_FDATA);
		    }
		 }

		 TMPL_WriteF( cmndaddress, TMPL_CONFIRM );

         if ( !TMPL_WaitUntilReady( TMPL_PROGRAM_TIMEOUT ) )
         {
            stat.Result = StatTimeout;
            if ( returnSR )
            {
               stat.SR = TMPL_ReadStatus();
            }

            return( stat );
         }

         numbytes -= byteswritten;

      } /* end if  ( ( address % TMPL_BUFFER_SIZE )  != 0 ) ) */

      /* while bytes remain */
      while ( numbytes != 0 )
	  {
         /* if TMPL_BUFFER_SIZE bytes remain */
         if ( numbytes > TMPL_BUFFER_SIZE )
		 {
		    /* write full TMPL_BUFFER_SIZE-byte buffer */
		    numwritebytes = TMPL_BUFFER_SIZE;
		 }
		 /* else */
		 else
		 {
		    /* write partial buffer */
		    numwritebytes = numbytes;
         }
         /* end if */

         byteswritten = numwritebytes;

         cmndaddress = address;

		 TMPL_WriteF( cmndaddress, TMPL_WRITE_TO_BUFFER );
	
		 numitems = numwritebytes / sizeof(TMPL_FDATA);

		 if ( ( numwritebytes % sizeof(TMPL_FDATA) ) != 0 )
		 {
	        numitems++;
		 }

		 TMPL_WriteF( cmndaddress, numitems-1 );
	
		 if ( numwritebytes > 0 ) /* while more data to write */ 
		 {
		    while ( numwritebytes > 0 ) /* if more bytes still to write */ 
			{ /* address is known even at this point */
#if BIG_ENDIAN_ARCHITECTURE
		       /* grab first byte */	
			   writedata = (TMPL_FDATA)( *buffer );
			   writedata = ( ( writedata << 8 ) & 0xff00 );
			   /* grab second byte */	
			   writedata = writedata | ( (TMPL_FDATA) *(buffer+1) );
#else /* little endian architecture */
			   /* grab 2 bytes */
			   writedata = *( (TMPL_FDATA_PTR)buffer ); 
#endif /* BIG_ENDIAN_ARCHITECTURE */ 

			   if ( numwritebytes == 1 ) 
			   {
#if BIG_ENDIAN_ARCHITECTURE
			      writedata |= 0x00ff;
#else
				  writedata |= 0xff00;
#endif
				  numwritebytes--;
			   }
			   else
			   {
			      numwritebytes -= sizeof(TMPL_FDATA);
			   }

			   buffer += sizeof(TMPL_FDATA);

			   fptr = TMPL_GetFptr(address);
			   *fptr = writedata; 
			   address += sizeof(TMPL_FDATA);
		    }
		 }

		 TMPL_WriteF( cmndaddress, TMPL_CONFIRM );

         if ( !TMPL_WaitUntilReady( TMPL_PROGRAM_TIMEOUT ) )
         {
            stat.Result = StatTimeout;
            if ( returnSR )
            {
               stat.SR = TMPL_ReadStatus();
            }

            return( stat );
         }

         numbytes -= byteswritten;

      } /* end while numbytes != 0 ) */

   }/* end if ( !TMPL_WaitUntilReady( TMPL_PROGRAM_TIMEOUT ) ) */

   if ( returnSR )
   {
      stat.SR = TMPL_ReadStatus();
   }

   /* return device to read array mode */
   TMPL_WriteF( TMPL_BASE_FLASH_ADDRESS, TMPL_READ_ARRAY );

⌨️ 快捷键说明

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