/** @file dma.h
*   @brief DMA Driver Definition File
*   @date 24.Feb.2014
*   @version 03.08.01
*   
*/

/* (c) Texas Instruments 2009-2014, All rights reserved. */

#ifndef __DMA_H__
#define __DMA_H__

#include "reg_dma.h"


/** @def BLOCK_TRANSFER
*   @brief Alias name for DMA Block transfer
*   @note This value should be used while setting the DMA control packet
*/
#define BLOCK_TRANSFER      1U

/** @def FRAME_TRANSFER
*   @brief Alias name for DMA Frame transfer
*   @note This value should be used while setting the DMA control packet
*/
#define FRAME_TRANSFER      0U

/** @def AUTOINIT_ON
*   @brief Alias name for Auto Initialization ON
*   @note This value should be used while setting the DMA control packet
*/
#define AUTOINIT_ON         1U

/** @def AUTOINIT_OFF
*   @brief Alias name for Auto Initialization OFF
*   @note This value should be used while setting the DMA control packet
*/
#define AUTOINIT_OFF        0U

/** @def ADDR_FIXED
*   @brief Alias name for Fixed Addressing mode
*   @note This value should be used while setting the DMA control packet
*/
#define ADDR_FIXED          0U

/** @def ADDR_INC1
*   @brief Alias name for Post-increment Addressing mode
*   @note This value should be used while setting the DMA control packet
*/
#define ADDR_INC1           1U

/** @def ADDR_OFFSET
*   @brief Alias name for Offset Addressing mode
*   @note This value should be used while setting the DMA control packet
*/
#define ADDR_OFFSET         3U

/** @def INTERRUPT_ENABLE
*   @brief Alias name for Interrupt enable
*   @note @note This value should be used for API argument @a intenable
*/
#define INTERRUPT_ENABLE	1U

/** @def INTERRUPT_DISABLE
*   @brief Alias name for Interrupt disable
*   @note @note This value should be used for API argument @a intenable
*/
#define INTERRUPT_DISABLE	0U


/** @def DMA_GCTRL_BUSBUSY
*   @brief Bit mask for BUS BUSY in GCTRL Register
*   @note @note This value should be used for API argument @a intenable
*/
#define DMA_GCTRL_BUSBUSY	(0x00004000U)

/** @enum dmaREQTYPE
*   @brief DMA TRANSFER Type definitions 
*
*   Used to define DMA transfer type
*/
enum dmaREQTYPE
{
    DMA_HW = 0x0U,  /**<  Hardware trigger  */
    DMA_SW = 0x1U   /**<  Software trigger  */
};


/** @enum dmaCHANNEL
*   @brief DMA CHANNEL definitions 
*
*   Used to define DMA Channel Number
*/
enum dmaCHANNEL
{
    DMA_CH0  = 0x00U,
    DMA_CH1  = 0x01U,
    DMA_CH2  = 0x02U,
    DMA_CH3  = 0x03U,
    DMA_CH4  = 0x04U,
    DMA_CH5  = 0x05U,
    DMA_CH6  = 0x06U,
    DMA_CH7  = 0x07U,
    DMA_CH8  = 0x08U,
    DMA_CH9  = 0x09U,
    DMA_CH10 = 0x0AU,
    DMA_CH11 = 0x0BU,
    DMA_CH12 = 0x0CU,
    DMA_CH13 = 0x0DU,
    DMA_CH14 = 0x0EU,
    DMA_CH15 = 0x0FU,
    DMA_CH16 = 0x10U,
    DMA_CH17 = 0x11U,
    DMA_CH18 = 0x12U,
    DMA_CH19 = 0x13U,
    DMA_CH20 = 0x14U,
    DMA_CH21 = 0x15U,
    DMA_CH22 = 0x16U,
    DMA_CH23 = 0x17U,
    DMA_CH24 = 0x18U,
    DMA_CH25 = 0x19U,
    DMA_CH26 = 0x1AU,
    DMA_CH27 = 0x1BU,
    DMA_CH28 = 0x1CU,
    DMA_CH29 = 0x1DU,
    DMA_CH30 = 0x1EU,
    DMA_CH31 = 0x1FU
};

/** @enum dmaACCESS
*   @brief DMA ACESS WIDTH definitions 
*
*   Used to define DMA access width
*/
typedef enum dmaACCESS
{
 ACCESS_8_BIT   =  0U,
 ACCESS_16_BIT  =  1U,
 ACCESS_32_BIT  =  2U,
 ACCESS_64_BIT  =  3U
}dmaACCESS_t;


/** @enum dmaPRIORITY
*   @brief DMA Channel Priority
*
*   Used to define to which priority queue a DMA channel is assigned to
*/
typedef enum dmaPRIORITY
{
	LOWPRIORITY = 0U,
	HIGHPRIORITY = 1U
}dmaPRIORITY_t;


/** @enum dmaREGION
*   @brief DMA Memory Protection Region
*
*   Used to define DMA Memory Protection Region
*/
typedef enum dmaREGION
{
	DMA_REGION0 = 0U,
	DMA_REGION1 = 1U,
	DMA_REGION2 = 2U,
	DMA_REGION3 = 3U
}dmaREGION_t;


/** @enum dmaRegionAccess
*   @brief DMA Memory Protection Region Access
*
*   Used to define access permission of DMA memory protection regions
*/
typedef enum dmaRegionAccess
{
	FULLACCESS = 0U,
	READONLY   = 1U,
	WRITEONLY  = 2U,
	NOACCESS   = 3U
}dmaRegionAccess_t;


