硬汉嵌入式论坛

 找回密码
 立即注册
查看: 1394|回复: 8
收起左侧

USBX filex levelx MSC 模拟U盘插入可识别,但无法格式化和存大一点的文件

[复制链接]

1

主题

9

回帖

12

积分

新手上路

积分
12
发表于 2025-1-5 22:17:59 | 显示全部楼层 |阅读模式
最近在学Threadx,就想着用用filex+levelx玩玩,于是就用STM32H750的内部512K SRAM模拟了一个U盘,现在U盘可以发现,可以存小一点的文件,也可以正常读取,但是存400K左右非常缓慢,也不容易存进去,200K左右的秒存,读的也快,格式化一直不成功,非常郁闷,哪位大神帮忙看看是什么问题啊!这个问题困扰我好久了,一直搞不定!

下面是部分代码:
lx_stm32_nor_simulator_driver.c:

static UINT  lx_nor_simulator_read(ULONG *flash_address, ULONG *destination, ULONG words)
{

    memcpy((VOID *)destination, (VOID *)flash_address, words * sizeof(ULONG));

    return(LX_SUCCESS);
}


static UINT  lx_nor_simulator_write(ULONG *flash_address, ULONG *source, ULONG words)
{

    memcpy((VOID *)flash_address, (VOID *)source, words * sizeof(ULONG));

    return(LX_SUCCESS);
}

static UINT  lx_nor_simulator_block_erase(ULONG block, ULONG erase_count)
{

                uint32_t index;
    ULONG   *pointer;

    //LX_PARAMETER_NOT_USED(erase_count);

    /* Setup pointer.  */
       
         for(index=0;index < erase_count;index++){

                        pointer = (ULONG *) (LX_NOR_SIMULATOR_FLASH_BASE_ADDRESS + (block+index) * (LX_NOR_SIMULATOR_SECTOR_SIZE * LX_NOR_SIMULATOR_SECTORS_PER_BLOCK));

                        /* Loop to erase block.  */
                        mem_set((VOID *) pointer, 0xff, words_per_block * sizeof(ULONG));
                }
         
    return(LX_SUCCESS);
}


usb_device_msc.c:

UINT USBD_STORAGE_Read(VOID *storage_instance, ULONG lun, UCHAR *data_pointer,
                       ULONG number_blocks, ULONG lba, ULONG *media_status)
{
  UINT status = UX_SUCCESS;

    while(number_blocks--)
    {
        status =  fx_media_read(&nor_simulator_flash_disk,lba,data_pointer);
        data_pointer+=STORAGE_SECTOR_SIZE;
        lba++;
    }  

  return 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;

    while(number_blocks--)
    {
        status =  fx_media_write(&nor_simulator_flash_disk,lba,data_pointer);
        data_pointer+=STORAGE_SECTOR_SIZE;
        lba++;
    }  

       
  /* USER CODE END USBD_STORAGE_Write */

  return status;
}


