Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

WIP: Add blinky example #1

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[target.xtensa-esp32-none-elf]
runner = "xtensa-esp32-elf-gdb -q -x xtensa.gdb"

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

[dev-dependencies]
xtensa-lx6-rt = { git = "https://github.com/esp-rs/xtensa-lx6-rt", rev = "89cea20c441d4a1624de99c7278df13af5c211a4" }
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");
}
66 changes: 66 additions & 0 deletions examples/blinky.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#![no_std]
#![no_main]
#![feature(asm)]

use xtensa_lx6_rt as _;

use core::panic::PanicInfo;
use esp8266_hal as hal;
use esp8266_hal::ehal::digital::v2::OutputPin;
use esp8266_hal::gpio::GpioExt;

/// 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;

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

// (https://github.com/espressif/openocd-esp8266/blob/97ba3a6bb9eaa898d91df923bbedddfeaaaf28c9/src/target/esp8266.c#L431)
// openocd disables the wdt's on halt
// we will do it manually on startup
let mut timg = dp.TIMER;
disable_timg_wdts(&mut timg);

let gpios = dp.GPIO.split();
let mut led_pin = gpios.gpio2.into_open_drain_output();
loop {
led_pin.set_high().unwrap();
delay(CORE_HZ);
led_pin.set_low().unwrap();
delay(CORE_HZ);
}
}

fn disable_timg_wdts(timg: &mut esp8266::TIMER) {
timg.frc1_ctrl.write(|w| unsafe { w.bits(0x80) });
timg.frc2_ctrl.write(|w| unsafe{ w.bits(0x80) });
}

/// 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
}


/// Basic panic handler - just loops
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
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
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub use embedded_hal as ehal;
pub mod gpio;
pub mod timer;
pub mod uart;
pub use esp8266 as pac;