Skip to content

Commit

Permalink
Merge pull request #62 from esp-rs/esp32s2-wifi
Browse files Browse the repository at this point in the history
Add support for ESP32-S2 WiFi
  • Loading branch information
jessebraham authored Sep 20, 2022
2 parents 82ea787 + f71cb41 commit 7d306f7
Show file tree
Hide file tree
Showing 17 changed files with 733 additions and 27 deletions.
18 changes: 18 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ rustflags = [
"-C", "link-arg=-Tesp32s3_rom_functions.x",
]

[target.xtensa-esp32s2-none-elf]
runner = "espflash --speed 921600 --monitor"

rustflags = [
"-C", "link-arg=-Tlinkall.x",
"-C", "link-arg=-Tesp32s2_rom_functions.x",

# Enable the atomic codegen option for Xtensa
"-C", "target-feature=+s32c1i",

# Tell the `core` library that we have atomics, even though it's not
# specified in the target definition
"--cfg", 'target_has_atomic="8"',
"--cfg", 'target_has_atomic="16"',
"--cfg", 'target_has_atomic="32"',
"--cfg", 'target_has_atomic="ptr"',
]

[build]
target = "riscv32imc-unknown-none-elf"

Expand Down
16 changes: 12 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void = { version = "1.0.2", default-features = false }
esp32c3-hal = { version = "0.2.0", optional = true }
esp32-hal = { version = "0.5.0", optional = true, features = [ "bluetooth","rt" ] }
esp32s3-hal = { version = "0.2.0", optional = true, features = [ "rt" ] }
esp32s2-hal = { version = "0.2.0", optional = true, features = [ "rt" ] }
riscv-rt = { version = "0.9.0", optional = true }
riscv = { version = "0.8.0", optional = true }
xtensa-lx-rt = { version = "0.13.0", optional = true }
Expand All @@ -36,22 +37,28 @@ riscv-target = { version = "0.1.2", optional = true }
ble-hci = { git = "https://github.com/bjoernQ/ble-hci", branch = "embedded-io" }

[target.xtensa-esp32-none-elf.dev-dependencies]
esp-println = { version = "0.3.0", features = [ "esp32", "log" ] }
esp-println = { version = "0.3.1", features = [ "esp32", "log" ] }
esp-backtrace = { version = "0.2.0", features = [ "esp32", "panic-handler", "exception-handler", "print-uart" ] }

[target.riscv32imc-unknown-none-elf.dev-dependencies]
esp-println = { version = "0.3.0", features = [ "esp32c3", "log" ] }
esp-println = { version = "0.3.1", features = [ "esp32c3", "log" ] }
esp-backtrace = { version = "0.2.0" , features = [ "esp32c3", "panic-handler", "exception-handler", "print-uart" ] }

[target.xtensa-esp32s3-none-elf.dev-dependencies]
esp-println = { version = "0.3.0", features = [ "esp32s3", "log" ] }
esp-println = { version = "0.3.1", features = [ "esp32s3", "log" ] }
esp-backtrace = { version = "0.2.0", features = [ "esp32s3", "panic-handler", "exception-handler", "print-uart" ] }

[target.xtensa-esp32s2-none-elf.dev-dependencies]
esp-println = { version = "0.3.1", features = [ "esp32s2", "log", "critical-section" ] }
esp-backtrace = { version = "0.2.0", features = [ "esp32s2", "panic-handler", "exception-handler", "print-uart" ] }
xtensa-atomic-emulation-trap = "0.2.0"

[features]
default = [ "utils" ]
esp32c3 = [ "riscv-target", "riscv", "riscv-rt", "esp32c3-hal" ]
esp32 = [ "esp32-hal", "xtensa-lx-rt/esp32", "xtensa-lx/esp32" ]
esp32s3 = [ "esp32s3-hal", "xtensa-lx-rt/esp32s3", "xtensa-lx/esp32s3" ]
esp32s2 = [ "esp32s2-hal", "xtensa-lx-rt/esp32s2", "xtensa-lx/esp32s2" ]
wifi_logs = []
dump_packets = []
utils = []
Expand All @@ -62,4 +69,5 @@ ble = []

