aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2024-04-15 22:28:34 +0000
committerGitHub <[email protected]>2024-04-15 22:28:34 +0000
commit6d122c274f9b2948ee902e58574df4057b693ba6 (patch)
treebfa0d5a06faa231a386632fd2a7c3b3d14387726
parent76bf3f733dd735296553b9c44640bb8a1575fda7 (diff)
parentc5119c6318f72a69f1bdfb91e7987dff954d6c1e (diff)
Merge pull request #2792 from wagcampbell/wgc/u5-flash-non-secure
Support non-secure flash operations on STM32U5
-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 580c490da..ddd4d73ff 100644
--- a/embassy-stm32/src/flash/u5.rs
+++ b/embassy-stm32/src/flash/u5.rs
@@ -14,26 +14,43 @@ 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")]
17 pac::FLASH.seccr().modify(|w| w.set_lock(true)); 18 pac::FLASH.seccr().modify(|w| w.set_lock(true));
19 #[cfg(not(feature = "trustzone-secure"))]
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")]
21 if pac::FLASH.seccr().read().lock() { 25 if pac::FLASH.seccr().read().lock() {
22 pac::FLASH.seckeyr().write_value(0x4567_0123); 26 pac::FLASH.seckeyr().write_value(0x4567_0123);
23 pac::FLASH.seckeyr().write_value(0xCDEF_89AB); 27 pac::FLASH.seckeyr().write_value(0xCDEF_89AB);
24 } 28 }
29 #[cfg(not(feature = "trustzone-secure"))]
30 if pac::FLASH.nscr().read().lock() {
31 pac::FLASH.nskeyr().write_value(0x4567_0123);
32 pac::FLASH.nskeyr().write_value(0xCDEF_89AB);
33 }
25} 34}
26 35
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")]
30 pac::FLASH.seccr().write(|w| { 40 pac::FLASH.seccr().write(|w| {
31 w.set_pg(pac::flash::vals::SeccrPg::B_0X1); 41 w.set_pg(pac::flash::vals::SeccrPg::B_0X1);
32 }); 42 });
43 #[cfg(not(feature = "trustzone-secure"))]
44 pac::FLASH.nscr().write(|w| {
45 w.set_pg(pac::flash::vals::NscrPg::B_0X1);
46 });
33} 47}
34 48
35pub(crate) unsafe fn disable_blocking_write() { 49pub(crate) unsafe fn disable_blocking_write() {
50 #[cfg(feature = "trustzone-secure")]
36 pac::FLASH.seccr().write(|w| w.set_pg(pac::flash::vals::SeccrPg::B_0X0)); 51 pac::FLASH.seccr().write(|w| w.set_pg(pac::flash::vals::SeccrPg::B_0X0));
52 #[cfg(not(feature = "trustzone-secure"))]
53 pac::FLASH.nscr().write(|w| w.set_pg(pac::flash::vals::NscrPg::B_0X0));
37} 54}
38 55
39pub(crate) unsafe fn blocking_write(start_address: u32, buf: &[u8; WRITE_SIZE]) -> Result<(), Error> { 56pub(crate) unsafe fn blocking_write(start_address: u32, buf: &[u8; WRITE_SIZE]) -> Result<(), Error> {
@@ -50,19 +67,35 @@ 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")]
53 pac::FLASH.seccr().modify(|w| { 71 pac::FLASH.seccr().modify(|w| {
54 w.set_per(pac::flash::vals::SeccrPer::B_0X1); 72 w.set_per(pac::flash::vals::SeccrPer::B_0X1);
55 w.set_pnb(sector.index_in_bank) 73 w.set_pnb(sector.index_in_bank)
56 }); 74 });
75 #[cfg(not(feature = "trustzone-secure"))]
76 pac::FLASH.nscr().modify(|w| {
77 w.set_per(pac::flash::vals::NscrPer::B_0X1);
78 w.set_pnb(sector.index_in_bank)
79 });
57 80
81 #[cfg(feature = "trustzone-secure")]
58 pac::FLASH.seccr().modify(|w| { 82 pac::FLASH.seccr().modify(|w| {
59 w.set_strt(true); 83 w.set_strt(true);
60 }); 84 });
85 #[cfg(not(feature = "trustzone-secure"))]
86 pac::FLASH.nscr().modify(|w| {
87 w.set_strt(true);
88 });
61 89
62 let ret: Result<(), Error> = blocking_wait_ready(); 90 let ret: Result<(), Error> = blocking_wait_ready();
91 #[cfg(feature = "trustzone-secure")]
63 pac::FLASH 92 pac::FLASH
64 .seccr() 93 .seccr()
65 .modify(|w| w.set_per(pac::flash::vals::SeccrPer::B_0X0)); 94 .modify(|w| w.set_per(pac::flash::vals::SeccrPer::B_0X0));
95 #[cfg(not(feature = "trustzone-secure"))]
96 pac::FLASH
97 .nscr()
98 .modify(|w| w.set_per(pac::flash::vals::NscrPer::B_0X0));
66 clear_all_err(); 99 clear_all_err();
67 ret 100 ret
68} 101}
@@ -70,12 +103,18 @@ 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")]
73 pac::FLASH.secsr().modify(|_| {}); 107 pac::FLASH.secsr().modify(|_| {});
108 #[cfg(not(feature = "trustzone-secure"))]
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")]
78 let sr = pac::FLASH.secsr().read(); 115 let sr = pac::FLASH.secsr().read();
116 #[cfg(not(feature = "trustzone-secure"))]
117 let sr = pac::FLASH.nssr().read();
79 118
80 if !sr.bsy() { 119 if !sr.bsy() {
81 if sr.pgserr() { 120 if sr.pgserr() {
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