Shop OBEX P1 Docs P2 Docs Learn Events
Issues writing to SD card to be read by a computer — Parallax Forums

Issues writing to SD card to be read by a computer

xander18xander18 Posts: 5
edited 2015-10-30 15:12 in Propeller 1
Hello all,

I'm really excited to be messing around with a Propellor. I have some experience with 8052 years ago and Arduino recently but I decided to step into something a bit more interesting. I'm working with Propellor C and SimpleIDE right now, trying to get my C up to speed. There's not as much info out there on Propellor C as SPIN and Arduino so I'm hoping this forum is at least somewhat receptive to stupid questions cause I'll definitely have one or two of them :/

I'm trying to just read and write a file to an SD card on my Activity Board. My code is somewhat based on John Kauffman's from this thread and is below.

I'm having two issues. The first is that the data is reading back in incorrectly, returning as 0. The second is that when I jump the SD card over to a computer it's writing in hex instead of numeric. I'm hoping to eventually use this for a DAQ system so I'll be trying to write a CSV. Any tips?
#include "simpletools.h" // Include simpletools header .
int DO = 22, CLK = 23, DI = 24, CS = 25; // SD card pins on Propeller BOE
// values for writes & values to receive reads
int myWriteInt = 11;
int myReadInt;

int main(void){

sd_mount(DO, CLK, DI, CS); // Mount SD card


FILE* fp = fopen("OneInt.txt", "w"); // Open a file for appending
print("\nmyWriteInt = %d",myWriteInt); // Tell user value to write
fwrite(&myWriteInt, 4, 1, fp); // Write number to SD card
fclose(fp);

fp = fopen("OneInt.txt", "r");
myReadInt = fread(&myReadInt, 4, 1, fp); // Read from SD & get status
print(", myReadInt = %d\n",myReadInt); // Tell user value read
fclose(fp); // Close the file

} // main

Comments

  • It sounds like you're looking for fprintf, not fwrite. And then fscanf instead of fread.
  • Also, I'm glad to hear PropGCC intrigues you! We welcome all questions here, simple and complex, so please ask away :)
  • David,

    I'll look into those commands, sounds promising.

    Yeah this Propellor stuff looks really cool! I think that the multicore aspect will make me think a lot more critically about how I structure programs and tasks. I read through the 'SimpleIDE is dead' thread and I see a lot of the points and concerns in there about it. But as a relative beginner it was awesome to be able to jump right into Propellor with very familiar software and coding. So good job to everyone who worked on that project and I'm looking forward to seeing the Propellor C community grow.
  • I got it working with fprintf and fscanf. It does what I need it to but the program got huge, too large to run both my read and write sections of the code at once (tested them one at a time though). Commenting out fprintf and fscanf it's 34 kilobytes, with those lines it's 68. I was running the default memory model (CMM Main RAM Compact) and it's too large for it.

    It builds on the 'XMMC External Flash Code Main RAM Data' model fine but damn, did I really max out the main memory that fast? Seems like a real small program. Am I totally missing some kind of optimization or memory management?
  • Ha! yea... those are huge functions but i didn't realize they were that big. The Simple library comes with shortened versions of printf and scanf called print (and printi) and scan (and scani) but I don't know if there are file equivalents. Hopefully someone else will jump in and answer that question.

    If there aren't file equivalents, you have a few options.
    1) Do it in a two step process: Convert to a string with sprinti and then write with fwrite (and reverse for reading, fread and sscani). Or, similar, write your own int-to-string and string-to-int methods.
    2) Use the SD card class in PropWare
    3) Use the SD card class in libpropeller

    Unfortunately, options #2 and #3 don't have "tutorials" in the same way as Parallax's Learn website, so they're not necessarily quite as easy. But both have examples you can use to learn from if you choose to go that route.
  • David,

    Thanks a ton for all the help. Monday morning, back at my desk playing around with this thing. I'll start playing around with option 1, at least for education's sake.

    I suspect I'll keep running up against performance issues like this as long as I'm trying to keep it simple. I need to step up my game here and move in the direction of options 2 and 3.

    Again, thanks for all the pointers.
  • DavidZemonDavidZemon Posts: 2,973
    edited 2015-11-02 14:27
    xander18 wrote: »
    I'll start playing around with option 1, at least for education's sake.

    Definitely a good idea. It's always good to learn how the standard library works :)
  • The file I/O and fprintf/fscanf libraries are large. Using both of them together will fill up the 32K hub RAM pretty fast. It is possible possible to do this on the P1, but you will need to use a trimmed down version of fprintf and fscanf to do it. Here's some code for a simple printf/fprintf that you might find useful. You will have to write your own fscanf if you want to save memory.
    c
    c
  • jazzedjazzed Posts: 11,803

    Dave's approach can yield smaller code because it focuses on the problem at hand.

    The code below shows the general solution built-in using sprint(buffer, format, ...) which is comparable to print(format, ...) (about 17KB of code with CMM).
    #include "simpletools.h" // Include simpletools header .
    int DO = 22, CLK = 23, DI = 24, CS = 25; // SD card pins on Propeller BOE
    // values for writes & values to receive reads
    int myWriteInt = 11;
    int myReadInt;
    char buffer[256];
    
    int main(void)
    {
      int size;
      FILE* fp;
    
      sd_mount(DO, CLK, DI, CS); // Mount SD card
    
      fp = fopen("OneInt.txt", "w"); // Open a file for appending
    
      sprint(buffer,"\nmyWriteInt = %d",myWriteInt); // Tell user value to write
      size =strlen(buffer);
      fwrite(buffer, size, 1, fp); // Write number to SD card
      fclose(fp);
    
      fp = fopen("OneInt.txt", "r");
      myReadInt = fread(&myReadInt, 4, 1, fp); // Read from SD & get status
      print(", myReadInt = %d\n",myReadInt); // Tell user value read
      fclose(fp); // Close the file
    
      return 0;
    } // main
    
  • I wasn't aware that the simple tools library had an sprint function. That would be a better way to go since it's already supported by Parallax.
  • xander18xander18 Posts: 5
    edited 2015-11-09 21:27
    Jazzed and Dave, thanks for the tips. Unfortunately other projects are coming up at work so I haven't had as much time to play around with it recently.

    Also jazzed, thanks for SimpleIDE :)

    Edit:

    Dave, taking a close look at your code now. This is interesting, I'm just gonna start googling until I'm caught up with it line by line.

    jazzed, clearly I need to learn my standard library considerably better.

    Thanks again!
Sign In or Register to comment.