c - USART communication on STM32L476G Discovery -
i have 2 boards: nucleo-f411re , stm32l467g discovery.
i need make virtual com connection between stm32l467g discovery , pc.
for nucleo : usart2 connected st-link, can communicate pc usart on usb. so, enable gpioa , usart2 clock. after that, configure gpioa link ports 2 , 3 on usart2, , works well! can receive characters on pc.
for stm32l467g discovery : stm32l467g discovery has usart2 on port 2 , 3 on gpioa too. but, in documentation, usart2 connected st-link usart2 on port 5 , 6 in gpiod. so, have modify souce code can't receive in computer..
check sources:
main.c
#include "stm32l4xx.h" #include "stm32l476g_discovery.h" uart_handletypedef huart2; gpio_inittypedef gpio_initstruct; void configure_system_clock(void); int main(void) { hal_init(); // hal init configure_system_clock(); // configure clock __gpiod_clk_enable(); // enable gpiod clock __usart2_clk_enable(); // enable usart2 clock huart2.instance = usart2; huart2.init.baudrate = 9600; huart2.init.wordlength = uart_wordlength_8b; huart2.init.stopbits = uart_stopbits_1; huart2.init.parity = uart_parity_none; huart2.init.mode = uart_mode_tx_rx; huart2.init.hwflowctl = uart_hwcontrol_none; hal_uart_init(&huart2); /**usart2 gpio configuration pa2 ------> usart2_tx pa3 ------> usart2_rx */ gpio_initstruct.pin = gpio_pin_5|gpio_pin_6; gpio_initstruct.mode = gpio_mode_af_pp; gpio_initstruct.pull = gpio_nopull; gpio_initstruct.speed = gpio_speed_low; gpio_initstruct.alternate = gpio_af7_usart2; hal_gpio_init(gpioa, &gpio_initstruct); char *msg = "hello you\n\r"; hal_uart_transmit(&huart2, (uint8_t*)msg, 15, 0xffff); while (42); } void configure_system_clock(void) { rcc_oscinittypedef rcc_oscinitstruct; rcc_clkinittypedef rcc_clkinitstruct; __pwr_clk_enable(); __hal_pwr_voltagescaling_config(pwr_regulator_voltage_scale2); rcc_oscinitstruct.oscillatortype = rcc_oscillatortype_hsi; rcc_oscinitstruct.hsistate = rcc_hsi_on; rcc_oscinitstruct.hsicalibrationvalue = 6; rcc_oscinitstruct.pll.pllstate = rcc_pll_on; rcc_oscinitstruct.pll.pllsource = rcc_pllsource_hsi; rcc_oscinitstruct.pll.pllm = 8; rcc_oscinitstruct.pll.plln = 336; rcc_oscinitstruct.pll.pllp = rcc_pllp_div7; rcc_oscinitstruct.pll.pllq = 7; hal_rcc_oscconfig(&rcc_oscinitstruct); rcc_clkinitstruct.clocktype = rcc_clocktype_sysclk|rcc_clocktype_pclk1; rcc_clkinitstruct.sysclksource = rcc_sysclksource_pllclk; rcc_clkinitstruct.ahbclkdivider = rcc_sysclk_div1; rcc_clkinitstruct.apb1clkdivider = rcc_hclk_div2; rcc_clkinitstruct.apb2clkdivider = rcc_hclk_div1; hal_rcc_clockconfig(&rcc_clkinitstruct, flash_latency_2); }
this code works fine on nucleo nucleo-f411re (but gpioa , port 2 , 3).
on stm32l467g discovery, code totaly fail, gpiod port 5 , 6 && gpioa port 2 , 3.
i can't see problem, can me please?
you initializing uart in main , ok, not catching way hal libraries want initialize peripheral's pin. should in hal_uart_mspinit() function can find in stm32f4_hal_msp.c file.
your main should this
hal_init(); // hal init configure_system_clock(); // configure clock huart2.instance = usart2; huart2.init.baudrate = 9600; huart2.init.wordlength = uart_wordlength_8b; huart2.init.stopbits = uart_stopbits_1; huart2.init.parity = uart_parity_none; huart2.init.mode = uart_mode_tx_rx; huart2.init.hwflowctl = uart_hwcontrol_none; hal_uart_init(&huart2); hal_uart_transmit(&huart2, (uint8_t*)msg, 15, 0xffff);
then in hal_uart_mspinit(uart_handletypedef* huart) should have this:
gpio_inittypedef gpio_initstruct; if(huart->instance==usart2) { /* peripheral clock enable */ __usart2_clk_enable(); __gpiod_clk_enable(); /**usart2 gpio configuration */ gpio_initstruct.pin = gpio_pin_5|gpio_pin_6; gpio_initstruct.mode = gpio_mode_af_pp; gpio_initstruct.pull = gpio_pullup; gpio_initstruct.speed = gpio_speed_high; gpio_initstruct.alternate = gpio_af7_usart2; hal_gpio_init(gpiod, &gpio_initstruct); hal_nvic_setpriority(usart2_irqn, 0, 1); hal_nvic_enableirq(usart2_irqn); }
pay attention "gpio_pullup", suggest config gpio uart. have define uart handler in stm32f4_it.c file:
void usart2_irqhandler(void) { hal_uart_irqhandler(&huart2); }
Comments
Post a Comment