 Hello and welcome to the STM32L4 MOOC online training. My name is Andre Barat and I'll guide you through the RCC hands-on session. The RCC or Resetting Clock Control manages system and peripheral clocks. The STM32L4 Discovery Board has three internal oscillators, one external crystal or resonator and it also contains three-faced locked loops. Many peripherals have dual clock domain, allowing to clock them either from one of the cortex and buses or from a clock source independent of the system clock. This allows for instance the ADC to run at its full speed while the core is running on a lower speed. This will represent a saving on power consumption. The RCC also manages various resets present on this device. Using the clock configuration window of the STM32QBmx it's possible to configure all clocks inside the microcontroller with ease. As we enable the peripherals from the pinout window they will also become available here. We can see that many peripherals have several clock options. Once the microcontroller clock output feature is enabled we will also be able to select from which clock we want to output. Our goal in this example is to learn how to change a clock source on the fly using HALAPI. We will also learn how to output a selected clock from one of the low speed oscillators on a dedicated pin. This feature is also available for all the high speed clocks. Without further ado let's launch our STM32QBmx to start initializing our peripherals. When the STM32QBmx is loaded we start a new project and we will type our part number STM32L4476VG. When the part number is found we start a new project. On the pinout tab we will go to RCC and we will select LSEO clock output which is mapped on the PA2 pin. We will go to the clock configuration tab and we will realize that the LSEO source MOOCs becomes available. In our example we will be outputting the LSI oscillator. We are ready to save the project and after choosing the system workbench for STM32 as our IDE we are ready to generate the code. After the code is generated by STM32QBmx we will start by inspecting the elements of our code. We are going to check the GPIO configuration and in this case only LSEO pin is configured. Now let's have a look inside of the system clock configuration function. There are two configuration structures to be specified later on on this function. The first one is the oscillator configuration and the second is the clock source configuration. Starting with the oscillator configuration the oscillators to be configured has to be assigned to oscillator type structure members. When everything is configured the HALRCC oscillator configuration function has to be called with a pointer to the configuration structure passed as argument. The second structure is for system clock source settings and prescalers for AHB and APB bus settings. MSI will be used as clock source when we are pleased with the structure that we filled we need to call the HALRCC configuration being the first parameter the pointer to the configuration structure and the second will be the weight states for the flash. LSEO will be enabled in this function. Let's now copy the configuration structures definition to the main function. We can see all members of the structures to be used to configure the oscillators. Once again in the oscillator type must be filled the oscillators to be configured. Here we can see all the members of the clock source configuration structure. Let's start by coding and switching the system clock source on the fly. Press control space for a list of proposals. We will start by filling the oscillator type member. Let's have a look into its declaration to see what we can use. In the comments there is written that these parameters can be a value of the RCC oscillator type. Let's find these parameters by using control f. We will now configure the high speed internal oscillator as this will be our new clock source. Now we will configure its new state as we are turning it on. Let's do it in the very same way as we did in the previous case. Actually, that's the way how to work with HL peripheral drivers in general. We will select and copy RCC HSI on to its state. We will leave the calibration value as default. Now the phase locked loop configuration. This one has its own structure within the oscillator structure. There are PLL multiplication and division factors, its own clock source and state. Let's turn it on and search for possible values. Now let's choose a source for the PLL. We will be using HSI oscillator as PLL clock source. Then just multiplication and division factors remain to be configured. They will be set to run on maximum frequency which is 800 MHz. You can verify the configuration selected in the clock configuration window of the STM32QBmx. Now we can finally call the HAL RCC oscillator configuration. And the parameter to be passed is appointed to the configuration structure. If something fails and the function doesn't return HAL OK, an error handler will be called. Let's continue to the clock configuration structure. As in the previous case, the first member states which clock should be configured. Let's put them all. Using all the clock sources, we will define PLL as our main clock source. All the pre-scalers will be set to 1. This doesn't play a big role in our example as we are not using any peripheral. Now we can finally call the HAL RCC clock configuration. It expects a pointer to the configuration structure as the first argument. As a second argument, it expects a number of weight states for the flash. As we are running full speed, we will use 4 weight states. As in the previous initialization, if the code configuration function isn't executed properly, the code will jump to an error handler. Now as an optional step, we can turn off the MSI. The PLL configuration won't be changed. The remaining lines can be deleted. Let's now build our project and start our debug session. Let's insert the breakpoint on the system clock configuration function. Let's press resume and the program will execute until the breakpoint is reached. Now press step over to jump inside the function. We will check now the RCC registers. Double click to fetch registers value. In the control register, MSI is on and the rest are off. In configuration register, SW byte is set to 0 meaning that MSI is being used by the system as clock source. We will press resume again. From now on, we are in the infinite loop. So we will press pause and we will check the registers content again. The registers highlighted in red means that they have been modified. We can see now that the PLL is on and ready. The HSI is on and ready and MSI has been turned off. In the configuration register, if we reanalyze once again the SW byte, we will see that it is defined as 0x3 or in binary 1.1. This means that the PLL is being used as clock source. In the flash ACR register, latency is set to 4 weight states. The clock source has been switched. If you connect now an oscilloscope probe to PA2PIN, you will be able to monitor the LSI frequency. This will be all for this session. Thank you for watching.