//////////////////////////////////////////////////////////// // Write command to the MCP2515 //////////////////////////////////////////////////////////// void can_write_cmd(char cmd){ spi_init_buffer(); spi_load_byte(cmd); spi_exchange(); spi_wait_for_completion(); }
//////////////////////////////////////////////////////////// // Write byte <data> to register with address <reg> //////////////////////////////////////////////////////////// void can_write_reg(char address, char data){ spi_init_buffer(); spi_load_byte(c2515Write); spi_load_byte(address); spi_load_byte(data); spi_exchange(); spi_wait_for_completion(); }
//////////////////////////////////////////////////////////// // Software reset in case the reset pin is not available //////////////////////////////////////////////////////////// void can_reset(){ // Reset the SPI buffer spi_init_buffer(); // Load and TX command spi_load_byte(c2515Reset); spi_exchange(); spi_wait_for_completion(); return; }
//////////////////////////////////////////////////////////// // Write <data> in <address> using <mask> //////////////////////////////////////////////////////////// void can_write_bits(char address, char data, char mask){ spi_init_buffer(); spi_load_byte(c2515BitMod); spi_load_byte(address); spi_load_byte(mask); spi_load_byte(data); spi_exchange(); spi_wait_for_completion(); }
//////////////////////////////////////////////////////////// // Wait for the completion of the SPI exchange // Skipped when running in debug mode. //////////////////////////////////////////////////////////// void spi_wait_for_completion(){ #ifdef DBG_COMPILE #warning "Debug build, spi_wait_for_completion will return immediately" return; #endif while (spi_counter){ }; spi_init_buffer(); }
void can_read_status(){ // Reset the SPI buffer spi_init_buffer(); // Load and TX command spi_load_byte(c2515Status); spi_load_byte(0x00); spi_exchange(); spi_wait_for_completion(); serial_printf("Status = "); serial_print_hex(spi_get_byte(1)); return; }
//////////////////////////////////////////////////////////// // Read the contents of the register at <address> //////////////////////////////////////////////////////////// char can_read_reg(char address){ spi_init_buffer(); // Read command spi_load_byte(c2515Read); // Register address spi_load_byte(address); // Expect one byte answer spi_load_zeros(0x01); // Start the transfer spi_exchange(); spi_wait_for_completion(); return spi_get_byte(2); }
//////////////////////////////////////////////////////////// // Read several bytes from the other device into the // data buffer. Used when you're talking through a // one-wire SPI interface (e.g. for nRF2401). // ! The data output pin should be disabled when this // ! routine is called. //////////////////////////////////////////////////////////// void spi_read(char nr_of_bytes){ // Buffer init spi_init_buffer(); // Set the number of bytes we want to read spi_load_zeros(nr_of_bytes); // Disable the output pin //spi_do_tris = 1; // Do the exchange spi_exchange(); // Wait for completion spi_wait_for_completion(); // Enable the output pin again //spi_do_tris = 0; return; }