[Lua] 纯文本查看 复制代码
-------------------------------------------------------
-- 文件名 : MR88FX02_Lib.lua
-- 版 本 : V1.0 2021-09--8
-- 说 明 :
-------------------------------------------------------
-- Define constants
AHBBCKCON = 0x40010228
PERCKEN = 0x40010234
FLASH_ACR = 0x40014000
FLASH_KEY = 0x40014004
FLASH_SR = 0x40014008
FLASH_IER = 0x4001400c
FLASH_OPTR = 0x40014010
FLASH_BLPR = 0x40014014
FLASH_WCR = 0x40014018
FLASH_WTC = 0x4001401c
FLASH_AUTH = 0x4001403c
IWDTKR = 0x40006000
FLASH_NVR0_BASE = 0x1ffff600
FLASH_NVR1_BASE = 0x1ffff800
FLASH_NVR2_BASE = 0x1ffffa00
FLASH_NVR3_BASE = 0x1ffffc00
FLASH_NVR4_BASE = 0x1ffffe00
FLASH_NVR5_NOPROT_CODE = 0x3355ccaa
-- Define read and write functions (placeholder)
function rword(addr)
data, re = pg_read32(addr)
return data
end
function wword(addr, value)
pg_write32(addr, value)
-- Simulate writing a 32-bit word to the address
-- print(string.format("Writing 0x%08X to address 0x%08X", value, addr))
end
-- Sleep function (placeholder)
function sleep(ms)
delayms(ms)
-- Simulate sleep (in milliseconds)
--print("Sleeping for " .. ms .. " ms")
end
-- Sector erase function
function sector_erase(addr)
local read_data
local sel
print("Erasing NVR")
wword(IWDTKR, 0x5a5a5a5a)
-- Enable clock
read_data = rword(AHBBCKCON)
wword(AHBBCKCON, read_data | (1 << 6))
read_data = rword(PERCKEN)
wword(PERCKEN, read_data | (1 << 17))
-- Clean flag
read_data = rword(FLASH_ACR)
wword(FLASH_ACR, read_data | 0x200)
wword(FLASH_SR, 0x1F) -- Clear SR
wword(FLASH_WCR, 0x00000001) -- Sector erase request
wword(FLASH_KEY, 0xaa665599) -- Operation key
wword(FLASH_KEY, 0xcdcd3232)
wword(addr, 0x12345678) -- Trigger erase
sleep(3) -- Max 5 ms
wword(FLASH_SR, 0x1) -- Clear SR.ERAD
wword(FLASH_KEY, 0x00000000) -- Clear key
end
-- Program word function
function prog_word(addr, data)
local read_data
print("Calling prog_main()...")
-- Feed watchdog in case it is running
wword(IWDTKR, 0x5a5a5a5a)
-- Enable clock
read_data = rword(AHBBCKCON)
wword(AHBBCKCON, read_data | (1 << 6))
read_data = rword(PERCKEN)
wword(PERCKEN, read_data | (1 << 17))
-- Clean flag
read_data = rword(FLASH_ACR)
wword(FLASH_ACR, read_data | 0x200)
wword(FLASH_SR, 0x1F) -- Clear SR
wword(FLASH_WCR, 0x00000002) -- Program request
wword(FLASH_KEY, 0x55aaaa55) -- Operation key
wword(FLASH_KEY, 0xb4b44b4b)
-- Program word
wword(addr, data)
sleep(1) -- Max less than 1 ms
wword(FLASH_SR, 0x2) -- Clear SR.PRGD
wword(FLASH_KEY, 0x00000000) -- Clear key
print("Exiting prog_main()...")
end
-- Chip erase function
function chip_erase()
local read_data
print("Calling chip_erase()...")
-- Feed watchdog in case it is running
wword(IWDTKR, 0x5a5a5a5a)
-- Enable clock
read_data = rword(AHBBCKCON)
wword(AHBBCKCON, read_data | (1 << 6))
read_data = rword(PERCKEN)
wword(PERCKEN, read_data | (1 << 17))
-- Clean flag
read_data = rword(FLASH_ACR)
wword(FLASH_ACR, read_data | 0x200)
wword(FLASH_SR, 0x1F) -- Clear SR
wword(FLASH_WCR, 0x00000201) -- Chip erase request
wword(FLASH_KEY, 0xaa665599) -- Operation key
wword(FLASH_KEY, 0xe8e81717)
wword(0, 0x12345678) -- Trigger erase
sleep(30) -- Max 40 ms
wword(FLASH_SR, 0x1) -- Clear SR.ERAD
wword(FLASH_KEY, 0x00000000) -- Clear key
print("Exiting chip_erase()...")
end
-- NVR2 unprotect function
function nvr2_unprotect()
print("Calling nvr2_unprotect()...")
chip_erase()
-- Sector erase
sector_erase(FLASH_NVR2_BASE)
-- Program words
prog_word(FLASH_NVR2_BASE, 0x3355ccaa)
prog_word(FLASH_NVR2_BASE + 0x04, 0x0000FFFF)
prog_word(FLASH_NVR2_BASE + 0x08, 0xFF0000FF)
prog_word(FLASH_NVR2_BASE + 0x10, 0xFF660099)
print("Exiting nvr2_unprotect()...")
end
-- NVR2 enable WDT freeze function
function nvr2_en_wdt_freeze()
print("Calling nvr2_en_wdt_freeze()...")
chip_erase()
-- Sector erase
sector_erase(FLASH_NVR2_BASE)
-- Program words
prog_word(FLASH_NVR2_BASE, 0x3355ccaa)
prog_word(FLASH_NVR2_BASE + 0x04, 0x0000FFFF)
prog_word(FLASH_NVR2_BASE + 0x08, 0xFF0000FF)
prog_word(FLASH_NVR2_BASE + 0x10, 0xFF0000FF)
print("Exiting nvr2_en_wdt_freeze()...")
end
-- Define buttons (simulated as functions)
function chip_erase_button()
chip_erase()
end
function nvr2_unprotect_button()
nvr2_unprotect()
end
function nvr2_en_wdt_freeze_button()
nvr2_en_wdt_freeze()
end
--复位期间执行的函数. 目的:debug期间冻结看门狗时钟,低功耗模式启动HCLK和FCLK
function InitUnderReset(void)
local str = "error"
print("InitUnderReset()")
--403A 407系列缺省没有开PWR标志位
-- if (pg_write32(RCC_APB1ENR_M3M0_M3N4, 0x10000000) == 0) then
-- goto quit_err
-- end
-- if (pg_write32(0xE0042004, 0x00000307) == 0) then
-- goto quit_err
-- end
-- if (ReadDeviceID() ~= 0) then
-- goto quit_err
-- end
nvr2_en_wdt_freeze_button()
::quit_ok::
str = "OK"
::quit_err::
print(str)
return str
end
--读芯片ID
function ReadDeviceID(void)
local str
local err = 0
local j
local ch_num
if (MULTI_MODE == 0) then
ch_num = 1
else
ch_num = MULTI_MODE
end
g_DevID = {pg_read32(0x40015800)} --全局变量g_DevID[]
str = "..DeviceID = "
for j = 1, ch_num, 1 do
str = str..string.format("%08X ", g_DevID[j])
if (g_DevID[j] == 0) then
if (ABORT_ON_ERROR == 1) then
err = 1
end
end
end
print(str)
return err
end
---------------------------结束-----------------------------------