/*  ============================================================================
 *   Copyright (c) Texas Instruments Inc 2002, 2003, 2004, 2005
 *
 *   Use of this software is controlled by the terms and conditions found in the
 *   license agreement under which this software has been supplied.
 *   ===========================================================================
 */

/** @file csl_dmaxHwSetupRaw.c
 *
 *    @brief File for functional layer of CSL API @a CSL_dmaxHwSetupRaw()
 *
 *  Description
 *    - The @a CSL_dmaxHwSetupRaw() function definition & it's associated
 *      functions
 *
 *  Modification 1
 *    - Modified on: 11/March/2005
 *    - Reason: created the sources
 * 
 *  @author asr.
 */

#include <csl_dmax.h>

/** ============================================================================
 *   @n@b CSL_dmaxHwSetupRaw
 *
 *   @b Description
 *   @n This function initializes the device registers with the register-values
 *      provided through the Config Data structure. This configures registers  
 *		based on a structure of register values, as compared to HwSetup, 
 *		which configures registers based on structure of bit field values 
 *		and may perform other functions (delays, etc.) 
 *
 *   @b Arguments
 *   @verbatim
 *          hDmax       Handle to the DMAX  
 * 
 *          config      Pointer to config structure
 *   @endverbatim
 *
 *   <b> Return Value </b>  CSL_Status
 *   @li                    CSL_SOK             - Configuration successful
 *   @li                    CSL_ESYS_BADHANDLE  - Invalid handle
 *   @li                    CSL_ESYS_INVPARAMS  - Configuration is not
 *                                                properly initialized
 *
 *   <b> Pre Condition </b>
 *   @n  Both CSL_dmaxInit() and a CSL_dmaxOpen() must be called
 *
 *
 *   <b> Post Condition </b>
 *   @n  The registers of the specified DMAX instance will be setup
 *       according to value passed.
 *
 *   @b Modifies
 *   @n Hardware registers of the specified DMAX instance.
 *
 *   @b Example
 *   @verbatim
        CSL_DmaxHandle       hDmax;
        CSL_DmaxConfig       config = CSL_DMAX_CONFIG_DEFAULTS;
        CSL_Status           status;

        status = CSL_dmaxHwSetupRaw (hDmax, &config);

     @endverbatim
 * ===========================================================================
 */

#pragma CODE_SECTION (CSL_dmaxHwSetupRaw, ".text:csl_section:dmax");

CSL_Status CSL_dmaxHwSetupRaw (
    CSL_DmaxHandle 	hDmax,
    CSL_DmaxConfig 	*config
)
{
    Uint8	*tmpPtr;
    Uint16	flag;
    Uint32	tmpReg;
    Uint32	val;
    Uint32	mask;
    CSL_Status	status;
    CSL_DmaxParameterEntry  *pEntry;

    if (hDmax == NULL)
        return CSL_ESYS_BADHANDLE;

    if (config == NULL)
        return CSL_ESYS_INVPARAMS;

    if ((Int) hDmax != CSL_ESYS_BADHANDLE) {

        flag = hDmax->eventUid & 0xFFFF;
        mask = 1u << flag;
        tmpReg = hDmax->regs->DEPR;
        val = (config->polarity == CSL_DMAX_POLARITY_RISING_EDGE) << flag;
        tmpReg |= val;
        hDmax->regs->DEPR = tmpReg;

        if (config->priority == CSL_DMAX_EVENT_HI_PRIORITY) {
            *(hDmax->hiTableEventEntryPtr) = config->eventCtrl;
            *(hDmax->loTableEventEntryPtr) = 0;

            hDmax->regs->DEHPR = mask;
            tmpPtr = (Uint8 *) (&(hDmax->regs->HiMaxParam));
        }
        else {
            *(hDmax->loTableEventEntryPtr) = config->eventCtrl;
            *(hDmax->hiTableEventEntryPtr) = 0;

            hDmax->regs->DELPR = mask;
            tmpPtr = (Uint8 *) (&(hDmax->regs->LoMaxParam));
        }

        /* ksb Nov. 12, 2004:
         *  The paramter table entry is actually 32 bits, but the paramPtr 
         *  offset is given in terms of dmax word offset from start of 
         *  Parameter Table. To get correct address, pEntry is defined as 
         *  Uint8 * but data will be acessed as 32 bit.
         */

        tmpPtr += (hDmax->paramPtr) * CSL_DMAX_WORD_SIZE;
        pEntry = (CSL_DmaxParameterEntry *) tmpPtr;
        *pEntry = *config->paramEntry;

        status = CSL_SOK;
    }
    else {
        status = CSL_ESYS_BADHANDLE;
    }

    return status;
}
