-
Notifications
You must be signed in to change notification settings - Fork 69
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
Swap to bxCan crate #207
Changes from all commits
13c70e8
44a3555
872b35c
295abe7
3859b2c
2e76280
314f14f
2dd3885
c6e9b50
123338e
494d08a
aab5787
2108746
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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) | ||
.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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems confusing. Has the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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(); | ||
|
There was a problem hiding this comment.
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.