Hal-driven serial programming traps

Source: Internet
Author: User

Http://bbs.elecfans.com/jishu_464356_1_1.html

Hand has a piece of Nucleo stm32l053x board, used to do serial port experiment, see the next St of the latest library Hal Drive, so want to use HAL driver to do serial port.
Using the process found that only send data can not receive data, with a logic analyzer to view the RX,TX angle, there is data, but is not receiving data.
Use stm32f103 traditional drive, a little problem, change to Hal why not? After a week of debugging finally found the problem.
First look at the stm32f103 pass-through driver code:

/** USART1 GPIO Configuration PA9------> Usart1_tx PA10------> Usart1_rx*/  /*Configure GPIO Pin:pa*/Gpio_initstruct.gpio_pin=Gpio_pin_9; Gpio_initstruct.gpio_mode=gpio_mode_af_pp; Gpio_initstruct.gpio_speed=Gpio_speed_50mhz; Gpio_init (Gpioa,&gpio_initstruct); /*Configure GPIO Pin:pa*/Gpio_initstruct.gpio_pin=gpio_pin_10; Gpio_initstruct.gpio_mode=gpio_mode_in_floating; Gpio_init (Gpioa,&GPIO_INITSTRUCT);

When we configure IO under the pass-through driver,

For TX We configure Gpio_mode to GPIO_MODE_AF_PP,

For Rx We configure Gpio_mode for gpio_mode_in_floating.

Sometimes people have the inertia of thinking that rightfully in the HAL should also be the same, I was in this place a somersault.

In the HAL Drive mode, the RX,TX must all be configured as GPIO_MODE_AF_PP.

The code is as follows: when driven with HAL

/**usart2 GPIO Configuration PA2------> Usart2_tx PA3------> Usart2_rx*/Gpio_initstruct.pin=gpio_pin_2; Gpio_initstruct.mode=gpio_mode_af_pp; Gpio_initstruct.pull=Gpio_nopull; Gpio_initstruct.speed=Gpio_speed_fast; Gpio_initstruct.alternate=Gpio_af4_usart2; Hal_gpio_init (Gpioa,&gpio_initstruct); Gpio_initstruct.pin=Gpio_pin_3; //gpio_initstruct.mode = gpio_mode_input; (This sentence must not be)Gpio_initstruct.alternate =Gpio_af4_usart2; Hal_gpio_init (Gpioa,&GPIO_INITSTRUCT);

Hope to provide some experience to the people behind, less detours.

Hal-driven serial programming traps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.