Shop OBEX P1 Docs P2 Docs Learn Events
Self re-programming using STORE WRITE — Parallax Forums

Self re-programming using STORE WRITE

piolepiole Posts: 8
edited 2013-12-11 14:29 in BASIC Stamp
Hi,
I've seen several posts where people did the reprogramming of their BS with another BS or with a Propeller.
But I wanted to do it wirelessly, with a WiFly module.
So I'm sharing this if anyone's interested, it pretty easy with a bs2px.
(I'm not giving details about how to connect Wifly here, only about self reprogramming).

So you need on bs2p side to have a dedicated Slot that runs a "reprogramming programme".
nbLines   VAR Byte         '[I]Nb of EEPROM lines that contain code[/I]
ix      VAR Byte
bite   VAR Byte
cksm      VAR     Word
cksmRcv  VAR Byte
jx       VAR     Nib

STORE 3          '[I]select the Slot where the programme is to be written to[/I]

[I]'Read the nb of lines through Wifly, for example[/I]
SERIN XBEE_RX, 396, 1000, Error, [WAIT("NBLINES "), nbLines ]  
ix=mybYTE


DO[INDENT]IF ix <= 0  THEN       '[I]check if all lines have been written[/I][/INDENT]
[INDENT=2]GOTO Continue[/INDENT]
[INDENT]ENDIF

[/INDENT]
[INDENT][I]'Read the first line of code in EEPROM [/I]
'[I]My actual SERIN code is a bit more complex to enable sync / ack from remote host, but this should work anyways[/I]
SERIN XBEE_RX, 396, 2000, TryAgain, [WAIT("DWL "), SPSTR 18]    '[I](18 Bytes to be stored in scratchpad RAM)[/I]

[/INDENT]
[INDENT]
[/INDENT]
[INDENT]GET 0,cksm          '[I]The first byte contains the ID of the line of code in EEPROM (must be summed in checksum)[/I][/INDENT]
[INDENT]FOR jx=0 TO 15[/INDENT]
[INDENT=2]GET jx+1,bite      '[I]read code bytes one by one in scratchpad RAM[/I][/INDENT]
[INDENT=2]cksm=cksm+bite[/INDENT]
[INDENT=2]WRITE 2048-(ix*16)+jx, bite     '[I]write code bytes into EEPROM
[/I][/INDENT]
[INDENT]NEXT[/INDENT]
[INDENT]GET 17,cksmRcv     '[I]read the 18th byte which is the checksum[/I][/INDENT]
[INDENT]IF cksmRcv <> -cksm//256 THEN      '[I]Compare checksums   
[/I][/INDENT]
[INDENT=2]GOTO Error                            [I]'OK I agree it would be better to check sum before writing to EEPROM... anyways your program might be corrupted[/I][/INDENT]
[INDENT]ENDIF                                              '[I]as the BS2px has not enough RAM to store a whole program[/I][/INDENT]
[INDENT]ix=ix-1
TryAgain:[/INDENT]
LOOP
Continue:
[I]'Something missing here ! should erase the empty code bytes (if the previous program is bigger than the new one)[/I]

On the remote side, I have a Python code running on my PC that sends the tokenized file (.obj) through a socket.

HOST = '192.168.1.XX'                         #[I] The remote host[/I]
PORT = 2000                                      # [I]The same port as used by the server[/I]
socket.setdefaulttimeout(30)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

f=open("MyNewSlot3Programme.obj", "rb") #[I]open the tokenized file[/I]
f.seek(943)                                               #[I]that's where the code starts[/I]
nbLines= f.read(1)                                     #[I]the first byte is the number of EEPROM lines[/I]

s.send('NBLINES ", nbLines)                       #[I]send the nb of bytes[/I]

for i in range(ord(nbLines)):
    data = (f.read(18))                                  #[I]read the 18 bytes[/I]
    s.send("DWL " + data)                           #[I]send the 18 bytes[/I]
    time.sleep(0.5)






Cheers

Paul

Comments

  • piolepiole Posts: 8
    edited 2013-12-11 14:29
    Just edited my post to correct mistake in the WRITE instruction.
    The program must be written at the bottom of the EEPROM.
Sign In or Register to comment.