cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L476RG USART not transmitting?

kfg4
Associate

I'm trying to send exactly 8 bits of data, and check my results with tera terminal, however I'm not seeing anything come up. I noticed that on the block diagram for the STM32L476RG nucleo board, that the APB1 Bus is also connected to AHB1, but there seems to be nothing to enable on AHB1 in order to make APB1 work.

#include "stm32l4xx.h"                  // Device header
 
void USART2_INIT(void);
void USART_write(int ch);
void delayMs(int delay);
 
int main(void)
{
	USART2_INIT();
	while(1)
	{
	USART_write('H');
	USART_write('I');
	delayMs(10);
	}
}
 
void USART2_INIT(void)
{
	RCC->AHB2ENR  |= 1;																		//Enables Clock for Port A
	RCC->APB1ENR1 |= 1 << 17; 													//Enables Clock for USUART2
	
	GPIOA->AFR[0]  |= 0x0700; 
	GPIOA->MODER   ^= 1 << 4;														//Alternate Mode for GPIO PA2
	
	
	USART2->BRR  = 0x0683;																// 9600 @16MHz
	USART2->CR1 |= 0x0008;																// Enable Tx
	USART2->CR1 |= 0x0001;																// Enable USUART		
}
 
void USART_write(int ch)
{
	//wait while TX_BUFFER IS EMPTY
	while(!(USART2->ISR & 0x0080))
	{
		USART2->TDR = (ch & 0xFF);
	}
	
}
 
void delayMs(int delay)
{
	int i;
	for(; delay > 0; delay--)
	{
		for(i = 0; i < 3195; i++);
	}
}

How do I get my USART to transmit?

2 REPLIES 2
TDK
Guru

If you use the bit definitions within stm32g4xx.h, it makes it a lot easier for others and possibly yourself to read the code, even if you don't want to use anything else from HAL. You can do what you want, of course.

If you feel a post has answered your question, please click "Accept as Solution".

Read out and check GPIO and UART registers content.

Observe the pin using oscilloscope/LA.

Guessing you use the Nucleo64 and the STLink's VCP (you might've said so) - check the STLink VCP with loopback (CN3 jumpered, target held in reset).

> GPIOA->MODER ^= 1 << 4;

Okay, nice trick; but as TDK said above, you may be better off being conservative and using the symbols defined in the CMSIS-mandated device header.

JW