This repository was archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Low Level API #10
Merged
Merged
Low Level API #10
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
//! This shows usage of the underlying low-level API | ||
//! | ||
//! Using this API is generally discouraged and reserved to a few very special use-cases. | ||
//! | ||
|
||
#![no_std] | ||
#![no_main] | ||
|
||
#[cfg(feature = "esp32")] | ||
use esp32_hal as hal; | ||
|
||
#[cfg(feature = "esp32s2")] | ||
use esp32s2_hal as hal; | ||
|
||
#[cfg(feature = "esp32s3")] | ||
use esp32s3_hal as hal; | ||
|
||
#[cfg(feature = "esp32c3")] | ||
use esp32c3_hal as hal; | ||
|
||
#[cfg(feature = "esp32c2")] | ||
use esp32c2_hal as hal; | ||
|
||
#[cfg(feature = "esp32c6")] | ||
use esp32c6_hal as hal; | ||
|
||
use hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}; | ||
|
||
use esp_backtrace as _; | ||
use esp_println::println; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let peripherals = Peripherals::take(); | ||
|
||
#[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))] | ||
{ | ||
#[cfg(feature = "esp32")] | ||
let system = peripherals.DPORT.split(); | ||
#[cfg(not(feature = "esp32"))] | ||
let system = peripherals.SYSTEM.split(); | ||
|
||
let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); | ||
|
||
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); | ||
let mut wdt = timer_group0.wdt; | ||
let mut rtc = Rtc::new(peripherals.RTC_CNTL); | ||
|
||
// Disable MWDT and RWDT (Watchdog) flash boot protection | ||
wdt.disable(); | ||
rtc.rwdt.disable(); | ||
} | ||
|
||
#[cfg(any(feature = "esp32c3", feature = "esp32c2"))] | ||
{ | ||
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 wdt0 = timer_group0.wdt; | ||
|
||
#[cfg(not(feature = "esp32c2"))] | ||
let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks); | ||
#[cfg(not(feature = "esp32c2"))] | ||
let mut wdt1 = timer_group1.wdt; | ||
|
||
rtc.swd.disable(); | ||
rtc.rwdt.disable(); | ||
wdt0.disable(); | ||
#[cfg(not(feature = "esp32c2"))] | ||
wdt1.disable(); | ||
} | ||
|
||
#[cfg(any(feature = "esp32c6"))] | ||
{ | ||
let system = peripherals.PCR.split(); | ||
let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); | ||
|
||
let mut rtc = Rtc::new(peripherals.LP_CLKRST); | ||
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); | ||
let mut wdt0 = timer_group0.wdt; | ||
|
||
#[cfg(not(feature = "esp32c2"))] | ||
let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks); | ||
#[cfg(not(feature = "esp32c2"))] | ||
let mut wdt1 = timer_group1.wdt; | ||
|
||
rtc.swd.disable(); | ||
rtc.rwdt.disable(); | ||
wdt0.disable(); | ||
#[cfg(not(feature = "esp32c2"))] | ||
wdt1.disable(); | ||
} | ||
|
||
let mut bytes = [0u8; 48]; | ||
let flash_addr = 0x9000; | ||
|
||
unsafe { | ||
esp_storage::ll::spiflash_read( | ||
flash_addr, | ||
bytes.as_mut_ptr() as *mut u32, | ||
bytes.len() as u32, | ||
) | ||
} | ||
.unwrap(); | ||
|
||
println!("Read from {:x}: {:02x?}", flash_addr, &bytes[..48]); | ||
|
||
bytes[0x00] = bytes[0x00].wrapping_add(1); | ||
bytes[0x01] = bytes[0x01].wrapping_add(2); | ||
bytes[0x02] = bytes[0x02].wrapping_add(3); | ||
bytes[0x03] = bytes[0x03].wrapping_add(4); | ||
bytes[0x04] = bytes[0x04].wrapping_add(1); | ||
bytes[0x05] = bytes[0x05].wrapping_add(2); | ||
bytes[0x06] = bytes[0x06].wrapping_add(3); | ||
bytes[0x07] = bytes[0x07].wrapping_add(4); | ||
|
||
unsafe { esp_storage::ll::spiflash_unlock() }.unwrap(); | ||
|
||
unsafe { esp_storage::ll::spiflash_erase_sector(flash_addr / 4096) }.unwrap(); | ||
|
||
unsafe { | ||
esp_storage::ll::spiflash_write(flash_addr, bytes.as_ptr() as *const u8, bytes.len() as u32) | ||
} | ||
.unwrap(); | ||
println!("Written to {:x}: {:02x?}", flash_addr, &bytes[..48]); | ||
|
||
let mut reread_bytes = [0u8; 48]; | ||
unsafe { | ||
esp_storage::ll::spiflash_read( | ||
flash_addr, | ||
reread_bytes.as_mut_ptr() as *mut u32, | ||
reread_bytes.len() as u32, | ||
) | ||
} | ||
.unwrap(); | ||
println!("Read from {:x}: {:02x?}", flash_addr, &reread_bytes[..48]); | ||
|
||
loop {} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.