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

CAN Support #100

Merged
merged 22 commits into from
Oct 20, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Support for `stm32f302x6` and `stm32f302x8` devices ([#132](https://github.com/stm32-rs/stm32f3xx-hal/pull/132))
- Support for the onboard real-time clock (RTC) ([#136](https://github.com/stm32-rs/stm32f3xx-hal/pull/136))
- Enable DMA for USART on `stm32f302` devices ([#139](https://github.com/stm32-rs/stm32f3xx-hal/pull/139))
- Basic CAN bus support ([#100](https://github.com/stm32-rs/stm32f3xx-hal/pull/100))

### Changed

Expand Down
13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ paste = "1"
rtcc = "0.2"
stm32f3 = "0.12"

[dependencies.embedded-hal-can]
version = "0.1.0"
optional = true

[dependencies.heapless]
version = "0.5.5"
optional = true

[dependencies.bare-metal]
version = "0.2"
features = ["const-fn"]
Expand Down Expand Up @@ -54,6 +62,7 @@ unproven = ["embedded-hal/unproven"]
device-selected = []
direct-call-deprecated = []
rt = ["stm32f3/rt"]
can = ["embedded-hal-can", "heapless"]

gpio-f302 = []
gpio-f303 = []
Expand Down Expand Up @@ -111,6 +120,10 @@ required-features = ["rt", "stm32f303xc", "stm32-usbd"]
name = "spi"
required-features = ["stm32f303"]

[[example]]
name = "can"
required-features = ["rt", "can", "stm32f303"]

[[example]]
name = "serial_dma"
required-features = ["stm32f303"]
Expand Down
93 changes: 93 additions & 0 deletions examples/can.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//! Example of using CAN.
#![no_std]
#![no_main]

use panic_semihosting as _;

use stm32f3xx_hal as hal;

use cortex_m_rt::entry;

use cortex_m::asm;
use hal::prelude::*;
use hal::stm32;
use hal::watchdog::IndependentWatchDog;

use hal::can::{Can, CanFilter, CanFrame, CanId, Filter, Frame, Receiver, Transmitter};
use nb::block;

// Each "node" needs a different ID, we set up a filter too look for messages to this ID
// Max value is 8 because we use a 3 bit mask in the filter
const ID: u16 = 0b100;

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

let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);

let _clocks = rcc
.cfgr
.use_hse(32.mhz())
.sysclk(32.mhz())
.pclk1(16.mhz())
.pclk2(16.mhz())
.freeze(&mut flash.acr);

// Configure CAN RX and TX pins (AF9)
let can_rx = gpioa.pa11.into_af9(&mut gpioa.moder, &mut gpioa.afrh);
let can_tx = gpioa.pa12.into_af9(&mut gpioa.moder, &mut gpioa.afrh);

// Initialize the CAN peripheral
let can = Can::can(dp.CAN, can_rx, can_tx, &mut rcc.apb1);

// Uncomment the following line to enable CAN interrupts
// can.listen(Event::Fifo0Fmp);

let (mut can_tx, mut rx0, _rx1) = can.split();

let mut led0 = gpiob
.pb15
.into_push_pull_output(&mut gpiob.moder, &mut gpiob.otyper);
led0.set_high().unwrap();

let filter = CanFilter::from_mask(0b100, ID as u32);
rx0.set_filter(filter);

// Watchdog makes sure this gets restarted periodically if nothing happens
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this is also now an example for the watchdog 👍

let mut iwdg = IndependentWatchDog::new(dp.IWDG);
iwdg.stop_on_debug(&dp.DBGMCU, true);
iwdg.start(100.ms());

// Send an initial message!
asm::delay(100_000);
let data: [u8; 1] = [0];

let frame = CanFrame::data_frame(CanId::BaseId(ID), &data);

block!(can_tx.transmit(&frame)).expect("Cannot send first CAN frame");

loop {
let rcv_frame = block!(rx0.receive()).expect("Cannot receive CAN frame");

if let Some(d) = rcv_frame.data() {
let counter = d[0].wrapping_add(1);

if counter % 3 == 0 {
led0.toggle().unwrap();
}

let data: [u8; 1] = [counter];
let frame = CanFrame::data_frame(CanId::BaseId(ID.into()), &data);

block!(can_tx.transmit(&frame)).expect("Cannot send CAN frame");
}

iwdg.feed();

asm::delay(1_000_000);
}
}
Loading