/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2023. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
/******************************************************************************
 * @version: V1.0.0 demo
 ******************************************************************************/

/*!
 * @file ecc_sram.c
 *
 * @brief This file contains the user ecc_sram function..
 *
 */

/* ===========================================  Includes  =========================================== */
#include "ecc_sram.h"
#include "ac780x_ecc_sram.h"
#include "ac780x_ecc_sram_reg.h"
#include <string.h>

/* ============================================  Define  ============================================ */
#define ECC_SRAM_CTRL_ERR2_EN_Pos (6UL)    /*!< ERR2_EN (Bit 6)               */
#define ECC_SRAM_CTRL_ERR2_EN_Msk (0x40UL) /*!< ERR2_EN (Bitfield-Mask: 0x01) */

/* ===========================================  Typedef  ============================================ */

/* ==========================================  Variables  =========================================== */
uint8_t g_testData __attribute__ ((section(".ARM.__at_0x20000010")));

/* ====================================  Functions declaration  ===================================== */

/* ======================================  Functions define  ======================================== */
/*!
 * @brief  ECC_SRAM Callback
 *
 * @param[in] device: pwdt module
 * @param[in] wpara: interrupt flag
 * @param[in] lpara: interrupt parameter
 *
 * @return none
 */
void ECC_SRAM_Callback(void *device, uint32_t wpara, uint32_t lpara)
{
    uint32_t errAddr = 0;

    /*SRAM ECC 2-bit error interrupt*/
    if (ECC_SRAM_GetErrStatus() == 1)
    {
        errAddr = 0x20000000 + (ECC_SRAM_Get2BitErrAddr() << 2);

        ECC_SRAM_ClearErrStatus();

        printf("A 2bit error occurred. The address is %x.\r\n", errAddr);
    }
}

/*!
 * @brief   SRAM Error Correcting Code initialization
 *
 * @param[in] none
 *
 * @return  none
 */
void ECC_SRAM_Initalize(void)
{
    ECC_SRAM_ConfigType config;
    memset(&config, 0, sizeof(config));

    /* General parameters */
    config.resetEn     = DISABLE;           /*!< ECC_SRAM 2bit error reset enable/disable */
    config.interruptEn = ENABLE;            /*!< ECC_SRAM interrupt enable/disable */
    config.callBack    = ECC_SRAM_Callback; /* Set ECC_SRAM interrupt callback function */

    ECC_SRAM_Init(&config);
}

/*!
 * @brief   ENABLE or DISABLE SRAM Error Correcting Code
 *
 * @param[in] en:ENABLE or DISABLE ECC_SRAM
 *
 * @return  none
 */
void ECC_SRAM_En(ACTION_Type en)
{
    if (en == ENABLE)
    {
        MODIFY_REG32(ECC_SRAM->CTRL, ECC_SRAM_CTRL_ERR2_EN_Msk, ECC_SRAM_CTRL_ERR2_EN_Pos, 1);
    }
    else
    {
        MODIFY_REG32(ECC_SRAM->CTRL, ECC_SRAM_CTRL_ERR2_EN_Msk, ECC_SRAM_CTRL_ERR2_EN_Pos, 0);
    }
}

/*!
 * @brief   Test ECC SRAM
 *
 * @param[in] none
 *
 * @return  none
 */
void ECC_SRAM_Test(void)
{
    ECC_SRAM_En(DISABLE);
    g_testData = 0x05;
    ECC_SRAM_En(ENABLE);
    g_testData = 0x55;
}

/* =============================================  EOF  ============================================== */
