# FATFS + FreeRtos Plus - using NOR Flash (part of Lab-Project-FreeRTOS-FAT-main)

**URL:** <https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911>\
**Category:** Libraries\
**Tags:** debug\
**Created:** [April 10, 2025, 11:42am UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911 "2025-04-10T11:42:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![wshany1234](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/wshany1234/32/5627_2.png) [@wshany1234](https://forums.freertos.org/u/wshany1234)\
**Post date:** [April 10, 2025, 11:42am UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/1 "2025-04-10T11:42:54Z")

</div>

Hi there  
I tried running FATFS on an STM32H563ZI for the first time. The ‘media’ will consist of a 4Mbyte NOR FLASH of wind-bond (W25Q32).  
As a reference, I used the eamples (SD-card) -using LAB project as an example.

Could someone please describe the procedures I need to take in order to activate this FATFS?  
is there exsist a example that run FATFS on flash memory ?

On my example the code failed: !  
The example consiste on SD example (part of the LAB project)  
I made several changes :  
By deleting certain explicit SD parts and changing the other api to use QAUDSPI to connect to my NOR flash, I was able to change the example e.g.  
create a similar function for SD card :  
e.g. FF\_Disk\_t \* FF\_SD-CardDiskInit(const char \* pcName)  
and create:  
FF\_Disk\_t \* FF\_FlashDiskInit(const char \* pcName).

1. Note: The quadspi + windbond NOR Flash was tested independently and found to be operating without any issues.

2. currently the problem was found in the function FF\_FlashDiskInit failed when activate the FF\_CreateIOManager() -this is FATFS original code.  
inside this function it call FF\_FlashMount() and  
there in turn call FF\_Mount()  
that call to FF\_PartitionSearch() - and it seem that the FATFS was not formated and it failed . is it what should I do before calling the init function?  
Thanks  
william

---

<div class="post-metadata">

**Author:** ![karahulx](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/karahulx/32/4893_2.png) [@karahulx](https://forums.freertos.org/u/karahulx)\
**Post date:** [April 11, 2025, 6:06am UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/2 "2025-04-11T06:06:58Z")

</div>

Hi @wshany1234  
Welcome to the FreeRTOS Community Forums!

FF\_PartitionSearch() is failing because there is no valid FAT partition table (MBR or BPB) on your NOR flash yet.  
This might be because the flash is NULL so the filesystem cannot find a valid FAT partition to mount. Before mounting for the first time, you must format it. This means you need to call `FF_Format()` before `FF_Mount` for the first boot-up.

Also can you share your `FF_FlashDiskInit` function ?

---

<div class="post-metadata">

**Author:** ![wshany1234](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/wshany1234/32/5627_2.png) [@wshany1234](https://forums.freertos.org/u/wshany1234)\
**Post date:** [April 11, 2025, 1:49pm UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/3 "2025-04-11T13:49:06Z")

</div>

Regards [Karahulx]  
Now that it makes sense, before your comments - I believe that FF\_FlashMount is in charge of formatting; in light of your remarks, it seems more reasonable that I do it explicitly.  
Yes, I am able to share the init function. slight\_smile:  
I’ll let you know the outcome.  
Thanks

FF\_Disk\_t \* FF\_FlashDiskInit( const char \* pcName )  
{  
FF\_Error\_t xFFError;  
BaseType\_t xPartitionNumber = 0;  
FF\_CreationParameters\_t xParameters;  
FF\_Disk\_t \* pxDisk;

```
xFlashStatus = prvFlashMMCInit( 0 );

if( xFlashStatus != pdPASS )
{
    FF_PRINTF( "FF_FlashInit: prvFLASHMMCInit failed\n" );
    pxDisk = NULL;
}
else
{
    pxDisk = ( FF_Disk_t * ) ffconfigMALLOC( sizeof( *pxDisk ) );
}

if( pxDisk == NULL )
	FF_PRINTF( "FF_FlashInit: Malloc failed\n" );
else
	/* Initialise the created disk structure. */
	memset( pxDisk, '\0', sizeof( *pxDisk ) );

pxDisk->ulNumberOfSectors = xFlashInfo.CardCapacity/ FATFS_FLASH_SECTOR_SIZE;

if( xPlusFATMutex == NULL )
{
	xPlusFATMutex = xSemaphoreCreateRecursiveMutex();
}

pxDisk->ulSignature = flashSIGNATURE;

if( xPlusFATMutex != NULL )
{

	xParameters.pucCacheMemory = NULL;
	memset( &xParameters, '\0', sizeof( xParameters ) );
	xParameters.ulMemorySize = sdIOMAN_MEM_SIZE;
	xParameters.ulSectorSize = FATFS_FLASH_SECTOR_SIZE;
	xParameters.fnWriteBlocks = prvFFWrite;
	xParameters.fnReadBlocks = prvFFRead;
	xParameters.pxDisk = pxDisk;
	#if ( FF_USE_STATIC_CACHE != 0 )
		xParameters.pucCacheMemory = ( uint8_t * ) pucFFBuffer;
	#endif

	/* prvFFRead()/prvFFWrite() are not re-entrant and must be
	 * protected with the use of a semaphore. */
	xParameters.xBlockDeviceIsReentrant = pdFALSE;

	/* The semaphore will be used to protect critical sections in
	 * the +FAT driver, and also to avoid concurrent calls to
	 * prvFFRead()/prvFFWrite() from different tasks. */
	xParameters.pvSemaphore = ( void * ) xPlusFATMutex;

	pxDisk->pxIOManager = FF_CreateIOManager( &xParameters, &xFFError );

	if( pxDisk->pxIOManager == NULL )
	{
		FF_PRINTF( "FF_FlashInit: FF_CreateIOManager: %s\n", ( const char * ) FF_GetErrMessage( xFFError ) );
		FF_FlashDelete( pxDisk );
		uint16_t sector = 400;

		QUADSPI_InitTypeDef_s quadspi_flash_init_params = QUADSPI_DeviceHandleTypeDefArr[0];

		QUADSPI_SectorErase(&quadspi_flash_init_params, sector * FLASH_MEMORY_SECTOR_SIZE,
						  (sector + 1) * FLASH_MEMORY_SECTOR_SIZE - 1);
		pxDisk = NULL;
	}
	else
	{
		pxDisk->xStatus.bIsInitialised = pdTRUE;
		pxDisk->xStatus.bPartitionNumber = xPartitionNumber;

		if( FF_FlashMount( pxDisk ) == 0 )
		{
			FF_FlashDelete( pxDisk );
			pxDisk = NULL;
		}
		else
		{
			if( pcName == NULL )
			{
				pcName = "/";
			}
			FF_FS_Add( pcName, pxDisk );
			FF_PRINTF( "FF_FlashInit: Mounted Flash as root \"%s\"\n", pcName );
			FF_FlashShowPartition( pxDisk );
		}
	} /* if( pxDisk->pxIOManager != NULL ) */
} /* if( xPlusFATMutex != NULL) */
return pxDisk;

```

}

---

<div class="post-metadata">

**Author:** ![wshany1234](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/wshany1234/32/5627_2.png) [@wshany1234](https://forums.freertos.org/u/wshany1234)\
**Post date:** [April 11, 2025, 2:20pm UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/4 "2025-04-11T14:20:55Z")

</div>

Hello once again!

I’ve just added the format to my flow: here it is.

It can be found here if FF\_format() requires a valid pxDisk, which can be done by executing FF\_CreateIOManager( &xParameters, &xFFError ) first.

Note: all the above code is part of the FF\_FlashDiskInit() that i show you before.

 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/2/2b803ab749ce2467ab15315f9e26453ff967876d.png)

(refer to the code).  
Here the line that FF\_format() failed, e.g.  
FF\_format() call to FF\_FormatDisk() -that is turn run :  
—\> if( xPartitionNumber \>= xSet.xPartitionsFound.iCount )) , which is the issue:  
it found that xSet.xPartitionsFound.iCount = 0.  
It indicates that xSet.code.xPartitionsFound.iCount has to be set by some code before activating the format. Correct?  
.  
It appears from looking at the code that FF\_PartitionSearch is one of the options that will accomplish this! but it will set the icount if it found somthing in the flash .got it !?

Which comes first, the chikning or the eggs?  
so the conclution it that some code need to generate the partition before the format can be run. Correct?

I’m a little confused, but I appreciate your assistance.  
Note: I make sure that the flash is erased (e.g. all valus are 0xFF) before running the test of the FF\_format().

william

---

<div class="post-metadata">

**Author:** ![karahulx](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/karahulx/32/4893_2.png) [@karahulx](https://forums.freertos.org/u/karahulx)\
**Post date:** [April 11, 2025, 6:18pm UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/5 "2025-04-11T18:18:03Z")

</div>

`FF_PartitionSearch()` is the function responsible for scanning the device and filling `xPartitionsFound.iCount` (if any partitions exist on disk).  
But, it seems the flash has not been partitioned yet.  
Before calling `FF_Format()` on a fresh erased flash (0xFF everywhere), you must call

```auto
FF_Partition( pxDisk, pParams );

```

This will:

- Create a partition table (MBR).
- Define where your partitions are.
- Set `xPartitionsFound.iCount` properly.
- And then `FF_Format()` can proceed to format those partitions.

[This](https://github.com/FreeRTOS/Lab-Project-FreeRTOS-FAT/blob/main/portable/common/ff_ramdisk.c#L95) can be a good starting point on how the initialization should occur.

Also, in the snippet you mentioned, why is `FF_CreateIOManager` failing , since you call the `FF_Format` in the `else` block?

---

<div class="post-metadata">

**Author:** ![wshany1234](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/wshany1234/32/5627_2.png) [@wshany1234](https://forums.freertos.org/u/wshany1234)\
**Post date:** [April 13, 2025, 4:45pm UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/6 "2025-04-13T16:45:02Z")

</div>

> [@karahulx](#):
>
> ‘FF\_CreateIOManager’

→ regarding the " Also, in the snippet you mentioned, why is `FF_CreateIOManager` failing , since you call the `FF_Format` in the `else` block?"

Maybe I didn’t express this issue properly. Let me explain the gain:  
I only pointed out that ‘FF\_CreateIOManager’ was not failing. is running well.  
To create the pxDisk, first execute FF\_CreateIOManager.  
The FF\_format() function then used this as a parameter; afterward, FF\_format() failed.

Now I realize that FF\_Partition(/) must be executed before FF\_format().  
Let you know.  
Thank you again for your assistance.

---

<div class="post-metadata">

**Author:** ![wshany1234](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/wshany1234/32/5627_2.png) [@wshany1234](https://forums.freertos.org/u/wshany1234)\
**Post date:** [April 13, 2025, 4:49pm UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/7 "2025-04-13T16:49:18Z")

</div>

after running the FF\_Partition(/) 🙂 several issues raise:  
first here is the the updated FF\_FlashDiskInit:  
FF\_Disk\_t \* FF\_FlashDiskInit( const char \* pcName )  
{  
FF\_Error\_t xFFError;  
BaseType\_t xPartitionNumber = 0;  
FF\_CreationParameters\_t xParameters;  
FF\_Disk\_t \* pxDisk;

```
xFlashStatus = prvFlashMMCInit( 0 );

if( xFlashStatus != pdPASS )
{
    FF_PRINTF( "FF_FlashInit: prvFLASHMMCInit failed\n" );
    pxDisk = NULL;
}
else
{
    pxDisk = ( FF_Disk_t * ) ffconfigMALLOC( sizeof( *pxDisk ) );
}

if( pxDisk == NULL )
	FF_PRINTF( "FF_FlashInit: Malloc failed\n" );
else
	/* Initialise the created disk structure. */
	memset( pxDisk, '\0', sizeof( *pxDisk ) );

pxDisk->ulNumberOfSectors = xFlashInfo.CardCapacity/ FATFS_FLASH_SECTOR_SIZE;

if( xPlusFATMutex == NULL )
{
	xPlusFATMutex = xSemaphoreCreateRecursiveMutex();
}

pxDisk->ulSignature = flashSIGNATURE;

if( xPlusFATMutex != NULL )
{

	xParameters.pucCacheMemory = NULL;
	memset( &xParameters, '\0', sizeof( xParameters ) );
	xParameters.ulMemorySize = sdIOMAN_MEM_SIZE;
	xParameters.ulSectorSize = FATFS_FLASH_SECTOR_SIZE;
	xParameters.fnWriteBlocks = prvFFWrite;
	xParameters.fnReadBlocks = prvFFRead;
	xParameters.pxDisk = pxDisk;
	#if ( FF_USE_STATIC_CACHE != 0 )
		xParameters.pucCacheMemory = ( uint8_t * ) pucFFBuffer;
	#endif

	/* prvFFRead()/prvFFWrite() are not re-entrant and must be
	 * protected with the use of a semaphore. */
	xParameters.xBlockDeviceIsReentrant = pdFALSE;

	/* The semaphore will be used to protect critical sections in
	 * the +FAT driver, and also to avoid concurrent calls to
	 * prvFFRead()/prvFFWrite() from different tasks. */
	xParameters.pvSemaphore = ( void * ) xPlusFATMutex;

	pxDisk->pxIOManager = FF_CreateIOManager( &xParameters, &xFFError );

	if( pxDisk->pxIOManager == NULL )
	{
		FF_PRINTF( "FF_FlashInit: FF_CreateIOManager: %s\n", ( const char * ) FF_GetErrMessage( xFFError ) );
		FF_FlashDelete( pxDisk );
		uint16_t sector = 0x400;

		QUADSPI_InitTypeDef_s quadspi_flash_init_params = QUADSPI_DeviceHandleTypeDefArr[0];
		//erase all the flash
		QUADSPI_SectorErase(&quadspi_flash_init_params, 0 * FLASH_MEMORY_SECTOR_SIZE,(sector ) * FLASH_MEMORY_SECTOR_SIZE - 1);
		pxDisk = NULL;
	}
	else
	{
		pxDisk->xStatus.bIsInitialised = pdTRUE;
		pxDisk->xStatus.bPartitionNumber = xPartitionNumber;
		FF_PartitionParameters_t xPartition;
		/* Media cannot be used until it has been partitioned. In this
		case a single partition is to be created that fills all available space - so
		by clearing the xPartition structure to zero. */
		memset( &xPartition, 0x00, sizeof( xPartition ) );
		xPartition.ulSectorCount = pxDisk->ulNumberOfSectors;
		xPartition.ulHiddenSectors = HIDDEN_SECTOR_COUNT;
		xPartition.xPrimaryCount = PRIMARY_PARTITIONS;
		xPartition.eSizeType = eSizeIsQuota;

		FF_Partition(pxDisk,&xPartition);
		//format the fatfs
		FF_Error_t perrot = FF_Format(pxDisk, 1 , pdTRUE ,pdTRUE );
		/* Create the FLASH disk. */

		if( FF_FlashMount( pxDisk ) == 0 )
		{
			FF_FlashDelete( pxDisk );
			pxDisk = NULL;
		}
		else
		{
			if( pcName == NULL )
			{
				pcName = "/";
			}
			FF_FS_Add( pcName, pxDisk );
			FF_PRINTF( "FF_FlashInit: Mounted Flash as root \"%s\"\n", pcName );
			FF_FlashShowPartition( pxDisk );
		}
	} /* if( pxDisk->pxIOManager != NULL ) */
} /* if( xPlusFATMutex != NULL) */
return pxDisk;

```

}

---

<div class="post-metadata">

**Author:** ![wshany1234](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/wshany1234/32/5627_2.png) [@wshany1234](https://forums.freertos.org/u/wshany1234)\
**Post date:** [April 13, 2025, 5:21pm UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/8 "2025-04-13T17:21:10Z")

</div>

\*\* I posted the code after this replay. \*\* The chat can only post one image per replay.

1. Lines 73-74 must be set prior to calling FF\_Partition(). - Otherwise, we don’t do anything, right?

1. After realizing that the FF\_Partition is functioning on a 512-byte sector (0x200), I changed my Flash sector read and write buffer to 512-bytes. (before that is was set to 256 bytes)
2. The partition appears to have started well, as prvPartitionPrimary() was operating, and I can see the pattern on the RAM buffer , which will eventually be saved to the flash.  
However, the final two lines of the prvPartitionPrimary() function are:  
a) FF\_ReleaseBuffer(pxSet-\>pxIOManager, pxSectorBuffer); b) FF\_FlushCache(pxSet-\>pxIOManager);  
regarding the FF\_ReleaseBuffer(): there is a flag inside this function e.g. ffconfigCACHE\_WRITE\_THROUGH that is currently 0 Is this correct? I’m not sure .but leave it as it.

After the running the FF\_Partition then run the the FF\_format.  
here how the first sector is the flash look like:

 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/2/2d103aa1c440a40395227deea70c975a4ca7137c.png)

then FF\_format() is run :  
inside the FF\_format() the FF\_PartitionSearch() is runing first and it return an error .  
it failed here:  
if( xPartitionNumber \>= xSet.xPartitionsFound.iCount )  
when both variable are :  
xPartitionNumber=1  
xSet.xPartitionsFound.iCount =1  
so the code :  
if( xPartitionNumber \>= xSet.xPartitionsFound.iCount )  
{  
xReturn = FF\_createERR( FF\_ERR\_IOMAN\_INVALID\_PARTITION\_NUM, FF\_MODULE\_FORMAT );  
break;  
}

Any thoughts about going to error? we are olmost there…(:\>)  
william

---

<div class="post-metadata">

**Author:** ![wshany1234](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/wshany1234/32/5627_2.png) [@wshany1234](https://forums.freertos.org/u/wshany1234)\
**Post date:** [April 13, 2025, 5:23pm UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/9 "2025-04-13T17:23:21Z")

</div>

This is what the code is referring to.

 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/a/ab0be11eb2ec91d4deaacf1da0a514f484d1d765.png)  
this is the code refering to…

---

<div class="post-metadata">

**Author:** ![karahulx](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/karahulx/32/4893_2.png) [@karahulx](https://forums.freertos.org/u/karahulx)\
**Post date:** [April 14, 2025, 11:14am UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/10 "2025-04-14T11:14:21Z")

</div>

You are seeing this error in `FF_Format()` due to a mismatch between the **partition number** being passed and the number of partitions detected in `FF_PartitionSearch()`.  
You are passing `1` as the partition number, which tells FreeRTOS+FAT to format the second partition (indexing starts from 0). However, `FF_PartitionSearch()` only detects 1 partition total — which means valid partition numbers are only `0`.  
Try changing your format call to use partition 0

```auto
FF_Error_t perrot = FF_Format(pxDisk, 0 , pdTRUE , pdTRUE );

```

---

<div class="post-metadata">

**Author:** ![williamclark](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/williamclark/32/5521_2.png) [@williamclark](https://forums.freertos.org/u/williamclark)\
**Post date:** [April 15, 2025, 5:23am UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/11 "2025-04-15T05:23:49Z")

</div>

It seems like you’re encountering issues while integrating FATFS with NOR flash memory on the STM32H563ZI, particularly when calling `FF_CreateIOManager()` and `FF_Mount()`. The error you’re facing is related to FATFS not recognizing the flash as being formatted. Before initializing FATFS, you need to ensure that the NOR flash is properly formatted with a valid FAT filesystem. Typically, FATFS requires a partition table and a correctly formatted filesystem (FAT16 or FAT32) to work properly. You might need to use a tool or software to manually format the NOR flash with a FAT filesystem if it hasn’t been done already. Once formatted, you should be able to initialize FATFS using `FF_FlashDiskInit()` and proceed with mounting and managing the file system. You can also check that the partition search is correctly identifying the NOR flash as a valid storage device by verifying the partition structure. Ensure that the read/write operations are correctly mapped to the NOR flash through the QuadSPI interface as well.

---

<div class="post-metadata">

**Author:** ![wshany1234](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/wshany1234/32/5627_2.png) [@wshany1234](https://forums.freertos.org/u/wshany1234)\
**Post date:** [April 15, 2025, 8:06am UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/12 "2025-04-15T08:06:00Z")

</div>

Thanks, Clark.  
Yes, we ensure that the flash has been erased before beginning the FatFs init.  
Also, for each flash write, erase the sector first and then write.

Read/write/erase operations are tested separately in bit tests, and they perform properly.  
Thank you for your comments.

---

<div class="post-metadata">

**Author:** ![wshany1234](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/wshany1234/32/5627_2.png) [@wshany1234](https://forums.freertos.org/u/wshany1234)\
**Post date:** [April 15, 2025, 8:09am UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/13 "2025-04-15T08:09:51Z")

</div>

Thanks, Karahulx.  
Yes, the remedy you offered solved the previous problem. Thanks  
However, we are now facing a new issue and I’m on it…let you know.  
william

---

<div class="post-metadata">

**Author:** ![karahulx](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/karahulx/32/4893_2.png) [@karahulx](https://forums.freertos.org/u/karahulx)\
**Post date:** [April 15, 2025, 9:48am UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/14 "2025-04-15T09:48:34Z")

</div>

Thanks for reporting back !

---

<div class="post-metadata">

**Author:** ![wshany1234](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/wshany1234/32/5627_2.png) [@wshany1234](https://forums.freertos.org/u/wshany1234)\
**Post date:** [May 12, 2025, 11:23am UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/15 "2025-05-12T11:23:43Z")

</div>

Hello, Karahulx.  
Sorry about the delay, I was on sick leave.  
Just wanted to let you know that the mount was running smoothly.Thanks  
Could you please explain what the following steps are for opening a new file and writing to it?  
(Perhaps I should open a different ticket?)  
Thanks

---

<div class="post-metadata">

**Author:** ![karahulx](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/karahulx/32/4893_2.png) [@karahulx](https://forums.freertos.org/u/karahulx)\
**Post date:** [May 12, 2025, 3:04pm UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/16 "2025-05-12T15:04:06Z")

</div>

Hi @wshany1234  
That’s great to hear that the previous solution is fixed.  
It would be better to open a new thread and explain the question in detail.  
Thanks.

---

<div class="post-metadata">

**Author:** ![wshany1234](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/wshany1234/32/5627_2.png) [@wshany1234](https://forums.freertos.org/u/wshany1234)\
**Post date:** [July 6, 2025, 12:59pm UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/17 "2025-07-06T12:59:37Z")

</div>

Hello, Karahuix.  
I understand that my sector size must be 4096 bytes because this is the minimum flash area size that can be erase.

This led me to various issues in the Fatfs code.

There is a hard coded value of 512 that corresponds to the sector size. My application must be 4096 bytes!  
here are the examples :

1. 

```auto
FF_IOManager_t * FF_CreateIOManager( FF_CreationParameters_t * pxParameters,
                                     FF_Error_t * pError )
{
......

    /* Normally:
     * ulSectorSize = 512
     * ulCacheSize = N x ulSectorSize. */
    if( ( ( usSectorSize % **512** ) != 0 ) || ( usSectorSize == 0 ) 

```

1. 

```auto
FF_Error_t FF_Mount( FF_Disk_t * pxDisk,
                     BaseType_t xPartitionNumber )
{
    .....
 if( ( ( pxPartition->usBlkSize % **512** ) != 0 ) || ( pxPartition->usBlkSize == 0 ) )
        {

```

What is predicted to happen with this sector’s size? How does it function with my flash sector size of 4096?s?

---

<div class="post-metadata">

**Author:** ![karahulx](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/karahulx/32/4893_2.png) [@karahulx](https://forums.freertos.org/u/karahulx)\
**Post date:** [July 8, 2025, 6:47am UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/18 "2025-07-08T06:47:41Z")

</div>

Hi @wshany1234  
You are correct in understanding the mismatch in the sector size will cause data layout misinterpretation and misalignment issues.

You can try the following approach to solve the problem:

- ulSectorSize = 512

- In your Flash disk driver, manage read-modify-write logic:  
a. Maintain a RAM buffer per 4KB flash sector.  
b. When writing a 512-byte sector:

---

<div class="post-metadata">

**Author:** ![wshany1234](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/wshany1234/32/5627_2.png) [@wshany1234](https://forums.freertos.org/u/wshany1234)\
**Post date:** [July 8, 2025, 1:22pm UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/19 "2025-07-08T13:22:25Z")

</div>

Thanks karahulx,  
So, basically, you want to leave the sectoor size at 512 (in the fatfs code) and follow the strategy you described? if so - it should works.  
There is another method that I believe can be used:  
if the writing address is modulo 4096 ==0, then erase the sector; otherwise, do not erase the sector and then , simply write to flash (all 0xFF cleared) Both should work.  
Let you know.  
Thanks

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [July 8, 2025, 1:28pm UTC](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911/20 "2025-07-08T13:28:46Z")

</div>

Assuming that changing the hardcoding from 512 to 4096 will work in your cause, another option may be to make the sector size compile time configurable. We can introduce a config `ffconfigSECTOR_SIZE` which defaults to 512 for backward compatibility.

[Next page](https://forums.freertos.org/t/fatfs-freertos-plus-using-nor-flash-part-of-lab-project-freertos-fat-main/22911.md?page=2)
