AnsweredAssumed Answered

usart problem

Question asked by sd.pranathi on Dec 10, 2012
Latest reply on Dec 11, 2012 by sd.pranathi
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

Outcomes