# currently published versions don't contain all relevant adjustments - using git dependencies for now
[patch.crates-io]
esp32s3-hal = { git = "https://github.com/esp-rs/esp-hal/", package = "esp32s3-hal", rev = "6eea2f266a451678d244917329e2e885940fce9c" }
esp32s3-hal = { git = "https://github.com/esp-rs/esp-hal/", package = "esp32s3-hal", rev = "b1d5e37f36508ccfa835b236b2d30c3115bd3f72" }
esp32s2-hal = { git = "https://github.com/esp-rs/esp-hal/", package = "esp32s2-hal", rev = "b1d5e37f36508ccfa835b236b2d30c3115bd3f72" }
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Wifi and Bluetooth LE on ESP32-C3,ESP32 and ESP32-S3 (on bare-metal Rust)
# Wifi and Bluetooth LE on ESP32-C3,ESP32,ESP32-S3 and ESP32-S2 (on bare-metal Rust)

## About

Expand Down Expand Up @@ -46,6 +46,7 @@ https://github.com/esp-rs/esp-wireless-drivers-3rdparty/ (commit 8e5147d2de8d9b9
| `cargo "+esp" run --example ble --release --target xtensa-esp32-none-elf --features "esp32,ble"` | ESP32 |
| `cargo "+esp" run --example dhcp --release --target xtensa-esp32-none-elf --features "esp32,embedded-svc,wifi"` | ESP32 |
| `cargo "+esp" run --example dhcp --release --target xtensa-esp32s3-none-elf --features "esp32s3,embedded-svc,wifi"` | ESP32-S3|
| `cargo "+esp" run --example dhcp --release --target xtensa-esp32s2-none-elf --features "esp32s2,embedded-svc,wifi"` | ESP32-S2|

Additional you can specify these features
|Feature|Meaning|
Expand Down
80 changes: 76 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

#[cfg(any(feature = "esp32c3", feature = "esp32", feature = "esp32s3"))]
#[cfg(any(
feature = "esp32c3",
feature = "esp32",
feature = "esp32s3",
feature = "esp32s2"
))]
fn main() -> Result<(), String> {
let features: u8 = cfg!(feature = "wifi") as u8 + cfg!(feature = "ble") as u8;

Expand All @@ -15,6 +20,10 @@ fn main() -> Result<(), String> {
return Err("BLE is not yet supported for ESP32-S3".into());
}

if cfg!(feature = "esp32s2") && cfg!(feature = "ble") {
return Err("BLE is not supported for ESP32-S2".into());
}

if features >= 2 {
println!("cargo:rustc-cfg=coex");
}
Expand Down Expand Up @@ -191,7 +200,6 @@ fn handle_chip() {
);
copy(out, include_bytes!("libs/esp32s3/libphy.a"), "libphy.a");
copy(out, include_bytes!("libs/esp32s3/libpp.a"), "libpp.a");
//copy(out, include_bytes!("libs/esp32s3/librtc.a"), "librtc.a");
copy(
out,
include_bytes!("libs/esp32s3/libsmartconfig.a"),
Expand All @@ -212,7 +220,66 @@ fn handle_chip() {
println!("cargo:rustc-link-lib={}", "net80211");
println!("cargo:rustc-link-lib={}", "phy");
println!("cargo:rustc-link-lib={}", "pp");
//println!("cargo:rustc-link-lib={}", "rtc");
println!("cargo:rustc-link-lib={}", "smartconfig");
println!("cargo:rustc-link-lib={}", "wapi");
println!("cargo:rustc-link-lib={}", "wpa_supplicant");

println!("cargo:rustc-link-search={}", out.display());

// Only re-run the build script when memory.x is changed,
// instead of when any part of the source code changes.
println!("cargo:rerun-if-changed=memory.x");
}

#[cfg(feature = "esp32s2")]
fn handle_chip() {
// Put the linker script somewhere the linker can find it
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());

copy(
out,
include_bytes!("ld/esp32s2/rom_functions.x"),
"esp32s2_rom_functions.x",
);

copy(
out,
include_bytes!("libs/esp32s2/libcoexist.a"),
"libcoexist.a",
);
copy(out, include_bytes!("libs/esp32s2/libcore.a"), "libcore.a");
copy(
out,
include_bytes!("libs/esp32s2/libespnow.a"),
"libespnow.a",
);
copy(out, include_bytes!("libs/esp32s2/libmesh.a"), "libmesh.a");
copy(
out,
include_bytes!("libs/esp32s2/libnet80211.a"),
"libnet80211.a",
);
copy(out, include_bytes!("libs/esp32s2/libphy.a"), "libphy.a");
copy(out, include_bytes!("libs/esp32s2/libpp.a"), "libpp.a");
copy(
out,
include_bytes!("libs/esp32s2/libsmartconfig.a"),
"libsmartconfig.a",
);
copy(out, include_bytes!("libs/esp32s2/libwapi.a"), "libwapi.a");
copy(
out,
include_bytes!("libs/esp32s2/libwpa_supplicant.a"),
"libwpa_supplicant.a",
);

println!("cargo:rustc-link-lib={}", "coexist");
println!("cargo:rustc-link-lib={}", "core");
println!("cargo:rustc-link-lib={}", "espnow");
println!("cargo:rustc-link-lib={}", "mesh");
println!("cargo:rustc-link-lib={}", "net80211");
println!("cargo:rustc-link-lib={}", "phy");
println!("cargo:rustc-link-lib={}", "pp");
println!("cargo:rustc-link-lib={}", "smartconfig");
println!("cargo:rustc-link-lib={}", "wapi");
println!("cargo:rustc-link-lib={}", "wpa_supplicant");
Expand All @@ -231,7 +298,12 @@ fn copy(path: &PathBuf, data: &[u8], name: &str) {
.unwrap();
}

#[cfg(not(any(feature = "esp32c3", feature = "esp32", feature = "esp32s3")))]
#[cfg(not(any(
feature = "esp32c3",
feature = "esp32",
feature = "esp32s3",
feature = "esp32s2"
)))]
fn main() {
panic!("Select a chip via it's cargo feature");
}
10 changes: 6 additions & 4 deletions examples/dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use esp32_hal as hal;
#[cfg(feature = "esp32c3")]
use esp32c3_hal as hal;
#[cfg(feature = "esp32s2")]
use esp32s2_hal as hal;
#[cfg(feature = "esp32s3")]
use esp32s3_hal as hal;

