/*  ============================================================================
 *   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_dmaxClose.c
 *
 *  @brief File for functional layer of CSL API @a CSL_dmaxClose()
 *
 *  Description
 *    - The @a CSL_dmaxClose() function definition & it's associated functions
 *
 *  Modification 1
 *    - Created on: 11/March/2005
 *    - Reason: created the sources
 * 
 *    @author asr.
 */

#include <csl_dmax.h>
extern CSL_DmaxResourceAlloc CslDmaxAlloc;
/** ============================================================================
 *   @n@b CSL_dmaxClose
 *
 *   @b Description
 *   @n This function closes the specified instance of DMAX.
 *
 *   @b Arguments
     @verbatim
            hDmax Handle to the DMAX instance
     @endverbatim  
 *     
 *   <b> Return Value </b> CSL_Status 
 *   @li               CSL_SOK  - DMAX close successful 
 *   @li               CSL_ESYS_BADHANDLE - Invalid handle
 *     
 *      
 *   <b> Pre Condition </b>
 *   @n  dmax must have opened properly
 *
 *   <b> Post Condition </b>
 *   @n  CSL for the DMAX instance is closed
 *
 *   @b Modifies
 *   @n CSL_DmaxObj structure values for the instance
 * 
 *   @b Example
     @verbatim
     
       CSL_dmaxClose (hDmax);
       
     @endverbatim  
 * =============================================================================
 */

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

CSL_Status CSL_dmaxClose (
    CSL_DmaxHandle hDmax
)
{
    CSL_Status		status;
    Uint32			ndx;
    Uint32			flag;
    CSL_DmaxAlloc	*allocPtr;

    if ((Int) hDmax != NULL) {
        ndx = (((hDmax->eventUid) >> 28) & 0xFFFF) - 1;
        flag = ((hDmax->eventUid) & 0xFFFF);
        allocPtr = (CSL_DmaxAlloc *) (&CslDmaxAlloc);
        allocPtr[ndx].allocMap &= (~(1 << flag));
        ndx = (((hDmax->paramUid) >> 28) & 0xFFFF) - 1;
        flag = ((hDmax->paramUid) & 0xFFFF);
        allocPtr[ndx].allocMap &= (~(1 << flag));
        hDmax->regs = (CSL_DmaxRegsOvly) NULL;
        hDmax->perNum = (CSL_InstNum) - 1;
        status = CSL_SOK;
    }
    else {
        status = CSL_ESYS_BADHANDLE;
    }

    return status;
}
