News:

;) This forum is the property of Proton software developers

Main Menu

Help with EEPROM gosub

Started by PicNoob, May 05, 2025, 05:57 PM

Previous topic - Next topic

PicNoob

When dealing with EEPROM for each write I need to stop interrupts, do the write, wait 5ms, and restart interrupts. So I'm trying to save some code space by placing that all in a gosub I can call.

Some sample code below. It generally works fine for saving settings. But I've noticed in some cases, unrelated function calls that push parameters break. So I think I'm blowing up the stack somehow.

Is a gosub the best way to do what I'm trying to do here? And do I have some sort of syntax issue with the setup?

 
Dim EWrite2_Addr As Byte
Dim EWrite2_Data As Byte   

For index = 0 To 11 
    m1_curve[index] = IA[index]
    'EWrite 3 + index, [m1_curve[index]] 'old method
    GoSub EWrite2[3 + index, m1_curve[index]] 'new method
Next

 
'********************************************************************************* 
EWrite2:
    GIE = 0            ' Disable global interrupts (clear GIE bit)
    Pop EWrite2_Addr        ' Get EEPROM address from parameter list
    Pop EWrite2_Data        ' Get data byte from parameter list
    EWrite EWrite2_Addr, [EWrite2_Data] ' Perform EEPROM write
    DelayMS 5               ' Delay 5ms to ensure write completion
    GIE = 1            ' Re-enable global interrupts (set GIE bit)
Return

Pepe

test this


For index = 0 To 11
    m1_curve[index] = IA[index]
    EWrite2(3 + index, m1_curve[index])
Next

 
'*********************************************************************************
Proc EWrite2(EWrite2_Addr As Byte,EWrite2_Data As Byte)
    GIE = 0            ' Disable global interrupts (clear GIE bit)
    EWrite EWrite2_Addr,[ EWrite2_Data] ' Perform EEPROM write
    DelayMS 5               ' Delay 5ms to ensure write completion
    GIE = 1            ' Re-enable global interrupts (set GIE bit)
EndProc

PicNoob

#2
Thanks that will compile in v4 but unfortunately, this program is stuck in v3.5, an older proton version. I've tried to port it over before but it (ironically enough) ends up with weird EEPROM hard to track down issues when I do. :( I'm hoping to identify the problem with the gosub, unless there is some other way of doing function calls in v3.5 I'm not familiar with.

Pepe

#3
test this for 3.5 version

Dim bind As Byte

For index = 0 To 11
    m1_curve[index] = IA[index]
    bind =  3 + index
    GoSub EWrite2[bind, m1_curve[index]]
Next

 
'*********************************************************************************
EWrite2:
    GIE = 0            ' Disable global interrupts (clear GIE bit)
    Pop  EWrite2_Data
    Pop  EWrite2_Addr
    EWrite EWrite2_Addr,[ EWrite2_Data] ' Perform EEPROM write
    DelayMS 5               ' Delay 5ms to ensure write completion
    GIE = 1            ' Re-enable global interrupts (set GIE bit)
Return

top204

#4
Make sure you set the Stack's size large enough using: Declare Stack_Size.

The older 8-bit compiler defaulted to a larger software stack, but the new Positron8 compiler defaults to a very low stack size used for the compiler's internals so save/restore certain SFRs and compiler system variables when required, and the Declare Stack_Size is not issued within a code listing, and the parameters pushed on to it with the Gosub may be over-flowing it.

Also please remember, the Gosub parameters were a very, very early mechanism for a procedure type functionality, and have been replaced with true procedures.

PicNoob

Quote from: top204 on Today at 09:02 AMMake sure you set the Stack's size large enough using: Declare Stack_Size.

The older 8-bit compiler defaulted to a larger software stack, but the new Positron8 compiler defaults to a very low stack size used for the compiler's internals so save/restore certain SFRs and compiler system variables when required, and the Declare Stack_Size is not issued within a code listing, and the parameters pushed on to it with the Gosub may be over-flowing it.

Also please remember, the Gosub parameters were a very, very early mechanism for a procedure type functionality, and have been replaced with true procedures.

Thanks for the tips!

On the stack size, how large does it need to be? I have it set to greater than the size of all data being pushed at any given moment. For example, if I call gosubs with no more than 10 bytes at any given moment, would a 10 byte stack size be sufficient? Still need to test pepe's proposed change also shortly!

Are the procedures handled the same way as gosub when compiled? An internal goto, calculation, and return sort of thing? On programs I have ported over to v4 I've left the old gosub code. I figured its all the same when compiled. But would be interested to hear if that isn't the case!