/*
 * Copyright 2021 MindMotion Microelectronics Co., Ltd.
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "board_init.h"

/*
 * Macros.
 */
#define BKP_REG_NUM 5u
#define BKP_DATA  0x5555

/*
 * Variables.
 */

/*
 * Declerations.
 */
void app_bkp_tamper();

/*
 * Functions.
 */
int main(void)
{
    BOARD_Init();
    RCC_EnableBKPWriteProtect(false);
    app_bkp_tamper();

    while (1)
    {
        /* Invasion event occurs. */
        if (0u != ( BKP_GetStatus(BOARD_DEBUG_BKP_PORT) & BKP_STATUS_TAMP) )
        {
            /* Check whether bkp data is cleared. */
            if (BKP_DATA != BKP_GetData(BOARD_DEBUG_BKP_PORT, BKP_REG_NUM))
            {
                printf("Generate intrusion events.\r\n");
            }
            BKP_ClearStatus(BOARD_DEBUG_BKP_PORT, BKP_STATUS_TAMP);
            BKP_EnableTamperConf(BOARD_DEBUG_BKP_PORT, NULL);
            app_bkp_tamper();
        }
    }
}

void app_bkp_tamper()
{
    BKP_Tamper_Type  bkp_tamper;
    bkp_tamper.TamperLevel = BKP_TamperActLevel_High;
    /* Enable bkp tamper and config tamper. */
    BKP_EnableTamperConf(BOARD_DEBUG_BKP_PORT, &bkp_tamper);
    /* Put data to backup area. */
    BKP_SetData(BOARD_DEBUG_BKP_PORT, BKP_REG_NUM, BKP_DATA);
}

/* EOF. */
