/*********************************************************************
 *
 *                  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


#define		DMA_ABORT_NOT_HONORED		// in the current release, a DMA abort is not honored if the DMA is suspended or the channel is disabled



// 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 DmaChnInitOp(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.
 ********************************************************************/
extern __inline__ void __attribute__((always_inline)) DmaChnInitOp(volatile DmaRegMap* pReg)
{
#ifdef DMA_ABORT_NOT_HONORED

	int wasOff=(pReg)->con.CHEN==0;
      	pReg->econSet=_DCH0ECON_CABORT_MASK;	// abort
      	DMACONCLR=_DMACON_SUSPEND_MASK;		// unsuspend
	DMACONSET=_DMACON_ON_MASK;		// make sure it's on
	if(wasOff)
	{
		pReg->conSet=_DCH0CON_CHEN_MASK;
		pReg->econSet=_DCH0ECON_CABORT_MASK;	// abort channel
	}
	while(pReg->con.CHEN);			// wait operation abort

#else
	pReg->econSet=_DCH0ECON_CABORT_MASK;	// abort channel
      	DMACONCLR=_DMACON_SUSPEND_MASK;		// unsuspend
       	DMACONSET=_DMACON_ON_MASK;		// make sure it's on
	while(pReg->con.CHEN);			// wait operation abort

#endif //DMA_ABORT_NOT_HONORED
}

/*********************************************************************
 * Function:        void DmaChnAbortOp(DmaRegMap* pReg)
 *
 * PreCondition:    None
 *
 * Input:		None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:		Aborts the current operation on the specified channel.
 * 			Does not turn on the DMA controller, does not unsuspend
 *
 * Note:            None.
 ********************************************************************/
extern __inline__ void __attribute__((always_inline)) DmaChnAbortOp(volatile DmaRegMap* pReg)
{

#ifdef DMA_ABORT_NOT_HONORED

	if(DMACONbits.SUSPEND)
	{	// abort will be ignored and we don't want to unsuspend the channel...
		// the least we can do
		pReg->con.w=0;		// turn the channel off
		pReg->ssa=0;
		pReg->dsa=0;		// clear the source and dest pointers
	}
	else
	{
		int wasOff=(pReg)->con.CHEN==0;		// remember the channel status
	      	pReg->econSet=_DCH0ECON_CABORT_MASK;	// abort channel
	       	if(wasOff)
		{
			pReg->conSet=_DCH0CON_CHEN_MASK;	//  turn on so the abort takes place
			pReg->econSet=_DCH0ECON_CABORT_MASK;	// abort channel
		}
	       	while(pReg->con.CHEN);			// wait abort
	}
#else

	pReg->econSet=_DCH0ECON_CABORT_MASK;		// abort
       	while(pReg->con.CHEN);				// wait abort

#endif //DMA_ABORT_NOT_HONORED

}

/*********************************************************************
 * Function:        void DmaChnOpenOp(DmaRegMap* pReg)
 *
 * PreCondition:    None
 *
 * Input:		None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:		Aborts the current operation on the specified channel.
 * 			Turns on the DMA controller but does not perform unsuspend
 *
 * Note:            None.
 ********************************************************************/
extern __inline__ void __attribute__((always_inline)) DmaChnOpenOp(volatile DmaRegMap* pReg)
{

#ifdef DMA_ABORT_NOT_HONORED

	if(DMACONbits.SUSPEND)
	{	// abort wil be ignored and we don't want to unsuspend the channel...
		// the least we can do
		pReg->con.w=0;		// turn the channel off
		pReg->ssa=0;
		pReg->dsa=0;		// clear the source and dest pointers

		DMACONSET=_DMACON_ON_MASK;		// make sure DMA is on
	}
	else
	{
		int wasOff=(pReg)->con.CHEN==0;		// remember the channel status
	      	pReg->econSet=_DCH0ECON_CABORT_MASK;	// abort channel
		DMACONSET=_DMACON_ON_MASK;		// make sure it's on
	       	if(wasOff)
		{
			pReg->conSet=_DCH0CON_CHEN_MASK;	//  turn on so the abort takes place
			pReg->econSet=_DCH0ECON_CABORT_MASK;	// abort channel
		}
	       	while(pReg->con.CHEN);			// wait abort
	}
#else

	pReg->econSet=_DCH0ECON_CABORT_MASK;		// abort
       	DMACONSET=_DMACON_ON_MASK;			// make sure it's on
	while(pReg->con.CHEN);				// wait abort

#endif //DMA_ABORT_NOT_HONORED

}




#endif	// _DMA_CHANNELS

#endif	// _DMAINCLUDE_H_

