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

📄 fdi_mpll.c

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

#if (DIRECT_ACCESS_VOLUME == TRUE)
   /* post flash erase mutex */
   SEM_MTX_POST(SEM_FlashErase);
#endif /* DIRECT_ACCESS_VOLUME */

   /*
    * place the current partition into read array mode, and restore
    * the preempted operation's state
    */
   CurrentFlashState = SET_PARTITION_STATE(address, HW_STATE_READ);
   *flash_ptr = FLASH_COMMAND_READ;

   CurrentFlashState = preempted_state;
   flash_ptr = (FLASH_DATA_PTR) GET_PARTITION_ADDRESS(CurrentFlashState);
   switch (GET_PARTITION_STATE(CurrentFlashState))
   {
      case HW_STATE_READ:
         *flash_ptr = FLASH_COMMAND_READ;
         break;
      case HW_STATE_STATUS:
         *flash_ptr = FLASH_COMMAND_STATUS;
         break;
      default:
         break;
   }

   return status;
}



/*#############################################################################
  ### FlashDevCopy
  ###
  ### DESCRIPTION:
  ###   This function is used to copy valid data during reclaim and during
  ###   updates to copy data that is not changing.  The Background Manager,
  ###   Reclaim Task, and Code Manager determine the relative address by
  ###   using the FDI mapping structures.  This function reads and writes
  ###   data by calling FlashDevRead() and FlashDevWrite().
  ###
  ### PARAMETERS:
  ###    IN:  dst_address (DWORD) - The starting address within flash to copy
  ###          data to.
  ###         src_address (DWORD) - The starting address within flash to read
  ###          the data from.
  ###         size (DWORD) - The amount of data, in bytes, to copy.
  ###
  ### RETURNS:
  ###   If the source and destination buffers do not lie entirely within the
  ###   data managed area (or code managed area if DAV is enabled), the
  ###   function returns HW_ERR_PARAM.  If an error occurred while writing
  ###   to the flash hardware, the function returns HW_ERR_WRITE.  Finally,
  ###   if all parameters are within the appropriate ranges and the buffer
  ###   was successfully copied, the funciton returns HW_ERR_NONE.
  ###
 */

