GithubHelp home page GithubHelp logo

ptpaterson / mcp2515-rs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from davidcole1340/mcp2515-rs

0.0 1.0 0.0 60 KB

`#![no_std]` library for interacting with MCP2515 CAN controller chips.

License: Apache License 2.0

Rust 100.00%

mcp2515-rs's Introduction

MCP2515

#![no_std] library for interacting with MCP2515 CAN controller chips. Platform-agnostic, tested with arduino-hal on ATmega2560.

Inspired by the following C++ libraries:

Usage

Import the relevant HAL crate for your platform. For this example I'm using arduino-hal on an ATmega2560.

#![no_std]
#![no_main]

use arduino_hal::{spi::Settings, Delay, Spi};
use core::fmt::Write;
use embedded_hal::can::{ExtendedId, Frame, Id};
use mcp2515::{error::Error, frame::CanFrame, regs::OpMode, CanSpeed, McpSpeed, MCP2515};

#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
    loop {}
}

#[arduino_hal::entry]
fn main() -> ! {
    let dp = arduino_hal::Peripherals::take().unwrap();
    let pins = arduino_hal::pins!(dp);

    let mut delay = Delay::new();

    let mut serial = arduino_hal::default_serial!(dp, pins, 115200);
    let (spi, cs) = Spi::new(
        dp.SPI,
        pins.d52.into_output(),
        pins.d51.into_output(),
        pins.d50.into_pull_up_input(),
        pins.d53.into_output(),
        Settings {
            data_order: arduino_hal::spi::DataOrder::MostSignificantFirst,
            clock: arduino_hal::spi::SerialClockRate::OscfOver128,
            mode: embedded_hal::spi::MODE_0,
        },
    );
    let mut can = MCP2515::new(spi, cs);
    can.init(
      &mut delay,
      mcp2515::Settings {
        mode: OpMode::Loopback,       // Loopback for testing and example
        can_speed: CanSpeed::Kbps100, // Many options supported.
        mcp_speed: McpSpeed::MHz8,    // Currently 16MHz and 8MHz chips are supported.
        clkout_en: false,
      }
    )
    .unwrap();

    loop {
        // Send a message
        let frame = CanFrame::new(
            Id::Extended(ExtendedId::MAX),
            &[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08],
        )
        .unwrap();
        can.send_message(frame).unwrap();
        writeln!(&mut serial, "Sent message!").unwrap();

        // Read the message back (we are in loopback mode)
        match can.read_message() {
            Ok(frame) => {
                writeln!(&mut serial, "Received frame {:?}", frame).unwrap();
            }
            Err(Error::NoMessage) => writeln!(&mut serial, "No message to read!").unwrap(),
            Err(_) => panic!("Oh no!"),
        }

        arduino_hal::delay_ms(1000);
    }
}

Output over serial:

Sent message!
No message to read!
Sent message!
Received frame CanFrame { id: Extended(ExtendedId(536870911)), rtr: false, dlc: 8, data: [1, 2, 3, 4, 5, 6, 7, 8] }
Sent message!
Received frame CanFrame { id: Extended(ExtendedId(536870911)), rtr: false, dlc: 8, data: [1, 2, 3, 4, 5, 6, 7, 8] }
Sent message!
Received frame CanFrame { id: Extended(ExtendedId(536870911)), rtr: false, dlc: 8, data: [1, 2, 3, 4, 5, 6, 7, 8] }

License

Licensed under either of

at your option.

mcp2515-rs's People

Contributors

davidcole1340 avatar ptpaterson avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.