Hi Primer2 community,
We are currently developing a project on Primer2 which is using the USART port (USART2 to be more specific) to receive data from an Xbee module... We implemented a circular buffer to stock temporarily our received packet without losing any data.
We are using the interrupt feature of the USART2 by setting the NVIC.
void NVIC_config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
// Init NVIC
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// Systick priority (not sure if we need that)
NVIC_SystemHandlerPriorityConfig( SystemHandler_SysTick, 3, 3 ); //6,6
}
And then we call the handler for this type of interruption...
USART2_IRQHandler = 0xD8
// Install new IRQ handler
OldHandler = (tHandler)UTIL_GetIrqHandler( USART2_IRQHandler );
UTIL_SetIrqHandler(USART2_IRQHandler, (tHandler)USART2_Handler); //pointing out the handler
So, the handler/interrupt function is...
void USART2_Handler(void)
{
FlagStatus rx_status = USART_GetITStatus(USART2, USART_IT_RXNE);
FlagStatus tx_status = USART_GetITStatus(USART2, USART_IT_TXE);
// interrupt was for receive (RXNE)
if(rx_status != RESET)
{
unsigned char RxData;
u8 tmphead;
/* Read one byte from the receive data register */
RxData = USART_ReceiveData(USART2); //Read the received data
tmphead = (USART_RxHead + 1) & USART_RX_BUFFER_MASK; //Calculate buffer index
if (tmphead == USART_RxTail)
{
/* ERROR! Receive buffer overflow */
/* data will get lost !!! */
USART_RxOverflow = TRUE;
} else {
USART_RxBuf[USART_RxHead] = RxData; // Store received data in buffer
USART_RxOverflow = FALSE;
}
USART_RxHead = tmphead; // Store new index
}
Now, our problem is that when we execute the program in step by step debug mode everything seems to work correctly, but when we run the entire application continuously, it never enters in our if condition:
if(rx_status != RESET)
We absolutely have no idea why?
And we have a function that returns the newest character received each time an interrupt occurs... But it seems that we are stuck in our while loop, because we never enter our if condition of our interrupt handler, which is kind of weird....
unsigned char USART_ReceiveChar( void )
{
u8 tmptail;
u8 buffer;
//see if we have any data
while ( USART_RxHead == USART_RxTail );
tmptail = (USART_RxTail + 1) & USART_RX_BUFFER_MASK;
buffer = USART_RxBuf[USART_RxTail];
USART_RxTail = tmptail;
return buffer;
}
Thank you!