Created
July 17, 2023 08:33
-
-
Save elfmimi/d0a8ba4b469ce7d748c4dccf416ecd99 to your computer and use it in GitHub Desktop.
pyOCD user script to erase sector0 of flash memory for RA4M1 / R7FA4M1AB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pyocd cmd --script ra-util.py -c "erase_sector0" | |
from pyocd.core.exceptions import TransferFaultError, TransferTimeoutError | |
if not "target_override" in options: | |
options.set("target_override", "cortex_m") | |
# options.set("target_override", "R7FA4M1AB") | |
if not "frequency" in options: | |
options.set("frequency", 12500000) | |
class Context: | |
pass | |
self = Context() | |
def read8(off): | |
return target.read8(off) | |
def read16(off): | |
return target.read16(off) | |
def read32(off): | |
return target.read32(off) | |
def write8(off, dat): | |
target.write8(off, dat) | |
def write16(off, dat): | |
target.write16(off, dat) | |
def write32(off, dat): | |
target.write32(off, dat) | |
@command(help="Test Flash Memory") | |
def erase_sector0(): | |
# PRCR | |
write16(0x4001_E3FE, 0xA503) | |
sckscr = read8(0x4001_E026) | |
print(f"SCKSCR = 0x{sckscr:02X}") | |
write8(0x4001_E026, 0x01) | |
sckscr = read8(0x4001_E026) | |
print(f"SCKSCR = 0x{sckscr:02X}") | |
opccr = read8(0x4001_E0A0) | |
print(f"OPCCR = 0x{opccr:02X}") | |
write8(0x4001_E0A0, 0x01) | |
opccr = read8(0x4001_E0A0) | |
print(f"OPCCR = 0x{opccr:02X}") | |
sckdivcr = read32(0x4001_E020) | |
print(f"SCKDIVCR = 0x{sckdivcr:08X}") | |
write32(0x4001_E020, 0x0000_0000) | |
sckdivcr = read32(0x4001_E020) | |
print(f"SCKDIVCR = 0x{sckdivcr:08X}") | |
# FENTRYR | |
write16(0x407E_FFB2, 0xAA01); | |
# FPR.write(0xA5); | |
write8(0x407E_C180, 0xA5) | |
# FPMCR.write(0x12); | |
write8(0x407E_C100, 0x12) | |
# FPMCR.write(!0x12); | |
write8(0x407E_C100, ~0x12) | |
# FPMCR.write(0x12); | |
write8(0x407E_C100, 0x12) | |
# wait tDIS = 2 us, done roughly | |
# self.delay.delay_us(2); | |
# FPR.write(0xA5); | |
write8(0x407E_C180, 0xA5) | |
# FPMCR.write(0x92); | |
write8(0x407E_C100, 0x92) | |
# FPMCR.write(!0x92); | |
write8(0x407E_C100, ~0x92) | |
# FPMCR.write(0x92); | |
write8(0x407E_C100, 0x92) | |
# wait tMS = 5 us for High-speed and 720ns for Middle-speed, done roughly | |
# self.delay.delay_us(5); | |
# FPR.write(0xA5); | |
write8(0x407E_C180, 0xA5) | |
# FPMCR.write(0x82); | |
write8(0x407E_C100, 0x82) | |
# FPMCR.write(!0x82); | |
write8(0x407E_C100, ~0x82) | |
# FPMCR.write(0x82); | |
write8(0x407E_C100, 0x82) | |
fpmcr = read8(0x407E_C100) | |
print(f"FPMCR = 0x{fpmcr:02X}") | |
# Set PCKA to 3 when FCLK is at 4MHz | |
# FISR.write(4 - 1); | |
# Set PCKA to 7 when FCLK is at 8MHz | |
# FISR.write(8 - 1); | |
write8(0x407E_C1D8, 8 - 1) | |
# FRESETR.write(1); | |
# FRESETR.write(0); | |
write8(0x407E_C124, 1) | |
write8(0x407E_C124, 0) | |
# let addr = 0x0000_0000 | |
# FSARH.write((addr >> 16) as u16); | |
write16(0x407E_C110, 0) | |
# FSARL.write(addr as u16); | |
write16(0x407E_C108, 0) | |
# let end_addr = addr + 0x800; | |
# FEARH.write((end_addr >> 16) as u16); | |
write16(0x407E_C120, 0) | |
# FEARL.write(end_addr as u16); | |
write16(0x407E_C118, 0x7FF) | |
# FCR.write(0x84); // Block Erase Command | |
write8(0x407E_C114, 0x84) | |
# while (FSTATR1.read() & 0x40) == 0 { } // Wait until FRDY = 1 | |
while (read8(0x407E_C12C) & 0x40) == 0: | |
pass | |
# FCR.write(0x00); | |
write8(0x407E_C114, 0x00) | |
# while (FSTATR1.read() & 0x40) != 0 { } // Wait while FRDY = 1 | |
while (read8(0x407E_C12C) & 0x40) != 0: | |
pass | |
# rprintln!("FSTATR2 = 0x{:X}", FSTATR2.read()); | |
fstatr2 = read8(0x407E_C1F0) | |
print(f"FSTATR2 = 0x{fstatr2:02X}") | |
# Switch backto read-mode | |
# FPR.write(0xA5); | |
write8(0x407E_C180, 0xA5) | |
# FPMCR.write(0x92); | |
write8(0x407E_C100, 0x92) | |
# FPMCR.write(!0x92); | |
write8(0x407E_C100, ~0x92) | |
# FPMCR.write(0x92); | |
write8(0x407E_C100, 0x92) | |
# wait tUndefined, done roughly | |
# self.delay.delay_us(1); | |
# FPR.write(0xA5); | |
write8(0x407E_C180, 0xA5) | |
# FPMCR.write(0x12); | |
write8(0x407E_C100, 0x12) | |
# FPMCR.write(!0x12); | |
write8(0x407E_C100, ~0x12) | |
# FPMCR.write(0x12); | |
write8(0x407E_C100, 0x12) | |
# wait tMS = 5 us for High-speed and 720ns for Middle-speed, done roughly | |
# self.delay.delay_us(5); | |
# FPR.write(0xA5); | |
write8(0x407E_C180, 0xA5) | |
# FPMCR.write(0x08); | |
write8(0x407E_C100, 0x08) | |
# FPMCR.write(!0x08); | |
write8(0x407E_C100, ~0x08) | |
# FPMCR.write(0x08); | |
write8(0x407E_C100, 0x08) | |
# FENTRYR.write(0xAA00); | |
write16(0x407E_FFB2, 0xAA00); | |
target.reset() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment