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
Next Next commit
Added some guards against Bus Off
  • Loading branch information
alexbohm committed Dec 21, 2022
commit c9404bcaa33a11d5df6973474a6bccbc035fa4e3
41 changes: 20 additions & 21 deletions esp-hal-common/src/twai/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,21 +343,21 @@ where
.bits()
}

/// Test if the transmit buffer is available for writing.
pub fn transmit_buffer_is_empty(&self) -> bool {
/// Check if the controller is in a bus off state.
pub fn is_bus_off(&self) -> bool {
self.peripheral
.register_block()
.status
.read()
.tx_buf_st()
.bit()
.bus_off_st()
.bit_is_set()
}

/// Get the number of messages that the peripheral has received.
/// Get the number of messages that the peripheral has available in the receive FIFO.
///
/// Note that this may not be the number of messages in the receive FIFO due to
/// fifo overflow/overrun.
pub fn num_messages(&self) -> u8 {
pub fn num_available_messages(&self) -> u8 {
self.peripheral
.register_block()
.rx_message_cnt
Expand All @@ -368,15 +368,17 @@ where
/// Clear the receive FIFO, discarding any valid, partial, or invalid packets.
///
/// This is typically used to clear an overrun receive FIFO.
///
/// TODO: Not sure if this needs to be guarded against Bus Off.
pub fn clear_receive_fifo(&self) {
while self.num_messages() > 0 {
while self.num_available_messages() > 0 {
self.release_receive_fifo();
}
}

/// Release the message in the buffer. This will decrement the received message
/// counter and prepare the next message in the FIFO for reading.
pub fn release_receive_fifo(&self) {
fn release_receive_fifo(&self) {
self.peripheral
.register_block()
.cmd
Expand All @@ -399,22 +401,14 @@ impl embedded_hal::can::Error for ESPTWAIError {
}

/// Copy data from multiple TWAI_DATA_x_REG registers, packing the source into the destination.
///
/// This function will step through the source memory by STEP bytes copying an individual byte
/// into each element of the destination.
///
unsafe fn copy_from_data_register(dest: &mut [u8], src: *const u32) {
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;
}
}
/// Copy data to multiple TWAI_DATA_x_REG registers, unpacking the source into the destination.
///
/// This function will step through the source memory by STEP bytes copying an individual byte
/// into each element of the destination.
///
unsafe fn copy_to_data_register(dest: *mut u32, src: &[u8]) {
let dest = from_raw_parts_mut(dest, src.len());

Expand Down Expand Up @@ -443,14 +437,14 @@ where
fn transmit(&mut self, frame: &Self::Frame) -> nb::Result<Option<Self::Frame>, Self::Error> {
let status = self.peripheral.register_block().status.read();

// Check that the peripheral is not already transmitting a packet.
if !status.tx_buf_st().bit_is_set() {
return nb::Result::Err(nb::Error::WouldBlock);
}
// Check that the peripheral is not in a bus off state.
if status.bus_off_st().bit_is_set() {
return nb::Result::Err(nb::Error::Other(ESPTWAIError::BusOff));
}
// Check that the peripheral is not already transmitting a packet.
if !status.tx_buf_st().bit_is_set() {
return nb::Result::Err(nb::Error::WouldBlock);
}

// Assemble the frame information into the data_0 byte.
let frame_format: u8 = frame.is_extended() as u8;
Expand Down Expand Up @@ -533,6 +527,11 @@ where
fn receive(&mut self) -> nb::Result<Self::Frame, Self::Error> {
let status = self.peripheral.register_block().status.read();

// Check that the peripheral is not in a bus off state.
if status.bus_off_st().bit_is_set() {
return nb::Result::Err(nb::Error::Other(ESPTWAIError::BusOff));
}

// Check that we actually have packets to receive.
if !status.rx_buf_st().bit_is_set() {
return nb::Result::Err(nb::Error::WouldBlock);
Expand Down