AnsweredAssumed Answered

USART Configuration

Question asked by beltran.victor_manue on May 6, 2014
Latest reply on May 6, 2014 by beltran.victor_manue
Hello everyone I need to use USART2 from my STM32F3 but I can't make it work. Here is my code. If someone can find my error please help me. 

#include "stm32f30x.h"
 
/**************************************************************************************/
 
void RCC_Configuration(void)
{
  /* Enable GPIO clock */
  RCC->AHBENR                |=            RCC_AHBENR_GPIODEN;
 
  /* Enable USART clock */
  RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
}
 
/**************************************************************************************/
 
void GPIO_Configuration(void)
{
 
  /* Connect PD5 to USART1_Tx */
  GPIOD->MODER                |=            2 << ( 5*2 );                              // [ 9:8 ]                     MODER4[ 1:0 ]               =     10b               Alternate function on 
                                                                                                                             //                                                                                                                   PIN C4
     //GPIOC->OTYPER          |=           1 << ( 5*1 );                         // [ 4 ]                         OT4                                        =     1                    Output as open drain
     GPIOD->OSPEEDR           |=            3 << ( 5*2 );                              // [ 9:8 ]                     OSPEEDR4[ 1:0 ]          =     11b               50 MHz High-speed on PIN C4
     //GPIOC->PUPDR          &=      ~( 3 << ( 5*2 ) );                    // [ 9:8 ]                     PUPDR4[ 1:0 ]               =     0                    No pull-up/down om PIN C4     
     GPIOD->AFR[ 0 ]      |=            7 << ( 5*4 );
 
  /* Connect PD6 to USART1_Rx */
     GPIOD->MODER |= 2 << (6*2);  // GPIO_Mode_AF
     GPIOD->AFR[0] |= 7 << (6*4);  //  AF7
 
}
 
/**************************************************************************************/
 
void USART2_Configuration(void)
{
 
  /* USART resources configuration (Clock, GPIO pins and USART registers) ----*/
  /* USART configured as follow:
        - BaudRate = 9600 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  USART2->BRR = 72000000/9600;
     USART2->CR1 &= ~USART_CR1_OVER8; // Oversampling mode = 16 
     USART2->CR1 &= ~USART_CR1_M;  // Word length = 8 bits
     USART2->CR1 &= ~USART_CR1_PCE;  // No parity
     USART2->CR1 |= USART_CR1_TE;  // Transmitter enable
     USART2->CR1 |= USART_CR1_RE;  // Receiver enable
     USART2->CR1 |= USART_CR1_UE;  // USART enable
     USART2->CR2 &= ~(USART_CR2_STOP_1 | USART_CR2_STOP_0);


}
 
/**************************************************************************************/
 
void OutString(char *s)
{
  while(*s)
  {
    while(!(USART2->ISR & USART_ISR_TXE)); // Wait for Empty
 
    USART2->TDR =(USART2, *s++);
  }
}
 
/**************************************************************************************/
 
int main(void)
{
  RCC_Configuration();
 
  GPIO_Configuration();
 
  USART2_Configuration();
 
  OutString("This is an echo back test for USART1\r\n");
 
  while(1) // Don't want to exit
  {
    uint16_t ch;
 
    while(!(USART2->ISR & USART_ISR_RXNE)); // Wait for Char
 
    ch  = USART2->RDR; // Collect Char
 
    while(!(USART2->ISR & USART_ISR_TXE)); // Wait for Empty
 
    USART2->TDR=(USART2, ch); // Echo Char
  }
}
 
/**************************************************************************************/
 
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
  /* Infinite loop */
  while (1)
  {
  }
}
#endif

Outcomes