Cover image
Try Now
2024-10-20

`#![no_std]`与MCP2515 CAN Controler芯片进行交互的库。

3 years

Works with Finder

1

Github Watches

12

Github Forks

21

Github Stars

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:

Cargo Features

All features are disabled by default.

  • defmt - Implements defmt::Format for most public types so they can be printed using defmt::info!() and relatives
  • ufmt - Implements ufmt::uDebug for most public types so they can be printed using ufmt::uwriteln!() and relatives

Examples

Examples for some common microcontrollers are available in the examples/ folder.

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 panic_halt as _;

use arduino_hal::{spi::Settings, Delay, Spi};
use embedded_can::{ExtendedId, Frame, Id};
use embedded_hal_bus::spi::ExclusiveDevice;
use mcp2515::{error::Error, frame::CanFrame, regs::OpMode, CanSpeed, McpSpeed, MCP2515};

#[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);

    ufmt::uwriteln!(&mut serial, "Hello, world!").unwrap();

    let (spi, cs) = Spi::new(
        dp.SPI,
        pins.d13.into_output(),
        pins.d11.into_output(),
        pins.d12.into_pull_up_input(),
        pins.d10.into_output(),
        Settings {
            data_order: arduino_hal::spi::DataOrder::MostSignificantFirst,
            clock: arduino_hal::spi::SerialClockRate::OscfOver128,
            mode: embedded_hal::spi::MODE_0,
        },
    );
    let spi_device = ExclusiveDevice::new(spi, cs, Delay::new()).unwrap();
    let mut can = MCP2515::new(spi_device);
    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();
        ufmt::uwriteln!(&mut serial, "Sent message!").unwrap();

        // Read the message back (we are in loopback mode)
        match can.read_message() {
            Ok(frame) => ufmt::uwriteln!(&mut serial, "Received frame {:?}", frame).unwrap(),
            Err(Error::NoMessage) => ufmt::uwriteln!(&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.

相关推荐

  • Joshua Armstrong
  • Confidential guide on numerology and astrology, based of GG33 Public information

  • https://suefel.com
  • Latest advice and best practices for custom GPT development.

  • Alexandru Strujac
  • Efficient thumbnail creator for YouTube videos

  • Emmet Halm
  • Converts Figma frames into front-end code for various mobile frameworks.

  • Elijah Ng Shi Yi
  • Advanced software engineer GPT that excels through nailing the basics.

  • lumpenspace
  • Take an adjectivised noun, and create images making it progressively more adjective!

  • Lists Tailwind CSS classes in monospaced font

  • https://maiplestudio.com
  • Find Exhibitors, Speakers and more

  • https://appia.in
  • Siri Shortcut Finder – your go-to place for discovering amazing Siri Shortcuts with ease

  • Carlos Ferrin
  • Encuentra películas y series en plataformas de streaming.

  • Yusuf Emre Yeşilyurt
  • I find academic articles and books for research and literature reviews.

  • tomoyoshi hirata
  • Sony α7IIIマニュアルアシスタント

  • apappascs
  • 发现市场上最全面,最新的MCP服务器集合。该存储库充当集中式枢纽,提供了广泛的开源和专有MCP服务器目录,并提供功能,文档链接和贡献者。

  • ShrimpingIt
  • MCP系列GPIO Expander的基于Micropython I2C的操作,源自ADAFRUIT_MCP230XX

  • jae-jae
  • MCP服务器使用剧作《无头浏览器》获取网页内容。

  • ravitemer
  • 一个功能强大的Neovim插件,用于管理MCP(模型上下文协议)服务器

  • patruff
  • Ollama和MCP服务器之间的桥梁,使本地LLMS可以使用模型上下文协议工具

  • pontusab
  • 光标与风浪冲浪社区,查找规则和MCP

  • av
  • 毫不费力地使用一个命令运行LLM后端,API,前端和服务。

  • WangRongsheng
  • 🧑‍🚀 llm 资料总结(数据处理、模型训练、模型部署、 o1 模型、mcp 、小语言模型、视觉语言模型)|摘要世界上最好的LLM资源。

  • Mintplex-Labs
  • 带有内置抹布,AI代理,无代理构建器,MCP兼容性等的多合一桌面和Docker AI应用程序。

  • appcypher
  • 很棒的MCP服务器 - 模型上下文协议服务器的策划列表

    Reviews

    2 (1)
    Avatar
    user_R1wNiDQk
    2025-04-17

    I've been using the mcp2515-rs library by davidcole1340 and I'm thoroughly impressed with its functionality and ease of use. This Rust-based crate offers a seamless interface for CAN bus communication, making it an essential tool for embedded systems developers. The comprehensive documentation on the provided GitHub page is extremely helpful, ensuring a smooth integration into any project. Highly recommended!