Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for g4 can2 and can3 #101

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 0 additions & 197 deletions src/can.rs

This file was deleted.

51 changes: 51 additions & 0 deletions src/can/g4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::{
pac::{FDCAN1, FDCAN2, FDCAN3, RCC},
util::rcc_en_reset,
};
use fdcan;

const MESSAGE_RAM_BASE_ADDRESS: u32 = 0x4000_a400;

// The RM is a bit contradictory. Table 3 implies that each FDCAN memory block is 0x400 in size.
// But section 44.3.3 says each block is 0x350 and that is what actually works.
const MESSAGE_RAM_SIZE: u32 = 0x350;

const FDCAN1_MESSAGE_RAM_ADDRESS: u32 = MESSAGE_RAM_BASE_ADDRESS;
const FDCAN2_MESSAGE_RAM_ADDRESS: u32 = FDCAN1_MESSAGE_RAM_ADDRESS + MESSAGE_RAM_SIZE;
const FDCAN3_MESSAGE_RAM_ADDRESS: u32 = FDCAN2_MESSAGE_RAM_ADDRESS + MESSAGE_RAM_SIZE;

macro_rules! create_cans {
($can:ident, $fdcan:ident, $msg_ram_address:ident ) => {
/// Interface to the CAN peripheral.
pub struct $can {
pub regs: $fdcan,
}
impl $can {
/// Initialize a CAN peripheral, including enabling and resetting
/// its RCC peripheral clock. This is not handled by the `bxcan` or `canfd` crates.
pub fn new(regs: $fdcan) -> Self {
let rcc = unsafe { &*RCC::ptr() };

rcc_en_reset!(apb1, fdcan, rcc);

Self { regs }
}

/// Print the (raw) contents of the status register.
pub fn read_status(&self) -> u32 {
unsafe { self.regs.psr.read().bits() }
}
}
unsafe impl fdcan::Instance for $can {
const REGISTERS: *mut fdcan::RegisterBlock = $fdcan::ptr() as *mut _;
}

unsafe impl fdcan::message_ram::Instance for $can {
const MSG_RAM: *mut fdcan::message_ram::RegisterBlock = ($msg_ram_address as *mut _);
}
};
}

create_cans!(Can1, FDCAN1, FDCAN1_MESSAGE_RAM_ADDRESS);
create_cans!(Can2, FDCAN2, FDCAN2_MESSAGE_RAM_ADDRESS);
create_cans!(Can3, FDCAN3, FDCAN3_MESSAGE_RAM_ADDRESS);
Loading