/*
 * Copyright (c) 2022, Shenzhen CVA Innovation CO.,LTD
 * All rights reserved.
 *
 * Shenzhen CVA Innovation CO.,LTD (CVA chip) is supplying this file for use
 * exclusively with CVA's microcontroller products. This file can be freely
 * distributed within development tools that are supporting such microcontroller
 * products.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
 * CVA SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
 * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 */

/*******************************************************************************
 * the includes
 ******************************************************************************/

#include "fhu_drv.h"
#include "fhu_reg.h"

/*******************************************************************************
 * the defines
 ******************************************************************************/

/*******************************************************************************
 * the typedefs
 ******************************************************************************/

/*******************************************************************************
 * the globals
 ******************************************************************************/

/*******************************************************************************
 * the functions
 ******************************************************************************/

void FhuDrv_Init(FhuDrvType *obj, struct _FhuRegType_ *fhuReg)
{
    obj->reg = fhuReg;
}

void FhuDrv_GetDefaultConfig(FhuDrv_ConfigType *cfg)
{
    cfg->chnEn = 0;
    cfg->irqEn = 0;
    cfg->rstEn = 0;
    cfg->nmiEn = 0;
}

bool FhuDrv_GetUserLockSts(FhuDrvType *obj)
{
    return (bool)FhuReg_GetUserLock(obj->reg);
}

void FhuDrv_Config(FhuDrvType *obj, const FhuDrv_ConfigType *cfg)
{
    FhuReg_SetUserKey(obj->reg, FHUDRV_USER_KEY_UNLOCK);

    FhuReg_SetChCtl(obj->reg, cfg->chnEn);
    FhuReg_SetIntCtl(obj->reg, cfg->irqEn);
    FhuReg_SetRstCtl(obj->reg, cfg->rstEn);
    FhuReg_SetNmiCtl(obj->reg, cfg->nmiEn);

    FhuReg_SetUserKey(obj->reg, ~FHUDRV_USER_KEY_UNLOCK);
}

uint16_t FhuDrv_GetChnSts(FhuDrvType *obj)
{
    return FhuReg_GetChnRcd(obj->reg);
}

void FhuDrv_ClearChnSts(FhuDrvType *obj, uint16_t clrValue)
{
    FhuReg_SetUserKey(obj->reg, FHUDRV_USER_KEY_UNLOCK);
    FhuReg_SetChnRcd(obj->reg, clrValue);
}

uint8_t FhuDrv_AnalogRealTimeChnSts(FhuDrvType *obj)
{
    return FhuReg_GetAdRcd(obj->reg);
}
