System Clock

The self-diagnostic routine has been implemented using the Real-Time Counter (RTC) and a Timer/Counter (TC). A TC has been chosen because this peripheral has the same clock domain as the CPU. The RTC can, however, be clocked from an independent clock source. E.g., the CPU could be clocked from the internal 20 MHz oscillator and the RTC from the internal 32 kHz oscillator.

The RTC is set up to generate an interrupt periodically. In this interrupt, a software 16-bit TC overflow counter is compared to its expected value, and if it is within a given configurable range, the counter value is cleared. Otherwise, the error handler will be called.

In the TC overflow interrupt the 16-bit overflow counter variable is increased. The reason for using a software counter value is that the TC will in most cases run at a much higher frequency than the RTC. The TC will overflow much quicker than the RTC. The error handler is called if the overflow counter is higher than a configurable threshold. Given that the overflow counter is cleared in the RTC interrupt, an overflow count higher than the threshold would mean that there is a mismatch between the frequency of the RTC compared to the frequency of the TC.

The self-test module will detect a failure in either the system clock, the internal 32 kHz oscillator, the RTC, or the TC. The risk of an undetected error is reduced to a scenario in which failures in both the RTC and the TC clocks give a correct ratio of their frequencies.

The example application is similar to previous examples. By default, the tinyAVR 1-series of devices run from an internal 20 MHz oscillator prescaled down to 3.3 MHz, which is the system frequency that is considered when setting up the parameters of the self-diagnostic routine. The TC period is changed if the button is pressed. This will make the test fail, and the LED will be turned OFF.

The RTC frequency and the RTC interrupt period can be configured in the files related to the RTC. Note that different RTC setups will be compatible with the self-diagnostic routine as long as the clock source is independent of the CPU-clock and the constant RTC_INTERUP_PERIOD_TIME is defined according to the RTC settings. It is also possible to configure the TC module, the prescaler, the tolerance for the test (in percent), and the system frequency. The latter has to correspond to the actual system frequency set by the main application. The self-diagnostic routine will not change the clock system according to that setting.

There are a number of methods to test this self-diagnostic routine.