硬汉嵌入式论坛

 找回密码
 立即注册
查看: 482|回复: 2
收起左侧

[脱机烧录] H7-TOOL脱机烧录实现配置美仁半导体MR88F001低功耗模式下关闭看门狗方法

[复制链接]

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
115434
QQ
发表于 2025-2-11 08:58:03 | 显示全部楼层 |阅读模式
这个配置稍有点复杂,做了个LUA实现,实测可以正常配置。

[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

---------------------------结束-----------------------------------


234.png






回复

使用道具 举报

1

主题

144

回帖

147

积分

初级会员

积分
147
发表于 2025-2-11 10:55:25 | 显示全部楼层
感谢白哥支持,太给力了
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
115434
QQ
 楼主| 发表于 2025-2-12 07:47:18 | 显示全部楼层
实测原始代码不动,直接将新增代码添加到高级脚本这里也是可以的

[Lua] 纯文本查看 复制代码
-- 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


123.png


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-4-26 08:54 , Processed in 0.367957 second(s), 27 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表