2015-06-17 10:32 PM
I am using STM32F101 microcontroller.
Is it necessary to disable the receive interrupt in order to transmit something through UART? My reception priority is high, so is it okay to keep the receive interrupt enabled while transmitting? #stm32 #uart #interrupt2015-06-18 05:28 AM
Assuming USART is set for full duplex the receive is independent from the transmit. You do not have to disable the receiver.
If USART is set for half duplex (for example RS-485 or infrared) and the transceiver echoes what you send then yes, you would have to disable the USART receiver. Jack Peacock2015-06-18 06:09 AM
You can always leave the RXNE interrupt enabled, it will only fire when there is data for you to collect.
You should disable the TXE interrupt if you have no data to send, as otherwise you can't clear the interrupt state.2015-06-18 10:09 PM
void
USART_SendString(USART_TypeDef* USARTx,
CUInt16 u16len
){
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE );
//Disable receive interrupt
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
//Clear pending interrupt bit
while
(cTlen);
//if next buffer comes wait till previous buffer is completely transmitted
//udelay(500);
cTlen = u16len;
memset
(cTX_Buff, 0x00,
sizeof
(cTX_Buff));
//Clear transmit buffer
memcpy
((
char
*)&cTX_Buff, (
char
*)&cSend_Buff, u16len);
//Copy send buff to tx buff
cTptr = cComm_Len = 0;
USARTx->DR = (cTX_Buff[cTptr++] & (uint16_t)0x01FF);
//Stm32 standard lib
USART_ITConfig(USART1, USART_IT_TXE, ENABLE );
//Enable transmit interrupt
}
Here below is the ISR for the transmission.. Once transmission is done we are re-enabling the receive interrupt and disabling the transmit interrupt
if
(USART_GetITStatus( USART1 , USART_IT_TXE ) != RESET )
{
//Transmit Data to UART till cTptr is less than data length and less than TX_Buff size
if
((cTptr < cTlen) && (cTptr <= TX_BUFF_SIZE))
{
//Send Data on UART
USART1->DR = (cTX_Buff[cTptr++] & (uint16_t)0x01FF);
//Stm32 standard peripheral library code
}
else
{
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE );
USART_ClearITPendingBit(USART1, USART_IT_TC);
cTlen = 0;
}
}
}
Is the approach used correct or is there is any flaw
2015-06-19 12:13 PM
The USART can send a receive at the same time, I'd leave the RXNE interrupt on all the time so as to empty the buffer as soon as possible preventing potential overflow conditions.
Consider also error states reported by the USART, these need to be identified and cleared.Be sure to use volatile variables when signalling in/too interrupts.