 Hello and welcome to the STM32L4 MOOC online training. My name is André Barata and I will guide you through the low power timer hands-on session. The low power timer peripheral embedded in the STM32L4 microcontroller provides a 16-bit timer that is able to run even in stop 0, stop 1 and stop 2 low power modes. This is possible thanks to the flexible clocking scheme. The low power timer peripheral provides a basic general-purpose timer functionalities with very low power consumption when compared to the other timers. One major functionality of the low power timer is its capability to keep running even when no internal clock source is active. This is possible by configuring it in a synchronous counting mode. The low power timer is a peripheral with dual clock domain. The APB clock domain contains the peripherals registers, sometimes referred also as APB interface. The kernel clock domain contains the low power timer peripherals core functions. The kernel clock domain can be clocked by the APB clock source or by HSI, LSI or LSE oscillators. Moreover, it can be clocked from an external clock source through the input 1 of the timer. The low power timer also features an encoder mode function that can be used to interface with incremental quadrature encoders using both inputs. The input and external trigger features glitch filters. In this session, we will learn how to configure and generate code for the low power timer peripheral in STM32 Cubemix. In specific, we will be using the timeout functionality which we will use to wake up the microcontroller in case there was no edge detected within a specified time frame. The low power timer can be configured to operate in timeout mode with external trigger on a pin, and the microcontroller can be put in low power mode. The low power timer counter will be reset each time there is an active edge on the pin. This means as long as the edges are detected, the microcontroller will be continued in low power mode. Once there is no edge within the specified time frame, the microcontroller is woken up by the compare event from low power timer. Let's now open the STM32 Cubemix to start generating our code. With the STM32 Cubemix open, we will start by clicking on new project. We will type our part number, STM32L476VG, and then we select appropriate part. We start the project. First we are going to configure the PE8 pin as output to drive the green LED on our discovery board. Then from the peripheral tree, we will configure the low power timer to count internal clock events mode with external trigger. The external trigger pin PC3 will be highlighted in green, and we will remap it to PA5. PE5 is located on the discovery board next to the PE0, which is internally wired to the center button of the joystick. We will create external interrupts by pressing it, and we will connect these two pins via a jumper. In case you don't have any jumper, one can be found in the back part of our discovery board. In the next step, we will enable the low speed external oscillator. This oscillator is already embedded on our discovery board. In the clock configuration window, we will configure the low speed external oscillator as clock source for the low power timer too. When clocked by the oscillator, the low power timer too is now independent on the system clock. And it's capable to run in top low power modes. The only thing we will need to change in the configuration tab is the following active edge. Other parameters will be left in default configuration. In the NVIC setting tab, we will need to enable the low power timer to global interrupt. At this point, everything has been configured in the STM32QBmx, so we can save our project and generate our code using system workbench for STM32 as our IDE. After the code has been successfully generated and system workbench for STM32 has been opened, we are going to the main.c file which is in the source folder. As we can see, the functions for the system clock configuration, GPIO and low power timer to initialization has been automatically generated and are called from the main function. Let's check the low power timer init function first. The handle structure for the low power timer too is filled with parameters based on our configuration in STM32QBmx. The HAL low power timer init function is called with a pointer to the handle structure past this argument. This function sets the peripheral registers based on the configuration of the handle structure. We will now open the STM32L4XXIT.C source file. This file contains exception and interrupt handlers. At the top of this file there are exception handlers for core exception such as non-maskable interrupts or hard-fought handlers. Let's now scroll down to the bottom. The low power timer to interrupt handler has been automatically generated in the file by the STM32QBmx. Inside this handler the HAL low power timer erkey handler function is called. Let's have a closer look on this function. As we can see this function checks which flag is set, clears it and calls a corresponding callback function. In our example we will use compare callback. When we check the callback functions we can see that they are defined as weak. This means that we can redefine this function elsewhere in the program and the compiler will consider the new definition as the valid one. If there is no other definition the compiler will use the weak one. Let's copy the callback function and redefine it in main.c source file. Usually the new definition of the callback functions are placed in between user code begin for and user code end for comments. Be sure not to copy also the weak symbol. Inside this callback we will turn the green LED to signalize that the compare value from the low power timer to has been reached. If we have a closer look to the system clock config function we can see that the low speed external oscillator has been turned on. In addition to that it has been set as clock source for low power timer 2. Let's continue our coding by defining a period and timeout for the low power timer 2. If we define the period as 65535 and the timeout as 32767 it will result in a 1 second timeout. The period value will be reached into the auto reload register containing a reload value and the timeout value to the compare register. Now we must find the proper function to start the low power timer 2 in request mode. We can start typing hal low power timer and press control space. This will give us a list of proposals. We will choose the hal low power timer timeout start it function. As it can be seen this function expects as arguments a pointer to the peripheral handle structure a period and timeout value. Everything is prepared let's pass it to the function. We will also check the return value and call error handler in case of a failure. As a last step since the low power timer 2 is clocked from the low speed external oscillator we can enter the stop 1 low power mode with WFI as stop entry. The meaning of this parameter is not relevant for this hands on, but you can refer to the low power mode part for more detailed information. In this mode the core clock as well as the clock for most of the peripherals are stopped and thus the overall power consumption is reduced. We have just finished our coding section, let's now build the project and enter the debug mode. Now if we run the program by pressing the resume button we will see that the green LED is turned on after one second. If we press the joystick center regularly before the one second timeout period expires we will postpone the timeout event. Thank you for watching.