/**
  ******************************************************************************
  * @file    DMA/M2M/main.c
  * @author  IOT Development Team
  * @version V1.0.0
  * @date    10-January-2020
  * @brief   Main program body
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "apm32f10x.h"
#include "platform_config.h"

/** @addtogroup APM32F10x_StdPeriph_Examples
  * @{
  */

/** @addtogroup DMA_SPI_RAM
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;

/* Private define ------------------------------------------------------------*/
#define Buffer_Size         32

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
volatile TestStatus TransferStatus = FAILED;
__IO uint32_t CurrDataCounterBegin = 0;
//__IO uint32_t CurrDataCounterEnd = 0x01; /* This variable should not be initialized to 0 */
uint8_t CurrData=0;
//uint8_t TransferStatus = 0;
const uint32_t SRC_Const_Buffer[Buffer_Size]=
{
    0x01020304,0x05060708,0x090A0B0C,0x0D0E0F10,
    0x11121314,0x15161718,0x191A1B1C,0x1D1E1F20,
    0x21222324,0x25262728,0x292A2B2C,0x2D2E2F30,
    0x31323334,0x35363738,0x393A3B3C,0x3D3E3F40,
    0x41424344,0x45464748,0x494A4B4C,0x4D4E4F50,
    0x51525354,0x55565758,0x595A5B5C,0x5D5E5F60,
    0x61626364,0x65666768,0x696A6B6C,0x6D6E6F70,
    0x71727374,0x75767778,0x797A7B7C,0x7D7E7F80
};
const uint32_t SRC_HalfWord[Buffer_Size]=
{
    0x00000304,0x00000708,0x00000B0C,0x00000F10,
    0x00001314,0x00001718,0x00001B1C,0x00001F20,
    0x00002324,0x00002728,0x00002B2C,0x00002F30,
    0x00003334,0x00003738,0x00003B3C,0x00003F40,
    0x00004344,0x00004748,0x00004B4C,0x00004F50,
    0x00005354,0x00005758,0x00005B5C,0x00005F60,
    0x00006364,0x00006768,0x00006B6C,0x00006F70,
    0x00007374,0x00007778,0x00007B7C,0x00007F80
};
uint32_t DST_Buffer[Buffer_Size];
DMA_Config_T DMA_ConfigStruct;


/* Private function prototypes -----------------------------------------------*/
TestStatus Buffercmp(const uint32_t* pBuffer, uint32_t* pBuffer1, uint16_t BufferLength);

/* Private functions ---------------------------------------------------------*/
void NVIC_Configuration(void)
{

    NVIC_ConfigPriorityGroup(NVIC_PRIORITY_GROUP_2);    //设置NVIC中断分组2:2位抢占优先级，2位响应优先级

}
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
//  TestStatus i;
    /*!< At this stage the microcontroller clock setting is already configured,
         this is done through SystemInit() function which is called from startup
         file (startup_apm32f10x_xx.s) before to branch to application main.
         To reconfigure the default setting of SystemInit() function, refer to
         system_apm32f10x.c file
       */
    NVIC_Configuration();    //设置NVIC中断分组2:2位抢占优先级，2位响应优先级
    RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_DMA1);  //使能DMA传输

    NVIC_EnableIRQRequest(DMA1_Channel6_IRQn, 0x00, 0x04);

    DMA_ConfigStruct.PeripheralBaseAddr = (uint32_t)SRC_Const_Buffer; //DMA外设ADC基地址
    DMA_ConfigStruct.MemoryBaseAddr = (uint32_t)DST_Buffer;  //DMA内存基地址
    DMA_ConfigStruct.DOT = DMA_DOT_PERIPHERAL_SRC;  //数据传输方向，从外设读取发送到内存
    DMA_ConfigStruct.BufferSize =8;  //DMA通道的DMA缓存的大小

//  DMA_InitStructure.DMA_BufferSize =65535;  //DMA通道的DMA缓存的大小

    DMA_ConfigStruct.PLoop = DMA_PERIPHERAL_LOOP_ENABLE;  //外设地址寄存器不变
    DMA_ConfigStruct.MLoop = DMA_MEMORY_LOOP_ENABLE;  //内存地址寄存器递增
//最大传输数量
//  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;  //内存地址寄存器递增

    DMA_ConfigStruct.PeripheralDataSize = DMA_PERIPHERAL_DATA_SIZE_WOED;  //数据宽度为32位
    DMA_ConfigStruct.MemoryDataSize = DMA_MEMORY_DATA_SIZE_WOED; //数据宽度为32位
    DMA_ConfigStruct.DMA_LoopMode = DMA_MODE_NORMAL;  //工作在正常缓存模式
//  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;  //工作在循环模式

    DMA_ConfigStruct.DMA_Priority = DMA_PRIORITY_MEDIUM; //DMA通道 x拥有中优先级
    DMA_IntEnable(DMA1_Channel6, DMA_IT_EOT);  //Transfer complete

    DMA_ConfigStruct.M2M = DMA_M2MEN_ENABLE;  //DMA通道x设置为内存到内存传输
    DMA_Config(DMA1_Channel6, &DMA_ConfigStruct);

//  DMA_Cmd(DMA1_Channel6, DISABLE );
    DMA_SetCurrDataCounter(DMA1_Channel6,32);//DMA通道的DMA缓存的大小
    DMA_ENABLE(DMA1_Channel6);

    CurrDataCounterBegin = DMA_ReadCurrDataCounter(DMA1_Channel6);
//  while (CurrDataCounterEnd != 32)
//      {
//          }
    /* 等待DMA传输完成 */
//  while(DMA_GetFlagStatus(DMA1_FLAG_TC6)==RESET)
//  {
//
//  }
//  while(CurrData==0)
//  {
//  }

    while (1)
    {
    }
}



/**
  * @brief  Compares two buffers.
  * @param  pBuffer, pBuffer1: buffers to be compared.
  * @param  BufferLength: buffer's length
  * @retval PASSED: pBuffer identical to pBuffer1
  *         FAILED: pBuffer differs from pBuffer1
  */
TestStatus Buffercmp(const uint32_t* pBuffer, uint32_t* pBuffer1, uint16_t BufferLength)
{
    while (BufferLength--)
    {
        if (*pBuffer != *pBuffer1)
        {
            return FAILED;
        }

        pBuffer++;
        pBuffer1++;
    }

    return PASSED;
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    /* Infinite loop */
    while (1)
    {
    }
}

#endif

/**
  * @}
  */

/**
  * @}
  */

