STM32 USART Lecture 10 – USART baud rate coding

  • Post author:
  • Post category:Blog

 

USART baud rate coding 

 

 

In this article, let’s learn the function to calculate the baud rate, and the function name is USART_SetBaudRate (Figure 1).

USART baud rate coding 
Figure 1. Function to calculate the baud rate.

 

The USART_SetBaudRate function is used to program the USART_BRR register. In this function, you have to resolve some TODOs. You can download this file from the resource section, and the name of the file is USART_BaudCalculation.

The API shown in Figure 1 is coded exactly the way we discussed in the previous article. The RCC_GetPCLK1Value() and RCC_GetPCLK2Value() functions shown in Figure 2 is used to get the value of the APB1 clock or APB2 clock.

USART baud rate coding 
Figure 2. RCC_GetPCLK1Value() and RCC_GetPCLK2Value() functions.

You have to use RCC_GetPCLK1Value() API since it is written in the I2C driver development section. You have to modify that API in order to read the value of PCLK2, i.e., APB2 bus, and create one more function whose name is RCC_GetPCLK2Value().

 

Steps:

1. Go to the project. You need to do some settings since RCC_GetPCLK1Value() and RCC_GetPCLK2Value() APIs deal with the RCC engine or RCC block of the microcontroller.

Now let’s separate these APIs in the project. In the project, let’s create a source file called driver source file and give the name as stm32f407xx_rcc_driver.c (Figure 3).

USART baud rate coding 
Figure 3. Creating a driver source file.

 

2. Then create a header file in the inc section (Figure 4) and give the name as stm32f407xx_rcc_driver.h (Figure 5).

USART baud rate coding 
Figure 4. Creating a header file (Step 1).

 

USART baud rate coding 
Figure 5. Creating a header file (Step 2).

 

3. Go to the rcc_driver.c file and include the header, as shown in Figure 6.

USART baud rate coding 
Figure 6. Adding the header to rcc_driver.c file.

 

4. After that, include the device-specific header file in the rcc_driver.h file (Figure 7).

USART baud rate coding 
Figure 7. Adding device specific header to rcc_driver.h file.

 

5.In i2c_driver.c, remove the API written to calculate the value of the APB1 clock (Figure 8) and place it in the rcc_driver.c file, as shown in Figure 9.

USART baud rate coding 
Figure 8. Code to be removed from i2c_driver.c.

 

USART baud rate coding 
Figure 9. Code pasted into rcc_driver.c.

 

6. At the top of the I2c driver, you will find a divide factor shown in Figure 10, cut them from there, and paste it into the rcc_driver.c file (Figure 11).

USART baud rate coding 
Figure 10. Divide factors in i2c_driver.c.

 

USART baud rate coding 
Figure 11. Divide factors in rcc_driver.c.

 

7. Remember that we have RCC_GetPCLK1Value() API and now modify this function to create one more API, i.e., RCC_GetPCLK2Value().

8. Give the API prototype of RCC_GetPCLK1Value() and RCC_GetPCLK2Value() in rcc_driver.h, as shown in Figure 12.

USART baud rate coding 
Figure 12. Prototype of RCC_GetPCLK1Value() and RCC_GetPCLK2Value().

 

9. Implement RCC_GetPCLK2Value() API in rcc_driver.c (Figure 13), for this you have to just modify the code of RCC_GetPCLK1Value().

Implementation of RCC_GetPCLK2Value()
Figure 13. Implementation of RCC_GetPCLK2Value().

 

10. Add the RCC driver header file in your device-specific header file, as shown in Figure 14.

Adding RCC driver header file in stm32f407xx.h
Figure 14. Adding RCC driver header file in stm32f407xx.h.

 

11. Complete the USART_SetBaudRate() API shown in Figure 1 and copy it into the USART driver, as shown in Figure 15.

Implementation of USART_SetBaudRate() API
Figure 15. Implementation of USART_SetBaudRate() API.

 

12. Give the prototype of USART_SetBaudRate() API in the other peripheral control APIs section of usart_driver.h (Figure 16).

Prototype of USART_SetBaudRate() API
Figure 16. Prototype of USART_SetBaudRate() API.

 

13. Since the baud rate should be configured at the end of init, call USART_SetBaudRate() function from the USART_Init(), as shown in Figure 17.

Call for USART_SetBaudRate() function from the USART_Init()
Figure 17. Call for USART_SetBaudRate() function from the USART_Init().

 

FastBit Embedded Brain Academy Courses,

Click here: https://fastbitlab.com/course1

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.

Leave a Reply