Skip to content

Commit bd546af

Browse files
committed
do not reset OB on probe-rs erase
1 parent 7f8b87c commit bd546af

16 files changed

Lines changed: 10 additions & 149 deletions

File tree

algos/common/src/lib.rs

Lines changed: 1 addition & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#![no_std]
22

33
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};
65

76
pub const KEY1: u32 = 0x4567_0123;
87
pub const KEY2: u32 = 0xCDEF_89AB;
@@ -68,91 +67,3 @@ pub const fn standard(r: &MemoryRegion) -> (u32, u32) {
6867
}
6968
panic!("region has no Standard programming mode")
7069
}
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 &region.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-
}

algos/f1/src/bin/ob.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ impl FlashAlgorithm for Algo {
2828
}
2929

3030
fn erase_all(&mut self) -> Result<(), ErrorCode> {
31-
options_erase();
32-
options_program(OPT_BASE, &OB_DEFAULTS)?;
33-
Ok(())
31+
Err(ERR_NOT_SUPPORTED)
3432
}
3533

3634
fn erase_sector(&mut self, _addr: u32) -> Result<(), ErrorCode> {

algos/f1/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ const OPT_STD: (u32, u32) = standard(region("OPT"));
1919
pub const OPT_ERASE_SIZE: u32 = OPT_STD.0;
2020
pub const OPT_WRITE_SIZE: u32 = OPT_STD.1;
2121

22-
/// Without restoring defaults, post-erase OB leaves `RDPR != 0xA5` and
23-
/// read-protects the chip on next reset.
24-
pub const OB_DEFAULTS_LEN: usize = ob_default_writes_size();
25-
pub const OB_DEFAULTS: [u8; OB_DEFAULTS_LEN] = ob_default_writes();
26-
2722
/// Undocumented read-cache register at FLASH+0x34; used by WCH's HAL
2823
/// (openwch/ch32v103 `ch32v10x_flash.c`). Without poking this post-erase or
2924
/// post-program, the prefetch can serve stale data.

algos/l1/src/bin/ob.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ impl FlashAlgorithm for Algo {
2828
}
2929

3030
fn erase_all(&mut self) -> Result<(), ErrorCode> {
31-
options_erase();
32-
options_program(OPT_BASE, &OB_DEFAULTS)?;
33-
Ok(())
31+
Err(ERR_NOT_SUPPORTED)
3432
}
3533

3634
fn erase_sector(&mut self, _addr: u32) -> Result<(), ErrorCode> {

algos/l1/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ const OPT_FAST: (u32, u32) = fast(region("OPT"));
1717
pub const OPT_PAGE_SIZE: u32 = OPT_FAST.0;
1818
pub const OPT_LOAD: u32 = OPT_FAST.1;
1919

20-
/// Without restoring defaults, post-erase OB leaves `RDPR != 0xA5` and
21-
/// read-protects the chip on next reset.
22-
pub const OB_DEFAULTS_LEN: usize = ob_default_writes_size();
23-
pub const OB_DEFAULTS: [u8; OB_DEFAULTS_LEN] = ob_default_writes();
24-
2520
pub fn wait_busy() {
2621
while FLASH.statr().read().bsy() {}
2722
FLASH.statr().write(|w| w.set_eop(true));

algos/v0/src/bin/ob.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ impl FlashAlgorithm for Algo {
2828
}
2929

3030
fn erase_all(&mut self) -> Result<(), ErrorCode> {
31-
options_erase();
32-
options_program(OPT_BASE, &OB_DEFAULTS)?;
33-
Ok(())
31+
Err(ERR_NOT_SUPPORTED)
3432
}
3533

3634
fn erase_sector(&mut self, _addr: u32) -> Result<(), ErrorCode> {

algos/v0/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ const OPT_STD: (u32, u32) = standard(region("OPT"));
1717
pub const OPT_ERASE_SIZE: u32 = OPT_STD.0;
1818
pub const OPT_WRITE_SIZE: u32 = OPT_STD.1;
1919

20-
/// Without restoring defaults, post-erase OB leaves `RDPR != 0xA5` and
21-
/// read-protects the chip on next reset.
22-
pub const OB_DEFAULTS_LEN: usize = ob_default_writes_size();
23-
pub const OB_DEFAULTS: [u8; OB_DEFAULTS_LEN] = ob_default_writes();
24-
2520
pub fn wait_busy() {
2621
while FLASH.statr().read().bsy() {}
2722
FLASH.statr().write(|w| w.set_eop(true));

algos/v00x/src/bin/ob.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ impl FlashAlgorithm for Algo {
2828
}
2929

3030
fn erase_all(&mut self) -> Result<(), ErrorCode> {
31-
options_erase();
32-
options_program(OPT_BASE, &OB_DEFAULTS)?;
33-
Ok(())
31+
Err(ERR_NOT_SUPPORTED)
3432
}
3533

3634
fn erase_sector(&mut self, _addr: u32) -> Result<(), ErrorCode> {

algos/v00x/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ const OPT_FAST: (u32, u32) = fast(region("OPT"));
1717
pub const OPT_PAGE_SIZE: u32 = OPT_FAST.0;
1818
pub const OPT_LOAD: u32 = OPT_FAST.1;
1919

20-
/// Without restoring defaults, post-erase OB leaves `RDPR != 0xA5` and
21-
/// read-protects the chip on next reset.
22-
pub const OB_DEFAULTS_LEN: usize = ob_default_writes_size();
23-
pub const OB_DEFAULTS: [u8; OB_DEFAULTS_LEN] = ob_default_writes();
24-
2520
pub fn wait_busy() {
2621
while FLASH.statr().read().bsy() {}
2722
FLASH.statr().write(|w| w.set_eop(true));

algos/v1/src/bin/ob.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ impl FlashAlgorithm for Algo {
2828
}
2929

3030
fn erase_all(&mut self) -> Result<(), ErrorCode> {
31-
options_erase();
32-
options_program(OPT_BASE, &OB_DEFAULTS)?;
33-
Ok(())
31+
Err(ERR_NOT_SUPPORTED)
3432
}
3533

3634
fn erase_sector(&mut self, _addr: u32) -> Result<(), ErrorCode> {

0 commit comments

Comments
 (0)