/**
 * \file CHKBO16X.C
 * 
 * \author Axel Wolf, SCI Cupertino
 * \author Dr. Jens Barrenscheen, HL MC PD, Munich
 * \author Hubert Piontek, University of Ulm
 *
 * Routine for checking for busoff situations on the C166 CAN module.
 *
 * Most of this is based von the Infineon/Siemens Application Note AP2922.
 * Some additions made at University of Ulm, February 2003.
 *
 * This CAN driver is mainly meant to be used by the publisher/subscriber
 * library developed at the University of Ulm, 1999-2003.
 */

/* ----------------------------------------- header: ----------------*/
/*********************************************************************
 * Program name:	"CHKBO16X.C"                                     *
 * Compiler used:	BSO/Tasking C166                                 *
 * Task:		Source File for function check_busoff_16x            *
 *			belonging to Siemens ApNote AP2922                       *
 *			"'C' CAN Driver Routines for the C166 family"            *
 *                                                                   * 
 * Last modifications:	April 28nd 1997                              *
 * Version:		1.0                                                  *
 * Authors:		Axel Wolf,  SCI Cupertino                            *
 *			Dr. Jens Barrenscheen, HL MC PD, Munich                  *
 *********************************************************************/
/* ----------------------------------- include files: -------------- */
#include <CANR_16X.H>       /* CAN control register definitions      */

/**
 * Checks the status register of the CAN controller to see, if it has
 * gone busoff, i.e. doesn't work anymore. Tries to recover from busoff
 * by reinitializing the CAN controller.
 * \return busoff state
 * \retval 0 Controller is working and online.
 * \retval 1 Controller is busoff. Recovery started.
 */
unsigned char check_busoff_16x(void)
{
	unsigned char busoff_var=0;

	if (SR & 0x80) {	/* if BOFF = 1 */
	   busoff_var=1;
	   CR=CR & 0xfe;	/* recover from Bus Off (clear INIT) */
	}
	return busoff_var;
}