C Programming Send Continuous Data Over Uart

If you've been reading the posts about STM32s that I've been writing, I owe you an apology. Usually when people write microcontroller tutorials, UART is one of the first peripherals that they talk about, and I've gone far too long without mentioning it. It is such a fundamental peripheral that I vaguely thought I'd already written about it until I got a couple of comments asking about it, so thank you for those reminders!

UART stands for "Universal Asynchronous Receiver / Transmitter", and it is a very simple serial communication interface. In its most basic form, it only uses two data signals: "Receive" (RX) and "Transmit" (TX). Since it is asynchronous (no clock signal), both devices need to use the same "baud rate", which is basically the transmission frequency measured in Hertz. If you have a baud rate of 9600, then you expect a new bit every 1 / 9600 of a second. (But technically, your actual transmission frequency will be slightly lower than the baud rate, because the standard includes extra "control" bits which are sent in addition to the actual data.)

One of the most common uses of UART is to transmit strings of text or binary data between devices. That, combined with the availability of cheap off-the-shelf USB / UART bridges, makes it a popular way to add some interactivity and a working printf(...) function to bare-metal applications.

And while a simple 2-wire UART connection is reliable enough for most purposes, there is also an extended USART standard which adds an optional "clock" line to synchronize the two devices' timing; the extra "S" stands for "Synchronous". The standards are otherwise very similar, so you might see UART and USART used interchangeably in some places. There are also a set of extra "flow control" signals, but I'm not going to talk about those or USART functionality in this post.

I will cover a few basic ways to use the STM32 UART peripherals, though:

  • Setting up the UART peripheral to send / receive data one byte at a time.
  • Implementing the C standard library's printf(...) function to send text strings over UART
  • Using interrupts to receive data as it arrives.
  • Setting up a "ring buffer" to handle continuous data reception.

If any of that sounds interesting, keep reading! The target hardware will be either an STM32L432KC "Nucleo-32" board or an STM32F103C8 "pill" board; they cost around $11 or $2-5 respectively. The "Nucelo" boards are easier to use, because they include a debugger. If you use a "pill" board, you'll also need an ST-LINK debugger and a USB / UART bridge such as a CP2102 board. And these examples are all available in a GitHub repository, if you just want a quick reference.

Basic UART Setup

Most STM32 chips contain several USART peripherals, including simpler UART peripherals which do not support the extra "synchronization" clock signal. You'll probably have at least 3 of them, but some larger chips have 8 or more since it is such a common interface. Like with other STM32 peripherals, you can check your chip's datasheet to see which pins can be used with which peripherals.

For these examples, I'll use the USART2 peripheral. If you're using an STM32L432KC "Nucleo-32" board, pins A2 and A15 are connected to its built-in debugger which will forward the serial connection over the board's USB connector. If you're using an STM32F103C8 "pill" board, pins A2 and A3 are connected to the USART2 peripheral.

The first step, as usual, is to initialize the chip by setting up the core system clock. I set up this project similarly to the ones in my previous tutorials, with a Makefile capable of building the program for different targets. You can see how that works in the GitHub repository, but tl;dr, the Makefile defines different compilation flags depending on the target chip. This is one way to adjust your code for different hardware, but you can also write separate "port" files for each target chip when your project outgrows it:

// Standard library includes. #include <stdint.h> #include <stdio.h> #include <string.h>  // Device header file. #ifdef VVC_F1   #include "stm32f1xx.h" #elif VVC_L4   #include "stm32l4xx.h" 

0 Response to "C Programming Send Continuous Data Over Uart"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel