Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
e015806
wip: initial implementation of transmission only.
alexbohm Sep 7, 2022
b8ec8b4
Moved TWAI to its own directory and added initial reception of packets.
alexbohm Sep 8, 2022
c8a2291
Added extended id transmit and receive.
alexbohm Sep 9, 2022
0e5e04f
Added maybe better code for making packet filters.
alexbohm Sep 11, 2022
69613cc
Fixed bug with ids and improved methods of copying data to the periph…
alexbohm Sep 12, 2022
c9404bc
Added some guards against Bus Off
alexbohm Sep 13, 2022
d675ac9
Added reception of remote frames.
alexbohm Sep 18, 2022
e08ef1f
Clean up of comments, etc
alexbohm Sep 18, 2022
cd343de
Updated TWAI naming and cleaned up example a bit.
alexbohm Sep 21, 2022
336e104
Updated bitselector to include better unpacking methods.
alexbohm Sep 29, 2022
3e4a46b
Add embedded-can and limit initial TWAI implementation to esp32c3.
alexbohm Nov 10, 2022
b28bb96
Added embedded-can to esp32c3 twai example.
alexbohm Nov 10, 2022
0389fe8
Switched twai filter to using bytestrings.
alexbohm Nov 12, 2022
f26d19a
Implemented new() for twai filters.
alexbohm Nov 13, 2022
3553a0c
Clean up TWAI docs and example.
alexbohm Nov 17, 2022
fc7a428
Fix filter constructors and add examples.
alexbohm Nov 18, 2022
54b5e5d
pre driver PeripheralRef update.
alexbohm Dec 17, 2022
2b6778d
PeripheralRef/twai
alexbohm Dec 17, 2022
a8e875e
Format comments with nightly rustfmt.
alexbohm Dec 17, 2022
d18a5cc
Add gpio PeripheralRef and use volatile for direct register access.
alexbohm Dec 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add gpio PeripheralRef and use volatile for direct register access.
  • Loading branch information
alexbohm committed Dec 22, 2022
commit d18a5cc96fb952a3cc0c366bedb4370603c11388
70 changes: 40 additions & 30 deletions esp-hal-common/src/twai/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
//! controllers. It supports Standard Frame Format (11-bit) and Extended Frame
//! Format (29-bit) frame identifiers.

use core::slice::{from_raw_parts, from_raw_parts_mut};

#[cfg(feature = "eh1")]
use embedded_can::{nb::Can, Error, ErrorKind, ExtendedId, Frame, Id, StandardId};
#[cfg(not(feature = "eh1"))]
Expand Down Expand Up @@ -35,20 +33,25 @@ pub struct EspTwaiFrame {
}

impl EspTwaiFrame {
/// Make a new frame from an id and a slice of the TWAI_DATA_x_REG
/// registers.
fn new_from_data_registers(id: impl Into<Id>, data: &[u32]) -> Self {
let mut d: [u8; 8] = [0; 8];
/// Make a new frame from an id, pointer to the TWAI_DATA_x_REG registers,
/// and the length of the data payload (dlc).
///
/// # Safety
/// This is unsafe because it directly accesses peripheral registers.
unsafe fn new_from_data_registers(
id: impl Into<Id>,
registers: *const u32,
dlc: usize,
) -> Self {
let mut data: [u8; 8] = [0; 8];

// Copy the data from the memory mapped peripheral into actual memory.
for (src, dest) in data.iter().zip(d.iter_mut()) {
*dest = *src as u8;
}
copy_from_data_register(&mut data[..dlc], registers);

Self {
id: id.into(),
data: d,
dlc: data.len(),
data,
dlc: dlc,
is_remote: false,
}
}
Expand Down Expand Up @@ -198,8 +201,8 @@ where
{
pub fn new<TX: OutputPin, RX: InputPin>(
peripheral: impl Peripheral<P = T> + 'd,
mut tx_pin: TX,
mut rx_pin: RX,
tx_pin: impl Peripheral<P = TX> + 'd,
rx_pin: impl Peripheral<P = RX> + 'd,
clock_control: &mut PeripheralClockControl,
clocks: &Clocks,
baud_rate: BaudRate,
Expand All @@ -208,6 +211,7 @@ where
clock_control.enable(crate::system::Peripheral::Twai);

// Set up the GPIO pins.
crate::into_ref!(tx_pin, rx_pin);
tx_pin.connect_peripheral_to_output(OutputSignal::TWAI_TX);
rx_pin.connect_input_to_peripheral(InputSignal::TWAI_RX);

Expand Down Expand Up @@ -430,11 +434,11 @@ impl Error for EspTwaiError {
/// memory-mapped registers. Specifically, this function is used with the
/// TWAI_DATA_x_REG registers which has different results based on the mode of
/// the peripheral.
unsafe fn _copy_from_data_register(dest: &mut [u8], src: *const u32) {
let src = from_raw_parts(src, dest.len());

for (dest, src) in dest.iter_mut().zip(src.iter()) {
*dest = *src as u8;
#[inline(always)]
unsafe fn copy_from_data_register(dest: &mut [u8], src: *const u32) {
for (i, dest) in dest.iter_mut().enumerate() {
// Perform a volatile read to avoid compiler optimizations.
*dest = src.add(i).read_volatile() as u8;
}
}

Expand All @@ -446,11 +450,11 @@ unsafe fn _copy_from_data_register(dest: &mut [u8], src: *const u32) {
/// memory-mapped registers. Specifically, this function is used with the
/// TWAI_DATA_x_REG registers which has different results based on the mode of
/// the peripheral.
#[inline(always)]
unsafe fn copy_to_data_register(dest: *mut u32, src: &[u8]) {
let dest = from_raw_parts_mut(dest, src.len());

for (dest, src) in dest.iter_mut().zip(src.iter()) {
*dest = *src as u32;
for (i, src) in src.iter().enumerate() {
// Perform a volatile write to avoid compiler optimizations.
dest.add(i).write_volatile(*src as u32);
}
}

Expand Down Expand Up @@ -625,10 +629,13 @@ where
if is_data_frame {
// Create a new frame from the contents of the appropriate TWAI_DATA_x_REG
// registers.
let register_data = unsafe {
from_raw_parts(self.peripheral.register_block().data_3.as_ptr(), dlc)
};
EspTwaiFrame::new_from_data_registers(id, register_data)
unsafe {
EspTwaiFrame::new_from_data_registers(
id,
self.peripheral.register_block().data_3.as_ptr(),
dlc,
)
}
} else {
EspTwaiFrame::new_remote(id, dlc).unwrap()
}
Expand Down Expand Up @@ -674,10 +681,13 @@ where
let id = ExtendedId::new(raw_id).unwrap();

if is_data_frame {
let register_data = unsafe {
from_raw_parts(self.peripheral.register_block().data_5.as_ptr(), dlc)
};
EspTwaiFrame::new_from_data_registers(id, register_data)
unsafe {
EspTwaiFrame::new_from_data_registers(
id,
self.peripheral.register_block().data_5.as_ptr(),
dlc,
)
}
} else {
EspTwaiFrame::new_remote(id, dlc).unwrap()
}
Expand Down