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

📄 davflash.c

📁 Flash file system
💻 C
📖 第 1 页 / 共 3 页
字号:
{

   UINT32   write_cnt;
   HW_ERROR Flash_Dev_Write_Err_Code;
   ERR_CODE status = ERR_NONE;

   if (InitializationComplete == FALSE)
   {
      return ERR_SYSTEM;
   }

   if (!ValidUserRange(addr32, byte_count))
   {
      return ERR_PARAM;
   }
   
   addr32 -= FLASH_START_ADDRESS;

   /* Perform writes a block at a time since lowlevel can't handle writing
      across block boundaries. */

   /* Keep writing until no more bytes to write... */
   while (byte_count > 0)
   {
      /* Calculate the number of bytes that can be written without going
         into the next block. */
      if ((addr32 / FLASH_InitInfo.BlockSize) ==
          ((addr32 + byte_count - 1) / FLASH_InitInfo.BlockSize))
      {
         write_cnt = byte_count;
      }
      else
      {
         write_cnt = FLASH_InitInfo.BlockSize -
               (addr32 - ((addr32 / FLASH_InitInfo.BlockSize) *
                FLASH_InitInfo.BlockSize));
      }

      Flash_Dev_Write_Err_Code = FlashDevWrite(buffer_ptr, addr32, write_cnt);

      mDEBUG_CHECK_ERRCODE(Flash_Dev_Write_Err_Code)

      if (Flash_Dev_Write_Err_Code != HW_ERR_NONE)
      {
         status = ERR_WRITE;
         break;
      }

      /* Increment addresses and decrement number of bytes left to write. */
      buffer_ptr += write_cnt;
      addr32 += write_cnt;
      byte_count -= write_cnt;
   }

   /* Close the interface until another initialization sequence occurs. */
   if (status)
   {
      FLASH_Exit();
   }
   
   return status;
}

/*########################################################################
  ### FLASH_ReadBuffer
  ###
  ### Purpose:
  ###   Copy information from a location in flash to a buffer in memory.
  ###
  ### Entry Enviroment:
  ###   The flash interface has been initialized. The buffer_ptr points to
  ###   buffer that is at least byte_count in size.
  ###
  ### Exit Enviroment:
  ###   The buffer pointed to by buffer_ptr now contains byte_count bytes
  ###   of data from the addr32 location in flash.
  ###
  ### Input:
  ###   addr32     - 32 bit address that is the first byte of data to be
  ###                read.
  ###   buffer_ptr - Pointer to a valid buffer in memory that is at least
  ###                byte_count bytes in size.
  ###   byte_count - Number of bytes to read from flash into memory.
  ###
  ### Output:
  ###   buffer_ptr - Pointer to a valid buffer that now contains 
  ###                information copied from flash.
  ###
  ###   ERR_CODE - ERR_NONE.
  ###
*/
ERR_CODE FLASH_ReadBuffer(UINT32    addr32,
                            UINT8_PTR buffer_ptr,
                            UINT32    byte_count)

{


   if (InitializationComplete == FALSE)
   {
      return ERR_SYSTEM;
   }

   if (!ValidUserRange(addr32, byte_count))
   {
      return ERR_PARAM;
   }
   
   addr32 -= FLASH_START_ADDRESS;
   if (FlashDevRead(buffer_ptr, addr32, byte_count) != HW_ERR_NONE)
   {
      return ERR_SYSTEM;
   }

   
   return ERR_NONE;
}   


