// 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 PJRC to track valid RTC clock
#include <TimeLib.h>
#if defined(__IMXRT1062__)
uint32_t *NVRAM_UINT32 ((uint32_t *)0x400D4100);
//uint32_t *NVRAM_UINT32 (SNVS_LPGPR0);
#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 );
if ( CrashReport ) Serial.print( CrashReport );
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" );
NVRAM_UINT32[0] = NVRAM_UINT32[1] = NVRAM_UINT32[2] = NVRAM_UINT32[3]=0;
uint32_t tt = micros();
for ( int jj = 0; jj < 1000; jj++ ) {
#if 1
NVRAM_UINT32[0]+=1;
NVRAM_UINT32[1]+=2;
NVRAM_UINT32[2]+=3;
NVRAM_UINT32[3]+=4;
#else
for ( int ii = 0; ii < DWORDS; ii++ ) {
NVRAM_UINT32[ii]++;
}
#endif
}
tt = micros() - tt;
Serial.printf("\t%lu us\t for 1K increments of 4 (7) RTC DWORDS\n", tt );
Serial.printf("\tvals= %lu %lu %lu %lu \n", NVRAM_UINT32[0], NVRAM_UINT32[1], NVRAM_UINT32[2], NVRAM_UINT32[3] );
NVRAM_UINT32[0] = NVRAM_UINT32[1] = NVRAM_UINT32[2] = NVRAM_UINT32[3]=0;
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 );
Serial.printf("\tvals= %lu %lu %lu %lu \n", NVRAM_UINT32[0], NVRAM_UINT32[1], NVRAM_UINT32[2], NVRAM_UINT32[3] );
tt = micros();
for ( int jj = 0; jj < 1000; jj++ ) {
CrashReport.breadcrumb( 1, jj );
CrashReport.breadcrumb( 2, jj );
CrashReport.breadcrumb( 3, jj );
CrashReport.breadcrumb( 4, jj );
}
tt = micros() - tt;
Serial.printf("\t%lu us\t for 1K increments of breadcrumb DWORDS\n", tt );
Serial.print( "_Add_1K_ NVRAM\n" );
NVRAM_UINT32[0] = NVRAM_UINT32[1] = NVRAM_UINT32[2] = NVRAM_UINT32[3]=0;
tt = micros();
for ( int jj = 0; jj < 1000; jj++ ) {
#if 1
NVRAM_UINT32[0]=jj;
NVRAM_UINT32[1]=jj;
NVRAM_UINT32[2]=jj;
NVRAM_UINT32[3]=jj;
#else
for ( int ii = 0; ii < DWORDS; ii++ ) {
NVRAM_UINT32[ii]++;
}
#endif
}
tt = micros() - tt;
Serial.printf("\t%lu us\t for 1K increments of 4 (7) RTC DWORDS\n", tt );
Serial.printf("\tvals= %lu %lu %lu %lu \n", NVRAM_UINT32[0], NVRAM_UINT32[1], NVRAM_UINT32[2], NVRAM_UINT32[3] );
NVRAM_UINT32[0] = NVRAM_UINT32[1] = NVRAM_UINT32[2] = NVRAM_UINT32[3]=0;
tt = micros();
#if defined(__IMXRT1062__)
for ( int jj = 0; jj < 1000; jj++ ) {
SNVS_LPGPR0 =jj;
SNVS_LPGPR1 =jj;
SNVS_LPGPR2 =jj;
SNVS_LPGPR3 =jj;
}
#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 );
Serial.printf("\tvals= %lu %lu %lu %lu \n", NVRAM_UINT32[0], NVRAM_UINT32[1], NVRAM_UINT32[2], NVRAM_UINT32[3] );
tt = micros();
for ( int jj = 0; jj < 1000; jj++ ) {
CrashReport.breadcrumb( 1, jj );
CrashReport.breadcrumb( 2, jj );
CrashReport.breadcrumb( 3, jj );
CrashReport.breadcrumb( 4, jj );
}
tt = micros() - tt;
Serial.printf("\t%lu us\t for 1K increments of breadcrumb 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();
}