Lecture 33 - Robustness Robustness generally refers to the capability of a micro-controller or its software to deal with unexpected events, such as faults and loss of power. Robustness is expected, especially from microcontrollers that operate in harsh environments such as industrial control, power and car electronics, and so on. We will first discuss two features that you will find in almost any microcontroller - brown out and watchdog timers. After that, we discuss how you can write software that improves the robustness of the application. 10:10 Review Solution Midterm 2 10:15 Brown Out When the operating voltage of a microcontroller drops below a given value, it becomes unstable and unpredictable. This is called a 'brown out' situation. It has to be avoided at all cost. Here are some of the things that can go wrong. 1/ CPU registers can clock a wrong value. This can even affect the program counter, which means that the program is essentially unpredictable. 2/ Peripheral registers can clock an unknown value, which means their functionality can make arbitrary jumps: - UART changing baudrate and line format - Interrupt request flags set/reset at random instants - I2C slave address randomly changing - etc etc (basically, unpredictable effects) 3/ I/O pins can randomly turn from input to output; I/O pins can randomly change value. On a PCB, that can lead to serious trouble (eg short circuit with a pin turned output. 4/ RAM values can randomly change. This implies data corruption (C variables randomly changing) or code corruptions (instructions randomly changing), 5/ While an NVM memory by itself will not loose its value upon power loss, a micro-controller that acts unpredicatbly may cause unexpected changes and overwrite NVM locations. All of these situations have to be avoided. The root problem is that a gradual reduction of power supply causes a gradual reduction of transistor capabilities and a gradual reduction of the noise margins. As power slowly ramps down from 3v3 to 0v, there is a transition period where the chip may still work 'somewhat'. That must be avoided. The solution is to shut the chip down when the voltage drops below a certain level. This shut down is firm and decisive. The event of loosing power is called BROWN-OUT, and the reset as a result of loosing power is called a BROWN-OUT RESET. A brown-out typically defines two voltage levels: - The voltage level where the chip is shut down, when the voltage is falling. This takes the form a reset vector. - The voltage level where the chip is rebooted again, when the voltage is rising. - The difference between these two voltage levels is called the HYSTERESIS. (graph with two threshold levels and brown out reset) Brown Out in MSP432 MSP432 does not use the terminology 'brown out', however it has a signal call POR (Power On Reset) that is generated according to the same logic as a brown out reset. However, the graph looks similar - see chapter 7 of MPS432P4 user guide. 10:30 Watchdog Timer Even a properly configured microcontroller can still crash sometime. This is because of any of the following reasons: 1/ Single Event Upset This is cause by a charged particle striking a transistor, and causing a temporary fault (0->1 or 1->0). This can cause a flip flop to flip state. This event, in turn can cause a chain reaction of other events that may lock up the processor. For example, a bit flip could change the opcode of an important instruction in your program. 2/ Software bugs Your microcontroller crashes in the field because there are bugs left. This is not such a far-fatched idea. It is always possible that there are bugs left that may lock up the processor. In other words, the software 'hangs' and the microcontroller needs to be recovered. The watchdog timer is a peripheral that helps to recover the microcontroller from such a 'lock-up' state. It is a mechanism to implement an automatic hard-reset in the processor when a certain time-out, the so-called watchdog timeout, occurs. A similar mechanism is present in large engines such as trains. In those engines, there is a button that the operating engineer has to press once in while to reset the micro-controller. 10:33 Watchdog Timer Hardware The principle of a watchdog timer is as follows: A counter increments until a given value (eg 2^15). When it overflows before the software resets it, the processor will reset. By default, the watchdog timer is configured to count up until 2^15, which at 3MHz gives a timeout of 10.92ms. This is why we are stopping the watchdog timer in every application, as the first activity. See MSP432 User Guide chapter 17 (including block diagram). The control register of the watchdog timer is protected through a 'password register': one of the registers must accept the value 05Ah otherwise it will not allow changes to the control register. Furthermore, any value written into this register which is not the password, will cause an immediate processor reset. This password is a protection mechanism to protect the microcontroller against software 'gone nuts', which would accidentally overwrite the watchdog timer control. For example, imagine a buggy software program that starts to clear the entire memory space - because of the password register, this clearing will be unsuccessful). 10:40 Watchdog Example The watchdog timer example is on https://github.com/vt-ece2534-s18/msp432-wdt The example works as follows: A timer periodically wakes up a microcontroller with a watchdog timer. Each time the microcontroller wakes up (approximately each second), it will reset the watchdog. When a button is pressed, the periodic watchdog reset is disabled. After the watchdog timeout period, the watchdog will soft-reset the CPU. When the CPU resets, it checks the most recent cause of reset. If it was the watchdog, it will quickly blink an LED. The example illustrates several interesting concepts: 1/ Power Modes A CPU can run into one of several "power modes". These have different levels of activity and different levels of power provisioning. Power modes are essential for low-power operations. A complete discussion of power modes is beyond the scope of a single lecture. Refer to chapter 8 of the MSP432 User Guide. A highly simplified version is as follows. There are 4 major power modes: 1/ active mode: the CPU is running and all peripherals are on 2/ LPM0 mode: the CPU is halted while all peripherals are still on 3/ LPM3 mode: the CPU and the peripherals are all halted the watchdog timer and the real time clock are still on 4/ LPM4 mode: the CPU and ALL the peripherals are all halted Depending in the power mode, it is also possible that the voltage and the clock frequency to chip components is reduced or even turned off. The transitions between power modes are typically driven by interrupts. That is, switching into LPM0 or LPM3 or LPM4 can be driven from software, but the activation out of LPM0, LPM3 or LPM4 can never be done based on software (since the CPU is asleep). 2/ SysTick timer SysTick is a simple period interrupt facility. It is a 24-bit counter inside of the Cortex-M4 CPU of the microcontroller. In this case it is used as a 1-second tick. The systick timer generates a 1-second ISR which wakes up the CPU every second. This is then used to reset the watchdog. The cyclic executive of the overall program looks as follows: while (1) { PCM_gotoLPM0(); WDT_A_clearTimer(); } 3/ GPIO interrupts The button is used to trigger an ISR. The ISR disables further SystTick interrupts. This, in turn, prevents further wakeup from LPM0 which eventually will allow the watchdog timer to expire. 10:50 Demonstration int main(void) { volatile int ii; // Stop WDT WDT_A_holdTimer(); /* If the watchdog just reset us, * we want to toggle a GPIO quickly to illustrate * that the watchdog timed out. */ if (ResetCtl_getSoftResetSource() & WATCHDOG_A) { InitLED(); while(1) { Toggle_Booster_LED(); for(ii=0;ii<40000;ii++) ; } } /* MCLK to REFO at 128Khz for LF mode and SMCLK to REFO */ CS_setReferenceOscillatorFrequency(CS_REFO_128KHZ); CS_initClockSignal(CS_MCLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1); CS_initClockSignal(CS_HSMCLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1); CS_initClockSignal(CS_SMCLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1); PCM_setPowerState(PCM_AM_LF_VCORE0); InitButtonS1(); InitLED(); /* Configuring WDT to timeout after 512k iterations of SMCLK, at 128k, * this will roughly equal 4 seconds*/ SysCtl_setWDTTimeoutResetType(SYSCTL_SOFT_RESET); WDT_A_initWatchdogTimer(WDT_A_CLOCKSOURCE_SMCLK, WDT_A_CLOCKITERATIONS_512K); /* SysTick to wake up every 128000 clocks */ SysTick_enableModule(); SysTick_setPeriod(128000); SysTick_enableInterrupt(); /* Enabling interrupts and starting the watchdog timer*/ enableButtonS1Interrupt(); Interrupt_enableMaster(); WDT_A_startTimer(); while (1) { /* go to sleep. This disables the CPU while keeping all peripherals active */ PCM_gotoLPM0(); /* each time we wake up (from Systick Interrupt) we continue here */ WDT_A_clearTimer(); } } void SysTick_Handler(void) { Toggle_Booster_LED(); return; } /* GPIO ISR for button press - When a button is pressed */ void PORT5_IRQHandler(void) { int status; status = MAP_GPIO_getEnabledInterruptStatus(GPIO_PORT_P5); MAP_GPIO_clearInterruptFlag(GPIO_PORT_P5, status); /* When we press S1, further interrupts from PORT_P5 PIN1 are disabled */ if (status & GPIO_PIN1) { SysTick_disableInterrupt(); } } 10:58 Conclusions We discussed - brown out reset - watchdog timer - demonstration of watchdog timer timeout