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.

Using USB in OTG mode in TM4C1294NCPDT

Other Parts Discussed in Thread: TM4C1294NCPDT, EK-TM4C1294XL

Hi,

I am working on tm4c1294ncpdt for my application. Now I am likely to extend my application such that to use USB_OTG in it. I have gone through the example programs in Tivaware. I can find several examples based on USB such as USB_host_keyboard, USB_host_mouse, USB_dev_bulk, USB_dev_serial, etc..

But in my application, I want to use it in an OTG mode and I couldn't find any example program in Tivaware to use USB in OTG mode. So anyone ,please share  your experience with me to use USB in OTG mode. It will be very helpful for me to complete my project.

Thanks in advance.

Regards,

Subash

  • Hello Subash,

    The folder "./examples/boards/dk-tm4c129x" has an example called "usb_otg_mouse". Did you look at it?

    Thanks,
    Sai
  • Hi Sai,

    Thank you for your response. I didn't check that program till now. Let me look into it. I am using ek-tm4c129xl board. May be I need to port some portion of code to match with my board. I will go through .

    Regards,

    Subash

  • I gone through the usb_otg_mouse program that is in the dk-tm4c129x folder. It seems even I cant port that program into my ek-tm4c1294xl board. Because that program is used for displaying something in the LCD display which is used in the dk-tm4c129 board according to the mouse movement.

    So, I feel it is not possible to use the same program in my board (ek-tm4c1294xl).

    My application is to transfer the data obtained from the sensors using tiva board to a USB pendrive (OTG supported) continuously. So can you please tell me, whether I can use any of the example programs that is given in the ek-tm4c1294xl board folder.


    Regards,

    Subash
  • Hello Subash,

    If the data has to be transferred to a pendrive then why do you need OTG? Host only mode would be sufficient.

    Regards
    Amit
  • Hi Amit,

    Thank you for clearing my doubt. Actually I was bit confused with the OTG concept. Now it is clear to me. I will start working with host mode.

    Regards,

    Subash

  • Hi Amit,

    I tried working on usb_host_msc program in ek-tm4c1294xl. It is working fine and it could detect my device. Now my doubt is whether we can send some data from microcontroller to the pendrive?

    Because here I could find some commands only for accessing the data in the pendrive. I couldn't find any commands for sending the data to pendrive.

    So can you please tell me how to transfer the data from the microcontroller to the pendrive.

    Thank you in advance

    Regards,

    Subash

  • Hello Subash,

    There is f_read being used to read the data. In the same manner you call f_write to write to a file. This is part of the Fat file system. More details on the fat file system are at the following link for the API's that are available to the user.

    elm-chan.org/.../00index_e.html

    Regards
    Amit
  • Hi Amit

    I will go through the link that you have given about FatFs file system. I think it will serve the purpose for my application.

    Thanks and regards,

    Subash

  • Hello Subash

    OK. Let us know if you need help. There are other FatFS examples that can be used to integrate the API

    Regards
    Amit
  • Hi Amit,

    I have gone through the link that you have specified. If you can able to provide me some more examples on FatFs file system, then it will be much more helpful to understand the concepts.

    Thanks and regards,

    Subash.

  • Hi Amit,

    The program usb_host_msc from the examples was executed successfully and when I am connected a pendrive to it, I can able to detect the pendrive. Also using the given commands, I can see what are all the files present in the pendrive. But I couldn't access the file. I mean I couldn't access the contents of the file from Tera Term which is the application I am using for serial communication.

    Can you please tell me how to access the contents of the file and also can you provide me some sample code for writing some contents into the file. Because my target in my project is to write the sensor data continuously into a file which is stored in the pendrive.

    Thanks and regards,

    Subash

  • Hello Subash

    Look at the sdcard example in DK-TM4C129x, which has routines for reading file and displaying it on the Serial Console

    Regards
    Amit
  • Hi Amit,

    Thanks a lot for your timely helps. Now I can able to successfully complete the task in my application. I can write the data continuously into a pendrive from my controller thereby data logging is done.

    But now one problem I am facing is that when I am writing some data into a file, it is completely replacing the existing content in the file and overwriting it. But my application needs continuous logging of data. So I need to write each data one after another each time.

    Can you please tell me what can I do for achieving this. Because the problem is I am using ff.c driver (Fat File system) for writing contents into the file. In that, for write function there are only 4 arguments it is taking and in all 4 arguments nothing is about a pointer for pointing to the exact place in the file (so that I can write to that place).

    So is there any alternative for writing the contents into a file without overwriting the existing contents.?

    Thanks and regards,

    Subash

  • Hello Subash,

    Did you open the file with the permission "FA_OPEN_APPEND"?

    Regards
    Amit
  • Hi Amit,

    Actually I am using ff.c driver for using file content. There I can see functions like f_open, f_close, f_read, f-write, etc. In that driver file, there is no function like open_ append.

    Can you please tell me which driver file you are using for file contents.

    Regards

    Subash

  • Hello Subash

    It is an argument to the f_open function

    Regards
    Amit
  • Hi Amit,

    I have gone through that function. And in the driver file ff.c, I can see that the arguments can be FA_OPEN_EXISTING, FA_OPEN_ALWAYS, etc.

    But I couldn't find any argument like FA_APPEND.

    Thanks and regards,

    Subash

  • Hello Subhash

    So it seems that there is a new version of the fatfs which we have not taken. You can try to use f_lseek to move the file pointer to end of the file to append new data. Also try it with the permissions FA_OPEN_EXISTING

    elm-chan.org/.../lseek.html

    Regards
    Amit
  • Hi Amit,

    I am opening a file using the argument FA_CREATE and the file is also getting created. But the size of the file is 0KB. So when I am trying to write something into that file, I couldn't write anything and the size of the file remains 0KB. Can we able to increase the size of the file using program?

    Also, when I am writing to a existing file, it is writing again and again the word which I am writing. But for me, it should write only once. But what happening is it is writing first, then leaving some space (filled with \00when I opened in my PC I can see this) and then again writing the same word again the same thing follows.

    Can you please tell me how to overcome this issue

    Thanks and regards

    Subash

  • int Cmd_write(int argc, char *argv[])
    {
    FRESULT fresult;
    uint32_t ui32BytesWrite;

    if(g_eState != STATE_DEVICE_READY)
    {
    return(FR_NOT_READY);
    }

    if(strlen(g_cCwd1Buf) + strlen(argv[1]) + 1 + 1 > sizeof(g_cTmp1Buf))
    {
    UARTprintf("Resulting path name is too long\n");
    return(0);
    }

    strcpy(g_cTmp1Buf, g_cCwd1Buf);

    if(strcmp("/", g_cCwd1Buf))
    {
    strcat(g_cTmp1Buf, "/");
    }

    strcat(g_cTmp1Buf, argv[1]);

    fresult = f_open(&g_sFileObject, g_cTmp1Buf, FA_WRITE);

    if(fresult != FR_OK)
    {
    return(fresult);
    }





    do
    {


    fresult = f_write(&g_sFileObject, g_cTmp1Buf, sizeof(g_cTmp1Buf) - 1,
    (UINT *)&ui32BytesWrite);

    if(fresult != FR_OK)
    {
    UARTprintf("\n");
    return(fresult);
    }


    }


    while(ui32BytesWrite==sizeof(g_cTmp1Buf) - 1);



    return(0);

    }


    This is the code I have written for writing into the file. g_cTmpBuf will contain the file name and again I am writing the same file name into the file. But the problem it is writing several times with inbetween several unknown characters.

    Please tell me where I am going wrong

    Thanks and regards
    Subash
  • Hello Subash

    Did you try using FA_OPEN_EXISTING?

    Regards
    Amit
  • Hi Amit,

    Ya I tried using FA_OPEN_EXISTING. but in this case, when I am trying to write something to the file, its is giving the error "Command returned with error code FR_DENIED".

    Then I tried opening the file using permission FA_OPEN_EXISTING | FA_WRITE. In this case, I can able to write into file, but the same problems what I have stated in the previous one was still existing.

    Thanks and regards

    Subash

  • Hello Subash,

    Look at the example for qs-logger in dk-tm4c123g examples. The file to look at is usbstick.c. it has the method to open an existing file for appending.

    Regards
    Amit
  • Hi Amit,

    Ya I looked at the example file you have stated and it is working. But the problem what I am facing now is when I am creating a file with no data in it, its size is shown as 4 bytes. And when I am trying to write 5 bytes of data into that file, I couldn't do it. Only 4 bytes of data are written into the file.

    Then I tried with creating a file and written more data into it from PC. After that if I am writing some 10 bytes of data into the file, all other spaces are filled with \00.

    I couldn't understand why these problems are occurring.

    Thanks and regards,

    Subash.

  • Hello Subash,

    This is a debug path that you will have to investigate. Having a known working example would help in debug as you can see what is the execution path and compare it with the failing software.

    Regards
    Amit
  • Hello Subash,

    I'am also trying to implement the same thing i.e. writing data into a pendrive. i was successfully able to read the contents of the pendrive on serial terminal using usb_host_msc.c file but no luck in writing data.
    I was going through your code and implemented the same in my code but unfortunately i was not able to write or create any file in the pen drive.

    could you help me out with my problem!
  • Hello Sumit,

    I can able to read and write contents into the file using the above code I have posted. First create a file (.txt) in the pen drive. Then from your program, call f_open function first with one of its argument containing that file name. (for eg: subash.txt). Then after calling f_open function, call f_write() function, with one of its argument containing what content you need to write in the file. (for eg: temp = 25.5).

    With this you could write contents into your file that you have created in your pen dive. Still if you face any problem after implementing this, please share your write function here. I will look into it.

    Regards,
    Subash.
  • Hello Subash,

    Thanks for your suggestion. Since i could not create & write data in the file using your function, I have edited the usb_host_msc.c  code on my own  as shown below. I wrote few lines of code in the while(1) loop

    while(1)

       {

        if(g_eState == STATE_DEVICE_READY)

                {

                   fresult = f_open(&g_sFileObject, "test42.csv", FA_WRITE|FA_OPEN_ALWAYS);

                        if(fresult != FR_OK)

                        {

                        //return(fresult);

                        }

                        char fileHeaders[] = "sensor1,sensor2,sensor3,sensor4\r\n";

                        fresult = f_write(&g_sFileObject, fileHeaders, sizeof(fileHeaders), &ui32BytesWrite);

                        char valueStr[150];

                        for(a=100;a<=249;a++)

                        {

                        usprintf(valueStr,"%d,%d,%d,%d\r\n",a,a,a,a);

                        fresult = f_write(&g_sFileObject, valueStr, sizeof(valueStr), (UINT *)&ui32BytesWrite);

                     

                        }

                        //fresult= f_close(&g_sFileObject);

                }

    with the Above code I'am able to create file and write data into the file, however some garbage data is also written in the file in the first row of  "sensor1" . attached is the excel file for the same

    One more thing I wanted to ask is where have you called your function int cmd_write() in your code??

     

    TEST42.rar

  • Hi Sumit,

    I saw your excel sheet. What I observed is you can able to write everything clearly. But that garbage data may be due to while creating a function, it is assigned with some limited memory. And then when you are trying to write some data into it, those memory is occupied with some garbage values and after that your values are getting filled. Try with f_lseek() function, that you can use to move the pointer wherever required so that place the pointer at the initial point and then start writing.

    Regarding your 2nd question, Inside the ReadLine() function, under the switch case State_device_ready, I am calling both read function and write function.
  • Hello Subash,

    I'am really thankfull for your suggestions. I'am implementing your suggestions now and hope to get the desired results!

    Thanks again.
  • Hello subash,

    I tried implementing the f_lseek(). still getiing garbage value.
    is there any example code for f_lseek(). I'am not able to understand what value to be given to move the pointer at initial point.
  • Hello Sumit,

    Please check the example code for SPMA076 (opus Audio Codec). It shows how to use f_lseek