Expand All @@ -32,7 +34,7 @@ use hal::system::SystemExt;

#[cfg(feature = "esp32c3")]
use riscv_rt::entry;
#[cfg(any(feature = "esp32", feature = "esp32s3"))]
#[cfg(any(feature = "esp32", feature = "esp32s3", feature = "esp32s2"))]
use xtensa_lx_rt::entry;

extern crate alloc;
Expand All @@ -54,13 +56,13 @@ fn main() -> ! {

#[cfg(feature = "esp32c3")]
let clocks = ClockControl::configure(system.clock_control, CpuClock::Clock160MHz).freeze();
#[cfg(any(feature = "esp32", feature = "esp32s3"))]
#[cfg(any(feature = "esp32", feature = "esp32s3", feature = "esp32s2"))]
let clocks = ClockControl::configure(system.clock_control, CpuClock::Clock240MHz).freeze();

let mut rtc = Rtc::new(peripherals.RTC_CNTL);

// Disable watchdog timers
#[cfg(not(feature = "esp32"))]
#[cfg(not(any(feature = "esp32", feature = "esp32s2")))]
rtc.swd.disable();

rtc.rwdt.disable();
Expand All @@ -75,7 +77,7 @@ fn main() -> ! {
let syst = SystemTimer::new(peripherals.SYSTIMER);
initialize(syst.alarm0, peripherals.RNG, &clocks).unwrap();
}
#[cfg(any(feature = "esp32", feature = "esp32s3"))]
#[cfg(any(feature = "esp32", feature = "esp32s3", feature = "esp32s2"))]
{
use hal::timer::TimerGroup;
let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks);
Expand Down
69 changes: 69 additions & 0 deletions ld/esp32s2/rom_functions.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* ROM APIs
*/

PROVIDE ( esp_rom_crc32_le = crc32_le );
PROVIDE ( esp_rom_crc16_le = crc16_le );
PROVIDE ( esp_rom_crc8_le = crc8_le );

PROVIDE ( esp_rom_gpio_pad_select_gpio = gpio_pad_select_gpio );
PROVIDE ( esp_rom_gpio_pad_pullup_only = gpio_pad_pullup );
PROVIDE ( esp_rom_gpio_pad_set_drv = gpio_pad_set_drv );
PROVIDE ( esp_rom_gpio_pad_unhold = gpio_pad_unhold );
PROVIDE ( esp_rom_gpio_connect_in_signal = gpio_matrix_in );
PROVIDE ( esp_rom_gpio_connect_out_signal = gpio_matrix_out );

PROVIDE ( esp_rom_efuse_mac_address_crc8 = esp_crc8 );
PROVIDE ( esp_rom_efuse_get_flash_gpio_info = ets_efuse_get_spiconfig );
PROVIDE ( esp_rom_efuse_get_flash_wp_gpio = ets_efuse_get_wp_pad );
PROVIDE ( esp_rom_efuse_get_opiconfig = ets_efuse_get_opiconfig );
PROVIDE ( esp_rom_efuse_is_secure_boot_enabled = ets_efuse_secure_boot_enabled );

PROVIDE ( esp_rom_uart_flush_tx = uart_tx_flush );
PROVIDE ( esp_rom_uart_tx_one_char = uart_tx_one_char );
PROVIDE ( esp_rom_uart_tx_wait_idle = uart_tx_wait_idle );
PROVIDE ( esp_rom_uart_rx_one_char = uart_rx_one_char );
PROVIDE ( esp_rom_uart_rx_string = UartRxString );
PROVIDE ( esp_rom_uart_set_as_console = uart_tx_switch );
PROVIDE ( esp_rom_uart_usb_acm_init = Uart_Init_USB );
PROVIDE ( esp_rom_uart_putc = ets_write_char_uart );

/* wpa_supplicant re-implements the MD5 functions: MD5Init, MD5Update, MD5Final */
/* so here we directly assign the symbols with the ROM API address */
PROVIDE ( esp_rom_md5_init = 0x4000526c );
PROVIDE ( esp_rom_md5_update = 0x4000528c );
PROVIDE ( esp_rom_md5_final = 0x4000530c );

PROVIDE ( esp_rom_printf = ets_printf );
PROVIDE ( esp_rom_delay_us = ets_delay_us );
PROVIDE ( esp_rom_install_uart_printf = ets_install_uart_printf );
PROVIDE ( esp_rom_get_reset_reason = rtc_get_reset_reason );
PROVIDE ( esp_rom_route_intr_matrix = intr_matrix_set );
PROVIDE ( esp_rom_get_cpu_ticks_per_us = ets_get_cpu_frequency );

PROVIDE ( esp_rom_spiflash_clear_bp = esp_rom_spiflash_unlock );
PROVIDE ( esp_rom_spiflash_write_enable = SPI_write_enable);

PROVIDE ( esp_rom_spiflash_fix_dummylen = spi_dummy_len_fix );
PROVIDE ( esp_rom_spiflash_set_drvs = SetSpiDrvs);
PROVIDE ( esp_rom_spiflash_select_padsfunc = SelectSpiFunction );
PROVIDE ( esp_rom_spiflash_common_cmd = SPI_Common_Command );

/* from esp32s2.rom.ld */
PROVIDE ( ets_delay_us = 0x4000d888 );
PROVIDE ( roundup2 = 0x4001bcd0 );
PROVIDE ( g_phyFuns = 0x3ffffd90 );
PROVIDE ( g_phyFuns_instance = 0x3ffffd94 );
PROVIDE ( phy_get_romfuncs = 0x4000a88c );
PROVIDE ( rom_read_hw_noisefloor = 0x40009c38 );
PROVIDE ( rom_phy_disable_low_rate = 0x4000a2b8 );
PROVIDE ( rom_phy_enable_low_rate = 0x4000a280 );
PROVIDE ( intr_matrix_set = 0x4000f1d0 );

/* from esp32s2.rom.newlib-funcs.ld */
strncpy = 0x40007f20;
strcpy = 0x40007cfc;
strncmp = 0x4001ae64;

/* from esp32s3.rom.libgcc.ld*/
__popcountsi2 = 0x40008fa8;
7 changes: 6 additions & 1 deletion src/binary/include_esp32s2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* automatically generated by rust-bindgen 0.59.2 */

#![allow(non_camel_case_types,non_snake_case,non_upper_case_globals,dead_code)]
#![allow(
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
dead_code
)]

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down
1 change: 1 addition & 0 deletions src/binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pub mod c_types;
#[cfg_attr(feature = "esp32c3", path = "include_esp32c3.rs")]
#[cfg_attr(feature = "esp32", path = "include_esp32.rs")]
#[cfg_attr(feature = "esp32s3", path = "include_esp32s3.rs")]
#[cfg_attr(feature = "esp32s2", path = "include_esp32s2.rs")]
pub mod include;
Loading

0 comments on commit 7d306f7

Please sign in to comment.