/*********************************************************************
 *
 *                  DMA API implementation file
 *
 *********************************************************************
 * FileName:        _dma_include.c
 * Dependencies:	Generic.h
 * 					Dma.h
 * 					INT.h
 *
 * Processor:       PIC32
 *
 * Complier:        MPLAB C32
 *                  MPLAB IDE
 * Company:         Microchip Technology, Inc.
 *
 * Software License Agreement
 *
 * The software supplied herewith by Microchip Technology Incorporated
 * (the Company) for its PIC32/PIC24F Microcontroller is intended
 * and supplied to you, the Companys customer, for use solely and
 * exclusively on Microchip PIC32/PIC24F Microcontroller products.
 * The software is owned by the Company and/or its supplier, and is
 * protected under applicable copyright laws. All rights are reserved.
 * Any use in violation of the foregoing restrictions may subject the
 * user to criminal sanctions under applicable laws, as well as to
 * civil liability for the breach of the terms and conditions of this
 * license.
 *
 * THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 *
 * $Id:$
 * $Name$
 *
 ********************************************************************/

#ifndef _DMAINCLUDE_H_
#define	_DMAINCLUDE_H_

#include <peripheral/generic.h>
#include <peripheral/dma.h>


#ifdef _DMA_CHANNELS



// NOTE: make sure this structure layout is the same as the physical layout of a channel registers!
// update DmaChnRegIx enumeration if the layout changes!


typedef struct
{
	// DCHxCON
	__DCH0CONbits_t	con;
	UINT		conClr;
	UINT		conSet;
	UINT		conInv;
	// DCHxECON
	__DCH0ECONbits_t econ;
	UINT 		econClr;
	UINT 		econSet;
	UINT 		econInv;
	// DCHxINT
	__DCH0INTbits_t	intr;
	UINT		intrClr;
	UINT		intrSet;
	UINT		intrInv;
	// DCHxSSA
	UINT		ssa;
	UINT		ssaClr;
	UINT		ssaSet;
	UINT		ssaInv;
	// DCHxDSA
	UINT		dsa;
	UINT		dsaClr;
	UINT		dsaSet;
	UINT		dsaInv;
	// DCHxSSIZ
	UINT		ssiz;
	UINT		ssizClr;
	UINT		ssizSet;
	UINT		ssizInv;
	// DCHxDSIZ
	UINT		dsiz;
	UINT		dsizClr;
	UINT		dsizSet;
	UINT		dsizInv;
	// DCHxSPTR
	UINT		sptr;
	UINT		sptrClr;
	UINT		sptrSet;
	UINT		sptrInv;
	// DCHxDPTR
	UINT		dptr;
	UINT		dptrClr;
	UINT		dptrSet;
	UINT		dptrInv;
	// DCHxCSIZ
	UINT		csiz;
	UINT		csizClr;
	UINT		csizSet;
	UINT		csizInv;
	// DCHxCPTR
	UINT		cptr;
	UINT		cptrClr;
	UINT		cptrSet;
	UINT		cptrInv;
	// DCHxDAT
	UINT		dat;
	UINT		datClr;
	UINT		datSet;
	UINT		datInv;
}DmaRegMap;		// layout of the DMA registers





#define		DMA_EVENT_MASK		(_DCH0ECON_CHAIRQ_MASK|_DCH0ECON_CHSIRQ_MASK|_DCH0ECON_PATEN_MASK|_DCH0ECON_SIRQEN_MASK|_DCH0ECON_AIRQEN_MASK)
								// mask in DCHxECON that preserves just the events and deletes the CFORCE and CABORT

#define		DMA_IFLAG_MASK		(_DCH0INT_CHERIF_MASK|_DCH0INT_CHTAIF_MASK|_DCH0INT_CHCCIF_MASK|_DCH0INT_CHBCIF_MASK| \
								_DCH0INT_CHDHIF_MASK|_DCH0INT_CHDDIF_MASK|_DCH0INT_CHSHIF_MASK|_DCH0INT_CHSDIF_MASK)
								// mask in DCHxINT that keeps just the int flags, deletes the IE bits

// local data




extern volatile DmaRegMap* const DmaMapTbl;
// dma register map table


// local inline functions
// TODO : replace with proper, system wide, conversion routines
/*********************************************************************
 * Function:        unsigned int VirtToPhys(const void* p)
 *
 * PreCondition:    None
 *
 * Input:			p	- pointer to be converted.
 *
 * Output:          a physical address corresponding to the virtual input pointer
 *
 * Side Effects:    None
 *
 * Overview:		Virtual to physical translation helper.
 *
 * Note:            None.
 ********************************************************************/
extern __inline__ unsigned int __attribute__((always_inline)) VirtToPhys(const void* p)
{
	return (int)p<0?((int)p&0x1fffffffL):(unsigned int)((BYTE*)p+0x40000000L);
}

// TODO : replace with proper, system wide, conversion routines
/*********************************************************************
 * Function:        void* PhysToVirtK0(unsigned int a)
 *
 * PreCondition:    None
 *
 * Input:			a	- address to be converted.
 *
 * Output:          a virtual pointer corresponding to the physical input address
 *
 * Side Effects:    None
 *
 * Overview:		Physical to virtual translation helper.
 *
 * Note:            None.
 ********************************************************************/
extern __inline__ void* __attribute__((always_inline)) PhysToVirtK0(unsigned int a)
{
	return a<0x40000000L?(void*)(a|0x80000000L):(void*)(a-0x40000000L);
}

// TODO : replace with proper, system wide, conversion routines
/*********************************************************************
 * Function:        void* PhysToVirtK1(unsigned int a)
 *
 * PreCondition:    None
 *
 * Input:			a	- address to be converted.
 *
 * Output:          a virtual pointer corresponding to the physical input address
 *
 * Side Effects:    None
 *
 * Overview:		Physical to virtual translation helper.
 *
 * Note:            None.
 ********************************************************************/
extern __inline__ void* __attribute__((always_inline)) PhysToVirtK1(unsigned int a)
{
	return a<0x40000000L?(void*)(a|0xa0000000L):(void*)(a-0x40000000L);
}


/*********************************************************************
 * Function:        void mDmaChnInitOp(DmaRegMap* pReg)
 *
 * PreCondition:    None
 *
 * Input:		None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:		Aborts the current operation on the specified channel.
 * 			Makes sure the DMA controller is on and running
 *
 * Note:            None.
 ********************************************************************/
#define	mDmaChnInitOp(pReg)	do{(pReg)->econSet=_DCH0ECON_CABORT_MASK; DMACONCLR=_DMACON_SUSPEND_MASK; DMACONSET=_DMACON_ON_MASK; \
						while((pReg)->con.CHEN);}while(0)



#endif	// _DMA_CHANNELS

#endif	// _DMAINCLUDE_H_

