/**
  ******************************************************************************
  * @file    ys32f030_syscfg.c
  * @author  YSPRING Application Team
  * @version V1.0.1
  * @date    2023.6.21
  * @brief   This file provides firmware functions to manage the following 
  *          functionalities of the SYSCFG peripheral:
  *           + Remapping the memory mapped at 0x00000000  
  *           + Remapping the DMA channels
  *           + Configuring the COMP (TIM1)
  *   
  *  @verbatim
 ===============================================================================
                     ##### How to use this driver #####
 ===============================================================================
    [..] 
               The SYSCFG registers can be accessed only when the SYSCFG 
               interface APB2 clock is enabled.
               To enable SYSCFG APB2 clock use:
               RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE).
  *  @endverbatim
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "ys32f030_syscfg.h"

/** @addtogroup YS32F030_StdPeriph_Driver
  * @{
  */

/** @defgroup SYSCFG 
  * @brief SYSCFG driver modules
  * @{
  */ 

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup SYSCFG_Private_Functions
  * @{
  */ 

/** @defgroup SYSCFG_Group1 SYSCFG Initialization and Configuration functions
 *  @brief   SYSCFG Initialization and Configuration functions 
 *
@verbatim
 ===============================================================================
        ##### SYSCFG Initialization and Configuration functions #####
 ===============================================================================

@endverbatim
  * @{
  */

/**
  * @brief  Deinitializes the SYSCFG registers to their default reset values.
  * @param  None
  * @retval None
  * @note   MEM_MODE bits are not affected by APB reset.
  * @note   MEM_MODE bits took the value from the user option bytes.
  * @note   CFGR2 register is not affected by APB reset.
  * @note   CLABBB configuration bits are locked when set.
  * @note   To unlock the configuration, perform a system reset.
  */
void SYSCFG_DeInit(void)
{
  /* Set SYSCFG_CFGR1 register to reset value without affecting MEM_MODE bits */
  SYSCFG->CFGR1 &= (~SYSCFG_CFGR1_MEM_MODE);

  SYSCFG->CFGR2 = 0;
  SYSCFG->CFGR3 = 0;
  SYSCFG->CFGR4 = 0;

}

/**
  * @brief  Configures the memory mapping at address 0x00000000.
  * @param  SYSCFG_MemoryRemap: selects the memory remapping.
  *          This parameter can be one of the following values:
  *            @arg SYSCFG_MemoryRemap_Flash: Main Flash memory mapped at 0x00000000  
  *            @arg SYSCFG_MemoryRemap_SystemMemory: System Flash memory mapped at 0x00000000
  *            @arg SYSCFG_MemoryRemap_SRAM: Embedded SRAM mapped at 0x00000000
  * @retval None
  */
void SYSCFG_MemoryRemapConfig(uint32_t SYSCFG_MemoryRemap)
{
  uint32_t tmpctrl = 0;

  /* Check the parameter */
  assert_param(IS_SYSCFG_MEMORY_REMAP(SYSCFG_MemoryRemap));

  /* Get CFGR1 register value */
  tmpctrl = SYSCFG->CFGR1;

  /* Clear MEM_MODE bits */
  tmpctrl &= (uint32_t) (~SYSCFG_CFGR1_MEM_MODE);

  /* Set the new MEM_MODE bits value */
  tmpctrl |= (uint32_t) SYSCFG_MemoryRemap;

  /* Set CFGR1 register with the new memory remap configuration */
  SYSCFG->CFGR1 = tmpctrl;
}

/**
  * @brief  Configure the DMAx channels remapping.
  * @param  SYSCFG_DMAChannelx: x can be 1 to 5
  *         @arg SYSCFG_DMAChannel1: DMA Channel1 MAP 
  *         @arg SYSCFG_DMAChannel2: DMA Channel2 MAP 
  *         @arg SYSCFG_DMAChannel3: DMA Channel3 MAP 
  *         @arg SYSCFG_DMAChannel4: DMA Channel4 MAP 
  *         @arg SYSCFG_DMAChannel5: DMA Channel5 MAP 
  * @param  SYSCFG_DMARemap: 
  *         @arg SYSCFG_DMA_MAP_ADC       : DMA map to ADC       
  *         @arg SYSCFG_DMA_MAP_SPI1_TX   : DMA map to SPI1_TX   
  *         @arg SYSCFG_DMA_MAP_SPI1_RX   : DMA map to SPI1_RX   
  *         @arg SYSCFG_DMA_MAP_SPI2_TX   : DMA map to SPI2_TX   
  *         @arg SYSCFG_DMA_MAP_SPI2_RX   : DMA map to SPI2_RX   
  *         @arg SYSCFG_DMA_MAP_UART1_TX  : DMA map to UART1_TX  
  *         @arg SYSCFG_DMA_MAP_UART1_RX  : DMA map to UART1_RX  
  *         @arg SYSCFG_DMA_MAP_UART2_TX  : DMA map to UART2_TX  
  *         @arg SYSCFG_DMA_MAP_UART2_RX  : DMA map to UART2_RX  
  *         @arg SYSCFG_DMA_MAP_I2C1_TX   : DMA map to I2C1_TX   
  *         @arg SYSCFG_DMA_MAP_I2C1_RX   : DMA map to I2C1_RX   
  *         @arg SYSCFG_DMA_MAP_I2C2_TX   : DMA map to I2C2_TX   
  *         @arg SYSCFG_DMA_MAP_I2C2_RX   : DMA map to I2C2_RX   
  *         @arg SYSCFG_DMA_MAP_TIM1_CH1  : DMA map to TIM1_CH1  
  *         @arg SYSCFG_DMA_MAP_TIM1_CH2  : DMA map to TIM1_CH2  
  *         @arg SYSCFG_DMA_MAP_TIM1_CH3  : DMA map to TIM1_CH3  
  *         @arg SYSCFG_DMA_MAP_TIM1_CH4  : DMA map to TIM1_CH4  
  *         @arg SYSCFG_DMA_MAP_TIM1_COM  : DMA map to TIM1_COM  
  *         @arg SYSCFG_DMA_MAP_TIM1_UP   : DMA map to TIM1_UP   
  *         @arg SYSCFG_DMA_MAP_TIM1_TRIG : DMA map to TIM1_TRIG 
  *         @arg SYSCFG_DMA_MAP_TIM3_CH1  : DMA map to TIM3_CH1  
  *         @arg SYSCFG_DMA_MAP_TIM3_CH3  : DMA map to TIM3_CH3  
  *         @arg SYSCFG_DMA_MAP_TIM3_CH4  : DMA map to TIM3_CH4  
  *         @arg SYSCFG_DMA_MAP_TIM3_TRG  : DMA map to TIM3_TRG  
  *         @arg SYSCFG_DMA_MAP_TIM3_UP   : DMA map to TIM3_UP   
  *         @arg SYSCFG_DMA_MAP_TIM6_UP   : DMA map to TIM6_UP   
  *         @arg SYSCFG_DMA_MAP_TIM15_CH1 : DMA map to TIM15_CH1 
  *         @arg SYSCFG_DMA_MAP_TIM15_UP  : DMA map to TIM15_UP  
  *         @arg SYSCFG_DMA_MAP_TIM15_TRIG: DMA map to TIM15_TRIG
  *         @arg SYSCFG_DMA_MAP_TIM15_COM : DMA map to TIM15_COM 
  *         @arg SYSCFG_DMA_MAP_TIM16_CH1 : DMA map to TIM16_CH1 
  *         @arg SYSCFG_DMA_MAP_TIM16_UP  : DMA map to TIM16_UP  
  *         @arg SYSCFG_DMA_MAP_TIM17_CH1 : DMA map to TIM17_CH1 
  *         @arg SYSCFG_DMA_MAP_TIM17_UP  : DMA map to TIM17_UP  
  * @param  NewState: new state of the DMA Channelx. 
  *         This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void SYSCFG_DMAChannelRemapConfig(uint32_t SYSCFG_DMAChannelx, uint32_t SYSCFG_DMARemap, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_SYSCFG_DMA_CHANNEL(SYSCFG_DMAChannelx));
  assert_param(IS_SYSCFG_DMA_REMAP(SYSCFG_DMARemap));
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  if(SYSCFG_DMAChannelx == SYSCFG_DMAChannel1)
  {
    SYSCFG->CFGR3 &= ~(0xFF<<SYSCFG_DMAChannelx*8);
    if (NewState != DISABLE)
    {
      SYSCFG->CFGR3 |= SYSCFG_DMARemap<<(SYSCFG_DMAChannelx*8);
      if(SYSCFG_DMARemap < 0x0D)
      {
        DMA_CHANNEL1 -> CCR |= 0x8000UL; //Set DMA CCR TRANS_MODE bit
      }
      else
      {
        DMA_CHANNEL1 -> CCR &= ~(0x8000UL);//CLEAR DMA CCR TRANS_MODE bit
      }
    }  
  }

  else if(SYSCFG_DMAChannelx == SYSCFG_DMAChannel2)
  {
    SYSCFG->CFGR3 &= ~(0xFF<<SYSCFG_DMAChannelx*8);
    if (NewState != DISABLE)
    {
      SYSCFG->CFGR3 |= SYSCFG_DMARemap<<(SYSCFG_DMAChannelx*8);
      if(SYSCFG_DMARemap < 0x0D)
      {
        DMA_CHANNEL2 -> CCR |= 0x8000UL; //Set DMA CCR TRANS_MODE bit
      }
      else
      {
        DMA_CHANNEL2 -> CCR &= ~(0x8000UL);//CLEAR DMA CCR TRANS_MODE bit
      }
    }  
  }

  else if(SYSCFG_DMAChannelx == SYSCFG_DMAChannel3)
  {
    SYSCFG->CFGR3 &= ~(0xFF<<SYSCFG_DMAChannelx*8);
    if (NewState != DISABLE)
    {
      SYSCFG->CFGR3 |= SYSCFG_DMARemap<<(SYSCFG_DMAChannelx*8);
      if(SYSCFG_DMARemap < 0x0D)
      {
        DMA_CHANNEL3 -> CCR |= 0x8000UL; //Set DMA CCR TRANS_MODE bit
      }
      else
      {
        DMA_CHANNEL3 -> CCR &= ~(0x8000UL);//CLEAR DMA CCR TRANS_MODE bit
      }
    }  
  }

  else if(SYSCFG_DMAChannelx == SYSCFG_DMAChannel4)
  {
    SYSCFG_DMAChannelx -= SYSCFG_DMAChannel4;
    SYSCFG->CFGR4 &= ~(0xFF<<SYSCFG_DMAChannelx*8);
    if (NewState != DISABLE)
    {
      SYSCFG->CFGR4 |= SYSCFG_DMARemap<<(SYSCFG_DMAChannelx*8);
      if(SYSCFG_DMARemap < 0x0D)
      {
        DMA_CHANNEL4 -> CCR |= 0x8000UL; //Set DMA CCR TRANS_MODE bit
      }
      else
      {
        DMA_CHANNEL4 -> CCR &= ~(0x8000UL);//CLEAR DMA CCR TRANS_MODE bit
      }
    }  
  }

  else if(SYSCFG_DMAChannelx == SYSCFG_DMAChannel5)
  {
    SYSCFG_DMAChannelx -= SYSCFG_DMAChannel4;
    SYSCFG->CFGR4 &= ~(0xFF<<SYSCFG_DMAChannelx*8);
    if (NewState != DISABLE)
    {
      SYSCFG->CFGR4 |= SYSCFG_DMARemap<<(SYSCFG_DMAChannelx*8);
      if(SYSCFG_DMARemap < 0x0D)
      {
        DMA_CHANNEL5 -> CCR |= 0x8000UL; //Set DMA CCR TRANS_MODE bit
      }
      else
      {
        DMA_CHANNEL5 -> CCR &= ~(0x8000UL);//CLEAR DMA CCR TRANS_MODE bit
      }
    }  
  }  
}

/**
  * @brief  TIMER1 ETR Input source selection
  * @param  SYSCFG_ETR_TIM1: 
  *            @arg SYSCFG_ETR_TIM1_GPIO : ETH come from GPIO
  *            @arg SYSCFG_ETR_TIM1_COMP1: ETH come from COMP1
  *            @arg SYSCFG_ETR_TIM1_COMP2: ETH come from COMP2
  *            @arg SYSCFG_ETR_TIM1_COMP3: ETH come from COMP3
  *            @arg SYSCFG_ETR_TIM1_COMP4: ETH come from COMP4
  * @retval None
  */
void SYSCFG_COMP_ETR_TIM1(uint32_t SYSCFG_ETR_TIM1)
{
  assert_param(IS_SYSCFG_COMP_ETR_TIM1(SYSCFG_ETR_TIM1));

  SYSCFG->CFGR2 &= (~SYSCFG_CFGR2_ETR_SRC_TIM1);
  SYSCFG->CFGR2 |= SYSCFG_ETR_TIM1;
}

/**
  * @brief  COMPx As TIMx break Input enable
  * @param  SYSCFG_COMPX_TIMX: 
  *            @arg SYSCFG_COMP4_BREAK_TIM17: COMP4 output as TIM17 break input
  *            @arg SYSCFG_COMP3_BREAK_TIM17: COMP3 output as TIM17 break input
  *            @arg SYSCFG_COMP2_BREAK_TIM17: COMP2 output as TIM17 break input
  *            @arg SYSCFG_COMP1_BREAK_TIM17: COMP1 output as TIM17 break input
  *            @arg SYSCFG_COMP4_BREAK_TIM16: COMP4 output as TIM16 break input
  *            @arg SYSCFG_COMP3_BREAK_TIM16: COMP3 output as TIM16 break input
  *            @arg SYSCFG_COMP2_BREAK_TIM16: COMP2 output as TIM16 break input
  *            @arg SYSCFG_COMP1_BREAK_TIM16: COMP1 output as TIM16 break input
  *            @arg SYSCFG_COMP4_BREAK_TIM15: COMP4 output as TIM15 break input
  *            @arg SYSCFG_COMP3_BREAK_TIM15: COMP3 output as TIM15 break input
  *            @arg SYSCFG_COMP2_BREAK_TIM15: COMP2 output as TIM15 break input
  *            @arg SYSCFG_COMP1_BREAK_TIM15: COMP1 output as TIM15 break input
  *            @arg SYSCFG_COMP4_BREAK_TIM1 : COMP4 output as TIM1  break input
  *            @arg SYSCFG_COMP3_BREAK_TIM1 : COMP3 output as TIM1  break input
  *            @arg SYSCFG_COMP2_BREAK_TIM1 : COMP2 output as TIM1  break input
  *            @arg SYSCFG_COMP1_BREAK_TIM1 : COMP1 output as TIM1  break input
  * 
  * @retval None
  */
void SYSCFG_COMPx_Break(uint32_t SYSCFG_COMPx_TIMx)
{
  assert_param(IS_SYSCFG_COMPX_TIMX(SYSCFG_COMPx_TIMx));

  SYSCFG->CFGR2 &= (~0x07FFF8);
  SYSCFG->CFGR2 |= SYSCFG_COMPx_TIMx;
}

/**
  * @brief  Connect the selected parameter to the break input of TIM1.
  * @note   The selected configuration is locked and can be unlocked by system reset
  * @param  SYSCFG_Break: selects the configuration to be connected to break
  *         input of TIM1
  *          This parameter can be any combination of the following values:
  *            @arg SYSCFG_Break_PVD: Connects the PVD event to the Break Input of TIM1
  *            @arg SYSCFG_Break_Lockup: Connects Lockup output of CortexM0 to the break input of TIM1.
  * @retval None
  */
void SYSCFG_BreakConfig(uint32_t SYSCFG_Break)
{
  /* Check the parameter */
  assert_param(IS_SYSCFG_LOCK_CONFIG(SYSCFG_Break));

  SYSCFG->CFGR2 |= (uint32_t) SYSCFG_Break;
}

/**
  * @}
  */

/**
  * @}
  */ 

/**
  * @}
  */ 

/**
  * @}
  */ 
