/*******************************************************************************
 * the includes
 ******************************************************************************/
#include "hwctrl.h"
#include "canuser.h"
#include "TLE94x1.h"
/*******************************************************************************
 * the defines
 ******************************************************************************/
 
 
 /*******************************************************************************
 * the typedefs
 ******************************************************************************/
 
 
 /*******************************************************************************
 * the globals
 ******************************************************************************/
 extern McuType mcu;
 uint32_t                     gCpuClockFrequency       = 0;
 
 /*******************************************************************************
 * the const
 ******************************************************************************/
 
 
 
 /*******************************************************************************
 * the functions
 ******************************************************************************/
static void hw_clock_init(void);

void hw_init(void)
{
    /* Initialize all MCU drivers: flash drv included */
    Mcu_Init(&mcu);

    WdgDrv_Disable(&mcu.wdgDrv);

    //初始化时钟
    hw_clock_init();

    SBC_SPI_INIT();

    

    /* get CAN controller default configuration */
    FlexCanBoot_Init();

    SBC_Init();
}


static void hw_clock_init(void)
{
    /* Setup the clock */
    ClockDrv_ModuleClkConfigType clockConfig;

    /* Setup the Pll div2 clock */
    clockConfig.gating = true;
    clockConfig.source = CLOCKDRV_PLL;
    clockConfig.div    = 1;
    ClockDrv_ConfigureClock(&mcu.clockDrv, CLOCKDRV_PLL_DIV, &clockConfig);

    /* Enable the clock for all port peripheral */
    clockConfig.gating = true;
    clockConfig.div    = 1;
    ClockDrv_ConfigureClock(&mcu.clockDrv, CLOCKDRV_PORTA, &clockConfig);
    ClockDrv_ConfigureClock(&mcu.clockDrv, CLOCKDRV_PORTB, &clockConfig);
    ClockDrv_ConfigureClock(&mcu.clockDrv, CLOCKDRV_PORTC, &clockConfig);

    /* Setup the Pll div2 clock */
    clockConfig.gating = true;
    clockConfig.source = CLOCKDRV_PLL;
    clockConfig.div    = 1;
    ClockDrv_ConfigureClock(&mcu.clockDrv, CLOCKDRV_PLL_DIV, &clockConfig);

    clockConfig.gating = true;
    clockConfig.source = CLOCKDRV_PLL;
    clockConfig.div    = 2;
    ClockDrv_ConfigureClock(&mcu.clockDrv, CLOCKDRV_PLL_DIV, &clockConfig);

    /* Setup the SPI clock */
    clockConfig.gating = true;
    clockConfig.source = CLOCKDRV_PLL_DIV;
    ClockDrv_ConfigureClock(&mcu.clockDrv, CLOCKDRV_SPI0, &clockConfig);

    uint32_t tTcr =  SpiReg_GetTcr((const SpiRegType *)&mcu.spiDrv0.reg);
    SpiDrv_SetPrescaler(&tTcr,0x02);

    /* Set system tick clock, 1ms event */
    ClockDrv_GetFreq(&mcu.clockDrv, CLOCKDRV_CORE, &gCpuClockFrequency);
    SysTick_Config(gCpuClockFrequency / 1000u);

    IrqDrv_EnableIrq(SysTick_IRQn);
}
    