cancel
Showing results for 
Search instead for 
Did you mean: 

UART Interrupt

meet
Associate
Posted on June 18, 2015 at 07:32

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 #interrupt
4 REPLIES 4
jpeacock
Associate II
Posted on June 18, 2015 at 14:28

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 Peacock

Posted on June 18, 2015 at 15:09

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.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
meet
Associate
Posted on June 19, 2015 at 07:09 Hi i would like you guys to look into my code so that i get a better understanding Actually i have a master slave communication between two controllers ( STM32 is the slave, it receives packets of about 255 bytes from the other controller and transmits only ACKs of around 10 bytes ) This is the UART Sendstring code which enables the transmit interrupt to send a packet of 10 bytes. We are disabling the receive interrupt while transmitting is it needed ?

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
Posted on June 19, 2015 at 21:13

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..