Skip to content

Instantly share code, notes, and snippets.

@nonninz
Created December 28, 2012 15:18
Show Gist options
  • Save nonninz/4398781 to your computer and use it in GitHub Desktop.
Save nonninz/4398781 to your computer and use it in GitHub Desktop.
// -------------------------------------------------------------------------
// Schreibt/liest ein Byte ueber den Hardware SPI Bus
uint8_t spi_putc( uint8_t data )
{
// put byte in send-buffer
SPDR = reverse_byte(data); // Reverse byte flips the bits in the byte to simulate a MSB SPI_mode
// wait until byte was send
while( !( SPSR & (1<<SPIF) ) )
;
/*return SPDR;*/
return reverse_byte(SPDR);
}
// -------------------------------------------------------------------------
void mcp2515_write_register( uint8_t adress, uint8_t data )
{
RESET(MCP2515_CS);
spi_putc(SPI_WRITE);
spi_putc(adress);
spi_putc(data);
SET(MCP2515_CS);
}
// -------------------------------------------------------------------------
uint8_t mcp2515_read_register(uint8_t adress)
{
uint8_t data;
RESET(MCP2515_CS);
spi_putc(SPI_READ);
spi_putc(adress);
data = spi_putc(0xff);
SET(MCP2515_CS);
return data;
}
// -------------------------------------------------------------------------
void mcp2515_bit_modify(uint8_t adress, uint8_t mask, uint8_t data)
{
RESET(MCP2515_CS);
spi_putc(SPI_BIT_MODIFY);
spi_putc(adress);
spi_putc(mask);
spi_putc(data);
SET(MCP2515_CS);
}
// ----------------------------------------------------------------------------
uint8_t mcp2515_read_status(uint8_t type)
{
uint8_t data;
RESET(MCP2515_CS);
spi_putc(type);
data = spi_putc(0xff);
SET(MCP2515_CS);
return data;
}
// -------------------------------------------------------------------------
uint8_t mcp2515_init(uint8_t speed)
{
SET(MCP2515_CS); // Digital pin 10
SET_OUTPUT(MCP2515_CS); // Digital pin 10
/*RESET(P_SCK);*/
/*RESET(P_MOSI);*/
/*RESET(P_MISO);*/
/*SET_OUTPUT(P_SCK);*/
/*SET_OUTPUT(P_MOSI);*/
/*SET_INPUT(P_MISO);*/
SET_INPUT(MCP2515_INT); // Digital pin 2
SET(MCP2515_INT); // Digital pin 2
// active SPI master interface
/*SPCR = (1<<SPE)|(1<<MSTR) | (0<<SPR1)|(1<<SPR0);*/
/*SPSR = 0;*/
// reset MCP2515 by software reset.
// After this he is in configuration mode.
RESET(MCP2515_CS);
spi_putc(SPI_RESET);
SET(MCP2515_CS);
// wait a little bit until the MCP2515 has restarted
_delay_us(10);
// load CNF1..3 Register
RESET(MCP2515_CS);
spi_putc(SPI_WRITE);
spi_putc(CNF3);
/* spi_putc((1<<PHSEG21)); // Bitrate 125 kbps at 16 MHz
spi_putc((1<<BTLMODE)|(1<<PHSEG11));
spi_putc((1<<BRP2)|(1<<BRP1)|(1<<BRP0));
*/
/*
spi_putc((1<<PHSEG21)); // Bitrate 250 kbps at 16 MHz
spi_putc((1<<BTLMODE)|(1<<PHSEG11));
spi_putc((1<<BRP1)|(1<<BRP0));
*/
spi_putc((1<<PHSEG21)); // Bitrate 250 kbps at 16 MHz
spi_putc((1<<BTLMODE)|(1<<PHSEG11));
//spi_putc(1<<BRP0);
spi_putc(speed);
// activate interrupts
spi_putc((1<<RX1IE)|(1<<RX0IE));
SET(MCP2515_CS);
// test if we could read back the value => is the chip accessible?
if (mcp2515_read_register(CNF1) != speed) {
/*SET(LED2_HIGH);*/
return false;
}
// deaktivate the RXnBF Pins (High Impedance State)
mcp2515_write_register(BFPCTRL, 0);
// set TXnRTS as inputs
mcp2515_write_register(TXRTSCTRL, 0);
// turn off filters => receive any message
mcp2515_write_register(RXB0CTRL, (1<<RXM1)|(1<<RXM0));
mcp2515_write_register(RXB1CTRL, (1<<RXM1)|(1<<RXM0));
// reset device to normal mode
mcp2515_write_register(CANCTRL, 0);
// SET(LED2_HIGH);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment