#include "bsp_dma2d.h" 
#include "elog.h" 

// uint8_t dma2d_transfering =0;  
// DMA2D->CR   & DMA2D_CR_START  可作为标记
void DMA2D_Fill( void * pDst, uint32_t width, uint32_t height, uint32_t lineOff, uint32_t pixelFormat,  uint32_t color)
 {
 
     /* DMA2D配置 */  
    // DMA2D->CR      = DMA2D_R2M;                                  // 配置为寄存器到储存器模式
    DMA2D->CR      = 0x00030000UL;                                  // 配置为寄存器到储存器模式
    DMA2D->OCOLR   = color;                                         // 设置填充使用的颜色，格式应该与设置的颜色格式相同
    DMA2D->OMAR    = (uint32_t)pDst;                                // 填充区域的起始内存地址
    DMA2D->OOR     = lineOff;                                       // 行偏移，即跳过的像素，注意是以像素为单位
    DMA2D->OPFCCR  = pixelFormat;                                   // 设置颜色格式
    DMA2D->NLR     = (uint32_t)(width << 16) | (uint16_t)height;    // 设置填充区域的宽和高，单位是像素

    // 传输中断
    // DMA2D->CR |= DMA2D_IT_TC|DMA2D_IT_TE|DMA2D_IT_CE;
    // DMA2D->CR   |= DMA2D_CR_START;

    // 启动传输 阻塞  -- 阻塞一般用于清屏，R2M 模式
    DMA2D->CR   |= DMA2D_CR_START;   
    while (DMA2D->CR & DMA2D_CR_START) {} 
}

void DMA2D_MemCopy(uint32_t pixelFormat, void * pSrc, void * pDst, int xSize, int ySize, int OffLineSrc, int OffLineDst)
{
    /* DMA2D配置 */
    // DMA2D->CR      = DMA2D_M2M;
    DMA2D->CR      = 0x00000000UL;
    DMA2D->FGMAR   = (uint32_t)pSrc;
    DMA2D->OMAR    = (uint32_t)pDst;
    DMA2D->FGOR    = OffLineSrc;
    DMA2D->OOR     = OffLineDst;
    DMA2D->FGPFCCR  = pixelFormat;
    DMA2D->FGPFCCR = pixelFormat;
    DMA2D->NLR     = (uint32_t)(xSize << 16) | (uint16_t)ySize;

    // 传输中断  用于数据传输  M2M模式
 
    // __HAL_DMA2D_ENABLE_IT(&hdma2d, DMA2D_IT_TC);
    // DMA2D->CR |= DMA2D_IT_TC|DMA2D_IT_TE|DMA2D_IT_CE;
    DMA2D->CR |= DMA2D_IT_TC;
    DMA2D->CR   |= DMA2D_CR_START;

    // // 启动传输 阻塞
    // DMA2D->CR   |= DMA2D_CR_START;   
    // while (DMA2D->CR & DMA2D_CR_START) {} 
}

void DMA2D_Blend(void* pFg, void* pBg, void* pDst,
    uint32_t offlineFg, uint32_t offlineBg, uint32_t offlineDist,
    uint16_t xSize, uint16_t ySize,
    uint32_t pixelFormat, uint8_t opa) {

    DMA2D->CR    = 0x00020000UL;                // 设置工作模式为存储器到存储器并带颜色混合

    DMA2D->FGMAR = (uint32_t)pFg;               // 设置前景数据内存地址
    DMA2D->BGMAR = (uint32_t)pBg;               // 设置背景数据内存地址
    DMA2D->OMAR  = (uint32_t)pDst;              // 设置数据输出内存地址

    DMA2D->FGOR  = offlineFg;                   // 设置前景数据传输偏移
    DMA2D->BGOR  = offlineBg;                   // 设置背景数据传输偏移
    DMA2D->OOR   = offlineDist;                 // 设置数据输出传输偏移

    DMA2D->NLR = (uint32_t)(xSize << 16) | (uint16_t)ySize; // 设置图像数据宽高（像素）

    DMA2D->FGPFCCR = pixelFormat                // 设置前景色颜色格式
    | (1UL << 16)                       // 忽略前景颜色数据中的Alpha通道
    | ((uint32_t)opa << 24);            // 设置前景色不透明度

    DMA2D->BGPFCCR = pixelFormat;               // 设置背景颜色格式
    DMA2D->OPFCCR  = pixelFormat;                // 设置输出颜色格式

    /* 启动传输 */
    DMA2D->CR   |= DMA2D_CR_START;

    /* 等待DMA2D传输完成 */
    while (DMA2D->CR & DMA2D_CR_START) {}
}

void DMA2D_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2D_IRQn 0 */
  if (DMA2D->CR &DMA2D_IT_TC){
  log_i(" DMA2D_IRQHandler ->   TC   "   );
  DMA2D->CR   &= ~DMA2D_CR_START;
  DMA2D->CR &= ~DMA2D_IT_TC;
  }
 
  log_i(" ......     "   );
  // DMA2D->CR &= ~DMA2D_IT_TC;
  /* USER CODE END DMA2D_IRQn 0 */
  // HAL_DMA2D_IRQHandler(&hdma2d);
  /* USER CODE BEGIN DMA2D_IRQn 1 */

  /* USER CODE END DMA2D_IRQn 1 */
}