 Hello and welcome to the STM32L for MOOC online training. My name is André Barata and this is the dedicated session for the Flash Memory. In this session we will learn how to handle option byte settings from the source code level. We will modify the brownout reset level from default to level 2. And then we will verify the option byte settings in ST-Link. All option bytes are available in the flash memory to configure certain specific features of the device. We will now open STM32QBmx to generate our project. As a first step we will click on new project. On the open window we will type our part number STM32L476VG and we will double click on the desired part. Remember that in this project we will modify the level of the brownout reset in the option bytes. So we will initialize the green LED on PE8 to make it visible when the new brownout reset level takes effect. In the clock configuration tab we will leave everything as default. And in the configuration tab we will leave everything as default as well. 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 STM32QBmx and our project is fully loaded on system workbench for STM32 we will open our main.c file stored inside of the source folder. In the main.c file you can see the initialization of all the peripherals defined in STM32QBmx. In this specific case we will just see the system clock and GPIO configuration. As a first step in the user code private variables section we will initialize a structure which contains the option bytes area and related values. So static flash underscore ob programming in it type def flash ob. Then we will proceed to the user code begin to section and we will start by unlocking the flash and the option bytes area. So for this we will use the hl flash and lock function. And additionally hl underscore flash underscore option bytes and lock. As a safety procedure we will implement the functions to lock both the flash and the option bytes just to prevent us from forgetting to do it at the end of the program. So as you might be guessing we will use hl underscore flash underscore option bytes underscore lock functions. And additionally hl underscore flash underscore lock. Now that we have our framework template we can start modifying our brownout reset settings. As every MCU has a different option bytes implementation we need to use the function described in the extension files. So the function will be hl flash ex ob get config. And the input parameter will be the handler for the structure previously initialized. From this structure we will need to modify two parameters. As we intend to modify the user option bytes let's select flash ob dot option bytes equal option byte underscore user. In the user option bytes we need to modify the brownout protection level. So flash ob dot user type equal ob underscore user underscore bor underscore level. After these two steps we know exactly in which area the bytes are and what bytes we want to modify. So here is the general area we are modifying. And here we can see the particular value to be modified. But first we need to check the current configuration of the brownout level and if it's different from our desired level 2 we need to modify it to the level by using flash dot ob dot user config equal ob underscore bor underscore level underscore 2. Then we will proceed to program the new option byte configuration. And as it is common in hl implementations we need to check for the result of the previous operation. So if the result is different from hl underscore ok we will need to call an error handler and in this case we will use the error handler generated by stm32qmx. And by copying it from some other hl function we can reuse the code. So the final step of programming the option bytes is to use the hl underscore flash underscore launch which will reset the microcontroller. Again the status is checked if the operation was completed successfully. After the reset the brownout reset level is set to level 2 and the green LED will be turned on on the evl statement. To do so we will use the hl underscore gpio underscore write pin and the input parameters will be the gpioe port, gpio pin 8 and gpio level which will be set. This will be all from the source code so we can build the project. And then we will be ready to upload the binary file of the project to our discovery board as we are not going to enter the debug mode in this example. Before starting our application let's open the stlink utility and connect to our board. On the target we wish to see the option bytes. Now we can see that the b or r level is equal to 0. Then we will upload and run the application and the green LED will be turned on which means that we successfully wrote to the option bytes. By connecting once more the stlink and by going once more to the option bytes menu we will see that the b or r level was modified from the default level 0 to level 2. So to fully revert the board we need first to erase the chip. Then back to the option byte menu we will enter level 0 and apply the changes. The board is fully set to stock and ready to be reprogrammed. We just finished our flash option bytes head and zone. Thank you for your attention.