Begin adding some working examples
This commit is contained in:
parent
3019b78d6a
commit
140333ff86
@ -57,10 +57,6 @@ smartled = ["esp-hal-common/smartled"]
|
||||
ufmt = ["esp-hal-common/ufmt"]
|
||||
vectored = ["esp-hal-common/vectored"]
|
||||
|
||||
# [[example]]
|
||||
# name = "hello_rgb"
|
||||
# required-features = ["smartled"]
|
||||
|
||||
# [[example]]
|
||||
# name = "spi_eh1_loopback"
|
||||
# required-features = ["eh1"]
|
||||
[[example]]
|
||||
name = "spi_eh1_loopback"
|
||||
required-features = ["eh1"]
|
||||
|
||||
72
esp32c2-hal/examples/advanced_serial.rs
Normal file
72
esp32c2-hal/examples/advanced_serial.rs
Normal file
@ -0,0 +1,72 @@
|
||||
//! This shows how to configure UART
|
||||
//! You can short the TX and RX pin and see it reads what was written.
|
||||
//! Additionally you can connect a logic analzyer to TX and see how the changes
|
||||
//! of the configuration change the output signal.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use esp32c2_hal::{
|
||||
clock::ClockControl,
|
||||
pac::Peripherals,
|
||||
prelude::*,
|
||||
serial::{
|
||||
config::{Config, DataBits, Parity, StopBits},
|
||||
TxRxPins,
|
||||
},
|
||||
timer::TimerGroup,
|
||||
// Rtc,
|
||||
Serial,
|
||||
IO,
|
||||
};
|
||||
use esp_backtrace as _;
|
||||
use esp_println::println;
|
||||
use nb::block;
|
||||
use riscv_rt::entry;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
let peripherals = Peripherals::take().unwrap();
|
||||
let system = peripherals.SYSTEM.split();
|
||||
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
|
||||
|
||||
// let mut rtc = Rtc::new(peripherals.RTC_CNTL);
|
||||
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
|
||||
let mut timer0 = timer_group0.timer0;
|
||||
let mut wdt0 = timer_group0.wdt;
|
||||
|
||||
// Disable watchdog timers
|
||||
// rtc.swd.disable();
|
||||
// rtc.rwdt.disable();
|
||||
wdt0.disable();
|
||||
|
||||
let config = Config {
|
||||
baudrate: 115200,
|
||||
data_bits: DataBits::DataBits8,
|
||||
parity: Parity::ParityNone,
|
||||
stop_bits: StopBits::STOP1,
|
||||
};
|
||||
|
||||
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
|
||||
let pins = TxRxPins::new_tx_rx(
|
||||
io.pins.gpio1.into_push_pull_output(),
|
||||
io.pins.gpio2.into_floating_input(),
|
||||
);
|
||||
|
||||
let mut serial1 = Serial::new_with_config(peripherals.UART1, Some(config), Some(pins), &clocks);
|
||||
|
||||
timer0.start(250u64.millis());
|
||||
|
||||
println!("Start");
|
||||
loop {
|
||||
serial1.write(0x42).ok();
|
||||
let read = block!(serial1.read());
|
||||
|
||||
match read {
|
||||
Ok(read) => println!("Read {:02x}", read),
|
||||
Err(err) => println!("Error {:?}", err),
|
||||
}
|
||||
|
||||
block!(timer0.wait()).unwrap();
|
||||
}
|
||||
}
|
||||
51
esp32c2-hal/examples/blinky.rs
Normal file
51
esp32c2-hal/examples/blinky.rs
Normal file
@ -0,0 +1,51 @@
|
||||
//! Blinks an LED
|
||||
//!
|
||||
//! This assumes that a LED is connected to the pin assigned to `led`. (GPIO5)
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use esp32c2_hal::{
|
||||
clock::ClockControl,
|
||||
gpio::IO,
|
||||
pac::Peripherals,
|
||||
prelude::*,
|
||||
system::SystemExt,
|
||||
timer::TimerGroup,
|
||||
Delay,
|
||||
// Rtc,
|
||||
};
|
||||
use esp_backtrace as _;
|
||||
use riscv_rt::entry;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
let peripherals = Peripherals::take().unwrap();
|
||||
let system = peripherals.SYSTEM.split();
|
||||
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
|
||||
|
||||
// Disable the watchdog timers. For the ESP32-C2, this includes the Super WDT,
|
||||
// the RTC WDT, and the TIMG WDTs.
|
||||
// let mut rtc = Rtc::new(peripherals.RTC_CNTL);
|
||||
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
|
||||
let mut wdt0 = timer_group0.wdt;
|
||||
|
||||
// rtc.swd.disable();
|
||||
// rtc.rwdt.disable();
|
||||
wdt0.disable();
|
||||
|
||||
// Set GPIO5 as an output, and set its state high initially.
|
||||
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
|
||||
let mut led = io.pins.gpio5.into_push_pull_output();
|
||||
|
||||
led.set_high().unwrap();
|
||||
|
||||
// Initialize the Delay peripheral, and use it to toggle the LED state in a
|
||||
// loop.
|
||||
let mut delay = Delay::new(&clocks);
|
||||
|
||||
loop {
|
||||
led.toggle().unwrap();
|
||||
delay.delay_ms(500u32);
|
||||
}
|
||||
}
|
||||
@ -4,11 +4,41 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use esp32c2_hal as _;
|
||||
use core::fmt::Write;
|
||||
|
||||
use esp32c2_hal::{
|
||||
clock::ClockControl,
|
||||
pac::Peripherals,
|
||||
prelude::*,
|
||||
timer::TimerGroup,
|
||||
// Rtc,
|
||||
Serial,
|
||||
};
|
||||
use esp_backtrace as _;
|
||||
use nb::block;
|
||||
use riscv_rt::entry;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
loop {}
|
||||
let peripherals = Peripherals::take().unwrap();
|
||||
let system = peripherals.SYSTEM.split();
|
||||
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
|
||||
|
||||
// let mut rtc = Rtc::new(peripherals.RTC_CNTL);
|
||||
let mut serial0 = Serial::new(peripherals.UART0);
|
||||
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
|
||||
let mut timer0 = timer_group0.timer0;
|
||||
let mut wdt0 = timer_group0.wdt;
|
||||
|
||||
// Disable watchdog timers
|
||||
// rtc.swd.disable();
|
||||
// rtc.rwdt.disable();
|
||||
wdt0.disable();
|
||||
|
||||
timer0.start(1u64.secs());
|
||||
|
||||
loop {
|
||||
writeln!(serial0, "Hello world!").unwrap();
|
||||
block!(timer0.wait()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
126
esp32c2-hal/examples/spi_eh1_loopback.rs
Normal file
126
esp32c2-hal/examples/spi_eh1_loopback.rs
Normal file
@ -0,0 +1,126 @@
|
||||
//! SPI loopback test
|
||||
//!
|
||||
//! Folowing pins are used:
|
||||
//! SCLK GPIO6
|
||||
//! MISO GPIO2
|
||||
//! MOSI GPIO7
|
||||
//! CS GPIO10
|
||||
//!
|
||||
//! Depending on your target and the board you are using you have to change the
|
||||
//! pins.
|
||||
//!
|
||||
//! This example transfers data via SPI.
|
||||
//! Connect MISO and MOSI pins to see the outgoing data is read as incoming
|
||||
//! data.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use core::fmt::Write;
|
||||
|
||||
use embedded_hal_1::spi::blocking::SpiBus;
|
||||
use esp32c2_hal::{
|
||||
clock::ClockControl,
|
||||
gpio::IO,
|
||||
pac::Peripherals,
|
||||
prelude::*,
|
||||
spi::{Spi, SpiMode},
|
||||
timer::TimerGroup,
|
||||
Delay,
|
||||
// Rtc,
|
||||
Serial,
|
||||
};
|
||||
use esp_backtrace as _;
|
||||
use riscv_rt::entry;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
let peripherals = Peripherals::take().unwrap();
|
||||
let mut system = peripherals.SYSTEM.split();
|
||||
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
|
||||
|
||||
// Disable the watchdog timers. For the ESP32-C2, this includes the Super WDT,
|
||||
// the RTC WDT, and the TIMG WDT.
|
||||
// let mut rtc = Rtc::new(peripherals.RTC_CNTL);
|
||||
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
|
||||
let mut wdt0 = timer_group0.wdt;
|
||||
|
||||
let mut serial0 = Serial::new(peripherals.UART0);
|
||||
|
||||
// rtc.swd.disable();
|
||||
// rtc.rwdt.disable();
|
||||
wdt0.disable();
|
||||
|
||||
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
|
||||
let sclk = io.pins.gpio6;
|
||||
let miso = io.pins.gpio2;
|
||||
let mosi = io.pins.gpio7;
|
||||
let cs = io.pins.gpio10;
|
||||
|
||||
let mut spi = Spi::new(
|
||||
peripherals.SPI2,
|
||||
sclk,
|
||||
mosi,
|
||||
miso,
|
||||
cs,
|
||||
1000u32.kHz(),
|
||||
SpiMode::Mode0,
|
||||
&mut system.peripheral_clock_control,
|
||||
&clocks,
|
||||
);
|
||||
|
||||
let mut delay = Delay::new(&clocks);
|
||||
writeln!(serial0, "=== SPI example with embedded-hal-1 traits ===").unwrap();
|
||||
|
||||
loop {
|
||||
// --- Symmetric transfer (Read as much as we write) ---
|
||||
write!(serial0, "Starting symmetric transfer...").unwrap();
|
||||
let write = [0xde, 0xad, 0xbe, 0xef];
|
||||
let mut read: [u8; 4] = [0x00u8; 4];
|
||||
|
||||
SpiBus::transfer(&mut spi, &mut read[..], &write[..]).expect("Symmetric transfer failed");
|
||||
assert_eq!(write, read);
|
||||
writeln!(serial0, " SUCCESS").unwrap();
|
||||
delay.delay_ms(250u32);
|
||||
|
||||
// --- Asymmetric transfer (Read more than we write) ---
|
||||
write!(serial0, "Starting asymetric transfer (read > write)...").unwrap();
|
||||
let mut read: [u8; 4] = [0x00; 4];
|
||||
|
||||
SpiBus::transfer(&mut spi, &mut read[0..2], &write[..])
|
||||
.expect("Asymmetric transfer failed");
|
||||
assert_eq!(write[0], read[0]);
|
||||
assert_eq!(read[2], 0x00u8);
|
||||
writeln!(serial0, " SUCCESS").unwrap();
|
||||
delay.delay_ms(250u32);
|
||||
|
||||
// --- Symmetric transfer with huge buffer ---
|
||||
// Only your RAM is the limit!
|
||||
write!(serial0, "Starting huge transfer...").unwrap();
|
||||
let mut write = [0x55u8; 4096];
|
||||
for byte in 0..write.len() {
|
||||
write[byte] = byte as u8;
|
||||
}
|
||||
let mut read = [0x00u8; 4096];
|
||||
|
||||
SpiBus::transfer(&mut spi, &mut read[..], &write[..]).expect("Huge transfer failed");
|
||||
assert_eq!(write, read);
|
||||
writeln!(serial0, " SUCCESS").unwrap();
|
||||
delay.delay_ms(250u32);
|
||||
|
||||
// --- Symmetric transfer with huge buffer in-place (No additional allocation
|
||||
// needed) ---
|
||||
write!(serial0, "Starting huge transfer (in-place)...").unwrap();
|
||||
let mut write = [0x55u8; 4096];
|
||||
for byte in 0..write.len() {
|
||||
write[byte] = byte as u8;
|
||||
}
|
||||
|
||||
SpiBus::transfer_in_place(&mut spi, &mut write[..]).expect("Huge transfer failed");
|
||||
for byte in 0..write.len() {
|
||||
assert_eq!(write[byte], byte as u8);
|
||||
}
|
||||
writeln!(serial0, " SUCCESS").unwrap();
|
||||
delay.delay_ms(250u32);
|
||||
}
|
||||
}
|
||||
80
esp32c2-hal/examples/spi_loopback.rs
Normal file
80
esp32c2-hal/examples/spi_loopback.rs
Normal file
@ -0,0 +1,80 @@
|
||||
//! SPI loopback test
|
||||
//!
|
||||
//! Folowing pins are used:
|
||||
//! SCLK GPIO6
|
||||
//! MISO GPIO2
|
||||
//! MOSI GPIO7
|
||||
//! CS GPIO10
|
||||
//!
|
||||
//! Depending on your target and the board you are using you have to change the
|
||||
//! pins.
|
||||
//!
|
||||
//! This example transfers data via SPI.
|
||||
//! Connect MISO and MOSI pins to see the outgoing data is read as incoming
|
||||
//! data.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use core::fmt::Write;
|
||||
|
||||
use esp32c2_hal::{
|
||||
clock::ClockControl,
|
||||
gpio::IO,
|
||||
pac::Peripherals,
|
||||
prelude::*,
|
||||
spi::{Spi, SpiMode},
|
||||
timer::TimerGroup,
|
||||
Delay,
|
||||
// Rtc,
|
||||
Serial,
|
||||
};
|
||||
use esp_backtrace as _;
|
||||
use riscv_rt::entry;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
let peripherals = Peripherals::take().unwrap();
|
||||
let mut system = peripherals.SYSTEM.split();
|
||||
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
|
||||
|
||||
// Disable the watchdog timers. For the ESP32-C2, this includes the Super WDT,
|
||||
// the RTC WDT, and the TIMG WDT.
|
||||
// let mut rtc = Rtc::new(peripherals.RTC_CNTL);
|
||||
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
|
||||
let mut wdt0 = timer_group0.wdt;
|
||||
|
||||
let mut serial0 = Serial::new(peripherals.UART0);
|
||||
|
||||
// rtc.swd.disable();
|
||||
// rtc.rwdt.disable();
|
||||
wdt0.disable();
|
||||
|
||||
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
|
||||
let sclk = io.pins.gpio6;
|
||||
let miso = io.pins.gpio2;
|
||||
let mosi = io.pins.gpio7;
|
||||
let cs = io.pins.gpio10;
|
||||
|
||||
let mut spi = Spi::new(
|
||||
peripherals.SPI2,
|
||||
sclk,
|
||||
mosi,
|
||||
miso,
|
||||
cs,
|
||||
100u32.kHz(),
|
||||
SpiMode::Mode0,
|
||||
&mut system.peripheral_clock_control,
|
||||
&clocks,
|
||||
);
|
||||
|
||||
let mut delay = Delay::new(&clocks);
|
||||
|
||||
loop {
|
||||
let mut data = [0xde, 0xca, 0xfb, 0xad];
|
||||
spi.transfer(&mut data).unwrap();
|
||||
writeln!(serial0, "{:x?}", data).ok();
|
||||
|
||||
delay.delay_ms(250u32);
|
||||
}
|
||||
}
|
||||
66
esp32c2-hal/examples/timer_interrupt.rs
Normal file
66
esp32c2-hal/examples/timer_interrupt.rs
Normal file
@ -0,0 +1,66 @@
|
||||
//! This shows how to use the TIMG peripheral interrupts.
|
||||
//! There is TIMG0 which contains a general purpose timer and a watchdog timer.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use core::cell::RefCell;
|
||||
|
||||
use critical_section::Mutex;
|
||||
use esp32c2_hal::{
|
||||
clock::ClockControl,
|
||||
interrupt,
|
||||
pac::{self, Peripherals, TIMG0},
|
||||
prelude::*,
|
||||
timer::{Timer, Timer0, TimerGroup},
|
||||
// Rtc,
|
||||
};
|
||||
use esp_backtrace as _;
|
||||
use riscv_rt::entry;
|
||||
|
||||
static TIMER0: Mutex<RefCell<Option<Timer<Timer0<TIMG0>>>>> = Mutex::new(RefCell::new(None));
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
let peripherals = Peripherals::take().unwrap();
|
||||
let system = peripherals.SYSTEM.split();
|
||||
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
|
||||
|
||||
// Disable the watchdog timers. For the ESP32-C2, this includes the Super WDT,
|
||||
// the RTC WDT, and the TIMG WDT.
|
||||
// let mut rtc = Rtc::new(peripherals.RTC_CNTL);
|
||||
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
|
||||
let mut timer0 = timer_group0.timer0;
|
||||
let mut wdt0 = timer_group0.wdt;
|
||||
|
||||
// rtc.swd.disable();
|
||||
// rtc.rwdt.disable();
|
||||
wdt0.disable();
|
||||
|
||||
interrupt::enable(pac::Interrupt::TG0_T0_LEVEL, interrupt::Priority::Priority1).unwrap();
|
||||
timer0.start(500u64.millis());
|
||||
timer0.listen();
|
||||
|
||||
critical_section::with(|cs| {
|
||||
TIMER0.borrow_ref_mut(cs).replace(timer0);
|
||||
});
|
||||
|
||||
unsafe {
|
||||
riscv::interrupt::enable();
|
||||
}
|
||||
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[interrupt]
|
||||
fn TG0_T0_LEVEL() {
|
||||
critical_section::with(|cs| {
|
||||
esp_println::println!("Interrupt 1");
|
||||
|
||||
let mut timer0 = TIMER0.borrow_ref_mut(cs);
|
||||
let timer0 = timer0.as_mut().unwrap();
|
||||
|
||||
timer0.clear_interrupt();
|
||||
timer0.start(500u64.millis());
|
||||
});
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user