/*
 * 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 "mcu.h"
#include "drivers/cpu/startup/system_M014x.h"

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

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

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

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

void Mcu_Init(McuType *obj)
{
    /* Initialize clock driver */
    ClockDrv_InitParamsType clockInitParams;
    clockInitParams.fircFreq     = CPU_INT_FAST_CLK_HZ;
    clockInitParams.sircFreq     = CPU_INT_SLOW_CLK_HZ;
    clockInitParams.xtalClkFreq  = CPU_XTAL_CLK_HZ;
    clockInitParams.rtcClkInFreq = 0;
    clockInitParams.tclkFreq[0]  = 0;
    clockInitParams.tclkFreq[1]  = 0;
    clockInitParams.tclkFreq[2]  = 0;
    ClockDrv_Init(&obj->clockDrv, ACG, APC, SCG, PCC, &clockInitParams);

    /* Initialize Flash driver */
    FlashDrv_Init(&obj->flashDrv, FTFC);

    /* Initialize flexCAN driver */
    FlexCanDrv_InitHwParType canInitHwPar;
    canInitHwPar.canInstanceIdx = 0;
    canInitHwPar.canRamNum      = 3;
    FlexCanDrv_Init(&obj->flexCanDrv0, CAN0, PCC, SCG, SIM, &canInitHwPar);
    canInitHwPar.canInstanceIdx = 1;
    canInitHwPar.canRamNum      = 2;
    FlexCanDrv_Init(&obj->flexCanDrv1, CAN1, PCC, SCG, SIM, &canInitHwPar);
    canInitHwPar.canInstanceIdx = 2;
    canInitHwPar.canRamNum      = 1;
    FlexCanDrv_Init(&obj->flexCanDrv2, CAN2, PCC, SCG, SIM, &canInitHwPar);
#if FEATURE_CAN_FD_INSTANCE_NUM == 4
    canInitHwPar.canInstanceIdx = 3;
    canInitHwPar.canRamNum      = 1;
    FlexCanDrv_Init(&obj->flexCanDrv3, CAN3, PCC, SCG, SIM, &canInitHwPar);
#endif
    
    /* Initialize PINS driver */
    PinsDrv_Init(&obj->pta, PORTA, GPIOA);
    PinsDrv_Init(&obj->ptb, PORTB, GPIOB);
    PinsDrv_Init(&obj->ptc, PORTC, GPIOC);
    PinsDrv_Init(&obj->ptd, PORTD, GPIOD);
    PinsDrv_Init(&obj->pte, PORTE, GPIOE);

    /* Initialize POWER driver */
    PowerDrv_Init(&obj->powerDrv, PMC, ACG);

    /* Initialize reset driver */
    ResetDrv_Init(&obj->resetDrv, RCM, PCC, APC);

    SpiDrv_Init(&obj->spiDrv2, SPI2);

    /* Initialize watchdog driver */
    WdgDrv_Init(&obj->wdgDrv, WDG);

    
}
