7 Replies Latest reply on May 1, 2020 12:26 PM by WaMa_286156

    Emfile file read Error

    AbPa_4654881

      Hi all,

       

      I am new to cypress community,I am currently working with CY8Ckit-059 psoc kit. I want to read data from the SD card that contaion "new.txt" file.and according to receive data I want to drive some signal(like LED or any thing through GPIO). I am using somthing like Below code. But I am not able to get any data into buffer. so the lile if (n != 0) is not getting executed... even after I am getting pFile as 1 because this loop  for( i = 0; i < 5; i++ )  is getting executed I can see that.

      can anayboday please help regarding this?

       

       

      #include "project.h"

      #include "string.h"

      #include "stdlib.h"

      #include <FS.h>

      #include <string.h>

      #include <Global.h>

      int main()

      {

      char abBuffer[1];

      FS_FILE * pFile;

      FS_Init();

      //FS_Mount(0);

      pFile = FS_FOpen("new.txt", "r");

      int i = 0;

      int n=0;

      CyGlobalIntEnable;

      if (pFile)

      {

      for( i = 0; i < 5; i++ )

      {

            LED_Write(0u);

            CyDelay(1000);

            LED_Write(1u);

            CyDelay(1000);

      }

      while(NULL !=  pFile && FS_FEof(dataFile)!=1 )

      {

      n=FS_Read(pFile, abBuffer, 1);

      if (n != 0)

          LED_Write(0u);

            CyDelay(5000);

            LED_Write(1u);

            CyDelay(5000);

      }

      if (abBuffer[0] == 1)

      {

          LED_Write(0);

      }

      else if (abBuffer[0] == 0)

      {

         LED_Write(0);

      }

      FS_FClose(pFile);

      }

      }

        • 1. Re: Emfile file read Error
          LePo_1062026

          AbPa,

           

          I found some issues with the code you supplied.  I've modified it.  You will find the modifications by looking for the comment that starts with:

          // mod'd len: or // len:

          The code below finds new.txt then blinks LED 5 times slowly.

          It then reads new.txt one character at a time.  For every character it reads it blinks LED fast (my modification to speed thinks up) until it reaches the end of the file and then stops.

           

          Issues I found in your code.  One fatal the others trivial.

          • Line 32: You referenced FS_FEof(dataFile) and dataFile did not exist.  Changed it to pFile.
          • Line 36: After if (n != 0) you didn't have '{' or '}' to surround the LED blinking.  I added them.
          • Lines 45 and 49 do nothing new.  They do the same thing (turn off the LED).
          • Line 32: You test pFile != 0.  However, Line 22 does the same thing.

           

          #include "project.h"
          #include "string.h"
          #include "stdlib.h"
          #include <FS.h>
          #include <string.h>
          #include <Global.h>
          
          int main()
          {
          char abBuffer[1];
          FS_FILE * pFile;
              FS_Init();
          
              //FS_Mount(0);
          
              pFile = FS_FOpen("new.txt", "r");
          
          int i = 0;
          int n=0;
              CyGlobalIntEnable;
          
              if (pFile)
              {
                  for( i = 0; i < 5; i++ )
                  {
                    LED_Write(0u);
                    CyDelay(1000);
                    LED_Write(1u);
                    CyDelay(1000);
                  }
          
                  while(NULL !=  pFile && FS_FEof(pFile)!=1 )    // mod'd len: pFile was dataFile
                  {
                      n=FS_Read(pFile, abBuffer, 1);
          
                      if (n != 0)
                      {    // mod'd len: missing {
                          LED_Write(0u);
                            CyDelay(50);    // mod'd len: shorten time to 50ms
                            LED_Write(1u);
                           CyDelay(50);    // mod'd len: shorten time to 50ms
                      }    // mod'd len: missing }
                  }
          
                  if (abBuffer[0] == 1)    // len: This is identical to below
                  {
                      LED_Write(0);
                  }
                  else if (abBuffer[0] == 0)    // len: This is identical to above
                  {
                      LED_Write(0);
                  }
          
                  FS_FClose(pFile);
              }
          }
          

           

          Give these changes a try.

           

          Len

          • 2. Re: Emfile file read Error
            LePo_1062026

            AbPa,

             

            I've modified your original code to use a UART terminal dump if you're interested.

            To use it add a UART to your TopDesign and name it "UART" and configure it for 115200 bps (8N1).

            #include "project.h"
            #include "string.h"
            #include "stdlib.h"
            #include <FS.h>
            #include <string.h>
            #include <Global.h>
            
            int main()
            {
            uint8 abBuffer[100];
            FS_FILE * pFile;
                FS_Init();
                UART_Start();
            
            //    volatile int abx = FS_Mount("Dick_Head");
                UART_PutString("Opening new.txt ...\r\n");
                
                pFile = FS_FOpen("new.txt", "r");
            
            int i = 0;
            int n=0;
                CyGlobalIntEnable;
            
                if (pFile)
                {
                    for( i = 0; i < 5; i++ )
                    {
                      LED_Write(0u);
                      CyDelay(1000);
                      LED_Write(1u);
                      CyDelay(1000);
                    }
            
                    while(FS_FEof(pFile)!=1 )    // mod'd len: pFile was dataFile
                    {
                        n=FS_Read(pFile, abBuffer, sizeof(abBuffer));
            
                        if (n > 0)
                        {
                            UART_PutArray(abBuffer, n);
                        }
                    }
            
                    LED_Write(0);
            
                    FS_FClose(pFile);
                }
            }
            

            Len

            • 3. Re: Emfile file read Error
              AbPa_4654881

              Thank You so much LePo_1062026. actually the original code that I was i trying didn't Have the issue you mentioned. that happned because of I edited the original code here to remove things other than Emfile issue. main problem is that below I am getting pFile as 1 so it is going inside the 

              if (pFile){ something..............};

              But when I am trying to read By this --->   n=FS_Read(pFile, abBuffer, 1);

              Then I am getting always n=0; and "new.txt" contain (1010101000010111.....) somethinng like this.

              I need to drive different signal according to values from "new.txt" file. and I am defining   char abBuffer[1];

              I am not getting What is the issue as I am able to open the file in readmode but even after not able to read single byte ?

              • 4. Re: Emfile file read Error
                LePo_1062026

                AbPa,

                 

                Can you send me your new.txt file?

                 

                I understand better what you are trying to do.

                In effect, you're trying to blink the LED based on the presence of a "1" or a "0".  Sort of a Morse code for the LED.

                 

                I created my new.txt and placed text in it.  I had no problem reading it.

                 

                Len

                • 5. Re: Emfile file read Error
                  AbPa_4654881

                  LePo,

                   

                  it is normal txt file only. I have attached here. now I will try using UART to see effect on terminal

                   

                  Thanks

                  • 6. Re: Emfile file read Error
                    LePo_1062026

                    AbPa,

                     

                    After the long blinks, the file "new.txt" is processed for '1' and '0' characters.  If a '1' is detected, the LED is ON for 50ms.  If a '0' is detected, the LED is OFF for 50ms.

                    All other characters are ignored.  This will continue until the end of the file is reached.

                    Try this code for your main.c
                    #include "project.h"
                    #include "string.h"
                    #include "stdlib.h"
                    #include <FS.h>
                    #include <string.h>
                    #include <Global.h>
                    
                    int main()
                    {
                    char abBuffer[1];
                    FS_FILE * pFile;
                        FS_Init();
                    
                        pFile = FS_FOpen("new.txt", "r");
                    
                    int i = 0;
                    int n=0;
                        CyGlobalIntEnable;
                    
                        if (pFile)
                        {
                            /* long blinks phase */
                            for( i = 0; i < 5; i++ )
                            {
                              LED_Write(0u);
                              CyDelay(1000);
                              LED_Write(1u);
                              CyDelay(1000);
                            }
                    
                            while(FS_FEof(pFile)!=1 )    // mod'd len: pFile was dataFile
                            {
                                n=FS_Read(pFile, abBuffer, 1);
                    
                                switch(abBuffer[0])
                                {
                                case '1':
                                    LED_Write(1);
                                    CyDelay(50);    // mod'd len: shorten time to 50ms
                                    break;
                                case '0':    
                                    LED_Write(0);
                                    CyDelay(50);    // mod'd len: shorten time to 50ms
                                    break;            
                                default:
                                    break;    // throw away character.
                                }
                            }
                            LED_Write(0);
                            FS_FClose(pFile);
                        }
                    }
                    

                     

                    In your original code you were comparing the input abBuffer[0] to 0 or 1 which is 0x00 or 0x01 respectively.  Your file new.txt has '1' and '0' characters which is 0x31 and 0x30 respectively.

                     

                    I assume this is more of what you were trying to do.

                     

                    If you are still getting n=0 add the following code after FS_Read():

                    int Error = FS_FError(pFile);
                    if (Error)
                    {
                         sprintf(acLog, "Could not read from file:\nReason = %s",Error);
                         UART_PutString(acLog));     // This will dump to the UART the error number.
                         UART_PutString(FS_ErrorNo2Text(Error));// This will dump to the terminal the error text.
                    }
                    

                    Len

                    • 7. Re: Emfile file read Error
                      WaMa_286156

                      please take a look at PSOC5 and SDCARD, warning

                       

                      I had strange issues with emfile also, it interrupts into the ARM vector table for errors. You have to manually patch that table and create your own error handler.

                       

                      For that reason, I used em-chan.org , and was able to finish my project.  It will handle about the same range of cards, and you have all the source code, and it is free.  Go to the "software" link, and select the FatFS link.