diff options
| author | Warren Campbell <[email protected]> | 2024-04-15 17:32:44 -0400 |
|---|---|---|
| committer | Warren Campbell <[email protected]> | 2024-04-15 18:02:29 -0400 |
| commit | c5119c6318f72a69f1bdfb91e7987dff954d6c1e (patch) | |
| tree | 25921be8dff704ccf45177895954497f87d002db /embassy-stm32/src/flash | |
| parent | 2fa0bb7d6eaf73dcb589f831b97559f5b81bb9c1 (diff) | |
Add support for using secure registers
Diffstat (limited to 'embassy-stm32/src/flash')
| -rw-r--r-- | embassy-stm32/src/flash/u5.rs | 39 |
1 files changed, 39 insertions, 0 deletions
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 | ||
| 16 | pub(crate) unsafe fn lock() { | 16 | pub(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 | ||
| 20 | pub(crate) unsafe fn unlock() { | 23 | pub(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() { | |||
| 27 | pub(crate) unsafe fn enable_blocking_write() { | 36 | pub(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 | ||
| 35 | pub(crate) unsafe fn disable_blocking_write() { | 49 | pub(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 | ||
| 52 | pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> { | 69 | pub(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 | |||
| 70 | pub(crate) unsafe fn clear_all_err() { | 103 | pub(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 | ||
| 76 | unsafe fn blocking_wait_ready() -> Result<(), Error> { | 112 | unsafe 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() { |
