/**
  ******************************************************************************
  * @file    ys32md211_dma.h
  * @author  YSPRING Application Team
  * @version V1.0.1
  * @date    2025.02.15
  * @brief   This file contains all the functions prototypes for the DMA firmware
  *          library.
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __YS32MD211_DMA_H
#define __YS32MD211_DMA_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "ys32md211.h"
#include "ys32md211_syscfg.h"

/** @addtogroup YS32MD211_StdPeriph_Driver
  * @{
  */

/** @addtogroup DMA
  * @{
  */
/* Exported types ------------------------------------------------------------*/

/** 
  * @brief  DMA Init structures definition
  */
typedef struct
{
  uint32_t DMA_PeripheralBaseAddr; /*!< Specifies the peripheral base address for DMAy Channelx.              */

  uint32_t DMA_MemoryBaseAddr;     /*!< Specifies the memory base address for DMAy Channelx.                  */

  uint32_t DMA_DIR;                /*!< Specifies if the peripheral is the source or destination.
                                        This parameter can be a value of @ref DMA_data_transfer_direction     */

  uint32_t DMA_BufferSize;         /*!< Specifies the buffer size, in data unit, of the specified Channel. 
                                        The data unit is equal to the configuration set in DMA_PeripheralDataSize
                                        or DMA_MemoryDataSize members depending in the transfer direction     */

  uint32_t DMA_PeripheralInc;      /*!< Specifies whether the Peripheral address register is incremented or not.
                                        This parameter can be a value of @ref DMA_peripheral_incremented_mode */

  uint32_t DMA_MemoryInc;          /*!< Specifies whether the memory address register is incremented or not.
                                        This parameter can be a value of @ref DMA_memory_incremented_mode     */

  uint32_t DMA_PeripheralDataSize; /*!< Specifies the Peripheral data width.
                                        This parameter can be a value of @ref DMA_peripheral_data_size        */

  uint32_t DMA_MemoryDataSize;     /*!< Specifies the Memory data width.
                                        This parameter can be a value of @ref DMA_memory_data_size            */

  uint32_t DMA_Mode;               /*!< Specifies the operation mode of the DMAy Channelx.
                                        This parameter can be a value of @ref DMA_circular_normal_mode
                                        @note: The circular buffer mode cannot be used if the memory-to-memory
                                              data transfer is configured on the selected Channel */

  uint32_t DMA_Priority;           /*!< Specifies the software priority for the DMAy Channelx.
                                        This parameter can be a value of @ref DMA_priority_level              */

  uint32_t DMA_M2M;                /*!< Specifies if the DMAy Channelx will be used in memory-to-memory transfer.
                                        This parameter can be a value of @ref DMA_memory_to_memory            */
}DMA_InitTypeDef;

/* Exported constants --------------------------------------------------------*/

/** @defgroup DMA_Exported_Constants
  * @{
  */

#define IS_DMA_ALL_PERIPH(PERIPH) (((PERIPH) == DMA_CHANNEL1) || \
                                   ((PERIPH) == DMA_CHANNEL2) || \
                                   ((PERIPH) == DMA_CHANNEL3))

/** @defgroup DMA_data_transfer_direction 
  * @{
  */

#define DMA_DIR_PeripheralSRC              ((uint32_t)0x00000000)
#define DMA_DIR_PeripheralDST              DMA_CHANNEL_CCR_DIR

#define IS_DMA_DIR(DIR) (((DIR) == DMA_DIR_PeripheralSRC) || \
                         ((DIR) == DMA_DIR_PeripheralDST))
/**
  * @}
  */

/** @defgroup DMA_peripheral_incremented_mode 
  * @{
  */

#define DMA_PeripheralInc_Disable          ((uint32_t)0x00000000)
#define DMA_PeripheralInc_Enable           DMA_CHANNEL_CCR_PINC

#define IS_DMA_PERIPHERAL_INC_STATE(STATE) (((STATE) == DMA_PeripheralInc_Disable) || \
                                            ((STATE) == DMA_PeripheralInc_Enable))
/**
  * @}
  */

/** @defgroup DMA_memory_incremented_mode 
  * @{
  */

#define DMA_MemoryInc_Disable              ((uint32_t)0x00000000)
#define DMA_MemoryInc_Enable               DMA_CHANNEL_CCR_MINC

#define IS_DMA_MEMORY_INC_STATE(STATE) (((STATE) == DMA_MemoryInc_Disable) || \
                                        ((STATE) == DMA_MemoryInc_Enable))
/**
  * @}
  */

/** @defgroup DMA_peripheral_data_size 
  * @{
  */

#define DMA_PeripheralDataSize_Byte        ((uint32_t)0x00000000)
#define DMA_PeripheralDataSize_HalfWord    DMA_CHANNEL_CCR_PSIZE_16
#define DMA_PeripheralDataSize_Word        DMA_CHANNEL_CCR_PSIZE_32

