/*
 * 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 <stddef.h>
#include "mft_cap_drv.h"

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

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

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

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

void MftCapDrv_Init(MftCapDrvType *obj, MftDrvType *base)
{
    obj->base = base;

    uint8_t i = 0;
    for(i = 0; i < MFT_CHANNEL_NUM; ++i)
    {
        MftCapCh_Init(&obj->channel[i], obj);
    }
}

void MftCapDrv_GetDefaultConfig(MftCapDrv_ConfigType *userConfig)
{
    userConfig->maxCountValue = 65535;
}

void MftCapDrv_Config(MftCapDrvType *obj, const MftCapDrv_ConfigType *userConfig)
{
    /* WPDIS = 1, disable write protection */
    MftReg_SetModeWpDis(obj->base->reg, 1);

    /* CNTIN = 0 */
    MftReg_SetCntInCntInit(obj->base->reg, 0U);
    /* Set MOD */
    MftReg_SetModuloMod(obj->base->reg, userConfig->maxCountValue);
    /* CPWMS = 0 */
    MftReg_SetScCPwms(obj->base->reg, 0);

    /* WPEN = 1, enable write protection */
    MftReg_SetFmsWpEn(obj->base->reg, 1);
}
