Skip to content

Commit

Permalink
#5 add rustdocs for modules
Browse files Browse the repository at this point in the history
  • Loading branch information
HusseinAbdelhamid committed Sep 10, 2024
1 parent e1b6fad commit 169c950
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 28 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ mockall = "0.11.0"
default = []
strict = []

[patch.crates-io]
bytes = { git = "https://github.com/atlas-aero/rt-bytes.git", branch = "cfg_target_has_atomic_v1.6.0" }

4 changes: 3 additions & 1 deletion example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ fn main() -> ! {
let can_id = Id::Standard(StandardId::new(0x55).unwrap());

// Create filter object for RX
let filter = Filter::new(can_id, 0).unwrap();
let mut filter = Filter::new(can_id, 0).unwrap();
// Set mask to match if only 2 LSB of ID match with filter
filter.set_mask_standard_id(0xFF);
let _ = can_controller.set_filter_object(filter);

// Create message frame
Expand Down
9 changes: 4 additions & 5 deletions src/can.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//!# CAN Controller
//!# CAN Controller device
//!
//!```
//! use mcp2517::can::Controller;
Expand All @@ -10,15 +10,14 @@
//! let cs_pin = ExampleCSPin{};
//!
//! // Initialize controller object
//! let mut can_controller = Controller::<_,_,sys_clk>::new(spi_bus,cs_pin);
//! let mut can_controller = Controller::new(spi_bus,cs_pin);
//!
//! // Use default configuration settings
//! let can_config = Configuration::default();
//!
//! // Configure CAN controller
//! let controller = can_controller.configure(&can_config,&sys_clk).unwrap();
//!
//!
//! can_controller.configure(&can_config, &sys_clk).unwrap();
//! ```
use crate::can::BusError::{CSError, TransferError};
use crate::can::ConfigError::{ClockError, ConfigurationModeTimeout, RequestModeTimeout};
use crate::config::{ClockConfiguration, Configuration};
Expand Down
32 changes: 20 additions & 12 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//!
//!
//! # CAN Module configuration
//! The [Configuration] struct provides an abstraction for configuring the CAN module registers.
//! ## Fifo configuration
Expand All @@ -13,12 +11,12 @@
//! use mcp2517::config::{FifoConfiguration,PayloadSize,RetransmissionAttempts};
//!
//! let fifo_config = FifoConfiguration{
//! pl_size:PayloadSize::EightBytes,
//! rx_size:10,
//! tx_attempts:RetransmissionAttempts::Unlimited,
//! tx_enable:true,
//! tx_priority:32,
//! tx_size:32,
//! pl_size: PayloadSize::EightBytes,
//! rx_size: 10,
//! tx_attempts: RetransmissionAttempts::Unlimited,
//! tx_enable: true,
//! tx_priority: 32,
//! tx_size: 32,
//! };
//!```
//! ## Clock configuration
Expand All @@ -32,12 +30,22 @@
//! use mcp2517::config::{ClockConfiguration, ClockOutputDivisor, PLLSetting, SystemClockDivisor};
//!
//! let clock_config = ClockConfiguration{
//! clock_output:ClockOutputDivisor::DivideBy2,
//! system_clock:SystemClockDivisor::DivideBy1,
//! pll:PLLSetting::DirectXTALOscillator,
//! disable_clock:false,
//! clock_output: ClockOutputDivisor::DivideBy2,
//! system_clock: SystemClockDivisor::DivideBy1,
//! pll: PLLSetting::DirectXTALOscillator,
//! disable_clock: false,
//! };
//!```
//! ## Bit rate configuration
//! It is recommended to use a SYSCLK frequency of 20 MHz or 40 MHz for the MCP2517FD CAN chip.
//! Based on the SYSCLK frequency used and the baud rate chosen, the CiNBTCFG regsiter values are configured.
//!```
//! use mcp2517::config::{BitRateConfig,CanBaudRate,SysClk};
//!
//! let bit_rate_config = BitRateConfig{
//! sys_clk: SysClk::MHz20,
//! can_speed: CanBaudRate::Kpbs500
//! };
//!
//!
use crate::status::OperationMode;
Expand Down
4 changes: 2 additions & 2 deletions src/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ impl Transfer<u8> for ExampleSPIBus {
return Ok(&[0, 0, 0b1001_0100]);
}

// return operation mode CAN2.0 (called in configure and during transmission)
Ok(&[0x0, 0x0, 0b1100_0000])
// return operation mode NormalCANFD mode (called in configure and during transmission)
Ok(&[0x0, 0x0, 0b0000_0000])
}
// C1FIFOSTA2
0x6C => Ok(&[0, 0, 0x1]),
Expand Down
24 changes: 20 additions & 4 deletions src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
//!# CAN Filter
//! The [Filter] object is used to create a CAN filter. The MCP2517FD CAN chip has 32 filter/mask registers.
//! Lower index of the filter means higher priority (highest priority =0, lowest priority = 31).
//!
//! ```
//! use mcp2517::filter::Filter;
//! use embedded_can::{Id,ExtendedId};
//!
//! // ID to match
//! let id = Id::Extended(ExtendedId::new(0xC672).unwrap());
//! // Create filter with index 2
//! let mut filter = Filter::new(id,2).unwrap();
//! // Set mask MSB bits, so that only the MSB of the message ID needs to match the filter
//! filter.set_mask_extended_id(0xFF00);
//!
//!
use crate::message::{EXTENDED_IDENTIFIER_MASK, STANDARD_IDENTIFIER_MASK};
use crate::registers::{FilterMaskReg, FilterObjectReg};
use embedded_can::{ExtendedId, Id, StandardId};
Expand All @@ -6,15 +22,15 @@ use embedded_can::{ExtendedId, Id, StandardId};
#[derive(Default, Debug)]
pub struct Filter {
/// filter & mask index
pub index: u8,
pub(crate) index: u8,
/// mask register bitfield
pub mask_bits: FilterMaskReg,
pub(crate) mask_bits: FilterMaskReg,
/// filter register bitfield
pub filter_bits: FilterObjectReg,
pub(crate) filter_bits: FilterObjectReg,
}

impl Filter {
/// Create new filter from embedded_can::Id and index, no mask
/// Create new filter from [embedded_can::Id] and index, no mask
pub fn new(identifier: Id, index: u8) -> Option<Self> {
if index > 31 {
return None;
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@
//! tx_size: 20,
//! tx_enable: true,
//! },
//! mode: RequestMode::NormalCAN2_0,
//! mode: RequestMode::NormalCANFD,
//! bit_rate: BitRateConfig{
//! sys_clk: SysClk::MHz20,
//! can_speed: CanBaudRate::Kpbs500
//! },
//! },
//! &clock,
//! ).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//!use embedded_can::{Id,StandardId};
//!
//! // Frame ID
//! let message_id = Id::Standard(StandardId::new(0x1234).unwrap());
//! let message_id = Id::Standard(StandardId::new(0x123).unwrap());
//! // Set message type to CAN2.0
//! let message_type = Can20{};
//! // Create payload buffer
Expand All @@ -21,6 +21,7 @@
//! let tx_message = TxMessage::new(message_type,bytes,message_id).unwrap();
//!```
//!
use bytes::Bytes;
use embedded_can::{ExtendedId, Id, StandardId};
use log::debug;
Expand Down

0 comments on commit 169c950

Please sign in to comment.