Src/EEPROM_Emul/eeprom_emul.c
static uint32_t FindPage(EE_Find_type Operation)
{
  EE_State_type currentpagestatus = STATE_PAGE_INVALID, followingpagestatus = STATE_PAGE_INVALID;
  uint32_t currentpage = 0U, followingpage = 0U, previouspage = 0U;

  /* Get currentpage status */
  currentpage = ubCurrentActivePage;
  currentpagestatus = GetPageState(PAGE_ADDRESS(currentpage));

  /* Get followingpage status */
  followingpage = FOLLOWING_PAGE(currentpage);
  followingpagestatus = GetPageState(PAGE_ADDRESS(followingpage));

  /* Get previouspage status */
  previouspage = PREVIOUS_PAGE(currentpage);

  /* Write, read or erase operation */
  switch (Operation)
  {
    case FIND_WRITE_PAGE:   /* ---- Write operation ---- */
      /* Normal operation, no page transfer on going */
      if (currentpagestatus == STATE_PAGE_ACTIVE)
      {
        /* Check if active page is not full */
        if (uwAddressNextWrite < PAGE_SIZE)
        {
          /* Return current Active page */
          return currentpage;
        }
        else
        /* No more space in current active page */
        {
          /* Check if following page is erasing state */
          if (followingpagestatus == STATE_PAGE_ERASING)
          {
            /* Force Cleanup, as not yet performed by user */
            if (EE_CleanUp() != EE_OK)
            {
              return EE_NO_PAGE_FOUND;
            }
          }

          /* Set current active page in valid state */
          if (SetPageState(currentpage, STATE_PAGE_VALID) != EE_OK)
          {
            return EE_NO_PAGE_FOUND;
          }

          /* Set following page as active */
          if (SetPageState(followingpage, STATE_PAGE_ACTIVE) != EE_OK)
          {
            return EE_NO_PAGE_FOUND;
          }
          uwAddressNextWrite = PAGE_HEADER_SIZE;   /* Skip page header */
          return followingpage;         /* Following page is now active one */
        }
      }
      /* Transfer is on going, page receiving data */
      else
      {
        if (currentpagestatus == STATE_PAGE_RECEIVE)
        {
          /* Check if receive page is not full */
          if (uwAddressNextWrite < PAGE_SIZE)
          {
            /* Return current receive page */
            return currentpage;
          }
          else
          /* No more space in current receive page */
          {
            /* Check if following page is erasing state */
            if (followingpagestatus == STATE_PAGE_ERASING)
            {
              /* Force Cleanup, as not yet performed by user */
              if (EE_CleanUp() != EE_OK)
              {
                return EE_NO_PAGE_FOUND;
              }
            }

            /* Set current receive page in valid state */
            if (SetPageState(currentpage, STATE_PAGE_VALID) != EE_OK)
            {
              return EE_NO_PAGE_FOUND;
            }

            /* Set following page as receive */
            if (SetPageState(followingpage, STATE_PAGE_RECEIVE) != EE_OK)
            {
              return EE_NO_PAGE_FOUND;
            }
            uwAddressNextWrite = PAGE_HEADER_SIZE;   /* Skip page header */
            return followingpage;         /* Following page is now active one */
          }
        }
        else
        {
          return EE_NO_PAGE_FOUND;   /* No active Page */
        }
      }

    case FIND_READ_PAGE:  /* ---- Read operation ---- */
      if (currentpagestatus == STATE_PAGE_ACTIVE)
      {
        return currentpage;
      }
      else
      {
        if (currentpagestatus == STATE_PAGE_RECEIVE)
        {
          return previouspage;
        }
        else
        {
          return EE_NO_PAGE_FOUND;   /* No active Page */
        }
      }

    case FIND_ERASE_PAGE: /* ---- Return the erased page */
      if (followingpagestatus == STATE_PAGE_ERASED)
      {
        return followingpage;
      }
      else
      {
        return EE_NO_PAGE_FOUND;  /* No erased Page */
      }

    default:
      ;
  }

  return EE_NO_PAGE_FOUND;
}
/**
  * @brief  Find suitable page for read/write/erase operation.
  *   It also update pages state if current page is full.
  *   And it force cleanup if all pages are full.
  * @param  Operation Type of page to be requested.
  *   This parameter can be one of the following values:
  *     @arg @ref FIND_READ_PAGE return the active page index
  *     @arg @ref FIND_WRITE_PAGE return the write page index
  *     @arg @ref FIND_ERASE_PAGE return the erase page index
  * @retval Page_Index
  *           - Page Index: on success
  *           - @ref EE_NO_PAGE_FOUND : if an error occurs
  */
