Shop OBEX P1 Docs P2 Docs Learn Events
FSRW FAT16 routines used by two cogs — Parallax Forums

FSRW FAT16 routines used by two cogs

edlikestoboogieedlikestoboogie Posts: 71
edited 2010-06-09 15:52 in Propeller 1
I don't know if this question has been asked or not, but my application requires two seperate cogs to open up and write/read to files on an SD CARD using fsrw1.6 . I don't want both cogs to try to open a file at once, so I made a sort of "filesystem" to facilitate requests to open files and send a stream to write. I now feel like this "filesystem" is unnecessary. Does anyone know if its safe to use FSRW on two seperate cogs? if FSRW has a file open will it abort with an error code when another cog tries to open a new file?

Thanks in advance for an answer to this quick question.

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2010-06-08 21:30
    You can't run 2 FSRWs using the same SD card. It would need some kind of a protocol between the COGs to takeover the IO-pins.
    What kind of files do you read/write? Do they have to be readable on a PC?

    I added some code that I called vMem to the FSRW which allows to read/write at random positions in up to 4 files. And you can open/read/write/close another file with the regular FSRW functions. But it requires the files to be continuous (not fragmented) and already filled with the maximum number of bytes that you want to store.

    Have a look over here:
    http://forums.parallax.com/forums/default.aspx?f=25&m=401244&g=401357#m401357
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-06-08 21:59
    You should be able to use a lock to make FSRW cog-safe.· The lock could be allocated in the mount routine, and you would need to set and clear the lock in each FSRW routine that you call.· Watch out for the aborts that FSRW uses.· This might prevent you from clearing a lock if you aren't careful.

    I have a modified version of FSRW 2.6 that uses a file handle to support multiple open files instead of using VAR to provide multiple instances.· All of the routines call a function to swap the 10 state variables with the handle variables.· I could easily add a lock to this routine to make it cog-safe if your interested.

    Dave
  • KyeKye Posts: 2,200
    edited 2010-06-08 22:31
    @http://forums.parallax.com/forums/default.aspx?f=25&m=458916

    Just keep including it for as many files as you need.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • edlikestoboogieedlikestoboogie Posts: 71
    edited 2010-06-08 23:39
    Kye, I'm not sure but I think your implementation is a bit heavy. What I like about fsrw 1.6 is that its only 1,000 longs. Does anyone have a lightweight thread safe FSRW object? If not I guess I will have to make 1.6 threadsafe myself.
  • edlikestoboogieedlikestoboogie Posts: 71
    edited 2010-06-08 23:46
    Actually to Kye's credit, his SD2.0 is only 1900 longs, not terribly gigantic. I will try to squish it into my project, but if anyone has written something lightweight from fsrw 1.6, please let me know.
  • edlikestoboogieedlikestoboogie Posts: 71
    edited 2010-06-09 03:19
    Hey Kye,

    So I am reading a bit of your code. If a cog opens a file, and another cog attempts to open another file through that one instance of SD2.0, will an abort code be returned?
  • KyeKye Posts: 2,200
    edited 2010-06-09 03:42
    Actually, that is a big no no and if you read the disclaimer at the top of the file I warn about that. The file system will just break if you do that.

    You need to include the object again. Only the VAR image will be coppied for the second and third inclusion however.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • edlikestoboogieedlikestoboogie Posts: 71
    edited 2010-06-09 04:23
    So to understand, your driver isn't thread safe, and I should make a "wrapper" object to call your methods and keep track of a flag to make sure two files aren't opened at the same time.? If I have two instances of your object in two seperate cogs, how should i write file accesses to prevent your driver from breaking?

    Post Edited (edlikestoboogie) : 6/9/2010 5:00:38 AM GMT
  • KyeKye Posts: 2,200
    edited 2010-06-09 15:52
    What I mean is...

    OBJ

    fs1: "SD2.0_FATEngine.spin"
    fs2: "SD2.0_FATEngine.spin"

    ...

    And you should only call the initializer once (The FATEngineStart function). If you then give each "fs" handle to the different cogs each one will have an exclusive file system for themselves. As in each cog will have to mount the file system and traverse directories and such to get to the file they want.

    No other work is required. Both cogs then share the readonly file sytem code and the block driver. This also means that preformance will go down slightly.

    ... Now, I really haven't tested this stuff throughly so do so at your own risk. I will warn you that if two processors are writing to the same block or such of the file system data can become corrupted. So avoid having both processors writing at the same time... Or bad stuff may happen. For reading there will be no problems.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
Sign In or Register to comment.