Copy files from SD card to USB drive small problem

General discussion on mikroC PRO for PIC32.
Post Reply
Author
Message
gzoino
Posts: 141
Joined: 05 Sep 2009 15:48

Copy files from SD card to USB drive small problem

#1 Post by gzoino » 11 Feb 2014 15:14

Hi,

I've been trying to develop this small routine to copy all my existing files from the onboard SD card to my USB drive utilizing the USB Host port of my LV 32MX v6. I finally got it to work with the modification I made to the example given to the PIC32 Host. But I'm having a small problem where the data can not be copied to the new created file of the USB drive. The only way it does copy the data is by me first typing some data myself into the file created on the USB drive. then my routine below will do the copying. But it only copies as many characters from the SD card to the USB drive which I typed in my self to The USB drive. It seems as though the file created in the USB drive is not usable unless you write to it yourself first like initializing the space :? . It probably has something to do with the creation of the file or file format which I'm not aware of. Can please some advise me what should be done to allow my route below to automatically add or append files to the new created file without me having to open the created file on the USB drive and write characters to it in order for my routine to then be able to write to that file. I hope the explanation is pretty clear as to the problem I've added comments to help you understand what each section does to help. This is only a piece of the example which I modified to do this and which is responsisble for the problem I'm having, it will not work alone if you cut and paste but should be enough to analyze the problem. Thanks for any advise

GZoino


// Application Part
uint8_t USBHost_MSCApplication(){
switch(MSC_App_State){
case 0:
if (FAT32_Init() == 0){
TFT_Write_Text("insert sd card", 190, 190);
MSC_App_State = 1;
}else{
TFT_Write_Text("two", 120, 140);
TFT_Write_Text(msgDevFatError, 10, 20);
}
break;
// the purpose is to copy files with data from sd card to USB drive
case 1: //this routene first checks if there is any files with the names starting with 'PSC_0001.txt on the sd card, if the file exist,
//it will create the same file name to the USB drive under the PSC directory. It then copies data existing in the 'PSC_0001.txt
//file to the file from the sd card to the file which was created in the USB Drive under the same name file. this is done sequentially
//untill it doesn't find any further files in the sd card.

FAT32_MakeDir("PSC"); //{ // creates DIR_A directory on USB DRIVE
FAT32_ChangeDir("PSC"); //change directory

if(1== Mmc_Fat_Exists("PSC_0001.TXT")){ // if the first "0001.txt file exists on the sd card then execute this section

for(USB_File = 1;;USB_File++){


SPI2_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, 64, _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_ACTIVE_2_IDLE);
Delay_ms(10);
//--- init the FAT library
if (!Mmc_Fat_Init()) {
// reinitialize spi at higher speed
SPI2_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, 8, _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_ACTIVE_2_IDLE);
}

FAT32_Init();
FAT32_ChangeDir("PSC");

IntToStr(USB_File/1000, FileNum2); //this section is used to create files name starting with "0001.txt on-
filename[4] = FileNum2[5];

IntToStr(USB_File/100, FileNum2); //the USB drive which is insertec into the USB Host port.
filename[5] = FileNum2[5];

IntToStr(USB_File/10, FileNum2); //each file will be created in number sequential wise
filename[6] = FileNum2[5];

IntToStr(USB_File, FileNum2);
filename[7] = FileNum2[5];

if(1 == Mmc_Fat_Exists(filename)){
Drawbox(&Box1);
TFT_Write_Text(filename, 190, 200);

fhandle = Mmc_Fat_Open(filename, FILE_READ, 0x01);// This function opens a file for manipulation.
Mmc_Fat_Assign(filename,0x01);
position = 0;
Mmc_Fat_Seek(position);//This routine is used to set the cursor position within an opened file and returns the cursor's new position within an opened file.
Delay_ms(10);
no_bytes = Mmc_Fat_ReadN(&data_buff, 5000);//This function reads multiple bytes.
fHandle = FAT32_Open(filename, FILE_WRITE);
FAT32_Write(fHandle,data_buff, 5000);

}else if(0 == Mmc_Fat_Exists(filename)){
break;
}

}

}

// FAT32_Dir(); this display tye current directory on the usb drive
MSC_App_State = 2;
break;

case 2 :
break;
}
return _USB_HOST_MSC_CMD_PASSED;
}

hooper
Posts: 143
Joined: 23 Oct 2009 08:51

Re: Copy files from SD card to USB drive small problem

#2 Post by hooper » 28 Jun 2014 10:15

hi gzoino,

Any solution for the issue?
I actually need the reverse direction: copy files from SD card to the USB stick.

gzoino
Posts: 141
Joined: 05 Sep 2009 15:48

Re: Copy files from SD card to USB drive small problem

#3 Post by gzoino » 30 Jun 2014 23:00

Hi yes this issue was revolved. Turns out that a closing statement was missing at the end which was not shown in the example given. After e-mailing my sample code to support, this was found to be the culprit, which resolved the issue. I believe there is a way to reverse this but you will have to look at the commands and example after downloading the USB host library.

GZ

hooper
Posts: 143
Joined: 23 Oct 2009 08:51

Re: Copy files from SD card to USB drive small problem

#4 Post by hooper » 01 Jul 2014 15:14

Thank you.
:D

hooper
Posts: 143
Joined: 23 Oct 2009 08:51

Re: Copy files from SD card to USB drive small problem

#5 Post by hooper » 07 Oct 2014 02:12

By the way, should this code work for usb and sd
both formatted to fat32 file system?

vzaka
Posts: 5
Joined: 21 Aug 2013 17:10

Re: Copy files from SD card to USB drive small problem

#6 Post by vzaka » 13 Jan 2021 21:52

Hello,

I see that you could write to USB flash drive .
Please, what USB Host Library (for PIC32) have you used to write to USB stick, in addition to FAT32_Library?

Now , I am using PIC24
Using only the FAT32_Library, I cannot write to USB. I can read/write only to SD card.
The other library, the USB_HOST_dsPIC, let me display only the directories of the USB stick.

Your help will be highly appreciated.
vzaka

Post Reply

Return to “mikroC PRO for PIC32 General”