Rust library for MCP2517 CAN controller
Find a file
2024-12-10 09:32:18 +01:00
.github/workflows #32 add RP2350 targets to QA pipeline 2024-12-09 12:56:17 +01:00
example #32 migrate to embedded-hal v1.0 2024-12-05 11:34:46 +01:00
src #32 simplify example code 2024-12-09 12:39:25 +01:00
.gitignore #5 add .vscode to .gitignore 2024-09-03 16:02:04 +02:00
Cargo.toml Incremented version to 0.2.0 2024-12-10 09:32:18 +01:00
DEVELOPMENT.md #6 add readme.md and license files 2024-10-15 13:44:21 +02:00
LICENSE-APACHE #6 add readme.md and license files 2024-10-15 13:44:21 +02:00
LICENSE-MIT #6 add readme.md and license files 2024-10-15 13:44:21 +02:00
README.md Added badges to README 2024-10-16 11:41:29 +02:00
rustfmt.toml Added base structure 2023-03-15 16:22:39 +01:00

rt-mcp2517

License License Crates.io Actions Status

Rust no_std library for MCP2517FD CAN controller

Crate currently offers the following features:

  • CAN2.0 and CAN FD format support
  • Standard and extended ID formats for CAN frames
  • no_std support

Example

use mcp2517::example::{ExampleClock, ExampleCSPin, ExampleSPIBus};
use mcp2517::can::{MCP2517, CanController};
use mcp2517::message::{Can20, TxMessage};
use mcp2517::filter::Filter;
use mcp2517::config::*;
use bytes::Bytes;
use embedded_can::{Id, StandardId};

let cs_pin = ExampleCSPin{};
let spi_bus = ExampleSPIBus::default ();
let clock = ExampleClock::default ();

let mut controller = MCP2517::new(spi_bus, cs_pin);

// configure CAN controller
controller
    .configure(
        &Configuration {
            clock: ClockConfiguration {
                clock_output: ClockOutputDivisor::DivideBy10,
                system_clock: SystemClockDivisor::DivideBy1,
                disable_clock: false,
                pll: PLLSetting::TenTimesPLL,
                 },
            fifo: FifoConfiguration {
                rx_size: 16,
                tx_attempts: RetransmissionAttempts::Three,
                tx_priority: 10,
                pl_size: PayloadSize::EightBytes,
                tx_size: 20,
                tx_enable: true,
                 },
            mode: RequestMode::NormalCANFD,
            bit_rate: BitRateConfig{
                sys_clk: SysClk::MHz20,
                can_speed: CanBaudRate::Kpbs500
                },
             },
        &clock,
         ).unwrap();

// Create message frame
let can_id = Id::Standard(StandardId::new(0x55).unwrap());

// Important note: Generic arg for message type for CAN2.0
// should be either 4 or 8, the DLC will be based off the
// length of the payload buffer. So for a payload of 5 bytes
// you can only use Can20::<8> as the message type
let message_type = Can20::<8 > {};
let payload = [0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8];
let pl_bytes = Bytes::copy_from_slice( & payload);
let can_message = TxMessage::new(message_type, pl_bytes, can_id).unwrap();

// Create and set filter object
let filter = Filter::new(can_id, 0).unwrap();
let _ = controller.set_filter_object(filter);

// Transmit CAN message
controller.transmit( & can_message).unwrap();

// Receive CAN message
let mut buff = [0u8; 8];
let result = controller.receive( & mut buff);
assert!(result.is_ok());
assert_eq!(buff, [0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);

Development

Any form of support is greatly appreciated. Feel free to create issues and PRs. See DEVELOPMENT for more details.

License

Licensed under either of

Each contributor agrees that his/her contribution covers both licenses.