writing to EEPROM

General problems? Ask here!

Moderator: cbarnes

writing to EEPROM

Postby Mr. Bill » Wed Oct 13, 2010 12:33 pm

Has any one ever successfully written to EEPROM from within an assembly program (or a C program) running on the MicroStamp11?
I have tried several approaches, and so far I have not been succesfull. I used the code from msload.asm:

* Writes the value contained in register A to the address pointed to
* by register Y.
staa 0,Y
jsr Delay_5_ms
ldx #$0700
D1: dex
bne D1

The spec sheet for the 28C64 EEPROM chip on my MicroStamp11 board states that the chip automatically erases and programs on a byte by byte basis
as long as a 5ms delay is provided after the write, which appears to be what is going in the code snippet on above.

Of course, I turn the WRITE PROT to WRITE mode before executing the code. I am also making sure the byte that is being changed is in an unused area of EEPROM (so as not to crash the program.) The program crashes as soon as the call the eeWrite routine with an address in EEPROM:

ldaa #$12 data to store into EEPROM
ldy #$E600 at address E600 (unused byte)
jsr eeWrite program crashes here
Mr. Bill
Posts: 4
Joined: Mon Oct 11, 2010 2:39 pm
Location: wwalker@kvctc.edu

Re: writing to EEPROM

Postby cbarnes » Wed Oct 13, 2010 2:08 pm

The reason that it works in the bootloader but not in your program is that the bootloader code is running from RAM. By contrast, a program executing from EEPROM cannot modify any location in EEPROM because the entire chip goes into a suspend state while the internal Erase-Write cycle is executing, causing the address/data pins to enter a high-impedance state. The CPU will no longer be executing your code at that point-- just $FF op codes.

In order to be successful, you'll need to first copy your EEPROM Write subroutine to RAM, jump to it to modify the desired byte or bytes, and then return to your main program in EEPROM. The danger in doing this is that, if there is a power glitch (or program bug) during this operation, the contents of EEPROM can become corrupted. This is why the EEPROM manufacturer provides a special feature called Software Data Protection, implemented via an internal state machine. The devices have this mechanism disabled, by default, when shipped from the manufacturer, but it is possible to enable it and use it. (Consult the EEPROM manufacturer's data sheet for details.) In such a configuration, you would leave the hardware Write Protect switch permanently in the Write position. Be forewarned, however, that once you enable SDP, MicroLoad can no longer be used to load programs into your board's EEPROM. You will instead need to create your own loader that implements SDP, or else you'll need to create a utility to turn off SDP before using MicroLoad.
Posts: 79
Joined: Mon Jul 26, 2004 10:28 pm
Location: Toronto

Re: writing to EEPROM

Postby Mr. Bill » Wed Oct 13, 2010 11:31 pm

Wow. That works like a charm! I moved the eeWrite routine to RAM and called it from my program in EEPROM.
(I should have figure that out on my own. I teach this stuff for a living.)
Yes, I notice that ocassionally the program goes haywire, even lighting up LEDs that I have on port A all by itself!
This doesn't happen with the WRITE/PROT switch in PROT. I can trap it with COP but the damaged has already been done.
Trapping it with Invalid opcode works a whole lot better.

I wonder?? Could I pull the 28C64 chips WR* line high using a port pin? That way, I
could turn write on in my eeWrite routine and then turn it off again when it finishes. The exposure to
glitches would be greatly reduced. Looking at the schematic, a 10K resitor from terminal 3 of
the WRITE/PROT switch tied to, say, PD5, would do the trick, I think. OK, I understand that would void the
warranty on the MicroStamp11 ... but we burn up at least three chips a semester anyway.

For the benefit of others who may want to use EEPROM for data storage, here are some code snippets:

org RAM
eewrite equ * ;these 10 bytes is where the
eewram rmb 10 ;eeWrite code will be placed

ldab #10 ;move the eewrite code to RAM
ldx #eewdumy
ldy #eewram
emv1: ldaa 0,x
staa 0,y
bne emv1

eewdumy: staa 0,y ;write to EEPROM here from RAM
ldx #3333 ;delay 10ms (5ms may be enough)
eew1: dex
bne eew1

ldaa a,#12 ;data to write
ldy #$E600 ;EEPROM location to write to
jsr eewrite
Mr. Bill
Posts: 4
Joined: Mon Oct 11, 2010 2:39 pm
Location: wwalker@kvctc.edu

Return to General Problems

Who is online

Users browsing this forum: No registered users and 1 guest