Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: Store variable value to recover after reset

  1. #1
    Junior Member
    Join Date
    Oct 2015
    Posts
    9

    Store variable value to recover after reset

    Hello, I have a teensy 3.2 project working correctly for a few years with spi flash to store supplied amount, until the process starts I can save user and date and after the process stops I save this amount, but if a energy's cut occurs I can't know that value.

    In a normal process the electronic can store that value houndred times, so writing this value to spi flash to recover after reset will cause to waste the maximum number of writing/erase allowed by the flash memory after some time of working.

    Is there a way to store that value in a part of internal memory that stays non-volatile after reset/reboot and that don't degrade the memory?

    Thank you in advance

  2. #2
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,851
    For Teensy 3.2, 3.5, and 3.6 there is a separate memory called EEPROM:


    For the Teensy LC, 4.0, and 4.1, the EEPROM library uses the flash memory the program and initial variable contents are stored in.

  3. #3
    Junior Member
    Join Date
    Oct 2015
    Posts
    9
    Thank you for your answer, I know about internal eeprom of the teensy as a part of the flash memory, The problem is the endurance, you can only write 100.000 times.

    I was thinking in some memory as other chips have a reset reason, so after rebooting you can know if it was a soft reset, watchdog, power cut, etc.

    Regards

    Quote Originally Posted by MichaelMeissner View Post
    For Teensy 3.2, 3.5, and 3.6 there is a separate memory called EEPROM:


    For the Teensy LC, 4.0, and 4.1, the EEPROM library uses the flash memory the program and initial variable contents are stored in.
    Last edited by jesusangel; 08-11-2020 at 06:40 AM. Reason: Readed 100.000 eeprom endurance

  4. #4
    Senior Member
    Join Date
    Jan 2015
    Location
    UK
    Posts
    139
    You could use a SPI FRAM:
    https://www.adafruit.com/product/1897

  5. #5
    Junior Member
    Join Date
    Oct 2015
    Posts
    9
    Thank you, I will check it, now I'm using SST26VF64B memory, so I need a FRAM compatible to not to change code or at least the less possible.

    Regards

    Quote Originally Posted by skpang View Post
    You could use a SPI FRAM:
    https://www.adafruit.com/product/1897

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,412
    If the T_3.2 had an RTC battery it may have some storage bytes kept alive. I know the T_3.6 does. Not sure if that section functions without the crystal installed.

  7. #7
    Junior Member
    Join Date
    Oct 2015
    Posts
    9
    Quote Originally Posted by defragster View Post
    If the T_3.2 had an RTC battery it may have some storage bytes kept alive. I know the T_3.6 does. Not sure if that section functions without the crystal installed.
    Thank you for your time, yes, it has RTC battery and crystal intalled to maintain time while not connected to power. Any advice where to start?

  8. #8
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,412
    Quote Originally Posted by jesusangel View Post
    Thank you for your time, yes, it has RTC battery and crystal intalled to maintain time while not connected to power. Any advice where to start?
    It has probably been too long and non-specific keyword to forum search - not sure if the sample was T_3.6 or other - it was by 'Frank B'. The Manual PDF will have some section on the RTC areas. I found Manual notes for the T_4x's 1062 and got a couple bytes to work.

  9. #9
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,685
    Quote Originally Posted by jesusangel View Post
    Thank you for your time, yes, it has RTC battery and crystal intalled to maintain time while not connected to power. Any advice where to start?
    This is a guess, only a guess, do not pass go...

    But looking at the startup code for T3.x code yyou see:
    Code:
    #if defined(KINETISK)
    	// RTC initialization
    	if (RTC_SR & RTC_SR_TIF) {
    		// this code will normally run on a power-up reset
    		// when VBAT has detected a power-up.  Normally our
    		// compiled-in time will be stale.  Write a special
    		// flag into the VBAT register file indicating the
    		// RTC is set with known-stale time and should be
    		// updated when fresh time is known.
    		#if ARDUINO >= 10600
    		rtc_set((uint32_t)&__rtc_localtime);
    		#else
    		rtc_set(TIME_T);
    		#endif
    		*(uint32_t *)0x4003E01C = 0x5A94C3A5;
    	}
    	if ((RCM_SRS0 & RCM_SRS0_PIN) && (*(uint32_t *)0x4003E01C == 0x5A94C3A5)) {
    		// this code should run immediately after an upload
    		// where the Teensy Loader causes the Mini54 to reset.
    		// Our compiled-in time will be very fresh, so set
    		// the RTC with this, and clear the VBAT resister file
    		// data so we don't mess with the time after it's been
    		// set well.
    		#if ARDUINO >= 10600
    		rtc_set((uint32_t)&__rtc_localtime);
    		#else
    		rtc_set(TIME_T);
    		#endif
    		*(uint32_t *)0x4003E01C = 0;
    	}
    #endif
    I like magic numbers
    So if you look at the T3.2 or in my case t3.6 manual for memory locations you see: 0x4003_E000 62 VBAT register file
    And I only see this one memory location (4 bytes) referenced in the source.

    How much space is available does anyone else use it or the like not sure.

    But that is where I would start.

    Edit: T3.6 manual 4.15 (VBat register file)

    This device includes a 128-byte register file that is powered in all power modes and is powered by VBat

    it is only reset during VBAT power-on reset

  10. #10
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,412
    Thanks KurtE - A magic number is a good keyword for search

  11. #11
    Junior Member
    Join Date
    Oct 2015
    Posts
    9
    Thank you all here is the solution.

    Regards

  12. #12
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,412
    Indeed that is 3 years old - exactly what I was thinking of : forum.pjrc.com/threads/45854-Battery-backed-NV-RAM

    Odd the T_4.x doesn't have that many bytes - but nice all the T_3.x's do.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •