Skip to content

Commit

Permalink
Enable TWAI for ESP32-C6 (#1323)
Browse files Browse the repository at this point in the history
* Enable TWAI for ESP32-C6

* CHANGELOG.md

* Make Clippy happy, again

* Make Clippy happy. For real this time

* Remove debug code
  • Loading branch information
bjoernQ authored Mar 21, 2024
1 parent baea915 commit 4e5020f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `interrupt::enable` now has a direct CPU enable counter part, `interrupt::enable_direct` (#1310)
- `Delay::delay(time: fugit::MicrosDurationU64)`
- Added async support for TWAI (#1320)
- Add TWAI support for ESP32-C6 (#1323)

### Fixed

Expand Down
8 changes: 8 additions & 0 deletions esp-hal/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,14 @@ impl PeripheralClockControl {
system
.twai0_conf()
.modify(|_, w| w.twai0_rst_en().clear_bit());

// use Xtal clk-src
system.twai0_func_clk_conf().modify(|_, w| {
w.twai0_func_clk_en()
.set_bit()
.twai0_func_clk_sel()
.variant(false)
});
}
#[cfg(twai1)]
Peripheral::Twai1 => {
Expand Down
19 changes: 16 additions & 3 deletions esp-hal/src/twai/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ use core::marker::PhantomData;
use embedded_can::{nb::Can, Error, ErrorKind, ExtendedId, Frame, Id, StandardId};
#[cfg(not(feature = "embedded-hal"))] // FIXME
use embedded_hal_02::can::{Can, Error, ErrorKind, ExtendedId, Frame, Id, StandardId};
#[cfg(not(esp32c6))]
use fugit::HertzU32;

use self::filter::{Filter, FilterType};
Expand Down Expand Up @@ -227,8 +228,11 @@ impl BaudRate {
// {.brp = 4, .tseg_1 = 16, .tseg_2 = 8, .sjw = 3, .triple_sampling = false}
// #define TWAI_TIMING_CONFIG_1MBITS() {.brp = 4, .tseg_1 = 15, .tseg_2 = 4,
// .sjw = 3, .triple_sampling = false}
//
// see https://github.com/espressif/esp-idf/tree/master/components/hal/include/hal/twai_types.h
const fn timing(self) -> TimingConfig {
match self {
#[allow(unused_mut)]
let mut timing = match self {
Self::B125K => TimingConfig {
baud_rate_prescaler: 32,
sync_jump_width: 3,
Expand Down Expand Up @@ -258,7 +262,15 @@ impl BaudRate {
triple_sample: false,
},
Self::Custom(timing_config) => timing_config,
};

#[cfg(esp32c6)]
{
// clock source on ESP32-C6 is xtal (40MHz)
timing.baud_rate_prescaler /= 2;
}

timing
}
}

Expand Down Expand Up @@ -298,10 +310,11 @@ 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) {
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.
assert!(clocks.apb_clock == HertzU32::MHz(80));
#[cfg(not(esp32c6))]
assert!(_clocks.apb_clock == HertzU32::MHz(80));

// Unpack the baud rate timings and convert them to the values needed for the
// register. Many of the registers have a minimum value of 1 which is
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/embassy_twai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! This example should work with another ESP board running the `twai` example
//! with `IS_SENDER` set to `true`.

//% CHIPS: esp32c3 esp32s2 esp32s3
//% CHIPS: esp32c3 esp32c6 esp32s2 esp32s3
//% FEATURES: async embassy embassy-executor-thread embassy-time-timg0 embassy-generic-timers

#![no_std]
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/twai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//!
//! `IS_FIRST_SENDER` below must be set to false on one of the ESP's

//% CHIPS: esp32c3 esp32s2 esp32s3
//% CHIPS: esp32c3 esp32c6 esp32s2 esp32s3
//% FEATURES: embedded-hal

#![no_std]
Expand Down

0 comments on commit 4e5020f

Please sign in to comment.