RS232 (UART) on STM32F4-Discovery (STM32F429ZIT6)

2019-05-07 10:31发布

Background

Here is some of my background so that your guy could know what related knowledge I have or I don't have. I'm totally a newbie of this kind of embedded system. And I don't really know anything about electronics, I'm a pure software guy.

My only experience of embedded system is Raspberry Pi, which is quite different from STM32F4 Discovery.

I could implement a UART read / write programming by using standard Linux C library on Raspberry Pi, which is connected to a PC with an USB-to-RS232 adapter with a voltage converter IC.

My Device and development environment

  • An STM32F429I-DISCO board with STM32F429ZIT6 MCU
  • An USB-to-RS232 cable and a voltage converter IC which works quite well on Raspberry Pi.
  • Keil uVision 5 IDE.

What I would like to achieve

I would like to implement a programming on STM32F429 Discovery board that could send / receive message to / from PC. (For example, send a string to PC. And when received an string from PC, light up the LED on board)

What I have done right now

Now I've setup my Keil uVision IDE, and could compile / run the blink LED example, I could also edit that example to control LED as the way I want it to be. (Ex, turn LED on when user button is pressed and turn it off when the button is released)

So the LED part is pretty much done, the missing piece it how to communicate with STM32F4 Discovery board using RS232 cable.

My Question

  1. First, I would like to know if I understand the PIN define of this board correctly. According to this datasheet, I should connect the TX pin on DB9 to PD2(UART5_RX), RX pin on DB9 to PC12(UART5_TX), VCC pin on DB9 to 3.3V, and GND on DB9 to GND. Is this connection correct?

  2. Is there any example code on how to use UART on new HAL library that come from the latest version of Keil uVision? I've searched on the web, but most sample code or tutorial are using a function called RCC_AHB1PeriphClockCmd which seems not exist in new HAL library that come from Keil uVision.

Update

  1. My USB-to-Serial adapter is 067b:2303 Prolific Technology, Inc. PL2303 Serial Port and attached with a level shifter IC. Looks like this:enter image description here

  2. When connected to Raspberry Pi, I was following this instruction, so the VCC / GND on the IC is connected to 3.3V / GND on Raspberry Pi, I'm not sure if this is needed or not.

  3. By "HAL" library, I mean this stm32f4xx_hal_usart.c file, according to license declaration in the comments, it seems come from STMicroelectronics. Although there are some instruction in this file, it's still quite hard for me to figure out how to use this driver. For example, I can't tell the difference between Enable USARTx interface clock and Enable the clock for USART GPIOs, and I don't know how can I achieve these function. So if there is an example code, it would be a great help as I could compare it with these instructions.

enter image description here enter image description here

1条回答
够拽才男人
2楼-- · 2019-05-07 11:08

I have pushed the Code for this answer to the following github address.

The code is trivial, answers the question, and have mainly been generated by STM-Cube. The .ioc file is also available for you to modify.

There are a number of other peripherals ready to be used, but untested including

USB VCport 
Timers set for RC hobby servos, just change pulse width 500-1500
ADC  in DMA mode
SPI  for flash etc
I2C  for port expansion or whatever

I share the frustration of the OP having spent many days, weeks and months fighting to get this stuff working on both the old and new libraries. Finally in 2016 there appears to be some light from ST and I can go on to do real apps for these great boards.

In fact, in doing this task, I had to re-install my cube and all its libraries before it worked properly this may have caused anguish in others.

I'll be adding LCD and touchscreen to this 'template' to make a great little 'droidlet'.

I usually attach a bluetooth module to one of the UARTS and debug through there.

Andy Brown Andy Brown has some great C++ libraries for some of the STs. They seem to be based on the StdPeripheral libs and need to be updated so we can use them via Cube and HAL on new (and old) devices. As a community we could do this more efficiently. Any ideas and takers?

Have fun.


The canonical Solution

Update your Cube and HAL libraries to the latest
Generate a cube project
Setup all your peripherals in the dialogs
Generate code
Compile
Add your application
Test  & Debug
Optimise
Release

You need only to take care that similar peripherals on different families have inconsistencies e.g. ADC on F0 is different from F1 and F4. F3 potentially has 16 bit ADC.


You are probably doing everything correctly other than RTFM. (Read the Fineprint in the Manual)

Unless you want to fight the UART, you are better advised to implement the Virtual Comport demo which uses a standard USB cable. Let the ST be the device.

Get a copy of STCube, look in the projects/peripherals directory for USB and Virtual comport. The HAL library is the same and also has this code.

When doing work on any of the Discovery boards, fire up a copy of STM32 Cube (free from ST), and create your configuration there. It shows you what is used and what is free, a great tool and will set up USB as a bonus.

Then use the code generator to create a skeleton for KEIL project. Compile , run add your app code and :-)

you may want to look here Embitz.org they have a great IDE and even better F429iDisco examples or the ST AC6 IDE and ST page for more examples. These IDE do not have code size limits, something you will run into using the LCD, touch etc on this device.


The Rest of the Answer

Unfortunately STM have of necessity assigned most of the pins on this discovery to the LCD, touch and memory chips onboard. There is very little Async function left - they probably thought the USB would be Ok.

Q1) - The way these are normally marked you will need to 'crossover' by connecting Rx to TX and Vice versa.

Check your code by wiring 'loopback' on the UART pins and check the LEDS.

Q2) - STM Cube will generate a working Keil project for you.

There's demo code that will compile and run in stm32cube_fw_f4_v130\STM32Cube_FW_F4_V1.3.0\Projects\STM32F429I-Discovery\Examples\UART with a Keil project (MDK-ARM). You will need to change to UART5 - it is configured for USART1 PA9(Tx) /PA10(RX), once working swap to UART5, or test locally USART1 -> UART5.

Test by connecting RX to Tx on the discovery, setup Rx then TX a string all should work and the Rxbuffer should reflect this.

Then connect to your adapter all should work., LEDS should flash.

If not, swap Tx and Rx.

Check loopback on the DB9.

If you have a modem or PC on the DB9, you will need to manage the modem control signals before any of this works. (RTC, CTS, DCD, DTR, DTE) See here. Raspberry does this for you.

Disconnect the STM from the adapter and check loopback from the PC on the DB9 then on Tx Rx pins.

If you get this far, STM flashes one LED, remote the other, then you have a parity, baudrate or wordlength problem.


Below is for the 407 discovery ( original discovery) - apologies initially didn't see you have the 429 - we tend to talk of F4Discovery vs F429i-Discovery. There are now other F4 based boards as well.

On F4-Discovery board - UART5 is not available, conflicts with I2S3D which is used CS43L422 (microphone) on PC12. Same pin overloading problem as the F429

The other UARTs and USART1 are also used.

Either - Configure for USART2 or USART3

The positive is USARTS 2 and 3 have built in RTS/CTS lines which you can use.

OR

You need to solder/ unsolder jumpers on the board to disable the Microphone.

I've generated many projects with USARTS on a few different STM32 boards and they all work flawlessly. The HAL programmers unfortunately only provide drivers for fixed length with or without a timeout, there is no Readline interface.

查看更多
登录 后发表回答