Skip to main content
pranathi1091
Associate III
December 10, 2012
Question

usart problem

  • December 10, 2012
  • 2 replies
  • 741 views
Posted on December 10, 2012 at 11:23

Hello All,

I am using STm32f2.

I am using USART1 , i wnat to read and write some data to the device connected to USART1

I am setting 9600 as baud rate, i could see RXNIE RE,TE ,SBK,WAKE,PS,PEIE,IDLEIE,TCIE bits set in CR1 register and TXe and TC bits set in SR register.

But my baud rate reister is always empty.

I am unable to write or read from the device

Please let me know what could be the problem

Here is my code

Usartx is USART1

PA9->USART1TX

Pa10->USART1RX

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

void Uartinit(void)

{

USART_InitTypeDef *USART_InitStruct;

//USART1 Configuration

//GPS Baud rate 9600

// NO HardwareFlowControl

//No parity

// USART_DeInit(USART1);

UsartLowlevelinit();

USART_InitStruct->USART_BaudRate=9600;

USART_InitStruct->USART_HardwareFlowControl=USART_HardwareFlowControl_None;

USART_InitStruct->USART_Mode=USART_Mode_Rx | USART_Mode_Tx;

USART_InitStruct->USART_Parity=USART_Parity_No;

USART_InitStruct->USART_StopBits=USART_StopBits_1_5;

USART_InitStruct->USART_WordLength=USART_WordLength_9b;

USART_Init(USART1, USART_InitStruct);

USART_ITConfig(USART1 , USART_IT_RXNE, ENABLE);

USART_ITConfig(USART1 , USART_IT_TXE, ENABLE);

USART_Cmd(USART1, ENABLE);

}

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

void NVIC_Configuration(void)

{

NVIC_InitTypeDef NVIC_InitStruct;

/* Configure the NVIC Preemption Priority Bits */

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

/* Enable the USART1 Interrupt */

NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;

NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 4;

NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStruct);

}

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

void UsartLowlevelinit (void)

{

GPIO_InitTypeDef GPIO_InitStructure;

//Peripheral Clock Enable

//Enable GPIO clock

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);

USARTx_CLK_INIT(USARTx_CLK, ENABLE);

//USART1 GPIO configuration

GPIO_PinAFConfig(USARTx_TX_GPIO_PORT, USARTx_TX_SOURCE, USARTx_TX_AF);

GPIO_PinAFConfig(USARTx_RX_GPIO_PORT, USARTx_RX_SOURCE, USARTx_RX_AF);

//Configure USART TX pin

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_InitStructure.GPIO_Pin = USARTx_TX_PIN;

GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = USARTx_RX_PIN;

GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStructure);

}

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

uint8_t ReceiveData(uint8_t *Data,uint32_t TimeOut)

{

uint8_t Counter=0;

while((USART_GetITStatus(USARTx, USART_IT_RXNE) != RESET) && (Counter != TimeOut))

{

Counter++;

}

if(Counter != TimeOut)

{

*Data = (uint8_t)USART_ReceiveData(USARTx);

return SUCCESS;

}

else

{

return ERROR;

}

} /**************************************************************************************/

void USART1_IRQHandler(void)

{

static uint8_t TxIndex = 0;

static uint8_t RxIndex = 0;

if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // Transmit the string in a loop

{

USART_SendData(USART1, g_ucGpsCommand[TxIndex++]);

if (TxIndex>=(sizeof(EVENT_BUFF_SIZE - 1)))

TxIndex = 0;

}

Thanks

#stm32-clock
    This topic has been closed for replies.

    2 replies

    Tesla DeLorean
    Guru
    December 10, 2012
    Posted on December 10, 2012 at 15:46

    Please let me know what could be the problem

    Why 9-bit with 1.5 stop bits?

    Why enable Interrupts for RX when you don't service it in the interrupt routine.

    Presumably you have a continuous stream of inbound data, and only periodic command sending.

    Why does the content of BRR matter? Should you be able to read it?

    If you have the wrong baud rate (use a scope), it is most likely because there is a discrepancy between HSE crystal value, and the software HSE_VALUE define.

    You don't provide the defines, make sure you are enabling the right clocks and pins.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    pranathi1091
    Associate III
    December 11, 2012
    Posted on December 11, 2012 at 04:46

    Thanks

    My baud rate issue is solved.I m clear with the clock I am using now

     

    Why enable Interrupts for RX when you don't service it in the interrupt routine.

     

    I did not get what u meant

    I have changed my word length to 8 bits with 1 stop bit which actually device requires.

    Presumably you have a continuous stream of inbound data, and only periodic command sending.

     

    Yes i have a stream of data coming in.But m not getting where the commands in my code are periodical

    Could u pls let me know.