/*******************************************************************************
 * Copyright (c) 2018-2021 Renesas Electronics Corporation. All rights reserved.
 *
 *     RENESAS ELECTRONICS CONFIDENTIAL AND PROPRIETARY
 *
 * This software is provided as reference/sample code under the license
 * agreement between Renesas Electronics Corporation and licensee (the
 * "License Agreement") and shall be treated as specified in the License
 * Agreement.
 * These instructions, statements, and software are the confidential
 * information of Renesas Electronics Corporation. They must be used and
 * modified solely for the purpose for which it was furnished by Renesas
 * Electronics Corporation. All or part of these instructions, statements and
 * software must not be reproduced nor disclosed to any third party in any
 * form, unless permitted by the License Agreement.
 *
 * THIS SOFTWARE IS PROVIDED BY RENESAS ELEOCTRONICS CORPORATION "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
 * SATISFACTORY QUALITY, ACCURACY, TITLE AND NON-INFRINGEMENT ARE DISCLAIMED.
 * IN NO EVENT SHALL RENESAS ELECTRONICS CORPORATION BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, PUNITIVE, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 ******************************************************************************/

/*******************************************************************************
 * DESCRIPTION   : window watchdog timer driver
 ******************************************************************************/
/******************************************************************************
 * @file          wdt.c
 * - Version      : 0.01
 * @brief         Window Watchdog Timer driver
 * .
 *****************************************************************************/
/******************************************************************************
 * History : DD.MM.YYYY Version  Description
 *         : 03.07.2020 0.01     First Release
 *****************************************************************************/

#include <stdint.h>
#include <wdt.h>
#include <mem_io.h>


#define ICUMX_WDTA0_BASE    (0xFFFEE080U)   /* Watchdog Timer base */
#define ICUMX_WDTA0WDTE     (ICUMX_WDTA0_BASE)
#define ICUMX_WDTA0EVAC     (ICUMX_WDTA0_BASE+0x0004U)
#define ICUMX_WDTA0REF      (ICUMX_WDTA0_BASE+0x0008U)
#define ICUMX_WDTA0MD       (ICUMX_WDTA0_BASE+0x000CU)


#define WDTA0MD_WDTA0WIE    (uint8_t)(1U<<3U)   /* Enables the 75% interrupt request INTWDTA0 */
#define WDTA0MD_WDTA0ERM    (uint8_t)(1U<<2U)   /* 0:NMI request mode 1:Reset mode */
#define WDTA0MD_WDTA0WS10   (3U)        /* 11B: window-open period is 100% */

/* overflow time setting */
#define WDT_11MS        (uint8_t)(0x00U)
#define WDT_23MS        (uint8_t)(0x01U)
#define WDT_46MS        (uint8_t)(0x02U)
#define WDT_93MS        (uint8_t)(0x03U)
#define WDT_187MS       (uint8_t)(0x04U)
#define WDT_374MS       (uint8_t)(0x05U)
#define WDT_749MS       (uint8_t)(0x06U)
#define WDT_1498MS      (uint8_t)(0x07U)

/* Activation code */
#define WDT_ACT_CODE        (0xACU)


/* Initialization Window Watchdog Timer */
void wdt_init(void)
{
    uint8_t wdta_val;

    /* This API is executed before copying a part of Loader to Local RAM. */
    /* Therefore, this API can not use the Memory mapped I/O API.         */
    /* When reading or writing memory, execute the same processing as     */
    /* Memory mapped I/O API in this function.                    */
    wdta_val = (WDTA0MD_WDTA0ERM /* NMI request mode */
             | WDTA0MD_WDTA0WIE /* Enables the 75% interrupt request INTWDTA0 */
             | WDTA0MD_WDTA0WS10
             | (uint8_t)(WDT_187MS << 4U)); /* overflow interval time */
    *(volatile uint8_t*)ICUMX_WDTA0MD = wdta_val;
}
/* End of function wdt_init(void) */

void wdt_restart(void)
{
    uint8_t reg;
    reg = mem_read8(ICUMX_WDTA0REF);
    mem_write8(ICUMX_WDTA0EVAC, (WDT_ACT_CODE - reg));
}
/* End of function wdt_restart(void) */