#define IS_DMA_PERIPHERAL_DATA_SIZE(SIZE) (((SIZE) == DMA_PeripheralDataSize_Byte) || \
                                           ((SIZE) == DMA_PeripheralDataSize_HalfWord) || \
                                           ((SIZE) == DMA_PeripheralDataSize_Word))
/**
  * @}
  */

/** @defgroup DMA_memory_data_size 
  * @{
  */

#define DMA_MemoryDataSize_Byte            ((uint32_t)0x00000000)
#define DMA_MemoryDataSize_HalfWord        DMA_CHANNEL_CCR_MSIZE_16
#define DMA_MemoryDataSize_Word            DMA_CHANNEL_CCR_MSIZE_32

#define IS_DMA_MEMORY_DATA_SIZE(SIZE) (((SIZE) == DMA_MemoryDataSize_Byte) || \
                                       ((SIZE) == DMA_MemoryDataSize_HalfWord) || \
                                       ((SIZE) == DMA_MemoryDataSize_Word))
/**
  * @}
  */

/** @defgroup DMA_circular_normal_mode 
  * @{
  */

#define DMA_Mode_Normal                    ((uint32_t)0x00000000)
#define DMA_Mode_Circular                  DMA_CHANNEL_CCR_CIRC

#define IS_DMA_MODE(MODE) (((MODE) == DMA_Mode_Normal) || ((MODE) == DMA_Mode_Circular))
/**
  * @}
  */

/** @defgroup DMA_priority_level 
  * @{
  */

#define DMA_Priority_VeryHigh              DMA_CHANNEL_CCR_PL_VeryHigh
#define DMA_Priority_High                  DMA_CHANNEL_CCR_PL_High
#define DMA_Priority_Medium                DMA_CHANNEL_CCR_PL_Mid
#define DMA_Priority_Low                   ((uint32_t)0x00000000)

#define IS_DMA_PRIORITY(PRIORITY) (((PRIORITY) == DMA_Priority_VeryHigh) || \
                                   ((PRIORITY) == DMA_Priority_High) || \
                                   ((PRIORITY) == DMA_Priority_Medium) || \
                                   ((PRIORITY) == DMA_Priority_Low))
/**
  * @}
  */

/** @defgroup DMA_memory_to_memory 
  * @{
  */

#define DMA_M2M_Disable                    ((uint32_t)0x00000000)
#define DMA_M2M_Enable                     DMA_CHANNEL_CCR_MEM2MEM

#define IS_DMA_M2M_STATE(STATE) (((STATE) == DMA_M2M_Disable) || ((STATE) == DMA_M2M_Enable))

/**
  * @}
  */

 /** @defgroup DMA_TransMode
  * @{
  */
#define DMA_TransMode_All                  ((uint32_t)0x00000000)
#define DMA_TransMode_One                  DMA_CHANNEL_CCR_TRANS_MODE
#define IS_DMA_TRANS_MODE(STATE) (((STATE) == DMA_TransMode_All) || ((STATE) == DMA_TransMode_One))
/**
  * @}
  */

/** @defgroup DMA_interrupts_definition
  * @{
  */

#define DMA_IT_TC                          DMA_CHANNEL_CCR_TCIE
#define DMA_IT_HT                          DMA_CHANNEL_CCR_HTIE
#define DMA_IT_TE                          DMA_CHANNEL_CCR_TEIE

#define IS_DMA_CONFIG_IT(IT) ((((IT) & 0xFFFFFFF1) == 0x00) && ((IT) != 0x00))

#define DMA_IT_GI1                        DMA_ISR_GIF1
#define DMA_IT_TC1                        DMA_ISR_TCIF1
#define DMA_IT_HT1                        DMA_ISR_HTIF1
#define DMA_IT_TE1                        DMA_ISR_TEIF1
#define DMA_IT_GI2                        DMA_ISR_GIF2
#define DMA_IT_TC2                        DMA_ISR_TCIF2
#define DMA_IT_HT2                        DMA_ISR_HTIF2
#define DMA_IT_TE2                        DMA_ISR_TEIF2
#define DMA_IT_GI3                        DMA_ISR_GIF3
#define DMA_IT_TC3                        DMA_ISR_TCIF3
#define DMA_IT_HT3                        DMA_ISR_HTIF3
#define DMA_IT_TE3                        DMA_ISR_TEIF3


#define IS_DMA_CLEAR_IT(IT) ((((IT) & 0xFFF00000) == 0x00) && ((IT) != 0x00))

