Shop OBEX P1 Docs P2 Docs Learn Events
Scratching my head with this FSRW code... need some help — Parallax Forums

Scratching my head with this FSRW code... need some help

Don MDon M Posts: 1,653
edited 2012-09-25 19:33 in Propeller 1
I am trying to write a block of data from dat to a uSD card. I can't seem to make this work. What am I doing wrong?
con

  _clkmode = xtal1 + pll16x
  _clkfreq = 80_000_000

  MS_001   = 80_000_000 / 1_000   

  SD_DO  = 0                                            ' SD card pins
  SD_CLK = 1
  SD_DI  = 2
  SD_CS  = 3

obj

  term   : "fullduplexserialplus"                       ' for terminal output
  sd     : "fsrw"                                       ' SD card routines     

var

pub main  | b, i, l, m, num_bytes

  term.start(31, 30, %0000, 115_200)                                            ' start terminal (use PST)
  pause(500)  

  term.tx(0)                                                                    ' clear screen
    
  m := \sd.mount(SD_DO)                                                         ' start sd object and mount card 
  if m == 0
    term.str(string("SD card mounted OK", 13))
  else
    term.str(string("SD card failed to mount / not present", 13))
    
  term.str(@version)                                                            ' display version on PST

  l := \sd.popen(string("test.txt"),"w")                                        ' open file for writing on sd card called test.txt
  if l == 0
    term.str(string("test.txt file opened", 13))
  else
    term.str(string("File failed to open", 13))

  i := 0                                                                        ' initialize index

  repeat i from 0 to 270                                                        ' cycle through data and write to sd card
    b := \sd.pwrite(data[i], 2)
    term.dec(i)                                                                 ' show index counter on PST
    term.tx(32)
    
  term.tx(13)     
  term.dec(b)                                                                   ' show number of bytes written to card
  term.tx(13)

  pause(1000)

  l := \sd.pclose                                                               ' close sd card file
  if l == 0
    term.str(string("File closed", 13))
  else
    term.str(string("File failed to close", 13))      


pub pause(ms) | t

  t := cnt
  repeat ms
    waitcnt(t += MS_001)

dat

version byte  "sniff_playground", 13, 0

data    byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00
        byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
        byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
        byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
        byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
        byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00
        byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
        byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
        byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
        byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 


Forgot to mention that this is what I see:

PST result 1.jpg



Edit: Added screen shot of PST
1024 x 341 - 63K

