2015-12-30 03:18 AM
I'm using a stm32f10x micorcontroller (steval-mki121v1 evaluation board) and i want to enable an interrupt event when a data is receive through the USART.
The problem is that the interrupt never occurs after the data is send.here the code:void USARTInit(void){ USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef nvicStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); /* Configure USART Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART Rx as input floating */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &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_Tx | USART_Mode_Rx; USART_Init(USART2, &USART_InitStructure); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //USART_ClearITPendingBit(USART2,USART_IT_RXNE); need this? //NVIC_EnableIRQ(USART2_IRQn); need this? nvicStructure.NVIC_IRQChannel = USART2_IRQn; nvicStructure.NVIC_IRQChannelPreemptionPriority = 0; nvicStructure.NVIC_IRQChannelSubPriority = 0; nvicStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvicStructure); USART_Cmd(USART2,ENABLE);}void USART2_IRQHandler() // never go here{ if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { char data = 0; data = USART_ReceiveData(USART2); USART_ClearITPendingBit(USART2,USART_IT_RXNE); }}Edit: the trasmission working good instead. it is just not receiving data.2015-12-30 06:01 AM
Does it actually receive anything? ie no reception, no interrupt
Does a simple polling/echo back loop work? ie wait on RXNE, get char, wait on TXE, send back Is this in a .CPP file?2015-12-30 08:49 AM
problem solved. the two xbee module i'm using for communication were not configured correctly.
another question: when i send a string like, for example, ''Hello123'', i receive only the first letter (''H''). why? do i need a loop into the Handler function?2015-12-30 09:06 AM
It's rather hard to see what you are doing.
When sending a string, you need to wait for TXE before sending each byte, the USART doesn't have any buffering beyond the single holding register. The interrupt routine would be dealing with one byte at a time, you shouldn't dwell there. Instead buffer the data, and serve a byte at a time at the TXE interrupt, when you are out of data to send turn off the TXE interrupt. A polled method to output a string would look like thisvoid OutString(char *s)
{
while(*s)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, *s++); // Send Char
}
}