diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index c7decb1b6aa..1ba21656b51 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -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 diff --git a/esp-hal/src/system.rs b/esp-hal/src/system.rs index 8f5f9f41e2d..42f04036d50 100755 --- a/esp-hal/src/system.rs +++ b/esp-hal/src/system.rs @@ -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 => { diff --git a/esp-hal/src/twai/mod.rs b/esp-hal/src/twai/mod.rs index ae302af21b6..46ccb1ba506 100644 --- a/esp-hal/src/twai/mod.rs +++ b/esp-hal/src/twai/mod.rs @@ -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}; @@ -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, @@ -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 } } @@ -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 diff --git a/examples/src/bin/embassy_twai.rs b/examples/src/bin/embassy_twai.rs index 3da9a69a397..a700fa0648e 100644 --- a/examples/src/bin/embassy_twai.rs +++ b/examples/src/bin/embassy_twai.rs @@ -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] diff --git a/examples/src/bin/twai.rs b/examples/src/bin/twai.rs index b9bf67dd2e1..f92d15193c7 100644 --- a/examples/src/bin/twai.rs +++ b/examples/src/bin/twai.rs @@ -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]