cancel
Showing results for 
Search instead for 
Did you mean: 

How is my SPI's baudrate calculated using STM32CubeMX?

Stefanie LAU
ST Employee

STM32CubeMX is a graphical tool that allows users to configure STM32 MCUs (microcontrollers) and MPUs (microprocessors) quickly and simply. In addition, the tool also assists in generating the corresponding initialization C code for the Arm Cortex-M core(s) or partial Linux device tree for Arm Cortex-A cores.
One of the many features included in STM32CubeMX is the ability to allow users to select and configure all the available MCU’s or MPU’s clocks through a GUI (graphical user interface).
In the clock configuration tab, users can:

  • select from various clock sources
  • apply different pre-scalers to adjust the clock sources to various peripherals and buses
  • configure individual peripheral frequencies with the click of a button

Furthermore, the clock tree configuration feature is “smart” in the sense that it can detect errors and abnormalities in clock configuration, highlight and report these errors to the user and automatically run its resolver to help fix the configuration issues.
In this article, we will use the STM32C031C6 as the reference STM32 MCU to generate a STM32CubeMX project and explain how the SPI baud rate is derived from the STM32CubeMX GUI’s (graphical user interface) SPI settings.


Step 1. Use STM32CubeMX to generate a STM32C0 based project. Upon selecting the ‘Access to MCU selector’ icon from the GUI’s homepage, search for the part number STM32C031C6 and double click on the target device from the list populated under the MCUs/MPUs list.

StefanieLAU_1-1693987620071.png

Step 2. With the newly generated project, enable and configure the SPI. To do so, on the “Pinout & Configuration” tab on the right-hand side of the GUI under ‘Categories’ expand the connectivity option and select SPI1. Note that other target devices may have more than one SPI for selection, but the process of enabling and configuring the SPI port will be all the same.

StefanieLAU_2-1693987620069.png

Expand the drop-down menu next to ‘mode’. From this menu, you will be able to select and configure the SPI mode of your choice.

StefanieLAU_3-1693987620073.png

Upon selecting the SPI configuration of your choice, STM32CubeMX will generate and present you with various configurations of modify.

StefanieLAU_4-1693987620075.png

Step 3. Now we will need to figure out the clock source for the STM32C0’s SPI. Using the STM32C0’s reference manual, we will look for the table that details the STM32C0’s peripheral register boundary addresses and locate SPI1.
From the table, the address of SPI1 starts at 0x4001_3000 through 0x4001_33FF. In this table on the left-hand side, the reference manual confirms that SPI1 is connected to bus APB.

StefanieLAU_5-1693987620093.png

To further confirm this, we can use the address boundaries of SPI1 (0x4001_3000 - 0x4001_33FF) and check them against the STM32C0’s memory map. Since the SPI1 address boundaries lie in the APB’s memory boundaries, we can confirm that SPI1 is connected to the APB bus.  

StefanieLAU_6-1693987620084.png

Step 4. Now that we’ve confirmed that the STM32C0 SPI1 is connected to the APB bus, we can navigate to the clock configuration tree tab in the STM32CubeMX project we generated a few steps prior.
We will want to locate and pay close attention to the clock source feeding into the APB peripheral clocks, since we know that SPI1 is connected to the APB bus.
From the clock configuration tree, we can see that HCLK feeds into PCLK which is the APB peripheral clocks.

StefanieLAU_7-1693987620078.png

Furthermore, we have the option to modify this APB peripheral clocks value by selecting an alternate value from the APB prescaler drop down icon. For the sake of our example calculation, we will leave the APB prescaler at 1 ultimately keeping the APB peripheral clocks value at 48MHz.

StefanieLAU_8-1693987620080.png

Step 5. Another contributor to the SPI peripheral’s baudrate is the prescaler (for baud rate) in the SPI’s clock parameters setting.
To locate this configuration, navigate back to the “Pinout & Configuration tab”. On the left-hand side of the GUI, make sure that the “Categories” is selected, then expand “Connectivity” and select SPI1.
In the middle of the GUI look for the “Configuration” section, select the “Parameter Settings” tab, and expand the “Clock Parameters” section.

StefanieLAU_9-1693987620086.png

Note that the SPI baud rate prescaler value can be also adjusted from STM32CubeMX’s default value by using the drop down-menu to select another value, as shown below:

StefanieLAU_10-1693987620089.png

 

For our SPI baud rate example calculation in this article, we will keep this SPI prescaler (for baud rate) at the default value of 2.


Step 6. From the steps above, we can see that the STM32’s SPI baud rate is based on the following two factors:

  • APB peripheral clock
  • prescaler (for baud rate) found in the SPI’s Clock Parameter’s settings


In our example case, the APB peripheral clock is set at 48MHz and the prescaler (for baud rate) is set at 2.
Thus, in our example the SPI baud rate is be calculated as follows:
APB clock ÷ prescaler = MBits/s baud
48MHz ÷ 2 = 24Mbit/s baud
As a final note, every STM32’s datasheet will include a table detailing the SPI characteristics of the device. It is important to note the maximum SPI frequencies and timings for optimal performance of the SPI peripheral in your application.

StefanieLAU_11-1693987620090.png

Relevant Links & Documentation:

Editors note: This article was originally published 2023-04-12. The article had 219 views prior to it's republishing.


 

Version history
Last update:
‎2023-12-07 05:30 AM
Updated by:
Contributors