Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool mcp2515_init(void)
- {
- char str[80];
- unsigned int test_value;
- SET(MCP2515_CS);
- SET_OUTPUT(MCP2515_CS);
- SET(USB_CS);
- //SET_OUTPUT(USB_CS);
- SET(SD_CS);
- //SET_OUTPUT(SD_CS);
- 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);
- SET(MCP2515_INT);
- // active SPI master interface
- SPCR = (1<<SPE)|(1<<MSTR)|(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(15);
- // 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));
- // activate interrupts
- spi_putc((1<<RX1IE)|(1<<RX0IE));
- SET(MCP2515_CS);
- test_value = mcp2515_read_register(CANCTRL);
- sprintf(str, "test value is [%d]nn",test_value);
- UART1_TxString(str);
- // test if we could read back the value => is the chip accessible?
- if (mcp2515_read_register(CNF1) != ((1<<BRP2)|(1<<BRP1)|(1<<BRP0))) {
- 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);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement