cancel
Showing results for 
Search instead for 
Did you mean: 

USART2 does n't work on STM32F302

Bont_17 Gtm
Associate II
Posted on July 11, 2017 at 14:08

Hello Everyone,

I am trying to send  string on USART2 of STM32F302 but I am not succeed ,  my Code worked on USART1 and USART3. Does any one help me ?

#ifdef __GNUC__

   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf

      set to 'Yes') calls __io_putchar() */

 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

#else

 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

#endif /* __GNUC__ */

PUTCHAR_PROTOTYPE

{

 //Place your implementation of fputc here

 //e.g. write a character to the USART

 USART_SendData(USART2, (uint8_t) ch);

 //Loop until the end of transmission

 while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)

 {}

 return ch;

}

 void usart1_init (void)

 {

  USART_InitTypeDef  USART_InitStructure;

  NVIC_InitTypeDef NVIC_InitStructure;

  /* Bit configuration structure for GPIOC PIN4 and PIN5 */

     GPIO_InitTypeDef GPIO_InitStructure;

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

 GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7);

         GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;

        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // GPIO_Mode_OUT   GPIO_Mode_AF

         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;    //   GPIO_Speed_Level_2    GPIO_Speed_50MHz

        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;             //  GPIO_PuPd_UP;

         GPIO_Init(GPIOA, &GPIO_InitStructure);

 // Configure USART2 setting:

           USART_InitStructure.USART_BaudRate = 9600;

           USART_InitStructure.USART_WordLength = USART_WordLength_8b;

           USART_InitStructure.USART_StopBits = USART_StopBits_1;

              USART_InitStructure.USART_Parity = USART_Parity_No;

           USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

           USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

           USART_Init(USART2, &USART_InitStructure);

           USART_Cmd(USART2,ENABLE);

USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

           NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;

           NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

           NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

           NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

           NVIC_Init(&NVIC_InitStructure);

}

int main(void)

{

  SystemInit();

  gpio_init();

  usart1_init ();

  // printf('Hello\n\r');

 while (1)

 {

        printf('BIGS\n\r');

          delay_ms(200);

 }

}
2 REPLIES 2
Posted on July 11, 2017 at 14:22

Looks reasonable enough, I'd probably do a few things differently.

Avoid enabling the RXNE and NVIC interrupts, I don't see a handler.

Try debugging. Does the code get stuck somewhere, or fault?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Bont_17 Gtm
Associate II
Posted on July 11, 2017 at 14:31

should I need a handler in USART2?

I Debugging this Code , TX and RX pin are n't getting any Signal but I used led on other port,  led are blinking