Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 00:13 08 Feb 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Volatile RAM. Thinking DS1307 without battery.

     Page 1 of 2    
Author Message
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1024
Posted: 06:26pm 21 Jan 2025
Copy link to clipboard 
Print this post

I need to set a flag that persists when I stop and restart (or load) the program but that resets in the event of a power-down.

We don't have a RAM feature that does this, right?

I have played with the DS1307 without battery and it works great but I keep missing so many PicoMite capabilities/features that I thought I'd ask, first.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9512
Posted: 07:07pm 21 Jan 2025
Copy link to clipboard 
Print this post

Ages ago I put in something like this but no-one expressed any interest so I think it got lost at some stage. I could resurrect it. Something on the lines of

SAVE PERSISTENT n%
? mm.info(PERSISTENT)
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9512
Posted: 07:26pm 21 Jan 2025
Copy link to clipboard 
Print this post

UPDATE

Just tested and it works for CPU RESTART or watchdog reset but not for pressing reset button
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4527
Posted: 07:43pm 21 Jan 2025
Copy link to clipboard 
Print this post

Hi Phenix,

Any R/W device that is connected could be used (when not reset by the driver). In example an LCD display that can be read (MISO/MOSI) as long as you do not clear the screen at start of your program, but first read it. Make a pixel red, and read it back.

Volhout
PicomiteVGA PETSCII ROBOTS
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9512
Posted: 07:55pm 21 Jan 2025
Copy link to clipboard 
Print this post

UPDATE

Works on RP2040 for both H/W and watchdog reset but only watchdog on RP2350. Have raised an issue with RPi as it should work for both
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1024
Posted: 08:00pm 21 Jan 2025
Copy link to clipboard 
Print this post

  matherp said  UPDATE

Just tested and it works for CPU RESTART or watchdog reset but not for pressing reset button


Perfect  

@Volhout: Yeah but it's nice no to have to add bits.  

I know you don't like this but the PIO quadrature counter persists even after re-initializing it by starting the MMBasic program. I have done this countless times and it never fails  
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1024
Posted: 08:01pm 21 Jan 2025
Copy link to clipboard 
Print this post

  matherp said  UPDATE

Works on RP2040 for both H/W and watchdog reset but only watchdog on RP2350. Have raised an issue with RPi as it should work for both


But if power is removed from the RP2350?
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1364
Posted: 08:40pm 21 Jan 2025
Copy link to clipboard 
Print this post

  PhenixRising said  I need to set a flag that persists when I stop and restart (or load) the program but that resets in the event of a power-down. ...

Just a reminder: The DS3231 RTC chip has 7 bytes that are actually intended for alarms. These could be used to remember states beyond power off.
Michael
causality ≠ correlation ≠ coincidence
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2098
Posted: 09:18pm 21 Jan 2025
Copy link to clipboard 
Print this post

  twofingers said  The DS3231 RTC chip has 7 bytes that are actually intended for alarms. These could be used to remember states beyond power off.


this is a great tip!

one caveat, the registers store BCD and are not fully 8 bits (bit 7 of each register means something) so you need to be attentive that any values written conform. I have just tried here writing &h7F, and it got munged into some weird value I have yet to interpret. Mine come from 99p modules off ebay so are probably chinese knock-offs so ymmv.

Stick to a few flags in the bottom couple of bits and you should be fine.
Edited 2025-01-22 07:23 by CaptainBoing
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2281
Posted: 11:05pm 21 Jan 2025
Copy link to clipboard 
Print this post

From DS1307 datasheet.
  Quote  56-Byte, Battery-Backed, General-Purpose RAM with Unlimited Writes

So with no battery and Flag% stored there that should ensure the flag is only erased on power fail and not Reset.
  DS1307 datasheet said  RTC AND RAM ADDRESS MAP
Table 2 shows the address map for the DS1307 RTC and RAM registers. The RTC registers are located in address
locations 00h to 07h. The RAM registers are located in address locations 08h to 3Fh. During a multibyte access, when the address pointer reaches 3Fh, the end of RAM space, it wraps around to location 00h, the beginning of the clock space.


n%=54321
> SAVE PERSISTENT n%
Error : Expected a string
> SAVE PERSISTENT "n%"
Error : Expected a string
> ? mm.info(PERSISTENT)
Error : Syntax
>
> option list
PicoMiteVGA MMBasic RP2350A Edition V6.00.02b1


Another option if the Flag doesn't get updated too often.
RTC GetTime

If Val(Right$(Date$,4)) > 2024 Then
  VAR RESTORE
 Else
  Flag% = 0
EndIf

RTC SetTime ....2025

'.....
If InfrequentCondition Then
  VAR SAVE Flag%
EndIf

Edited 2025-01-22 10:18 by phil99

Footnote added 2025-01-22 21:59 by phil99
Late entry
Beta2 has now been released with SAVE PERSISTENT added and as noted by @btwolf a few posts below this, some form of error test may be needed. He suggests a checksum. If that is 8 bits you still have 56 to play with. Though if the number saved is always less than say 32 bits a simple IF MM.INFO(PERSISTENT) < 2^32 Then ... or whatever will do.
SAVE PERSISTENT 54321
> ? MM.INFO(PERSISTENT)
54321
> cpu restart
> ? MM.INFO(PERSISTENT)
54321
PicoMiteVGA MMBasic RP2350A Edition V6.00.02b2 'reset button
Copyright 2011-2024 Geoff Graham
Copyright 2016-2024 Peter Mather

