|
| 1 | +//! Simple CAN example. |
| 2 | +//! Requires a transceiver connected to PB8, 9 (CAN1) or PB5 PB6 (CAN2). |
| 3 | +
|
| 4 | +#![no_main] |
| 5 | +#![no_std] |
| 6 | + |
| 7 | +use panic_halt as _; |
| 8 | + |
| 9 | +use bxcan::filter::Mask32; |
| 10 | +use bxcan::{Frame, StandardId}; |
| 11 | +use cortex_m_rt::entry; |
| 12 | +use nb::block; |
| 13 | +use stm32f4xx_hal::{can::Can, pac, prelude::*}; |
| 14 | + |
| 15 | +#[entry] |
| 16 | +fn main() -> ! { |
| 17 | + let dp = pac::Peripherals::take().unwrap(); |
| 18 | + |
| 19 | + let mut rcc = dp.RCC.constrain(); |
| 20 | + |
| 21 | + // To meet CAN clock accuracy requirements an external crystal or ceramic |
| 22 | + // resonator must be used. The blue pill has a 8MHz external crystal. |
| 23 | + // Other boards might have a crystal with another frequency or none at all. |
| 24 | + rcc.cfgr.use_hse(8.mhz()).freeze(); |
| 25 | + |
| 26 | + let gpiob = dp.GPIOB.split(); |
| 27 | + let mut can1 = { |
| 28 | + let can = Can::new(dp.CAN1, &mut rcc.apb1); |
| 29 | + |
| 30 | + let rx = gpiob.pb9.into_alternate_af9(); |
| 31 | + let tx = gpiob.pb8.into_alternate_af9(); |
| 32 | + |
| 33 | + bxcan::Can::new(can) |
| 34 | + }; |
| 35 | + can1.configure(|config| { |
| 36 | + // APB1 (PCLK1): 8MHz, Bit rate: 500kBit/s, Sample Point 87.5% |
| 37 | + // Value was calculated with http://www.bittiming.can-wiki.info/ |
| 38 | + config.set_bit_timing(0x001c_0000); |
| 39 | + }); |
| 40 | + |
| 41 | + // Configure filters so that can frames can be received. |
| 42 | + let mut filters = can1.modify_filters(); |
| 43 | + filters.enable_bank(0, Mask32::accept_all()); |
| 44 | + |
| 45 | + let can2 = { |
| 46 | + let can = Can::new(dp.CAN2, &mut rcc.apb1); |
| 47 | + |
| 48 | + let rx = gpiob.pb5.into_floating_input(); |
| 49 | + let tx = gpiob.pb6.into_alternate_af7(); |
| 50 | + |
| 51 | + let mut can2 = bxcan::Can::new(can); |
| 52 | + can2.configure(|config| { |
| 53 | + // APB1 (PCLK1): 8MHz, Bit rate: 500kBit/s, Sample Point 87.5% |
| 54 | + // Value was calculated with http://www.bittiming.can-wiki.info/ |
| 55 | + config.set_bit_timing(0x001c_0000); |
| 56 | + }); |
| 57 | + |
| 58 | + // A total of 28 filters are shared between the two CAN instances. |
| 59 | + // Split them equally between CAN1 and CAN2. |
| 60 | + filters.set_split(14); |
| 61 | + let mut slave_filters = filters.slave_filters(); |
| 62 | + slave_filters.enable_bank(14, Mask32::accept_all()); |
| 63 | + can2 |
| 64 | + }; |
| 65 | + |
| 66 | + // Drop filters to leave filter configuraiton mode. |
| 67 | + drop(filters); |
| 68 | + |
| 69 | + // Select the interface. |
| 70 | + let mut can = can1; |
| 71 | + //let mut can = can2; |
| 72 | + |
| 73 | + // Split the peripheral into transmitter and receiver parts. |
| 74 | + block!(can.enable()).unwrap(); |
| 75 | + |
| 76 | + // Echo back received packages in sequence. |
| 77 | + // See the `can-rtfm` example for an echo implementation that adheres to |
| 78 | + // correct frame ordering based on the transfer id. |
| 79 | + let mut test: [u8; 8] = [0; 8]; |
| 80 | + let mut count: u8 = 0; |
| 81 | + let id: u16 = 0x500; |
| 82 | + loop { |
| 83 | + test[0] = count; |
| 84 | + test[1] = 1; |
| 85 | + test[2] = 2; |
| 86 | + test[3] = 3; |
| 87 | + test[4] = 4; |
| 88 | + test[5] = 5; |
| 89 | + test[6] = 6; |
| 90 | + test[7] = 7; |
| 91 | + let test_frame = Frame::new_data(StandardId::new(id).unwrap(), test); |
| 92 | + block!(can.transmit(&test_frame)).unwrap(); |
| 93 | + if count < 255 { |
| 94 | + count += 1; |
| 95 | + } else { |
| 96 | + count = 0; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments