/*
 * 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)
{
    /* channel control enable default value */
    cfg->chnEn = 0;
    /* channel interrupt enable default value */
    cfg->irqEn = 0;
    /* channel reset enable default value */
    cfg->rstEn = 0;
    /* channel NMI enable default value */
    cfg->nmiEn = 0;
}
 
bool FhuDrv_GetUserLockSts(FhuDrvType *obj)
{
    /* Get user lock status */
    return (bool)FhuReg_GetUserLock(obj->reg);
}
 
void FhuDrv_Config(FhuDrvType *obj, const FhuDrv_ConfigType *cfg)
{
    /* Unlock user register */
    FhuReg_SetUserKey(obj->reg, FHUDRV_USER_KEY_UNLOCK);

    /* Config channel control */
    FhuReg_SetChCtl(obj->reg, cfg->chnEn);
    /* Config interrupt control */
    FhuReg_SetIntCtl(obj->reg, cfg->irqEn);
    /* Config reset control */
    FhuReg_SetRstCtl(obj->reg, cfg->rstEn);
    /* Config NMI control */
    FhuReg_SetNmiCtl(obj->reg, cfg->nmiEn);

    /* Lock user register */
    FhuReg_SetUserKey(obj->reg, ~FHUDRV_USER_KEY_UNLOCK);
}
 
uint16_t FhuDrv_GetChnSts(FhuDrvType *obj)
{
    /* Get channel status record */
    return FhuReg_GetChnRcd(obj->reg);
}

void FhuDrv_ClearChnSts(FhuDrvType *obj, uint16_t clrValue)
{
    /* Unlock user register */
    FhuReg_SetUserKey(obj->reg, FHUDRV_USER_KEY_UNLOCK);
    /* Clear channel status record */
    FhuReg_SetChnRcd(obj->reg, clrValue);
}

uint8_t FhuDrv_AnalogRealTimeChnSts(FhuDrvType *obj)
{
    /* Get analog real time channel status */
    return FhuReg_GetAdRcd(obj->reg);
}

