AnsweredAssumed Answered

USART2 (PA2, PA3) in polling mode

Question asked by cho.henry on May 12, 2016
Latest reply on May 13, 2016 by Chernobay.Vitali.001

 

Hello

 

I am trying to send and read data via USART2 (PA2, PA3) of STM32f407.

I can successfully send and read data withInterrupt. However, I cannot read data via PA3 in polling mode.

So, I also tried to read some data viaUSART6 (PC7) in polling mode, and then I can read data successfully

Why don’t I read data via PA3 in pollingmode?

Here is my code.

Thank you so much.

 

/**

 *@brief Definition for COM port1, connected to USART2

 */

#define MKL_USART2                        USART2

#define MKL_USART2_CLK                    RCC_APB1Periph_USART2

 

#define MKL_USART2_TX_PIN                 GPIO_Pin_2

#define MKL_USART2_TX_GPIO_PORT           GPIOA

#define MKL_USART2_TX_GPIO_CLK            RCC_AHB1Periph_GPIOA

#define MKL_USART2_TX_SOURCE              GPIO_PinSource2

#define MKL_USART2_TX_AF                  GPIO_AF_USART2

 

#define MKL_USART2_RX_PIN                 GPIO_Pin_3

#define MKL_USART2_RX_GPIO_PORT           GPIOA

#define MKL_USART2_RX_GPIO_CLK            RCC_AHB1Periph_GPIOA

#define MKL_USART2_RX_SOURCE              GPIO_PinSource3

#define MKL_USART2_RX_AF                  GPIO_AF_USART2

 

#define MKL_USART2_IRQn                   USART2_IRQn

#define USART2_IRQHANDLER                                        USART2_IRQHandler

// #define MKL_INTERRUPT_MODE           1

 

/*******************************************************************************

* Function Name  : USART_Configuration

* Description    : Configure USART2

*******************************************************************************/

void MKL_USART_INIT()

{

           MKL_USART_Configuration();

           //for interrupt

#ifdef    MKL_INTERRUPT_MODE

             MKL_USART_NVIC_Config();

#endif

}

 

void MKL_USART_Configuration(void)

{                                                                                                                                   

   GPIO_InitTypeDef GPIO_InitStructure;

   USART_InitTypeDef USART_InitStructure;

   // USART 2   

   RCC_AHB1PeriphClockCmd(MKL_USART2_TX_GPIO_CLK,ENABLE);

   RCC_AHB1PeriphClockCmd(MKL_USART2_RX_GPIO_CLK,ENABLE);

   RCC_APB1PeriphClockCmd(MKL_USART2_CLK,ENABLE);

   

   GPIO_PinAFConfig(MKL_USART2_TX_GPIO_PORT, MKL_USART2_TX_SOURCE,MKL_USART2_TX_AF);

   GPIO_PinAFConfig(MKL_USART2_RX_GPIO_PORT, MKL_USART2_RX_SOURCE,MKL_USART2_RX_AF);

   

   /*

   *  MKL_USART_TX -> PA2 ,MKL_USART_RX -PA3

   */

   GPIO_InitStructure.GPIO_Pin = MKL_USART2_TX_PIN;

   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

   GPIO_Init(MKL_USART2_TX_GPIO_PORT, &GPIO_InitStructure);

   // RX

   GPIO_InitStructure.GPIO_Pin = MKL_USART2_RX_PIN;

   GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

   GPIO_Init(MKL_USART2_RX_GPIO_PORT, &GPIO_InitStructure);

   

   /*

        USARTx configured as follow:

        - BaudRate = 115200 baud 

                 - Word Length = 8 Bits

        - One Stop Bit

        - No parity

        - Hardware flow control disabled (RTS and CTS signals)

        - Receive and transmit   

   */

   

   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_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;

   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

   USART_Init(MKL_USART2, &USART_InitStructure);

   /* Enable the MKL_USART Transmit interrupt: this interrupt is generatedwhen the

           MKL_USARTtransmit data register is empty */

#ifdef    MKL_INTERRUPT_MODE  

   USART_ITConfig(MKL_USART2,USART_IT_RXNE,ENABLE);

#endif

 

   USART_Cmd(MKL_USART2, ENABLE);

}

 

void MKL_USART_NVIC_Config(void)

{

   NVIC_InitTypeDef NVIC_InitStructure;

   

   /* Enable the USARTx Interrupt */

   NVIC_InitStructure.NVIC_IRQChannel = MKL_USART2_IRQn;

   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

   NVIC_Init(&NVIC_InitStructure);

}

 

 

 

//////////////////////

// read string from MKL17 - interrupt mode

void USART2_IRQHANDLER(void)

{

 if(USART_GetITStatus(MKL_USART2, USART_IT_RXNE) != RESET)   {

     USART_SendData(Open_USART1 ,USART_ReceiveData(MKL_USART2));

  }

 

}

 

 

 

//////////////////////

// read a character - polling

char read_char_from_mkl_uart(){

   while (!USART_GetFlagStatus(MKL_USART2, USART_FLAG_RXNE));

   return USART_ReceiveData(MKL_USART2);

}

 

//////////////////////

// read string - polling

int read_string_from_mkl_uart( unsignedchar* readstring )

{

           unsignedchar t;

           intindex = 0, i=30;

 

   while (!USART_GetFlagStatus(MKL_USART2, USART_FLAG_RXNE));

           while(   (t = (uint8_t)read_char_from_mkl_uart()) != 0x0D )   {

                      readstring[index++]= t;

           }

           readstring[index]= 0;

 

           returnindex;

}

Outcomes