NVRAM usage on T_3.x and T_4.x and MicroMod - TeensyUser/doc GitHub Wiki
The code below shows using battery backed RAM provided while power is present or if an RTC cell is powering while off.
This can hold info for debugging on restart, or state info or counter value from prior execution.
Also shows program controlled RESET of Teensy or entering Bootloader.
// Accessing as 4 32 bit DWORDS of NVRAM backed data on 1062 MCU
// For T_3.x there are 7 DWORDS, the 8th is used by PJC to track valid RTC clock
#include <TimeLib.h>
#if defined(__IMXRT1062__)
uint32_t *NVRAM_UINT32 ((uint32_t *)0x400D4100);
#define DWORDS 4
#else
uint32_t *NVRAM_UINT32 ((uint32_t *)0x4003E000);
#define DWORDS 7
#endif
uint32_t tRam[DWORDS]; // just for speed compare of writes
elapsedMillis tSome;
void showTime(void)
{
setSyncProvider(getTeensy3Time); // use the Teensy 4.0 RTC timer
if (timeStatus() != timeSet)
Serial.println(F("Unable to sync with RTC"));
else
Serial.println(F("RTC has set system time"));
time_t tm = Teensy3Clock.get();
tm = now();
Teensy3Clock.set(tm);
Serial.print("hour:");
Serial.print(hour());
Serial.print("\tminute:");
Serial.print(minute());
Serial.print("\tsecond:");
Serial.println(second());
}
void setup() {
#if defined(__IMXRT1062__)
SNVS_LPCR |= (1 << 24); //Enable NVRAM - documented in SDK
#endif
Serial.begin(115200);
while (!Serial && millis() < 4000 );
Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
tSome = 0;
showTime();
showHelp();
for ( int ii = 0; ii < DWORDS; ii++ ) {
tRam[ii] = 0;
}
}
void loop() {
if ( tSome >= 1000 ) {
tSome -= 1000;
for ( int ii = 0; ii < DWORDS; ii++ ) {
Serial.print( '\t' );
Serial.print( NVRAM_UINT32[ii] );
}
Serial.println();
for ( int ii = 0; ii < DWORDS; ii++ ) {
NVRAM_UINT32[ii]++;
}
}
while ( Serial.available() ) {
char cc = Serial.read();
if ( 'r' == cc ) {
SCB_AIRCR = 0x5FA0004;
while (true);
}
else if ( 'b' == cc ) {
Serial.print( "Bootloader !!!\n" );
delay(10);
asm("bkpt #251");
}
else if ( 'z' == cc ) {
Serial.print( "_ZERO_ NVRAM\n" );
for ( int ii = 0; ii < DWORDS; ii++ ) {
NVRAM_UINT32[ii] = 0;
}
}
else if ( 't' == cc ) {
Serial.print( "_Add_1K_ NVRAM\n" );
uint32_t tt = micros();
for ( int jj = 0; jj < 1000; jj++ ) {
for ( int ii = 0; ii < DWORDS; ii++ ) {
NVRAM_UINT32[ii]++;
}
}
tt = micros() - tt;
Serial.printf("\t%lu us\t for 1K increments of 4 (7) RTC DWORDS\n", tt );
tt = micros();
#if defined(__IMXRT1062__)
for ( int jj = 0; jj < 1000; jj++ ) {
SNVS_LPGPR0 += 1;
SNVS_LPGPR1 += 2;
SNVS_LPGPR2 += 3;
SNVS_LPGPR3 += 4;
}
#else
for ( int jj = 0; jj < 1000; jj++ ) {
for ( int ii = 0; ii < DWORDS; ii++ ) {
NVRAM_UINT32[ii] += 1 + ii ;
}
}
#endif
tt = micros() - tt;
Serial.printf("\t%lu us\t for 1K increments of 4 (7) RTC #define DWORDS\n", tt );
tt = micros();
for ( int jj = 0; jj < 1000; jj++ ) {
for ( int ii = 0; ii < DWORDS; ii++ ) {
tRam[ii]++;
}
}
tt = micros() - tt;
Serial.printf("\t%lu us\t\t for 1K increments of 4 (7) RAM DWORDS\n", tt );
}
else if ( '?' == cc ) {
showHelp();
}
else {
Serial.print( "_10_INC_ NVRAM\n" );
tSome += 10000;
}
}
}
void showHelp() {
Serial.print(" 'r':RESTART 'b':bootloadr 'z':ZERO '?':Help t:INC 1K else:INC 10 to each\n");
}
time_t getTeensy3Time()
{
return Teensy3Clock.get();
}