|
1 | 1 | #![no_std] |
2 | 2 |
|
3 | 3 | pub use ch32_metapac::FLASH; |
4 | | -pub use ch32_metapac::metadata::ir::{Block, BlockItem, BlockItemInner, Register}; |
5 | | -pub use ch32_metapac::metadata::{METADATA, MemoryRegion, Mode, NvStruct}; |
| 4 | +pub use ch32_metapac::metadata::{METADATA, MemoryRegion, Mode}; |
6 | 5 |
|
7 | 6 | pub const KEY1: u32 = 0x4567_0123; |
8 | 7 | pub const KEY2: u32 = 0xCDEF_89AB; |
@@ -68,91 +67,3 @@ pub const fn standard(r: &MemoryRegion) -> (u32, u32) { |
68 | 67 | } |
69 | 68 | panic!("region has no Standard programming mode") |
70 | 69 | } |
71 | | - |
72 | | -/// Rounded to 4 so the result is valid for both standard (halfword) and |
73 | | -/// fast-mode (word) OBPG paths. |
74 | | -pub const fn ob_default_writes_size() -> usize { |
75 | | - let nv = find_ob_struct(); |
76 | | - let block = find_block(nv); |
77 | | - let mut max_end = 0usize; |
78 | | - let mut d = 0; |
79 | | - while d < nv.defaults.len() { |
80 | | - let (name, _) = nv.defaults[d]; |
81 | | - if let Some(item) = item_by_name(block, name) { |
82 | | - if let BlockItemInner::Register(reg) = &item.inner { |
83 | | - let bytes = ((reg.bit_size + 7) / 8) as usize; |
84 | | - let end = item.byte_offset as usize + bytes; |
85 | | - if end > max_end { |
86 | | - max_end = end; |
87 | | - } |
88 | | - } |
89 | | - } |
90 | | - d += 1; |
91 | | - } |
92 | | - (max_end + 3) & !3 |
93 | | -} |
94 | | - |
95 | | -/// Fast-mode OBPG does not auto-generate complements, so ch32-data lists |
96 | | -/// complement siblings (e.g. NRDPR) in `defaults` and we write them too. |
97 | | -pub const fn ob_default_writes<const N: usize>() -> [u8; N] { |
98 | | - let mut buf = [0xFFu8; N]; |
99 | | - let nv = find_ob_struct(); |
100 | | - let block = find_block(nv); |
101 | | - let mut d = 0; |
102 | | - while d < nv.defaults.len() { |
103 | | - let (name, value) = nv.defaults[d]; |
104 | | - if let Some(item) = item_by_name(block, name) { |
105 | | - if let BlockItemInner::Register(reg) = &item.inner { |
106 | | - let bytes = ((reg.bit_size + 7) / 8) as usize; |
107 | | - let start = item.byte_offset as usize; |
108 | | - if start + bytes <= N { |
109 | | - let mut b = 0; |
110 | | - while b < bytes { |
111 | | - buf[start + b] = ((value as u64 >> (8 * b)) & 0xff) as u8; |
112 | | - b += 1; |
113 | | - } |
114 | | - } |
115 | | - } |
116 | | - } |
117 | | - d += 1; |
118 | | - } |
119 | | - buf |
120 | | -} |
121 | | - |
122 | | -const fn find_ob_struct() -> &'static NvStruct { |
123 | | - let mut i = 0; |
124 | | - while i < METADATA.memory.len() { |
125 | | - let region = &METADATA.memory[i]; |
126 | | - let mut j = 0; |
127 | | - while j < region.structs.len() { |
128 | | - if str_eq(region.structs[j].kind, "ob") { |
129 | | - return ®ion.structs[j]; |
130 | | - } |
131 | | - j += 1; |
132 | | - } |
133 | | - i += 1; |
134 | | - } |
135 | | - panic!("no `ob` struct in METADATA.memory") |
136 | | -} |
137 | | - |
138 | | -const fn find_block(nv: &'static NvStruct) -> &'static Block { |
139 | | - let mut i = 0; |
140 | | - while i < nv.ir.blocks.len() { |
141 | | - if str_eq(nv.ir.blocks[i].name, nv.block) { |
142 | | - return &nv.ir.blocks[i]; |
143 | | - } |
144 | | - i += 1; |
145 | | - } |
146 | | - panic!("OB struct's block name not found in IR") |
147 | | -} |
148 | | - |
149 | | -const fn item_by_name(block: &'static Block, name: &str) -> Option<&'static BlockItem> { |
150 | | - let mut i = 0; |
151 | | - while i < block.items.len() { |
152 | | - if str_eq(block.items[i].name, name) { |
153 | | - return Some(&block.items[i]); |
154 | | - } |
155 | | - i += 1; |
156 | | - } |
157 | | - None |
158 | | -} |
0 commit comments