STM32 USART Pt1: Basic



Dont forget to check out the vidoe version of this tutorial on my YouTube channel. 

The objective of my tutorials is not to divulge into the timing intricacies of any protocol. Only when it is necessary to program and setup the peripheral in the STM32 will I explain any low-level details of how the protocol operates.

One of the simpler communications peripherals to setup is the USART is UART mode. For this reason and because of its usefulness it proceeds GPIO. By the end of this tutorial you will learn to setup the USART in asynchronous mode thus making it a UART. Using the concepts learned here you will be able to write a reusable library for printing onto a serial monitor, that tutorial will be linked at the end of this tutorial.




The first order of business is to figure out what bus the USARTs are connected to. The image below demonstrates that UART1 is on the APB2 bus while all the rest of the USARTS are one APB1. This is a very important factor because those two busses have different peripheral clock frequencies which we will use to calculate our desired baud-rate. In addition to the baud rate calculation, knowing which bus the USART is located on helps us find it in our RCC module to enable their clocks.


So for example if we wish to use USART1 on the APB2  bus we then know by common convention that its clock enable bit will be located on the APB2 Enable Register in the RCC which translates to :

RCC->APPB2ENR

Like wise we know by convention that the specific bit will look similar in nature:

RCC_APB2NER_USART1EN 

If we put it all together with a nice OR operation we get the following line of code:

RCC->APB2ENR |= RCC_APB2NER_USART1EN

Page 181 of RM0008 shows that USART1 TX and RX pins are on PA9 and PA10 respectively when REMAP =0,  and PB6 and PB7 when REMAP = 1 The remap configuration bit allows us to remap our peripherals to different pins if desired. It may be the case that we are using those pins for another peripheral so there will be a conflict, at which point we would remap them, or simply use a different USART but the remap is there in case you need it. REMAP defaults to 0 at system reset.


At this point you can use the previously written GPIO driver to set up PA9 and PA10 to alternate function mode. For the sake of those who are not familiar with the driver I will program the GPIO in explicitly here.
A common configuration for UART communication is to have no parity control,  1 stop bit, and 8 bit word length. Knowing this STM has made the USART reset/default values to match this common configuration which means we have very little to do. By default the USART is also set to asynchronous mode thus making it a UART. Your frame packet ends up looking like the UART pictured below. Notice how it has no clock and just a start bit and an end bit along with your data.


Setting the baud-rate for the UART is nothing out of this world but does require math, just basic multiplication. And this where the USART you chose comes into play. The baud-rate is generated based on a value programmed into the BRR register that value is called USARTDIV. You do not directly program the baud rate into the register, for example you would not set that register to 9600 to get a baud-rate of 9600. That would be too easy and logical so that is not how they designed it. Instead you will use the formula below to generate a USARTDIV value that we will  then pass to the BRR register and that is how it will generate your desired baud rate. The value that will go into the BRR register is the USARTDIV value for which we will solve shortly.


In the formula above Fck is the frequency of our peripheral clock, which is not always the speed of our system clock. If we would have chosen to use one of the USARTs that is on the APB1 bus you will notice below that the peripheral clock on that bus is 36MHz.

Baud Rate Register:

In this example we are finding the USARTDIV value for USART1 to operate at 9600 baud rate on APB2 bus. Fck is a known value of 72MHz for APB2 bus and baud-rate is a known value dictated by us and USARTDIV is unknown we algebraically manipulate the formula to get the USARTDIV alone on one side and solve for all known values. This final USARTDIV value is what we will put in the BRR register to get the desired baud-rate of 9600.

Solving for USARTDIV we get a value 468.75 we grab the whole part (468) and convert it to hex which yields 0x1D4. Then the remaining fractional part 0.75 is multiplied by 16 and that result is converted into hex. The two hex numbers are combined to yield 0x1D4C. Ultimately this value will get put into the BRR register simply as USART1->BRR = 0x1D4C Do not forget this is the answer fro UART1 on the APB2 bus. Another UART would yield a different result because it would be on the APB1 bus and the value plugged in for Fck would be 36MHz.


Control Register:

The final thing left to do is to enable the UART RX and TX as well as the UART itself. All these setting bits are found in Control Register 1 (CR1) . Other relevant settings are also found in CR1 but as mentioned before we will be using them at default values so there is no need to change anything with them. This register also contains interrupt related bit which I will discuss in the second part of this tutorial in which we implement interrupt based communication. The line below takes care of RX enable (RE), TX enable (TE),  and UART enable (UE). Make sure UART enable is the last thing you do. Datasheet states for a lot of peripherals that they must not be enabled until configured.

USART1->CR1 |= USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;


Status Register:

In order to know when our data has been sent or received we will poll the status register (SR)

The highlighted bit in the register are as follow:

  • TXE: Transmit data register empty. This bit indicates that the contents of the data register (DR which is where we write the data to be sent) have been moved to the shift registers where the transmission actually takes place. Meaning the data register is empty and thus you can write new data to it for transmission, does not mean the data has been sent yet.
  • TC: Transmission Complete. This bit will be set when the data transmission is actually complete.
  • RXNE: Receiver Not Empty. Simply put, we have received data. 
It is important to distinguish between TXE and TC, while they may be used interchangeably in the code I have provided below, they are indeed telling us two different things. Other bits in this register relate to the options we did not enable like Parity error etc.. You  can check out the data sheet to learn more about those.

Data Register:

As mentioned above the Data Register is where we will write our data. It has bits 0 through 8 in case you set your word length to 9 bits, otherwise you will only use bits 0-7. Writing data to the DR register starts the transmission.

Sending & Receiving:

In this basic example we will wait for data to be received and then we will transmit it back, basically an echo program. So whip out your favorite USB to Serial converter dongle, and open up your favorite COM port monitor.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* Includes */

#include "stm32f10x.h"

int main(void) {


 //-----------------------| UART CODE |------------------------------------------
        //USART1 / GPIOA clock enable
 RCC->APB2ENR |= RCC_APB2ENR_USART1EN | RCC_APB2ENR_IOPAEN; 

 //remaping if needed
 //AFIO->MAPR |= AFIO_MAPR_USART1_REMAP ; //remap RX TX to PB7 PB6

 //pin configurations: PA9- TX is set to ALternate-push-pull and 50MHz
 GPIOA->CRH |= GPIO_CRH_MODE9 | GPIO_CRH_CNF9_1;
 GPIOA->CRH &= ~(GPIO_CRH_CNF9_0);

 /*
  * PIN PA10 is the RX pin and it has to be set to input &FLOATING
  * this is the rest value of the pin so we dont do anything to it
  *
  * */

 //USART DIV value
 USART1->BRR = 0x1D4C; //for 72MHZ on APB2 bus

 //----------  RX enable        TX enable     UART enable
 USART1->CR1 |= USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;

 while (1) {

  if (USART1->SR & USART_SR_RXNE) //if RX is not empty
  {
   char temp = USART1->DR; //fetch the data received
   USART1->DR = temp;  //send it back out
   while (!(USART1->SR & USART_SR_TC)) //wait for TX to be complete
    ;

  }

 }

}


That does it for the basic UART, next lets implement a more efficient interrupt driven UART









Comments

  1. If I have a 72MHz clock and fraction part is 0.75, then what is hex value for it
    ????

    ReplyDelete
    Replies
    1. Sorry I am just now answering this , but you multiply the decimal by 16 and the non-decimal part of the answer is what you put as a hex value

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hello, I've been watching your vids in youtube and have give me a lot of material to start in the STM32fx family, in my case I'm using the libopencm3 library but the theroy behind is the same.
    I've managed to setup the chip so it listens the USART2 for incoming data(in this case a GPS) and then send it to the PC over USART1. This is done via an Interrupt set by the Control Register, RXNEIE that calls a funcion(usart2_isr) whenever data is received.
    This function then checks the Control register and the Status register to see if there's data in the register. And then sends it to the USART1.
    But my problem is that This just spits everything. My question is if you know how can I parse this incoming data into a string and then compare it to see if is a valid data(from the GPS).
    Thank you very much for all the content and effort you are putting into this blog and in the youtube channel, hope you keep it up and hook more people into this world.

    PS: I might not have explained my situation too good, I just started with the embedded world and everything is a challenge. Thank for the response in advance.

    ReplyDelete
    Replies
    1. When you receive your data you need to put it into a buffer ( a variable) and check the buffer for whatever string you are looking for and if you find what you are looking for you send it to USART1. Some sort of string search function or you can just go into the array (because for multiple bytes your buffer variable is just an array) and search index by index until you reach something you are looking for.

      Delete
  4. its refreshing to see direct manipulation of registers, and hence more understanding od whats acually going on inside the device.
    Keep it up.

    ReplyDelete
  5. Borgata Hotel Casino & Spa reopens June 22
    MGM 동해 출장샵 Resorts International has reopened the hotel and casino to the 시흥 출장마사지 public 인천광역 출장샵 following the COVID-19 당진 출장안마 pandemic. MGM Resorts 안동 출장마사지 International reopened the

    ReplyDelete
  6. is it possible without USB TO TTL converter?

    ReplyDelete
  7. How do i connect the STM with TTL converter?

    ReplyDelete
Share your comments with me

Archive

Contact Form

Send