/* E5.0.480 START */
HW_ERROR FlashDevCopy(DWORD dst_address, DWORD src_address, DWORD size)
{
   BYTE_PTR       dst_byte_ptr, src_byte_ptr;
   FLASH_DATA_PTR dst_data_ptr, src_data_ptr;
   FLASH_DATA     temp_buffer[COPY_BUFFER_SIZE / sizeof(FLASH_DATA)];

   DWORD dst_offset;
   DWORD byte_size, data_size;
   HW_ERROR status = HW_ERR_NONE;

   DWORD preempted_state;
   BYTE suspended = (BYTE) FALSE;


   /* power loss simulation for testing purposes */
   mDEBUG_PLR_SIMULATION(size);

   /* address validation */
   dst_address += LOWLVL_FLASH_START_ADDRESS;
   VALIDATE_ADDRESS(dst_address, size);
   src_address += LOWLVL_FLASH_START_ADDRESS;
   VALIDATE_ADDRESS(src_address, size);


   /* if size is 0, return */
   if (size == 0)
   {
      return HW_ERR_NONE;
   }


   /* align addresses & determine buffer offset */
   dst_data_ptr = (FLASH_DATA_PTR) ALIGN_ADDRESS(dst_address);
   dst_offset   = UNALIGNED_BYTES(dst_address);
   dst_byte_ptr = (BYTE_PTR) temp_buffer + dst_offset;
   data_size    = COPY_BUFFER_SIZE - dst_offset;
   src_byte_ptr = (BYTE_PTR) src_address;


   /* take flash write mutex and perform hardware precondition */
   SEM_MTX_WAIT(SEM_FlashWrite);
   HARDWARE_PRECONDITION;

   /* save current flash state */
   preempted_state = CurrentFlashState;

   /* suspend if flash is busy erasing */
   CurrentFlashState = SET_PARTITION_STATE(dst_address, HW_STATE_STATUS);
   *dst_data_ptr = FLASH_COMMAND_STATUS;
   if ((*dst_data_ptr & FLASH_STATUS_READY) != FLASH_STATUS_READY)
   {
      *dst_data_ptr = FLASH_COMMAND_SUSPEND;
      *dst_data_ptr = FLASH_COMMAND_STATUS;
      while ((*dst_data_ptr & FLASH_STATUS_READY) != FLASH_STATUS_READY)
      {
      }

      if (*dst_data_ptr & FLASH_STATUS_ERASE_SUSPENDED)
      {
         suspended = (BYTE) TRUE;
      }
   }


   while ((size > 0) && (status == HW_ERR_NONE))
   {
      /* */
      byte_size = GET_MIN(data_size, size);

      /* calculate the number of FLASH_DATA to write */
      data_size = (byte_size + dst_offset + sizeof(FLASH_DATA) - 1) /
                  sizeof(FLASH_DATA);

      /*
       * fill the temp buffer with F's if the amount of data is
       * not the size of the hardware buffer
       */
      if (byte_size != COPY_BUFFER_SIZE)
      {
         /*temp_buffer[0] = ALL_F;*/
         /*temp_buffer[data_size] = ALL_F;*/
         MemorySet((BYTE_PTR) temp_buffer, 0xff, COPY_BUFFER_SIZE);
      }
      src_data_ptr = (FLASH_DATA_PTR) ALIGN_ADDRESS((DWORD) src_byte_ptr);
      CurrentFlashState =
         SET_PARTITION_STATE((DWORD) src_data_ptr, HW_STATE_READ);
      *src_data_ptr = FLASH_COMMAND_READ;
      MemoryMove(dst_byte_ptr, src_byte_ptr, (WORD) byte_size);

      /* write buffer */
      CurrentFlashState =
         SET_PARTITION_STATE((DWORD) dst_data_ptr, HW_STATE_STATUS);
      status = WriteToFlash(dst_data_ptr, temp_buffer, data_size);

      /* update pointers/variables */
      dst_data_ptr += data_size;
      dst_byte_ptr = (BYTE_PTR) temp_buffer;
      src_byte_ptr += byte_size;
      size -= byte_size;
      data_size = COPY_BUFFER_SIZE;
      dst_offset = 0;
   }

   /* place the partition back into read array mode */
   dst_data_ptr--;
   CurrentFlashState = SET_PARTITION_STATE((DWORD)dst_data_ptr, HW_STATE_READ);
   *dst_data_ptr = FLASH_COMMAND_READ;

   /* resume if flash was suspended */
   if (suspended)
   {
      *dst_data_ptr = FLASH_COMMAND_RESUME;
   }

   /* restore flash state */
   CurrentFlashState = preempted_state;
   dst_data_ptr = (FLASH_DATA_PTR) GET_PARTITION_ADDRESS(CurrentFlashState);
   switch (GET_PARTITION_STATE(CurrentFlashState))
   {
      case HW_STATE_READ:
         *dst_data_ptr = FLASH_COMMAND_READ;
         break;
      case HW_STATE_STATUS:
         *dst_data_ptr = FLASH_COMMAND_STATUS;
         break;
      default:
         break;
   }

   /* perform hardware postcondition, post flash write mutex, and return */
   HARDWARE_POSTCONDITION;
   SEM_MTX_POST(SEM_FlashWrite);
   return status;
}
/* E5.0.480 END */



/*#############################################################################
  ### FlashDevRead
  ###
  ### DESCRIPTION:
  ###   Fills the specified buffer with the number of bytes defined by 'size'
  ###   from the device's absolute physical address.  The Background Manager,
  ###   Reclaim Task, and Foreground Manager determine the relative address
  ###   by using the FDI mapping structure.  The FlashDevRead() function
  ###   translates the relative address into a physical system address and
  ###   verifies the address range.  The function reads the data into the
  ###   buffer and returns the appropriate value.
  ###
  ### PARAMETERS:
  ###    IN:  buffer_ptr (BYTE_PTR) - The buffer to use while reading data.
  ###         address (DWORD) - The starting address within flash to read from.
  ###         size (DWORD) - The amount of data, in bytes, to read.
  ###    OUT: buffer_ptr - Contains the contents read.
  ###
  ### RETURNS:
  ###   If the entire source buffer does not lie within the data managed area
  ###   (or code managed area if DAV is enabled), the function returns
  ###   HW_ERR_PARAM.  Otherwise, the function returns HW_ERR_NONE.
  ###
 */

