cancel
Showing results for 
Search instead for 
Did you mean: 

Tx-Rx problem with USART in STM32L- Discovery !

bipinkumar117
Associate
Posted on April 25, 2012 at 17:11

This is my code for Transmission :

void USART3_tx(void)

{

USART_InitTypeDef USART_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

 

  /* Enable GPIOB and USART3 clock */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

 

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);

 

  /* Configure USART Tx and Rx as alternate function push-pull */

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

 

  /* USARTx configured as follow:

  - BaudRate = 9600 baud  

  - Word Length = 8 Bits

  - One Stop Bit

  - No parity

  - Hardware flow control not enabled (RTS and CTS signals)

  - Receive and transmit enabled

  */

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_Tx | USART_Mode_Rx;

USART_Init(USART3, &USART_InitStructure);

 

USART_Cmd(USART3, ENABLE);

 }

int main(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;  

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  

  GPIO_Init(GPIOB, &GPIO_InitStructure);

USART3_tx();

USART_SendData(USART3, 0xFF);

while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);

GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET);

}

This is the code for Reception :

void USART3_rx(void)

{

USART_InitTypeDef USART_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

 

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

  

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);

 

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;

  GPIO_Init(GPIOB, &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(USART3, &USART_InitStructure);

 

USART_Cmd(USART3, ENABLE);

}

int main(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;  

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  

  GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_SET);

USART3_rx();

while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != RESET);

while(USART_ReceiveData(USART3)!= 0xFF);

GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET);

GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_RESET);

}

I am using two Xbee modules with two STM32L-Discovery boards to realize this Tx - Rx.

Kindly Help!

#stm32l---discovery #usart #uart
5 REPLIES 5
Posted on April 25, 2012 at 20:22

The RX code looks busted.

main()
{
// ... Init code
GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_SET);
USART3_rx();
do
{
while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);
}
while(USART_ReceiveData(USART3) != 0xFF);
GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET);
GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_RESET);
while(1); // do not exit
}

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

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6mz&d=%2Fa%2F0X0000000bvU%2FRAFMXzyckiHoC1zI8D1N19TOzFGd9exSUxw1uS5gtL8&asPdf=false
Posted on April 30, 2013 at 13:39

Mixing APB1/APB2 references.

Failing to wait on USART TXE before USART_SendData()
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
becker007
Associate
Posted on May 08, 2013 at 12:51

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6n4&d=%2Fa%2F0X0000000bvV%2FQ0c3jbOupoJOyRr1uZ1CsIj5nqKdSNLy7FCp5_NxSVI&asPdf=false
Posted on May 08, 2013 at 13:29

I would imagine generic C code for the protocol exists.

Couple of recommendations for code, use bytes for the data array, and front test TXE rather than back test TC.

void SendArray(uint8_t *zeichen)
{
for(i=0;i<5;i++)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, zeichen[i]);
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..