issue to reporogram T4.0

WMXZ

Well-known member
I have the following scenario
T4 with coin cell for RTC connected to PC-USB. (T4 is soldered to revD audioboard, no external power)

SW shuts down Teensy (turns off power). (SNVS_LPCR |=(1<<6) ; ) (T4 will be woken-up later by RTC alarm)

At this point T4 cannot be reprogrammed anymore
no reaction to
- button-press
- hold botton while disconnect and reconnect PC-USB

To reprogram T4, I need to disconnect coin cell
and hold botton while disconnect and reconnect PC-USB (red light shows bootloader activity)

wakeup-of T4 by connecting shortly reset-pin to ground, works.

It seems existing external VBAT interferes with boot-process
not sure if this is a 'feature' of my program (SNVS setting) or of the boot process.
 
Similar will happen using the POWER pad/button. When the T4 goes to sleep the unit is offline with regard to bootloader and active code access/operation. And the active RTC Cell power 'enforces' this, likewise some number of I/O pins held at 3.3V will leak current to the RTC Low Power circuit and act like the RTC Cell is present even when it is not, i.e. maintain POWER Off state.

I'm assuming the (SNVS_LPCR |=(1<<6) ; ) sleep for RTC alarm uses the same mechanism - where 3.3V is shut down and the only processor part active is the low power RTC section that watches for the POWER button - and these RTC Alarm (other?) conditions.

By any chance does the POWER button held GND for up to a second wake the T4?

Probably/Possibly not if the RTC Alarm wake is absolute?

Does a 5+ second POWER held GND … Telling T4 to turn off

Followed by :: POWER button held GND for up to a second wake the T4?

If the long press POWER down shuts down any more of the low power core it might bypass the RTC Alarm wake - and then POWER ON short press might restart the T4 from scratch setup restart?

If that doesn't work is there an option to add the POWER button (or other) to the active list during RTC Alarm sleep?
 
yes, on/off (reset) line works (nearly) as expected

T4 running: long (>5s) on/off line to GND switches T4 off , BUT T4 comes on again immediately (I was still grounding on-off line)
T4 off: short on/off line to GND switches T4 on

Not sure what should happen if on/off line is grounded for more than the necessary time for a running T4?
 
yes, on/off (reset) line works (nearly) as expected

T4 running: long (>5s) on/off line to GND switches T4 off , BUT T4 comes on again immediately (I was still grounding on-off line)
T4 off: short on/off line to GND switches T4 on

Not sure what should happen if on/off line is grounded for more than the necessary time for a running T4?

Was this test done when in the p#1 RTC Alarm pending sleep state? Not clear? But I was wondering if the POWER Button will effectively cancel the Alarm Sleep and allow return to operation in 5+1 seconds without removing the RTC cell?