HW_ERROR FlashDevRead(BYTE_PTR buffer_ptr, DWORD address, DWORD size)
{
   volatile FLASH_DATA_PTR flash_data_ptr;
   volatile BYTE_PTR flash_byte_ptr;

   FLASH_DATA_PTR ram_data_ptr;
   BYTE_PTR ram_byte_ptr;

   DWORD flash_offset, ram_offset;
   DWORD preempted_state;
   BYTE suspended = (BYTE) FALSE;


   /* validate address */
   address += LOWLVL_FLASH_START_ADDRESS;
   VALIDATE_ADDRESS(address, size);

   /* return if nothing to read */
   if (size == 0)
   {
      return HW_ERR_NONE;
   }

   /* set up pointers */
   flash_data_ptr = (FLASH_DATA_PTR) ALIGN_ADDRESS(address);
   flash_byte_ptr = (BYTE_PTR) address;
   ram_byte_ptr = buffer_ptr;

   /* calculate alignment offsets */
   flash_offset = (DWORD) UNALIGNED_BYTES(address);
   ram_offset = (DWORD) UNALIGNED_BYTES((DWORD) buffer_ptr);

   /* set current flash state */
   preempted_state = CurrentFlashState;
   CurrentFlashState = SET_PARTITION_STATE(address, HW_STATE_STATUS);

   /* suspend if partition is busy */
   *flash_data_ptr = FLASH_COMMAND_STATUS;
   if (IS_PARTITION_BUSY(flash_data_ptr))
   {
      *flash_data_ptr = FLASH_COMMAND_SUSPEND;
      *flash_data_ptr = FLASH_COMMAND_STATUS;
      while ((*flash_data_ptr & FLASH_STATUS_READY) != FLASH_STATUS_READY)
      {
      }

      if (*flash_data_ptr & (FLASH_STATUS_WRITE_SUSPENDED | 
          FLASH_STATUS_ERASE_SUSPENDED))
      {
         suspended = (BYTE) TRUE;
      }
   }

   /*
    * determine if source and destination addresses allow the reading on
    * aligned addresses
    */
   CurrentFlashState = SET_PARTITION_STATE(address, HW_STATE_READ);
   *flash_data_ptr = FLASH_COMMAND_READ;

   if ((flash_offset == ram_offset) && (size > sizeof(FLASH_DATA)))
   {
      /* read initial unaligned bytes */
      if (flash_offset != 0)
      {
         while (flash_offset < sizeof(FLASH_DATA))
         {
            *ram_byte_ptr = *flash_byte_ptr;
            ram_byte_ptr++;
            flash_byte_ptr++;
            size--;
            flash_offset++;
         }
      }

      /* read aligned data */
      ram_data_ptr = (FLASH_DATA_PTR) ram_byte_ptr;
      flash_data_ptr = (FLASH_DATA_PTR) flash_byte_ptr;
      while (size >= sizeof(FLASH_DATA))
      {
         *ram_data_ptr = *flash_data_ptr;
         ram_data_ptr++;
         flash_data_ptr++;
         size -= sizeof(FLASH_DATA);
      }

      /* read remaining unaligned bytes */
      if (size > 0)
      {
         ram_byte_ptr = (BYTE_PTR) ram_data_ptr;
         flash_byte_ptr = (BYTE_PTR) flash_data_ptr;

         while (size > 0)
         {
            *ram_byte_ptr = *flash_byte_ptr;
            ram_byte_ptr++;
            flash_byte_ptr++;
            size--;
         }
      }
   }

   /*
    * source and destination addresses do not allow the reading on aligned
    * addresses; therefore, a byte-by-byte copy needs to be done
    */
   else
   {
      while (size > 0)
      {
         *ram_byte_ptr = *flash_byte_ptr;
         ram_byte_ptr++;
         flash_byte_ptr++;
         size--;
      }
   }

   /* if suspended, resume */
   if (suspended)
   {
      /* E5.0.480 START */
      CurrentFlashState = SET_PARTITION_STATE((DWORD) flash_data_ptr,
                                              HW_STATE_STATUS);
      *flash_data_ptr = FLASH_COMMAND_STATUS;
      if (*flash_data_ptr & FLASH_STATUS_WRITE_SUSPENDED)
      {
         RESUME_WRITE_SUSPENDED_FLASH(flash_data_ptr);
      }
      else
      {
         *flash_data_ptr = FLASH_COMMAND_RESUME;
      }
      /* E5.0.480 END */
   }

   /* restore flash state */
   CurrentFlashState = preempted_state;
   flash_data_ptr = (FLASH_DATA_PTR) GET_PARTITION_ADDRESS(CurrentFlashState);
   switch (GET_PARTITION_STATE(CurrentFlashState))
   {
      case HW_STATE_READ:
         *flash_data_ptr = FLASH_COMMAND_READ;
         break;
      case HW_STATE_STATUS:
         *flash_data_ptr = FLASH_COMMAND_STATUS;
         break;
      default:
         break;
   }

   return HW_ERR_NONE;
}



#if (CONSISTENT_ACCESS_TIME == TRUE)
/*#############################################################################
  ### FlashDevUpdateDLT
  ###
  ### DESCRIPTION:
  ###   Allows the Reclaim Task to update the data lookup table when the
  ###   status of the spare block is BLOCK_ERASING.  It scans the unit
  ###   headers in the spare block and updates the header index field in

⌨️ 快捷键说明

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