printf_fifo.c 4.57 KB
#include "os_config.h"

#include "c_def.h"
#include "debug.h"
//#include "oem.h"


//#include "prj.h"
#include "printf_fifo.h"


void printf_fifo_init( PRINTF_FIFO *fifo, U8 *buf, U32 bufsize,
		FIFO_CALLBACK pput, FIFO_CALLBACK pget, FIFO_CALLBACK pempty, FIFO_CALLBACK pfull,
		FIFO_CALLBACK phalfEmpty, FIFO_CALLBACK phalfFull )
{
	fifo->buf		=	buf;
	fifo->maxsize	=	bufsize;
	fifo->head		=	fifo->tail		=	fifo->buf;
	fifo->size		=	0;

	fifo->putCallback	=	pput;
	fifo->getCallback	=	pget;
	fifo->emptyCallback	=	pempty;
	fifo->fullCallback	=	pfull;

	fifo->halfEmptyCallbk	=	phalfEmpty;
	fifo->halfFullCallbk	=	phalfFull;
}


U32 fifo_getDataSize( PRINTF_FIFO *fifo )
{
	return fifo->size;
}

U32 fifo_getEmptySpace( PRINTF_FIFO *fifo )
{
	return	(fifo->maxsize	-	fifo->size );
}

//static U32 fifoPut( PRINTF_FIFO *fifo, U8 *src, U32 size, BOOL isr )
U32 fifoPut( PRINTF_FIFO *fifo, U8 *src, U32 size, BOOL isr )
{
	TX_INTERRUPT_SAVE_AREA;

/*
	U32	sz	=	size;
	U8	*tail		=	fifo->tail;
	U8	*fifoend	=	 fifo->buf + fifo->maxsize;

	DBG_Assert( size > 0 );
	DBG_Assert( printf_fifo_getEmptySpace( fifo ) >= size );
*/
	U32	sz;
	U8	*tail		=	fifo->tail;
	U8	*fifoend	=	 fifo->buf + fifo->maxsize;

	DBG_Assert( size > 0 );
	if( size == 0 )	return 0;

	sz = fifo->maxsize	- fifo->size;
	if( size >= sz )	size = sz;
	else sz = size;

	if( size == 0 )	return 0;		// no space


	while ( size-- > 0 ) {
		
		*tail ++	=	*src ++;
		if( tail >= fifoend ) {
			tail		=	fifo->buf;
		}
	}

	if( isr ) {
		fifo->tail	=	tail;
		fifo->size	+=	sz;
	}
	else {
		TX_DISABLE;

		fifo->tail	=	tail;
		fifo->size	+=	sz;

		TX_RESTORE;
	}


#if 0//fifo->halfFullCallbk is NULL
	if( fifo->size >= fifo->maxsize/2 )
	{
		if( fifo->halfFullCallbk != NULL )
		{
			fifo->halfFullCallbk( fifo );
		}
	}
#endif

	if( fifo->putCallback != NULL )
	{
		fifo->putCallback( fifo );
	}

	return sz;
}

U32 fifo_put( PRINTF_FIFO *fifo, U8 *src, U32 size )
{
	return fifoPut( fifo, src, size, FALSE );
}

U32 fifo_iput( PRINTF_FIFO *fifo, U8 *src, U32 size )
{
	//scanf_rxPutCharCallback
	return fifoPut( fifo, src, size, TRUE );
}


//static U32 fifoGet( PRINTF_FIFO *fifo, U8 *dst, U32 size, BOOL isr )
U32 fifoGet( PRINTF_FIFO *fifo, U8 *dst, U32 size, BOOL isr )
{
	TX_INTERRUPT_SAVE_AREA

/*	
	U32	sz	=	size;
	U8	*head		=	fifo->head;
	U8	*fifoend	=	 fifo->buf + fifo->maxsize;

	DBG_Assert( size > 0 );
	DBG_Assert( fifo_getDataSize( fifo ) >= size );
*/
	U32	sz;
	U8	*head		=	fifo->head;
	U8	*fifoend	=	 fifo->buf + fifo->maxsize;

	DBG_Assert( size > 0 );
	if( size == 0 )	return 0;

	sz = fifo->size;
	if( size >= sz )	size = sz;
	else sz = size;

	if( size == 0 )	return 0;		// empty

	while ( size-- > 0 ) {
		
		*dst ++	=	*head ++;
		if( head >= fifoend ) {
			head	=	fifo->buf;
		}		
	}

	if( isr )
	{
		//asm("break 1,1");
		fifo->head	=	head;
		fifo->size	-=	sz;
	}
	else
	{
		TX_DISABLE;

		fifo->head	=	head;
		fifo->size	-=	sz;

		TX_RESTORE;
	}

#if 0
	if( fifo->size <= fifo->maxsize/2 )
	{
#if 0
		if( fifo->halfEmptyCallbk != NULL )
		{
			//set the "UART_TX_EVENT_FIFO_EMPTY" event.
			//fifoHalfEmptyCallback
			fifo->halfEmptyCallbk( fifo );
		}
#else

#ifdef UART_TX_EVENT_FIFO_EMPTY_ENABLE
		//fifoHalfEmptyCallback
		DBG_Assert(FALSE);
		UART_TX_EVENT_FIFO_EMPTY_SET;
#endif

#endif
	}
#endif

	if( fifo->getCallback!= NULL )
	{
		fifo->getCallback( fifo );
	}

	return sz;
}

U32 fifo_get( PRINTF_FIFO *fifo, U8 *dst, U32 size )
{
	return fifoGet( fifo, dst, size, FALSE );
}
U32 fifo_iget( PRINTF_FIFO *fifo, U8 *dst, U32 size )
{
	return fifoGet( fifo, dst, size, TRUE );
}


void fifo_clear( PRINTF_FIFO *fifo )
{
	TX_INTERRUPT_SAVE_AREA;

	TX_DISABLE;

	fifo->head	=	fifo->tail	=	fifo->buf;
	fifo->size	=	0;

	TX_RESTORE;
}


U32	fifo_peekChar( PRINTF_FIFO *fifo, U8 cha )
{
	U8	*head		=	fifo->head;
	int	size		=	fifo->size;
	U8	*fifoend	=	 fifo->buf + fifo->maxsize;

	U32	index	=	0;
	
	while( size -- ) {
		if( cha == *head ) {
			return ++index;
		}
		
		++index;
		if( ++head >= fifoend )	head	=	fifo->buf;
	}
	
	return 0;
}

/*
U32	printf_fifo_idiscardData( PRINTF_FIFO *fifo, U32 size )
{
	U8	*head		=	fifo->head;
	U8	*fifoend	=	 fifo->buf + fifo->maxsize;
	
	DBG_Assert( size <= fifo->size );
	
	fifo->head +=	size;
	if( fifo->head >= fifoend ) {
		fifo->head	-=	fifo->maxsize;
	}
	
	fifo->size -=	size;
	
	return size;
}

U32	printf_fifo_discardData( PRINTF_FIFO *fifo, U32 size )
{
	TX_INTERRUPT_SAVE_AREA

	U8	*head		=	fifo->head;
	U8	*fifoend	=	 fifo->buf + fifo->maxsize;

	
	DBG_Assert( size <= fifo->size );
		
	TX_DISABLE;
	
	fifo->head +=	size;
	if( fifo->head >= fifoend ) {
		fifo->head	-=	fifo->maxsize;
	}
	
	fifo->size -=	size;

	TX_RESTORE;
	
	return size;
}
*/