cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with UART on STM32 Nucleo (F411re.)

Npiet.1
Associate II

I am new to Nucleo board STM32F411re.

I am trying to work out an UART example on the devlopment board

I am trying to send data through UART - TX line and trying to read that data using Putty.

I can't able to see data in Putty. When I debugged I saw data was not written to DR registers.

// ----------------------------------------------------------------------------
 
#include <stdio.h>
#include <stdlib.h>
#include "diag/Trace.h"
#include <stm32f4xx_hal.h>
#include "stm32f4xx.h"
#include "stm32f4xx_hal_gpio.h"
#include "stm32f4xx_hal.h"
#include "stm32f4xx_usart.h"
 
// ----------------------------------------------------------------------------
//
// Standalone STM32F4 empty sample (trace via DEBUG).
//
// Trace support is enabled by adding the TRACE macro definition.
// By default the trace messages are forwarded to the DEBUG output,
// but can be rerouted to any device or completely suppressed, by
// changing the definitions required in system/src/diag/trace_impl.c
// (currently OS_USE_TRACE_ITM, OS_USE_TRACE_SEMIHOSTING_DEBUG/_STDOUT).
//
 
// ----- main() ---------------------------------------------------------------
 
// Sample pragmas to cope with warnings. Please note the related line at
// the end of this function, used to pop the compiler diagnostics status.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#pragma GCC diagnostic ignored "-Wreturn-type"
 
void init_periperalclock()
{
	  /* Enable GPIO clock */
	  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;   //Port A clock enable
 
	  /* Enable USART clock */
	  RCC->APB2ENR |= RCC_APB2ENR_USART1EN;  //PA9 Serial_1 TX & PA10 Serial_1 RX
}
 
void init_gpiopins()
{
 
	/* GPIO Configurations for PA9 and PA10 which is the pins for USART1 */
	GPIOA ->MODER |= GPIO_MODER_MODER9_0;// PA9 Serial_1 TX //1<<19;
	GPIOA ->OTYPER|= 512;//;// 1<<9
	GPIOA ->OSPEEDR |= 512;//GPIO_OSPEEDER_OSPEEDR9_0; // 1<<9
	GPIOA ->PUPDR |= GPIO_PUPDR_PUPDR9_0 ; //1<<19
	GPIOA->AFR[1]|= 9<<4; // PA9 AFR enable
 
	GPIOA ->MODER |= GPIO_MODER_MODER10_0;// PA10 Serial_1 RX //1<<20;
	//GPIOA ->OTYPER|= GPIO_ODR_ODR_10;// 1<<10
	GPIOA->AFR[1]|= 10<<8; // PA10 AFR enable
 
	/* GPIO Configurations for PA2 and PA3 which is the pins for USART2 for loopback setup */
 
}
 
void init_UsartConfiguration()
{
	/* BRR = Systemclock / ((Over2 - 0) * Baudrate)
	BRR in Decimal fraction is 0d546.875 == 0x222 (0d546 <--> 0x222)  & (0.875*16 == 14) (0d14 <--> 0xE)
	Thus BRR --> 0x222E*/
 
	USART1->BRR = 0x222E;
 
	USART1->CR1 &= ~USART_CR1_OVER8; // Oversampling mode = 16
 
	USART1->CR1 &= ~USART_CR1_M;  // Word length = 8 bits
 
	USART1->CR1 &= ~USART_CR1_PCE;  // No parity
 
	USART1->CR1 |= USART_CR1_TE;  // Transmitter enable
 
	//USART1->CR1 |= USART_CR1_RE;  // Receiver enable
 
	USART1->CR1 |= USART_CR1_UE;  // USART enable
 
	USART1->CR2 &= ~(USART_CR2_STOP_1 | USART_CR2_STOP_0);  // STOP[1:0] = 00 thus zero stop bits
}
 
 
int USART_SendChar(value) {
   //while (!(USART1->SR & USART_SR_TXE)); //check Bit 7 TXE: Transmit data register empty (1: Data is transferred to the shift register)
 
   USART1->DR = value;
   while(!(USART1->SR & USART_SR_TXE)); //check Bit 7 TXE: Transmit data register empty (1: Data is transferred to the shift register)
   return 0;
 }
 
int
main(int argc, char* argv[])
{
  // At this stage the system clock should have already been configured
  // at high speed.
	int value = 96;
	init_periperalclock();
	init_gpiopins();
	init_UsartConfiguration();
	trace_printf("System clock: %u Hz\n", SystemCoreClock);
	trace_printf("Value:",value);
	while(1)
	{
		USART_SendChar(value);
	}
}
 
 
 
#pragma GCC diagnostic pop
 
// ----------------------------------------------------------------------------

0 REPLIES 0