@brief  Find suitable page for read/write/erase operation.
It also update pages state if current page is full.
And it force cleanup if all pages are full.
@param  Operation Type of page to be requested.
@brief Find suitable page for read / write / erase operation . It also update pages state if current page is full . And it force cleanup if all pages are full . @param Operation Type of page to be requested .
static uint32_t FindPage(EE_Find_type Operation)
{
  EE_State_type currentpagestatus = STATE_PAGE_INVALID, followingpagestatus = STATE_PAGE_INVALID;
  uint32_t currentpage = 0U, followingpage = 0U, previouspage = 0U;
  currentpage = ubCurrentActivePage;
  currentpagestatus = GetPageState(PAGE_ADDRESS(currentpage));
  followingpage = FOLLOWING_PAGE(currentpage);
  followingpagestatus = GetPageState(PAGE_ADDRESS(followingpage));
  previouspage = PREVIOUS_PAGE(currentpage);
  switch (Operation)
  {
    case FIND_WRITE_PAGE:   
      if (currentpagestatus == STATE_PAGE_ACTIVE)
      {
        if (uwAddressNextWrite < PAGE_SIZE)
        {
          return currentpage;
        }
        else
        {
          if (followingpagestatus == STATE_PAGE_ERASING)
          {
            if (EE_CleanUp() != EE_OK)
            {
              return EE_NO_PAGE_FOUND;
            }
          }
          if (SetPageState(currentpage, STATE_PAGE_VALID) != EE_OK)
          {
            return EE_NO_PAGE_FOUND;
          }
          if (SetPageState(followingpage, STATE_PAGE_ACTIVE) != EE_OK)
          {
            return EE_NO_PAGE_FOUND;
          }
          uwAddressNextWrite = PAGE_HEADER_SIZE;   
          return followingpage;         
        }
      }
      else
      {
        if (currentpagestatus == STATE_PAGE_RECEIVE)
        {
          if (uwAddressNextWrite < PAGE_SIZE)
          {
            return currentpage;
          }
          else
          {
            if (followingpagestatus == STATE_PAGE_ERASING)
            {
              if (EE_CleanUp() != EE_OK)
              {
                return EE_NO_PAGE_FOUND;
              }
            }
            if (SetPageState(currentpage, STATE_PAGE_VALID) != EE_OK)
            {
              return EE_NO_PAGE_FOUND;
            }
            if (SetPageState(followingpage, STATE_PAGE_RECEIVE) != EE_OK)
            {
              return EE_NO_PAGE_FOUND;
            }
            uwAddressNextWrite = PAGE_HEADER_SIZE;   
            return followingpage;         
          }
        }
        else
        {
          return EE_NO_PAGE_FOUND;   
        }
      }
    case FIND_READ_PAGE:  
      if (currentpagestatus == STATE_PAGE_ACTIVE)
      {
        return currentpage;
      }
      else
      {
        if (currentpagestatus == STATE_PAGE_RECEIVE)
        {
          return previouspage;
        }
        else
        {
          return EE_NO_PAGE_FOUND;   
        }
      }
    case FIND_ERASE_PAGE: 
      if (followingpagestatus == STATE_PAGE_ERASED)
      {
        return followingpage;
      }
      else
      {
        return EE_NO_PAGE_FOUND;  
      }
    default:
      ;
  }
  return EE_NO_PAGE_FOUND;
}
static uint32_t FindPage ( EE_Find_type Operation ) { EE_State_type currentpagestatus = STATE_PAGE_INVALID , followingpagestatus = STATE_PAGE_INVALID ; uint32_t currentpage = 0U , followingpage = 0U , previouspage = 0U ; currentpage = ubCurrentActivePage ; currentpagestatus = GetPageState ( PAGE_ADDRESS ( currentpage ) ) ; followingpage = FOLLOWING_PAGE ( currentpage ) ; followingpagestatus = GetPageState ( PAGE_ADDRESS ( followingpage ) ) ; previouspage = PREVIOUS_PAGE ( currentpage ) ; switch ( Operation ) { case FIND_WRITE_PAGE : if ( currentpagestatus == STATE_PAGE_ACTIVE ) { if ( uwAddressNextWrite < PAGE_SIZE ) { return currentpage ; } else { if ( followingpagestatus == STATE_PAGE_ERASING ) { if ( EE_CleanUp ( ) != EE_OK ) { return EE_NO_PAGE_FOUND ; } } if ( SetPageState ( currentpage , STATE_PAGE_VALID ) != EE_OK ) { return EE_NO_PAGE_FOUND ; } if ( SetPageState ( followingpage , STATE_PAGE_ACTIVE ) != EE_OK ) { return EE_NO_PAGE_FOUND ; } uwAddressNextWrite = PAGE_HEADER_SIZE ; return followingpage ; } } else { if ( currentpagestatus == STATE_PAGE_RECEIVE ) { if ( uwAddressNextWrite < PAGE_SIZE ) { return currentpage ; } else { if ( followingpagestatus == STATE_PAGE_ERASING ) { if ( EE_CleanUp ( ) != EE_OK ) { return EE_NO_PAGE_FOUND ; } } if ( SetPageState ( currentpage , STATE_PAGE_VALID ) != EE_OK ) { return EE_NO_PAGE_FOUND ; } if ( SetPageState ( followingpage , STATE_PAGE_RECEIVE ) != EE_OK ) { return EE_NO_PAGE_FOUND ; } uwAddressNextWrite = PAGE_HEADER_SIZE ; return followingpage ; } } else { return EE_NO_PAGE_FOUND ; } } case FIND_READ_PAGE : if ( currentpagestatus == STATE_PAGE_ACTIVE ) { return currentpage ; } else { if ( currentpagestatus == STATE_PAGE_RECEIVE ) { return previouspage ; } else { return EE_NO_PAGE_FOUND ; } } case FIND_ERASE_PAGE : if ( followingpagestatus == STATE_PAGE_ERASED ) { return followingpage ; } else { return EE_NO_PAGE_FOUND ; } default : ; } return EE_NO_PAGE_FOUND ; }
@brief  Find suitable page for read/write/erase operation.
@brief Find suitable page for read / write / erase operation .
/* Get currentpage status */ /* Get followingpage status */ /* Get previouspage status */ /* Write, read or erase operation */ /* ---- Write operation ---- */ /* Normal operation, no page transfer on going */ /* Check if active page is not full */ /* Return current Active page */ /* No more space in current active page */ /* Check if following page is erasing state */ /* Force Cleanup, as not yet performed by user */ /* Set current active page in valid state */ /* Set following page as active */ /* Skip page header */ /* Following page is now active one */ /* Transfer is on going, page receiving data */ /* Check if receive page is not full */ /* Return current receive page */ /* No more space in current receive page */ /* Check if following page is erasing state */ /* Force Cleanup, as not yet performed by user */ /* Set current receive page in valid state */ /* Set following page as receive */ /* Skip page header */ /* Following page is now active one */ /* No active Page */ /* ---- Read operation ---- */ /* No active Page */ /* ---- Return the erased page */ /* No erased Page */