简体   繁体   中英

File system, unexpected write/read on disk output

I've been working on this for 3 days, can't see the error, I need fresh eyes :) I work on a online embedded class, and at this particular lab we need to implement a file system. I have unexpected results as described in the code comments in the main function. (E=expected, R=result)

Write/read functions:

#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;
}

The Buff results after reed are unexpected. Why does it work for 0 but not the rest?

Even more strange is when I run this test:

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"

What is happening?

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

The issue is at call of function Flash_WriteArray(). In Flash_WriteArray(), the last parameter should be the number of words (32-bit or 4 bytes) we want to write. To write 512 bytes, this parameter should be 128 (512/4).

如果您的cpu是big endian,则写单词和读取字节可能会给您带来endian问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM