1

I am learning embedded systems on the ARM9 processor (SAM9G20). I am more familiar with procedural programming for general purpose. Thus what I am doing is going through the data sheet and learning what registers there are and how to manipulate them.

My question is, how do I know when the computer reset? I know that there is a Reset Controller that manages resets. A register called the Status Register (RSTC_SR) stores the source of the reset. Do I need to keep periodically reading this register?

My solution is to store the number of resets in the FRAM (or start by setting it to 0), once a reset happens, I compare this variable with the register value in my main function. If the register value is higher then obviously it reset. However I am sure there is a more optimized way (perhaps using interrupts). Or is this how its usually done?

Community
  • 1
  • 1
Sarah cartenz
  • 1,313
  • 2
  • 14
  • 37
  • 3
    You would know that the system was reset when your system start up code is called. – Some programmer dude Feb 18 '19 at 11:49
  • I'm reading in the data-sheet (13.4.3) that it's possible to manage ISR in function of certain kind of resets. – Sir Jo Black Feb 18 '19 at 11:58
  • You need to study _what_ a reset is, first of all, for any of this to have a meaning. At reset, all hardware, all registers and the program counter are restored to execute from the function programmed at the reset vector (the 32 bit address written at address 0 of an ARM). This is the definition of a reset. – Lundin Feb 18 '19 at 13:03
  • @SirJoBlack : 13.4.3 refers only to the NRST pin that can be configured as a reset pin to an interrupt pin - if it is configured as an interrupt, it is not really a reset - although the interrupt handler may issue a reset. You could then use that to count or log the reset pin being asserted, but there are other sources of reset that would not be counted. So I am not sure that helps. – Clifford Feb 18 '19 at 16:01
  • The SAM9G20 is classified by Atmel/Microchip as a MPU rather than a *"micro-controller"*. It has a MMU, and typically executes its boot ROM, then a bootstrap, then U-Boot, and then Linux after a reset. – sawdust Feb 20 '19 at 09:17

4 Answers4

2

A simple solution is to exploit the structure of your code. Many code bases for embedded take this form:

int main(void)
{
  // setup stuff here
  while (1)
  {
    // handle stuff here
  }
  return 0;
}

You can exploit that the code above while(1) is only run once at startup. You could increment a counter there, and save it in non-volatile storage. That would tell you how many times the microcontroller has reset.

Another example is on Arduino, where the code is structured such that a function called setup() is called once, and a function called loop() is called continuously. With this structure, you could increment the variable in the setup()-function to achieve the same effect.

Morten Jensen
  • 5,818
  • 3
  • 43
  • 55
  • What if there is a reset between the reset ISR and the pointer where main is called? If resets happen, that's a quite likely place for them. – Lundin Feb 18 '19 at 13:06
2

You do not need to periodically check, since every time the machine is reset your program will re-start from the beginning.

Simply add checks to the startup code, i.e. early in main(), as needed. If you want to figure out things like how often you reset, then that is more difficult since typically (no experience with SAMs, I'm an STM32 type of guy) on-board timers etc will also reset. Best would be some kind of real-world independent clock, like an RTC that you can poll and save the value of. Please consider if you really need this, though.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    possibly even _before_ `main()` - a lot of code and possibly interrupts may run before main which may itself force a reset. Ideally before any interrupts occur or any code that may cause a reset or get stuck and force a watchdog. – Clifford Feb 18 '19 at 15:49
  • 1
    @Clifford I was kind of assuming a sensible platform that doesn't come out of reset with any particular interrupts enabled. Which micros do that? – unwind Feb 19 '19 at 08:33
  • None. That is not what I suggested. The start-up code that runs before main performs standard library initialisation, including I/O and timing which may utilise interrupts. – Clifford Feb 19 '19 at 09:17
1

Whenever your processor starts up, it has by definition come out of reset. What the reset status register does is indicate the source or reason for the reset, such as power-on, watchdog-timer, brown-out, software-instruction, reset-pin etc.

It is not a matter of knowing when your processor has reset - that is implicit by the fact that your code has restarted. It is rather a matter of knowing the cause of the reset.

You need not monitor or read the reset status at all if your application has no need of it, but in some applications perhaps it is a useful diagnostic for example to maintain a count of various reset causes as it may be indicative of the stability of your system software, its power-supply or the behaviour of the operators. Ideally you'd want to log the cause with a timestamp assuming you have an suitable RTC source early enough in your start-up. The timing of resets is often a useful diagnostic where simply counting them may not be.

Any counting of the reset cause should occur early in your code start-up before any interrupts are enabled (because an interrupt may itself cause a reset). This may require you to implement the counters in the start-up code before main() is invoked in cases where the start-up code might enable interrupts - for stdio or filesystem support fro example.

Clifford
  • 88,407
  • 13
  • 85
  • 165
0

A way to do this is to run the code in debug mode (if you got a debugger for the SAM). After a reset the program counter(PC) points to the address where your code starts.

Fuzi
  • 1
  • 3
  • That is kind of an answer to the _title_ of the question, but the body of the question is a little more detailed, and this is not an answer to that I think. – Clifford Feb 18 '19 at 15:56
  • You might be true, but to me it's not clear if the goal is logging the reset state or simply 'see' if a reset occurs. And for the second I think my answer is an easy way to achieve that goal. – Fuzi Feb 19 '19 at 11:27