Forum Rule: Always post complete source code & details to reproduce any issue!
Page 2 of 2 FirstFirst 1 2
Results 26 to 33 of 33

Thread: Is there a hook to know when the on/off pin on the Teensy 4.0 has been grounded?

  1. #26
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,154
    @Defragster:
    I'm not sure what the "reserved" Bit24 does exactly, but it helps (and seems to work now):
    Code:
    void setup() {
      Serial.println(IMXRT_SNVS.offset100, HEX);
      Serial.println(IMXRT_SNVS.offset104, HEX);
      SNVS_LPCR |= (1<<24); //Enable NVRAM(?!) - undocumented
      IMXRT_SNVS.offset100 = 0xbeef;
      IMXRT_SNVS.offset104 = 0x1234;
      Serial.println(IMXRT_SNVS.offset100, HEX);
      Serial.println(IMXRT_SNVS.offset104, HEX);
    ... 
    reset or power down
    Note, Bit 24 must be set before write(?) - found it by try and error. Tested without battery - power dwn and reset only.
    I've fixed a little issue in the library (SNVS reset not needed), please download again. It's needed here.

    One should measure/verify the power consumption on power off - with bit 24 set and without.
    Could that it enables ways more..?
    Last edited by Frank B; 01-20-2020 at 08:22 PM.

  2. #27
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,154
    A demo for that:
    Code:
    #include "T4_PowerButton.h"
    
    void myCallback(void) {
      digitalWriteFast( 13, 1);
      Serial.println ("Callback called - Switching off now.");
      delay(100);
    }
    
    void setup() {
      pinMode(13, OUTPUT);
      Serial.begin(115200);
      while (!Serial && millis() < 4000 );
    
      if (arm_power_button_pressed()) {
        Serial.println("Restart after power down");
      } else {
        Serial.println("First power on");
      }
    
      Serial.println(IMXRT_SNVS.offset100, HEX);
      Serial.println(IMXRT_SNVS.offset104, HEX);
      SNVS_LPCR |= (1<<24); //Enable NVRAM(?!) - undocumented
      IMXRT_SNVS.offset100 = 0xbeef;
      IMXRT_SNVS.offset104 = 0x1234;
      Serial.println(IMXRT_SNVS.offset100, HEX);
      Serial.println(IMXRT_SNVS.offset104, HEX);
      set_arm_power_button_callback(&myCallback); //Start callback
    
    }
    
    uint32_t tm = millis();
    void loop() {
      digitalWriteFast( 13, !digitalReadFast(13));
      delay(100);
      if (millis() - tm > 10000) {
        Serial.println("\n\nReset!!!!!");
        delay(500);
        arm_reset();
      }
    }
    Last edited by Frank B; 01-20-2020 at 09:40 PM.

  3. #28
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,565
    … telling me this post is too short ... ???
    Quote Originally Posted by Frank B View Post
    @Defragster:
    I'm not sure what the "reserved" Bit24 does exactly, but it helps (and seems to work now):
    Code:
    void setup() {
      Serial.println(IMXRT_SNVS.offset100, HEX);
      Serial.println(IMXRT_SNVS.offset104, HEX);
      SNVS_LPCR |= (1<<24); //Enable NVRAM(?!) - undocumented
      IMXRT_SNVS.offset100 = 0xbeef;
      IMXRT_SNVS.offset104 = 0x1234;
      Serial.println(IMXRT_SNVS.offset100, HEX);
      Serial.println(IMXRT_SNVS.offset104, HEX);
    ... 
    reset or power down
    Note, Bit 24 must be set before write(?) - found it by try and error. Tested without battery - power dwn and reset only.
    I've fixed a little issue in the library (SNVS reset not needed), please download again. It's needed here.

    One should measure/verify the power consumption on power off - with bit 24 set and without.
    Could that it enables ways more..?
    THAT DOES IT - GREAT FIND Frank! I did some searches and scanning of NXP docs and I found no indications of what you explored to find.

    I can now write to those four DWORDS!

    Once that bit is set it stays while powered USB or RTC vBat. That bit auto resets on full power loss. But I don't see any trouble in always setting it.

    Here is sample code showing the two ways used to index to that 128 bits of NVRAM with 'NVRAM_UINT32':
    Code:
    // #define NVRAM_UINT32 ((uint32_t *)0x400D4100)
    uint32_t *NVRAM_UINT32 ((uint32_t *)0x400D4100);
    
    elapsedMillis tSome;
    uint32_t lOne = NVRAM_UINT32[0];
    void setup() {
    	SNVS_LPCR |= (1 << 24); //Enable NVRAM(?!) - undocumented
    	Serial.begin(115200);
    	while (!Serial && millis() < 4000 );
    	Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
    	tSome = 0;
    }
    void loop() {
    	if ( tSome >= 1000 ) {
    		tSome -= 1000;
    		for ( int ii = 0; ii < 4; ii++ ) {
    			Serial.print( "  " );
    			Serial.print( NVRAM_UINT32[ii], HEX );
    		}
    		Serial.print( "  diff =" );
    		Serial.print( NVRAM_UINT32[0] - lOne );
    		Serial.println();
    		lOne = NVRAM_UINT32[0];
    	}
    	for ( int ii = 0; ii < 4; ii++ ) {
    		NVRAM_UINT32[ii]++;
    	}
    }
    Here is sample output - the diff count is pretty low for updating for a second? I wonder how slow the access is?
    1118AD 1118AC 10764F 10764C diff =1365
    111E01 111E00 107BA3 107BA0 diff =1364
    112356 112355 1080F8 1080F5 diff =1365
    1128AB 1128AA 10864D 10864A diff =1365

  4. #29
    @Frank, thanks a lot for the library.
    You implemented the T4.0 software reset function that many people were looking for.
    Great work!

  5. #30
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,154
    It also allows a faster power-off with the button (use a empty callback).

    for the records: If you want to start the bootloader from your program: asm("bkpt #251");

  6. #31
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,154
    Tim,
    maybe it's so slow because it is clocked by the 32khz crystal.
    Just a guess.

  7. #32
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,565
    For those cases where expected Restart stops in bootloader - that may be on Paul's list to address as possible.

  8. #33
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,565
    Quote Originally Posted by Frank B View Post
    Tim,
    maybe it's so slow because it is clocked by the 32khz crystal.
    Just a guess.
    It must relate to that Frank.
    I did a quick edit to write only 1 of the four when updated from a RAM value {rather than read inc and write of all four NVRAM's} and it went to 10915 updates / second from 1365/sec.

Posting Permissions

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