2025-10-13 7:53 AM
Hello,
I am trying to set up an external flash memory (MX25V1635F) on a Nucleo-U575ZI-Q using SPI.
- I managed to simulate a disk using RAM memory, Windows formats it perfectly, which leads me to believe that my USBX configuration is correct.
- I also tested my LevelX functions for reading and writing to my NOR flash, and they seem to be working correctly, which leads me to believe that the LevelX configuration is correct.
- I disabled all my FileX functions (format, open, read, and write) because it seems that Windows (USB Host) takes over the formatting. (Btw, if FileX can't be used, how can I write a formatted file to my flash memory?).
My problem: when I leave the USBD_STORAGE_Write and USBD_STORAGE_Read functions empty, Windows can see the size of my disk and offers to format it (without success):
And when I implement these two functions (based on the only example I found online using the Levelx functions lx_nor_flash_sector_write and lx_nor_flash_sector_read: LINK), Windows can no longer read my disk volume or offer to format it. I think my functions are not defined correctly :
/**
* @brief USBD_STORAGE_Read
* This function is invoked to read from media.
* @PAram storage_instance : Pointer to the storage class instance.
* @PAram lun: Logical unit number is the command is directed to.
* @PAram data_pointer: Address of the buffer to be used for reading or writing.
* @PAram number_blocks: number of sectors to read/write.
* @PAram lba: Logical block address is the sector address to read.
* @PAram media_status: should be filled out exactly like the media status
* callback return value.
* @retval status
*/
UINT USBD_STORAGE_Read(VOID *storage_instance, ULONG lun, UCHAR *data_pointer,
ULONG number_blocks, ULONG lba, ULONG *media_status)
{
UINT status = UX_SUCCESS;
/* USER CODE BEGIN USBD_STORAGE_Read */
UX_PARAMETER_NOT_USED(storage_instance);
UX_PARAMETER_NOT_USED(media_status);
while(number_blocks--)
{
// status = fx_media_read(&nor_flash_disk,lba,data_pointer);
status = lx_nor_flash_sector_read(&nor_flash,lba,data_pointer);
data_pointer+=SECTOR_SIZE_LOGICAL;//512
lba++;
if (status != FX_SUCCESS) {
Error_Handler();
}
}
//
// for (UINT block = 0; block < number_blocks; block++)
// {
// //status = fx_media_read(&nor_flash_disk,lba,data_pointer);
// status = lx_nor_flash_sector_read(&nor_flash, lba + block, data_pointer);
// data_pointer += SECTOR_SIZE_LOGICAL;//512
// if(status != UX_SUCCESS)
// {
// break;
// }
// }
/* USER CODE END USBD_STORAGE_Read */
return status;
}
/**
* @brief USBD_STORAGE_Write
* This function is invoked to write in media.
* @PAram storage_instance : Pointer to the storage class instance.
* @PAram lun: Logical unit number is the command is directed to.
* @PAram data_pointer: Address of the buffer to be used for reading or writing.
* @PAram number_blocks: number of sectors to read/write.
* @PAram lba: Logical block address is the sector address to read.
* @PAram media_status: should be filled out exactly like the media status
* callback return value.
* @retval status
*/
UINT USBD_STORAGE_Write(VOID *storage_instance, ULONG lun, UCHAR *data_pointer,
ULONG number_blocks, ULONG lba, ULONG *media_status)
{
UINT status = UX_SUCCESS;
/* USER CODE BEGIN USBD_STORAGE_Write */
UX_PARAMETER_NOT_USED(storage_instance);
UX_PARAMETER_NOT_USED(media_status);
while(number_blocks--)
{
// status = fx_media_write(&nor_flash_disk,lba,data_pointer);
status = lx_nor_flash_sector_write(&nor_flash,lba,data_pointer);
data_pointer+=SECTOR_SIZE_LOGICAL;//512
lba++;
if(status != FX_SUCCESS)
{
Error_Handler();
}
}
// for (UINT block = 0; block < number_blocks; block++)
// {
// //status = fx_media_write(&nor_flash_disk, lba + block, data_pointer);
// status = lx_nor_flash_sector_write(&nor_flash, lba + block, data_pointer);
// data_pointer += SECTOR_SIZE_LOGICAL;//512
// if(status != UX_SUCCESS)
// {
// break;
// }
// }
/* USER CODE END USBD_STORAGE_Write */
return status;
}
Also, should I use lx_nor_flash_open, and if so, where should I call it?
lx_nor_flash_open(&nor_flash, "NOR_FLASH_DISK", lx_stm32_nor_custom_driver_initialize);
Thank you for your help,