文件系统,磁盘输出上的意外写入/读取

时间:2018-04-02 14:20:47

标签: c embedded keil

我已经为此工作了3天,无法看到错误,我需要新鲜的眼睛:) 我在一个在线嵌入式课程上工作,在这个特定的实验室里,我们需要实现一个文件系统。 我有主要功能中的代码注释中描述的意外结果。 (E =预期,R =结果)

写入/读取功能:

#define EDISK_ADDR_MIN      0x00020000  // Flash Bank1 minimum address
#define EDISK_ADDR_MAX      0x0003FFFF  // Flash Bank1 maximum address
// Write an array of 32-bit data to flash starting at given address.
 int Flash_FastWrite(uint32_t *source, uint32_t addr, uint16_t count)
{
 uint32_t flashkey;
 uint32_t volatile *FLASH_FWBn_R = (uint32_t volatile*)0x400FD100;
 int writes = 0;
  if(MassWriteAddrValid(addr))
 {
  DisableInterrupts();  // may be optional step
  while(FLASH_FMC2_R&FLASH_FMC2_WRBUF){}; // wait for hardware idle
   while((writes < 32) && (writes < count))
  {
   FLASH_FWBn_R[writes] = source[writes];
   writes = writes + 1;
  }
  FLASH_FMA_R = addr;
  if(FLASH_BOOTCFG_R&FLASH_BOOTCFG_KEY)  // by default, the key is 0xA442
   flashkey = FLASH_FMC_WRKEY;
  else                                  // otherwise, the key is 0x71D5
   flashkey = FLASH_FMC_WRKEY2;
  FLASH_FMC2_R = (flashkey|FLASH_FMC2_WRBUF);    // start writing
  while(FLASH_FMC2_R&FLASH_FMC2_WRBUF){};
  EnableInterrupts();
 }
  return writes;
}

// Write 1 sector of 512 bytes of data to the disk, data comes from RAM
enum DRESULT eDisk_WriteSector(
const uint8_t *buff,  // Pointer to the data to be written
uint8_t sector){     // sector number
uint32_t addr;
uint32_t *copybuff;
copybuff =(uint32_t*)(buff);//Flash_WriteArray needs uint32_t format
addr=EDISK_ADDR_MIN+(512*sector);// starting ROM address 
if(addr>EDISK_ADDR_MAX) // return RES_PARERR if exceeds 
    return RES_PARERR;
Flash_WriteArray(copybuff, addr, 512);// write 512 bytes from RAM into ROM                                       
                                      // written by the instructor.
return RES_OK;
}

// Read 1 sector of 512 bytes from the disk, data goes to RAM
enum DRESULT eDisk_ReadSector(
uint8_t *buff,     // Pointer to a RAM buffer into which to store
uint8_t sector){        // sector number to read from
        uint16_t i;
        uint8_t *diskpt;
diskpt=(uint8_t *)(EDISK_ADDR_MIN+512*sector);      // starting ROM address 
if(EDISK_ADDR_MIN+512*sector>EDISK_ADDR_MAX)    
    return RES_PARERR;
else
 {
    for ( i = 0; i < 512; i++ )// copy 512 bytes from ROM into RAM
        buff[i] = *diskpt++;
    return RES_OK;
 }
}
enum DRESULT eDisk_Format(void){
// erase all flash from EDISK_ADDR_MIN to EDISK_ADDR_MAX
for(uint32_t i=EDISK_ADDR_MIN;i<=EDISK_ADDR_MAX;i++)
    Flash_Erase(i);
return RES_OK;
}

uint8_t Buff[512];
 int main(void)
{
 eDisk_Init(0); // if(drive == 0){return RES_OK;}             
 eDisk_Format();
 testbuildbuff("auf0");
 eDisk_WriteSector(Buff,0); 
 testbuildbuff("Excelent"); // writes "Excelent" onto Buff
 eDisk_WriteSector(Buff,1); 
 testbuildbuff("bus3");
 eDisk_WriteSector(Buff,2);
 testbuildbuff("bus4");
 eDisk_WriteSector(Buff,3);
 // E=expected. R=result
 eDisk_ReadSector(Buff, 0); //E: Buff="auf0" R:Buff="auf0"
 eDisk_ReadSector(Buff, 1); //E: Buff="Excelent" R: Buff is empty
 eDisk_ReadSector(Buff, 2); //E: Buff="bus3" R: Buff is empty
 eDisk_ReadSector(Buff, 3); //E: Buff="bus4" R: 4Buff is empty
 return 0;
}

芦苇后的Buff结果是意料之外的。为什么它适用于0而不是其余部分?

更奇怪的是当我进行这个测试时:

testbuildbuff("auf0 buf1 buf2");
eDisk_WriteSector(Buff,2); 
testbuildbuff("Excelent");
eDisk_WriteSector(Buff,1); 
testbuildbuff("bus3");
eDisk_WriteSector(Buff,3);
testbuildbuff("bus4");
eDisk_WriteSector(Buff,4);
eDisk_ReadSector(Buff, 4); //Buff is empty
eDisk_ReadSector(Buff, 3); //Buff is empty
eDisk_ReadSector(Buff, 2); //Buff = "auf0 buf1 buf2"
eDisk_ReadSector(Buff, 1); //Buff = "Excelent"

发生了什么事?

PS:IDE Keil uVision v5.2,uC:TM4C123

2 个答案:

答案 0 :(得分:3)

问题在于函数Flash_WriteArray()的调用。 在Flash_WriteArray()中,最后一个参数应该是我们要写的字数(32位或4字节)。要写入512个字节,此参数应为128(512/4)。

答案 1 :(得分:-1)

如果你的cpu是big endian,写单词和读取字节可能会给你一个endian问题。