From 315fbd5a6b2c7cf4aabf2ec2a2dd484b0559b0e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Mich=C3=A1lek?= Date: Mon, 25 Jul 2022 13:58:56 +0200 Subject: [PATCH 1/4] initial version of ESP32-S2 SPI display example --- esp32s2-hal/examples/spi_display.rs | 120 ++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 esp32s2-hal/examples/spi_display.rs diff --git a/esp32s2-hal/examples/spi_display.rs b/esp32s2-hal/examples/spi_display.rs new file mode 100644 index 00000000000..a9ef6a261d0 --- /dev/null +++ b/esp32s2-hal/examples/spi_display.rs @@ -0,0 +1,120 @@ +#![no_std] +#![no_main] + +use embedded_graphics::prelude::RgbColor; +use esp32s2_hal::{clock::ClockControl, pac::Peripherals, prelude::*, timer::TimerGroup, RtcCntl}; +use esp_println::println; +use esp_backtrace as _; +use xtensa_atomic_emulation_trap as _; +use xtensa_lx_rt::entry; + +use esp32s2_hal::{gpio}; +use esp32s2_hal::spi; +use esp32s2_hal::prelude::*; + +#[entry] +fn main() -> ! { + let peripherals = Peripherals::take().unwrap(); + let mut system = peripherals.SYSTEM.split(); + let mut clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + + // Disable the RTC and TIMG watchdog timers + let mut rtc_cntl = RtcCntl::new(peripherals.RTC_CNTL); + let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); + let mut wdt0 = timer_group0.wdt; + let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks); + let mut wdt1 = timer_group1.wdt; + + rtc_cntl.set_wdt_global_enable(false); + wdt0.disable(); + wdt1.disable(); + + + println!("About to initialize the ESP32-S2/S3-USB-OTG SPI LED driver ST7789VW"); + use esp32s2_hal::IO; + // let mut system = peripherals.SYSTEM.split(); + // let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); + let backlight= io.pins.gpio9; + let mut backlight = backlight.into_push_pull_output(); + backlight.set_high(); + use embedded_graphics::draw_target::DrawTarget; + + +// dc: gpio::Gpio4, +// rst: gpio::Gpio8, +// peripheral_spi: spi::Spi, +// sclk: gpio::Gpio6, +// mosi: gpio::Gpio7, +// cs: gpio::Gpio5, +// peripheral_clock_control:ClockControl, +let mosi = io.pins.gpio7; +let cs = io.pins.gpio5; +let rst = io.pins.gpio8; +let dc = io.pins.gpio4; + // let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); + let mut spi = spi::Spi::new( + peripherals.SPI3, + io.pins.gpio6, + mosi, + Some(io.pins.gpio12), + Some(cs), + 80u32.kHz(), + spi::SpiMode::Mode0, + &mut system.peripheral_clock_control, + &mut clocks, + ); + use display_interface_spi::SPIInterfaceNoCS; + let di = SPIInterfaceNoCS::new(spi, dc.into_push_pull_output()); + // let di = SPIInterfaceNoCS::new( + // spi::Master::::new( + // spi, + // spi::Pins { + // sclk, + // sdo, + // sdi: Option::>::None, + // cs: Some(cs), + // }, + // config, + // )?, + // dc.into_output()?, + // ); + + let reset = rst.into_push_pull_output(); + + let mut display = st7789::ST7789::new(di, reset, 240, 240); + use esp32s2_hal::{Delay}; + let mut delay = Delay::new(&clocks); + display.init(&mut delay).unwrap(); + // .map_err(|err| error!("{:?}", err)) + // .err(); + display + .set_orientation(st7789::Orientation::Landscape); + // .map_err(|err| error!("{:?}", err)) + // .ok(); + display.clear(RgbColor::WHITE); + use embedded_graphics::{text::Text, mono_font::{ + ascii::{FONT_6X10, FONT_9X18_BOLD}, + MonoTextStyleBuilder, + }, }; + use embedded_graphics::mono_font::{ascii::FONT_8X13, MonoTextStyle}; + use embedded_graphics::prelude::Point; + use embedded_graphics::Drawable; + Text::new( + "Bare Metal is for everyone! ", + Point::new(10, 110), + MonoTextStyle::new(&FONT_8X13, RgbColor::RED) + ) + .draw(&mut display).unwrap(); + + + Text::new( + "160-165 BPM", + Point::new(100, 140), + MonoTextStyle::new(&FONT_8X13, RgbColor::GREEN) + ) + .draw(&mut display).unwrap(); + println!("Initialized"); + + loop {} +} From afe65994887cc8bf00a1aaeea6e817e0a0ac22c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Mich=C3=A1lek?= Date: Mon, 25 Jul 2022 14:18:08 +0200 Subject: [PATCH 2/4] add display example for esp32s2 --- esp32s2-hal/.cargo/config.toml | 21 ++++- esp32s2-hal/Cargo.toml | 64 +++++++------ esp32s2-hal/examples/spi_display.rs | 141 ++++++++++++---------------- 3 files changed, 111 insertions(+), 115 deletions(-) diff --git a/esp32s2-hal/.cargo/config.toml b/esp32s2-hal/.cargo/config.toml index 6518c11d6a9..a2bb2ecf9eb 100644 --- a/esp32s2-hal/.cargo/config.toml +++ b/esp32s2-hal/.cargo/config.toml @@ -3,8 +3,25 @@ runner = "espflash --monitor" [build] rustflags = [ - "-C", "link-arg=-nostartfiles", - "-C", "link-arg=-Wl,-Tlinkall.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"', + + "-C", + "link-arg=-nostartfiles", + "-C", + "link-arg=-Wl,-Tlinkall.x", ] target = "xtensa-esp32s2-none-elf" diff --git a/esp32s2-hal/Cargo.toml b/esp32s2-hal/Cargo.toml index 8b71e9ec0fb..7d9fa7b156c 100644 --- a/esp32s2-hal/Cargo.toml +++ b/esp32s2-hal/Cargo.toml @@ -1,53 +1,55 @@ [package] -name = "esp32s2-hal" +name = "esp32s2-hal" version = "0.1.0" authors = [ "Jesse Braham ", "Björn Quentin ", ] -edition = "2021" +edition = "2021" description = "HAL for ESP32-S2 microcontrollers" -repository = "https://github.com/esp-rs/esp-hal" -license = "MIT OR Apache-2.0" +repository = "https://github.com/esp-rs/esp-hal" +license = "MIT OR Apache-2.0" -keywords = [ - "embedded", - "embedded-hal", - "esp", - "esp32s2", - "no-std", -] -categories = [ - "embedded", - "hardware-support", - "no-std", -] +keywords = ["embedded", "embedded-hal", "esp", "esp32s2", "no-std"] +categories = ["embedded", "hardware-support", "no-std"] [dependencies] -bare-metal = "1.0" -embedded-hal = { version = "0.2", features = ["unproven"] } +bare-metal = "1.0" +embedded-hal = { version = "0.2", features = ["unproven"] } embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.8" } -xtensa-lx = { version = "0.7", features = ["esp32s2"] } -xtensa-lx-rt = { version = "0.13", features = ["esp32s2"], optional = true } +xtensa-lx = { version = "0.7", features = ["esp32s2"] } +xtensa-lx-rt = { version = "0.13", features = ["esp32s2"], optional = true } + +# ESP32-S2 SPI Example +st7789 = "0.6" +display-interface = "0.4" +display-interface-spi = "0.4" +esp-backtrace = { git = "https://github.com/esp-rs/esp-backtrace", features = [ + "esp32s2", + "panic-handler", + "print-uart", +] } +xtensa-atomic-emulation-trap = "0.1.0" +embedded-graphics = "0.7" [dependencies.esp-hal-common] -path = "../esp-hal-common" +path = "../esp-hal-common" features = ["esp32s2"] [dev-dependencies] embedded-graphics = "0.7" -panic-halt = "0.2" -ssd1306 = "0.7" -smart-leds = "0.3" -esp-println = { version = "0.2.0", features = ["esp32s2"] } +panic-halt = "0.2" +ssd1306 = "0.7" +smart-leds = "0.3" +esp-println = { version = "0.2.0", features = ["esp32s2"] } [features] -default = ["rt"] -eh1 = ["esp-hal-common/eh1"] -rt = ["xtensa-lx-rt/esp32s2"] -smartled = ["esp-hal-common/smartled"] -ufmt = ["esp-hal-common/ufmt"] +default = ["rt"] +eh1 = ["esp-hal-common/eh1"] +rt = ["xtensa-lx-rt/esp32s2"] +smartled = ["esp-hal-common/smartled"] +ufmt = ["esp-hal-common/ufmt"] [[example]] -name = "hello_rgb" +name = "hello_rgb" required-features = ["smartled"] diff --git a/esp32s2-hal/examples/spi_display.rs b/esp32s2-hal/examples/spi_display.rs index a9ef6a261d0..6a582d62c3c 100644 --- a/esp32s2-hal/examples/spi_display.rs +++ b/esp32s2-hal/examples/spi_display.rs @@ -1,17 +1,23 @@ #![no_std] #![no_main] -use embedded_graphics::prelude::RgbColor; -use esp32s2_hal::{clock::ClockControl, pac::Peripherals, prelude::*, timer::TimerGroup, RtcCntl}; +use embedded_graphics::{draw_target::DrawTarget, prelude::RgbColor}; +use esp32s2_hal::{ + clock::ClockControl, + gpio, + pac::Peripherals, + prelude::*, + spi, + timer::TimerGroup, + RtcCntl, + IO, +}; +// use esp_backtrace as _; use esp_println::println; -use esp_backtrace as _; -use xtensa_atomic_emulation_trap as _; +use panic_halt as _; +// use xtensa_atomic_emulation_trap as _; use xtensa_lx_rt::entry; -use esp32s2_hal::{gpio}; -use esp32s2_hal::spi; -use esp32s2_hal::prelude::*; - #[entry] fn main() -> ! { let peripherals = Peripherals::take().unwrap(); @@ -29,92 +35,63 @@ fn main() -> ! { wdt0.disable(); wdt1.disable(); - - println!("About to initialize the ESP32-S2/S3-USB-OTG SPI LED driver ST7789VW"); - use esp32s2_hal::IO; - // let mut system = peripherals.SYSTEM.split(); - // let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + println!("About to initialize the SPI LED driver ST7789VW"); let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - let backlight= io.pins.gpio9; + let backlight = io.pins.gpio9; let mut backlight = backlight.into_push_pull_output(); backlight.set_high(); - use embedded_graphics::draw_target::DrawTarget; + let mosi = io.pins.gpio7; + let cs = io.pins.gpio5; + let rst = io.pins.gpio8; + let dc = io.pins.gpio4; -// dc: gpio::Gpio4, -// rst: gpio::Gpio8, -// peripheral_spi: spi::Spi, -// sclk: gpio::Gpio6, -// mosi: gpio::Gpio7, -// cs: gpio::Gpio5, -// peripheral_clock_control:ClockControl, -let mosi = io.pins.gpio7; -let cs = io.pins.gpio5; -let rst = io.pins.gpio8; -let dc = io.pins.gpio4; - // let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - let mut spi = spi::Spi::new( - peripherals.SPI3, - io.pins.gpio6, - mosi, - Some(io.pins.gpio12), - Some(cs), - 80u32.kHz(), - spi::SpiMode::Mode0, - &mut system.peripheral_clock_control, - &mut clocks, - ); - use display_interface_spi::SPIInterfaceNoCS; - let di = SPIInterfaceNoCS::new(spi, dc.into_push_pull_output()); - // let di = SPIInterfaceNoCS::new( - // spi::Master::::new( - // spi, - // spi::Pins { - // sclk, - // sdo, - // sdi: Option::>::None, - // cs: Some(cs), - // }, - // config, - // )?, - // dc.into_output()?, - // ); + let mut spi = spi::Spi::new( + peripherals.SPI3, + io.pins.gpio6, + mosi, + Some(io.pins.gpio12), + Some(cs), + 80u32.kHz(), + spi::SpiMode::Mode0, + &mut system.peripheral_clock_control, + &mut clocks, + ); + use display_interface_spi::SPIInterfaceNoCS; + let di = SPIInterfaceNoCS::new(spi, dc.into_push_pull_output()); let reset = rst.into_push_pull_output(); let mut display = st7789::ST7789::new(di, reset, 240, 240); - use esp32s2_hal::{Delay}; + use esp32s2_hal::Delay; let mut delay = Delay::new(&clocks); display.init(&mut delay).unwrap(); - // .map_err(|err| error!("{:?}", err)) - // .err(); - display - .set_orientation(st7789::Orientation::Landscape); - // .map_err(|err| error!("{:?}", err)) - // .ok(); - display.clear(RgbColor::WHITE); - use embedded_graphics::{text::Text, mono_font::{ - ascii::{FONT_6X10, FONT_9X18_BOLD}, - MonoTextStyleBuilder, - }, }; - use embedded_graphics::mono_font::{ascii::FONT_8X13, MonoTextStyle}; - use embedded_graphics::prelude::Point; - use embedded_graphics::Drawable; - Text::new( - "Bare Metal is for everyone! ", - Point::new(10, 110), - MonoTextStyle::new(&FONT_8X13, RgbColor::RED) - ) - .draw(&mut display).unwrap(); + // .map_err(|err| error!("{:?}", err)) + // .err(); + display.set_orientation(st7789::Orientation::Landscape); + // .map_err(|err| error!("{:?}", err)) + // .ok(); + display.clear(RgbColor::WHITE); + println!("Initialized"); + + use embedded_graphics::{ + mono_font::{ + ascii::{FONT_6X10, FONT_8X13, FONT_9X18_BOLD}, + MonoTextStyle, + MonoTextStyleBuilder, + }, + prelude::Point, + text::Text, + Drawable, + }; - - Text::new( - "160-165 BPM", - Point::new(100, 140), - MonoTextStyle::new(&FONT_8X13, RgbColor::GREEN) + Text::new( + "Hello from ESP-RS! ", + Point::new(10, 110), + MonoTextStyle::new(&FONT_8X13, RgbColor::BLACK), ) - .draw(&mut display).unwrap(); - println!("Initialized"); + .draw(&mut display) + .unwrap(); - loop {} + loop {} } From 17d8b52852bdc6a4eb45f538b5dd8b112e4476fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Mich=C3=A1lek?= Date: Mon, 25 Jul 2022 16:35:25 +0200 Subject: [PATCH 3/4] clean up warnings --- esp32s2-hal/examples/spi_display.rs | 60 ++++++++++++++--------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/esp32s2-hal/examples/spi_display.rs b/esp32s2-hal/examples/spi_display.rs index 6a582d62c3c..c907fa6b5cd 100644 --- a/esp32s2-hal/examples/spi_display.rs +++ b/esp32s2-hal/examples/spi_display.rs @@ -1,21 +1,30 @@ #![no_std] #![no_main] -use embedded_graphics::{draw_target::DrawTarget, prelude::RgbColor}; +use display_interface_spi::SPIInterfaceNoCS; +use embedded_graphics::{ + draw_target::DrawTarget, + prelude::RgbColor, + mono_font::{ + ascii::{FONT_8X13, FONT_9X18_BOLD}, + MonoTextStyle, + }, + prelude::Point, + text::Text, + Drawable, +}; +use esp_println::println; use esp32s2_hal::{ clock::ClockControl, - gpio, pac::Peripherals, prelude::*, spi, timer::TimerGroup, RtcCntl, IO, + Delay }; -// use esp_backtrace as _; -use esp_println::println; use panic_halt as _; -// use xtensa_atomic_emulation_trap as _; use xtensa_lx_rt::entry; #[entry] @@ -39,14 +48,14 @@ fn main() -> ! { let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); let backlight = io.pins.gpio9; let mut backlight = backlight.into_push_pull_output(); - backlight.set_high(); + backlight.set_high().unwrap(); let mosi = io.pins.gpio7; let cs = io.pins.gpio5; let rst = io.pins.gpio8; let dc = io.pins.gpio4; - let mut spi = spi::Spi::new( + let spi = spi::Spi::new( peripherals.SPI3, io.pins.gpio6, mosi, @@ -57,41 +66,32 @@ fn main() -> ! { &mut system.peripheral_clock_control, &mut clocks, ); - use display_interface_spi::SPIInterfaceNoCS; - let di = SPIInterfaceNoCS::new(spi, dc.into_push_pull_output()); + let di = SPIInterfaceNoCS::new(spi, dc.into_push_pull_output()); let reset = rst.into_push_pull_output(); - let mut display = st7789::ST7789::new(di, reset, 240, 240); - use esp32s2_hal::Delay; let mut delay = Delay::new(&clocks); + display.init(&mut delay).unwrap(); - // .map_err(|err| error!("{:?}", err)) - // .err(); - display.set_orientation(st7789::Orientation::Landscape); - // .map_err(|err| error!("{:?}", err)) - // .ok(); - display.clear(RgbColor::WHITE); + display.set_orientation(st7789::Orientation::Landscape).unwrap(); + display.clear(RgbColor::WHITE).unwrap(); println!("Initialized"); - use embedded_graphics::{ - mono_font::{ - ascii::{FONT_6X10, FONT_8X13, FONT_9X18_BOLD}, - MonoTextStyle, - MonoTextStyleBuilder, - }, - prelude::Point, - text::Text, - Drawable, - }; - Text::new( - "Hello from ESP-RS! ", - Point::new(10, 110), + "Hello from", + Point::new(80, 110), MonoTextStyle::new(&FONT_8X13, RgbColor::BLACK), ) .draw(&mut display) .unwrap(); + Text::new( + "ESP-RS", + Point::new(90, 140), + MonoTextStyle::new(&FONT_9X18_BOLD, RgbColor::RED), + ) + .draw(&mut display) + .unwrap(); + loop {} } From 547adce9be247d28f3c8c669708e40df876ffc4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Mich=C3=A1lek?= Date: Mon, 25 Jul 2022 17:14:23 +0200 Subject: [PATCH 4/4] clean up --- esp32s2-hal/examples/spi_display.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/esp32s2-hal/examples/spi_display.rs b/esp32s2-hal/examples/spi_display.rs index c907fa6b5cd..037a93dc9af 100644 --- a/esp32s2-hal/examples/spi_display.rs +++ b/esp32s2-hal/examples/spi_display.rs @@ -54,12 +54,14 @@ fn main() -> ! { let cs = io.pins.gpio5; let rst = io.pins.gpio8; let dc = io.pins.gpio4; + let sck = io.pins.gpio6; + let miso = io.pins.gpio12; let spi = spi::Spi::new( peripherals.SPI3, - io.pins.gpio6, + sck, mosi, - Some(io.pins.gpio12), + Some(miso), Some(cs), 80u32.kHz(), spi::SpiMode::Mode0,