cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 CubeMX UART

Nicolas Cmb
Associate III
Posted on May 11, 2017 at 15:27

Hello St Community,

I'm working on STM32F103 RB microcontroller, I've download STM32CubeMX and STM32CubeF1 .

I want to show a message on my computer, thanks to a terminal, which I code on my STM32.

I know that an example exist in the STM32CubeF1 but I don't succeed to enable and show the messsage on the terminal.

I use Putty for the terminal and configure as thay say.

Q1: What terminal can I use to do what I want?

Q2: Is it possible to, when I pressed the button send the message in a loop and when I pressed a second time it stop

Thanks to replie to my questions

Nicolas0690X00000606yVQAQ.png
8 REPLIES 8
Nesrine M_O
Lead II
Posted on May 11, 2017 at 15:53

Hi

Cmb.Nicolas

,

I supposed that you are using theSTM32F103RB-Nucleo board

The example under the

STM32CubeF1

package is the following : STM32Cube_FW_F1_V1.4.0\Projects\STM32F103RB-Nucleo\Examples\UART\UART_HyperTerminal_DMA

  • - STM32F103RB_Nucleo Set-up - Connect USART1 TX (PA9 - D8 on CN5) to RX pin of PC serial port (or USB to UART adapter) and USART1 RX (PA10 - D2 on CN9) to TX pin of PC serial port.
  • - Hyperterminal configuration: -

    Word Length = 7 Bits

    - One Stop Bit - Odd parity - BaudRate = 9600 baud - flow control: None

-Nesrine-

Posted on May 11, 2017 at 15:59

Hi Nicolas,

To make a correct communication between the microcontroller and you computer you should have several things as a usb-to-serial adapter, to you have it?

Giving answer to your question:

Q1. You can use any terminal, the only thing to take on count is to configure it rigth (I use hercules and works really fine to me).

Q2. Off course is possible to make a code with that works like that, the best way is to do (in my opinion) is making a flow char and after that convert in to code.

For give to you a better help I would like that you post some screenshots of the Cube configurations or the .ioc file to check it, and if its possible a figure with the schematic of your implementation whit the pins that you are ussing for serial port, with that we could suggest to you a code. 

Posted on May 11, 2017 at 16:22

Hi

ELMHIRI.Syrine

and thanks for your help,

Yes you're right I talked about

STM32F103RB-Nucleo board,however I think that the word lenght has to be on 8 bits (8 Bits = 7 data bit + 1 parity bit).

My other question is:

I haven't find into the HAL_library, the eventwhen you push the button (not when you pressed or release).Is this exist?

It's for my project; I have to send some messages in terms of numbers of pushing the button.

I actuelly do a little program on when I pressed the button, it switched the lightning rate Led; but this wasn't really accurated.

-Nicolas-

Posted on May 12, 2017 at 08:32

Hi Elkin,

In fact, I have just an USB-to-microUSB connector but not an 

USB To Serial Adapter. I think this

http://www.mouser.fr/ProductDetail/Gravitech/USB-SER-NANO-5/?qs=sGAEpiMZZMvq007EO%252bXAYYlbvgKp85ZE

should work for the UART.

My configuration is the following one :

0690X00000606zJQAQ.png

0690X00000606yxQAA.png

I will transmit and receive messages with the UART via PIN PA_10 and PA_9

I enable the CAN communication for another program which will sends message to another STM32F103 RB microcontroler

Thank you for your help,

-Nicolas-

Posted on May 13, 2017 at 04:33

Hi Nicolas,

Well, if you have a Nucleo board, you have an advantage, the ST-Link microcontroller that is embeddedfor programming works not only as ST-Link but generates a virtual serial port also (Cool, don't?), for fast testing is really good and helpful, because you don't need an external USB-Serial converter. If you are not quite sure about how to use it, you can refer to the Nucleo's user manual (UM1724), page

0690X00000606kZQAQ.png

So, for a fast testing, as said in the user manual you have to configure USART2 in pins PA2 and PA3. I'm going to show you a little example about how to use the CubeMX and how to make a test code for System Workbench, I'm using a F030 Nucleo board, but in the practice works same.

In the Cube, select the micro an create the new project, and cofigure the USART2 in pins PA2 and PA3, also configure pin PC13 for the user switch as GPIO input.

0690X0000060702QAA.png

After that, go to configuration tab and configure the peripherals: USART2 at 9600bps, 8 bits including parity

, parity none, and 1 stop bit. And name your GPIO, I'll call it SWITCH.

0690X00000606kfQAA.png

0690X0000060707QAA.png

After that configure the outputs of the project, choose you IDE, I'll select SW4STM32, and name your project:

0690X000006070CQAQ.png

And after that generate the code:

0690X000006070HQAQ.png

Well, now pass to System Workbench, and import the code to your workspace:

0690X000006070MQAQ.png

After the project have been imported, open the main.c file that is in Src folder, and enter to stand on the mainline:

0690X000006070RQAQ.png

As your peripherals are initialized, you can use those rigth now with the HAL libraries, so, I want that when I press the switch, send a message, and don't send anymore while is pressed, so I declare a char array for the text and make the code, the switch button is normaly High, and when is pressed goes to Low. For the uart, past the arguments that are on the figure, be sure that timeout be enough to send all the characters (20ms)

0690X00000606w2QAA.png

With those code lines now is time to build the project, just make clic on hammer:

0690X00000606vIQAQ.png

If the build is right, now is moment to programming your board, so make right clic the project, select target and program chip:

See attachements

After that, the board is now programed, so lets test the uart communication:

See attachements

Check which serial port is assigned to the ST-Link:

See attachements

And almost for finish, in your favorite terminal software configure the serial terminal, I'm using Hercules, so I set, port COM7, 9600 bps, 8 bits data size, no parity, open the port and press the switch, every time you press the button, the message should appear on the terminal:

See attachements

Hope this info be useful not just for you but for all community!

Posted on May 13, 2017 at 13:53

in STM32Cube_FW_F1_V1.4.0/Drivers/BSP/STM32F1xx_Nucleo/ you will find stm32f1xx_nucleo.h and stm32f1xx_nucleo.c

those are the board support package. they have hooks to make it easy to use the LEDs and buttons

Posted on May 13, 2017 at 17:52

Thank you so much for helping me

Posted on May 13, 2017 at 17:53

Thank you I will see that