xQueuePeek() inside an ISR

paulgcoleman wrote on Wednesday, December 19, 2012:

If I use a block time of zero is it safe to use xQueuePeek() inside an ISR?

Thanks, Paul.

rtel wrote on Wednesday, December 19, 2012:

You can’t use the existing peek function from an ISR, but could add something like the following (not actually tested!)

signed portBASE_TYPE xQueuePeekFromISR( xQueueHandle pxQueue, void * const pvBuffer )
{
signed portBASE_TYPE xReturn;
unsigned portBASE_TYPE uxSavedInterruptStatus;
signed char *pcOriginalReadPosition;
	uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
	{
		/* We cannot block from an ISR, so check there is data available. */
		if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
		{
			/* Remember the read position in case we are just peeking. */
			pcOriginalReadPosition = pxQueue->pcReadFrom;
			prvCopyDataFromQueue( pxQueue, pvBuffer );
			
			/* The data is not being removed, so reset the read pointer to its
			original position. */
			pxQueue->pcReadFrom = pcOriginalReadPosition;
			xReturn = pdPASS;
		}
		else
		{
			xReturn = pdFAIL;
		}
	}
	portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
	return xReturn;
}

Regards.

paulgcoleman wrote on Wednesday, December 19, 2012:

Okay, thanks.

Paul.