cancel
Showing results for 
Search instead for 
Did you mean: 

USART Rx problem

opiol
Associate II
Posted on February 13, 2012 at 20:16

Hello,

I have a little problem with Rx on stm32f417.

I designed my own board, byt i am putting this here, becouse i cannot find another approperiate forum.

I tried to set up UART2, but i cannot make reciving working.

HW is OK, i tested that.

void USART_Inicializace(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure2;

USART_InitTypeDef USART_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);  // 1.

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE); // 2.

GPIO_PinAFConfig  ( GPIOD, GPIO_PinSource5 , GPIO_AF_USART2) ;

GPIO_PinAFConfig  ( GPIOD, GPIO_PinSource6 , GPIO_AF_USART2) ;  

//  

//   //  Tx

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // alternate function!

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP ;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOD, &GPIO_InitStructure);

////

//   // Rx

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

  GPIO_Init(GPIOD, &GPIO_InitStructure);

//

  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);

// jeste zbyva konfigurace preruseni

// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

//

//   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);

// USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

}

Thank you.
13 REPLIES 13
Posted on February 13, 2012 at 20:28

The code looks reasonable.

How, and to what, do you have the receive pin connected too?

Can you loop back a connection from the Tx to Rx pins?

Can you see Tx data with a scope?

Have you tried Rx/Tx with a simple polled loop?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
opiol
Associate II
Posted on February 13, 2012 at 21:06

thanks for reply,

i mesured Rx with scope on via neat mcu, signals from PC are good, max232 works.

i am also able to recive characters on terminal send from mcu.

i dont know what you mean  by loop exactly, i tried this but without effect.

int main(void)

  USART_Inicializace();

  

    while (1)                                  

 {

  USART_SendData(USART2, (uint8_t)USART_ReceiveData(USART2));

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

  {}

   

 };

 

}

Posted on February 13, 2012 at 21:56

Polled echo back would look something like this, where you confirm there is some data available, and the transmit buffer is empty.

int main(void)
{
USART_Inicializace();
while(1) 
{
if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET)
{
uint16_t x = USART_ReceiveData(USART2);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, x);
}
}
}

Though it's starting to sound like an issue with how the PD6 pin is connected/soldered.

Can you loop back a connection from the Tx to Rx pins? Wire the Rx and Tx pins together externally, either beyond the MAX232, or before if removed.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
opiol
Associate II
Posted on February 14, 2012 at 22:16

It's working!

Pin6 wasn't configured as alternate function.

i just added 

GPIOD->MODER    |=  (2UL << 2*6)  ;

and it's working beautifly.

thank you for help.
jean_prieur
Associate III
Posted on April 22, 2013 at 19:01

Hello,

opiol.zbigniew, your instruction:

GPIOD->MODER |= (2UL << 2*6) ;

solve my USART Rx problem ! But I don't understand why... What this expression means ? I have another problem... I can echo the USART RX on the USART TX, but I can't collect the proper hexa messages of my USART RX frame.I know that my USART frame contain 3 hexa words but when I watch the variable 'x' I have only two hexa messages... What's the problem ? Thanks !
Posted on April 22, 2013 at 19:19

It sets PD6 in AF mode (see MODER definition in Reference Manual)

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  // << MODER ''10''

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP ;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOD, &GPIO_InitStructure);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jean_prieur
Associate III
Posted on April 22, 2013 at 20:10

Thanks Clive1, indeed I'm not obligated to put this ''moder'' instruction !

My other problem is : I want to get all of the USART hex messages in my received frame (usualy 3 hex messages). With my scope I can see that the USART Rx is repeated on the USART OUT. But I'm unable to catch (in order) my 3 hex messages... This is my code, where 'x' is a message of the receipted frame:

void
USART3_IRQHandler(
void
){
if
( USART_GetITStatus(USART3, USART_IT_RXNE) )
{
if
(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != RESET)
{
uint8_t x = USART_ReceiveData(USART3); 
while
(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
USART_SendData(USART3, x);
}
}
}

When I'm in debbug mode and I put a breakpoint on

USART_SendData(USART3, x);

and I received a MIDI message, I can watch two 'x' messages, not three... Do you have any ideas ? Thanks !
Posted on April 22, 2013 at 20:42

I don't know how you're buffering things, or why you wouldn't see data as sent. You might want to reflect on the error bits in the SR to see if those tell you anything.

My echo routine isn't very elastic, in the real world I'd use a small ring buffer to mitigate that rather than wait for TXE. The inflow/outflow here should be pretty balanced.

I wouldn't park the debugger on the code and look at the USART registers, the debugger can be made to look at the registers in an invasive manner.

You definitely need to figure out why it doesn't receive one of your bytes. From the current description, I don't know.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
dthedens23
Associate II
Posted on April 23, 2013 at 19:22

Ah, MAX232 is a 5V part.  Do you mean MAX3232 a 3.3V part?

If you are connected to a PC, you will need a few more pins than just Rx,TX, right?