// ----------------------------------------------------------------------------
void sja1000_set_mode(can_mode_t mode)
{
	uint8_t reg = 0;
	
	// enter reset mode
	sja1000_write(MOD, (1<<AFM) | (1<<RM));
	
	if (mode == LISTEN_ONLY_MODE) {
		reg = (1<<LOM);
	}
	else if (mode == LOOPBACK_MODE) {
		reg = (1<<STM);
	}
	
	// set new mode
	sja1000_write(MOD, (1<<AFM) | (1<<RM) | reg);
	
	// leave reset mode
	sja1000_write(MOD, (1<<AFM) | reg);
}
uint8_t sja1000_send_message(const can_t *msg)
{
	uint8_t frame_info;
	uint8_t address;
	
	if (!sja1000_check_free_buffer() || (msg->length > 8))
		return FALSE;
	
	frame_info = msg->length | ((msg->flags.rtr) ? (1<<RTR) : 0);
	
	if (msg->flags.extended)
	{
		// write frame info
		sja1000_write(TX_INFO, frame_info | (1<<FF));
		
		// write extended identifier
		sja1000_write(20, msg->id << 3);
		sja1000_write(19, msg->id >> 5);
		sja1000_write(18, msg->id >> 13);
		sja1000_write(17, msg->id >> 21);
		
		address = 21;
	}
Exemplo n.º 3
0
bool sja1000_init(uint8_t bitrate)
{
	if (bitrate >= 8)
		return false;

	#if !SJA1000_MEMORY_MAPPED
		SET(SJA1000_WR);
		SET(SJA1000_RD);
		RESET(SJA1000_ALE);
		RESET(SJA1000_CS);

		SET_OUTPUT(SJA1000_WR);
		SET_OUTPUT(SJA1000_RD);
		SET_OUTPUT(SJA1000_ALE);

		SET_OUTPUT(SJA1000_CS);
		DDR(SJA1000_DATA) = 0xff;
	#endif

	// enter reset mode
	sja1000_write(MOD, (1<<RM)|(1<<AFM));

	// choose PeliCAN-Mode
	sja1000_write(CDR, (1<<CANMODE) | SJA1000_CLOCK_REGISTER);

	// select the bitrate configuration
	sja1000_write(BTR0, pgm_read_byte(&_sja1000_cnf[bitrate][0]));
	sja1000_write(BTR1, pgm_read_byte(&_sja1000_cnf[bitrate][1]));

	// filter are not practical useable, so we disable them
	sja1000_write(AMR0, 0xff);
	sja1000_write(AMR1, 0xff);
	sja1000_write(AMR2, 0xff);
	sja1000_write(AMR3, 0xff);

	// set output driver configuration
	sja1000_write(OCR, (1<<OCTP0)|(1<<OCTN0)|(1<<OCMODE1));

	// enable receive interrupt
	sja1000_write(IER, (1<<RIE));

	// leave reset-mode
	sja1000_write(MOD, (1<<AFM));

	return true;
}
Exemplo n.º 4
0
// ----------------------------------------------------------------------------
void sja1000_reset_bus_off(void)
{
    uint8_t status = sja1000_read(SR) & (1<<BS);
    sja1000_write(SR, status & ~(1<<BS));
}