Skip to content

Instantly share code, notes, and snippets.

@eloycoto
Last active March 18, 2023 12:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eloycoto/5a3d062a00c7b696e8b088f183d0a399 to your computer and use it in GitHub Desktop.
Save eloycoto/5a3d062a00c7b696e8b088f183d0a399 to your computer and use it in GitHub Desktop.
#![no_std]
#![no_main]
use esp_backtrace as _;
use esp_println::println;
use hal::{
clock::ClockControl, peripherals::Peripherals, prelude::*, timer::TimerGroup, Delay, Rtc, IO,
};
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.DPORT.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
// Disable the RTC and TIMG watchdog timers
let mut rtc = Rtc::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.rwdt.disable();
wdt0.disable();
wdt1.disable();
println!("Hello world!");
// Set GPIO7 as an output, and set its state high initially.
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let mut led = io.pins.gpio2.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);
}
}
MEMORY
{
/* NOTE K = KiBi = 1024 bytes */
FLASH : ORIGIN = 0x08000000, LENGTH = 128K
RAM : ORIGIN = 0x20000000, LENGTH = 32K
}
#![no_std]
#![no_main]
// Imports
use cortex_m_rt::entry;
use panic_halt as _;
use stm32f4xx_hal::{
gpio::Pin,
pac::{self},
prelude::*,
};
#[entry]
fn main() -> ! {
// Setup handler for device peripherals
let dp = pac::Peripherals::take().unwrap();
// Configure the LED pin as a push pull ouput and obtain handler.
// On the Nucleo FR401 theres an on-board LED connected to pin PA5.
let gpioa = dp.GPIOA.split();
let mut led = gpioa.pa5.into_push_pull_output();
// Configure the button pin (if needed) and obtain handler.
// On the Nucleo FR401 there is a button connected to pin PC13.
// Pin is input by default
let gpioc = dp.GPIOC.split();
let button = gpioc.pc13;
// Create and initialize a delay variable to manage delay loop
let mut del_var = 10_0000_u32;
// Initialize LED to on or off
led.set_low();
// Application Loop
loop {
// Call delay function and update delay variable once done
del_var = loop_delay(del_var, &button);
// Toggle LED
led.toggle();
}
}
// Delay Function
fn loop_delay<const P: char, const N: u8>(mut del: u32, but: &Pin<P, N>) -> u32 {
// Loop for until value of del
for _i in 1..del {
// Check if button got pressed
if but.is_low() {
// If button pressed decrease the delay value
del = del - 2_5000_u32;
// If updated delay value reaches zero then reset it back to starting value
if del < 2_5000 {
del = 10_0000_u32;
}
// Exit function returning updated delay value if button pressed
return del;
}
}
// Exit function returning original delay value if button no pressed (for loop ending naturally)
del
}

Installing:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Setup:

rustup update
rustup component add llvm-tools-preview
rustup target add thumbv7em-none-eabihf
cargo install cargo-binutils cargo-embed cargo-flash cargo-expand
cargo install cargo-generate

arm-toolchain

wget https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu-rm/10.3-2021.10/gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2

Initialize:

https://docs.rust-embedded.org/book/start/qemu.html
cargo generate --git https://github.com/rust-embedded/cortex-m-quickstart --name stm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment