/***********************************************************************
 * Copyright (c)  2017 - 2021, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : dma.c
 * Description : dma driver header file
 * Author(s)   : Will
 * version     : V1.0
 * Modify date : 2021-9-18
 ***********************************************************************/
#ifndef __DMA_H__
#define __DMA_H__

#include  "um321xF.h"

#define DMA_Channel_0   0
#define DMA_Channel_1   1
#define DMA_Channel_2   2
#define DMA_Channel_3   3
#define DMA_Channel_4   4
#define DMA_Channel_5   5
#define DMA_Channel_6   6
#define DMA_Channel_7   7

#define DMAx_BASE_ADDR(x)								(0x40020000 + 0x20 * (x)) 
#define REG_DMA_SRCADDRC(x)								(*(volatile uint32_t *)(DMAx_BASE_ADDR(x) + 0x00))		//通道源地址寄存器
#define REG_DMA_DSTADDRC(x)								(*(volatile uint32_t *)(DMAx_BASE_ADDR(x) + 0x04))		//通道目的地址寄存器
#define REG_DMA_CHCTRLC(x)								(*(volatile uint32_t *)(DMAx_BASE_ADDR(x) + 0x08))		//通道控制信息寄存器 
#define REG_DMA_CHSTSC(x)								(*(volatile uint32_t *)(DMAx_BASE_ADDR(x) + 0x0C))		//通道传送状态寄存器
#define REG_DMA_SPEC(x)								    (*(volatile uint32_t *)(DMAx_BASE_ADDR(x) + 0x10))		//通道源设备选择寄存器
#define REG_DMA_DPEC(x)								    (*(volatile uint32_t *)(DMAx_BASE_ADDR(x) + 0x14))		//通道目的设备选择寄存器
	   
/********* DMA源地址和目的地址传输数据位宽 **********/
#define DMA_TR_WIDTH_8									(0<<30)			//8位数据位宽
#define DMA_TR_WIDTH_16									(1<<30)			//16位数据位宽	
#define DMA_TR_WIDTH_32									((uint32_t)2<<30)			//32位数据位宽

/******** DMA传输模式 *************/
#define DMA_MEM_TO_MEM                  				(0<<12)			//memory to memory
#define DMA_MEM_TO_PERIP                				(1<<12)			//memory to peripheral
#define DMA_PERIP_TO_MEM                				(2<<12)			//peripheral to memory
#define DMA_PERIP_TO_PERIP              				(3<<12)			//peripheral to peripheral

/***********SRC_INC**********/
#define DMA_SINC_INC		            				(1<<1)			//源地址递增	
#define DMA_SINC_DEC		            				(2<<1)			//源地址递减		
#define DMA_SINC_NOC		            				(0<<1)			//源地址不变	
				
/***********DST_INC**********/																				
#define DMA_DINC_INC		            				(1<<3)			//目的地址递增		
#define DMA_DINC_DEC		            				(2<<3)			//目的地址递减 
#define DMA_DINC_NOC		            				(0<<3)			//目的地址不变

/***********源外设硬件握手信号SRC_PER**********/
#define DMA_SRC_PER_SPI0_RX								(1<<0)			//SPI0接收
#define DMA_SRC_PER_SPI1_RX								(3<<0)			//SPI1接收
#define DMA_SRC_PER_UART1_RX							(5<<0)			//UART1接收
#define DMA_SRC_PER_ADC_RX								(6<<0)			//ADC接收
#define DMA_SRC_PER_GPIO0_GTIMER						(7<<0)			//GPIO0_GTIMER中断信号
#define DMA_SRC_PER_LPTIMER3_RX2						(8<<0)			
#define DMA_SRC_PER_LPTIMER3_RX1						(9<<0)			
#define DMA_SRC_PER_LPTIMER2_RX2						(10<<0)			
#define DMA_SRC_PER_LPTIMER2_RX1						(11<<0)			
#define DMA_SRC_PER_LPTIMER1_RX2						(12<<0)			
#define DMA_SRC_PER_LPTIMER1_RX1						(13<<0)			
#define DMA_SRC_PER_LPTIMER0_RX2						(14<<0)			
#define DMA_SRC_PER_LPTIMER0_RX1						(15<<0)			
#define DMA_SRC_PER_Atimer_CTU							(16<<0)			
#define DMA_SRC_PER_Atimer_CC4							(17<<0)			
#define DMA_SRC_PER_Atimer_CC3							(18<<0)			
#define DMA_SRC_PER_Atimer_CC2							(19<<0)			
#define DMA_SRC_PER_Atimer_CC1							(20<<0)			

#define DMA_SRC_PER_MEMORY								(0<<0)


/***********目的外设硬件握手信号DST_PER**********/
#define DMA_DST_PER_SPI0_TX								(0<<0)			//SPI0发送
#define DMA_DST_PER_SPI1_TX								(2<<0)			//SPI1发送
#define DMA_DST_PER_UART1_TX							(4<<0)			//UART1发送
#define DMA_DST_PER_GPIO0_GTIMER					    (7<<0)			//GPIO0_GTIMER中断信号

#define DMA_DST_PER_MEMORY								(0<<0)

#define DMA_DRC_PER_Atimer_CTU							(16<<0)			
#define DMA_DRC_PER_Atimer_CC4							(17<<0)			
#define DMA_DRC_PER_Atimer_CC3							(18<<0)			
#define DMA_DRC_PER_Atimer_CC2							(19<<0)			
#define DMA_DRC_PER_Atimer_CC1							(20<<0)		

#define DMA_IRQ_ENABLE									(1)
#define DMA_IRQ_DISABLE                                 (0)

void dma_init(uint8_t channel_index);
void dma_init2(uint8_t channel_index);
void dma_irq_init(uint8_t channel_index,uint8_t irq_enable,void (*pfunc_tc)());
void dma_irq_transfer(uint8_t channel_index, uint32_t src_addr, uint32_t dest_addr, uint16_t length);
void dma_poll_transfer(uint8_t channel_index, uint32_t src_addr, uint32_t dest_addr, uint16_t length);

void dma_pwm_init(uint8_t channel_index);
void dma_cap_init(uint8_t channel_index);
#endif

