/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss of reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#include "kernel.h"

/****************************************************************************/
/*					Public API functions									*/
/****************************************************************************/

/** @addtogroup bx_time */
/** @{ */

/**
*	Reloads a timer.
*
*	@param	timer			Pointer to the timer to be reloaded
*	@param	value			The time value to load into the timer. If this
*							value is 0, then the period of the timer will
*							be used. If the period is als0 0, then the timer
*							will not be touched and 0 is returned.
*	@retval	>0				The timer was successfully reloaded
*	@retval	0				The timer was not running or should have been
*							reloaded with 0.
*	@retval	BX_ERR_OBJECT	The timer parameter was not a valid timer object
*	@retval	BX_ERR_INACTIVE	The timer was not running
*/

int		BX_TimerReload( BX_TIME *timer, unsigned value )
{
int		cpsr;

	if ( timer->type != BX_TYPE_TIME ) return( BX_ERR_OBJECT );

	cpsr = _bx_IrqDis();

	if ( timer->stat == TIMER_CANCEL || timer->stat == TIMER_UNUSED ) {

		// The timer is not in use

		_bx_IrqRes( cpsr );
		return( BX_ERR_INACTIVE );
	}
	else {

		if ( ! value ) value = timer->peri;

		if ( value ) {

			timer->stat = TIMER_RELOAD;
			timer->load = value;
			_bx_QueueEvent( cpsr, _bx_TimerProcess, timer, (void *) 1 );
			return( 1 );
		}
		else {

			_bx_IrqRes( cpsr );
			return( 0 );
		}
	}
}

/** @} */