Comments

  • Don MDon M Posts: 1,653
    edited 2012-09-25 16:21
    Oops.. I see my repeat loop should only go to 269 (or 270 -1) but that didn't help....

    I should also mention I'm using Martins new DNA board and 2 GB sd card.
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-09-25 16:59
    pwrite requires a pointer, so you should pass @data instead of data to it. However, that's not causing the pclose failure. Print out the return code that you get from pclose and that should help to narrow down the problem.
  • Don MDon M Posts: 1,653
    edited 2012-09-25 17:14
    Okay I made it a bit further but the data stored on the SD card is not what I expected. I thought I would see the hex bytes stored. Evidently I must be using the wrong format...

    Here's my new code:
    con
    
      _clkmode = xtal1 + pll16x
      _clkfreq = 80_000_000
    
      MS_001   = 80_000_000 / 1_000   
    
      SD_DO  = 0                                            ' SD card pins
      SD_CLK = 1
      SD_DI  = 2
      SD_CS  = 3
    
    obj
    
      term   : "fullduplexserialplus"                       ' for terminal output
      sd     : "fsrw"                                       ' SD card routines     
    
    var
    
    pub main  | b, i, l, m, num_bytes
    
      term.start(31, 30, %0000, 115_200)                                            ' start terminal (use PST)
      pause(500)  
    
      term.tx(0)                                                                    ' clear screen
        
      m := \sd.mount(SD_DO)                                                         ' start sd object and mount card 
      if m < 0
        term.str(string("SD card failed to mount / not present", 13))
        abort  
      else
        term.str(string("SD card mounted OK", 13))
        
      term.str(@version)                                                            ' display version on PST
    
      l := \sd.popen(string("test.txt"),"w")                                        ' open file for writing on sd card called test.txt
      if l < 0
        term.str(string("File failed to open", 13)) 
      else
        term.str(string("test.txt file opened", 13)) 
    
      i := 0                                                                        ' initialize index
    
      repeat i from 0 to 270 - 1                                                    ' cycle through data and write to sd card
        b := \sd.pwrite(@data[i], 2)
        term.dec(i)                                                                 ' show index counter on PST
        term.tx(32)
        
      term.tx(13)     
      term.dec(b)                                                                   ' show number of bytes written to card
      term.tx(13)
    
      pause(1000)
    
      l := \sd.pclose                                                               ' close sd card file
      if l < 0
        term.str(string("File failed to close", 13))
      else  
        term.str(string("File closed", 13))
    
      l := \sd.unmount   
      if l < 0
        term.str(string("Failed to unmount", 13))
      else  
        term.str(string("Card unmounted", 13))        
    
    
    pub pause(ms) | t
    
      t := cnt
      repeat ms
        waitcnt(t += MS_001)
    
    dat
    
    version byte  "sniff_playground", 13, 0
    
    data    byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00  
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
    
    

    PST screen shot:

    PST result 2.PNG


    Notepad screen shot:

    PST result 2A.jpg
    962 x 321 - 17K
    1024 x 182 - 20K
  • Don MDon M Posts: 1,653
    edited 2012-09-25 18:30
    So I'm still kind of lost here. I've gotten the card writing part working and can store the DAT file. I had to convert the dat from $0B, $00, etc... to "0B 00 ...". It now stores them as ascii characters but that isn't what I want.

    How do I store it as hex data? I know this may seem elementary to some but it's confusing to me sometimes....
  • kuronekokuroneko Posts: 3,623
    edited 2012-09-25 18:40
    If all you want is the data table to appear in the file I'd use sd.pwrite(@data{0}, 270).
  • Don MDon M Posts: 1,653
    edited 2012-09-25 19:23
    Well that kind of works but it seems like I'm missing some data. Not sure why.

    Here's my code:
    con
    
      _clkmode = xtal1 + pll16x
      _clkfreq = 80_000_000
    
      MS_001   = 80_000_000 / 1_000   
    
      SD_DO  = 0                                            ' SD card pins
      SD_CLK = 1
      SD_DI  = 2
      SD_CS  = 3
    
    obj
    
      term   : "fullduplexserialplus"                       ' for terminal output
      sd     : "fsrw2_6A"                                       ' SD card routines     
    
    var
    
      byte  buf[500]
    
    pub main  | b, i, l, m, num_bytes
    
      term.start(31, 30, %0000, 115_200)                                            ' start terminal (use PST)
      pause(500)  
    
      term.tx(0)                                                                    ' clear screen
        
      m := \sd.mount(SD_DO)                                                         ' start sd object and mount card 
      if m < 0
        term.str(string("SD card failed to mount / not present", 13))
        abort  
      else
        term.str(string("SD card mounted OK", 13))
        
      term.str(@version)                                                            ' display version on PST
    
      l := \sd.popen(string("test.txt"),"w")                                        ' open file for writing on sd card called test.txt
      if l < 0
        term.str(string("File failed to open", 13)) 
      else
        term.str(string("test.txt file opened in write mode", 13)) 
    
      sd.pwrite(@data{0}, 269)
    
      pause(1000)
    
      l := \sd.pclose                                                               ' close sd card file
      if l < 0
        term.str(string("File failed to close", 13))
      else  
        term.str(string("File closed", 13))
    
      pause(1000)  
    
      l := \sd.popen(string("test.txt"),"r")                                        ' open file for writing on sd card called test.txt
      if l < 0
        term.str(string("File failed to open", 13)) 
      else
        term.str(string("test.txt file opened in read mode", 13))
    
      l := \sd.pread(@buf[0], 269)
      if l > -1
        term.str(string("Done", 13))
    
      pause(1000)           
    
      l := \sd.unmount   
      if l < 0
        term.str(string("Failed to unmount", 13))
      else  
        term.str(string("Card unmounted", 13))
    
      pause(1000)
      
      num_bytes := 270
      
      repeat i from 0 to num_bytes - 1
        term.hex((buf[i]), 2)
        i++          
    
    
    pub pause(ms) | t
    
      t := cnt
      repeat ms
        waitcnt(t += MS_001)
    
    dat
    
    version byte  "sniff_playground", 13, 0
    
    data    byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00  
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00 
            byte  $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00, $0B, $0B, $00, $33, $33, $00, $10, $10, $00
    
    data1   byte  "0B0B003333001010000B0B003333001010000B0B00333300101000"  
            byte  "0B0B003333001010000B0B003333001010000B0B00333300101000"  
            byte  "0B0B003333001010000B0B003333001010000B0B00333300101000"   
            byte  "0B0B003333001010000B0B003333001010000B0B00333300101000"    
            byte  "0B0B003333001010000B0B003333001010000B0B00333300101000"  
            byte  "0B0B003333001010000B0B003333001010000B0B00333300101000"  
            byte  "0B0B003333001010000B0B003333001010000B0B00333300101000"  
            byte  "0B0B003333001010000B0B003333001010000B0B00333300101000"   
            byte  "0B0B003333001010000B0B003333001010000B0B00333300101000"    
            byte  "0B0B003333001010000B0B003333001010000B0B00333300101000"   
    
    

    And here's what it prints out:

    PST result 3.jpg


    Any ideas?
    1024 x 337 - 31K
  • kuronekokuroneko Posts: 3,623
    edited 2012-09-25 19:30
    The data table is 270 bytes in size so you really should write/read 270 bytes, not 269 (pwrite/pread expect a length value). Also, in your print loop get rid of the i++. What's it supposed to do? The way it's now you only print the even members (buf[2n]).
  • Don MDon M Posts: 1,653
    edited 2012-09-25 19:33
    kuroneko wrote: »
    Also, in your print loop get rid of the i++. What's it supposed to do?

    That did it! Thanks. You're a genius! Thats what was causing it to skip over data.

    Thanks again. Marking this solved.
Sign In or Register to comment.