/******************************************************************************/
/* Williams Advanced Engineering Limited                     Copyright(C) 2021*/
/* Grove                                                  All rights reserved */
/*                                                                            */
/******************************************************************************/

#include "Ios_Digital.h"


/* Digital channel struture array declaration */
DigitalChannelConfigType tDigitalConfig[NUM_DIG_CHANNELS_TOT] =
{
     /* Port   , Direction,     PortPin ID */
     {hetPORT1 , DirOutput,     PIO_SYS_PWR_RELEASE},
     {dmmPORT  , DirOutput,     PIO_LED_1_ID},
     {dmmPORT  , DirOutput,     PIO_LED_2_ID},
     {dmmPORT  , DirOutput,     PIO_LED_3_ID},
     {dmmPORT  , DirOutput,     PIO_EXT_WDG_ID},
     {hetPORT1 , DirOutput,     PIO_GP_HSD_00_ID},
     {hetPORT1 , DirOutput,     PIO_GP_HSD_01_ID},
     {hetPORT1 , DirOutput,     PIO_GP_HSD_02_ID},
     {hetPORT1 , DirOutput,     PIO_GP_HSD_03_ID},
     {hetPORT1 , DirOutput,     PIO_GP_HSD_04_ID},
     {hetPORT1 , DirOutput,     PIO_GP_HSD_05_ID},
     {hetPORT1 , DirOutput,     PIO_GP_HSD_06_ID},
     {hetPORT1 , DirOutput,     PIO_GP_HSD_07_ID},
     {hetPORT1 , DirOutput,     PIO_GP_LSD_00_ID},
     {hetPORT1 , DirOutput,     PIO_GP_LSD_01_ID},
     {hetPORT1 , DirOutput,     PIO_GP_LSD_02_ID},
     {hetPORT1 , DirOutput,     PIO_GP_LSD_03_ID},
     {hetPORT1 , DirOutput,     PIO_CTCR_HSD_00_ID},
     {hetPORT1 , DirOutput,     PIO_CTCR_HSD_01_ID},
     {hetPORT1 , DirOutput,     PIO_CTCR_HSD_02_ID},
     {hetPORT1 , DirOutput,     PIO_CTCR_LSD_00_ID},
     {hetPORT1 , DirOutput,     PIO_CTCR_LSD_01_ID},
     {hetPORT1 , DirOutput,     PIO_CTCR_LSD_02_ID},
     {gioPORTA ,  DirInput,     PIO_CTCR_INT_DET_ID},
     {gioPORTA , DirOutput,     PIO_CTCR_INT_RST_ID},
     {gioPORTB ,  DirInput,     PIO_CTCR_FLT_HS_00_ID},
     {gioPORTA ,  DirInput,     PIO_CTCR_FLT_HS_01_ID},
     {gioPORTA ,  DirInput,     PIO_CTCR_FLT_HS_02_ID},
     {gioPORTB ,  DirInput,     PIO_CTCR_FLT_LS_00_ID},
     {gioPORTB ,  DirInput,     PIO_CTCR_FLT_LS_01_ID},
     {gioPORTB ,  DirInput,     PIO_CTCR_FLT_LS_02_ID},
     {hetPORT1 ,  DirInput,     PIO_DIG_IN_00_ID},
     {hetPORT1 ,  DirInput,     PIO_DIG_IN_01_ID},
     {hetPORT1 ,  DirInput,     PIO_DIG_IN_02_ID},
     {hetPORT1 ,  DirInput,     PIO_DIG_IN_03_ID},
     {hetPORT1 ,  DirInput,     PIO_DIG_IN_04_ID},
     {spiPORT1 ,  DirInput,     PIO_DIG_IN_05_ID},
     {spiPORT1 ,  DirInput,     PIO_DIG_IN_06_ID},
     {spiPORT1 ,  DirInput,     PIO_DIG_IN_07_ID},
     {spiPORT1 ,  DirInput,     PIO_DIG_IN_08_ID},
     {spiPORT1 ,  DirInput,     PIO_DIG_IN_09_ID}
};

void fnIosInitDigitalIOs(void)
{

    fnIosInitDigitalIOsDirection();

    fnIosInitDigitalOutputs();
}

void fnIosInitDigitalIOsDirection(void)
{
    uint8 i;
    uint32 uReg;

    for(i = 0; i< NUM_DIG_CHANNELS_TOT; i++)
    {

        if(tDigitalConfig[i].tPinDir == DirOutput)
            tDigitalConfig[i].McuPort->DIR |= (uint32)(1 << tDigitalConfig[i].PortPinID);
        else
        {
            uReg = tDigitalConfig[i].McuPort->DIR;
            uReg  &= ~((uint32)(1u<<tDigitalConfig[i].PortPinID));
            tDigitalConfig[i].McuPort->DIR =  uReg;
        }
    }
}

void fnIosInitDigitalOutputs(void)
{
    uint8 i;

    for(i = 0; i< NUM_DIG_CHANNELS_TOT; i++)
    {
        if(tDigitalConfig[i].tPinDir == DirOutput)
            gioSetBit(tDigitalConfig[i].McuPort, tDigitalConfig[i].PortPinID, 0);
    }
}

void fnIosSetDigitalChannel(DigitalChannelID_Type tChannelId, uint8 Value)
{
    if(tDigitalConfig[tChannelId].tPinDir == DirOutput)
    {
        gioSetBit(tDigitalConfig[tChannelId].McuPort, tDigitalConfig[tChannelId].PortPinID, Value);
    }
}

uint8 fnIosGetDigitalChannel(DigitalChannelID_Type tChannelId)
{
    uint8 bPinValue = 0;

    if(tDigitalConfig[tChannelId].tPinDir == DirInput)
    {
        bPinValue = gioGetBit(tDigitalConfig[tChannelId].McuPort, tDigitalConfig[tChannelId].PortPinID);
    }

    return(bPinValue);
}

