/*
 * @Author       : stark1898y 1658608470@qq.com
 * @Date         : 2024-12-15 16:13:41
 * @LastEditors: mbw && 1600520629@qq.com
 * @LastEditTime: 2024-12-16 10:50:56
 * @FilePath: \ble_-tyq_-bjq_-ch584-m\bsp\src\bsp_iwdg.c
 * @Description  :
 *
 * Copyright (c) 2024 by yzy, All Rights Reserved.
 */
#include "bsp_iwdg.h"

#include "bsp_uart.h"
#include "log.h"

// https://www.cnblogs.com/debugdabiaoge/p/17580033.html
// https://www.cnblogs.com/risc5-ble/p/17853714.html

#undef LOG_ENABLE
#define LOG_ENABLE 1

#undef LOG_TAG
#define LOG_TAG "idwg"

uint8_t IWDGTaskId;

#define IWIG_FEED_EVENT (0x01 << 0)
uint16_t IWDG_ProcessEvent(uint8_t task_id, uint16_t events)
{
    if (events & IWIG_FEED_EVENT)
    {
        FEED_IWDG();
        tmos_start_task(task_id, IWIG_FEED_EVENT, 20000);
        return (events ^ IWIG_FEED_EVENT);
    }
    return 0;
}

// 32k rc
// div 0:4  | 1:8   | 2:16  | 3:32  |
//     4:64 | 5:128 | 6:256 | 7:512(32K分频512，时钟62.5Hz) |
// reload max 4095

// 独立看门狗（IWDG）由专用的内部低速时钟（LSI）驱动，能够在低功耗模式下正常工作。
void IWDG_Init(uint16_t ms)
{
    uint16_t div  = 512;
    float tick_ms = 1000 / (32000 / 512);

    uint16_t reload = (ms / tick_ms) + 1;

    // // 解除IWDG保护
    // R32_IWDG_KR = 0x5555;

    // R32_IWDG_CFG = (RB_PR & (div << 12)) | (RB_STOP_EN) | (reload & RB_RLR);
    // // 开启IWDG保护
    // R32_IWDG_KR = 0xCCCC;

    // logDebug("R32_IWDG_CFG = %X", R32_IWDG_CFG);

    R32_IWDG_KR = 0x5555;      // 解除IWDG保护
    R32_IWDG_CFG |= (7 << 12); // 32K分频512，时钟62.5Hz
    R32_IWDG_CFG &= 0xFFFFF000;
    R32_IWDG_CFG |= 0x10; // 看门狗时间约256ms（0x10/(32K/512))
    R32_IWDG_KR = 0xCCCC;
    PRINT("R32_IWDG_CFG = %x\r\n", R32_IWDG_CFG);

    IWDGTaskId = TMOS_ProcessEventRegister(IWDG_ProcessEvent);
    tmos_set_event(IWDGTaskId, IWIG_FEED_EVENT);
}

// #define  RB_RESET_FLAG      0x07                      // RO: recent reset flag
// #define  RST_FLAG_SW        0x00
// #define  RST_FLAG_RPOR      0x01
// #define  RST_FLAG_WTR       0x02
// #define  RST_FLAG_MR        0x03
// //#define  RST_FLAG_GPWSM     0x04                      // RO, power on reset flag during sleep/shutdown: 0=no power on reset during sleep/shutdown, 1=power on reset occurred during sleep/shutdown
// #define  RST_FLAG_GPWSM     0x05
// // RB_RESET_FLAG: recent reset flag
// //   000 - SR, software reset, by RB_SOFTWARE_RESET=1 @RB_WDOG_RST_EN=0
// //   001 - RPOR, real power on reset
// //   010 - WTR, watch-dog timer-out reset
// //   011 - MR, external manual reset by RST pin input low
// //   101 - GRWSM, global reset by waking under shutdown mode
//   1?? - LRW, power on reset occurred during sleep

// 打印复位状态寄存器，显示复位原因
// 最近一次复位状态：
// 000：软件复位 SR（RB_WDOG_RST_EN=0时软件复位可产生此状态，否则可复位但不产生此状态）；
// 001：上电复位 RPOR；
// 010：看门狗超时复位 WTR；
// 011：外部手动复位 MR；
// 101：从下电模式唤醒时的复位 GRWSM；
// 100/110/111：唤醒复位 LRW，且此前的上一次复位分别是 SR/WTR/MR。
void ShowRestartReason(void)
{
    uint8_t reason = R8_RESET_STATUS & 0x07;
    logDebug("R8_RESET_STATUS = %02x", reason);
    switch (reason)
    {
        case RST_FLAG_SW:
            logDebug("RST_FLAG_SW");
            break;
        case RST_FLAG_RPOR:
            logDebug("RST_FLAG_RPOR");
            break;
        case RST_FLAG_WTR:
            logDebug("RST_FLAG_WTR");
            break;
        case RST_FLAG_MR:
            logDebug("RST_FLAG_MR");
            break;

        case RST_FLAG_GPWSM:
            logDebug("RST_FLAG_GPWSM");
            break;

        case 0x04:
            logDebug("LRW, last SR");
            break;
        case 0x06:
            logDebug("LRW, last WTR");
            break;
        case 0x07:
            logDebug("LRW, last MR");

        default:
            break;
    }
}