#define IS_DMA_GET_IT(IT) (((IT) == DMA_IT_GI1) || ((IT) == DMA_IT_TC1) || \
                           ((IT) == DMA_IT_HT1) || ((IT) == DMA_IT_TE1) || \
                           ((IT) == DMA_IT_GI2) || ((IT) == DMA_IT_TC2) || \
                           ((IT) == DMA_IT_HT2) || ((IT) == DMA_IT_TE2) || \
                           ((IT) == DMA_IT_GI3) || ((IT) == DMA_IT_TC3) || \
                           ((IT) == DMA_IT_HT3) || ((IT) == DMA_IT_TE3))

/**
  * @}
  */

/** @defgroup DMA_flags_definition 
  * @{
  */
#define DMA_FLAG_GI1                      DMA_ISR_GIF1
#define DMA_FLAG_TC1                      DMA_ISR_TCIF1
#define DMA_FLAG_HT1                      DMA_ISR_HTIF1
#define DMA_FLAG_TE1                      DMA_ISR_TEIF1
#define DMA_FLAG_GI2                      DMA_ISR_GIF2
#define DMA_FLAG_TC2                      DMA_ISR_TCIF2
#define DMA_FLAG_HT2                      DMA_ISR_HTIF2
#define DMA_FLAG_TE2                      DMA_ISR_TEIF2
#define DMA_FLAG_GI3                      DMA_ISR_GIF3
#define DMA_FLAG_TC3                      DMA_ISR_TCIF3
#define DMA_FLAG_HT3                      DMA_ISR_HTIF3
#define DMA_FLAG_TE3                      DMA_ISR_TEIF3

#define IS_DMA_CLEAR_FLAG(FLAG) ((((FLAG) & 0xFFF00000) == 0x00) && ((FLAG) != 0x00))

#define IS_DMA_GET_FLAG(FLAG) (((FLAG) == DMA_FLAG_GI1) || ((FLAG) == DMA_FLAG_TC1) || \
                               ((FLAG) == DMA_FLAG_HT1) || ((FLAG) == DMA_FLAG_TE1) || \
                               ((FLAG) == DMA_FLAG_GI2) || ((FLAG) == DMA_FLAG_TC2) || \
                               ((FLAG) == DMA_FLAG_HT2) || ((FLAG) == DMA_FLAG_TE2) || \
                               ((FLAG) == DMA_FLAG_GI3) || ((FLAG) == DMA_FLAG_TC3) || \
                               ((FLAG) == DMA_FLAG_HT3) || ((FLAG) == DMA_FLAG_TE3))
/**
  * @}
  */

/** @defgroup DMA_Buffer_Size 
  * @{
  */

#define IS_DMA_BUFFER_SIZE(SIZE) (((SIZE) >= 0x1) && ((SIZE) < 0x10000))

/**
  * @}
  */
  
  /** @defgroup DMA_Remap_Config 
  * @{
  */ 
#define DMA_MAP_CH1          SYSCFG_DMAChannel1
#define DMA_MAP_CH2          SYSCFG_DMAChannel2  
#define DMA_MAP_CH3          SYSCFG_DMAChannel3  

#define IS_DMA_CHANNEL(DMAChannel) (((DMAChannel) == DMA_MAP_CH1) || \
                                    ((DMAChannel) == DMA_MAP_CH2) || \
                                    ((DMAChannel) == DMA_MAP_CH3))

#define        DMA_MAP_ADC             SYSCFG_DMA_MAP_ADC       
#define        DMA_MAP_SPI1_TX         SYSCFG_DMA_MAP_SPI1_TX   
#define        DMA_MAP_SPI1_RX         SYSCFG_DMA_MAP_SPI1_RX   
#define        DMA_MAP_UART1_TX        SYSCFG_DMA_MAP_UART1_TX  
#define        DMA_MAP_UART1_RX        SYSCFG_DMA_MAP_UART1_RX  
#define        DMA_MAP_UART2_TX        SYSCFG_DMA_MAP_UART2_TX  
#define        DMA_MAP_UART2_RX        SYSCFG_DMA_MAP_UART2_RX  
#define        DMA_MAP_I2C1_TX         SYSCFG_DMA_MAP_I2C1_TX   
#define        DMA_MAP_I2C1_RX         SYSCFG_DMA_MAP_I2C1_RX   
#define        DMA_MAP_TIM1_CH1        SYSCFG_DMA_MAP_TIM1_CH1  
#define        DMA_MAP_TIM1_CH2        SYSCFG_DMA_MAP_TIM1_CH2  
#define        DMA_MAP_TIM1_CH3        SYSCFG_DMA_MAP_TIM1_CH3  
#define        DMA_MAP_TIM1_CH4        SYSCFG_DMA_MAP_TIM1_CH4  
#define        DMA_MAP_TIM1_COM        SYSCFG_DMA_MAP_TIM1_COM  
#define        DMA_MAP_TIM1_UP         SYSCFG_DMA_MAP_TIM1_UP   
#define        DMA_MAP_TIM1_TRIG       SYSCFG_DMA_MAP_TIM1_TRIG 
#define        DMA_MAP_TIM3_CH1        SYSCFG_DMA_MAP_TIM3_CH1  
#define        DMA_MAP_TIM3_CH3        SYSCFG_DMA_MAP_TIM3_CH3  
#define        DMA_MAP_TIM3_CH4        SYSCFG_DMA_MAP_TIM3_CH4  
#define        DMA_MAP_TIM3_TRG        SYSCFG_DMA_MAP_TIM3_TRG  
#define        DMA_MAP_TIM3_UP         SYSCFG_DMA_MAP_TIM3_UP   
#define        DMA_MAP_TIM16_CH1       SYSCFG_DMA_MAP_TIM16_CH1 
#define        DMA_MAP_TIM16_UP        SYSCFG_DMA_MAP_TIM16_UP  
#define        DMA_MAP_TIM17_CH1       SYSCFG_DMA_MAP_TIM17_CH1 
#define        DMA_MAP_TIM17_UP        SYSCFG_DMA_MAP_TIM17_UP  

