cancel
Showing results for 
Search instead for 
Did you mean: 

I am using STM32G431RBTx nucleo 64 board. How to use USART2 Tx? I have written one in stm32cube IDE but cannot get the output.

TSing.2
Associate II
 
3 REPLIES 3

So you've likely coded your solution incorrectly. Either missing clocks, gpio settings, AF mux configuration, etc. It would be easier to diagnose if the salient code sections where pasted/provided.

Watch also for potential conflicts with pins the ST-LINK/V2-1 uses for the VCP

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
TSing.2
Associate II

I am not sure about the clock use of 24 MHz, pasting the code here:

#include <stdint.h>

#include "stm32g431xx.h"

/* */

#define GPIOAEN (1U<<0) //clock enable for port A

#define UART2EN   (1U<<17) // UART2 is is connected to APB1 bus and to enable clock, APB1ENR PIN 17 should be 1

#define CR1_TE (1U<<3)

#define CR1_UE (1U<<0)

#define ISR_TXFE (1U<<23)

#define SYS_FREQ 24000000

#define APB1_CLK SYS_FREQ

#define UART_BAUDRATE 115200

static void uart_set_baudrate(USART_TypeDef *USARTx, uint32_t PeriphClk, uint32_t BaudeRate);

static uint16_t compute_uart_bd(uint32_t PeriphClk, uint32_t BaudeRate);

void uart2_write(int ch);

void uart2_tx_init(void);

int main(void)

{

uart2_tx_init();

while(1)

{

uart2_write('Y');

}

}

void uart2_write(int ch)

{

/* Make sure the UART2 DR is empty */

while(!(USART2->ISR & ISR_TXFE)) {}

/* Write the data to UART2 DR */

USART2->TDR = (ch & 0xFF);

}

void uart2_tx_init(void)

{

/* ************ configure UART2 GPIO pins ********** */

/* Enable clock access to GPIOA*/

RCC->AHB2ENR |= GPIOAEN;

/* Set PA2 (Tx) to alternate function mode */

GPIOA->MODER &= ~(1U<<4);

GPIOA->MODER |= (1U<<5);

/* Set PA2 (Tx) to alternate function type to UART2_TX AF7*/

GPIOA->AFR[0] |= (1U<<8);

GPIOA->AFR[0] |= (1U<<9);

GPIOA->AFR[0] |= (1U<<10);

GPIOA->AFR[0] &= ~(1U<<11);

/* ************ configure UART2 module ********** */

/* Configure the clock access to UART2 */

RCC->APB1ENR1 = UART2EN;

/* Configure the baud rate of UART2 */

uart_set_baudrate(USART2, APB1_CLK, UART_BAUDRATE);

/* Configure the transfer direction */

USART2->CR1 = CR1_TE;

/* Enable the UART module */

USART2->CR1 |= CR1_UE;

}

static void uart_set_baudrate(USART_TypeDef *USARTx, uint32_t PeriphClk, uint32_t BaudeRate)

{

USARTx->BRR = compute_uart_bd(PeriphClk, BaudeRate);

}

static uint16_t compute_uart_bd(uint32_t PeriphClk, uint32_t BaudeRate)

{

return((PeriphClk + (BaudeRate/2U))/BaudeRate);

}

can you help me out with the code pasted. I cannot go ahead.