USB Mass Storage interface with external NOR-Flash connected via SPI to STM32F412ZG
Hello,
I am trying to interface USB Mass Storage with external Flash. I have implemented the complete structure that is necessary as below,
typedef struct _USBD_STORAGE
{
int8_t (* Init) (uint8_t lun);
int8_t (* GetCapacity) (uint8_t lun, uint32_t *block_num, uint16_t *block_size);
int8_t (* IsReady) (uint8_t lun);
int8_t (* IsWriteProtected) (uint8_t lun);
int8_t (* Read) (uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
int8_t (* Write)(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
int8_t (* GetMaxLun)(void);
int8_t *pInquiry;
}USBD_StorageTypeDef;
USBD_StorageTypeDef USBD_MSC_Template_fops =
{
FLASH_Init,
FLASH_GetCapacity,
FLASH_IsReady,
FLASH_IsWriteProtected,
FLASH_Read,
FLASH_Write,
FLASH_GetMaxLun,
(int8_t *)UMSC_FLASH_Inquirydata,
};In addition, I have defined all the functions that performs flash operations (i.e read/write operations, initialization etc). I have also passed the structure to USB register,
/* Init Device Library,Add Supported Class and Start the library*/
USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);
USBD_RegisterClass(&hUsbDeviceFS, &USBD_MSC);
USBD_MSC_RegisterStorage(&hUsbDeviceFS, &USBD_MSC_Template_fops);
USBD_Start(&hUsbDeviceFS);When i Connect to the PC, It detects as a mass storage device. But when i try to open, it opts me to format the flash. When try to format, it always throws me an error that flash cannot be formatted. The configurations that i have set in PC and my code are as follows,
#define FLASH_LUN_NBR 1
#define FLASH_BLK_NBR 0x2000
#define FLASH_BLK_SIZ 2048
#define FLASH_END_ADDR 0xFFFFFF
#define FLASH_START_ADDR 0x000000
PC side configuration:
Allocation of bytes --> 256KB
Is anything else has to be configured to make this work or my code requires a FTL or FATfs for this to work? (According to my research the PC FATfs should take care of this?) I am currently using a direct SPI read write operations to the flash.
The hardware I am using are as follows,
MCU : STM32F412ZG (SPI2 Bus)
Flash: S25FL129p (NOR Type, 16MB)
Sector size : 256KB
It would be great if someone helps me out on this problem.