> ? MM.INFO(PERSISTENT)
8695604412579392460   'random data after reset
PicoMiteVGA MMBasic RP2350A Edition V6.00.02b2 'reset button
Copyright 2011-2024 Geoff Graham
Copyright 2016-2024 Peter Mather

> ? MM.INFO(PERSISTENT)
4081666594875190157
>
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1024
Posted: 06:26am 22 Jan 2025
Copy link to clipboard 
Print this post

  phil99 said  
n%=54321
> SAVE PERSISTENT n%
Error : Expected a string
> SAVE PERSISTENT "n%"
Error : Expected a string
> ? mm.info(PERSISTENT)
Error : Syntax
>
> option list
PicoMiteVGA MMBasic RP2350A Edition V6.00.02b1




Yeah, you won't have it because Pete only just reactivated it to test.  
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7106
Posted: 09:14am 22 Jan 2025
Copy link to clipboard 
Print this post

Hmmm... There's usually a hardware answer. :)

Put a series resistor and capacitor on a GPIO pin. Put a diode from the top of the cap to 3V3, so that it's reverse-biased. Use SETPIN GPn DOUT / PIN(GPn)=1 to charge the capacitor (then SETPIN GPn OFF to isolate it if you want to). Even a small cap will stay charged for some time. When you boot the pin will be set as input by default so just use a%=pin(GPn) to read the status. When the power goes off the cap will discharge through the collapsing 3V3 rail so the flag will reset.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4527
Posted: 10:10am 22 Jan 2025
Copy link to clipboard 
Print this post

Mick,

Does not work with 2350 due to the GPIO bug. But I admit, it is simple.
On the other hand, a 74HC74 flipflop can do similar (may require 2 resitors) on one GPIO pin.

But if you (just) want to distinguish between reset due to power loss, and reset due to watchdog/internal failure there may be simpler solutions.

Volhout
Edited 2025-01-22 20:13 by Volhout
PicomiteVGA PETSCII ROBOTS
 
bfwolf
Newbie

Joined: 03/01/2025
Location: Germany
Posts: 26
Posted: 10:13am 22 Jan 2025
Copy link to clipboard 
Print this post

Silly question: Why not use a dedicated RAM area and store different values ​​there (depending on the desired state) with a CRC or "checksum" above them? Then "destroy" these values ​​after the evaluation.

After power-on, there are very likely random values ​​in this RAM area and the CRC or "checksum" would then be incorrect and you would recognize this. You can also ensure that in "startup" after reset or watchdog the values ​​are "destroyed" or overwritten by other defined values ​​with the correct CRC or "checksum" and thus recognize that something like this was happening.

You just have to find a RAM area that has a fixed address and is not affected by MMBasic.

bfwolf
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4527
Posted: 10:25am 22 Jan 2025
Copy link to clipboard 
Print this post

Mick,

Simple idea: whenusing a original picomite, the Vbus is checked though GPIO24. Vbus is your input voltage. Vsys is the voltage that runs the RP2040.
When you add a larger capacitor (supercap ?) to Vsys, the RP2040 keeps running for seconds to minutes (depending the size of the supercap) during which period you can sample Vbus, record the power fail, save the power fail (including date-time) in your filesystem... and then go down controlled.

Just one e-cap or supercap, and a few lines of code.

Volhout
PicomiteVGA PETSCII ROBOTS
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1024
Posted: 11:50am 22 Jan 2025
Copy link to clipboard 
Print this post

Some good info, here. Glad I brought it up  
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2281
Posted: 12:04pm 22 Jan 2025
Copy link to clipboard 
Print this post

SAVE PERSISTENT and MM.INFO(PERSISTENT) added in b2.
See footnote in my previous post.
 
bfwolf
Newbie

Joined: 03/01/2025
Location: Germany
Posts: 26
Posted: 08:41pm 22 Jan 2025
Copy link to clipboard 
Print this post

@PhenixRising, @Volhout:
Did nobody notice my above post?  
It wasn't ment as a joke/hoax!

I sometimes used this method e.g. to communicate between a bootloader and an application and vice versa!

I's a simple method and takes no additional special hardware! The only thing to find is a RAM area (some bytes) which isn't affected by e.g. the startup-code. Some microcontrollers even have a seperated RAM area which is small and difficult to use as "common application RAM space".
 
Frank N. Furter
Guru

Joined: 28/05/2012
Location: Germany
Posts: 902
Posted: 07:30am 23 Jan 2025
Copy link to clipboard 
Print this post

@PhenixRising:

Please don't forget to put a diode in front of the capacitor from Volhout's idea. Then the capacitor cannot be discharged externally.
This is how I have implemented it in the past and it has always worked well...

Frank
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1024
Posted: 07:41am 23 Jan 2025
Copy link to clipboard 
Print this post

  bfwolf said  @PhenixRising, @Volhout:
Did nobody notice my above post?  
It wasn't ment as a joke/hoax!

I sometimes used this method e.g. to communicate between a bootloader and an application and vice versa!

I's a simple method and takes no additional special hardware! The only thing to find is a RAM area (some bytes) which isn't affected by e.g. the startup-code. Some microcontrollers even have a seperated RAM area which is small and difficult to use as "common application RAM space".


It was the reason for the start of the thread. Before going for a hardware solution, I wondered if there was some sort of persistent-memory-while-power-was-applied that I was unaware of.
Pete had already done this in the past but there was no interest and so it was disabled.
Now it has been reinstated and works perfectly for my needs.

See phil99's posts from earlier.

  Quote  SAVE PERSISTENT and MM.INFO(PERSISTENT) added in b2.

Edited 2025-01-23 17:43 by PhenixRising
 
     Page 1 of 2    
Print this page
© JAQ Software 2025