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