Getting Started with STM32WB55: UART Transmit

In this guide, we shall see what is the UART and how to develop a driver to transmit data over printf function.

In this guide, we shall cover the following:

  • What is UART.
  • Driver development.
  • Results.

1. What is UART:

To transfer data, computers adopt two method: serial and parallel. In serial communication data is sent a bit at a time (1 line)while in parallel communication 8 or more lines are often used to transfer data. The UART is a form of serial communication. When distance is short, digital signal can be transferred as it is on a single wire and require no modulation. This is how PC keyboard transfer data between the keyboard and motherboard. For long distances like telephone lines, serial data communication requires a modem to modulate.We will not go into the details of this in this lesson, this shall be left for subsequent lessons. 

Synchronous vs. Asynchronous

Serial communication operate in two ways : synchronous and asynchronous. In the synchronous mode, a block of data is sent at a time while in the asynchronous mode, a single byte of data is sent at a time. It is possible to write code to provide both the synchronous and asynchronous serial communication functions however this could turn out tedious and over complicated therefore, special chips are manufactured to perform these functions. When these chips are added to a microcontroller, they become known as Serial Communication Interface (SCI). The chip that provides the the asynchronous communication (UART) is the main theme of this lesson. The chip for synchronous communication is known as the USART and shall left for another lesson. 

Asynchronous Communication Data Framing

The data is received in zeros and ones format, in order to make sense of the data, the sender and the receiver must agree on a set of rules i.e. a protocol, on how data is packed, how many bits constitute a character and when data begins and ends.

The Protocol

  1. Start and Stop bits

Asynchronous serial communication is widely used in character transfer. Each character is packed between start and stop bits. This is known as the framing.

start bit :  Always 1 bit, value is always 0

stop bit:   1 or 2 bits, value is always 1

Lets take an example. Lets say we are transferring ASCII character ‘A’ 

its binary code is 0100 0001 and 0x41 in hex.Furthermore, it is framed between a start bit and 2 stop bits. This is what its frame will look like.

 2. Parity bit

Some systems require parity bit in order to maintain data integrity.The parity bit of a character byte is included in the data frame after the stop bit(s).

 3.Rate of data transfer : Baud rate/bps

We shall explain this concept with an example. Lets say we are asked to calculate the number of bits used  and time taken in transferring 50 pages of text, each with 80×25 characters. Assuming 8 bits per character and 1 stop bit

solution

For each character a total of 10 bits is used ( 1 start bit, 8 bits character, 1 stop bit).

Therefore total number of bits  = 80 x 25 x10 = 20,000 bits per page

50 pages  implies, 50×20,000  =  1,000,000 bits.

Therefore it will take 1 million bits to transfer  this information.

The time it takes to transfer the entire data using

a. 9600 baudrate implies,  1,000,000 / 9600 = 204 seconds

b. 57,600 baudrate implies 1,000,000 /57,600 = 17 seconds

Baudrate simply means the transfer of bits per second. There are various standardized baudrate we can choose from when we program our serial communication devices.  The key thing is, both communication devices must have the same baudrate. We shall see this later on in our example code.

2. Driver development:

Create new project and name it UART_TX.

After the CubeMX window appeared, configure the clock as following:

From Pinout and Configuration, open System Core then RCC:

Set the HSE to be external Ceramic oscillator.

Now, open Clock Configuration tab and setup the clock as following:

Make sure that the input frequency is 32MHz since the WB55 Nucleo has 32MHz on board oscillator.

That all for the clock setup.

Now, we shall configure the UART.

From the schematic of the Nucleo WB55, we can find that PB6 and PB7 are connected to the UART part of the ST-Link V2 embedded within the Nucleo board:

Hence, configure the UART as following:

Set the PB6 and PB7 to as USART1_TX and USART1_RX respectively.

From pinout and configuration, select Connectivity and select USART1.

Set the mode to Asynchronous and keep the setting as default.

Save the project and this will generate the code.

In main.c

Within user code begin includes, include the stdio header file:

#include "stdio.h"

In user code begin PV, declare a variable that will act as counter:

uint8_t counter;

In user code 0, we shall retarget the printf function to push the data over UART.

To achieve this:

int __io_putchar(int ch)
{
	HAL_UART_Transmit(&huart1, &ch, 1, 10);
	return ch;
}

By using this function (int __io_putchar (int ch)), we can retarget printf to be used with UART or any other peripheral.

In user code 3 in while 1 loop:

print the counter and increment it then delay by 100 milliseconds as following:

	  printf("Counter value = %d \r\n",counter++);
	  HAL_Delay(100);

Save the project and run it on the Nucleo board.

3. Results:

Open serial terminal program, set the baudrate to 115200 and you should get this:

Happy coding 🙂

Add Comment

Your email address will not be published. Required fields are marked *