Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

Add a basic example which blinks an LED #10

Merged
merged 2 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[target.xtensa-esp32-none-elf]
runner = "xtensa-esp32-elf-gdb -q -x xtensa.gdb"

[build]
rustflags = [
"-C", "link-arg=-nostartfiles",
"-C", "debuginfo=0", # not 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer required, the new llvm fork supports debug info.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for clarification, am I removing the entire rustflags array, its first 2 elements, or just the debuginfo line?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MabezDev Which exact llvm fork are you using? If I use https://github.com/espressif/llvm-project/tree/xtensa_release_9.0.1 I still get CFI not supported errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arjanmels I'm using that fork; it should work. Note that if you're trying to compile .S assembly files, it will be passed to the gcc assembler at the moment, which will produce these errors. Inline assembly works though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange, not working for me. I recompiled llvm and rust, but no luck, must be missing something.

This is what I get if I check my version (afraid it may not be super helpfull:

$RUSTC --version --verbose
rustc 1.44.0-dev
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.44.0-dev
LLVM version: 9.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is weird, so you can't create debug info at all? I've defintely got it working on my machine: espressif/llvm-project#10 (comment)

Ive been pretty tied up this week, but today I've been revisiting the rust fork, looking to improve the ABI calls, and use the built in llvm module, which might fix your issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No debug info not working for me. If I understand your comment for espressif/llvm-project#10 (comment) correct you are working with a local rebase, so theoretically could be something different?

More likely that I did something wrong perhaps, but I don't know what. Anyway I am managing ok without the debugger at the moment as I have the serial output up and running, but debugger would be nice of course.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That rebase was temporary, the current default branch of the fork has the correct changes in it.

"-C", "link-arg=-Wl,-Tlink.x",
]
target = "xtensa-esp32-none-elf"
19 changes: 19 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,22 @@ esp32 = { version = "0.2.2" }
[dependencies.embedded-hal]
features = ["unproven"]
version = "0.2.3"

[dev-dependencies]
panic-halt = "0.2.0"
xtensa-lx6-rt = { git = "https://github.com/esp-rs/xtensa-lx6-rt", rev = "89cea20c441d4a1624de99c7278df13af5c211a4" }

[[example]]
name = "blinky"

[profile.dev]
lto = true
incremental = false
debug = false # debug adds frame pointers - which must be omitted
codegen-units = 1

[profile.release]
lto = true
incremental = false
debug = false # debug adds frame pointers - which must be omitted
codegen-units = 1
18 changes: 18 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

fn main() {
// Put the linker script somewhere the linker can find it
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("memory.x"))
.unwrap()
.write_all(include_bytes!("memory.x"))
.unwrap();
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");
}
94 changes: 94 additions & 0 deletions examples/blinky.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#![no_std]
#![no_main]
#![feature(asm)]

extern crate esp32_hal as hal;
extern crate panic_halt;
extern crate xtensa_lx6_rt;

use hal::prelude::*;

/// The default clock source is the onboard crystal
/// In most cases 40mhz (but can be as low as 2mhz depending on the board)
const CORE_HZ: u32 = 40_000_000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't intend to change this but to keep this as a side note, in the PR #7 this can be calculated at run time depending on the clock source used.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out. I'm fine with waiting until that PR has merged and updating the example if that's preferable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe merge this PR first because #7 has been superseded by #11


const WDT_WKEY_VALUE: u32 = 0x50D83AA1;

#[no_mangle]
fn main() -> ! {
let dp = unsafe { hal::pac::Peripherals::steal() };

let mut rtccntl = dp.RTCCNTL;
let mut timg0 = dp.TIMG0;
let mut timg1 = dp.TIMG1;

// (https://github.com/espressif/openocd-esp32/blob/97ba3a6bb9eaa898d91df923bbedddfeaaaf28c9/src/target/esp32.c#L431)
// openocd disables the wdt's on halt
// we will do it manually on startup
disable_timg_wdts(&mut timg0, &mut timg1);
disable_rtc_wdt(&mut rtccntl);

let pins = dp.GPIO.split();
let mut led = pins.gpio2.into_open_drain_output();

loop {
led.set_high().unwrap();
delay(CORE_HZ);
led.set_low().unwrap();
delay(CORE_HZ);
}
}

fn disable_rtc_wdt(rtccntl: &mut hal::pac::RTCCNTL) {
/* Disables the RTCWDT */
rtccntl
.wdtwprotect
.write(|w| unsafe { w.bits(WDT_WKEY_VALUE) });
rtccntl.wdtconfig0.modify(|_, w| unsafe {
w.wdt_stg0()
.bits(0x0)
.wdt_stg1()
.bits(0x0)
.wdt_stg2()
.bits(0x0)
.wdt_stg3()
.bits(0x0)
.wdt_flashboot_mod_en()
.clear_bit()
.wdt_en()
.clear_bit()
});
rtccntl.wdtwprotect.write(|w| unsafe { w.bits(0x0) });
}

fn disable_timg_wdts(timg0: &mut hal::pac::TIMG0, timg1: &mut hal::pac::TIMG1) {
timg0
.wdtwprotect
.write(|w| unsafe { w.bits(WDT_WKEY_VALUE) });
timg1
.wdtwprotect
.write(|w| unsafe { w.bits(WDT_WKEY_VALUE) });

timg0.wdtconfig0.write(|w| unsafe { w.bits(0x0) });
timg1.wdtconfig0.write(|w| unsafe { w.bits(0x0) });
}

/// cycle accurate delay using the cycle counter register
pub fn delay(clocks: u32) {
// NOTE: does not account for rollover
let target = get_ccount() + clocks;
loop {
if get_ccount() > target {
break;
}
}
}

/// Performs a special register read to read the current cycle count.
/// In the future, this can be precompiled to a archive (.a) and linked to so we don't
/// have to require the asm nightly feature - see cortex-m-rt for more details
pub fn get_ccount() -> u32 {
let x: u32;
unsafe { asm!("rsr.ccount a2" : "={a2}"(x) ) };
x
}
10 changes: 10 additions & 0 deletions memory.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* Specify main memory areas */
MEMORY
{
/* Use values from the ESP-IDF 'bootloader' component.
/* TODO: Use human-readable lengths */
/* TODO: Use the full memory map - this is just a test */
/* vectors ( RX ) : ORIGIN = 0x40080000, len = 0x400 */
iram_seg ( RX ) : ORIGIN = 0x40080400, len = 0xFC00
dram_seg ( RW ) : ORIGIN = 0x3FFF0000, len = 0x1000
}