记上篇测试了flash读写,链接:https://bbs.21ic.com/icview-3205114-1-1.html 下面趁热打铁,把flash做成U盘测试下。
找到AT-START-F425开发板关于msc做U盘的例程改动一下。增加flash读写。U盘例子看了一下,很容易就加上了flash读写。
下面在U盘例子上增加一个盘。原例子是把mcu内部flash做的u盘。
首先修改如图
msc_bot_scsi.h文件
#define MSC_SUPPORT_MAX_LUN 2
然后主要是msc_diskio.c内修改:
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] disk read
* @param lun: logical units number
* @param addr: logical address
* @param read_buf: pointer to read buffer
* @param len: read length
* @retval status of usb_sts_type
*/
usb_sts_type msc_disk_read(uint8_t lun, uint32_t addr, uint8_t *read_buf, uint32_t len)
{
uint32_t i = 0;
uint32_t flash_addr = addr + USB_FLASH_ADDR_OFFSET;
<font color="#ff0000"> printf("MSC_R%d>>%X:%X\r\n",lun,addr,len);//打印输出</font>
switch(lun)
{
case INTERNAL_FLASH_LUN:
for(i = 0; i < len; i ++)
{
read_buf[i] = *((uint8_t *)flash_addr);
flash_addr += 1;
}
break;
case SPI_FLASH_LUN:
<font color="#ff0000"> spiflash_read(read_buf,addr,len);</font>
break;
case SD_LUN:
break;
default:
break;
}
return USB_OK;
}
/**
* @brief disk write
* @param lun: logical units number
* @param addr: logical address
* @param buf: pointer to write buffer
* @param len: write length
* @retval status of usb_sts_type
*/
usb_sts_type msc_disk_write(uint8_t lun, uint32_t addr, uint8_t *buf, uint32_t len)
{
uint32_t flash_addr = addr + USB_FLASH_ADDR_OFFSET;
uint32_t i = 0, tolen = len;
uint32_t erase_addr = flash_addr;
<font color="#ff0000"> printf("MSC_W%d>>%X:%X\r\n",lun,addr,len);</font>
switch(lun)
{
case INTERNAL_FLASH_LUN:
flash_unlock();
while(tolen >= sector_size)
{
flash_sector_erase(erase_addr);
tolen -= sector_size;
erase_addr += sector_size;
}
for(i = 0; i < len; i ++)
{
flash_byte_program(flash_addr+i, buf[i]);
}
flash_lock();
break;
<font color="#ff0000"> case SPI_FLASH_LUN:
spiflash_write(buf,addr,len);</font>
break;
case SD_LUN:
break;
default:
break;;
}
return USB_OK;
}
/**
* @brief disk capacity
* @param lun: logical units number
* @param blk_nbr: pointer to number of block
* @param blk_size: pointer to block size
* @retval status of usb_sts_type
*/
usb_sts_type msc_disk_capacity(uint8_t lun, uint32_t *blk_nbr, uint32_t *blk_size)
{
uint32_t devid = (*((uint32_t *)DEBUG_BASE) & 0x00007000) >> 12;
msc_flash_size = (*((uint32_t *)0x1FFFF7E0) << 10) - (USB_FLASH_ADDR_OFFSET - FLASH_BASE);
switch(devid)
{
case 2:
sector_size = SECTOR_SIZE_1K;
break;
case 3:
sector_size = SECTOR_SIZE_2K;
break;
case 4:
sector_size = SECTOR_SIZE_4K;
break;
default:
sector_size = SECTOR_SIZE_2K;
break;
}
switch(lun)
{
case INTERNAL_FLASH_LUN:
*blk_nbr = msc_flash_size / sector_size;
*blk_size = sector_size;
break;
<font color="#ff0000"> case SPI_FLASH_LUN://对应W25Q128
*blk_nbr = 1024*1024*16 / 4096;
*blk_size = 4096; </font>
break;
case SD_LUN:
break;
default:
break;
}
return USB_OK;
}
主要修改基本完成了。记得初始化spiflash。
下面看看效果。枚举了2个U盘,一个内部flash的,一个W25Q128 flash的。枚举后要初始格式化一下。
下面是拷贝文件进去测试。
可以看到读写时串口信息输出。
|