/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2016. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
/*
 *@version 1.1.0
 */
 
/**************************************
主函数中初始化DMA内存到内存传输，
将g_dstBuffer数组的值传输到g_srcBuffer
数组中，并打印出g_srcBuffer数组的值
***************************************/
 
/**********<Incldue>**********/
#include <string.h>
#include "ac780x_adc.h"
#include "ac780x_adc_reg.h"
#include "ac780x_gpio.h"
#include "ac780x.h"
#include "ac780x_debugout.h"
#include "ac780x_dma.h"

#define DMA_TRANSFER_NUM              8

/*********<Variable>********/
uint32_t g_srcBuffer[DMA_TRANSFER_NUM + 1] = {1,2,3,4,5,6,7,8,9};
uint32_t g_dstBuffer[DMA_TRANSFER_NUM + 1] = {9,8,7,6,5,4,3,2,1};
uint32_t g_finishFlag = 0;

/**
 * DMACallback
 *
 * @param[in]  wpara:dmaChannelStatus 
 *             lpara:0
 * @return 0
 *
 * @brief ADC DMAcallback function
 */
void DMACallback(void *device, uint32_t wpara, uint32_t lpara)
{
    /*dma finish*/
    if ((wpara & 0x01) == 0x1)  
    {
        g_finishFlag = 1;          
    }
    
    /*dma half finish*/
    if ((wpara & 0x02) == 0x2)
    {
    }
    
    /*dma error*/
    if ((wpara & 0x04) == 0x4)
    {
    }
}

/**
 * ADC_DMAInit
 *
 * @param[in] void
 * @return void
 *
 * @brief ADC DMA config
 */
void MEM2MME_DMA_Init(void)
{
    uint32_t tmpMemStartAddr = (uint32_t)&g_srcBuffer[0];
    uint32_t tmpMemEndAddr = (uint32_t)&g_srcBuffer[DMA_TRANSFER_NUM + 1];      /*setting memory DMA address*/
    uint32_t tmpPerStartAddr = (uint32_t)&g_dstBuffer[0];
    DMA_ConfigType tmpDMAConfig;
    memset(&tmpDMAConfig, 0x00, sizeof(DMA_ConfigType));
    
    tmpDMAConfig.memStartAddr = tmpMemStartAddr;                                /*DMA transfer start addr*/
    tmpDMAConfig.memEndAddr = tmpMemEndAddr;                                    /*DMA transfer end addr*/
    tmpDMAConfig.periphStartAddr = tmpPerStartAddr;
    tmpDMAConfig.channelEn = ENABLE;                                            /*channel enable*/
    tmpDMAConfig.finishInterruptEn = ENABLE;                                    /*DMA finish interrupt enable*/    
    tmpDMAConfig.halfFinishInterruptEn = DISABLE;                               /*DMA half finish interrupt disbale*/  
    tmpDMAConfig.errorInterruptEn = ENABLE;                                     /*DMA error interrupt enable*/
    tmpDMAConfig.channelPriority = DMA_PRIORITY_VERY_HIGH;                      /*DMA priority*/
    tmpDMAConfig.circular = DISABLE;                                            /*disable circular mode*/
    tmpDMAConfig.direction = DMA_READ_FROM_PERIPH;                              /*direction to memory*/
    tmpDMAConfig.MEM2MEM = ENABLE;                                              /*memory to memory enable*/
    tmpDMAConfig.memByteMode = DMA_MEM_BYTE_MODE_1TIME;                         /*memory byte mode*/
    tmpDMAConfig.memIncrement = ENABLE;                                         /*memory address increment enable*/
    tmpDMAConfig.periphIncrement = ENABLE;                                      /*peripheral address increment disable*/
    tmpDMAConfig.memSize = DMA_MEM_SIZE_32BIT;                                  /*0:8-bit,1:16-bit,2:32-bit*/
    tmpDMAConfig.periphSize = DMA_PERIPH_SIZE_32BIT;                            /*0:8-bit,1:16-bit,2:32-bit*/
    tmpDMAConfig.transferNum = DMA_TRANSFER_NUM;                                /*DMA transfer number*/
    tmpDMAConfig.callBack = DMACallback;                                        /*ADC DMA callback function*/

    DMA_Init(DMA0_CHANNEL0, &tmpDMAConfig);                                     /*DMA channel0 init*/         
    NVIC_EnableIRQ(DMA0_CHANNEL0_IRQn);                                         /*DMA0 channel0 interrupt enable*/
}

int main(void)
{
    uint8_t i;
    
    InitDebug();
    InitDelay();
    
    MEM2MME_DMA_Init();                      /* memory to memory dma init */
    
    while(g_finishFlag != 1);
    g_finishFlag = 0;
    
    printf("value: ");
    for (i=0;i<DMA_TRANSFER_NUM;i++)
    {
        printf("%08x ", g_srcBuffer[i]);     /*printf ADC buffer value*/
    }
    printf("\r\n");    
    
    while(1);
}
