This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

USB Stick Writing Speed is slow

Other Parts Discussed in Thread: SYSBIOS

Hi,

I am using TI-RTOS FatSDUSBCopy Example Code. I removed SD card reading part and tried to write in USB Stick with 512 buffer size. I could achieved around 1.5Mbps (192kBpS). How can I improve writing speed? .

Thank you 

  • Jerad,

    Which device are you using, and which version of TI-RTOS?

    Do you know what speed is supported by the USB stick you are using?  I’ve seen some reports of some flash drives topping out at 3Mbps.  Also, have you tried different buffer sizes?

    Thanks,
    Scott

  • Hi Scott,

    Thank you for your support. I am using Tiva C TM4C1294XL Connected Launchpad with TI-RTOS for TivaC 2.10.1.38.
    I tried changing buffer size from 512 to 4096 and with different USB sticks.

    Thank you,
    Jerad
  • Jerad,

    OK, thanks.

    Can you please post some code showing how you modified the example?  For example, if no longer reading from the SD card, are you writing a pre-allocated buffer?  And are you writing it over and over again?  And how you are determining  the write speed – between what points in your program are you measuring this?

    Thanks,
    Scott

  • Hi, Scott,

    Thank you for the reply.

    I measured putting breakpoints between fopen and fclose function. Then I calculate Time manually. I expected more than 6Mbps for my application. I expect your soon reply.

    Thank you.
    Regards,

    Jerad

    /*
    * ======== fatsdusbcopy.c ========
    */

    /* XDCtools Header files */
    #include <xdc/std.h>
    #include <xdc/cfg/global.h>
    #include <xdc/runtime/System.h>

    /* BIOS Header files */
    #include <ti/sysbios/BIOS.h>
    #include <ti/sysbios/knl/Clock.h>
    #include <ti/sysbios/knl/Task.h>

    /* TI-RTOS Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/SDSPI.h>
    #include <ti/drivers/USBMSCHFatFs.h>
    #include <ti/sysbios/fatfs/ff.h>

    /* Example/Board Header files */
    #include "Board.h"

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    /* Buffer size used for the file copy process */
    #define MEM_SIZE 10240000
    #define CPY_BUFF_SIZE 2048

    /* String conversion macro */
    #define STR_(n) #n
    #define STR(n) STR_(n)

    /* Drive number used for FatFs */
    #define SD_DRIVE_NUM 1
    #define USB_DRIVE_NUM 0

    /*
    * Static task stack for the USBMSCHFatFs USB driver
    * This allows the application to reduce the required system Heap.
    */
    static uint8_t usbServiceTaskStack[2*CPY_BUFF_SIZE];

    const char inputfilesd[] = "fat:"STR(SD_DRIVE_NUM)":input.txt";
    const char outputfileusb[] = "fat:"STR(USB_DRIVE_NUM)":output.txt";

    const char textarray[] = \
    "***********************************************************************\n"
    "0 1 2 3 4 5 6 7\n"
    "01234567890123456789012345678901234567890123456789012345678901234567890\n"
    "This is some text to be inserted into the inputfile if there isn't \n"
    "already an existing file located on the SDCard. \n"
    "If an inputfile already exists, or if the file was already once \n"
    "generated, then the inputfile will NOT be modified. \n"
    "\n"
    "This output file is copied from the SDCard to the USB Thumb Drive \n"
    "End of Demo. Now, get back to work! \n"
    "***********************************************************************\n";

    unsigned char cpy_buff[CPY_BUFF_SIZE];

    WCHAR ff_convert (WCHAR wch, UINT dir)
    {
    if (wch < 0x80) {
    /* ASCII Char */
    return wch;
    }

    /* I don't support unicode it is too big! */
    return 0;
    }

    WCHAR ff_wtoupper (WCHAR wch)
    {
    if (wch < 0x80) {
    /* ASCII Char */
    if (wch >= 'a' && wch <= 'z') {
    wch &= ~0x20;
    }
    return wch;
    }

    /* I don't support unicode it is too big! */
    return 0;
    }

    /*
    * ======== taskFxn ========
    * Task to perform a file copy
    *
    * Task tries to open an existing file inputfile[]. If the file doesn't
    * exist, create one and write some known content into it.
    * The contents of the inputfile[] are then copied to an output file
    * outputfile[]. Once completed, the contents of the output file are
    * printed onto the system console (stdout).
    *
    * Task for this function is created statically. See the project's .cfg file.
    */
    Void taskFxn(UArg arg0, UArg arg1)
    {
    // SDSPI_Handle sdspiHandle;
    // SDSPI_Params sdspiParams;
    USBMSCHFatFs_Handle usbmschfatfsHandle;
    USBMSCHFatFs_Params usbmschfatfsParams;

    /* Variables for the CIO functions */
    FILE *src, *dst;

    /* Variables to keep track of the file copy progress */
    unsigned int bytesRead = 0;
    unsigned int bytesWritten = 0;
    unsigned int filesize;
    unsigned int totalBytesCopied = 0;

    /* Mount and register the USB Drive */
    USBMSCHFatFs_Params_init(&usbmschfatfsParams);
    usbmschfatfsParams.serviceTaskStackPtr = usbServiceTaskStack;
    usbmschfatfsParams.serviceTaskStackSize = sizeof(usbServiceTaskStack);
    usbmschfatfsHandle = USBMSCHFatFs_open(Board_USBMSCHFatFs0,
    USB_DRIVE_NUM,
    &usbmschfatfsParams);
    if (usbmschfatfsHandle == NULL) {
    System_abort("Error starting the USB Drive\n");
    }
    else {
    System_printf("Drive %u is mounted\n", USB_DRIVE_NUM);
    }

    /* Need to block until a USB Drive has been enumerated */
    if (!USBMSCHFatFs_waitForConnect(usbmschfatfsHandle, 10000)) {
    System_abort("No USB drive present, aborting...\n");
    }

    /* Create a new file object for the file copy */
    dst = fopen(outputfileusb, "w");
    if (!dst) {
    System_printf("Error opening \"%s\"\n", outputfileusb);
    System_abort("Aborting...\n");
    }
    else {
    System_printf("Starting file copy\n");
    }

    int ii=0;
    char buffer[CPY_BUFF_SIZE];
    memset(buffer, 'C', CPY_BUFF_SIZE);
    memset(cpy_buff, 'A', CPY_BUFF_SIZE);
    bytesRead=CPY_BUFF_SIZE;
    /* Copy the contents from the src to the dst */
    for (ii=0;ii<MEM_SIZE/CPY_BUFF_SIZE;ii++) { //22.40 //45.45 50.10 //51:25-55:50
    /* Read from source file */ //56:45-1:45
    // bytesRead = fread(cpy_buff, 1, CPY_BUFF_SIZE, src); //29:15-33:30 //36:10 40:15//37:30-38:20
    // if (bytesRead == 0) {
    // break; /* Error or EOF */ //6:10-10:20//18:25-22:35 //28:15-32:20
    // }

    // memset(cpy_buff, 'B', bytesRead);
    /* Write to dst file */
    bytesWritten = fwrite(buffer, 1, CPY_BUFF_SIZE, dst);
    if (bytesWritten < bytesRead) {
    System_printf("Disk Full\n");
    break; /* Error or Disk Full */
    }

    /* Update the total number of bytes copied */
    totalBytesCopied += bytesWritten;
    }

    fflush(dst);

    /* Get the filesize of the source file */
    // fseek(src, 0, SEEK_END);
    // filesize = ftell(src);
    // rewind(src);

    /* Close both inputfile[] and outputfile[] */
    // fclose(src);
    fclose(dst);

    System_printf("File \"%s\" (%u B) copied to \"%s\" (Wrote %u B)\n",
    inputfilesd, filesize, outputfileusb, totalBytesCopied);

    /* Now output the outputfile[] contents onto the console */
    dst = fopen(outputfileusb, "r");
    if (!dst) {
    System_printf("Error opening \"%s\"\n", outputfileusb);
    System_abort("Aborting...\n");
    }

    /* Print file contents */
    while (true) {
    /* Read from output file */
    bytesRead = fread(cpy_buff, 1, CPY_BUFF_SIZE, dst);
    if (bytesRead == 0) {
    break; /* Error or EOF */
    }
    /* Write output */
    System_printf("%s", cpy_buff);
    }

    /* Close the file */
    fclose(dst);

    /* Stopping the SDCard */
    // SDSPI_close(sdspiHandle);
    // System_printf("Drive %u unmounted\n", SD_DRIVE_NUM);

    /* Stopping the USB Drive */
    USBMSCHFatFs_close(usbmschfatfsHandle);
    System_printf("Drive %u unmounted\n", USB_DRIVE_NUM);

    BIOS_exit(0);
    }

    /*
    * ======== main ========
    */
    int main(void)
    {
    /* Call board init functions */
    Board_initGeneral();
    Board_initGPIO();
    // Board_initSDSPI();
    Board_initUSBMSCHFatFs();
    /* Turn on user LED */
    GPIO_write(Board_LED0, Board_LED_ON);

    System_printf("Starting the FatSD USB Copy example\n");

    /* Start BIOS */
    BIOS_start();

    return (0);
    }

  • Hi Scott,

    I have another question. I guess I am using LOW_SPEED of USB 1.x. Is there an easy method to change LOW_SPEED to FULL_SPEED. As I studied I think we cannot achieve USB2.0 without external PHY.

    Thank you.
  • Jerad,

    I think you are right.  And the 1.5MBps that you measured lines up with this. 

    I’m still waiting on some emails I’ve sent out to others to clarify the speed you should expect to see.  I will reply back when I can confirm this….

    Regards,
    Scott

  • Scott,

    Thank you for considering my question. I am looking for your soonest reply 
    Thank you.

    Regards,
    Jerad

  • Hi Scott,

    Any updates on my problem?

    Thank you.

  • Hi Jerad,

    Sorry, no, I’m still waiting to hear back.

    Regards,
    Scott

  • Hi Jerad,

    sorry for the long delay. I've tried the same example and am also seeing slow write speeds. The USB analyzer I have shows Full Speed operation, so there must be something else going on here..

    I've opened a bug (SDOCM00115670) to track this issue. I have yet to find the root cause of this problem, so I can't offer a workaround for this yet.

  • Hi Tom,

    I will track the bug.
    Thank you for your response.

    Regards,
    Jerad.