cancel
Showing results for 
Search instead for 
Did you mean: 

Sendig USART does not send STM32F303RE

JMart.13
Senior

I'm trying to send data via USART to a serial monitor (i'm following a course) but i can't see it's receiving the data, the course is creating its own library manipulating registers.

void UART2_WRITE(int ch); 
 
int main(void)
{
 
	//enable clock to gpioa
	mcal::reg::reg_access::reg_or();
  //enable clock to usart2	
	mcal::reg::reg_access::reg_or();
	
	//configure PA2 as UART2 TX
	//has to be configured as a alternate pin
	mcal::reg::reg_access::set_pin_alternate(); //alt pin a 2
	
	//pa3 as rx
	mcal::reg::reg_access::set_pin_alternate(); //alt pin a 6
	//set 000 0111 0000 000 to the register afr2 
	mcal::reg::reg_access::reg_or();
  mcal::reg::reg_access::reg_or();
  
	//configure usart2 baudrate to 9600
	//brr = *** / desired_baud, brr = 16 000 000 / 9 600 = 1666.6666... into hex = 0x683
	mcal::reg::reg_access::reg_set();
	
	//configure 1 stop bit
	mcal::reg::reg_access::reg_set();
	
	//configure no flow control
	mcal::reg::reg_access::reg_set();
	
	//enable UART2 UE bit
	mcal::reg::reg_access::reg_or();
	
	//enable tx, 8-bit
	// 0000 0000 0000 1000 represents the bit at TE of cr1
	mcal::reg::reg_access::reg_set();
 
	//No flow control
	while(1){
			
		UART2_WRITE('s'); 
		
		for(int i = 0; i < 180000; i++); 
		
	}
 
	
}
 
void UART2_WRITE(int ch){
	std::uint32_t _ch = ch & 0xFF;
	
  //wait until transmit buffer is empty	
	while(!(mcal::reg::USART2->ISR & (1U << 7))){} // 0x0080 corresponds to TX buffer
	//mcal::reg::reg_access::reg_set();
		
		mcal::reg::reg_access_dynamic::reg_set(mcal::reg::usart2_tdr, _ch); 
}

debugging in keil uvision i see that the TDR register stores the data I want to send, but i can't see the result in the screen. I don't know if i'm configuring this the right way.

edit: for some reason the code is not showing completely so i paste the complete main here: https://pastebin.com/mFqjfJDu?fbclid=IwAR26CXZr1CdsFQMkwkblc-RHu1Tc1z7n_rUGz-XsSNYXO6B_Bvwx46LmqRQ 

Thanks in advance.

6 REPLIES 6
JMart.13
Senior

for some reason, the part before the while loop is not showing right, but it is writing the registers ok.

Read out and check/post content of UART and relevant GPIO registers.

What is your hardware? Did you observe the Tx pin using oscilloscope/logic analyzer? Did you verify the path to PC is OK, using plain loopback?

JW

Hi, i'm new in the community so i didn't know that, apologize. Unfortunately, I don't have an oscilloscope to check if TX is sending the data, I am using a stm32f303re nucleo board. And what do you refer to in the last part? plain loopback?

> I don't have an oscilloscope

Get one. Or at least a basic USB-connected logic analyzer.

> And what do you refer to in the last part? plain loopback?

You would normally check the serial port coming from the PC so, that you would short Rx and Tx, and when typing in the terminal program, you should see the same characters to arrive.

But let's assume the virtual serial port buit in the 'F303 Nucleo works OK.

Then check the solder bridges which connect this port between STLink and the target 'F303, and write a simple program, which does nothing just sets the pin which would be USART_Rx to GPIO Input, the pin which would be USART_Tx to GPIO output, and then in an infinite loop, read the Rx pin state and write it into the Tx pin. Again, typing in the terminal in PC, you should see characters to be echoed.

If this works, try then your program working. As I've said above, read out and check the UART and relevant GPIO registers against values expected based on reading the respective chapters in the reference manual. I assume you have clocks set up properly; that might be another set of readouts and checks, this time from RCC.

JW

ok thanks, i will check and i'll do a test.

Thanks again.

JMart.13
Senior

Ok, Solved the problem, it seems my board does not work at 16MHz, it works at 8MHz, another thing was a function I thought was bad, In the function, I was writing to other registers and i didn't saw that, and was not putting the GPIO as alternate.