Support ESP32-C2 with 26MHz Xtal

This commit is contained in:
bjoernQ 2022-12-13 13:08:08 +01:00 committed by Jesse Braham
parent cfe83827cf
commit dc8963c0a8
11 changed files with 54 additions and 11 deletions

View File

@ -63,6 +63,9 @@ esp32c3 = ["esp32c3/rt", "riscv"]
esp32s2 = ["esp32s2/rt", "xtensa", "xtensa-lx/esp32s2", "xtensa-lx-rt/esp32s2", "esp-synopsys-usb-otg", "usb-device"] esp32s2 = ["esp32s2/rt", "xtensa", "xtensa-lx/esp32s2", "xtensa-lx-rt/esp32s2", "esp-synopsys-usb-otg", "usb-device"]
esp32s3 = ["esp32s3/rt", "xtensa", "xtensa-lx/esp32s3", "xtensa-lx-rt/esp32s3", "lock_api", "esp-synopsys-usb-otg", "usb-device"] esp32s3 = ["esp32s3/rt", "xtensa", "xtensa-lx/esp32s3", "xtensa-lx-rt/esp32s3", "lock_api", "esp-synopsys-usb-otg", "usb-device"]
esp32c2_40mhz = []
esp32c2_26mhz = []
# Implement the `embedded-hal==1.0.0-alpha.x` traits # Implement the `embedded-hal==1.0.0-alpha.x` traits
eh1 = ["embedded-hal-1", "embedded-hal-nb"] eh1 = ["embedded-hal-1", "embedded-hal-nb"]

View File

@ -12,6 +12,13 @@ fn main() {
n => panic!("Exactly 1 chip must be enabled via its Cargo feature, {n} provided"), n => panic!("Exactly 1 chip must be enabled via its Cargo feature, {n} provided"),
} }
if cfg!(feature = "esp32c2")
&& cfg!(feature = "esp32c2_40mhz")
&& cfg!(feature = "esp32c2_26mhz")
{
panic!("Only one xtal speed feature can be selected");
}
// Define all required configuration symbols for the enabled chip. // Define all required configuration symbols for the enabled chip.
// //
// When adding a new device, at the bare minimum the following symbols MUST be // When adding a new device, at the bare minimum the following symbols MUST be

View File

