Writing to SD after removal/reinsert
geo_leeman
Posts: 190
in Propeller 1
I'm switching a data logger over to C++ (yay!) from spin, but am hitting an odd behavior in the SD card writing bit. I've got a logging switch (on/off) so the user could turn it off, remove the card, insert a new one (or the same really), and turn it back on. I can turn it off/on to my heart's content, but as soon as I remove the card, it's a no go with no further writes occurring. I've simplified it to the program below to demonstrate the issue. I let this run a few loops, then remove the card during the period after the file is closed (and my safe to remove LED is on). Upon reinsertion, no more writes actually happen. Any ideas? Thanks!
#include <stdio.h> #include <propeller.h> #include "simpletools.h" int main(void) { while(1) { sd_mount(19, 20, 21, 22); FILE* fp = fopen("test.txt", "a"); fwrite("Testing 123...\n", 1, 15, fp); fclose(fp); high(7); // LED for safe to remove ON print("2 second pause\n"); pause(2000); low(7); // LED for safe to remove OFF } }
Comments
Not sure it will help any, but do check the return code of sd_mount and see if its throwing any errors.
If this ends up being a feature that isn't supported by Simple + PropGCC, you have a few other alternatives: convert a Spin object to C++ with spin2cpp, use PropWare's SD/FATFS library, use libpropeller's SD/FATFS library
Ah - I'll have a look at making some kind on unmount then and update with the results.
Ray
Ouch. That's too bad
Yes. In fact, PropWare doesn't even need to run anything in a separate cog. If you just want an exact copy of your test program above but all running in a single cog, then try this: (warning: untested)
But, if you'd like async writes, you'd want something like this (warning: untested)
Code size for these two examples is as follows:
Single cog: Code size = 13,952, Total size = 15,328
Multi cog: Code size = Code size = 15,224, Total size = 16,600
The unfortunate thing about this is that PropWare isn't very Windows-friendly. If you're on Windows, I'd recommend libpropeller's SD class. I checked - it has an unmount method and the docs even make explicit mention of freeing up the driver cog.
Could be global, could be static to the function. Probably gets compiled to the same thing?
@geo_leeman, if you prefer to stick with the Simple + PropGCC approach, you probably want to just override the function. If you define your own version of dfs_mount with exactly the same signature, the linker will (should?) select your function instead of the one provided by whatever lib PropGCC has it in. Then of course you can define a dfs_unmount as appropriate and invoke it.