|
| 1 | +//! Showcases advanced CAN filter capabilities. |
| 2 | +//! Does not require additional transceiver hardware. |
| 3 | +
|
| 4 | +#![no_main] |
| 5 | +#![no_std] |
| 6 | + |
| 7 | +use bxcan::{ |
| 8 | + filter::{ListEntry16, ListEntry32, Mask16}, |
| 9 | + ExtendedId, Frame, StandardId, |
| 10 | +}; |
| 11 | +use panic_halt as _; |
| 12 | + |
| 13 | +use cortex_m_rt::entry; |
| 14 | +use embedded_hal::digital::v2::OutputPin; |
| 15 | +use nb::block; |
| 16 | +use stm32f1xx_hal::{can::Can, pac, prelude::*}; |
| 17 | + |
| 18 | +#[entry] |
| 19 | +fn main() -> ! { |
| 20 | + let dp = pac::Peripherals::take().unwrap(); |
| 21 | + |
| 22 | + let mut flash = dp.FLASH.constrain(); |
| 23 | + let mut rcc = dp.RCC.constrain(); |
| 24 | + |
| 25 | + // To meet CAN clock accuracy requirements, an external crystal or ceramic |
| 26 | + // resonator must be used. |
| 27 | + rcc.cfgr.use_hse(8.mhz()).freeze(&mut flash.acr); |
| 28 | + |
| 29 | + #[cfg(not(feature = "connectivity"))] |
| 30 | + let can = Can::new(dp.CAN1, &mut rcc.apb1, dp.USB); |
| 31 | + |
| 32 | + #[cfg(feature = "connectivity")] |
| 33 | + let can = Can::new(dp.CAN1, &mut rcc.apb1); |
| 34 | + |
| 35 | + let mut can = bxcan::Can::new(can); |
| 36 | + |
| 37 | + // Use loopback mode: No pins need to be assigned to peripheral. |
| 38 | + can.configure(|config| { |
| 39 | + // APB1 (PCLK1): 8MHz, Bit rate: 500Bit/s, Sample Point 87.5% |
| 40 | + // Value was calculated with http://www.bittiming.can-wiki.info/ |
| 41 | + config.set_bit_timing(0x001c_0000); |
| 42 | + config.set_loopback(true); |
| 43 | + config.set_silent(true); |
| 44 | + }); |
| 45 | + |
| 46 | + let mut filters = can.modify_filters(); |
| 47 | + assert!(filters.num_banks() > 3); |
| 48 | + |
| 49 | + // The order of the added filters is important: it must match configuration |
| 50 | + // of the `split_filters_advanced()` method. |
| 51 | + |
| 52 | + // 2x 11bit id + mask filter bank: Matches 0, 1, 2 |
| 53 | + // TODO: Make this accept also ID 2 |
| 54 | + filters.enable_bank( |
| 55 | + 0, |
| 56 | + [ |
| 57 | + Mask16::frames_with_std_id(StandardId::new(0).unwrap()), |
| 58 | + Mask16::frames_with_std_id(StandardId::new(1).unwrap()), |
| 59 | + ], |
| 60 | + ); |
| 61 | + |
| 62 | + // 2x 29bit id filter bank: Matches 4, 5 |
| 63 | + filters.enable_bank( |
| 64 | + 1, |
| 65 | + [ |
| 66 | + ListEntry32::data_frames_with_id(ExtendedId::new(4).unwrap()), |
| 67 | + ListEntry32::data_frames_with_id(ExtendedId::new(5).unwrap()), |
| 68 | + ], |
| 69 | + ); |
| 70 | + |
| 71 | + // 4x 11bit id filter bank: Matches 8, 9, 10, 11 |
| 72 | + filters.enable_bank( |
| 73 | + 2, |
| 74 | + [ |
| 75 | + ListEntry16::data_frames_with_id(StandardId::new(8).unwrap()), |
| 76 | + ListEntry16::data_frames_with_id(StandardId::new(9).unwrap()), |
| 77 | + ListEntry16::data_frames_with_id(StandardId::new(10).unwrap()), |
| 78 | + ListEntry16::data_frames_with_id(StandardId::new(11).unwrap()), |
| 79 | + ], |
| 80 | + ); |
| 81 | + |
| 82 | + // Enable filters. |
| 83 | + drop(filters); |
| 84 | + |
| 85 | + // Sync to the bus and start normal operation. |
| 86 | + block!(can.enable()).ok(); |
| 87 | + |
| 88 | + // Some messages shall pass the filters. |
| 89 | + for &id in &[0, 1, 8, 9, 10, 11] { |
| 90 | + let frame_tx = Frame::new_data(StandardId::new(id).unwrap(), [id as u8]); |
| 91 | + block!(can.transmit(&frame_tx)).unwrap(); |
| 92 | + let frame_rx = block!(can.receive()).unwrap(); |
| 93 | + assert_eq!(frame_tx, frame_rx); |
| 94 | + } |
| 95 | + for &id in &[4, 5] { |
| 96 | + let frame_tx = Frame::new_data(ExtendedId::new(id).unwrap(), [id as u8]); |
| 97 | + block!(can.transmit(&frame_tx)).unwrap(); |
| 98 | + let frame_rx = block!(can.receive()).unwrap(); |
| 99 | + assert_eq!(frame_tx, frame_rx); |
| 100 | + } |
| 101 | + |
| 102 | + // Some messages shall not be received. |
| 103 | + for &id in &[3, 6, 7, 12] { |
| 104 | + let frame_tx = Frame::new_data(ExtendedId::new(id).unwrap(), [id as u8]); |
| 105 | + block!(can.transmit(&frame_tx)).unwrap(); |
| 106 | + while !can.is_transmitter_idle() {} |
| 107 | + |
| 108 | + assert!(can.receive().is_err()); |
| 109 | + } |
| 110 | + |
| 111 | + let mut gpiob = dp.GPIOB.split(&mut rcc.apb2); |
| 112 | + let mut led = gpiob.pb9.into_push_pull_output(&mut gpiob.crh); |
| 113 | + led.set_high().unwrap(); |
| 114 | + |
| 115 | + loop {} |
| 116 | +} |
0 commit comments