aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-stm32/Cargo.toml3
-rw-r--r--embassy-stm32/src/flash/u5.rs39
-rw-r--r--examples/stm32u5/Cargo.toml4
3 files changed, 46 insertions, 0 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml
index 459d2e370..1f9a51678 100644
--- a/embassy-stm32/Cargo.toml
+++ b/embassy-stm32/Cargo.toml
@@ -115,6 +115,9 @@ low-power-debug-with-sleep = []
115## Automatically generate `memory.x` file using [`stm32-metapac`](https://docs.rs/stm32-metapac/) 115## Automatically generate `memory.x` file using [`stm32-metapac`](https://docs.rs/stm32-metapac/)
116memory-x = ["stm32-metapac/memory-x"] 116memory-x = ["stm32-metapac/memory-x"]
117 117
118## Use secure registers when TrustZone is enabled
119trustzone-secure = []
120
118## Re-export stm32-metapac at `embassy_stm32::pac`. 121## Re-export stm32-metapac at `embassy_stm32::pac`.
119## This is unstable because semver-minor (non-breaking) releases of embassy-stm32 may major-bump (breaking) the stm32-metapac version. 122## This is unstable because semver-minor (non-breaking) releases of embassy-stm32 may major-bump (breaking) the stm32-metapac version.
120## If this is an issue for you, you're encouraged to directly depend on a fixed version of the PAC. 123## If this is an issue for you, you're encouraged to directly depend on a fixed version of the PAC.
diff --git a/embassy-stm32/src/flash/u5.rs b/embassy-stm32/src/flash/u5.rs
index 4a2168b14..ddd4d73ff 100644
--- a/embassy-stm32/src/flash/u5.rs
+++ b/embassy-stm32/src/flash/u5.rs
@@ -14,10 +14,19 @@ pub(crate) const fn get_flash_regions() -> &'static [&'static FlashRegion] {
14} 14}
15 15
16pub(crate) unsafe fn lock() { 16pub(crate) unsafe fn lock() {
17 #[cfg(feature = "trustzone-secure")]
18 pac::FLASH.seccr().modify(|w| w.set_lock(true));
19 #[cfg(not(feature = "trustzone-secure"))]
17 pac::FLASH.nscr().modify(|w| w.set_lock(true)); 20 pac::FLASH.nscr().modify(|w| w.set_lock(true));
18} 21}
19 22
20pub(crate) unsafe fn unlock() { 23pub(crate) unsafe fn unlock() {
24 #[cfg(feature = "trustzone-secure")]
25 if pac::FLASH.seccr().read().lock() {
26 pac::FLASH.seckeyr().write_value(0x4567_0123);
27 pac::FLASH.seckeyr().write_value(0xCDEF_89AB);
28 }
29 #[cfg(not(feature = "trustzone-secure"))]
21 if pac::FLASH.nscr().read().lock() { 30 if pac::FLASH.nscr().read().lock() {
22 pac::FLASH.nskeyr().write_value(0x4567_0123); 31 pac::FLASH.nskeyr().write_value(0x4567_0123);
23 pac::FLASH.nskeyr().write_value(0xCDEF_89AB); 32 pac::FLASH.nskeyr().write_value(0xCDEF_89AB);
@@ -27,12 +36,20 @@ pub(crate) unsafe fn unlock() {
27pub(crate) unsafe fn enable_blocking_write() { 36pub(crate) unsafe fn enable_blocking_write() {
28 assert_eq!(0, WRITE_SIZE % 4); 37 assert_eq!(0, WRITE_SIZE % 4);
29 38
39 #[cfg(feature = "trustzone-secure")]
40 pac::FLASH.seccr().write(|w| {
41 w.set_pg(pac::flash::vals::SeccrPg::B_0X1);
42 });
43 #[cfg(not(feature = "trustzone-secure"))]
30 pac::FLASH.nscr().write(|w| { 44 pac::FLASH.nscr().write(|w| {
31 w.set_pg(pac::flash::vals::NscrPg::B_0X1); 45 w.set_pg(pac::flash::vals::NscrPg::B_0X1);
32 }); 46 });
33} 47}
34 48
35pub(crate) unsafe fn disable_blocking_write() { 49pub(crate) unsafe fn disable_blocking_write() {
50 #[cfg(feature = "trustzone-secure")]
51 pac::FLASH.seccr().write(|w| w.set_pg(pac::flash::vals::SeccrPg::B_0X0));
52 #[cfg(not(feature = "trustzone-secure"))]
36 pac::FLASH.nscr().write(|w| w.set_pg(pac::flash::vals::NscrPg::B_0X0)); 53 pac::FLASH.nscr().write(|w| w.set_pg(pac::flash::vals::NscrPg::B_0X0));
37} 54}
38 55
@@ -50,16 +67,32 @@ pub(crate) unsafe fn blocking_write(start_address: u32, buf: &[u8; WRITE_SIZE])
50} 67}
51 68
52pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> { 69pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> {
70 #[cfg(feature = "trustzone-secure")]
71 pac::FLASH.seccr().modify(|w| {
72 w.set_per(pac::flash::vals::SeccrPer::B_0X1);
73 w.set_pnb(sector.index_in_bank)
74 });
75 #[cfg(not(feature = "trustzone-secure"))]
53 pac::FLASH.nscr().modify(|w| { 76 pac::FLASH.nscr().modify(|w| {
54 w.set_per(pac::flash::vals::NscrPer::B_0X1); 77 w.set_per(pac::flash::vals::NscrPer::B_0X1);
55 w.set_pnb(sector.index_in_bank) 78 w.set_pnb(sector.index_in_bank)
56 }); 79 });
57 80
81 #[cfg(feature = "trustzone-secure")]
82 pac::FLASH.seccr().modify(|w| {
83 w.set_strt(true);
84 });
85 #[cfg(not(feature = "trustzone-secure"))]
58 pac::FLASH.nscr().modify(|w| { 86 pac::FLASH.nscr().modify(|w| {
59 w.set_strt(true); 87 w.set_strt(true);
60 }); 88 });
61 89
62 let ret: Result<(), Error> = blocking_wait_ready(); 90 let ret: Result<(), Error> = blocking_wait_ready();
91 #[cfg(feature = "trustzone-secure")]
92 pac::FLASH
93 .seccr()
94 .modify(|w| w.set_per(pac::flash::vals::SeccrPer::B_0X0));
95 #[cfg(not(feature = "trustzone-secure"))]
63 pac::FLASH 96 pac::FLASH
64 .nscr() 97 .nscr()
65 .modify(|w| w.set_per(pac::flash::vals::NscrPer::B_0X0)); 98 .modify(|w| w.set_per(pac::flash::vals::NscrPer::B_0X0));
@@ -70,11 +103,17 @@ pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), E
70pub(crate) unsafe fn clear_all_err() { 103pub(crate) unsafe fn clear_all_err() {
71 // read and write back the same value. 104 // read and write back the same value.
72 // This clears all "write 1 to clear" bits. 105 // This clears all "write 1 to clear" bits.
106 #[cfg(feature = "trustzone-secure")]
107 pac::FLASH.secsr().modify(|_| {});
108 #[cfg(not(feature = "trustzone-secure"))]
73 pac::FLASH.nssr().modify(|_| {}); 109 pac::FLASH.nssr().modify(|_| {});
74} 110}
75 111
76unsafe fn blocking_wait_ready() -> Result<(), Error> { 112unsafe fn blocking_wait_ready() -> Result<(), Error> {
77 loop { 113 loop {
114 #[cfg(feature = "trustzone-secure")]
115 let sr = pac::FLASH.secsr().read();
116 #[cfg(not(feature = "trustzone-secure"))]
78 let sr = pac::FLASH.nssr().read(); 117 let sr = pac::FLASH.nssr().read();
79 118
80 if !sr.bsy() { 119 if !sr.bsy() {
diff --git a/examples/stm32u5/Cargo.toml b/examples/stm32u5/Cargo.toml
index 03294339d..01320b88d 100644
--- a/examples/stm32u5/Cargo.toml
+++ b/examples/stm32u5/Cargo.toml
@@ -24,5 +24,9 @@ heapless = { version = "0.8", default-features = false }
24 24
25micromath = "2.0.0" 25micromath = "2.0.0"
26 26
27[features]
28## Use secure registers when TrustZone is enabled
29trustzone-secure = ["embassy-stm32/trustzone-secure"]
30
27[profile.release] 31[profile.release]
28debug = 2 32debug = 2