FAT16 Filecopy

General discussion on mikroPascal PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
karl567
Posts: 64
Joined: 19 Apr 2012 17:25
Location: Konstanz

FAT16 Filecopy

#1 Post by karl567 » 24 Apr 2018 17:33

Hi,
is this the fastest way for copiing 2 files? The file to copy has a size of 150 kByte. And my routine needs about 12 seconds (dsPIC 140 MHz). The MicroSD is class4 (4MB/s).

SPI settings:
SPI2_Init_Advanced(_SPI_Master, SPI_8_Bit, _SPI_Prescale_Sec_1, _SPI_Prescale_Pri_4,
_SPI_SS_Disable, _SPI_Data_Sample_Middle, _SPI_CLK_Idle_High, _SPI_Active_32_Idle);

Code: Select all

Mmc_Fat_Init();
file_handle1:=Mmc_Fat_Open(filename1, file_read, 0x01);
file_handle2:=Mmc_Fat_Open(filename2, file_write, 0x80);

Mmc_Fat_Activate(file_handle2);
Mmc_Fat_Rewrite;
Mmc_Fat_Activate(file_handle1)
Mmc_Fat_Reset(file_size);

for i:=0 to 600-1
do begin
  Mmc_Fat_ReadN(@buffer1,256);
  Mmc_Fat_Activate(file_handle2);
  Mmc_Fat_Write(@buffer1,256);
  Mmc_Fat_Activate(file_handle1);
end;

Mmc_Fat_Close();  // Close first file
Mmc_Fat_Close();  // Close second file 
Karl

karl567
Posts: 64
Joined: 19 Apr 2012 17:25
Location: Konstanz

FAT16 Filecopy

#2 Post by karl567 » 25 Apr 2018 12:20

OK. Now it works. 2 seconds for 150kB!
Is there anybody who can do it faster?

FAT16 copy routine

Code: Select all

function Mmc_Fat_Copy (source:^char; target:^char): byte;
var buffer : array[2048] of byte;
var i:dword;
begin
  GIE_bit:=0;                           // Disable Interrupts
  result:=1;

  Mmc_Fat_Init();
  if Mmc_Fat_Exists(source)=0
  then begin
    result:=2;                          // Error: Sourcefile missing!
    GIE_bit:=1;
    exit;
  end;

  file_handle1:=Mmc_Fat_Open(source,FILE_READ,0x01);
  file_handle2:=Mmc_Fat_Open(target,FILE_WRITE,0x80);
  if (file_handle1<0) or (file_handle2<0)
  then result:=3                        // Error: MicroSD File Error!

  else begin
    i:=0;
    Mmc_Fat_Activate(file_handle2);
    Mmc_Fat_Rewrite();
    Mmc_Fat_Activate(file_handle1);
    Mmc_Fat_Reset(File_Size);

    while File_Size>sizeof(buffer)
    do begin
      Mmc_Fat_Seek(i);
      Mmc_Fat_ReadN(@buffer,sizeof(buffer));
      Mmc_Fat_Activate(file_handle2);
      Mmc_Fat_Write(buffer,sizeof(buffer));
      Mmc_Fat_Activate(file_handle1);
      File_Size:=File_Size-sizeof(buffer);
      i:=i+sizeof(buffer);
    end;
    if File_Size<>0
    then begin
      Mmc_Fat_Seek(i);
      Mmc_Fat_ReadN(@buffer,File_Size);
      Mmc_Fat_Activate(file_handle2);
      Mmc_Fat_Write(buffer,File_Size);
    end;
  end;
  Mmc_Fat_Close();
  Mmc_Fat_Close();
  GIE_bit:=1;                           // Enable Interrupts
end;

Best regards
Karl

Post Reply

Return to “mikroPascal PRO for dsPIC30/33 and PIC24 General”