According to official documentation the Teensy 4.1 comes with two RAM banks.
The RAM2 bank also called DMAMEM has the interesting property that it isn't reset after a warm reboot which can be triggered after press of the white button on the board or using watchdog timer.
For a larger project i want to keep track of the number of sequential warm reboots between cold reboots. For this i need to have the ability distinguish between the two types.
The Teensy will be used without a host computer connected to it. To test this functionality i adapted the teensy tutorial number 3 like this.
Using this code one observes:
Thus the count up mechanism works successfully however the starting point depends on a value in uninitialized value which can be arbitrary. If the Teensy 4.1 is power cycled it starts counting from a different place.
I am looking for a way to reliably detect a first boot or alternative detect a warm boot. Elsewhere in the forum I found this this snippet of code.
However it doesn't actually detect warm boots. It just compares an arbitrary constant with uninitialized memory. This test might mistake a cold boot for a warm boot if the uninitialized memory just happens to be initialized in the wrong way. While one could add more similar tests to reduce the likelihood of such a mistake one can't eliminate the chance this way and i am fundamentally dubious of having the correctness of the program depend on the behavior of uninitialized memory.
Is there a better approach to distinguish cold vs warm boot?
A better way would rely only on documented behavior and not undocumented aspects of proprietary software (such as undocumented behavior of closed-source boot loader which isn't asserted to remain the same) or proprietary hardware (mechanisms whose behaviour differs between warm reboot and cold reboot which aren't documented in official docs of the chip or the ISA).
EDIT: Replace with rewritten text used to create a duplicate because i was unaware that a partial question was submitted
EDIT2: Clarify i am interested in the number of soft reboots between power cycles not the total number of boots.
The RAM2 bank also called DMAMEM has the interesting property that it isn't reset after a warm reboot which can be triggered after press of the white button on the board or using watchdog timer.
For a larger project i want to keep track of the number of sequential warm reboots between cold reboots. For this i need to have the ability distinguish between the two types.
The Teensy will be used without a host computer connected to it. To test this functionality i adapted the teensy tutorial number 3 like this.
C:
void setup() {
static int DMAMEM bootcount = 0; // this is non-sensical
Serial.begin(38400);
delay(1000); // give user time to restart serial console to not miss first message.
Serial.printf("I've been rebooted %d times", bootcount);
bootcount += 1;
arm_dcache_flush(&bootcount, sizeof(bootcount)); // without this i've never observed the counter being incremented
void loop()
{
Serial.println("Hello World");
delay(1000);
}
Using this code one observes:
Flash code using Arduino IDE and connect to serial via the command
Bash:
cu -l /dev/ttyACM0 -s 38400
Connected.
I've been rebooted -1954828805 timesHello World
Hello World
Hello World
Hello World
...
After press white button to warm-restart the teensy code the serial connection dies
cu: Got hangup signal
Disconnected.
If one quickly reconnects to serial using the same command as before one sees
Connected.
I've been rebooted -1954828804 timesHello World
Hello World
Hello World
Hello World
....
Repeating the same cycle one observes
cu: Got hangup signal
Disconnected.
anabrid@think17:~$ cu -l /dev/ttyACM0 -s 38400
Connected.
I've been rebooted -1954828803 timesHello World
Hello World
Hello World
Hello World
cu: Got hangup signal
Disconnected.
Thus the count up mechanism works successfully however the starting point depends on a value in uninitialized value which can be arbitrary. If the Teensy 4.1 is power cycled it starts counting from a different place.
anabrid@think17:~$ cu -l /dev/ttyACM0 -s 38400
Connected.
I've been rebooted -1971638790 timesHello World
Hello World
Hello World
cu: Got hangup signal
^[[A
Disconnected.
anabrid@think17:~$ cu -l /dev/ttyACM0 -s 38400
Connected.
I've been rebooted -1971638789 timesHello World
Hello World
Hello World
Hello World
cu: Got hangup signal
Disconnected.
anabrid@think17:~$ cu -l /dev/ttyACM0 -s 38400
Connected.
I've been rebooted -1971638788 timesHello World
Hello World
Hello World
Hello World
Hello World
cu: Got hangup signal
Disconnected.
anabrid@think17:~$ cu -l /dev/ttyACM0 -s 38400
Connected.
I've been rebooted -1971638787 timesHello World
Hello World
Hello World
Hello World
cu: Got hangup signal
Disconnected.
I am looking for a way to reliably detect a first boot or alternative detect a warm boot. Elsewhere in the forum I found this this snippet of code.
C:
bool isWarmBoot()
{
static DMAMEM unsigned bootCheck; // DMAMEM is not zeroed during bootup
if (bootCheck != 0xAAAA'AAAA)
{
bootCheck = 0xAAAA'AAAA; // some number
arm_dcache_flush(&bootCheck, sizeof(bootCheck)); // dmamem is cached, force a write to memory
return false;
}
return true;
}
However it doesn't actually detect warm boots. It just compares an arbitrary constant with uninitialized memory. This test might mistake a cold boot for a warm boot if the uninitialized memory just happens to be initialized in the wrong way. While one could add more similar tests to reduce the likelihood of such a mistake one can't eliminate the chance this way and i am fundamentally dubious of having the correctness of the program depend on the behavior of uninitialized memory.
Is there a better approach to distinguish cold vs warm boot?
A better way would rely only on documented behavior and not undocumented aspects of proprietary software (such as undocumented behavior of closed-source boot loader which isn't asserted to remain the same) or proprietary hardware (mechanisms whose behaviour differs between warm reboot and cold reboot which aren't documented in official docs of the chip or the ISA).
EDIT: Replace with rewritten text used to create a duplicate because i was unaware that a partial question was submitted
EDIT2: Clarify i am interested in the number of soft reboots between power cycles not the total number of boots.
Last edited: