Sending text commands to STM32 with USART

If you think that your STM32 board feels lonely, you can connect it to your laptop. One of the ways how you can do that is USART. That’s probably the easiest way. For example, let’s say that we want to send text commands from a laptop to STM32 board. One command should turn on an LED, and another one should turn it off. And of course, STM32 should curse us in case of invalid command.

I used a template project for STM32 which I have described earlier. Everything that I described in that post applies here (same components, same software, same commands). But source code became a little bit more complicated:

https://github.com/artem-smotrakov/stm32f103-usb-commands

Hardware: STM32F103, ST-LINK/V2 and USB-Serial adapter

Here is a list of components we need:

As I described in my previous post, I connected an LED to PA1 pin. Then, we need to connect an USB-Serial adapter to STM32. According to datasheet for STM32F103, PA9 and PA10 pins can be configured for USART (see page #31 of the datasheet):

I hope your laptop has two free USB ports because we need one for ST-LINK/V2, and another one for USB-Serial adapter.

That’s it about hardware.

Software: STM32 and USART

I put the sources on GitHub:

git clone https://github.com/artem-smotrakov/stm32f103-usb-commands

The structure is similar to what I described in my post about template project for SMT32:

Here is what we have in main.c:

This simple project supports only two text commands:

The sources can be compiled and uploaded to STM32 board with commands like the following (you need to specify a path to STM32 standard peripheral library):

STD_PERIPH_LIBS=/home/user/libs/STM32F10x_StdPeriph_Lib_V3.5.0 \
make all
STD_PERIPH_LIBS=/home/user/libs/STM32F10x_StdPeriph_Lib_V3.5.0 \
make burn

Sending commands to USB-Serial port

If you use Linux, there should be a file in /dev associated with a USB port which you use for USB-Serial adapter. You can use “dmesg” tool to find it:

dmesg | grep tty

It should print something like the following:

[ 0.000000] console [tty0] enabled
[ 5.228613] systemd[1]: Created slice system-getty.slice.
[29158.699946] usb 1-2: FTDI USB Serial Device converter now attached to ttyUSB0
[29869.017886] ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0
[29877.238602] usb 1-2: FTDI USB Serial Device converter now attached to ttyUSB0

In my case, I had “/dev/ttyUSB0” file. You may need to set 666 access rights to it:

sudo chmod 666 /dev/ttyUSB0

After that you can send data to STM32 by writing to this file, for example:

# turn LED on
echo -e "a1 on\r" > /dev/ttyUSB0
# turn LED off
echo -e "a1 off\r" > /dev/ttyUSB0

Or, you can use your favorite terminal emulation program, for example, Minicon:

sudo apt-get install minicom
sudo minicom -s

The second command above shows a menu where you can configure minicom with proper settings for USB-Serial connection (file name, baud rate, word length, stop bits, parity and flow control). You can find those settings in usart_init() function. Then, it will offer you to save settings, and connect to the device). Or, you can just use the following command:

minicom --device /dev/ttyUSB0

That’s pretty much it. Good luck!

Originally published at https://blog.gypsyengineer.com on October 6, 2016.

STM32F103, ST-LINK/V2 programmer/debugger and USB-Serial adapter
STM32F103, ST-LINK/V2 programmer/debugger and USB-Serial adapter

I write about Java, security, electronics and DIY

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store