I know that an interrupt should be as short as possible, however, what is short?
My routine is quite long, but has quite some if-statements, so the execution time is short. But maybe it still is considered (way too) long.
Will my code cause problems (in the future)? Currently I have only one interrupt, but I want to add later more (for DMX control for example). This one is to handle MIDI input.
It is meant for an STM32F103C8T6 (72 MHz) and later STM32F446ZET6 (168 MHz). The debug statement are temporarily.
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == huart1.Instance)
{
// Don't overwrite existing data.
if (((_RxHead + 1) % BUFFER_SIZE) != _RxTail)
{
// Check for sysex.
if (_SysexReceived == 0)
{
if (_RxByte == 0xF0)
{
_RxBuffer[_RxHead] = _RxByte;
_RxHead = (_RxHead + 1) % BUFFER_SIZE;
_SysexReceived = 1;
}
// No sysex, skip active sensing/timing.
else if ((_RxByte != 0xF8) && (_RxByte != 0xFE))
{
// Add byte.
_RxBuffer[_RxHead] = _RxByte;
_RxHead = (_RxHead + 1) % BUFFER_SIZE;
}
}
else if (_SysexReceived < MAX_SYSEX_LENGTH)
{
// Sysex: add byte.
_RxBuffer[_RxHead] = _RxByte;
_RxHead = (_RxHead + 1) % BUFFER_SIZE;
_SysexReceived = (_RxByte == 0xF7) ? 0 : _SysexReceived + 1;
}
else // SysexReceived >= (or ==) MAX_SYSEX_LENGTH
{
// Too long message, only write last 0x7F.
if (_RxByte == 0xF7)
{
_RxBuffer[_RxHead] = _RxByte;
_RxHead = (_RxHead + 1) % BUFFER_SIZE;
_SysexReceived = 0;
}
}
#ifdef __BUFFER_DEBUG
__ReceivedBufferData[__ReceivedIndex] = _RxByte;
__ReceivedBufferHead[__ReceivedIndex] = _RxHead;
__ReceivedBufferTail[__ReceivedIndex] = _RxTail;
__ReceivedBufferSysex[__ReceivedIndex] = _SysexReceived;
__ReceivedIndex++;
if (__ReceivedIndex == __RECEIVED_BUFFER_SIZE)
{
__ReceivedIndex = 0;
__ReceivedRounds++;
}
#endif
}
else
{
_Error_Handler(__FILE__, __LINE__);
}
}
}
Update:
After fixing some problems somewhere else in the code and slightly different ranges in some if statements, it works quite well. Only many and big sysex-messages makes it a bit sluggish but that's not related to the ISR.