#define IS_DMA_REMAP(REMAP) (((REMAP) == DMA_MAP_ADC) || \
                                    ((REMAP) == DMA_MAP_SPI_TX)    || \
                                    ((REMAP) == DMA_MAP_SPI_RX)    || \
                                    ((REMAP) == DMA_MAP_UART1_TX)  || \
                                    ((REMAP) == DMA_MAP_UART1_RX)  || \
                                    ((REMAP) == DMA_MAP_UART2_TX)  || \
                                    ((REMAP) == DMA_MAP_UART2_RX)  || \
                                    ((REMAP) == DMA_MAP_I2C_TX)    || \
                                    ((REMAP) == DMA_MAP_I2C_RX)    || \
                                    ((REMAP) == DMA_MAP_TIM1_CH1)  || \
                                    ((REMAP) == DMA_MAP_TIM1_CH2)  || \
                                    ((REMAP) == DMA_MAP_TIM1_CH3)  || \
                                    ((REMAP) == DMA_MAP_TIM1_CH4)  || \
                                    ((REMAP) == DMA_MAP_TIM1_COM)  || \
                                    ((REMAP) == DMA_MAP_TIM1_UP)   || \
                                    ((REMAP) == DMA_MAP_TIM1_TRIG) || \
                                    ((REMAP) == DMA_MAP_TIM3_CH1)  || \
                                    ((REMAP) == DMA_MAP_TIM3_CH3)  || \
                                    ((REMAP) == DMA_MAP_TIM3_CH4)  || \
                                    ((REMAP) == DMA_MAP_TIM3_TRG)  || \
                                    ((REMAP) == DMA_MAP_TIM3_UP)   || \
                                    ((REMAP) == DMA_MAP_TIM16_CH1) || \
                                    ((REMAP) == DMA_MAP_TIM16_UP)  || \
                                    ((REMAP) == DMA_MAP_TIM17_CH1) || \
                                    ((REMAP) == DMA_MAP_TIM17_UP))

/**
  * @}
  */

/**
  * @}
  */

/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */

/* Function used to set the DMA configuration to the default reset state ******/
void DMA_DeInit(DMA_CHANNEL_TypeDef* DMAy_Channelx);

/* Initialization and Configuration functions *********************************/
void DMA_Init(DMA_CHANNEL_TypeDef* DMAy_Channelx, DMA_InitTypeDef* DMA_InitStruct);
void DMA_StructInit(DMA_InitTypeDef* DMA_InitStruct);
void DMA_Cmd(DMA_CHANNEL_TypeDef* DMAy_Channelx, FunctionalState NewState);
void DMA_RemapConfig(uint32_t DMAy_Channelx, uint32_t DMARemap, FunctionalState NewState);
void DMA_SetTransferMode(DMA_CHANNEL_TypeDef* DMAy_Channelx, uint16_t TransferMode);
/* Data Counter functions******************************************************/ 
void DMA_SetCurrDataCounter(DMA_CHANNEL_TypeDef* DMAy_Channelx, uint16_t DataNumber);
uint16_t DMA_GetCurrDataCounter(DMA_CHANNEL_TypeDef* DMAy_Channelx);

/* Interrupts and flags management functions **********************************/
void DMA_ITConfig(DMA_CHANNEL_TypeDef* DMAy_Channelx, uint32_t DMA_IT, FunctionalState NewState);
FlagStatus DMA_GetFlagStatus(uint32_t DMAy_FLAG);
void DMA_ClearFlag(uint32_t DMAy_FLAG);
ITStatus DMA_GetITStatus(uint32_t DMAy_IT);
void DMA_ClearITPendingBit(uint32_t DMAy_IT);

#ifdef __cplusplus
}
#endif

#endif /*__YS32MD211_DMA_H */

/**
  * @}
  */

/**
  * @}
  */