I'd try it but code sketch wasn't posted. In setup() { while(!Serial); Serial.Print("SETUP() … USB something to set alarm"); while( !Serial.available()); : … // prior code to set up the RTC alarm

As far as GND on Power - it seems here as expected until the GND is removed {voltage rises} any press and HOLD to GND will change state only once.
 
This was a recorder program, but will try is I could generate a minimal program and post, using my hibernate program during T4beta test
 
here is a test Code
Code:
#include "core_pins.h"
#include "TimeLib.h"

/******************* Seting Alarm ***************/
#define SNVS_LPSR_PGD_MASK              (0x8U)
#define SNVS_LPCR_LPTA_EN_MASK          (0x2U)

void rtc_init() 
{ 
  CCM_CCGR2 |= CCM_CCGR2_IOMUXC_SNVS(CCM_CCGR_ON);
  SNVS_LPGPR = SNVS_DEFAULT_PGD_VALUE;
  SNVS_LPSR = SNVS_LPSR_PGD_MASK;
  // ? calibration
  // ? tamper pins
  
  SNVS_LPCR &= ~SNVS_LPCR_LPTA_EN_MASK; // clear alarm
  while (SNVS_LPCR & SNVS_LPCR_LPTA_EN_MASK); 
  SNVS_LPTAR=0;

  SNVS_LPCR |= 1;             // start RTC
  while (!(SNVS_LPCR & 1));
}

void rtc_set_time(uint32_t secs) 
{ //uint32_t secs = 1547051415;
  SNVS_LPCR &= ~1;   // stop RTC
  while (SNVS_LPCR & 1);
  SNVS_LPSRTCMR = (uint32_t)(secs >> 17U);
  SNVS_LPSRTCLR = (uint32_t)(secs << 15U);
  SNVS_LPCR |= 1;             // start RTC
  while (!(SNVS_LPCR & 1));
}

uint32_t rtc_secs() {
  uint32_t seconds = 0;
  uint32_t tmp = 0;

  /* Do consecutive reads until value is correct */
  do
  { seconds = tmp;
    tmp = (SNVS_LPSRTCMR << 17U) | (SNVS_LPSRTCLR >> 15U);
  } while (tmp != seconds);

  return seconds;
}

void rtc_stopAlarm()
{
  SNVS_LPSR |= 1;
  SNVS_LPCR &= ~SNVS_LPCR_LPTA_EN_MASK;
  while (SNVS_LPCR & SNVS_LPCR_LPTA_EN_MASK); 
}
void rtc_isr(void)
{ 
  SNVS_LPSR |= 1;
//  Serial.println("Alarm");
  asm volatile ("DSB");
}

void rtc_initAlarm(uint32_t prio=13)
{
  SNVS_LPSR |= 1;
  attachInterruptVector(IRQ_SNVS_IRQ, rtc_isr);
  NVIC_SET_PRIORITY(IRQ_SNVS_IRQ, prio*16); // 8 is normal priority
  NVIC_DISABLE_IRQ(IRQ_SNVS_IRQ);
}

void rtc_setAlarm(uint32_t alarmSeconds)
{   uint32_t tmp = SNVS_LPCR; //save control register

    /* disable SRTC alarm interrupt */
    rtc_stopAlarm();

    SNVS_LPTAR=alarmSeconds;
    while(SNVS_LPTAR != alarmSeconds);

    NVIC_ENABLE_IRQ(IRQ_SNVS_IRQ);

    SNVS_LPCR = tmp | SNVS_LPCR_LPTA_EN_MASK; // restore control register and set alarm
    while(!(SNVS_LPCR & SNVS_LPCR_LPTA_EN_MASK)); 
}

uint32_t rtc_getAlarm()
{
  return SNVS_LPTAR;  
}

void doShutdown(void) 
{ 
//  SNVS_LPCR |=(1<<3) ; // enable wake-up
  SNVS_LPCR |=(1<<6) ; // turn off power
  while(1) continue;
}

void setWakeupCallandSleep(uint32_t nsec)
{
  uint32_t to=now();
  if(!(SNVS_LPCR & 1))
  { rtc_init();
    rtc_set_time(to);   //LPSRTC will start at 0 otherwise
  }
  rtc_initAlarm(4);
  rtc_setAlarm(to+nsec);
  doShutdown();
}


uint32_t blink(void)
{ static uint32_t tx0;
  if(millis()>tx0+1000) 
  { digitalWriteFast(13,!digitalReadFast(13));
    tx0=millis();
    Serial.println("blink");
    return 1;
  }
  return 0;
}

void setup() {
  Serial.begin(9600);
  while (!Serial);
  
  // set the Time library to use Teensy 3.0's RTC to keep time
  setSyncProvider(Teensy3Clock.get);
  delay(100);
  if (timeStatus()!= timeSet) 
    Serial.println("Unable to sync with the RTC");
  else 
    Serial.println("RTC has set the system time");
  //
  Serial.println("Start");
  pinMode(13,OUTPUT);
  
}

void loop() {
  static uint32_t ic=0;
  ic += blink();
  if(ic>=10)
  { Serial.println("End"); Serial.flush();
    delay(1000);
    setWakeupCallandSleep(10);
  }
}
 
Running that code on the PJRC Beta final breakout with RTC Cell it seems to work - and hitting the POWER button does seem to override the RTC Sleep Alarm.

@WMXZ - on your end running that sketch - can you see how it interacts with steps in post #2 as a way to not pull the battery.
 
Running the code - with the call to :: setWakeupCallandSleep(10);

The GREEN power LED on the T4 Beta Breakout goes out for the 10 second Sleep Alarm wake cycle.

Pressing the breakout POWER button a half second immediately turns the power back on, entering setup() - the same as if the Alarm time had expired.

So the solution after "SW shuts down Teensy (turns off power). (SNVS_LPCR |=(1<<6) ; )" is:

To reprogram T4 - without removing the RTC Cell power:
- press and hold the PGM Button
- Press POWER button, i.e. GND the POWER pin half a second
- release the POWER Button
- release the PGM Button
>> Teensy 4 is in Bootloader.

This is on PJRC Beta Breakout with POWER button and GREEN LED powered by T4 3.3V output that goes out when the T4 enters Sleep/Power Off.

This was an expected 'Possible' outcome noted in Post #2. The RTC Alarm wake is just a 'timed' Power Off - and Power Button can also wake the T4.
 
This is what one would could easily implement also in an embedded system, bring out both lines (program and power on/off lines)

Now, I found another "feature":

RTC coin cell connected, program is running: pressing power on/off button for >5 sec, program shuts down AND restart immediately (i.e. power cycles)
 
This is what one would could easily implement also in an embedded system, bring out both lines (program and power on/off lines)

Now, I found another "feature":

RTC coin cell connected, program is running: pressing power on/off button for >5 sec, program shuts down AND restart immediately (i.e. power cycles)

That is not something I have ever observed previously - unless there is bounce on the Power GND line - when there was no Wake timer set.

This may be caused by a 'residual' active RTC Alarm Timer detected as expiring. The Wake Timer was interrupted with the Overt POWER On, but nothing cleared that RTC timer? So on the next Power Off the low power unit will check status and see that it was supposed to WAKE the T4?

With above posted Code I can see this - If the T4 goes to Sleep I can Power On the T4. Then on the next Power Off sequence it immediately wakes.

Can one differentiate why T4 arrived in setup(), and can one see in setup() detecting or clearing an in progress RTC wake Timer?

If status of either or both of those items could be displayed in setup() that would be something to work with. Also Could the RTC Real Time be printed by this code? I've never gotten the hang of this yet - my attempts - even with Teensy Time set I keep coming up as :: :00::02::05 :01 :01 1970

With status of a pending RTC wake that would explain the immediate Power On as soon as it goes OFF and the lp_RTC unit gets control. Also being able to print the RTC would allow pasting SerMon Output showing running clock and button press effects on time passed.
 
So, I changed running phase to 20 blinks, and after start I connected permanently on/off to GND
after first 5 seconds program power cycled, but then grounded power on/off had NO effect anymore (program blinks for 20 secs and sleeps for 10 secs)
Edit: my hunch is that the way I'm using SNVS is interfering with power on/off setting.
 
So, I changed running phase to 20 blinks, and after start I connected permanently on/off to GND
after first 5 seconds program power cycled, but then grounded power on/off had NO effect anymore (program blinks for 20 secs and sleeps for 10 secs)
Edit: my hunch is that the way I'm using SNVS is interfering with power on/off setting.

That was my post #2 guess. The POWER and SNVS?/Timer share the LP unit.

> Sleeping T4 wakes on Power GND'ing
> Power on doesn't clear or reset the SNVS timeout so the next Power Off triggers it back on.

@WMXZ - so far I'm not sure a way to RESET the T4 has been found - from posts it seems executing the expected process results in going to Bootloader?
> Sounds like this could be abused to make a reset. Set WAKE timer to shortest possible value - execute 'Power Down' - though it may require RTC to work?


There is something BAD here - another odd state:
> Power with posted code
> Allow it to cycle 10 sec and shut down
> Allow it to restart with start and 1 sec SerMon print
> Press and hold POWER BUTTON 5s
> It shuts down in a few secs and it now in an odd state - when it GOES OFF - the PJRC Green POWER LED stays LIT now? The 3.3V segment is not shutting down after doing this code with the : SNVS_LPCR |=(1<<6)
- yet the Teensy goes missing ( using TyCommander as SerMon )
> It comes to life again with a 5s press of the POWER button?

When it returns to running the posted code the 3v3 Powered PJRC breakout LED goes on and off with the code running as expected.

@WMXZ - did you get a final PJRC Breakout with RTC , Power button, Green LED?
 
@WMXZ - is there an UNDO of the 'SNVS_LPCR |=(1<<6) ' change that would make the beta breakout Power button act like it seemed to before - turning off the 3v3 LED? The other beta breakouts didn't have RTC Cell holder.
 
@WMXZ - did you get a final PJRC Breakout with RTC , Power button, Green LED?
Not sure (I did not had time to assemble the latest Breakout board). I will check this evening.

I did my tests with a recent T4 purchase (from PJRC).
For my latest test, I physically connected ON/OFF to GND and let it there: apart from first shutdown, T4 was the ignoring this pin.

I expected
teensy shuts down after 5 sec and will stay shut, as alarm was not set yet.
it may however be that alarm-enable and an earlier alarm time was still set (sure it is as VBAT maintains state) and therefore wakes-up T4 immediately.
Maybe startup or setup could clear SNVS_LP alarm settings.

Not clear yet, why after this (un)expected wake-up, T4 ignores in the following the on/off pin and runs as programmed.
it may be that in order to activate off-counter, on/off pin has to be released (going high)
this means you cannot keep T4 from running with on/off pin simply set to GND (while RTC coin is attached)
 
Maybe startup or setup could clear SNVS_LP alarm settings.
inserting as first call into setup()
Code:
void rtc_resetAlarm()
{
  SNVS_LPCR &= ~SNVS_LPCR_LPTA_EN_MASK;
  while (SNVS_LPCR & SNVS_LPCR_LPTA_EN_MASK); 
}

and called as
Code:
void setup() {
  rtc_resetAlarm();
....
}

resolves the issue of dangling "RTC alarm enable"

now if I connect on/off line to ground, program stops after 5 secs and remains shut.
 
Tried rtc_stopAlarm(); yesterday and that wasn't the ticket - not sure that rtc_resetAlarm(); doing the trick either to fully reset the Alarm system?

I pulled RTC Cell and held buttons - repowered USB & Cell and on power up the time was GONE showing I cleared the RTC section.

Upped the loop() count to 14 - and did POWER Off before it expired and as before the LED on Beta PJRC Breakout goes OFF when the T4 goes Off.

Started Code and let it cycle and the LED goes Off on setWakeupCallandSleep(), let it wake
> then interrupted a cycle with OFF before sleep and T4 goes OFF - but LED stays ON - showing the 3V3 not turned off?

It seems there is some interplay and more involved with the Sleep and Power through the LP section?

The POWER is designed to be normally high - like T_3.x's Reset - so holding it LOW is odd - and as observed even Held low the T4 will wake when Sleep time expires.
 
Tried rtc_stopAlarm(); yesterday and that wasn't the ticket - not sure that rtc_resetAlarm(); doing the trick either to fully reset the Alarm system?
Not sure anymore.
Put T4 on powered USB hub, and new confusing issue.
Also used progmem to keep rtc_reset, rtc_stopAlarm, rtc_isr, so their code does not disappear when shutting down (I hope)

after shutdown, program stops, restarts and ends when asked to shutdown, but this time USB does not disconnect (Term stays online, 40 mA is flowing to Teensy, reduced from 80 mA) and T4 remains disconnected

Even with power on/off remaining connected to GND, pressing button reloads program and runs twice, after that program terminates as before.

Completely lost here, but to be honest, this is not a problem for my application, but ... and I like to know.

actual code
Code:
#include "core_pins.h"
#include "TimeLib.h"

/******************* Seting Alarm ***************/
#define SNVS_LPSR_PGD_MASK              (0x8U)
#define SNVS_LPCR_LPTA_EN_MASK          (0x2U)

void rtc_init() 
{ 
  CCM_CCGR2 |= CCM_CCGR2_IOMUXC_SNVS(CCM_CCGR_ON);
  SNVS_LPGPR = SNVS_DEFAULT_PGD_VALUE;
  SNVS_LPSR = SNVS_LPSR_PGD_MASK;
  // ? calibration
  // ? tamper pins
  
  SNVS_LPCR &= ~SNVS_LPCR_LPTA_EN_MASK; // clear alarm
  while (SNVS_LPCR & SNVS_LPCR_LPTA_EN_MASK); 
  SNVS_LPTAR=0;

  SNVS_LPCR |= 1;             // start RTC
  while (!(SNVS_LPCR & 1));
}

void rtc_set_time(uint32_t secs) 
{ //uint32_t secs = 1547051415;
  SNVS_LPCR &= ~1;   // stop RTC
  while (SNVS_LPCR & 1);
  SNVS_LPSRTCMR = (uint32_t)(secs >> 17U);
  SNVS_LPSRTCLR = (uint32_t)(secs << 15U);
  SNVS_LPCR |= 1;             // start RTC
  while (!(SNVS_LPCR & 1));
}

uint32_t rtc_secs() {
  uint32_t seconds = 0;
  uint32_t tmp = 0;

  /* Do consecutive reads until value is correct */
  do
  { seconds = tmp;
    tmp = (SNVS_LPSRTCMR << 17U) | (SNVS_LPSRTCLR >> 15U);
  } while (tmp != seconds);

  return seconds;
}

PROGMEM void rtc_resetAlarm()
{
  SNVS_LPCR &= ~SNVS_LPCR_LPTA_EN_MASK;
  while (SNVS_LPCR & SNVS_LPCR_LPTA_EN_MASK); 
}

PROGMEM void rtc_stopAlarm()
{
  SNVS_LPSR |= 1;
  rtc_resetAlarm();
}
PROGMEM void rtc_isr(void)
{ 
  rtc_stopAlarm();
  
  SNVS_LPSR |= 1;
//  Serial.println("Alarm");
  asm volatile ("DSB");
}

void rtc_initAlarm(uint32_t prio=13)
{
  SNVS_LPSR |= 1;
  attachInterruptVector(IRQ_SNVS_IRQ, rtc_isr);
  NVIC_SET_PRIORITY(IRQ_SNVS_IRQ, prio*16); // 8 is normal priority
  NVIC_DISABLE_IRQ(IRQ_SNVS_IRQ);
}

void rtc_setAlarm(uint32_t alarmSeconds)
{   uint32_t tmp = SNVS_LPCR; //save control register

    /* disable SRTC alarm interrupt */
    rtc_stopAlarm();

    SNVS_LPTAR=alarmSeconds;
    while(SNVS_LPTAR != alarmSeconds);

    NVIC_ENABLE_IRQ(IRQ_SNVS_IRQ);

    SNVS_LPCR = tmp | SNVS_LPCR_LPTA_EN_MASK; // restore control register and set alarm
    while(!(SNVS_LPCR & SNVS_LPCR_LPTA_EN_MASK)); 
}

uint32_t rtc_getAlarm()
{
  return SNVS_LPTAR;  
}

void doShutdown(void) 
{ 
//  SNVS_LPCR |=(1<<3) ; // enable wake-up
  SNVS_LPCR |=(1<<6) ; // turn off power
  while(1) continue;
}

void setWakeupCallandSleep(uint32_t nsec)
{
  uint32_t to=now();
  if(!(SNVS_LPCR & 1))
  { rtc_init();
    rtc_set_time(to);   //LPSRTC will start at 0 otherwise
  }
  rtc_initAlarm(4);
  rtc_setAlarm(to+nsec);
  doShutdown();
}


uint32_t blink(void)
{ static uint32_t tx0;
  if(millis()>tx0+1000) 
  { digitalWriteFast(13,!digitalReadFast(13));
    tx0=millis();
    Serial.println("blink");
    return 1;
  }
  return 0;
}

void setup() {
  rtc_init();
  Serial.begin(9600);
  while (!Serial);
  
  // set the Time library to use Teensy 3.0's RTC to keep time
  setSyncProvider(Teensy3Clock.get);
  delay(100);
  if (timeStatus()!= timeSet) 
    Serial.println("Unable to sync with the RTC");
  else 
    Serial.println("RTC has set the system time");
  //
  Serial.println("Start");
  pinMode(13,OUTPUT);
  
}

void loop() {
  static uint32_t ic=0;
  ic += blink();
  if(ic>=20)
  { Serial.println("End"); Serial.flush();
    delay(1000);
    setWakeupCallandSleep(10);
  }
}
 
Odd - I put your code on here an dit ran 3 cycles and is stopped? with power LED on :
Code:
blink @ms=30030 :: [c] 14:57:50 11/25/2019
End
RTC has set the system time
Start
blink
…
blink
End
RTC has set the system time
Start
blink
… 
blink
End
RTC has set the system time
Start
blink
…
blink
End

That was after swapping out the sample edit here showing time/date …

So it was powered and online - but not running?

Removed USB power and restarted - only made two passes and as above quit with 3V3 LED on …

Reloaded sketch after pulling USB and RTC Cell - started and completed 2 cycles - the LED should have gone off with 'End' but it did not - so like it didn't fully Sleep. Same on repeating.


Not sure what changed in new posting? Reverted to the old edited code here and has completed a full 8 passes of 30 secs and continues. Will look at merging and post this code with RTC Time printed

Code:
..
blink @ms=28028 :: [c] 16:39:13 11/25/2019
blink @ms=29029 :: [c] 16:39:14 11/25/2019
blink @ms=30030 :: [c] 16:39:15 11/25/2019
End
Start :: RTC has set the system time :: [c] 16:39:26 11/25/2019
blink @ms=1001 :: [c] 16:39:26 11/25/2019
blink @ms=2002 :: [c] 16:39:27 11/25/2019
blink @ms=3003 :: [c] 16:39:28 11/25/2019
blink @ms=4004 :: [c] 16:39:29 11/25/2019
...
 
@WMXZ - it isn't the PROGMEM on three func()'s. I put that on my func()'s and it is working past two iterations.

Found it - adding the call to stopAlarm in the _isr() - causes prior code to now halt on the second pass after printing 'End':
Code:
PROGMEM void rtc_isr(void)
{
[B][COLOR="#FF0000"]  rtc_stopAlarm();
[/COLOR][/B]
  SNVS_LPSR |= 1;
  //  Serial.println("Alarm");
  asm volatile ("DSB");
}

This line is redundant to the stopAlarm - but removing that doesn't keep it running: SNVS_LPSR |= 1;

IT is the end call to rtc_resetAlarm(); that halts in the _isr() if the print "Alarm" is enabled that shows after end where it halts when the reset is called.

edit: BTW> not sure the value of PROGMEM? If the Teensy 4 starts it is in the power up state and nothing runs until any code is copied from FLASH to RAM - though this isn't the source of the trouble as it stops the same with no PROGMEM.

EDIT #2:: It takes this in setup() :: rtc_stopAlarm();

To make the Power button work properly and not Auto On during the long press - and return the breakout green LED to OFF when Power is taken to Off.
I put the rtc_stopAlarm(); at setup() end before pinMode(13...
 
Last edited:
@WMXZ - above I noted this { post #12 } with a 2 second turnaround could be a way to RESET/Restart the T4 - which I've not seen yet?

There is a thread pjrc.com/threads/29607-Over-the-air-updates where such a reset would be handy after putting fresh code in the T4 flash.

Posted here Over-the-air-updates is the sketch from here as I left it - with a way to trigger the 2 second update, but that code has lots of RTC and other (beyond the gratuitous UI) code that I'm not sure is needed. With your understanding could you boil down the needed setup and trigger code to effectively do Reset() { setWakeupCallandSleep(2); } ?

Did all the needed info come from the T4 RefMan? Was there an app note with added details?
 
@defragster,
Sure you can minimize code.
The original version was done during T4-beta. There was work by someone else before, but I only used T4 RefMan.
 
@defragster,
Sure you can minimize code.
The original version was done during T4-beta. There was work by someone else before, but I only used T4 RefMan.

Looking across the forum these notes came up in T4 Beta thread:

FRANKB : https://forum.pjrc.com/threads/54711-Teensy-4-0-First-Beta-Test?p=210084&viewfull=1#post210084
FLASHMEM __attribute__((noreturn))
void switch_core_off(void) {SNVS_LPCR |= (1<<6);exit(0);}

WMXZ : https://forum.pjrc.com/threads/54711-Teensy-4-0-First-Beta-Test?p=195382&viewfull=1#post195382
Added alarm on LPRTC (official manual does not give information but CMSIS/Keil etc give other addresses)
OK, got Alarm firing
AND
LPRTC Alarm can indeed be used to restart program after "Turn off System Power" command
Without additional actions (clock etc) consumption drops from 80 mA while running to 25 mA while System Power Off.

1/12/19 original code WMXZ: https://forum.pjrc.com/threads/54711-Teensy-4-0-First-Beta-Test?p=195387&viewfull=1#post195387
 
Anyhow,
here is a stripped down version
Code:
#include "core_pins.h"
#include "TimeLib.h"

/******************* Seting Alarm ***************/
#define SNVS_LPCR_LPTA_EN_MASK          (0x2U)

void rtc_stopAlarm()
{
  SNVS_LPSR |= 1;

  // disable alarm
  SNVS_LPCR &= ~SNVS_LPCR_LPTA_EN_MASK;
  while (SNVS_LPCR & SNVS_LPCR_LPTA_EN_MASK); 
  // clear alarm value
  SNVS_LPTAR=0;
  while(SNVS_LPTAR != 0);
}

void rtc_isr(void)
{ 
  SNVS_LPSR |= 1;
  Serial.println("Alarm"); Serial.flush();// only to tell if ISR is called
  asm volatile ("DSB");
}

void rtc_setAlarm(uint32_t alarmSeconds, uint32_t prio=13)
{   uint32_t tmp = SNVS_LPCR; //save control register

    /* disable SRTC alarm interrupt */
    rtc_stopAlarm();

    SNVS_LPTAR=alarmSeconds;
    while(SNVS_LPTAR != alarmSeconds);

    attachInterruptVector(IRQ_SNVS_IRQ, rtc_isr);
    NVIC_SET_PRIORITY(IRQ_SNVS_IRQ, prio*16); // 8 is normal priority
    NVIC_DISABLE_IRQ(IRQ_SNVS_IRQ);
    NVIC_ENABLE_IRQ(IRQ_SNVS_IRQ);

    SNVS_LPCR = tmp | SNVS_LPCR_LPTA_EN_MASK; // restore control register and set alarm
    while(!(SNVS_LPCR & SNVS_LPCR_LPTA_EN_MASK)); 
}

uint32_t rtc_getAlarm()
{
  return SNVS_LPTAR;  
}

void doShutdown(void) 
{ 
  SNVS_LPCR |=(1<<6) ; // turn off power
}

void setWakeupCallandSleep(uint32_t nsec)
{
  uint32_t to = now();
  rtc_setAlarm(to+nsec,4);
  doShutdown();
  while(1) asm("wfi");
}


uint32_t blink(void)
{ static uint32_t tx0;
  if(millis()>tx0+1000) 
  { digitalWriteFast(13,!digitalReadFast(13));
    tx0=millis();
    Serial.println("blink");
    return 1;
  }
  return 0;
}

void setup() {
  Serial.begin(9600);
  while (!Serial);
  
  // set the Time library to use Teensy 3.0's RTC to keep time
  setSyncProvider(Teensy3Clock.get);
  delay(100);
  if (timeStatus()!= timeSet) 
    Serial.println("Unable to sync with the RTC");
  else 
    Serial.println("RTC has set the system time");
  //
  Serial.println("Start");
  rtc_stopAlarm();
  pinMode(13,OUTPUT);
  
}

void loop() {
  static uint32_t ic=0;
  ic += blink();
  if(ic>=20)
  { Serial.println("End"); Serial.flush();
    delay(1000); // to let USB write last line
    setWakeupCallandSleep(2);
  }
}

I still do not understand how the ISR could print out from time to time (not always)
smallest value I found was 2 seconds, with 1 second program did not start-up.
 
When I turned on the _isr() "Alarm" I was seeing it with prior code - but timing may have changed and USB may get lost on exit?

Good to see it pared down some and working - I wasn't sure how much of that was really needed to get RTC active and usable to trust Sleep to Wake on Cue.

Indeed - '0' won't work - and '1' sec often worked - but if on boundary of a TICK - it may act like a '0'. So my test posted on other thread used (2) for that reason as the best I found.
 
What I not understand is the following:
I tell SNVS to wake-up the processor, say in 1 minute.
I expect that alarm ISR is called after 1 minute, but MCU is not running, therefore alarm ISR can not run.
Also, power off means all RAM (including alarm ISR code) is lost.
So what the purpose of ISR?
commenting attachInterruptVector seems not work (no wake-up). I.e. the default ISR that has no "SNVS_LPSR |= 1;" and therefore seems not to work.
It seems that the alarm interrupt is called immediately after being enabled, but the wake-up is at the indicated time.

Edit, as '+' member could you please edit the title of this thread and remove the disturbing 'o'?
 
Back
Top