Shop OBEX P1 Docs P2 Docs Learn Events
Is there a way to access a "dat" section stored on a sd card? — Parallax Forums

Is there a way to access a "dat" section stored on a sd card?

Don MDon M Posts: 1,652
edited 2014-03-01 13:08 in Propeller 1
Was wondering if I stored this on an sd card as am1.txt
dat

am1     byte  $08, $02, "Data 1...........           "        ' 30 bytes total - 2 numbers & 28 characters
        byte  $08, $07, "Data 2...........           "
        byte  $08, $12, "Data 3...........           "
        byte  $08, $16, "Data 4...........           "
        byte  $08, $17, "Data 5...........           ", 0


Can it be included somehow during compile?

Should it have a different file name?

Are there any code examples doing this?

Thanks.
Don

Comments

  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-02-28 11:41
    You can use raw data from a file, but it has to be on local drive where the compiler sees it. See the FILE data type for DAT blocks.

    If you want to read a block during run-time, what you could do, assuming your structure is absolutely intact (size), is a block read from a file to the DAT section of your program -- you can do it in one line:
    check := \sd.pread(@DatBlock, DAT_BLOCK_BYTES)
    


    If check = DAT_BLOCK_BYTES then you had a successful read. Do this at the top of your program before other elements want to access values in the DAT table.

    You should probably verify that the file is present and the correct size before running that line.
  • Dave HeinDave Hein Posts: 6,347
    edited 2014-02-28 12:06
    You could convert the file to raw binary data using BTSC. You would have to add a dummy PUB statement and rename the file with a ".spin" extension. You would then type "bstc -c am1.spin" to generate am1.dat. You would then do what Jon suggested, and use the FILE directive to include am1.dat in a DAT section in your program.

    Another way is to add a method that returns the address of am1, and include the file as an object. So in this case am1.spin would look like this.
    pub getaddress
      return @am1
    
    dat
    
    am1     byte  $08, $02, "Data 1...........           "        ' 30 bytes total - 2 numbers & 28 characters
            byte  $08, $07, "Data 2...........           "
            byte  $08, $12, "Data 3...........           "
            byte  $08, $16, "Data 4...........           "
            byte  $08, $17, "Data 5...........           ", 0
    
  • Don MDon M Posts: 1,652
    edited 2014-02-28 12:31
    JonnyMac wrote: »
    You can use raw data from a file, but it has to be on local drive where the compiler sees it. See the FILE data type for DAT blocks.

    If you want to read a block during run-time, what you could do, assuming your structure is absolutely intact (size), is a block read from a file to the DAT section of your program -- you can do it in one line:
    check := \sd.pread(@DatBlock, DAT_BLOCK_BYTES)
    


    If check = DAT_BLOCK_BYTES then you had a successful read. Do this at the top of your program before other elements want to access values in the DAT table.

    You should probably verify that the file is present and the correct size before running that line.

    Actually I like this method probably the best. I allows me to have different dat files on the sd card and be able to call them up into the program at will. The only gotcha is knowing ahead of time the number of bytes in the file... I wonder if the filesize method in fsrw would give me that number... I'll have to experiment a bit.

    Thanks for the suggestions.
  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-02-28 12:37
    I wonder if the filesize method in fsrw would give me that number

    Yes. I use this feature in the AP-16+. I can provide firmware updates to customers. On boot-up, the program looks for a special file name. If it exists and is exactly 32K (an eeprom image file), then it gets read and copied to the boot eeprom, the file is erased, and the Propeller reboots itself running the new firmware.
  • Don MDon M Posts: 1,652
    edited 2014-02-28 14:52
    So i'm trying to make this work.

    Here's what is on the sd card in a file named data.txt
    
    byte  $08, $02, "Data 1...........           "byte  $08, $07, "Data 2...........           "byte  $08, $12, "Data 3...........           "byte  $08, $16, "Data 4...........           "byte  $08, $17, "Data 5...........           ", 0
    
    


    And here is my DAT section
    DAT
    
      DatBlock byte $00, $00, "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    
    

    And I do this to retrieve the data from the sd card
      sd_mount                               ' Mounts card
      open_read                              ' Opens file data.txt
      file_size                              ' Gets file size and stores in variable named size
    
      sd.pread(@DatBlock, size)              ' Reads data from sd file and store "size" number of bytes into DAT DatBlock
    
      sd_unmount                             ' Un mount sd card
    
    

    When I look at the data from the DAT section it is messed up.

    How do I get it to format correctly in the DAT section?

    Do I have the data stored wrong on the sd card?
  • msrobotsmsrobots Posts: 3,709
    edited 2014-02-28 15:12
    You need to create a binary file of your data.

    so Data.txt will not work. After compile the DAT section is no source anymore just binary bytes. see #3 by Dave Hein.

    Enjoy!

    Mike
  • Don MDon M Posts: 1,652
    edited 2014-02-28 15:17
    msrobots wrote: »
    You need to create a binary file of your data.

    so Data.txt will not work. After compile the DAT section is no source anymore just binary bytes. see #3 by Dave Hein.

    Enjoy!

    Mike

    Just so I understand here... I'm not trying to compile the data from the sd card along with my other spin file but instead be able to access the file from within the spin program to "populate" the DAT section DatBlock if that's possible.

    Does this make sense? Is this what you are referring to doing in Dave's post?
  • Don MDon M Posts: 1,652
    edited 2014-02-28 15:23
    I think I understand the other option which was to make the DAT section on sd card into a binary and in my spin program in the DAT section I would use the FILE directive to access the sd file during compile correct?
  • Dave HeinDave Hein Posts: 6,347
    edited 2014-02-28 15:34
    Yes, the option I described was if you want to compile the data into your program.
  • msrobotsmsrobots Posts: 3,709
    edited 2014-03-01 13:08
    You can also compile your Dat section as standalone binary
    pub getaddress
      return @am1
    
    dat
    
    am1     byte  $08, $02, "Data 1...........           "        ' 30 bytes total - 2 numbers & 28 characters
            byte  $08, $07, "Data 2...........           "
            byte  $08, $12, "Data 3...........           "
            byte  $08, $16, "Data 4...........           "
            byte  $08, $17, "Data 5...........           ", 0
    

    if you compile this you get a binary file of 180 bytes. save to disk (sd) as AM1.bin

    to load your bin file into the existing space in your main prog you need to skip the first 24 bytes (init and first pub) to find your dat block.
      sd_mount                               ' Mounts card
      open_read                              ' Opens file data.txt
      file_size                                  ' Gets file size and stores in variable named size
    
      sd.pread(@DatBlock, 24)              ' Reads data from sd file and store 24 of bytes into DAT DatBlock
                                                            '  - this reads init and first pub to skip them in the file. now filepointer at $08,$02, Data1
    
      sd.pread(@DatBlock, 30)             ' Reads data from sd file and store 30 bytes into DAT DatBlock
                                                           ' this is now $08,$02,"Data1+23 bytes"
    
      sd.pread(@DatBlock, 30)             ' Reads data from sd file and store 30 bytes into DAT DatBlock
                                                           ' this is now $08,$07,"Data2+23 bytes"
    ...
    
      sd_unmount                             ' Un mount sd card
    

    Hope this helps...

    Enjoy!

    Mike
Sign In or Register to comment.