 Hello and welcome to the STM32 L4 MOOC online training. My name is André Barata and this is the dedicated session for the SRAM2 memory. The L4 Discovery board that we have been working on has 32 kilobytes of SRAM2. SRAM2 is quite an interesting peripheral as it has two unique features. It has hardware CRC for robust data storage and the possibility of data retention even in standby mode. This feature won't be subject of study during this hands-on. In this session we will focus mainly on the hardware functionalities of the SRAM2 and how we can use them through the system registers. As we want to maximize efficiency we will learn how to set the content of SRAM2 using solely the embedded hardware functionalities, saving time and resources as the MCU is not involved in this process. So in our exercise we will reset the SRAM2 by hardware and demonstrate how to modify the linker file to store data or an array in SRAM2 region. We will now open STM32 Cubemix to generate and configure our hardware. As a first step we will click on new project and on the new open window we will type our part number STM32L476VG and we will double click on the desired part. In this project we will implement and configure most of the things in the code, yet for debug purpose we will initialize the green LED on PE8 to indicate the user that the SRAM2 was properly set and the red LED on PB2 to indicate that an error has occurred. In the clock configuration tab we will leave everything as default, and in configuration tab we will leave also everything as default. As everything is set we can just save our project and generate the code using system workbench for STM32 as our IDE. After the code is generated by STM32 Cubemix and our project is fully loaded on system workbench for STM32 we will open our main.c file stored inside the source folder. On main.c file you are supposed to see the initialization of all peripherals defined on STM32 Cubemix. In this case we will just see the system clock and GPIO configuration. Since our goal is to place an array in SRAM2 memory to do so we need to define a region in the linker file and later place an array inside this region. The linker file extension for the GCC compiler is .ld. So let's open the linker file, if we scroll down alongside the file we will see the specification of the regions in the memory area. We have RAM, RAM2 and FLASH. Let's see if we can find a region that specifies the SRAM2 area. So as you can see we have the FLASH defined on the file, we have the SRAM defined on the file, yet we are lacking on the SRAM2 definition. So I would suggest to implement it as follows. Region definition starts with .SRAM2 underscore section and we need to implement the following and we need to specify the section in RAM2 area. The linker file is already modified so we can come to the main.c file and define our array placed in RAM2 memory. But before that we need to define our test pattern which will be written from memory to SRAM2. We need this pattern to differentiate the state of SRAM2 either in an inconsistent state or erased by hardware. So we will define the test pattern as 0xaa which is 10101010 set of bits. Then let's define SRAM2 array size. Let's define it as a 16 element size array. Finally we need to define our array in SRAM2 and to do so we need to tell the linker file that this array should be placed in this memory region. We will use the attribute keyword and here we will put the desired section name. The name will be the one we chose previously which was SRAM2 section and then our variable name. SRAM2 array will be initialized with the test array size defined earlier on. We'll also need to initialize a loop counter and the timeout variable. The use of both will be explained later on on this hands-on. Just to wrap up, in case we need to start some set of variables in a dedicated session of memory which is described on the linker file we need to use. The attribute keyword, then the section keyword or the name of the section, in our case SRAM2 underscore section, dot name of our variable SRAM2 array. Then, on the user code begin to section, we will fill our array with some test pattern different than zero, which is the default value after array's operation. We will do so by filling every element of the array in a loop. SRAM2 memory is written protected by default. To disable this protection we will need to manipulate the system registers. The allocated function to unlock the right protection is double underscore HAL-SIS-CONFIG-SRAM-WRP-UNLOCK. After unlocking the memory we will proceed to completely erase the SRAM2 memory and we will do so by using the macro. Double underscore HAL-SIS-CONFIG-SRAM2-ERASE. This triggers the hardware erase of SRAM2 memory. There is a status bit assigned to the process of erasing the SRAM2 memory which needs to be checked just to make sure the erase process finished. We will make use of the initialize timeout variable and make it equal to the C-stick actual value plus a number of extra ticks. A C-stick period is equal to 1 millisecond and our timeout value will be equal to C-stick plus 10, which means 10 milliseconds of total timeout duration. We will keep the program in a while loop for 10 milliseconds or as long as the macro. Double underscore HAL-SIS-CONFIG-GET-FLAG-SIS-CONFIG-FLAG-SRAM2-BUSY-BEAT returns true. We will check if the timeout expires and if yes, we will have a break leaving the while loop. We will proceed to check the integrity of the array created on SRAM2 and if it's not filled with zeros, we will turn on the red LED, notifying the user for the e-constancy of the data. The red LED is connected to the port B pin 2 as you might recall from the STM32Q by MIX configuration. As a last step, we will turn on the green LED. So in case of a problem, both LEDs will be switched on. If SRAM2 is properly initialized, only the green LED will be on. The green LED is connected to the port E pin 8. This will be all from the source code so we can build the project and before entering the debugging session, we will set two breakpoints. The first one will be just after the test pattern loop. After this point, we are supposed to see the content of the SRAM2 with a value different than zero. We will set the second breakpoint after invoking the hardware arrays of the SRAM2 and then the content should be equal to zero. Let's now enter the debug mode. On the watch expressions tab, we will add our arrays stored in the SRAM2 to be able to see its content throughout the application. If we start our application, it will stop on the first breakpoint and its content will be 170, which is the decimal representation for AA in hexadecimal. If we press to resume the program again, it will stop in the second breakpoint in the end of the hardware arrays of the SRAM2. The content of the array must be zero. If we resume one last time, we will see that the green LED was turned on. We just finished our SRAM2 hands-on. Thank you for your attention.