//-----------------------------------------------------------------------------
//
//  Copyright (C) 2004, Motorola Inc. All Rights Reserved
//
//------------------------------------------------------------------------------
//
// Copyright (C) 2004-2007, Freescale Semiconductor, Inc. All Rights Reserved.
// THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
// AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
//
//------------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//
//  File:  bspIrda.c
//
//  Provides BSP-specific configuration routines for the IR port.
//
//-----------------------------------------------------------------------------

#pragma warning(push)
#pragma warning(disable: 4115 4201 4204 4214)
#include <windows.h>
#pragma warning(pop)

#include "bsp.h"
#include "uart.h"

//-----------------------------------------------------------------------------
// External Functions

//-----------------------------------------------------------------------------
// External Variables

//-----------------------------------------------------------------------------
// Defines

//-----------------------------------------------------------------------------
// Types

//-----------------------------------------------------------------------------
// Global Variables

//-----------------------------------------------------------------------------
// Local Variables

//------------------------------------------------------------------------------
// Local Functions

//-----------------------------------------------------------------------------
//
// Function: BSPIrdaEnable
//
// This is a private function to enable the IRDA.
//
// Parameters:
//      bEnable
//          [in]    TRUE if enable IRDA. FALSE if disable.
//
// Returns:
//      none.
//-----------------------------------------------------------------------------
VOID BSPIrdaEnable(BOOL bEnable)
{
    //PCSP_PBC_REGS pPBC;
    //PHYSICAL_ADDRESS phyAddr = {BSP_BASE_REG_PA_PBC_BASE, 0};
    // Map PBC registers to virtual address space
    //pPBC = (PCSP_PBC_REGS) MmMapIoSpace(phyAddr, sizeof(CSP_PBC_REGS), FALSE);
    //if (pPBC == NULL)
    //{
    //    return ;
    //}

    if (bEnable)
    {
        // Enable IRDA transceiver
        //OUTREG16(&pPBC->BCTRL1_CLEAR, (1 << PBC_BCTRL1_CLEAR_IREN_LSH));
    }
    else
    {
        // Disable IRDA transceiver
        //OUTREG16(&pPBC->BCTRL1_SET, (1 << PBC_BCTRL1_SET_IREN_LSH));
    }
    //MmUnmapIoSpace(pPBC,sizeof(CSP_PBC_REGS));
}

//-----------------------------------------------------------------------------
//
// Function: BSPIrdaSetMode
//
// This is a private function to set the IRDA mode.
//
// Parameters:
//      mode
//          [in]    IR mode selection.
//
// Returns:
//      none.
//-----------------------------------------------------------------------------
VOID BSPIrdaSetMode(irMode_c mode)
{
    PHYSICAL_ADDRESS phyAddr = {0, 0};
    //PCSP_PBC_REGS pPBC;
    PCSP_FIRI_REG pFiriReg = NULL;

    // Map PBC registers to virtual address space
    //pPBC = (PCSP_PBC_REGS) MmMapIoSpace(phyAddr, sizeof(CSP_PBC_REGS), FALSE);
    //if (pPBC == NULL)
    //{
     //   return ;
    //}

    switch (mode)
    {
        case SIR_MODE:
            //OUTREG16(&pPBC->BCTRL2_CLEAR, (1 << PBC_BCTRL2_IRDA_MOD_LSH));
            break;
        case MIR_MODE:
        case FIR_MODE:
            // Map FIRI peripheral physical address to virtual address
            phyAddr.QuadPart = CSP_BASE_REG_PA_FIRI;
            pFiriReg = (PCSP_FIRI_REG) MmMapIoSpace(phyAddr,
                                                   sizeof(CSP_FIRI_REG), FALSE);
            if (pFiriReg == NULL)
            {
                return ;
            }
            // IrDA Mode clear
            //OUTREG16(&pPBC->BCTRL2_SET, (1 << PBC_BCTRL2_IRDA_MOD_LSH));
            // Invert TPP bit
            OUTREG32(&pFiriReg->FIRI_TCR,
                     CSP_BITFVAL(FIRI_TCR_TPP, FIRI_TCR_TPP_INVERT));
            // Set the Mode according to the IRDA_TXD
            //OUTREG16(&pPBC->BCTRL2_CLEAR, (1 << PBC_BCTRL2_IRDA_MOD_LSH));
            // Invert the polarity back to original
            OUTREG32(&pFiriReg->FIRI_TCR,
                     CSP_BITFVAL(FIRI_TCR_TPP, FIRI_TCR_TPP_NO_INVERT));
            MmUnmapIoSpace(pFiriReg,sizeof(PCSP_FIRI_REG));
            break;
        default:
            return;
    }

   // MmUnmapIoSpace(pPBC,sizeof(CSP_PBC_REGS));
    return;
}
