Shop OBEX P1 Docs P2 Docs Learn Events
Help Writing Spin Program to EEPROM — Parallax Forums

Help Writing Spin Program to EEPROM

mynet43mynet43 Posts: 644
edited 2009-06-10 21:31 in Propeller 1
I'm trying to write a little program to read a spin program from an SD card and write it to the Propeller EEPROM, so it will boot up when power is cycled.

I've generated the saved program from the Propeller Tool by selecting:
Run/Compile Current/View Info

This displays the object info. From this I select: Show Hex. This brings up a listing of the program and several new option buttons.
I select the "Save EEPROM File" button and enter the file name I want to save. I then rename the file and save it to the SD card.

The program I'm using to copy from the SD card to the EEPROM is shown below. It seems to work fine, with no errors.

The messages displayed are:
VGA Started
i2c Started
SD Card Mounted
File Opened
File Closed
WgtBin written. (WgtBin is the name of the EEPROM file on the SD card.

I'm at the point where I've decided I'm missing something.

Here's the program I'm using:

CON
        _clkmode        = xtal1 + pll16x
        _xinfreq        = 5_000_000

' VGA and Keyboard info for vga.start
  BasePin  = 16                 ' base pin for video output (0, 8, 16, 24) - 16 on proto board
  
  kbd_dpin = 26                 ' keyboard dpin number - pin 26 on proto board
  kbd_cpin = 27                 ' keyboard cpin number - pin 27 on proto board
  kbd_lock = %0_001_000         ' keyboard locks: disable scroll lock key, NumLock, CapsLock, ScrollLock all off
  kbd_auto = %01_01000          ' keyboard auto repeat: 0.5 sec delay, 15 chars/sec repeat (?)
  kbdflag  = 0                  ' set this if a keyboard is connected/needed

OBJ
  sd  : "fsrw"                    ' FAT File System Read/Write
  i2c : "Basic_I2C_Driver"
  vga : "VGA_1024"

VAR
   long f0[noparse][[/noparse]128]

DAT
   filname   byte "WgtBin", 0        ' file name on SD card to copy to EEPROM

PUB CopyToEEPROM | a, b, c, d, i, j, sstart, start_time

'   start the VGA and Keyboard routines
    vga.start(BasePin, kbdflag, kbd_dpin, kbd_cpin, kbd_lock, kbd_auto)

    vga.cursloc(0,1)                 ' locate cursor x,y
    vga.str(string("VGA Started",13))
    
    i2c.Initialize(i2c#BootPin)
    vga.str(string("i2c Started",13,13))

    if sd.mount(0) < 0              ' base pin 0
      vga.str(string("Can't mount SD card",13))
    else
      vga.str(string("SD Card Mounted",13))
      
    if sd.popen(@filname,"r")
      vga.str(string("Can't open file",13))
    else
      vga.str(string("File Opened",13))

    sstart:=0
    i:=true

    repeat 64                           
      if sd.pread(@f0,512) <> 512
        vga.str(string("Can't read program",13))
'      else
'        vga.str(string("Read Program OK",13))

'      sdfat.pread(@f0, 512)
      j:=@f0

      repeat 16
        if i2c.WritePage(i2c#BootPin, i2c#EEPROM, sstart, j, 32)
          vga.str(string("Copy EEPROM write error",13))
          i:=false
          quit
'       else
'         vga.str(string("EEPROM write OK",13))
              
        start_time := cnt ' prepare to check for a timeout
        repeat while i2c.WriteWait(i2c#BootPin, i2c#EEPROM, sstart)
          if cnt - start_time > clkfreq / 10
            vga.str(string("Copy EEPROM wait error",13))
            i:=false
            quit
'         else
'           vga.str(string("EEPROM wait OK",13))
      sstart += 32
      j += 32
      
    if sd.pclose < 0
      vga.str(string("Error closing file",13))
    else
      vga.str(string("File Closed",13))

    if i == true
      vga.out(13)
      vga.str(@filname)
      vga.str(string(" written.",13))
    else
      vga.str(string("Write error [noparse][[/noparse]"))
      vga.dec(i)
      vga.str(string("]!",13)) 



You can see I've stolen some of it from other users [noparse]:)[/noparse]


Here's a hex dump of the first part of the EEPROM file (WgtBin) stored on the SD Card:

0000: 00 B4 C4 04 6F 73 10 00 20 1F 94 44 24 00 9C 44 ....os.. ..D$..D
0010: A0 01 03 02 14 00 04 00 40 00 0C 00 A0 01 14 00 ........@.......
0020: 68 14 08 21 02 05 02 65 01 87 80 2B 06 03 0E 01 h..!...e...+....
0030: 64 06 03 0A 01 38 0D 06 03 0D 32 52 65 74 75 72 d....8....2Retur
0040: 6E 65 64 20 66 72 6F 6D 20 73 74 61 72 74 0D 00 ned from start..
0050: 01 37 03 35 38 1A 38 1B 37 02 38 28 06 03 01 01 .7.58.8.7.8(....
0060: 87 81 11 06 03 0E 01 35 06 04 01 01 87 81 1C 06 .......5........
0070: 03 0E 01 87 81 26 06 03 0E 01 06 04 09 35 00 8B .....&.......5..
0080: 00 06 04 0A FC 0A 0E 01 8B 00 06 03 0E 01 38 0D ..............8.
0090: 06 03 0D 04 68 01 87 81 2D 06 03 0E 01 38 0D 06 ....h...-....8..
00A0: 03 0D 00 87 81 3D 38 77 06 04 04 65 01 87 81 4A .....=8w...e...J
00B0: 06 03 0E 01 64 06 03 0A 01 38 0D 06 03 0D 3F 91 ....d....8....?.
00C0: 69 35 6D 37 21 08 14 38 27 08 08 01 38 2A 06 04 i5m7!..8'...8*..
00D0: 08 09 78 01 38 0D 06 04 08 09 6C 01 06 04 03 01 ..x.8.....l.....
00E0: 87 81 5C 06 03 0E 00 87 81 69 38 72 06 04 04 65 ..\......i8r...e
00F0: 01 87 81 77 06 03 0E 01 64 06 03 0A 01 38 0D 06 ...w....d....8..

I think I should be storing something in the first part of the file before it's copied, but I don't know what to store there.

Would someone please take a look at this and tell me where I'm going wrong.

Thanks for your help.

Jim

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-10 03:56
    What's wrong?

    Your program looks like it ought to copy the specified file from an SD card file to the boot EEPROM.

    What do you need to store in the first part of the file before it's copied? The .binary file should be
    what you need to copy to EEPROM. There is a checksum in the Spin header (bytes $0000-$000F),
    but I think it's only used during the download from the PC. The program loader in FemtoBasic (and
    some other programs) also uses this checksum when loading from EEPROM or an SD card file, but
    it stops checksumming at the end of the program in the .binary file and doesn't care what's beyond
    that.
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-06-10 06:43
    sstart and j are incremented outside of the loop aren't they? Should be incremented inside Did you propably copy that code from my thread, where Mike told me just that and did not incorporate the change? ;o)
    EEPROM-file is fine ... you only waste the part of the 32kB that's not used by your program, but on a 2 GB SD-card ... who cares?

    I'd only check the datasheet of the EEPROM you used to adjust the number of bytes you write to it in the inner loop.
    ( PS: Ah ... I see you already did ... in my code it was 64 )


    PPS: And I added
    ············· if (sstart//1024) == 0
    ··············· term.tx( "." )
    after the if i2c.WritePage...

    This gives a nice progress indication ;o)


    Post Edited (MagIO2) : 6/10/2009 6:53:37 AM GMT
  • mynet43mynet43 Posts: 644
    edited 2009-06-10 13:45
    @ Mike & MagicIO,

    Thanks to both of you. Yes, I borrowed code from each of you [noparse]:)[/noparse]

    MagicIO, you nailed the problem. It was incrementing of the two variables outside of the loop.
    It's one of those things I could have stared at for eons and not seen.

    I fixed those and now it works perfectly.

    I'm using the at24c512, which has a 128 byte page, so I can probably change the write from 32 to 128 bytes.
    The reason I had 32 was I was afraid I'd lose something at the end of the file.

    I'm still not clear on the difference between the "binary" and the "eeprom" files.
    Are these documented somewhere? This seems to work OK with the eeprom file.

    Thanks again for the help. You saved me a lot of time!

    Jim
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-06-10 21:12
    At boot time the propeller loads the complete HUB-RAM with the data stored in EEPROM. This is the 32kBytes at the beginning of EEPROM. And that is exactly what is in the EEPROM-file regardless if all of the memory is used or not.
    The binary file only contains the used space of the EEPROM, so usually it's a bit smaller. That's it.
  • mynet43mynet43 Posts: 644
    edited 2009-06-10 21:31
    MagIO2,

    Thanks for the info, it helps.

    By the way, I tried the program with 128 byte writes, and it worked fine with this EEPROM.

    Jim
Sign In or Register to comment.