Skip to content

Swap to bxCan crate #207

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

Merged
merged 13 commits into from
Jul 2, 2021
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Basic serial implementation also available for UART4 and UART5 [#246]
- Implement serial DMA also for Serial [#246]

## Breaking Changes

- Refactor CAN to use the [`bxCan`](https://github.com/stm32-rs/bxcan) crate. ([#207])

## [v0.7.0] - 2021-06-18

### Added
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ cortex-m-rt = "0.6.14"
defmt = { version = "0.2.2", optional = true }
embedded-dma = "0.1.2"
embedded-hal = "0.2.5"
embedded-hal-can = { version = "0.1.0", optional = true }
embedded-time = "0.12.0"
nb = "1.0.0"
paste = "1.0.5"
rtcc = "0.2.1"
stm32f3 = "0.13.2"
bxcan = { version = ">=0.4, <0.6", optional = true }
stm32-usbd = { version = "0.6.0", optional = true }
void = { version = "1.0.2", default-features = false }

Expand Down Expand Up @@ -74,7 +74,7 @@ device-selected = []
direct-call-deprecated = []
ld = []
rt = ["stm32f3/rt"]
can = ["embedded-hal-can"]
can = ["bxcan"]

svd-f301 = ["stm32f3/stm32f301"]
svd-f302 = ["stm32f3/stm32f302"]
Expand Down Expand Up @@ -180,7 +180,7 @@ required-features = ["ld", "stm32f303"]

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

[[example]]
name = "serial_dma"
Expand Down
47 changes: 30 additions & 17 deletions examples/can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use hal::pac;
use hal::prelude::*;
use hal::watchdog::IndependentWatchDog;

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

// Each "node" needs a different ID, we set up a filter too look for messages to this ID
Expand All @@ -32,9 +34,10 @@ fn main() -> ! {
let _clocks = rcc
.cfgr
.use_hse(32.MHz())
.sysclk(32.MHz())
.pclk1(16.MHz())
.pclk2(16.MHz())
.hclk(64.MHz())
.sysclk(64.MHz())
.pclk1(32.MHz())
.pclk2(64.MHz())
.freeze(&mut flash.acr);

// Configure CAN RX and TX pins (AF9)
Expand All @@ -46,36 +49,46 @@ fn main() -> ! {
.into_af9_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);

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

// Uncomment the following line to enable CAN interrupts
// can.listen(Event::Fifo0Fmp);
// Use loopback mode: No pins need to be assigned to peripheral.
// APB1 (PCLK1): 64MHz, Bit rate: 500kBit/s, Sample Point 87.5%
// Value was calculated with http://www.bittiming.can-wiki.info/
can.modify_config()
.set_bit_timing(0x001c_0003)
Copy link
Member

Choose a reason for hiding this comment

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

Minor nit: Could you give this magic value a name? I see, that your comment explains the reason for this value, but I (as someone who has not really looked into can) do not fully grasp the meaning of each single bit.

.set_loopback(false)
.set_silent(false);

let (mut tx, mut rx0, _rx1) = can.split();
let mut filters = can.modify_filters();

filters.enable_bank(0, Mask32::accept_all());

// Enable filters.
drop(filters);
Copy link
Member

Choose a reason for hiding this comment

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

This seems confusing. Has the Drop trait the site effect of applying the filter?

Copy link
Member

Choose a reason for hiding this comment

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

Ah I see, filter holds a mutual reference to can, so can can not be used later, as long as the reference is held. Is this correct? And does it still compile without this drop?


// Sync to the bus and start normal operation.
block!(can.enable()).ok();

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.into());
rx0.set_filter(filter);

// Watchdog makes sure this gets restarted periodically if nothing happens
let mut iwdg = IndependentWatchDog::new(dp.IWDG);
iwdg.stop_on_debug(&dp.DBGMCU, true);
iwdg.start(100.milliseconds());

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

let frame = CanFrame::new_data(CanId::BaseId(ID), &data);
let frame = Frame::new_data(StandardId::new(ID).unwrap(), data);

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

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

if let Some(d) = rcv_frame.data() {
let counter = d[0].wrapping_add(1);
Expand All @@ -85,9 +98,9 @@ fn main() -> ! {
}

let data: [u8; 1] = [counter];
let frame = CanFrame::new_data(CanId::BaseId(ID), &data);
let frame = Frame::new_data(StandardId::new(ID).unwrap(), data);

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

iwdg.feed();
Expand Down
Loading