-
Notifications
You must be signed in to change notification settings - Fork 69
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
CAN Support #100
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
0b8672e
canbus v1
zklapow e103a93
use crate traits
zklapow e893bd9
update crate name
zklapow e495e35
correct can frame length
zklapow b49010b
cleanup warnings
zklapow 2d761ae
add stm32f302x6/8 and CAN exmaple
zklapow 1a6ea4e
fix PR comments and warnings
zklapow 510e7a7
more cleanup
zklapow d402aa6
feature flag and cleanup
zklapow 2fa6fed
use arrayvec
zklapow eba0d39
Update src/can.rs
zklapow be1a6fe
Update Cargo.toml
zklapow 4618c51
switch to heapless, cleanup from comments
zklapow bd0dd68
add some brief docs
zklapow d2595f5
Merge branch 'master' into canbus-with-crate
zklapow 6d18d68
Merge branch 'master' into canbus-with-crate
zklapow df3203a
Merge branch 'master' into canbus-with-crate
zklapow 5031371
add CAN to changelog
zklapow 76efc71
Merge branch 'master' into canbus-with-crate
zklapow a73e28e
release RX and TX
zklapow a14c520
PR comments
zklapow 3a22595
rename
zklapow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nice, this is also now an example for the watchdog 👍