app_filex.c:

        if(flag == 0){
                nor_sim_status =  fx_media_format(&nor_simulator_flash_disk,                                                                     // nor_simulator_flash_disk pointer
                                                                                                                                                        fx_stm32_levelx_nor_driver,                                                                    // Driver entry
                                                                                                                                                        (VOID *)LX_NOR_SIMULATOR_DRIVER_ID,                                                            // Device info pointer //LX_NOR_SIMULATOR_DRIVER_ID
                                                                                                                                                        (UCHAR *) fx_nor_simulator_media_memory,                                                       // Media buffer pointer
                                                                                                                                                        sizeof(fx_nor_simulator_media_memory),                                                         // Media buffer size
                                                                                                                                                        FX_NOR_SIMULATOR_VOLUME_NAME,                                                                  // Volume Name
                                                                                                                                                        FX_NOR_SIMULATOR_NUMBER_OF_FATS,                                                               // Number of FATs
                                                                                                                                                        32,                                                                                            // Directory Entries
                                                                                                                                                        FX_NOR_SIMULATOR_HIDDEN_SECTORS,                                                               // Hidden sectors
                                                                                                                                                        1024,//((LX_NOR_SIMULATOR_FLASH_SIZE - LX_NOR_SIMULATOR_SECTOR_SIZE) / FX_NOR_SIMULATOR_SECTOR_SIZE), // Total sectors minus one
                                                                                                                                                        512,                                                                                                                                                                          // Sector size
                                                                                                                                                        8,                                                                                             // Sectors per cluster
                                                                                                                                                        1,                                                                                             // Heads
                                                                                                                                                        1);                                                                                            // Sectors per track


                /* Check the format nor_sim_status */
                if (nor_sim_status != FX_SUCCESS)
                {
                        /* USER CODE BEGIN NOR Simulator format error */
                        while(1);
                        /* USER CODE END NOR Simulator format error */
                }
        }
  /* Open the SIMULATOR NOR driver */
  nor_sim_status =  fx_media_open(&nor_simulator_flash_disk, FX_NOR_SIMULATOR_VOLUME_NAME, fx_stm32_levelx_nor_driver, (VOID *)LX_NOR_SIMULATOR_DRIVER_ID, (VOID *) fx_nor_simulator_media_memory, sizeof(fx_nor_simulator_media_memory));

  /* Check the media open nor_sim_status */
  if (nor_sim_status != FX_SUCCESS)
  {
    /* USER CODE BEGIN NOR Simulator open error */
    while(1);
    /* USER CODE END NOR Simulator open error */
  }
  /* USER CODE BEGIN fx_app_thread_entry 1 */
       
  /* Get the available usable space */
  nor_ospi_status =  fx_media_space_available(&nor_simulator_flash_disk, &available_space_pre);

  /* Check the get available state request status.  */
  if (nor_ospi_status != FX_SUCCESS)
  {
    Error_Hand();
  }

  /* Create a file called STM32.TXT in the root directory.  */
  nor_ospi_status =  fx_file_create(&nor_simulator_flash_disk, "STM32.TXT");

  /* Check the create status.  */
  if (nor_ospi_status != FX_SUCCESS)
  {
    /* Check for an already created status. This is expected on the
    second pass of this loop!  */
    if (nor_ospi_status != FX_ALREADY_CREATED)
    {
      /* Create error, call error handler.  */
      Error_Hand();
    }
  }

  /* Open the test file.  */
  nor_ospi_status =  fx_file_open(&nor_simulator_flash_disk, &fx_file, "STM32.TXT", FX_OPEN_FOR_WRITE);

  /* Check the file open status.  */
  if (nor_ospi_status != FX_SUCCESS)
  {
    /* Error opening file, call error handler.  */
    Error_Hand();
  }

  /* Seek to the beginning of the test file.  */
  nor_ospi_status =  fx_file_seek(&fx_file, 0);

  /* Check the file seek status.  */
  if (nor_ospi_status != FX_SUCCESS)
  {
    /* Error performing file seek, call error handler.  */
    Error_Hand();
  }

  /* Write a string to the test file.  */
  nor_ospi_status =  fx_file_write(&fx_file, data, sizeof(data));

  /* Check the file write status.  */
  if (nor_ospi_status != FX_SUCCESS)
  {
    /* Error writing to a file, call error handler.  */
    Error_Hand();
  }

  /* Close the test file.  */
  nor_ospi_status =  fx_file_close(&fx_file);

  /* Check the file close status.  */
  if (nor_ospi_status != FX_SUCCESS)
  {
    /* Error closing the file, call error handler.  */
    Error_Hand();
  }

  nor_ospi_status = fx_media_flush(&nor_simulator_flash_disk);

  /* Check the media flush  status.  */
  if (nor_ospi_status != FX_SUCCESS)
  {
    /* Error closing the file, call error handler.  */
    Error_Hand();
  }

   /* Open the test file.  */
  nor_ospi_status =  fx_file_open(&nor_simulator_flash_disk, &fx_file, "STM32.TXT", FX_OPEN_FOR_READ);

  /* Check the file open status.  */
  if (nor_ospi_status != FX_SUCCESS)
  {
    /* Error opening file, call error handler.  */
    Error_Hand();
  }

  /* Seek to the beginning of the test file.  */
  nor_ospi_status =  fx_file_seek(&fx_file, 0);

  /* Check the file seek status.  */
  if (nor_ospi_status != FX_SUCCESS)
  {
    /* Error performing file seek, call error handler.  */
    Error_Hand();
  }

  /* Read the first 28 bytes of the test file.  */
  nor_ospi_status =  fx_file_read(&fx_file, read_buffer, sizeof(data), &bytes_read);

  /* Check the file read status.  */
  if ((nor_ospi_status != FX_SUCCESS) || (bytes_read != sizeof(data)))
  {
    /* Error reading file, call error handler.  */
    Error_Hand();
   }

  /* Close the test file.  */
  nor_ospi_status =  fx_file_close(&fx_file);

  /* Check the file close status.  */
  if (nor_ospi_status != FX_SUCCESS)
  {
    /* Error closing the file, call error handler.  */
    Error_Hand();
  }
