最近做一个STM32的USB,看到古道热肠AT45DB161搞成了一个“古老的U盘”,手头有几片那样的SPI FLASH,也想搞搞,但是遇到了些问题,如下: 移植的是STM32的USB库Mass Storage,读写AT45DB161的用了古道热肠的,读写都可以。也能格式化。 https://bbs.21ic.com/upfiles/img/20097/20097717936608.jpg 打开做的U盘,有: https://bbs.21ic.com/upfiles/img/20097/200977171022425.jpg 按文件名以及在WinHex中看到的 https://bbs.21ic.com/upfiles/img/20097/200977171038923.jpg 发现是offset 0中的数据 在U盘里建立一个文本文档 https://bbs.21ic.com/upfiles/img/20097/200977171052287.jpg 保存后,U盘里多了很多东西 https://bbs.21ic.com/upfiles/img/20097/20097717115984.jpg 很奇怪,其中的文件名称和在WinHex中看到的数据一样,怀疑就是读了那数据出来的。比如那个文件夹any key.to就是 https://bbs.21ic.com/upfiles/img/20097/200977171121313.jpg 1E0的内容 本应是FAT12字符的部分,在建立文本文档后也变成了 https://bbs.21ic.com/upfiles/img/20097/200977171135913.jpg
我个人认为可能是USB读写FLASH过程中出了点问题 这是读函数 void Read_Memory(void) {
if (!Block_Read_count) //块读计数不为0 { #ifdef _USE_MSD MSD_ReadBlock(Data_Buffer, Memory_Offset, 512); #else printf("Read_Memory Block_offset:0x%x
",Memory_Offset); DataFlash_ReadSector(Memory_Offset,Data_Buffer ); #endif UserToPMABufferCopy(Data_Buffer, ENDP1_TXADDR, BULK_MAX_PACKET_SIZE); Block_Read_count = 512 - BULK_MAX_PACKET_SIZE; Block_offset = BULK_MAX_PACKET_SIZE; } else { UserToPMABufferCopy(Data_Buffer + Block_offset, ENDP1_TXADDR, BULK_MAX_PACKET_SIZE); Block_Read_count -= BULK_MAX_PACKET_SIZE; Block_offset += BULK_MAX_PACKET_SIZE; }
SetEPTxCount(ENDP1, BULK_MAX_PACKET_SIZE); SetEPTxStatus(ENDP1, EP_TX_VALID);
Memory_Offset += BULK_MAX_PACKET_SIZE; Transfer_Length -= BULK_MAX_PACKET_SIZE;
CSW.dDataResidue -= BULK_MAX_PACKET_SIZE; Led_RW_ON();
if (Transfer_Length == 0) { Block_Read_count = 0; Block_offset = 0; Memory_Offset = 0; Bot_State = BOT_DATA_IN_LAST; Led_RW_OFF(); } }
这个是写函数 void Write_Memory(void) { u32 temp = Counter + 64;
i = 0; for (; Counter < temp; Counter++) { Data_Buffer[Counter] = Bulk_Data_Buff; i++; } Memory_Offset += Data_Len; Transfer_Length -= Data_Len;
if (!(Transfer_Length % 512)) { Counter = 0; #ifdef _USE_MSD MSD_WriteBlock(Data_Buffer, Memory_Offset - 512, 512); #else DataFlash_WriteSector(Memory_Offset - 512,Data_Buffer); printf("Write_Memory:
Memory_Offset0x%x- 0x200=0x%x
",Memory_Offset,(Memory_Offset -512)); #endif }
CSW.dDataResidue -= Data_Len; SetEPRxStatus(ENDP2, EP_RX_VALID); /* enable the next transaction*/
Led_RW_ON();
if ((Transfer_Length == 0) || (Bot_State == BOT_CSW_Send)) { Counter = 0; Set_CSW (CSW_CMD_PASSED, SEND_CSW_ENABLE); Led_RW_OFF(); } }
帮忙看看这个问题,好想我这个也类似古老的U盘也能跑起来,希望得到指点。特别是搞出来过的古道热肠 !
|