Skip to content

Commit

Permalink
Clean up of comments, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbohm committed Sep 18, 2022
1 parent d53e509 commit e3c1ce8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 65 deletions.
8 changes: 3 additions & 5 deletions esp-hal-common/src/twai/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub struct SingleExtendedFilter {
impl Filter for SingleExtendedFilter {
const FILTER_TYPE: FilterType = FilterType::Single;
fn to_registers(&self) -> [u8; 8] {
panic!("Unimplemented");
todo!();
}
}

Expand All @@ -123,8 +123,7 @@ pub struct DualStandardFilter {
impl Filter for DualStandardFilter {
const FILTER_TYPE: FilterType = FilterType::Dual;
fn to_registers(&self) -> [u8; 8] {
// TODO: this.
panic!("Unimplemented");
todo!();
}
}
///
Expand All @@ -139,7 +138,6 @@ pub struct DualExtendedFilter {
impl Filter for DualExtendedFilter {
const FILTER_TYPE: FilterType = FilterType::Dual;
fn to_registers(&self) -> [u8; 8] {
// TODO: this.
panic!("Unimplemented");
todo!();
}
}
24 changes: 8 additions & 16 deletions esp-hal-common/src/twai/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ use self::filter::{Filter, FilterType};
pub mod bitselector;
pub mod filter;

/// Very basic implementation of the Frame trait.
///
/// Very basic structure backing the embedded_hal::can::Frame trait.
#[derive(Debug)]
pub struct ESPTWAIFrame {
id: can::Id,
Expand Down Expand Up @@ -53,8 +52,7 @@ impl embedded_hal::can::Frame for ESPTWAIFrame {
return None;
}

// TODO: See struct docs for TODO list, ideally this copy would be eliminated somehow.
let mut d: [u8; 8] = Default::default();
let mut d: [u8; 8] = [0; 8];
let (left, _unused) = d.split_at_mut(data.len());
left.clone_from_slice(data);

Expand All @@ -67,9 +65,13 @@ impl embedded_hal::can::Frame for ESPTWAIFrame {
}

fn new_remote(id: impl Into<can::Id>, dlc: usize) -> Option<Self> {
// CAN2.0 frames cannot have more than 8 bytes.
if dlc > 8 {
return None;
}
Some(ESPTWAIFrame {
id: id.into(),
data: Default::default(),
data: [0; 8],
dlc: dlc,
is_remote: true,
})
Expand All @@ -82,22 +84,18 @@ impl embedded_hal::can::Frame for ESPTWAIFrame {
}
}

#[inline(always)]
fn is_remote_frame(&self) -> bool {
self.is_remote
}

#[inline(always)]
fn id(&self) -> can::Id {
self.id
}

#[inline(always)]
fn dlc(&self) -> usize {
self.dlc
}

#[inline(always)]
fn data(&self) -> &[u8] {
match self.is_remote_frame() {
true => &[],
Expand Down Expand Up @@ -186,8 +184,6 @@ where
clocks: &Clocks,
baud_rate: BaudRate,
) -> Self {
// TODO: Probably should do a low level reset.

// Enable the peripheral clock for the TWAI peripheral.
clock_control.enable(crate::system::Peripheral::TWAI);

Expand All @@ -207,7 +203,6 @@ where
/// Set the bitrate of the bus.
///
/// Note: The timings currently assume a APB_CLK of 80MHz.
///
fn set_baud_rate(&mut self, baud_rate: BaudRate, clocks: &Clocks) {
// TWAI is clocked from the APB_CLK according to Table 6-4 [ESP32C3 Reference Manual](https://www.espressif.com/sites/default/files/documentation/esp32-c3_technical_reference_manual_en.pdf)
// Included timings are all for 80MHz so assert that we are running at 80MHz.
Expand Down Expand Up @@ -301,8 +296,6 @@ where
///
/// According to the [ESP32C3 Reference Manual](https://www.espressif.com/sites/default/files/documentation/esp32-c3_technical_reference_manual_en.pdf) 29.4.1.2 Operation Mode, in "Normal Mode":
/// The TWAI controller can transmit and receive messages including error signals (such as error and overload Frames)
///
///
pub struct TWAI<T> {
peripheral: T,
}
Expand All @@ -312,7 +305,6 @@ where
T: Instance,
{
/// Stop the peripheral, putting it into reset mode and enabling reconfiguration.
///
pub fn stop(self) -> TWAIConfiguration<T> {
// Put the peripheral into reset/configuration mode by setting the reset mode bit.
self.peripheral
Expand Down Expand Up @@ -367,7 +359,7 @@ where
///
/// This is typically used to clear an overrun receive FIFO.
///
/// TODO: Not sure if this needs to be guarded against Bus Off.
/// TODO: Not sure if this needs to be guarded against Bus Off or other error states.
pub fn clear_receive_fifo(&self) {
while self.num_available_messages() > 0 {
self.release_receive_fifo();
Expand Down
72 changes: 28 additions & 44 deletions esp32c3-hal/examples/twai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ use esp32c3_hal::{
pac::Peripherals,
prelude::*,
timer::TimerGroup,
twai::{
self,
bitselector::{BitSelectorNewExact, Selector},
ESPTWAIFrame,
},
twai::{self, ESPTWAIFrame},
Rtc, UsbSerialJtag,
};

Expand Down Expand Up @@ -69,23 +65,24 @@ fn main() -> ! {
// ],
// };
// TODO: even though this is a single, standard id filter, extended ids will also match this filter.
// A filter that matches standard ids of an even value.
let filter = twai::filter::SingleStandardFilter {
id: twai::bitselector::BitSelector {
bits: [
Selector::Set,
Selector::Any,
Selector::Any,
Selector::Any,
Selector::Any,
Selector::Any,
Selector::Any,
Selector::Any,
Selector::Any,
Selector::Any,
Selector::Any,
twai::bitselector::Selector::Reset,
twai::bitselector::Selector::Any,
twai::bitselector::Selector::Any,
twai::bitselector::Selector::Any,
twai::bitselector::Selector::Any,
twai::bitselector::Selector::Any,
twai::bitselector::Selector::Any,
twai::bitselector::Selector::Any,
twai::bitselector::Selector::Any,
twai::bitselector::Selector::Any,
twai::bitselector::Selector::Any,
],
},
rtr: twai::bitselector::BitSelector::new_exact(0b0),
rtr: twai::bitselector::BitSelector::new_any(),
data: [
twai::bitselector::BitSelector::new_any(),
twai::bitselector::BitSelector::new_any(),
Expand All @@ -110,36 +107,23 @@ fn main() -> ! {
let frame = block!(can.receive()).unwrap();
writeln!(UsbSerialJtag, " Received: {:?}", frame).unwrap();

// Increment the payload bytes by one.
let mut data: [u8; 8] = [0; 8];
data[..frame.dlc()].copy_from_slice(frame.data());
let frame = if frame.is_data_frame() {
// Increment the payload bytes by one.
let mut data: [u8; 8] = [0; 8];
data[..frame.dlc()].copy_from_slice(frame.data());

for b in data[..frame.dlc()].iter_mut() {
(*b, _) = (*b).overflowing_add(1);
}
for b in data[..frame.dlc()].iter_mut() {
(*b, _) = (*b).overflowing_add(1);
}

ESPTWAIFrame::new(frame.id(), &data[..frame.dlc()]).unwrap()
} else {
// Echo back the request.
frame
};

let frame = ESPTWAIFrame::new(frame.id(), &data[..frame.dlc()]).unwrap();
// Transmit the frame back.
writeln!(UsbSerialJtag, "Transmitting: {:?}", frame).unwrap();
// writeln!(UsbSerialJtag, "Transmitting: {:?}", frame).unwrap();
let _result = block!(can.transmit(&frame)).unwrap();

// let status = can.status();
// writeln!(
// UsbSerialJtag,
// "CAN Status: {:b}\n\t RX_BUF_ST {}\n\t OVERRUN_ST {}\n\t TX_BUF_ST {}\n\t TX_COMPLETE {}\n\t RX_ST {}\n\t TX_ST {}\n\t ERR_ST {}\n\t BUS_OFF_ST {}\n\t MISS_ST {}",
// status,
// (status >> 0) & 0b1 != 0,
// (status >> 1) & 0b1 != 0,
// (status >> 2) & 0b1 != 0,
// (status >> 3) & 0b1 != 0,
// (status >> 4) & 0b1 != 0,
// (status >> 5) & 0b1 != 0,
// (status >> 6) & 0b1 != 0,
// (status >> 7) & 0b1 != 0,
// (status >> 8) & 0b1 != 0,
// )
// .ok();

// block!(timer0.wait()).unwrap();
}
}

0 comments on commit e3c1ce8

Please sign in to comment.