Description

isEnabled() – checks if the CAN bus is in operation

Syntax

if( MCAN0.isEnabled() )

Parameters

none

Returns

bool
– true: CAN bus is in operation
– false: CAN bus is not in operation (e.g. because of a bus failure BUS-OFF or it has not yet been begin())

Example Code

The following example switches the b_autorecover function of, send msg., checks and re-enables the bus in case of a bus failure.

void setup()
{
	while(!Serial);
	Serial.println("Check the bus state of CAN0");
	
	// disable the autorecover feature
	MCAN0.b_autorecover = false;
	Serial.println("Now cause some bus failure (e.g. CANH/L short circuit)");
}
void loop()
{
	// Send CAN messages
	CANMessage myMsg;
	myMsg.id = 0x12345;
	myMsg.ext = true;
	myMsg.len = 1;
	myMsg.data[0] = 0xA5;
	MCAN0.sendMsgBuf(myMsg); 
	delay(100);
	// Check the Bus state
	if(!MCAN0.isEnabled())
	{
		//Bus failure! restarting the bus
		digitalWrite(LED_BUILTIN, HIGH); 
		MCAN0.reenable();
	}
	else
		digitalWrite(LED_BUILTIN, LOW);
}

Notes and Warnings

Be aware of the b_autorecover setting. It might be handy to have it enabled for cases of temporary failures on the can bus. The device would automatically get back into operation on the CAN bus afterwards. Still, in other cases e.g. if the device operates at a different baud rate than the remaining nodes, this would rather lead to a persistent blocking of the bus traffic. Depending on your application it might be better to switch the b_autorecover feature off, check manually and use reenable() or setMode() re-establish proper bus operation again.
b_autorecover is true by default.

Examples can be found in the Arduino IDE menu:
File -> Examples -> DICE -> CAN Bus ->

See also

Find more details about the CAN buses of dice devices
defines
setMode()
reenable()
SAMC21_CAN – Library
CAN operation background
users manual
BOOTLOADER SETTINGS – Library
samc21 datasheet