Thursday , 29 June 2017
iten
Home » Electronics » How to use STM32 Nucleo serial port

How to use STM32 Nucleo serial port

As we have seen in the previous tutorial about this new developing board from ST, the STM32 Nucleo provides an integrated ST Link v2.1 interface. ST Link is mainly designed to allow flashing of target MCU trough the mini-USB interface. But, it provides at least another really useful feature: a Virtual COM port. When you install the ST-Link drivers, a new device appears in your hardware devices list: the ST-Link Virtual COM port.

2015-01-24 08_38_11-Device Manager

If you use a Linux PC or a Mac, you'll find a new terminal in the /dev directory. Usually, this device is named something similar to tty.usbmodemXXXX, as shown below.

Schermata 2015-01-24 alle 08.40.43

The serial port is mostly useful for two reasons: if you want to debug your firmware printing messages (not strictly necessary with the ARM architecture, since we can also use ARM semihosting) or if you want to exchange commands and messages between your Nucleo board and your PC (perhaps, using a dedicated application you are building).

In this post I'll show you how to properly configure and use the integrated virtual COM port of STM32 Nucleo board. But, before we start coding, it could be really useful take a look to the hardware. ST provides the full hardware project of the STM32 Nucleo (the board is designed using the Altium Designer CAD, a professional CAD used in the electronics industry, but you are not required to have a so expensive piece of software to use your Nucleo). I'll assume the Nucleo-F401RE model, but it should be really easy to rearrange instructions to properly use your specific Nucleo.

First: pinout

A complex yet flexible MCU like the STM32 provides I/Os that have "overloaded" functionalities.This means that, before we can use a peripheral (in our case, the USART), we need to configure the peripherals associated to corresponding pins. Looking to STM32CubeMX tool, we discover that the STM32F401RETx processor has 3 different USARTs: USART1, USART2 and USART6.

2015-01-24 08_52_46-STM32CubeMX uart2.ioc_ STM32F401RETx

Now we have to take a look to the Nucleo schematics. As we can see in the following picture, the USART_TX and USART_RX ports are connected to PA2 and PA3 pins. This means that the Nucleo board is configured to use the USART2 peripheral of target MCU.

stm32-nucleo-usart-pinout

Ok. We've grabbed all the necessary information related to the hardware needed to start coding.

Second: the code

Before we start configuring the USART2 peripheral, we need a test project. We'll generate an empty project using the GCC ARM Eclipse plug-in, as shown in my series about the GCC toolchain for the STM32 platform. When you generate the test project, you can using the following configuration parameters.

Schermata 2015-01-24 alle 09.17.09If you have followed my previous tutorial about GNU Eclipse plug-in, you already know that the plug-ins generates an incorrect clock configuration for the Nucleo-F4 board. I've shown how to use the STM32CubeMX tool to generate the right clock initialization code. For the sake of simplicity, this is the code you have to put inside the _initialize_hardware.c file.

Next, we have to add a function to configure the USART interface. We call it MX_USART2_UART_Init, as shown below.

The function is really self-explaining. huart2 is an instance of UART_HandleTypeDef descriptor. It's a struct used to configure the UART peripheral. However, this code is still not sufficient to use the UART. We need to configure the hardware part, setting the right pins and initializing the right clocks associated to UART peripheral. This work is done with the following hook function:

Even in this case, the code is really self explaining. First, we need to initialize peripheral clock for PORTA GPIOs. Next, we need to enable the clock associated to UART2 peripheral. Finally, we have to proper configure PIN2 e PIN3 as UART2 TX and UART2 RX.

An important aspect to remark is that we don't need to explicit call this function in the initialization section. This is an hook function automatically called by the HAL_UART_Init() function which we call inside the function MX_USART2_UART_Init().  The right place to call this initialization function is inside the __initialize_hardware() function in the  _initialize_hardware.c file.

Ok. Now we only need to write a simple main() function to test the UART.

The main() is really simple: it simply prints a message on the UART and hangs for ever.

Before we can compile the whole project, we need to do another final operation. By default, the GNU-ARM plugin for Eclipse disables unused STM32 HAL files, in order to speed up compile operation. So, we need to enable the compilation of stm32f4xx_hal_uart.c file.
Go in Project Explorer->system->src->stm32f4-hal and click with mouse right button on the stm32f4xx_hal_uart.c file, as shown in the following picture:

Schermata 02-2457080 alle 13.25.27

Click on "Properties" and go to C/C++ Build and uncheck "Exclude from build", as shown below.

Schermata 02-2457080 alle 14.10.44

Ok. Now we can compile the test project and upload on our Nucleo board using GDB and OpenOCD.

To see messages on the UART you have several options according the Operating System you use. On the Eclipse Marketplace you'll find several terminal emulator plug-ins. TCF is one of these. Another option for Windows OSes is to use putty. On Mac and Linux ckermit is a suitable option.

Schermata 02-2457080 alle 15.08.19

You can download the whole project from my github repository.

In a next article, I'll show you how to use the others Nucleo UARTs. Stay tuned 😉


Check Also

Correct way to perform re-annotation of designators in Altium

