/**************************************************************************//**
 *
 *  \copyright  This software is the property of  Technologies. Any
 *              information contained in this doc should not be reproduced,
 *              or used, or disclosed without the written authorization from
 *               Technologies.
 *
 *  \brief      This is the source file of the watchdog module .
 *
 *  \file       bl_watchdog.c
 *  \ingroup    driver_module
 *  \author     
 *
 *  \version    1.0.0
 *  \date       14/05/2014
 *
 *  \par        Changelist
 *      Version  | Date       | Authors          | CR# | Descriptions
 *      -------- | ---------- | ---------------- | --- | -------------
 *      01.00.00 | 14/05/2014 | ning.chen        | N/A | BootDrv010001
 *****************************************************************************/
#include "bl_common.h"
#include "bl_rte_funcfg.h"
#include "bl_system.h"
#include "bl_watchdog.h"
#include "bl_rte_cfg.h"
#include "boot_app_cfg.h"
#include "nvic.h"
#include "wdog.h"

/*****************************************************************************
 *  QAC Suppression
 *****************************************************************************/
/*PRQA S 303,306,310,342,1257,1258,1503,1532,3412,3453 EOF*/
/*
 * MISRA-C:2004 11.3(303):Cast a pointer to an integral type.
 * To address a register must cast an integral type to a pointer.
 *
 * MISRA-C:2004 11.3(306):Cast a pointer to an integral type.
 * To address a flash must cast an integral type to a pointer.
 *
 * MISRA-C:2004 11.4(310):Casting to different object pointer type.
 *
 * 342:K&R compilers do not support the ISO glue operator '##'.
 *
 * 1257:Suffixed integer constant implicitly converted to smaller integer type
 * on assignment.
 *
 * 1258:Suffixed integer constant explicitly cast to another type.
 *
 * MISRA-C:2004 14.1(1503):The function is defined but is not used within this
 * project.
 * these functions will be used When correlative macro is enable.
 *
 * 1532:The function is only referenced in one translation unit.
 * all functions in this file are configurations in the Security Module.
 *
 * MISRA-C:2004 19.4(3412):Macro defines an unrecognized code-fragment.
 *
 * MISRA-C:2004 19.7(3453):A function could probably be used instead of this
 * function-like macro.
 *
 */


/*****************************************************************************
 *  Internal Macro Definitions
 *****************************************************************************/
#define WDG_CPMUCOP       (*(volatile bl_u8_t *)(0x3Cu))
/* 0x40 disable RTI&COP when debug,
   0x00 enable RTI&COP when debug*/
#define WDG_CPMUCOP_RSBCK_CFG        (0x40u)

#define WDG_CPMUARMCOP    (*(volatile bl_u8_t *)(0x3Fu))


#if (SYS_OSC_CLOCK_FREQ == 1000) /* internal clcok */
/*262ms*/
#define WDG_COPCTL_CR_VALUE     (0x03u)
#elif ((SYS_OSC_CLOCK_FREQ == 4000) || (SYS_OSC_CLOCK_FREQ == 8000))
/*262ms and 131ms*/
#define WDG_COPCTL_CR_VALUE     (0x04u)
#elif ((SYS_OSC_CLOCK_FREQ == 12000) || (SYS_OSC_CLOCK_FREQ == 16000))
/*349ms and 262ms*/
#define WDG_COPCTL_CR_VALUE     (0x05u)
#else
#error "the configurations of watchdog is error."
#endif

#define WDG_ARMCOP_FIRST_VALUE  (0x55u)
#define WDG_ARMCOP_SECOND_VALUE (0xAAu)
#define WDG_ARMCOP_WRONG_VALUE  (0x11u)

#if 0
/*extern the identifiers is generated by the linker.*/
#define __SEG_START_REF(a)  __SEG_START_##a /**/
#define __SEG_END_REF(a)    __SEG_END_##a
#define __SEG_SIZE_REF(a)   __SEG_SIZE_##a
#define __SEG_START_DEF(a)  extern bl_u8_t __SEG_START_REF(a) []
#define __SEG_END_DEF(a)    extern bl_u8_t __SEG_END_REF(a) []
#define __SEG_SIZE_DEF(a)   extern bl_u8_t __SEG_SIZE_REF(a) []

__SEG_START_DEF(WDG_FEED);/*PRQA S 3447,3684*/
#endif

/*****************************************************************************
 *  Function Definitions
 *****************************************************************************/
/**************************************************************************//**
 *
 *  \details Initialize watchdog module.
 *
 *  \return None.
 *
 *  \since V2.0.0
 *
 *****************************************************************************/
void Wdg_Init(void)
{
    //WDG_CPMUCOP = (WDG_COPCTL_CR_VALUE | WDG_CPMUCOP_RSBCK_CFG);
#if (RTE_FUN_COPY_WDG_TO_RAM == BL_FUN_ON)  //條件成立，
    //必須把 3DF0 載入 就是把 Wdg_Feed 載到 3DF0
        #if 0
        Bl_MemCpy((bl_Buffer_t *)RTE_WATCHDOG_FEED_INTERFACE_ADDR,
                    (const bl_Buffer_t *)RTE_WATCHDOG_OPS_FEED,
                    (bl_u32_t)RTE_WATCHDOG_FEED_INTERFACE_SIZE);
        #else
        
        #endif

#else
    wdog_init();
#endif
}

/**************************************************************************//**
 *
 *  \details Feed watchdog.
 *
 *  \return None.
 *
 *  \since V2.0.0
 *
 *****************************************************************************/
static u16 g_test_freewog = 0; 
#if (RTE_FUN_COPY_WDG_TO_RAM == BL_FUN_ON)  //條件成立，
    __attribute__((section(".RAMCODE_WDG1"))) void Wdg_Feed(void)
    {

    }
#else

    void Wdg_Feed(void)
    {
        g_test_freewog++;
        wdog_task();
    }

#endif

/**************************************************************************//**
 *
 *  \details Deinitialize watchdog module.
 *
 *  \return None.
 *
 *  \since V2.0.0
 *
 *****************************************************************************/
void Wdg_Deinit(void)
{
    /*The watchdog of xep100 is not deinitialized.*/
    wdt_disable();
    return ;
}

/**************************************************************************//**
 *
 *  \details Reset watchdog module.
 *
 *  \return None.
 *
 *  \since V2.0.0
 *
 *****************************************************************************/
void Wdg_FastReset(void)
{
    ResetMCUHandle();
    #if 0
    for(;;)
    {
    }
    #endif
}



#if (RTE_FUN_COPY_WDG_TO_RAM == BL_FUN_ON)  //條件成立，
        //#pragma location = "RAMCODE"
    const __IO wdg_Header g_WdgHeader __attribute__((section(".RAMCODE_WDG"))) =
#else
    const __IO wdg_Header g_WdgHeader =
#endif

        {
            //0x1234,
            //0x5678,
            &Wdg_Feed,
            //0x12345678
        };

