cancel
Showing results for 
Search instead for 
Did you mean: 

USART Recieve Problem

javadpoostini
Associate II
Posted on March 06, 2016 at 21:01

Hello Everyone.

Transferring data via USART2; I have some problem. Hope to see a recommendation!

I am doing as this : Sending data to PC on TX line normally without any Interrupt and receiving data from PC on RX line with use of Interrupt Handler.

My code is such below.

The problem occur-es when I am starting the data transfer between stm32f407 and PC; the program stops at the USART interrupt handler function and then hangs.

It seems that something is happened after the program detect RXNE flag and tries to read USART RX buffer, but it can not read the receiving buffer completely.

so stops at the middle of decoding code.

Any body have any Idea about?

Thanks.

 volatile char c; volatilechar RF_Buffer_IN[50];  int main(void) {

/* Initialize system */

SystemInit();

  /* Initialize USART */

MY_USART_initilize(19200)  

while (1) {

/* Transferring Some data via TX Line */

USART_SendData(USART2, c);   

}

}

/*---------------------------------------------------------------------------------------------*/  void MY_USART_initilize(float Buadrate)

{ GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStruct; /*-----------------------------USART2 For Sending data to PC----------------------------*/ // clocks RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA , ENABLE); /* Configure USART2 Tx/RX (PA2/PA3) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // Map USART2 to A2,A3 GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); // Initialize USART2 USART_InitStructure.USART_BaudRate = Buadrate; 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_Tx | USART_Mode_Rx; /* Configure USART */ USART_Init(USART2, &USART_InitStructure); /* Enable the USART */ USART_Cmd(USART2, ENABLE); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; NVIC_Init(&NVIC_InitStruct);

}

/* USART IRQ handler */

void USART2_IRQHandler(void) { static int rx_index = 0; if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) // Received characters modify string { GPIO_SetBits(GPIOD, GPIO_Pin_15);

    RF_Buffer_IN[rx_index++] = USART_ReceiveData(USART2); if (rx_index >= (sizeof(RF_Buffer_IN) - 1)) { rx_index = 0; }    /******* Decoding is done here ********/

}  

 

4 REPLIES 4
jogerh
Associate II
Posted on March 07, 2016 at 15:34

static int rx_index = 0;

void USART2_IRQHandler(void) {

if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) // Received characters modify string

{

GPIO_SetBits(GPIOD, GPIO_Pin_15);

RF_Buffer_IN[rx_index++] = USART_ReceiveData(USART2);

if (rx_index >= (sizeof(RF_Buffer_IN) - 1))

{

rx_index = 0;

}

/******* Decoding is done here ********/

}

Posted on March 07, 2016 at 18:01

The baud rate shouldn't be a float, and when sending characters you should wait for TXE to be true before jamming characters into the register.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
javadpoostini
Associate II
Posted on March 08, 2016 at 20:18

Hi Clive

You mean that before reading the entry (RX line), I should check for TXE to be 1 ? or when i want to send data via (TX line) I should check for TXE ?

The key point here is that when disabling transmit and only in receive mode, I still has the issue.

Thanks for clarifying....

Posted on March 08, 2016 at 21:16

I'm objecting too

while (1) {
/* Transferring Some data via TX Line */
USART_SendData(USART2, c);
}

Perhaps you need to ponder how you are debugging this? Is the part actually receiving anything? Does polling work? Can you loop back the data being sent (properly) and see that being received? If you transmit a 'U' character repetitively, can you see that on a scope, and confirm the bit timing? Are you using C++ or .cpp files? If so consider name mangling. If the baud rate or bit timing is wrong, then confirm that HSE_VALUE reflects the frequency of your external oscillator. The STM32 is not compatible with RS232 levels, it is using a 3V CMOS levels. Make sure you have the TX/RX sense correct, ie DCE vs DTE What else are you doing in the IRQHandler? Perhaps the problem is not with the code being presented.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..