Skip to main content
Visitor II
November 22, 2019
Question

Hi ,i have created a file stm32.txt by porting fatfs into flash and verified write and read baremetal,now can this file be visible to my pc if i bring up and add an usb interface msc device config.Is this possible .

  • November 22, 2019
  • 3 replies
  • 1104 views

I have done this:

1) created a file using fatfs on flash open and read ,it is working.

2) created a usb interface configured as msc device,when plugged into pc, new drive gets created, however it asks to format it. I have added only

read functionality of flash to this:

STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) . not added write functionality.

/////////////////////////////////////main.c//////////////////////////////////////////

USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);

 USBD_RegisterClass(&hUsbDeviceFS, &USBD_MSC);

USBD_MSC_RegisterStorage(&hUsbDeviceFS,&USBD_Storage_Interface_fops_FS);  

USBD_Start(&hUsbDeviceFS); 

 if( FATFS_LinkDriver(&USER_Driver, USERPath)== 0)

{

  /*##-2- Init the SD Card #################################################*/

#if FATFS_MKFS_ALLOWED

   FRESULT res;

    res = f_mkfs(USERPath, FM_ANY, 0, workBuffer, 4096);

   if (res != FR_OK)

   {

    Error_Handler();

   }

#endif

 FS_FileOperations();

the usb does not detect the file stm32.txt created , fs_fileoperation also fails .

what is wrong ,thanks in advance.

    This topic has been closed for replies.

    3 replies

    Graduate II
    November 22, 2019

    I wouldn't try to access them both concurrently, but yes a correctly built MSC device should be able to access a FatFs created volume on a PC.

    The MSC would need to report the capacity and block size used.

    sviiAuthor
    Visitor II
    November 25, 2019

    Hi, i am not able to format the flash below is the code :

    int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)

    {

      

    uint32_t add_blk = 0;

    add_blk = blk_addr;

    uint32_t a0 = 0;

    uint32_t a1 = 0;

      

    a0 = add_blk * 512;   

     a1 = blk_len * 1;

     SST80B_ReadSector(buf,0,a0,a1);

      

     return (USBD_OK);

      

    }

    int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)

    {

    uint8_t temp_b0[512] ={0};

     uint8_t temp_b1[512] ={0};

     uint8_t temp_b2[512] ={0};

     uint8_t temp_b3[512] ={0};

     uint8_t temp_b4[512] ={0};

     uint8_t temp_b5[512] ={0};

     uint8_t temp_b6[512] ={0};

     uint8_t temp_b7[512] ={0};

     uint32_t a0 = 0;

     uint32_t a1 = 0;

     a0 = add_blk * 512;   

     a1 = blk_len * 1;

     uint32_t add_data0 = (a0/4096); 

     uint32_t add_data1 = add_data0 * 4096; 

     uint32_t blka = a0;  

     uint32_t blkc = 0;

     SST80B_ReadSector(temp_b1,0,(add_data1+512),512);

     SST80B_ReadSector(temp_b2,0,(add_data1+1024),512);

     SST80B_ReadSector(temp_b3,0,(add_data1+1536),512);

     SST80B_ReadSector(temp_b4,0,(add_data1+2048),512);

     SST80B_ReadSector(temp_b5,0,(add_data1+2560),512);

     SST80B_ReadSector(temp_b6,0,(add_data1+3072),512);

     SST80B_ReadSector(temp_b7,0,(add_data1+3584),512);

     if(blka < 512)

     blkc = 1;

     else if ((blka >= 512)&& (blka < 1024))

     blkc = 2; 

     else if ((blka >= 1024)&& (blka < 1536))

     blkc = 3; 

     else if ((blka >= 1536)&& (blka < 2048))

     blkc = 4;

     else if ((blka >= 2048)&& (blka < 2560))

     blkc = 5;

     else if ((blka >= 2560)&& (blka < 3072))

     blkc = 6;

     else if ((blka >= 3072)&& (blka < 3584))

     blkc = 7;

     else

     blkc =8; 

     chipsector_erase_call(add_data1);

     if(blkc == 1)

     {

    SST25Write((a0),(uint8_t *)buf,512);

    SST25Write((add_data1+512),(uint8_t *)temp_b1,512);

    SST25Write((add_data1+1024),(uint8_t *)temp_b2,512);

    SST25Write((add_data1+1536),(uint8_t *)temp_b3,512);

    SST25Write((add_data1+2048),(uint8_t *)temp_b4,512);

    SST25Write((add_data1+2560),(uint8_t *)temp_b5,512);

    SST25Write((add_data1+3072),(uint8_t *)temp_b6,512);

    SST25Write((add_data1+3584),(uint8_t *)temp_b7,512);

    blkc = 0;

     }

    if(blkc == 2)

     {

    SST25Write(a0,(uint8_t *)buf,512);

    SST25Write((add_data1),(uint8_t *)temp_b0,512);

    SST25Write((add_data1+1024),(uint8_t *)temp_b2,512);

    SST25Write((add_data1+1536),(uint8_t *)temp_b3,512);

    SST25Write((add_data1+2048),(uint8_t *)temp_b4,512);

    SST25Write((add_data1+2560),(uint8_t *)temp_b5,512);

    SST25Write((add_data1+3072),(uint8_t *)temp_b6,512);

    SST25Write((add_data1+3584),(uint8_t *)temp_b7,512);

     blkc = 0;

     }

    if(blkc == 3)

     {

    SST25Write((a0),(uint8_t *)buf,512);

    SST25Write((add_data1),(uint8_t *)temp_b0,512);

    SST25Write((add_data1+512),(uint8_t *)temp_b1,512);

    SST25Write((add_data1+1536),(uint8_t *)temp_b3,512);

    SST25Write((add_data1+2048),(uint8_t *)temp_b4,512);

    SST25Write((add_data1+2560),(uint8_t *)temp_b5,512);

    SST25Write((add_data1+3072),(uint8_t *)temp_b6,512);

    SST25Write((add_data1+3584),(uint8_t *)temp_b7,512);

    blkc = 0;

     }

      

    if(blkc == 4)

     {

    SST25Write((a0),(uint8_t *)buf,512);

    SST25Write((add_data1),(uint8_t *)temp_b0,512);

    SST25Write((add_data1+512),(uint8_t *)temp_b1,512);

    SST25Write((add_data1+1024),(uint8_t *)temp_b2,512);

    SST25Write((add_data1+2048),(uint8_t *)temp_b4,512);

    SST25Write((add_data1+2560),(uint8_t *)temp_b5,512);

    SST25Write((add_data1+3072),(uint8_t *)temp_b6,512);

    SST25Write((add_data1+3584),(uint8_t *)temp_b7,512);

    blkc = 0;

     }

    if(blkc == 5)

     {

    SST25Write(a0,(uint8_t *)buf,512);

    SST25Write((add_data1),(uint8_t *)temp_b0,512);

    SST25Write((add_data1+512),(uint8_t *)temp_b1,512);

    SST25Write((add_data1+1024),(uint8_t *)temp_b2,512);

    SST25Write((add_data1+1536),(uint8_t *)temp_b3,512);

    SST25Write((add_data1+2560),(uint8_t *)temp_b5,512);

    SST25Write((add_data1+3072),(uint8_t *)temp_b6,512);

    SST25Write((add_data1+3584),(uint8_t *)temp_b7,512);

    blkc = 0;

     }

    if(blkc == 6)

     {

    SST25Write(a0,(uint8_t *)buf,512);

    SST25Write((add_data1),(uint8_t *)temp_b0,512);

    SST25Write((add_data1+512),(uint8_t *)temp_b1,512);

    SST25Write((add_data1+1024),(uint8_t *)temp_b2,512);

    SST25Write((add_data1+1536),(uint8_t *)temp_b3,512);

    SST25Write((add_data1+2048),(uint8_t *)temp_b4,512);

    SST25Write((add_data1+3072),(uint8_t *)temp_b6,512);

    SST25Write((add_data1+3584),(uint8_t *)temp_b7,512);

    blkc = 0;

     }

    if(blkc == 7)

     {

    SST25Write(a0,(uint8_t *)buf,512);

    SST25Write((add_data1),(uint8_t *)temp_b0,512);

    SST25Write((add_data1+512),(uint8_t *)temp_b1,512);

    SST25Write((add_data1+1024),(uint8_t *)temp_b2,512);

    SST25Write((add_data1+1536),(uint8_t *)temp_b3,512);

    SST25Write((add_data1+2048),(uint8_t *)temp_b4,512);

    SST25Write((add_data1+2560),(uint8_t *)temp_b5,512);

    SST25Write((add_data1+3584),(uint8_t *)temp_b7,512);

    blkc = 0;

     }

     if(blkc == 8)

     {

    SST25Write(a0,(uint8_t *)buf,512);

    SST25Write((add_data1),(uint8_t *)temp_b0,512);

    SST25Write((add_data1+512),(uint8_t *)temp_b1,512);

    SST25Write((add_data1+1024),(uint8_t *)temp_b2,512);

    SST25Write((add_data1+1536),(uint8_t *)temp_b3,512);

    SST25Write((add_data1+2048),(uint8_t *)temp_b4,512);

    SST25Write((add_data1+2560),(uint8_t *)temp_b5,512);

    SST25Write((add_data1+3072),(uint8_t *)temp_b6,512);

    blkc = 0;

     }

     return (USBD_OK);

    }

    thanks in advance.

    Graduate II
    November 25, 2019

    That does seem overly complex and convoluted. Is the erase size a sector or a sub-sector?

    Can't you tell the systems you're using a 4KB block size.

    You have stack space to accommodate this? Using GNU/GCC?​

    sviiAuthor
    Visitor II
    November 26, 2019

    Hi thanks for the reply, the erase is the sector size which is 4K minimum for the flash erase operation, i did configure to use 4KB block size allocation but the system fails , it would have been easier if it had worked , hence tried to fix the size to 512 bytes allocation size .Sorry for the above code, didn't use any comments to follow,

    what it does is to read ,erase , write a sector for 512 bytes with 4k erase without loosing the subsector data within the sector.Thanks in advance.

    sviiAuthor
    Visitor II
    November 26, 2019

    hi, the

    a0 = add_blk * 512;   

    a1 = blk_len * 1;

    a0 is the address to the flash ie add_blk= 1; a0 =512.

    a1 is the blk len should this multiplied by 512 else remain the same as above confused . Thanks

    sviiAuthor
    Visitor II
    November 27, 2019

    hi, thanks i was able to make the usb msc work ,created a folder wrote a text file and checked it.