diff --git a/Cargo.lock b/Cargo.lock index b213cffb..4aa63196 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -257,6 +257,7 @@ dependencies = [ "atsamd-hal", "common-arm", "embedded-hal", + "mcan", ] [[package]] @@ -580,6 +581,20 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" +[[package]] +name = "fdcan" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "553fb8420a8ca433ed032a3f284deeb95c12e4d3d25cdc15196669a27b2ae34b" +dependencies = [ + "bitflags 1.3.2", + "nb 1.1.0", + "paste", + "static_assertions", + "vcell", + "volatile-register", +] + [[package]] name = "fnv" version = "1.0.7" @@ -824,6 +839,7 @@ dependencies = [ "cortex-m-rt", "cortex-m-rtic", "defmt", + "fdcan", "messages", "postcard", "stm32h7xx-hal", @@ -1458,6 +1474,7 @@ dependencies = [ "embedded-dma", "embedded-hal", "embedded-storage", + "fdcan", "fugit", "nb 1.1.0", "paste", diff --git a/Cargo.toml b/Cargo.toml index 4984755c..137adc2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,26 +1,19 @@ [workspace] resolver = "2" -members = [ - "boards/*", - "examples/*", - "libraries/*" -] +members = ["boards/*", "examples/*", "libraries/*"] # Specify which members to build by default. Some libraries, such as messages, contain dev-dependencies that will give # compile errors if built directly. -default-members = [ - "boards/*", - "examples/*" -] +default-members = ["boards/*", "examples/*"] [workspace.dependencies.embedded-hal] -version="0.2.7" +version = "0.2.7" [workspace.dependencies.stm32h7xx-hal] -git = "https://github.com/stm32-rs/stm32h7xx-hal" +git = "https://github.com/stm32-rs/stm32h7xx-hal" # We use 35 even though we have the 33. -features = ["defmt", "rt", "stm32h735" ] +features = ["defmt", "rt", "stm32h735", "can"] [workspace.dependencies.atsamd-hal] git = "https://github.com/uorocketry/atsamd" @@ -70,4 +63,5 @@ opt-level = 0 #Try to remove this [profile.release.build-override] -#opt-level = 0 \ No newline at end of file +#opt-level = 0 + diff --git a/boards/nav/Cargo.toml b/boards/nav/Cargo.toml index c6c13b4c..9398a12a 100644 --- a/boards/nav/Cargo.toml +++ b/boards/nav/Cargo.toml @@ -9,10 +9,11 @@ edition = "2021" cortex-m = { workspace = true } cortex-m-rt = "^0.7.0" cortex-m-rtic = "1.1.3" -common-arm-stm32h7 = {path = "../../libraries/common-arm-stm32h7"} -common-arm = {path = "../../libraries/common-arm"} +common-arm-stm32h7 = { path = "../../libraries/common-arm-stm32h7" } +common-arm = { path = "../../libraries/common-arm" } stm32h7xx-hal = { workspace = true } postcard = "1.0.2" messages = { path = "../../libraries/messages" } systick-monotonic = "1.0.1" defmt = "0.3.2" +fdcan = "0.2" diff --git a/boards/nav/src/main.rs b/boards/nav/src/main.rs index 21dd83db..138c6d07 100644 --- a/boards/nav/src/main.rs +++ b/boards/nav/src/main.rs @@ -4,13 +4,24 @@ mod data_manager; use common_arm::*; +use core::num::{NonZeroU16, NonZeroU8}; use data_manager::DataManager; use defmt::info; +use fdcan::{ + config::NominalBitTiming, + filter::{StandardFilter, StandardFilterSlot}, + frame::{FrameFormat, TxFrameHeader}, + id::StandardId, +}; use stm32h7xx_hal::gpio::gpioa::{PA1, PA2}; use stm32h7xx_hal::gpio::gpioc::{PC13, PC3}; use stm32h7xx_hal::gpio::Input; +use stm32h7xx_hal::gpio::Speed; use stm32h7xx_hal::gpio::{Output, PushPull}; +use stm32h7xx_hal::pac; use stm32h7xx_hal::prelude::*; +use stm32h7xx_hal::spi; +use stm32h7xx_hal::{rcc, rcc::rec}; use systick_monotonic::*; /// Custom panic handler. @@ -52,16 +63,64 @@ mod app { let ccdr = rcc .use_hse(48.MHz()) .sys_ck(300.MHz()) + .pll1_strategy(rcc::PllConfigStrategy::Iterative) + .pll1_q_ck(24.MHz()) .freeze(pwrcfg, &ctx.device.SYSCFG); + let btr = NominalBitTiming { + prescaler: NonZeroU16::new(12).unwrap(), + seg1: NonZeroU8::new(13).unwrap(), + seg2: NonZeroU8::new(2).unwrap(), + sync_jump_width: NonZeroU8::new(1).unwrap(), + }; + // GPIO let gpioc = ctx.device.GPIOC.split(ccdr.peripheral.GPIOC); let gpioa = ctx.device.GPIOA.split(ccdr.peripheral.GPIOA); + let gpiod = ctx.device.GPIOD.split(ccdr.peripheral.GPIOD); + let gpiob = ctx.device.GPIOB.split(ccdr.peripheral.GPIOB); + assert_eq!(ccdr.clocks.pll1_q_ck().unwrap().raw(), 24_000_000); + let fdcan_prec = ccdr + .peripheral + .FDCAN + .kernel_clk_mux(rec::FdcanClkSel::Pll1Q); + + let can1 = { + let rx = gpiob.pb12.into_alternate().speed(Speed::VeryHigh); + let tx = gpiob.pb13.into_alternate().speed(Speed::VeryHigh); + ctx.device.FDCAN2.fdcan(tx, rx, fdcan_prec) + }; + + let mut can = can1; + can.set_protocol_exception_handling(false); + + can.set_nominal_bit_timing(btr); + + can.set_standard_filter( + StandardFilterSlot::_0, + StandardFilter::accept_all_into_fifo0(), + ); + + let (sck, miso, mosi) = (gpioa.pa5, gpioa.pa6, gpioa.pa7); + + let spi = ctx + .device + .SPI1 + .spi((sck, miso, mosi), spi::MODE_0, 1.MHz(), &ccdr.clocks); // leds let mut led_red = gpioa.pa1.into_push_pull_output(); let mut led_green = gpioa.pa2.into_push_pull_output(); + // UART for sbg + let tx = gpiod.pd1.into_alternate(); + let rx = gpiod.pd0.into_alternate(); + + let serial = ctx + .device + .UART4 + .serial((tx, rx), 9_800.bps(), ccdr.peripheral.UART4, &ccdr.clocks) + .unwrap(); blink::spawn().ok(); /* Monotonic clock */ diff --git a/libraries/common-arm-atsame/Cargo.toml b/libraries/common-arm-atsame/Cargo.toml index eb199ecf..1b98028f 100644 --- a/libraries/common-arm-atsame/Cargo.toml +++ b/libraries/common-arm-atsame/Cargo.toml @@ -8,4 +8,6 @@ edition = "2021" [dependencies] common-arm = { path = "../common-arm" } atsamd-hal = { workspace = true } -embedded-hal = {workspace = true } \ No newline at end of file +embedded-hal = { workspace = true } +mcan = "0.3" + diff --git a/libraries/common-arm-atsame/src/lib.rs b/libraries/common-arm-atsame/src/lib.rs index 701862fe..b2649251 100644 --- a/libraries/common-arm-atsame/src/lib.rs +++ b/libraries/common-arm-atsame/src/lib.rs @@ -4,3 +4,5 @@ //! This crate contains common code for HYDRA. Any code that is not board specific but is ATSAME specific should be put in //! here. //! + +pub use mcan; diff --git a/libraries/common-arm/Cargo.toml b/libraries/common-arm/Cargo.toml index a44ef2f6..276dd48c 100644 --- a/libraries/common-arm/Cargo.toml +++ b/libraries/common-arm/Cargo.toml @@ -17,6 +17,6 @@ derive_more = "0.99.17" cortex-m-rt = "^0.7.0" embedded-sdmmc = "0.3.0" embedded-hal = "0.2.7" -mcan = "0.3" nb = "1.1.0" +mcan = "0.3.0" messages = { path = "../messages" } diff --git a/libraries/common-arm/src/lib.rs b/libraries/common-arm/src/lib.rs index 83adaaf1..8209122f 100644 --- a/libraries/common-arm/src/lib.rs +++ b/libraries/common-arm/src/lib.rs @@ -6,8 +6,6 @@ //! here. //! -pub use mcan; - mod error; mod health; mod logging;