STM32 USB NAND Flash模拟U盘无法格式化问题的解决
扫描二维码
随时随地手机看文章
前几天,一直在寻找NAND Flash模拟U盘程序无法格式化的问题。在中秋月圆之夜,还苦逼地在实验室调代码,也许是杭州大圆月的原因,今晚感觉整人特别亢奋,效率也特别高,灵感也多。终于,在不懈的努力下,找到代码中的害群之马,把无法格式的问题解决掉了。下面就来说说。
这几天一直在想问题出在哪里,不知道自己的代码跟官方的例程对照了多少次,把不一样的地方全都改了一遍,最终未果。今晚思路特别清晰,于是在想到格式化实际上就是向存储器写数据而已,而设计到写数据部分的代码就只在mass_mal.c、memory.c以及存储器的驱动文件。于是反复检查这几个文件,终于发现一点端倪了,问题出在memroy.c这个文件里。
memory.c这个文件只有两个函数:Read_Memory()和Write_Memory()。先贴下代码吧:
void Read_Memory(uint8_t lun, uint32_t Memory_Offset, uint32_t Transfer_Length)
{
static uint32_t Offset, Length;
if (TransferState == TXFR_IDLE )
{
Offset = Memory_Offset * Mass_Block_Size[lun];
Length = Transfer_Length * Mass_Block_Size[lun];
TransferState = TXFR_ONGOING;
}
if (TransferState == TXFR_ONGOING )
{
if (!Block_Read_count)
{
MAL_Read(lun ,
Offset ,
Data_Buffer,
Mass_Block_Size[lun]);
USB_SIL_Write(EP1_IN, (uint8_t *)Data_Buffer, BULK_MAX_PACKET_SIZE);
Block_Read_count = Mass_Block_Size[lun] - BULK_MAX_PACKET_SIZE;
Block_offset = BULK_MAX_PACKET_SIZE;
}
else
{
USB_SIL_Write(EP1_IN, (uint8_t *)Data_Buffer + Block_offset, BULK_MAX_PACKET_SIZE);
Block_Read_count -= BULK_MAX_PACKET_SIZE;
Block_offset += BULK_MAX_PACKET_SIZE;
}
SetEPTxCount(ENDP1, BULK_MAX_PACKET_SIZE);
#ifndef USE_STM3210C_EVAL
SetEPTxStatus(ENDP1, EP_TX_VALID);
#endif
Offset += BULK_MAX_PACKET_SIZE;
Length -= BULK_MAX_PACKET_SIZE;
CSW.dDataResidue -= BULK_MAX_PACKET_SIZE;
Led_RW_ON();
}
if (Length == 0)
{
Block_Read_count = 0;
Block_offset = 0;
Offset = 0;
Bot_State = BOT_DATA_IN_LAST;
TransferState = TXFR_IDLE;
Led_RW_OFF();
}
}
void Write_Memory (uint8_t lun, uint32_t Memory_Offset, uint32_t Transfer_Length)
{
static uint32_t W_Offset, W_Length;
uint32_t temp = Counter + 64;
if (TransferState == TXFR_IDLE )
{
W_Offset = Memory_Offset * Mass_Block_Size[lun];
W_Length = Transfer_Length * Mass_Block_Size[lun];
TransferState = TXFR_ONGOING;
}
if (TransferState == TXFR_ONGOING )
{
for (Idx = 0 ; Counter < temp; Counter++)
{
*((uint8_t *)Data_Buffer + Counter) = Bulk_Data_Buff[Idx++];
}
W_Offset += Data_Len;
W_Length -= Data_Len;
if (!(W_Length % Mass_Block_Size[lun]))
{
Counter = 0;
MAL_Write(lun ,
W_Offset - Mass_Block_Size[lun],
Data_Buffer,
Mass_Block_Size[lun]);
}
CSW.dDataResidue -= Data_Len;
#ifndef STM32F10X_CL
SetEPRxStatus(ENDP2, EP_RX_VALID); /* enable the next transaction*/
#endif /* STM32F10X_CL */
Led_RW_ON();
}
if ((W_Length == 0) || (Bot_State == BOT_CSW_Send))
{
Counter = 0;
Set_CSW (CSW_CMD_PASSED, SEND_CSW_ENABLE);
TransferState = TXFR_IDLE;
Led_RW_OFF();
}
}
在我的工程(我的工程使用NAND的驱动代码是网上流行的圈圈写的驱动):
我们讲下数据的传递过程,以读为例子:
1、如果是读的话会调用Mass_Storage_In()函数
2、接着Mass_Storage_In会调用SCSI_Write10_Cmd()函数
void SCSI_Read10_Cmd(uint8_t lun , uint32_t LBA , uint32_t BlockNbr);
3、SCSI_Read10_Cmd则会调用 Read_Memory(lun, LBA , BlockNbr);
void Read_Memory(uint8_t lun, uint32_t Memory_Offset, uint32_t Transfer_Length);
4、Read_Memory调用MAL_Read(lun ,Offset ,Data_Buffer,Mass_Block_Size[lun]);
5、MAL_Read还会调用FlashReadOneSector()函数,该函数里又有一句:
uint32 FlashReadOneSector(uint32 Addr, uint8 *pBuf, uint32 Remain)
看看FlashReadOneSector()这个函数:
uint32 FlashReadOneSector(uint32 Addr, uint8 *pBuf, uint32 Remain)
{
uint32 i;
if(Addr>FLASH_MAX_SECTOR_ADDR)return 1; //如果地址超出范围,则返回失败代码1,越界
Addr=FlashAddrRemap(Addr); //重新影射地址
if((Addr&(~(FLASH_PAGE_SIZE-1)))
!=(FlashCurrentReadSectorAddr&(~(FLASH_PAGE_SIZE-1)))) //如果跨page
{
//如果跨页的,则写读数据命令
FlashWriteCommand(0x00);
FlashWriteAddr4Byte(Addr);
FlashWriteCommand(0x30);
FlashWait(); //等待数据读回
}
else
{
//如果没有跨页,则可以直接读
FlashWriteCommand(0x05);
FlashWriteAddr2Byte(Addr);
FlashWriteCommand(0xE0);
FlashWait(); //等待数据读回
}
for(i=0;i { pBuf[i]=FlashReadDataByte(); //读一字节数据 } FlashCurrentReadSectorAddr=Addr; //保存当前操作的地址 if(Remain==0) //如果不会接着读,那么就设置当前读过的地址为无效值 { FlashCurrentReadSectorAddr=~0; } return 0; } MAL_Read(lun ,Offset ,Data_Buffer,Mass_Block_Size[lun]);将Mass_Block_Size[lun]作为第三个参数传递给FlashReadOneSector(),这个函数第三个参数是Remain,在这个代码中Remain只有在程序最后if(Remain==0)出现过,而我们调用是传递Mass_Block_Size[lun]这是个常数,永远不会满足if(Remain==0)这个条件,也就是说程序会一直读不会停止。 对于写函数也一样:Write_Memory()会调用MAL_Write(lun ,W_Offset - Mass_Block_Size[lun],Data_Buffer,Mass_Block_Size[lun]),MAL_Write()调用FlashWriteOneSector(),这个函数的代码如下: uint32 FlashWriteOneSector(uint32 Addr, uint8 * pBuf, uint32 Remain) { uint32 i; uint32 SwapPageAddr; // printf("Addr = 0x%xrn",Addr); // printf("Remain = 0x%xrn",Remain); if(Addr>FLASH_MAX_SECTOR_ADDR)return 1; //如果地址超出范围,则返回失败代码1,越界 Addr=FlashAddrRemap(Addr); //重新影射地址 if((Addr&(~(FLASH_PAGE_SIZE-1)))!=(FlashCurrentWriteSectorAddr&(~(FLASH_PAGE_SIZE-1)))) //如果跨page { // printf("跨页rn"); if(FlashNeedWriteBack) //如果前面写了数据,则需要将当前读出的page写回 { if(FlashWritePage()&0x01) //写入失败 { Addr=FlashDealBadBlock(Addr-FLASH_PAGE_SIZE,3)+FLASH_PAGE_SIZE; //坏块处理 } } if((Addr&(~(FLASH_BLOCK_SIZE-1)))!=(FlashCurrentWriteSectorAddr&(~(FLASH_BLOCK_SIZE-1)))) //如果跨block,则需要擦除新的块, { //在擦除之前,要先将原来的块复制到交换区,并且将该块前面部分数据写回 //该函数除了将整块数据复制到交换区以外,并且还将擦除掉原来的块,然后将前面部分复制回原来的块 // printf("跨块rn"); Addr=FlashCopyBlockToSwap(Addr); } //从交换区中读出对应的一页 FlashWriteCommand(0x00); FlashWriteAddr4Byte(FlashGetCurrentSwapBlock()+(Addr&(FLASH_BLOCK_SIZE-1))); FlashWriteCommand(0x35); FlashWait(); //随机写 FlashWriteCommand(0x85); FlashWriteAddr4Byte(Addr); //写4字节地址 for(i=0;i { FlashWriteDataByte(pBuf[i]); } FlashNeedWriteBack=1; //需要写回 } else //没有超过一页地址,则直接写数据 { //随机写 // printf("直接写rn"); FlashWriteCommand(0x85); FlashWriteAddr2Byte(Addr); for(i=0;i { FlashWriteDataByte(pBuf[i]); } FlashNeedWriteBack=1; //需要写回 } FlashCurrentWriteSectorAddr=Addr; //保存本次地址 if(Remain==0) //剩余扇区数为0,不会再写了,需要写回 { if(FlashNeedWriteBack) //如果前面写了数据,则需要将当前读出的page写回 { if(FlashWritePage()&0x01) //写入失败 { Addr=FlashDealBadBlock(Addr,3); //坏块处理 } } //计算剩余页数 Remain=(((Addr+FLASH_BLOCK_SIZE)&(~(FLASH_BLOCK_SIZE-1)))-(Addr&(~(FLASH_PAGE_SIZE-1))))/FLASH_PAGE_SIZE-1; //计算在交换块中的起始页地址 SwapPageAddr=FlashGetCurrentSwapBlock()+(Addr&(FLASH_BLOCK_SIZE-1)); for(i=0;i { Addr+=FLASH_PAGE_SIZE; //从下一页开始写 SwapPageAddr+=FLASH_PAGE_SIZE; if(0x01==(FlashCopyPage(SwapPageAddr,Addr)&0x01)) //如果复制失败 { Addr=FlashDealBadBlock(Addr,2); //处理坏块 } } Fla