@ -227,7 +227,8 @@ impl ClockControl {
/// Use what is considered the default settings after boot. /// Use what is considered the default settings after boot.
#[allow(unused)] #[allow(unused)]
pub fn boot_defaults(clock_control: SystemClockControl) -> ClockControl { pub fn boot_defaults(clock_control: SystemClockControl) -> ClockControl {
ClockControl { #[cfg(feature = "esp32c2_40mhz")]
return ClockControl {
_private: (), _private: (),
desired_rates: RawClocks { desired_rates: RawClocks {
cpu_clock: HertzU32::MHz(80), cpu_clock: HertzU32::MHz(80),
@ -235,14 +236,28 @@ impl ClockControl {
xtal_clock: HertzU32::MHz(40), xtal_clock: HertzU32::MHz(40),
i2c_clock: HertzU32::MHz(40), i2c_clock: HertzU32::MHz(40),
}, },
} };
#[cfg(feature = "esp32c2_26mhz")]
return ClockControl {
_private: (),
desired_rates: RawClocks {
cpu_clock: HertzU32::MHz(80),
apb_clock: HertzU32::MHz(40),
xtal_clock: HertzU32::MHz(26),
i2c_clock: HertzU32::MHz(26),
},
};
} }
/// Configure the CPU clock speed. /// Configure the CPU clock speed.
#[allow(unused)] #[allow(unused)]
pub fn configure(clock_control: SystemClockControl, cpu_clock_speed: CpuClock) -> ClockControl { pub fn configure(clock_control: SystemClockControl, cpu_clock_speed: CpuClock) -> ClockControl {
let apb_freq; let apb_freq;
#[cfg(feature = "esp32c2_40mhz")]
let xtal_freq = XtalClock::RtcXtalFreq40M; let xtal_freq = XtalClock::RtcXtalFreq40M;
#[cfg(feature = "esp32c2_26mhz")]
let xtal_freq = XtalClock::RtcXtalFreq26M;
let pll_freq = PllClock::Pll480MHz; let pll_freq = PllClock::Pll480MHz;
if cpu_clock_speed.mhz() <= xtal_freq.mhz() { if cpu_clock_speed.mhz() <= xtal_freq.mhz() {

View File

@ -59,10 +59,16 @@ pub(crate) fn init() {
} }
pub(crate) fn configure_clock() { pub(crate) fn configure_clock() {
#[cfg(feature = "esp32c2_40mhz")]
assert!(matches!( assert!(matches!(
RtcClock::get_xtal_freq(), RtcClock::get_xtal_freq(),
XtalClock::RtcXtalFreq40M XtalClock::RtcXtalFreq40M
)); ));
#[cfg(feature = "esp32c2_26mhz")]
assert!(
matches!(RtcClock::get_xtal_freq(), XtalClock::RtcXtalFreq26M),
"Did you flash the right bootloader configured for 26MHz xtal?"
);
RtcClock::set_fast_freq(RtcFastClock::RtcFastClock8m); RtcClock::set_fast_freq(RtcFastClock::RtcFastClock8m);

View File

@ -1,5 +1,5 @@
[target.xtensa-esp32-none-elf] [target.xtensa-esp32-none-elf]
runner = "espflash --monitor" runner = "espflash flash --monitor"
[build] [build]
rustflags = [ rustflags = [

View File

@ -1,12 +1,12 @@
[target.riscv32imc-unknown-none-elf] [target.riscv32imc-unknown-none-elf]
runner = "espflash --monitor" runner = "espflash flash --monitor"
rustflags = [ rustflags = [
"-C", "link-arg=-Tlinkall.x" "-C", "link-arg=-Tlinkall.x"
] ]
# for testing: you can specify this target to see atomic emulation in action # for testing: you can specify this target to see atomic emulation in action
[target.riscv32imac-unknown-none-elf] [target.riscv32imac-unknown-none-elf]
runner = "espflash --monitor" runner = "espflash flash --monitor"
rustflags = [ rustflags = [
"-C", "link-arg=-Tlinkall.x" "-C", "link-arg=-Tlinkall.x"
] ]

View File

@ -46,7 +46,7 @@ ssd1306 = "0.7.1"
static_cell = "1.0.0" static_cell = "1.0.0"
[features] [features]
default = ["rt", "vectored"] default = ["rt", "vectored", "xtal40mhz"]
direct-boot = [] direct-boot = []
eh1 = ["esp-hal-common/eh1", "dep:embedded-hal-1", "dep:embedded-hal-nb"] eh1 = ["esp-hal-common/eh1", "dep:embedded-hal-1", "dep:embedded-hal-nb"]
rt = ["riscv-rt"] rt = ["riscv-rt"]
@ -55,7 +55,9 @@ vectored = ["esp-hal-common/vectored"]
async = ["esp-hal-common/async", "embedded-hal-async"] async = ["esp-hal-common/async", "embedded-hal-async"]
embassy = ["esp-hal-common/embassy"] embassy = ["esp-hal-common/embassy"]
embassy-time-systick = ["esp-hal-common/embassy-time-systick", "embassy-time/tick-hz-16_000_000"] embassy-time-systick = ["esp-hal-common/embassy-time-systick", "embassy-time/tick-hz-16_000_000"]
embassy-time-timg0 = ["esp-hal-common/embassy-time-timg0", "embassy-time/tick-hz-1_000_000"] embassy-time-timg0 = ["esp-hal-common/embassy-time-timg0", "embassy-time/tick-hz-1_000_000"]
xtal40mhz = ["esp-hal-common/esp32c2_40mhz"]
xtal26mhz = ["esp-hal-common/esp32c2_26mhz"]
[[example]] [[example]]
name = "spi_eh1_loopback" name = "spi_eh1_loopback"

View File

@ -2,6 +2,8 @@ use std::{env, fs::File, io::Write, path::PathBuf};
#[cfg(feature = "direct-boot")] #[cfg(feature = "direct-boot")]
fn main() { fn main() {
check_features();
// Put the linker script somewhere the linker can find it // Put the linker script somewhere the linker can find it
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
@ -36,6 +38,8 @@ fn main() {
#[cfg(not(feature = "direct-boot"))] #[cfg(not(feature = "direct-boot"))]
fn main() { fn main() {
check_features();
// Put the linker script somewhere the linker can find it // Put the linker script somewhere the linker can find it
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("memory.x")) File::create(out.join("memory.x"))
@ -62,6 +66,12 @@ fn main() {
add_defaults(); add_defaults();
} }
fn check_features() {
if cfg!(feature = "xtal40mhz") && cfg!(feature = "xtal26mhz") {
panic!("Only one xtal speed feature can be selected");
}
}
fn add_defaults() { fn add_defaults() {
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("hal-defaults.x")) File::create(out.join("hal-defaults.x"))

View File

@ -1,12 +1,12 @@
[target.riscv32imc-unknown-none-elf] [target.riscv32imc-unknown-none-elf]
runner = "espflash --monitor" runner = "espflash flash --monitor"
rustflags = [ rustflags = [
"-C", "link-arg=-Tlinkall.x" "-C", "link-arg=-Tlinkall.x"
] ]
# for testing: you can specify this target to see atomic emulation in action # for testing: you can specify this target to see atomic emulation in action
[target.riscv32imac-unknown-none-elf] [target.riscv32imac-unknown-none-elf]
runner = "espflash --monitor" runner = "espflash flash --monitor"
rustflags = [ rustflags = [
"-C", "link-arg=-Tlinkall.x" "-C", "link-arg=-Tlinkall.x"
] ]

View File

@ -1,5 +1,5 @@
[target.xtensa-esp32s2-none-elf] [target.xtensa-esp32s2-none-elf]
runner = "espflash --monitor" runner = "espflash flash --monitor"
[build] [build]
rustflags = [ rustflags = [

View File

@ -1,5 +1,5 @@
[target.xtensa-esp32s3-none-elf] [target.xtensa-esp32s3-none-elf]
runner = "espflash --monitor" runner = "espflash flash --monitor"
[build] [build]
rustflags = [ rustflags = [