Shop OBEX P1 Docs P2 Docs Learn Events
SD Card access on different COG's — Parallax Forums

SD Card access on different COG's

trancefreaktrancefreak Posts: 186
edited 2013-09-10 03:46 in Propeller 1
Hi,

I've a question with regards to accessing files on a SD Card with simpletools.h.
Is it possible, that two cogs are accessing two separate files at the same time using the sd driver of simpletools?

Or do I have to use two different SD Cards to achieve that?

To be more precise:
I need one cog to access file A on the SD card while another cog accesses file B on the same SD card.
Is that possible?

Thx,
Christian

Comments

  • jazzedjazzed Posts: 11,803
    edited 2013-08-29 06:05
    The propeller-gcc file-system code uses one cog and can open more than one file at a time.
  • trancefreaktrancefreak Posts: 186
    edited 2013-08-29 06:38
    Hi Jazzed,

    Thanks very much for your reply and clarification!!


    Thx,
    Christian
  • David BetzDavid Betz Posts: 14,516
    edited 2013-08-29 07:33
    jazzed wrote: »
    The propeller-gcc file-system code uses one cog and can open more than one file at a time.
    I thought that the question was whether two COGs could access the filesystem at the same time. I don't think that the standard PropGCC filesystem support has sufficient locking to allow simultaneous access by multiple COGs. Is the Simple Library filesystem support different?
  • trancefreaktrancefreak Posts: 186
    edited 2013-08-29 07:49
    Hi David,

    Yes, that was what I wanted to know.
    I'm planning an implementation where two cogs are reading from one sd card. I need to play wav files. One file is background music and one is fx (speech, fx, ...).
    So my idea was to have two cogs which read the audio content from sd card. So is this possible or do I have to use two sd cards to achieve this?

    Regards,
    Christian
  • David BetzDavid Betz Posts: 14,516
    edited 2013-08-29 07:58
    Hi David,

    Yes, that was what I wanted to know.
    I'm planning an implementation where two cogs are reading from one sd card. I need to play wav files. One file is background music and one is fx (speech, fx, ...).
    So my idea was to have two cogs which read the audio content from sd card. So is this possible or do I have to use two sd cards to achieve this?

    Regards,
    Christian
    Unfortunately, there are a number of problems you could run into with this. First, as I said in my other message, I don't think the PropGCC filesystem will allow access from multiple COGs. However, if you're only reading the files then you might be able to access raw sectors on the SD card by just using a lock to synchronize access from multipel COGs. Another possible solution would be to have a single COG handle all file I/O but have it "serve" files to two other COGs that are handling your background music and fx. Then only the server COG will be accessing the filesystem.
  • jazzedjazzed Posts: 11,803
    edited 2013-08-29 08:12
    David Betz wrote: »
    I thought that the question was whether two COGs could access the filesystem at the same time. I don't think that the standard PropGCC filesystem support has sufficient locking to allow simultaneous access by multiple COGs. Is the Simple Library filesystem support different?

    I gave the most appropriate answer I could think of.
  • Jeff MartinJeff Martin Posts: 758
    edited 2013-09-05 13:49
    Using multiple cogs to access the same resource (an SD card in this case) would cause contention problems, like David noted. However, also as noted by Steve and David, you can open up more than one file at a time with one cog, and we recommend doing it this way and serving the data out to two different cogs (if need be).

    Here's an answer from Andy whose thoughts I asked for outside of this thread:

    ...multiple files work. Starting with examples in SimpleIDE\Learn\Examples\Devices\Memory, only use one sd_mount call, and then multiple FILE *pointer = fopen statements with different pointer names. Then, use fwrite and fread and just make sure to use the right pointer. (Avoid fprintf and fscanf, they are memory hogs!)

    Here's an example with a not recommended lack of tests. (The same can be done to the "with Tests" example, which tests to make sure the file successfully opened and all that.)
    /*
      SD Minimal (Modified to handle two open files).side
      Create test.txt, write characters in, read back out, display. 
      
      [url]http://learn.parallax.com/propeller-c-simple-devices/sd-card-data[/url]
    */
    #include "simpletools.h"                      // Include simpletools header   
    int DO = 22, CLK = 23, DI = 24, CS = 25;      // SD card pins on Propeller BOE
    int main(void)                                // main function
    {
      sd_mount(DO, CLK, DI, CS);                  // Mount SD card
      FILE* fp = fopen("test.txt", "w");          // Open a file for writing
      FILE* fp2 = fopen("test2.txt", "w");        //  <-- Add
      fwrite("Testing 321...\n", 1, 15, fp);      // Add contents to the file
      fwrite("Testing 123...\n", 1, 15, fp2);      // Add contents to the file
      fclose(fp);                                 // Close the file
      fclose(fp2);                                 // Close the file
      
      char s[15];                                 // Buffer for characters
      char t[15];                                 // Buffer for characters
      fp = fopen("test.txt", "r");                // Reopen file for reading
      fp2 = fopen("test2.txt", "r");               // Reopen file for reading
      fread(s, 1, 15, fp);                        // Read 15 characters
      fread(t, 1, 15, fp2);                       // Read 15 characters
      fclose(fp);                                 // Close the file
      fclose(fp2);                                // Close the file
      print("First 15 chars in test.txt:\n");     // Display heading
      print("%s", s);                             // Display characters
      print("\n");                                // With a newline at the end
      print("First 15 chars in test2.txt:\n");    // Display heading
      print("%s", t);                             // Display characters
      print("\n");                                // With a newline at the end
    }
    
  • Dave HeinDave Hein Posts: 6,347
    edited 2013-09-05 14:58
    You should be able to use a lock to prevent the cogs from accessing the file system at the same time. However, it might be easier to use a single cog to act as a data server, as David Betz suggested. You probably need to do something like this anyhow to ensure that your buffers don't run empty when playing out a wave file.
  • trancefreaktrancefreak Posts: 186
    edited 2013-09-10 03:46
    Hi Jeff and Dave,

    Thanks for your answers. Yes, I will do the sd access with one cog and the others will just get the file data from the "sd cog".
    Question is, how the mechanism could look like to tell the "sd cog" to deliver some data. The cogs can only communicate through a mailbox. Is someone used to such a design, how to tell
    another cog to deliver data from a file into buffer?

    Maybe the mailbox itself has two buffers, which are filled by the "sd cog" and the buffers are filled by using flags to tell the "sd cog" to do it and another flag which is set to true, if the buffer is
    filled.

    Or is there a cleaner, nicer way to do this?

    Christian
Sign In or Register to comment.