/** @enum dmaInterrupt
*   @brief DMA Interrupt
*
*   Used to define DMA interrupts
*/
typedef enum dmaInterrupt
{
	FTC = 1U,   /**<  Frame transfer complete Interrupt      */
	LFS = 2U,   /**<  Last frame transfer started Interrupt  */
	HBC = 3U,   /**<  First half of block complete Interrupt */
	BTC = 4U    /**<  Block transfer complete Interrupt      */
}dmaInterrupt_t;

/** @struct g_dmaCTRL
*   @brief Interrupt mode globals
*
*/
typedef struct  dmaCTRLPKT
{
    uint32_t SADD;       /* initial source address             */
    uint32_t DADD;       /* initial destination address        */
    uint32_t CHCTRL;     /* next ctrl packet to be trigger + 1 */
    uint32_t FRCNT;      /* frame   count                      */
    uint32_t ELCNT;      /* element count                      */
    uint32_t ELDOFFSET;  /* element destination offset         */
    uint32_t ELSOFFSET;  /* element source offset              */
    uint32_t FRDOFFSET;  /* frame detination offset            */
    uint32_t FRSOFFSET;  /* frame source offset                */
    uint32_t PORTASGN;   /* dma port                           */
    uint32_t RDSIZE;     /* read element size                  */
    uint32_t WRSIZE;     /* write element size                 */
    uint32_t TTYPE;      /* trigger type - frame/block         */
    uint32_t ADDMODERD;  /* addresssing mode for source        */
    uint32_t ADDMODEWR;  /* addresssing mode for destination   */
    uint32_t AUTOINIT;   /* auto-init mode                     */
    uint32_t COMBO;      /* next ctrl packet trigger(Not used) */
} g_dmaCTRL;

typedef volatile struct
{
     
     struct                                     /* 0x000-0x400 */
     {
      uint32_t ISADDR;
      uint32_t IDADDR;
      uint32_t ITCOUNT; 
      uint32_t  rsvd1;    
      uint32_t CHCTRL;
      uint32_t EIOFF;
      uint32_t FIOFF;
      uint32_t  rsvd2;  
     }PCP[32U];

     struct                                     /* 0x400-0x800   */
     {
       uint32_t res[256U];
     } RESERVED;

     struct                                     /* 0x800-0xA00 */
     {
      uint32_t CSADDR;
      uint32_t CDADDR;
      uint32_t CTCOUNT;
      uint32_t  rsvd3; 
     }WCP[32U];

} dmaRAMBASE_t;

#define dmaRAMREG ((dmaRAMBASE_t *)0xFFF80000U)

typedef struct dma_config_reg
{
    uint32_t CONFIG_CHPRIOS;
    uint32_t CONFIG_GCHIENAS;
    uint32_t CONFIG_DREQASI[8U];
    uint32_t CONFIG_FTCINTENAS;
    uint32_t CONFIG_LFSINTENAS;
    uint32_t CONFIG_HBCINTENAS;
    uint32_t CONFIG_BTCINTENAS;
    uint32_t CONFIG_DMAPCR;
    uint32_t CONFIG_DMAMPCTRL;
} dma_config_reg_t;


/**
 * @defgroup DMA DMA
 * @brief Direct Memory Access Controller
 *
 * The DMA controller is used to transfer data between two locations in the memory map in the background
 * of CPU operations. Typically, the DMA is used to:
 * - Transfer blocks of data between external and internal data memories
 * - Restructure portions of internal data memory
 * - Continually service a peripheral
 * - Page program sections to internal program memory
 *
 * Related files:
 * - reg_dma.h
 * - sys_dma.h
 * - sys_dma.c
 *
 * @addtogroup DMA
 * @{
 */
/* DMA Interface Functions */
void dmaEnable(void);
void dmaDisable(void);
void dmaSetCtrlPacket(uint32_t channel, g_dmaCTRL g_dmaCTRLPKT);
void dmaSetChEnable(uint32_t channel,uint32_t type);
void dmaReqAssign(uint32_t channel,uint32_t reqline);
uint32_t dmaGetReq(uint32_t channel);
void dmaSetPriority(uint32_t channel, dmaPRIORITY_t priority);
void dmaEnableInterrupt(uint32_t channel, dmaInterrupt_t inttype);
void dmaDisableInterrupt(uint32_t channel, dmaInterrupt_t inttype);
void dmaDefineRegion(dmaREGION_t region, uint32_t start_add, uint32_t end_add);
void dmaEnableRegion(dmaREGION_t region, dmaRegionAccess_t access, boolean intenable);
void dmaDisableRegion(dmaREGION_t region);
void dmaEnableParityCheck(void);
void dmaDisableParityCheck(void);
void dmaGetConfigValue(dma_config_reg_t *config_reg, config_value_type_t type);

/** @fn void dmaGroupANotification(dmaInterrupt_t inttype, uint32_t channel)
*   @brief Interrupt callback
*   @param[in] inttype  Interrupt type
*                        - FTC
*                        - LFS
*                        - HBC
*                        - BTC
*   @param[in] channel  channel number 0..15
* This is a callback that is provided by the application and is called apon
* an interrupt.  The parameter passed to the callback is a copy of the 
* interrupt flag register.
*/
void dmaGroupANotification(dmaInterrupt_t inttype, uint32_t channel);

/**@}*/
#endif