/*########################################################################
  ### FLASH_CopyFlash
  ###
  ### Purpose:
  ###   Copy data from one area of flash into another.  The source
  ###   and destination should not overlap.
  ###
  ### Entry Enviroment:
  ###   Flash module has been initialized and the flash device(s)
  ###   to be read, is in read array mode.
  ###
  ### Exit Enviroment:
  ###   Flash devices will be in read array mode.
  ###
  ### Input:
  ###   src_addr32 - 32 bit address in flash that is where the information
  ###                will be read from.
  ###   dst_addr32 - 32 bit address in flash where the information
  ###                will be written to.
  ###   byte_count - total number of bytes to copy.
  ###
  ### Output:
  ###   ERR_CODE - Upon completion of the function,
  ###                Success - ERR_NONE
  ###                Failure - Any of the following: 
  ###                          ERR_SYSTEM, STD_InvalidParamater,
  ###                          FLASH_WriteFail, FLASH_VPPFail, 
  ###                          FLASH_CmdSeqFail.
  ###                     
*/
ERR_CODE FLASH_CopyFlash(UINT32 src_addr32,
                           UINT32 dst_addr32,
                           UINT32 byte_count)
{

   UINT32   write_cnt;
   HW_ERROR Flash_Dev_Write_Err_Code;
   ERR_CODE status = ERR_NONE;

   if (byte_count == 0)
   {
      return ERR_NONE;
   }
   
   if (!InitializationComplete)
   {
      return ERR_SYSTEM;
   }
   
   if (!ValidUserRange(src_addr32, byte_count))
   {
      return ERR_PARAM;
   }
   
   if (!ValidUserRange(dst_addr32, byte_count))
   {
      return ERR_PARAM;
   }
 
   dst_addr32 -= FLASH_START_ADDRESS;
   src_addr32 -= FLASH_START_ADDRESS;

   /* Perform writes a block at a time since lowlevel can't handle writing
      across block boundaries. */

   /* Keep copying until no more bytes to copy... */
   while (byte_count > 0)
   {
      /* Calculate the number of bytes that can be written to the destination
         without going into the next block. */
      if ((dst_addr32 / FLASH_InitInfo.BlockSize) ==
          ((dst_addr32 + byte_count - 1) / FLASH_InitInfo.BlockSize))
      {
         write_cnt = byte_count;
      }
      else
      {
         write_cnt = FLASH_InitInfo.BlockSize -
               (dst_addr32 - ((dst_addr32 / FLASH_InitInfo.BlockSize) *
                FLASH_InitInfo.BlockSize));
      }

      Flash_Dev_Write_Err_Code =
            FlashDevCopy(dst_addr32, src_addr32, write_cnt);

      mDEBUG_CHECK_ERRCODE(Flash_Dev_Write_Err_Code)

      if (Flash_Dev_Write_Err_Code != HW_ERR_NONE)
      {
         status = ERR_WRITE;
         break;
      }

      /* Increment addresses and decrement number of bytes left to write. */
      dst_addr32 += write_cnt;
      src_addr32 += write_cnt;
      byte_count -= write_cnt;
   }

   return status;
}


/*########################################################################
  ### FLASH_EraseBlock
  ###
  ### Purpose:
  ###   Erases exactly one block of information. If smart_erase is set,
  ###   the block will be checked first to see if it is already erased.
  ###   If it is already erased, the function will just return with no
  ###   error.
  ###
  ### Entry Enviroment:
  ###   The flash interface has been initialized.
  ###
  ### Exit Enviroment:
  ###   N/A
  ###
  ### Input:
  ###   addr32      - address of a location in the block to erase.
  ###
  ###   smart_erase - if TRUE, the block will be checked before erasing
  ###
  ### Output:
  ###   ERR_CODE - Upon completion of the function,
  ###                Success - ERR_NONE
  ###                Failure - Any of the following: 
  ###                          ERR_SYSTEM, STD_InvalidParamater,
  ###                          FLASH_EraseFail, FLASH_VPPFail,
  ###                          FLASH_CmdSeqFail.
  ###                     
  ###                        
*/
ERR_CODE FLASH_EraseBlock(UINT32 addr32, BOOLEAN smart_erase)

{

   HW_ERROR Flash_Dev_Erase_Err_Code;

   ERR_CODE status = ERR_NONE;

 
   if (InitializationComplete == FALSE)
   {
      return ERR_SYSTEM;
   }
   
   addr32 = (addr32 / FLASH_InitInfo.BlockSize) * 
                                       FLASH_InitInfo.BlockSize;

   if (!ValidUserRange(addr32, FLASH_InitInfo.BlockSize))
   {
      return ERR_PARAM;
   }
   
   if (smart_erase == TRUE)
   {
      if (FLASH_IsBlockErased(addr32))
      {
         return ERR_NONE;
      }
   }
   
   addr32 -= FLASH_START_ADDRESS;

    /* Take the Sem_flashwrite semaphore to ensure that a DAV erase does not
       preempt a background write*/
   SEM_MTX_WAIT(SEM_FlashWrite);

   Flash_Dev_Erase_Err_Code = FlashDevEraseBlock(addr32);

   mDEBUG_CHECK_ERRCODE(Flash_Dev_Erase_Err_Code)
   
   if (Flash_Dev_Erase_Err_Code != HW_ERR_NONE)
   {
      status = ERR_ERASE;
   }

   SEM_MTX_POST(SEM_FlashWrite);

   if (status)
   {
      FLASH_Exit();
   }
   
   return status;
}

/*########################################################################
  ### FLASH_Exit
  ###
  ### Purpose:
  ###   Close the flash interface by executing any necessary cleanup
  ###   functions.
  ###
  ### Entry Enviroment:
  ###   The flash device has been initialized.
  ###
  ### Exit Enviroment:
  ###   Any initialization settings are reset and the interface must be
  ###   reinitialized to continue access to the flash.
  ###
  ### Input:
  ###   N/A
  ###
  ### Output:
  ###   ERR_CODE          - Upon completion of the function,
  ###                         Success - ERR_NONE
  ###                         Failure - ERR_SYSTEM
  ###
*/
ERR_CODE FLASH_Exit(void)
{
   if (InitializationComplete)
   {

      InitializationComplete = FALSE;
   }
   
   return ERR_NONE;
}

#endif /* DIRECT_ACCESS_VOLUME */

⌨️ 快捷键说明

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