/*******************************************************************************
 * the includes
 ******************************************************************************/
#include "hwctrl.h"
#include "canuser.h"

/*******************************************************************************
 * the defines
 ******************************************************************************/
 
 
 /*******************************************************************************
 * the typedefs
 ******************************************************************************/
 
 
 /*******************************************************************************
 * the globals
 ******************************************************************************/
 extern McuType mcu;
 uint32_t                     gCpuClockFrequency       = 0;
 
 /*******************************************************************************
 * the const
 ******************************************************************************/
 
 
 
 /*******************************************************************************
 * the functions
 ******************************************************************************/
static void hw_clock_init(void);
static void GPIO_init(void);



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

    WdgDrv_Disable(&mcu.wdgDrv);

    //初始化时钟
    hw_clock_init();


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

    GPIO_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);
    ClockDrv_ConfigureClock(&mcu.clockDrv, CLOCKDRV_PORTD, &clockConfig);
    ClockDrv_ConfigureClock(&mcu.clockDrv, CLOCKDRV_PORTE, &clockConfig);


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

    IrqDrv_EnableIrq(SysTick_IRQn);

    
}

static void GPIO_init(void)
{
    PinsDrv_SetMuxModeSel(&mcu.ptd, 0, PINSDRV_MUX_AS_GPIO);
    PinsDrv_SetPinDirection(&mcu.ptd, 0, 0);
    PinsDrv_SetMuxModeSel(&mcu.ptd, 1, PINSDRV_MUX_AS_GPIO);
    PinsDrv_SetPinDirection(&mcu.ptd, 1, 0);
    PinsDrv_SetMuxModeSel(&mcu.ptd, 2, PINSDRV_MUX_AS_GPIO);
    PinsDrv_SetPinDirection(&mcu.ptd, 2, 0);
    PinsDrv_SetMuxModeSel(&mcu.ptd, 3, PINSDRV_MUX_AS_GPIO);
    PinsDrv_SetPinDirection(&mcu.ptd, 3, 0);
    PinsDrv_SetMuxModeSel(&mcu.ptd, 5, PINSDRV_MUX_AS_GPIO);
    PinsDrv_SetPinDirection(&mcu.ptd, 5, 0);

    PinsDrv_SetMuxModeSel(&mcu.ptc, 1, PINSDRV_MUX_AS_GPIO);
    PinsDrv_SetPinDirection(&mcu.ptc, 1, 0);
    PinsDrv_SetMuxModeSel(&mcu.ptc, 16, PINSDRV_MUX_AS_GPIO);
    PinsDrv_SetPinDirection(&mcu.ptc, 16, 0);
    PinsDrv_SetMuxModeSel(&mcu.ptc, 15, PINSDRV_MUX_AS_GPIO);
    PinsDrv_SetPinDirection(&mcu.ptc, 15, 0);

    //PinsDrv_SetPortInputDisable(&mcu.ptd,0);
}


uint8_t getKeyIO(KEYID_t keyid)
{
    switch (keyid)
    {
    case KEY_KB_F:
        return PinsDrv_ReadPin(&mcu.ptd, 2) == 0 ? 1:0;
    case KEY_KB_R:
        return PinsDrv_ReadPin(&mcu.ptd, 3) == 0 ? 1:0;
    case KEY_HG_F:
        return PinsDrv_ReadPin(&mcu.ptd, 1) == 0 ? 1:0;
    case KEY_HG_R:
        return PinsDrv_ReadPin(&mcu.ptd, 0) == 0 ? 1:0;

    case KEY_TT_U:
        return PinsDrv_ReadPin(&mcu.ptd, 5) == 0 ? 1:0;
    case KEY_TT_D:
        return PinsDrv_ReadPin(&mcu.ptc, 1) == 0 ? 1:0;

    case KEY_ZERO_ON:
        return PinsDrv_ReadPin(&mcu.ptc, 15) == 0 ? 1:0;
    case KEY_ZERO_OFF:
        return PinsDrv_ReadPin(&mcu.ptc, 16) == 0 ? 1:0;
    default:
        return 0;
    }
}

