 Hello and welcome to the STM32 L4 MOOC online training. My name is Andre Baratta and this is the dedicated session for the RTC peripheral. RTC stands for Real Time Clock and it provides an automatic wakeup to manage all power modes. The Real Time Clock is an independent BCD timer slash counter. The RTC provides a time of day clock calendar with programal alarm interrupts. RTC domain includes not only clock and calendar, but also wakeup timer which can be used as pre-odical event to wake up the MCU from low power modes and a set of registers in backup RAM area, 32 for 32-bit long registers. The content of those registers are preserved by RTC power supply domain, even if the power supply of the MCU is switched off. For instance, we would need a backup battery connect to the RTC power supply domain. As long as the supply voltage remains in the operation range, the RTC never stops, regardless of the device status, run mode, low power mode or under reset. For this hands-on we are interested on the tamper detection functionalities of the RTC. The purpose is to detect physical tampering in a secure application and to automatically erase sensitive data in case of intrusion. This feature is commonly used, for example, in metering applications such as gas or water metering. We will use an external GPIO to detect the tamper event, which invokes protection mechanisms both in run and low power modes. Tamper detection capable GPIOs are part of the RTC power supply domain, and detection of event is possible even in standby mode, which will be used on this hands-on. The application will be working in standby mode. Tamper detection related actions will erase backup memory registers content, and this way we will be able to detect any unauthorized attempt to break into the device. We will use discovery board LEDs as the bug output. Green LED means the backup RAM is okay. Red LED the backup RAM was erased slash tamper detection. For practical reasons we will use the middle button of the blue joystick as a tamper sensor. In real world application this would be replaced by a special anti-tamper switch. We will now open STM32Q by MIX to generate our project. As a first step we will click on new project, on the new open window we will type our part number STM32L476VG and double click on the desired part. We will use RTC peripheral and as it needs a clock source, please go to the RCC peripheral, select low speed clock external or LSE, and then select crystal ceramic resonator. We can see now that two pins are highlighted in green to connect the crystal resonator. Most common clock source for RTC is 32kW clock crystal oscillator and we are going to use it as well. On RTC peripheral check activate clock source, activate calendar. As our application will be working in standby mode and we need pre-autical wakeup event source. Last action to do with RTC peripheral configuration is the selection of a particular tamper pin. Please select tamper pin 2 as this pin PA0 is connected to the central key position of the joystick on our discovery board. To finalize we will configure the LEDs. Red LED is connected to the PB2 Please configure it as GPIO output and the same for the green LED which is connected to the pin PE8. Let's go now to the clock configuration tab. Let's select LSE clock source in RTC clock source multiplexer and set MCU clock to the maximum value 800 megahertz. We will let STM32QBMX handle all implied modification for us and move to the configuration tab. On the configuration tab let's configure the RTC peripheral. Let's assume that we need to wake up from standby mode every 5 seconds. To achieve this goal we need to set the wakeup counter to 4 as the counter starts from 0. Due to practical reasons please select one hertz clock for wakeup timer. We also have to activate RTC interrupts to enable MCU to wake up from standby mode and the same for tamper events. Both interrupts should be activated in the NVIC setting tab. Now we can save the project by giving project a name selecting the destination folder and choosing the IDE. In our case system workbench for STM32. Let's not generate the code. After the code is generated by STM32QBMX and our project is fully loaded on system workbench for STM32 we will open our main.c file stored inside the source folder. Let's now go to the private variables section and define a 30-bit long data pattern which will be written to our backup RAM or memory register. For instance we can select 0xA5F0A5F0 pattern. Then we will define a variable with the location of the first RTC backup register located on the RTC backup memory RAM. Now we can go to the user code section number 2 and the edit application functionality. In order to make this session more comprehensible additional comment lines were added to the code. We decide to use standby mode in our application. As you know after resume from standby mode the reset of the MCU is preformed. We need to differentiate between two source of reset event. First one is reset caused by the wakeup from standby mode and the second one is caused by power on reset. We will handle the two cases separately. Yet before we check which source caused the MCU to go on standby mode we need to differentiate the power on sequence from the successive auto wakeup cycles. Let's start by checking the standby flag with the macro hl underscore pwr underscore get underscore flag. So two scenarios are possible the one resuming from standby mode or the power up sequence. Power up sequence means the first time the application runs where we will preform the writing of the pattern to the backup memory RAM. Starting from the first case the standby flag is set. The first action we will preform is to clear the standby flag and for that we will use the macro hl underscore pwr underscore clear underscore flag. Then we need to differentiate what caused the microcontroller to resume the standby mode. We will read the backup register by using the hl underscore rtcex underscore backup read function and the parameters are the rtc handler and the location of the backup register that we intend to read. So the next step will be to check if the data in our backup register is still valid or if it was erased by the temperature detection related actions. If this value is not equal to our predefined data pattern we will toggle with the red led with 100 milliseconds period. Otherwise if the content matches our predefined value pattern we will toggle with the green led for one second. Second case means power on sequence and in this sequence we need to write our data pattern to the backup RAM register using hl extended functions. Since the implementation of the backup RAM vary from line to line hl rtcex backup write first parameter of the function is the rtc peripheral handler. Second parameter is a number of particular backup register. Please do not select register number zero because this register is already used by the hl rtc functions. Third parameter of the function is the data to be written on the register. After the pattern is written on our predefined backup register we will proceed to configure the rtc and its auto wakeup timer. We will start by disabling the auto wakeup timer with the function hl underscore rtcex underscore deactivate wakeup timer. The only parameter is the rtc peripheral handler. Then we will clear the wakeup flag with a macro hl underscore pwr underscore clear underscore flag. And as a last step we will configure the auto wakeup timer by using hl underscore rtcex underscore set wakeup timer underscore it. The parameters are the rtc peripheral handler the desired period we will set it to four as we want a five second period and we will choose the appropriate pre-scalar value. Now we have coded our application functionality and the last action is to enter the standby mode. To enter the standby mode we will use the function hl underscore pwr underscore enter standby mode. Now we can build our project. Flash the program in our microcontroller using the run option. Please do not start the debugger. Now we can take the discovery in our hands and we can see that the green led flashes every five seconds for a while. Then we can simulate a temper event by pressing the central position of the blue joystick and we can see that the red led starts to blink quickly. Now we can press the reset button on the discovery board and we can see that the red led is still blinking. The only way to return back to the initial state of the application is to unplug the usb cable meaning switching off the power supply. Waiting for several seconds to discharge all power supply decoupling capacitors and plug the usb cable again. This will be all for this hands-on session. Thank you for watching.