 Hello and welcome to the STM32L4 MOOC online training. My name is André Barata and I'll guide you through the UseArt hands-on session. The UseArt stands for Universal Synchronous Asynchronous Receiver Transmitter. The UseArt interface is widely used for serial communication in embedded systems. It is a very flexible serial module that supports a synchronous UART communication SPI monster mode or lean mode. It can also interface with smart cards and infrared devices. Certain features that are useful when implementing mode bus communication are provided. Applications making use of the UseArt benefits from the easy and inexpensive connection between devices, which only requires a few pins. In addition, the UseArt peripheral can be functionally in low power modes. In this session we will learn how to configure and generate code for the UseArt peripheral in STM32 Cubemix. In specific, we will make use of the STLink virtual COM port to establish a communication between the MCU and the computer without any external UART to USB converter. When remapping RX to TX signals of the UseArt to peripheral to PD5 and PD6 pins, they connect to the STLink which allows to transfer data between the onboard MCU and PC. To be able to see the transmitted data, we will need to use a terminal application. After this introduction, let's open STM32 Cubemix to start configuring our peripherals. Let's start by creating a new project. We will type our part number STM32L476VG. We will select the desired part and will press start project. In this example, the UseArt2 will be enabled and configured to a synchronous mode. As we intended to communicate through the STLink virtual COM port, some remapping is needed. To see the remapping options for a certain signal, press and hold control button and left click on a pin to which the signal is currently assigned. Hold left mouse button and remap both UseArt2 signal to pins PD5 and PD6 using drag and drop. That's all that has to be done in the pinout window in this example. There is nothing to be configured in the clock configuration window, yet on the right side of the window you can see that the UseArt2 has become enabled and by default it's clocked by the APB1 bus. Moving on to the configuration window, there are many parameters that can be configured in UseArt. Starting from the common ones such as Baudroid, Parity or word length to the advanced ones such as pin swapping, active level of each signal, auto baudroid detection etc. We are only going to modify word length from 7 bits to 8 bits. Now we will configure DMA channel for transmit direction so please open DMA setting tab. Press F button then select UseArt2 transmit DMA request. You can see that the 7th channel of DMA1 has been automatically assigned since this one is dedicated for UseArt2 transmit requests. The direction is from memory to peripheral and the priority is by default low. As there won't be another DMA request configured we can leave it as it is. The rest of the settings we will leave it as default. This means DMA mode, memory increment and 8 bit data width for both memory and peripheral. We have just configured everything we need for this example so now we can save the project and generate our code. Do never forget to use system workbench for STM32 as our selected IDE. Once the code is generated and the IDE is fully loaded we can open the main.c file which is in the source folder. As always the necessary header files are automatically included and handle structures for the configured peripherals have been created. Moreover functions for the system clock configuration, UseArt2 and DMA initialization have been configured and are called from the main function. Let's have a look on the function that initializes UseArt2. Inside this function the handle structure for the UseArt2 is filled with the predefined parameters and at the end HALURTneed function is called. This function sets the peripheral registers based on the configuration of the handle structure. All parameters are set based on the configuration done in STM32 Cubemix. For instance you can see that the word length is set to 8 bits. Now let's have a look into the DMA init function. Inside this function only DMA1's clock is enabled. There is set the clock controller and the interrupt for the channel 7 of DMA1 is enabled in NVIC. The priority of this interrupt is also configured there. For the DMA channel configuration we have to go inside the STM32 for XXHALMSP.cs source file which contains system level initialization for peripherals. The initialization function for each peripheral is called MSP init. Inside this function the peripheral clock is enabled, pins are configured and DMA channels or interrupts are also configured if used. Let's start writing our code by creating a buffer that will hold the message to be transmitted. In order to transmit our message we have to find the proper function. We can start by typing HALURT and press control space. We will get a list of possible functions. The function HALURT transmit DMA will be used. As you can see the function returns an HAL status value if it has been executed properly, otherwise it will return HALURR. There are three arguments to be passed to this function. The first one is a pointer to the UART handle structure, the second one is a pointer to the data buffer and the last one is the number of data transfers. We can copy the name of the handle structure from the top of the main.c file. The name of the data buffer can also be copied. As you probably know the name of an array is also a pointer to the first element. For the number of transfers we will use the size of function. We will divide the size of the whole array by the size of the element minus one, as it tends to make sense to transfer also the character that indicates the end of a string. We are also going to check the return value and call error handler in case of a failure. On the following step we will enter the sleep low power mode and let DMA plus OR to the job without CPU interaction. In sleep mode the clock sources for the core are turned off and thus the overall power consumption is reduced thanks to DMA CPU is not needed to transfer the data. But before that we need to call the HAL SuspendTick function to disable interrupts coming from the SysTick timer. This timer is configured and start inside the HAL init function which is called at the beginning of the main function. It is used as time base and configured by default to generate interrupts with one millisecond period. If we didn't disable the SysTick interrupt the SysTick timer would wake up the microcontroller for sleep mode within one millisecond. Now we can finally call the function used to enter the sleep mode. As always the function name starts with HAL then PWR since it's related to power control we can press control space to look for the proper function. HAL PWR enter sleep mode seems to be a good choice. The function has no return value and it expects two arguments the regulator mode and sleep entry. The explanation of the meaning of these two arguments is out of the scope of this example. We will pass PWR main regulator on and sleep entry WFI. The only important thing for now is that we are entering sleep mode in which cork lock is turned off. We have just finished the coding part and now we can build the project and enter the debug session. To be able to see the transmitted message we need to install a terminal application and set its valid rate to 150,000 and 200 bits per second 8 bits size message one stop bit without parity. We will use that a terminal application to show the transmitted data. Also don't forget to open the new connection with stlink virtual comport. Now let's run the program and let's have a look on the terminal window. As you can see we have successfully received the messages. The example is now finished. Thank you for your attention.