/********************************************************************************
 * @file    TIM_TI_DMA.c
 * @author  SOC AE Team
 * @version V1.1
 * @date    2025-06-27
 * @brief   定时器溢出触发DMA把源数组数据搬运到目标数组中。
 *
 *******************************************************************************/
#include "sc32_conf.h"
#include "Function_Init.h"
#include "string.h"

static FlagStatus TIM_DMA_Flag = RESET;

 uint8_t TIM_TI_DMA_SrcData[16] = "ABCDEFGHIJ";

void TIM_TI_DMA_Function(void)
{

  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; //定义TIM初始化结构体变量
  DMA_InitTypeDef DMA_InitStruct; //定义DMA初始化结构体变量
  uint8_t TIM_TI_DMA_DstData[10];

  /* 使能TIM0时钟 */
  RCC_APB0Cmd(ENABLE);
  RCC_APB0PeriphClockCmd(RCC_APB0Periph_TIM1, ENABLE);
  /* 使能DMA的时钟 */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA, ENABLE);

  /* TIM配置结构体初始化 */
  TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
  /* 设置TIM配置结构体 */
  TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;	//TIM工作在向上计数模式
  TIM_TimeBaseInitStruct.TIM_EXENX = TIM_EXENX_Disable;	//外部重加载管脚失能
#if defined(SC32f10xx)||defined(SC32f12xx)
  TIM_TimeBaseInitStruct.TIM_Preload = 65535 - (32000000 / 1000);	//设置TIM的定时器评论为1000Hz@APB0Clock=32M
#endif
  TIM_TimeBaseInitStruct.TIM_Prescaler = TIM_PRESCALER_8;	//TIM分频为8分频,定时器频率为100HZ
  TIM_TimeBaseInitStruct.TIM_WorkMode = TIM_WorkMode_Timer;	//TIM工作在定时器模式，时钟源为APB0时钟
  /* TIM时基配置 */
  TIM_TIMBaseInit(TIM1, &TIM_TimeBaseInitStruct);
  TIM_DMACmd(TIM1, TIM_DMAReq_TI, ENABLE);
  TIM_Cmd(TIM1, ENABLE);

  /* DMA外设配置 */
  DMA_StructInit(&DMA_InitStruct);
  DMA_InitStruct.DMA_BufferSize = 10;	//DMA搬运数量为10个
  DMA_InitStruct.DMA_Burst = DMA_Burst_Disable;	//Brust模式关闭
  DMA_InitStruct.DMA_CircularMode = DMA_CircularMode_Disable;	//开启循环模式
  DMA_InitStruct.DMA_DataSize = DMA_DataSize_Byte;	//传输宽度为字节
  DMA_InitStruct.DMA_SourceMode = DMA_SourceMode_INC_CIRC;	//源地址循环递增
  DMA_InitStruct.DMA_TargetMode = DMA_TargetMode_INC_CIRC;	//目标地址递增循环
  DMA_InitStruct.DMA_SrcAddress = (uint32_t)&TIM_TI_DMA_SrcData;	//设置第一次传输的首地址
  DMA_InitStruct.DMA_DstAddress = (uint32_t)TIM_TI_DMA_DstData;	//设置目标地址
  DMA_InitStruct.DMA_Request = DMA_Request_TIM1_TI;	//TIM1触发DMA0
  DMA_InitStruct.DMA_Priority = DMA_Priority_HIGH;	//设置优先级为高
  DMA_Init(DMA0, &DMA_InitStruct);
  /* 开启DMA相关的中断 */
  DMA_ITConfig(DMA0,DMA_IT_INTEN | DMA_IT_TCIE,ENABLE);
  NVIC_EnableIRQ(DMA0_IRQn);
  memset(TIM_TI_DMA_DstData,0x00,11);	//把目标数据清理
  DMA_Cmd(DMA0, ENABLE);
  
  printf("TIM DMA开始搬运，源数组数据为：%s。\r\n",TIM_TI_DMA_SrcData);
  while(1)
  {
    if(TIM_DMA_Flag == SET)
    {
      printf("TIM DMA搬运完成，目标数组数据为：%s。\r\n",TIM_TI_DMA_DstData);
      memset(TIM_TI_DMA_DstData,0x00,11);	//把目标数据清理
     
      /* 开启下一轮DMA传输 */
      DMA_SetCurrDataCounter(DMA0, 10);
      TIM_DMACmd(TIM1,TIM_DMAReq_TI,DISABLE);
      TIM_DMACmd(TIM1,TIM_DMAReq_TI,ENABLE);
      TIM_DMA_Flag = RESET;
    }
  }
}

void TIM_TI_DMA_DMA0Handler(void)
{
  if(DMA_GetFlagStatus(DMA0, DMA_FLAG_TCIF) == SET)
  {
    DMA_ClearFlag(DMA0, DMA_FLAG_TCIF);
    TIM_DMA_Flag = SET;
  }
}

