cancel
Showing results for 
Search instead for 
Did you mean: 

recieving data from usart

karoui
Associate II
Posted on April 13, 2013 at 01:10

Hi,

I am trying to execute a simple program that sends ''ABC '' from a pin  to another via usart. i can send but can not recieve and while debugging the cursor is blocked in this line :

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

it means that the USART's  register RD doesn't recieve any data? can you help me to solve this problem?

this is mi code 

#include ''stm32f4_discovery.h''

#define TxBufferSize   (countof(TxBuffer))

#define countof(a)   (sizeof(a) / sizeof(*(a)))

/* Declare the GPIO structure */

GPIO_InitTypeDef  GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

uint8_t TxBuffer[] = ''ABC'';

uint8_t RxBuffer[TxBufferSize];

int i;

/* Private function prototypes -----------------------------------------------*/

void Delay(__IO uint32_t nCount);

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

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

int main(void)

{

  /* GPIOD Periph clock enable */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

  

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_Init(GPIOD, &GPIO_InitStructure);

  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

    GPIO_InitStructure.GPIO_OType =GPIO_OType_PP;

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

  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  

  /* Configure USARTy */

  USART_Init(USART2, &USART_InitStructure);

  

  /* Enable the USARTy */

  USART_Cmd(USART2,ENABLE);

  /* Enable the USART Receive interrupt */

 

 /* Initialize Leds LD3 and LD4 mounted on STM32VLDISCOVERY board */

  STM_EVAL_LEDInit(LED3);

  STM_EVAL_LEDInit(LED4);

  

 STM_EVAL_LEDOn(LED4); 

  Delay(0xAFFFF);Delay(0xAFFFF);Delay(0xAFFFF);

  STM_EVAL_LEDOff(LED4);

  STM_EVAL_LEDOn(LED4); 

  i=0;

  while(i<TxBufferSize)

  {

    USART_SendData(USART2, TxBuffer[i]);

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

    {

    }

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

    {

    }

    RxBuffer[i] = USART_ReceiveData(USART2);

    i++;

  }

    

    

  while (1)

  {

     if(RxBuffer[0]=='A')

    {

    STM_EVAL_LEDOn(LED3);

   STM_EVAL_LEDOn(LED4);    

    Delay(0xAFFFF);

    STM_EVAL_LEDOff(LED3);

   STM_EVAL_LEDOff(LED4);

    Delay(0xAFFFF);

    }

  }

}

7 REPLIES 7
Posted on April 13, 2013 at 01:44

Where do people learn logical thinking and programming these days?

Still pretty lame, but non-blocking

i = 0;
j = 0;
while((i<TxBufferSize) || (j<TxBufferSize))
{
if ((i<TxBufferSize) && (USART_GetFlagStatus(USART2, USART_FLAG_TXE) != RESET))
USART_SendData(USART2, TxBuffer[i++]);
if ((j<TxBufferSize) && (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET))
RxBuffer[j++] = USART_ReceiveData(USART2);
}

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

still not working ! 

and  it's almost the same thing

Posted on April 13, 2013 at 02:41

Then consider if you're receiving any data, and if so what. If your not receiving data consider why? Is it transmitting anything? Do you need to remap USART2 to use PD5/PD6?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 13, 2013 at 02:49

void USART_Configuration(void) // sourcer32@gmail.com
{
/* USART2 Initialization */
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_9b; // 8+1 For Parity 
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_Even;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
/* Configure USART2 */
USART_Init( USART2, &USART_InitStructure);
/* Enable USART2 */
USART_Cmd(USART2, ENABLE);
}
void RCC_Configuration(void)
{ 
/* Enable GPIO and AFIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
/* Enable USART2 Clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable the USART2 Pins Software Remapping */
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); 
/* Configure USART2 Tx PD5 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Configure USART2 Rx PD6 as input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
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..
Posted on April 13, 2013 at 15:38

/* Configure USART2 Rx PD6 as input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

 Why?

And who is sourcer32? JW
Posted on April 13, 2013 at 18:47

Because the code looks to have been lifted from a VLDiscovery board, and there are more references to that than an STM32F4 Discovery. The initial question relates to why stuff needs to block.

For a USART2 PD5/PD6 example on the STM32F4-Discovery

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/DispForm.aspx?ID=3697&RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/USART%20Problems&Source=https%3a//my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder%3D%252Fpublic%252FSTe2ecommunities%2...

.

Who indeed, when you figure it out left me know.

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

i fixed the problem 

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

  

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  

  GPIO_InitStructure.GPIO_Pin = 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_Init(GPIOA, &GPIO_InitStructure);

  

  USART_InitStructure.USART_BaudRate = 4800; 

  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;

  

  /* Configure USARTy */

  USART_Init(USART2, &USART_InitStructure);

  

  /* Enable the USARTy */

  USART_Cmd(USART2,ENABLE);

  GPIO_PinAFConfig(GPIOA,GPIO_PinSource2, GPIO_AF_USART2);

  GPIO_PinAFConfig(GPIOA,GPIO_PinSource3, GPIO_AF_USART2);

Thanks .