/*
 * 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_M011x.h"

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

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

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

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

void Mcu_Init(McuType *obj)
{
    /* Initialize ADC driver */
    
    /* Initialize clock driver */
    ClockDrv_InitParamsType clockInitParams;
    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, RCM, PCC0, SCG0, SCG1, PCC1, SCG2, &clockInitParams);

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

    /* Initialize flexCAN driver */
    FlexCanDrv_InitHwParType initHwPar;
    initHwPar.canRamNum = 2;
    FlexCanDrv_Init(&obj->flexCanDrv, CAN, PCC1, PCC0, SCG0, SCG2, SIM, &initHwPar);

    /* 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, SCG2, SCG1, SCG0);

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

    SpiDrv_Init(&obj->spiDrv0, SPI0);

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