cancel
Showing results for 
Search instead for 
Did you mean: 

usart2 problem

gilad
Associate
Posted on February 18, 2014 at 11:59

I'm working with the STM32F051 

I have this code which works for usart1 but not for usart2.

appreciate any help

-Gilad

#include ''stm32f0xx.h''

#define UART2

#ifdef UART1

  #define USARTx USART1  

  #define DATA 0x31

#else

  #define USARTx USART2  

  #define DATA 0x32

#endif

void main(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  USART_InitTypeDef USART_InitStructure;

 

  

  /* Enable GPIO A clock */

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

#ifdef  UART2 

  /* Enable USART2 clock */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

#else  

       /* Enable USART clock */

      RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

      /* Configure the HSI as USART clock */

      RCC_USARTCLKConfig(RCC_USART1CLK_HSI);

#endif

  

  /* Configure USART Rx & Tx as alternate function */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;//GPIO_Speed_40MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  

  /* Mux out USART2 Rx & Tx */

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1/*GPIO_AF_USART2*/); // PA2 USART2_TX

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1/*GPIO_AF_USART2*/); // PA3 USART2_RX

  

  USART_InitStructure.USART_BaudRate = 115200;

  USART_InitStructure.USART_WordLength = USART_WordLength_8b;

  USART_InitStructure.USART_StopBits = USART_StopBits_1;

  USART_InitStructure.USART_Parity = USART_Parity_No;//USART_Parity_None;

  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  

  /* USART configuration */

  USART_Init(USARTx, &USART_InitStructure);

  

  /* Enable USART */

  USART_Cmd(USARTx, ENABLE);

  

  while(1)

  {

    while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET); /* Waitwhile TX full */

    USART_SendData(USARTx, DATA);

  }

}

3 REPLIES 3
chen
Associate II
Posted on February 18, 2014 at 12:53

Hi

''#ifdef  UART2 

  /* Enable USART2 clock */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

#else  

       /* Enable USART clock */

      RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

      /* Configure the HSI as USART clock */

      RCC_USARTCLKConfig(RCC_USART1CLK_HSI);

#endif''

Why are you not setting the USART clock for USART2?

gilad
Associate
Posted on February 18, 2014 at 14:31

Hi,

According to the driver this line should do it:

  /* Enable USART2 clock */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

chen
Associate II
Posted on February 18, 2014 at 15:26

Hi

''  /* Configure USART Rx & Tx as alternate function */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;//GPIO_Speed_40MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_Init(GPIOA, &GPIO_InitStructure);''

One problem is that USART2 does not come out on the same pins as USART1

USART 2 comes out on PA2 and PA3 or PA14 and PA15