It's really common that at the end of the board layout we have that all …

35 comments

  1. I guess there just aren't any tutorials which touches Eclipse IDE, just aren't!.
    This is due the inconsistency mess that comes with any Java tools....
    I consider I am an experienced engineer (coinscode.com)
    and I was struggling entire day to get one simple ST Micro
    compiled under Linux and I could not. I gave up. Though, the
    tutorial is complete but Eclipse screw it up totally, and screws up
    any decent programmer.

    • Carmine Noviello

      Hi Marius,
      I agree with you: Eclipse can transform every programmer in a serial killer, especially if you have a non so recent PC. I've also successfully setup a complete working toolchain based on a Makefile and a simple text editor like VIM. I'll spend two words about this soon.

      • But there's also Code::Blocks which runs much more smoothly than eclipse - and guess what, it's not Java based 😉
        I've read about some people using it for gcc arm embedded work, have not tried embedded dev in c::b myself

  2. Hey, just want to say thanks for the post. Clear explanation and great write-up. I'm actually using Keil uvision but it's still very relevant, thanks

    • Is it possible for you to add HAL_UART_Receive() function? I tried to look everywhere but can't find any examples on Receiving from Serial monitor/keybaord.. Thanks!

  3. Hello, thanks for all tips. I'm trying to get UART working for quite some time now and it's driving me nuts I can't do it. I'm using CUBE + IAR and HAL. Any tips why instead of getting normal characters I'm receiving garbage? I'm using HAL_UART_Transmit. What I'm missing?

    • Hi,
      to give a better help I need to know the type of your Nucleo board. Moreover, past here the clock and UART configuration routines.

  4. Hi Carmine,
    Its a great tutorial I must say. But do you have a an example to interact with the STM32 Nucleo over serial port.I am trying it out using the Serial APIS on windows and can get the data on the STM32 Nucleo (with few glitches).Just wondering if you have a working - clean example for the same.
    Eager to hear from your side.!
    Rgds,
    Rp

  5. Hi Carmine,
    OK , I followed the instructions from your book and able to create the Eclipse Environment. I am able to build and flash the hello-nucleo image and see the LED in action.
    Just wondering is it that simple to use the projects you have uploaded on git.I tried with CH8 chapter for f401re to test the serial port application. But I am getting this error :

    make all
    Building file: ../system/src/stm32f4xx/stm32f4xx_hal.c
    Invoking: Cross ARM C Compiler
    arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -Og -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-move-loop-invariants -Wextra -g3 -DDEBUG -DOS_USE_SEMIHOSTING -DTRACE -DOS_USE_TRACE_SEMIHOSTING_DEBUG -DSTM32F401xE -I/Users/cnoviello/STM32Toolchain/mastering-stm32/nucleo-f401RE/include -I/Users/cnoviello/STM32Toolchain/mastering-stm32/nucleo-f401RE/system/include -I/Users/cnoviello/STM32Toolchain/mastering-stm32/nucleo-f401RE/system/include/cmsis -I/Users/cnoviello/STM32Toolchain/mastering-stm32/nucleo-f401RE/system/include/stm32f4xx -I/Users/cnoviello/STM32Toolchain/mastering-stm32/nucleo-f401RE/system/include/cmsis/device -std=gnu11 -MMD -MP -MF"system/src/stm32f4xx/stm32f4xx_hal.d" -MT"system/src/stm32f4xx/stm32f4xx_hal.o" -c -o "system/src/stm32f4xx/stm32f4xx_hal.o" "../system/src/stm32f4xx/stm32f4xx_hal.c"
    ../system/src/stm32f4xx/stm32f4xx_hal.c:54:27: fatal error: stm32f4xx_hal.h: No such file or directory
    compilation terminated.
    make: *** [system/src/stm32f4xx/stm32f4xx_hal.o] Error 1
    system/src/stm32f4xx/subdir.mk:213: recipe for target 'system/src/stm32f4xx/stm32f4xx_hal.o' failed

    Or I may be missing something very minuscule here. While I try to figure it out can you please tell if you have any serial Application running on windows(even a console line will do) which can send a string pattern ( N number of times - different string with different length).

    C:\STM32Toolchain\projects\nucleo-f401RE\src\ch8 is the file I am referring to currently to test the serial communication (reception part mostly).

    C:\STM32Toolchain\projects\nucleo-f401RE is the project I am referring to .

    Thanks and Rgds,
    Rp
    And yes Merry X-Mas !!

  6. OK I fixed this one by fixing the Include Paths for GNU C and C++.But I am really looking for a way(a windows application which can send data as I said previously) to STM32 and later can also process it.
    Thanks in advance !
    Rgds,
    Rp

    • Hi,
      For the Windows application you can take in consideration to create a simple Python script with pyserial module. It would be really easy to do the app you need.

  7. ok thanks for the info /I will try it out.
    can it also send the end of line characters at the end of the line read form the text file?

  8. thanks, very useful! got it to work faster than feared.
    do you have also examples using the interrupt driven uart?

  9. Hello Carmine. All great posts. Great Job. I have followed these instructions to get this working with my STM32F401RE and could build it. I did veer from the path a but by enabling TIM1 in CubeMX using FreeRTOS as my OS. What I am getting is a TIM6_DAC_IRQn is undeclared in the stm32f4xx_hal_tim_template.c file and TIM6 undeclared in the in the same file.
    How can I be able to clear this up so can build the project?

  10. I facing compilation error :

    ./src/_initialize_hardware.o: In function MX_USART1_UART_Init':
    C:\Users\wi2wi\stm4f32xx\test\Debug/../src/_initialize_hardware.c:152: undefined reference to
    HAL_UART_Init'

    I have check hal_uart.h file properties exclude resource from build

    Please help me to solve this error.

  11. Hi Carmine,
    First I would like to say that your book has been very helpful! Definitely worth the $20. But when I got to chapter 8 and couldn't get the serial communication to work. I used the code from this tutorial (not the one in the book) and compiled it successfully, opened ckermit (I'm using Ubuntu), and realized my device is not showing up in the dev folder shown here: http://pastebin.com/sxyJVJQJ
    I tried "set line stlinkv2_5" and "set line /dev/tty" in ckermit and neither one worked. Any help would be great!

    • Carmine Noviello

      Hi Tyler,
      can you post the output from the "dmesg" command? I'm interested to the part when the ST-LINK interface is attached to the USB port. It looks an issue related to drivers.

      • I am working on a STM32F4 Disco board on Ubuntu with OpenOCD and Eclipse. I have read that I cannot do this via the usb cable connected to CN5 because I do not have a nucleo board. Would it work if I bought a USB to UART or USB to Serial? Would my computer recognize it as a COM device? I am trying to get it to work like the output of this 20 second video: https://www.youtube.com/watch?v=hHdDslVI7O0 I have already changed the nucleo code to fit the disco board by changing the pins and baud rate. I will post a link to the source and the output of "dmesg" tomorrow morning. Thank you for taking the time to help me. It means a lot.

        • Carmine Noviello

          Hi Tyler,
          Like all ST development boards (Nucleo, Disco, Eval), the STM32F4-Disco has an integrated ST-LINK interface, which also provides a VCP. So, by connecting it to the USB you should be able to send UART message to the PC. It's a matter of drivers.

          The CN5 allows you to test USB OTG features, but it's not what you are looking for (it doesn't help having a UART to USB cable).

          • I'm confused because I am getting different answers between you and the ST forums: https://goo.gl/B2IKXN This is the output of "dmesg" when just the CN1 cable is connected. When CN1 and CN5 are connected to the computer, all of the LEDs on the board turn off (even when in DFU mode). Regardless of the configuration, I cannot get the device /dev/tty* to appear.

          • Carmine Noviello

            Ummm, I don't have direct experience with the STM32F4-Discovery board. I thought it has the VCP as the most of ST dev boards, but it seems that this feature is only possibile in the "more recent" STM32F4-DISC1. But it is also required do modify the board, as described here at page 13.

            You so need an external UART-to-USB dongle to interface one of the 407VG UARTs.

          • I will purchase one. Thanks for the help!

          • Carmine Noviello

            Yes, I think it's the best option. Because the other solution suggested by the user markt on the ST forum requires that you have a quite good knowledge of this platform, and that you are able to configure the USB stack from ST to create a "custom" VCP port. A 5$ UART->USB converter is the best choice in this phase.

  12. I apologize, I did not link the output. This is the output of "dmesg" http://pastebin.com/FuTf7NW4

  13. controller : STM32F411RE

    I am using USART2 as serial port with interrupt based. I enable NVIC in stmcubemx, its generated code whenever data is transferred from terminal interrupt is coming but uart state is HAL_UART_STATE_READY. Because of that its going to receive function UART_Receive_IT function and came out. Please help solve this issue and also I need guidance how to make uart as a interrupt mode.

  14. Hello,

    I have one query. Can we use multiple uart to receive data using STM 32F207 Nucleo 144 board?? If yes do we Need to use multiple bufferes or Need to use timers??

    I want to initilaise UART1,2,3 to receive data flowing through communication bus System.

    • Yes, you are free to use as many UARTs as you need. Clearly, the application needs to be designed accordingly. Using multiple buffers is a possible solution (and probably the simplest one), but it depends a lot on the way you arrange your application.

      There is no need to use timers, I guess.

      • Hi carmine,

        I'm using Nucleo F207ZG board in that i tried to make USB_CDC

        I attached St in build link with USB Micro B cable (CN1) to ubuntu machine and also connected Micro B cable (CN13) to another USB port of same ubuntu machine but it not showing properly , Ubuntu machine detect only St-link.

        In usbd_conf.c I did hpcd_USB_OTG_FS.Init.vbus_sensing_enable = DISABLE;

        And Heap size as 4000 and stack size as 2000

        Then aslo it's STM Target board not detect , what i want to do ? Is there is problem in USB Cable whic i fixed Micro B usb cable in CN13

        And what about jumper JP4 , (I'm using USB as device only so i OFF it )

Leave a Reply

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