[rtems commit] bsps/xqspipsu: Calculate erase sectors correctly

Joel Sherrill joel at rtems.org
Thu Jun 8 14:27:57 UTC 2023


Module:    rtems
Branch:    master
Commit:    7a14c3df8bdeee722022b3353723bde2973f445e
Changeset: http://git.rtems.org/rtems/commit/?id=7a14c3df8bdeee722022b3353723bde2973f445e

Author:    Kinsey Moore <kinsey.moore at oarcorp.com>
Date:      Wed May 24 13:56:41 2023 -0500

bsps/xqspipsu: Calculate erase sectors correctly

When given the exact bounds of a sector, the current algorithm
calculates that 3 sectors need to be erased. This corrects the
calculation such that only 1 sector needs to be erased for erasures that
exactly match sector boundaries.

---

 bsps/shared/dev/spi/xqspipsu-flash-helper.c | 19 +++----------------
 1 file changed, 3 insertions(+), 16 deletions(-)

diff --git a/bsps/shared/dev/spi/xqspipsu-flash-helper.c b/bsps/shared/dev/spi/xqspipsu-flash-helper.c
index 0c1a9ffd2a..cb705ab505 100644
--- a/bsps/shared/dev/spi/xqspipsu-flash-helper.c
+++ b/bsps/shared/dev/spi/xqspipsu-flash-helper.c
@@ -534,22 +534,18 @@ int QspiPsu_NOR_Erase(
   u32 NumSect;
   int Status;
   u32 SectSize;
-  u32 SectMask;
 
   WriteEnableCmd = WRITE_ENABLE_CMD;
 
   if(QspiPsuPtr->Config.ConnectionMode == XQSPIPSU_CONNECTION_MODE_PARALLEL) {
-    SectMask = (Flash_Config_Table[FCTIndex]).SectMask - (Flash_Config_Table[FCTIndex]).SectSize;
     SectSize = (Flash_Config_Table[FCTIndex]).SectSize * 2;
     NumSect = (Flash_Config_Table[FCTIndex]).NumSect;
   } else if (QspiPsuPtr->Config.ConnectionMode == XQSPIPSU_CONNECTION_MODE_STACKED) {
     NumSect = (Flash_Config_Table[FCTIndex]).NumSect * 2;
-    SectMask = (Flash_Config_Table[FCTIndex]).SectMask;
     SectSize = (Flash_Config_Table[FCTIndex]).SectSize;
   } else {
     SectSize = (Flash_Config_Table[FCTIndex]).SectSize;
     NumSect = (Flash_Config_Table[FCTIndex]).NumSect;
-    SectMask = (Flash_Config_Table[FCTIndex]).SectMask;
   }
 
   /*
@@ -614,18 +610,9 @@ int QspiPsu_NOR_Erase(
   /*
    * Calculate no. of sectors to erase based on byte count
    */
-  NumSect = (ByteCount / SectSize) + 1;
-
-  /*
-   * If ByteCount to k sectors,
-   * but the address range spans from N to N+k+1 sectors, then
-   * increment no. of sectors to be erased
-   */
-
-  if (((Address + ByteCount) & SectMask) ==
-    ((Address + (NumSect * SectSize)) & SectMask)) {
-    NumSect++;
-  }
+  u32 SectorStartBase = RTEMS_ALIGN_DOWN(Address, SectSize);
+  u32 SectorEndTop = RTEMS_ALIGN_UP(Address + ByteCount, SectSize);
+  NumSect = (SectorEndTop - SectorStartBase)/SectSize;
 
   for (Sector = 0; Sector < NumSect; Sector++) {
 



More information about the vc mailing list