|
| 1 | +#![macro_use] |
| 2 | + |
| 3 | + |
| 4 | +use core::num::NonZeroU32; |
| 5 | + |
| 6 | +#[panic_handler] |
| 7 | +fn panic(_info: &core::panic::PanicInfo) -> ! { |
| 8 | + unsafe { |
| 9 | + asm!("udf #0"); |
| 10 | + core::hint::unreachable_unchecked(); |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +pub const FUNCTION_ERASE: u32 = 1; |
| 15 | +pub const FUNCTION_PROGRAM: u32 = 2; |
| 16 | +pub const FUNCTION_VERIFY: u32 = 3; |
| 17 | + |
| 18 | +pub type ErrorCode = NonZeroU32; |
| 19 | + |
| 20 | +pub trait FlashAlgo: Sized + 'static { |
| 21 | + /// Initialize the flash algorithm. |
| 22 | + fn new(address: u32, clock: u32, function: u32) -> Result<Self, ErrorCode>; |
| 23 | + |
| 24 | + /// Erase entire chip. May only be called after init() with FUNCTION_ERASE |
| 25 | + fn erase_all(&mut self) -> Result<(), ErrorCode>; |
| 26 | + |
| 27 | + /// Erase sector. May only be called after init() with FUNCTION_ERASE |
| 28 | + fn erase_sector(&mut self, addr: u32) -> Result<(), ErrorCode>; |
| 29 | + |
| 30 | + /// Program bytes. May only be called after init() with FUNCTION_PROGRAM |
| 31 | + fn program_page(&mut self, addr: u32, size: u32, data: *const u8) -> Result<(), ErrorCode>; |
| 32 | +} |
| 33 | + |
| 34 | +#[macro_export] |
| 35 | +macro_rules! algo { |
| 36 | + ($type:ty) => { |
| 37 | + static mut _IS_INIT: bool = false; |
| 38 | + static mut _ALGO_INSTANCE: MaybeUninit<$type> = MaybeUninit::uninit(); |
| 39 | + |
| 40 | + #[no_mangle] |
| 41 | + #[link_section = ".entry"] |
| 42 | + pub unsafe extern "C" fn _algo_init(addr: u32, clock: u32, function: u32) -> u32 { |
| 43 | + if _IS_INIT { |
| 44 | + _algo_uninit(); |
| 45 | + } |
| 46 | + _IS_INIT = true; |
| 47 | + match <$type as FlashAlgo>::new(addr, clock, function) { |
| 48 | + Ok(inst) => { |
| 49 | + _ALGO_INSTANCE.as_mut_ptr().write(inst); |
| 50 | + _IS_INIT = true; |
| 51 | + 0 |
| 52 | + } |
| 53 | + Err(e) => e.get(), |
| 54 | + } |
| 55 | + } |
| 56 | + #[no_mangle] |
| 57 | + #[link_section = ".entry"] |
| 58 | + pub unsafe extern "C" fn _algo_uninit() -> u32 { |
| 59 | + if !_IS_INIT { |
| 60 | + return 1; |
| 61 | + } |
| 62 | + _ALGO_INSTANCE.as_mut_ptr().drop_in_place(); |
| 63 | + _IS_INIT = false; |
| 64 | + 0 |
| 65 | + } |
| 66 | + #[no_mangle] |
| 67 | + #[link_section = ".entry"] |
| 68 | + pub unsafe extern "C" fn _algo_erase_all() -> u32 { |
| 69 | + if !_IS_INIT { |
| 70 | + return 1; |
| 71 | + } |
| 72 | + let this = &mut *_ALGO_INSTANCE.as_mut_ptr(); |
| 73 | + match <$type as FlashAlgo>::erase_all(this) { |
| 74 | + Ok(()) => 0, |
| 75 | + Err(e) => e.get(), |
| 76 | + } |
| 77 | + } |
| 78 | + #[no_mangle] |
| 79 | + #[link_section = ".entry"] |
| 80 | + pub unsafe extern "C" fn _algo_erase_sector(addr: u32) -> u32 { |
| 81 | + if !_IS_INIT { |
| 82 | + return 1; |
| 83 | + } |
| 84 | + let this = &mut *_ALGO_INSTANCE.as_mut_ptr(); |
| 85 | + match <$type as FlashAlgo>::erase_sector(this, addr) { |
| 86 | + Ok(()) => 0, |
| 87 | + Err(e) => e.get(), |
| 88 | + } |
| 89 | + } |
| 90 | + #[no_mangle] |
| 91 | + #[link_section = ".entry"] |
| 92 | + pub unsafe extern "C" fn _algo_program_page(addr: u32, size: u32, data: *const u8) -> u32 { |
| 93 | + if !_IS_INIT { |
| 94 | + return 1; |
| 95 | + } |
| 96 | + let this = &mut *_ALGO_INSTANCE.as_mut_ptr(); |
| 97 | + match <$type as FlashAlgo>::program_page(this, addr, size, data) { |
| 98 | + Ok(()) => 0, |
| 99 | + Err(e) => e.get(), |
| 100 | + } |
| 101 | + } |
| 102 | + }; |
| 103 | +} |
0 commit comments