#if 0
  /* Get the available usable space, after the file has been created */
  nor_ospi_status =  fx_media_space_available(&nor_simulator_flash_disk, &available_space_post);

  /* Check the get available state request status.  */
  if (nor_ospi_status != FX_SUCCESS)
  {
    Error_Hand();
  }

  /* Close the media.  */
  nor_ospi_status =  fx_media_close(&nor_simulator_flash_disk);

  /* Check the media close status.  */
  if (nor_ospi_status != FX_SUCCESS)
  {
    /* Error closing the media, call error handler.  */
    Error_Hand();
  }
#endif
  /* USER CODE END fx_app_thread_entry 1 */
  /* USER CODE END fx_app_thread_entry 0 */
        for(;;){
                HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
                tx_thread_sleep(200);
        }

}
回复

使用道具 举报

1

主题

9

回帖

12

积分

新手上路

积分
12
 楼主| 发表于 2025-1-5 22:23:14 | 显示全部楼层
这个是整个工程,谁能帮忙看看啊!

stm32h750(filex levelx usbx msc ram2).7z

16.38 MB, 下载次数: 10

回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
117512
QQ
发表于 2025-1-6 01:51:21 | 显示全部楼层
有个关键的地方,虚拟出来的U盘,电脑端显示容量是多少。
回复

使用道具 举报

10

主题

75

回帖

120

积分

初级会员

积分
120
发表于 2025-1-6 09:15:49 | 显示全部楼层
levelx把1个4096物理扇区划分成8个512逻辑扇区,再把第1个逻辑扇区存lx信息,所以每个物理扇区实际可用空间只有7/8。总512KB实际可用448KB。
再加上fat32会占用一部分空间存文件系统组织结构,实际空闲空间肯定没那么大了。可以通过media的api获取,或者像硬汉说的看电脑端识别出来的信息。
还有
lx_nor_flash_total_blocks计算时不需要-1吧,它不是最大block号,而是block总数。
Sectors per cluster设置为1好些吧,因为lx的原因,设置成8也不可能跟物理扇区对齐了。
回复

使用道具 举报

1

主题

9

回帖

12

积分

新手上路

积分
12
 楼主| 发表于 2025-1-6 13:36:06 | 显示全部楼层
eric2013 发表于 2025-1-6 01:51
有个关键的地方,虚拟出来的U盘,电脑端显示容量是多少。

显示的是512K
回复

使用道具 举报

1

主题

9

回帖

12

积分

新手上路

积分
12
 楼主| 发表于 2025-1-6 13:39:17 | 显示全部楼层
egoistaw 发表于 2025-1-6 09:15
levelx把1个4096物理扇区划分成8个512逻辑扇区,再把第1个逻辑扇区存lx信息,所以每个物理扇区实际可用空间 ...

1.lx_nor_flash_total_blocks不减1也试过,没有作用
2.Sectors per cluster设为1试过,没有作用,
3.可用内存显示500K左右,存400K就得等半天
现在格式化不了,不知道是啥情况
回复

使用道具 举报

10

主题

75

回帖

120

积分

初级会员

积分
120
发表于 2025-1-6 23:48:30 | 显示全部楼层
看了一下,你的工程整体问题比较多。USB MSC 扇区读写驱动有问题,没有加上number_blocks,导致仅仅在重复读取当前扇区。文件系统格式化也有问题,会导致可用存储虚标。我发了个帖子附上了可用的levelx的demo。
回复

使用道具 举报

1

主题

9

回帖

12

积分

新手上路

积分
12
 楼主| 发表于 2025-1-7 08:01:10 | 显示全部楼层
egoistaw 发表于 2025-1-6 23:48
看了一下,你的工程整体问题比较多。USB MSC 扇区读写驱动有问题,没有加上number_blocks,导致仅仅在重复 ...

非常感谢,看到你的贴子一定好好研究一下
回复

使用道具 举报

10

主题

75

回帖

120

积分

初级会员

积分
120
发表于 2025-1-7 10:27:59 | 显示全部楼层
callan 发表于 2025-1-7 08:01
非常感谢,看到你的贴子一定好好研究一下

昨晚没看到你的代码里有个lba++,USB读写应该没啥问题。帖子我发文件系统分区了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-8-12 06:53 , Processed in 0.059196 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表