⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 st79_clk.c

📁 st公司新出的一款8位单片机st79的lib库
💻 C
📖 第 1 页 / 共 3 页
字号:
    DownConter--;
  }
  if (DownConter != 0)
  {
    Swif = SUCCESS;
  }
  else
  {
    Swif = ERROR;
  }
  return Swif;
}


/**
  * @brief Enables or disablle the Configurable Clock Output (CCO).
  * @par Full description:
  * CLK_NewState parameter set the CCOEN bit.
  * @param[in] CLK_NewState new state of CCEN bit (CCO register), value accepted ENABLE, DISABLE.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Examples:
  * This example shows how to call the function:
  * @code
  * CLK_CCOCmd(ENABLE)
  * @endcode
  */
void CLK_CCOCmd(FunctionalState CLK_NewState)
{
  if (CLK_NewState != DISABLE)
  {
    /* Set CCOEN bit */
    CLK->CCOR |= CLK_CCOR_CCOEN;
  }
  else
  {
    /* Reset CCOEN bit */
    CLK->CCOR &= (u8)(~CLK_CCOR_CCOEN);
  }

}


/**
  * @brief Checks whether the specified CLK flag is set or not.
  * @par Full description:
  * CLK_FLAG_NUM: specifies the flag to check.
  * This parameter can be set of the following values:
  * - CLK_FLAG_ACTHALT_NUM: Slow active halt enable;
  * - CLK_FLAG_LSIRDY_NUM: LSI oscillator clock ready;
  * - CLK_FLAG_FASTHALTWAKEUP_NUM: Fast halt wake up enable;
  * - CLK_FLAG_HSIRDY_NUM: HSI oscillator clock ready;
  * - CLK_FLAG_HSERDY_NUM: HSE oscillator clock ready;
  * - CLK_FLAG_PLLRDY_NUM: PLL oscillator clock ready;
  * - CLK_FLAG_BYPASS_NUM: PLL Bypass enable;
  * - CLK_FLAG_PLLREF_NUM: PLL reference frequency;
  * - CLK_FLAG_SWIF_NUM: Clock switch interrupt flag;
  * - CLK_FLAG_SWBSY_NUM: Clock switch busy;
  * - CLK_FLAG_CSSD_NUM: Clock security system detection;
  * - CLK_FLAG_AUX_NUM: Auxiliaty oscillator connected as clockmaster;
  * - CLK_FLAG_CCOBSY_NUM: Configurator Clock Output busy;
  * - CLK_FLAG_CCORDY_NUM: Configurator Clock Output ready.
  *  CLK_FLAG_POS parameter specifies the position of the flag to check.
  *  This parameter can be set of the following values:
  *  - CLK_FLAG_ACTHALT_POS: Slow active halt enable;
  *  - CLK_FLAG_LSIRDY_POS: LSI oscillator clock ready;
  *  - CLK_FLAG_FASTHALTWAKEUP_POS: Fast halt wake up enable;
  *  - CLK_FLAG_HSIRDY_POS: HSI oscillator clock ready;
  *  - CLK_FLAG_HSERDY_POS: HSE oscillator clock ready;
  *  - CLK_FLAG_PLLRDY_POS: PLL oscillator clock ready;
  *  - CLK_FLAG_BYPASS_POS: PLL Bypass enable;
  *  - CLK_FLAG_PLLREF_POS: PLL reference frequency;
  *  - CLK_FLAG_SWIF_POS: Clock switch interrupt flag;
  *  - CLK_FLAG_SWBSY_POS: Clock switch busy;
  *  - CLK_FLAG_CSSD_POS: Clock security system detection;
  *  - CLK_FLAG_AUX_POS: Auxiliaty oscillator connected as clockmaster;
  *  - CLK_FLAG_CCOBSY_POS: Configurator Clock Output busy;
  *  - CLK_FLAG_CCORDY_POS: Configurator Clock Output ready.
  * @param[in] CLK_FLAG_NUM Flag to check.
  * @param[in] CLK_FLAG_POS position of flag to check.
  * @retval FlagStatus, status of the checked flag
  * @par Required preconditions:
  * None
  * @par Examples:
  * This example shows how to call the function:
  * @code
  * CLK_GetFlagStatus(CLK_FLAG_PLLREF_NUM, CLK_FLAG_PLLRDY_POS);
  * @endcode
  */
FlagStatus CLK_GetFlagStatus(CLK_ClockFlagNumber_TypeDef CLK_FLAG_NUM, CLK_ClockFlagPosition_TypeDef CLK_FLAG_POS)
{
  u8 statusreg = 0;
  FlagStatus bitstatus = RESET;

  /* Get the CLK register index */

  if (CLK_FLAG_NUM <= 0x03)               /* The flag to check is in ICKRregister */
  {
    statusreg = CLK->ICKR;
  }
  else if (CLK_FLAG_NUM == 0x04)          /* The flag to check is in ECKRregister */
  {
    statusreg = CLK->ECKR;
  }
  else if ((CLK_FLAG_NUM > 0x04) && (CLK_FLAG_NUM <= 0x07))          /* The flag to check is in PLLR register */
  {
    statusreg = CLK->PLLR;
  }
  else if ((CLK_FLAG_NUM > 0x07) && (CLK_FLAG_NUM <= 0x09))          /* The flag to check is in SWIC register */
  {
    statusreg = CLK->SWCR;
  }
  else if ((CLK_FLAG_NUM > 0x09) && (CLK_FLAG_NUM <= 0x0B))          /* The flag to check is in CSS register */
  {
    statusreg = CLK->CSSR;
  }
  else     /* The flag to check is in CCO register */
  {
    statusreg = CLK->CCOR;
  }

  if ((statusreg & (u8)CLK_FLAG_POS) != (u8)RESET)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }

  /* Return the flag status */
  return bitstatus;
}


/**
  * @brief Checks whether the specified CLK interrupt has occurred or not.
  * @par Full description:
  * The CLK_IT parameter specifies the CLK interrupt source to check.
  * it can be set with one of the following values:
  * - CLK_IT_SWIE: Clock Switch Interrupt;
  * - CLK_IT_CSSDIE: Clock Security System Detection interrupt.
  * @param[in] CLK_IT specifies the CLK interrupt.
  * @retval ITStatus, new state of CLK_IT (SET or RESET).
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Examples:
  * This example shows how to call the function:
  * @code
  * ITStatus val;
  * val = CLK_GetITStatus(CLK_IT_SWIE);
  * if (val == RESET) { ... }
  * @endcode
  */
ITStatus CLK_GetITStatus(CLK_ClockInterruptSource_TypeDef CLK_IT)
{
  ITStatus bitstatus = RESET;

  if (CLK_IT == CLK_IT_SWIE)
  {
    /* Check the status of the clock switch interrupt */
    if ((CLK->SWCR & (u8)0x04) != (u8)RESET)
    {
      bitstatus = SET;
    }
    else
    {
      bitstatus = RESET;
    }
  }
  else
  {
    /* Check the status of the security system detection interrupt */
    if ((CLK->CSSR & (u8)0x04) != (u8)RESET)
    {
      bitstatus = SET;
    }
    else
    {
      bitstatus = RESET;
    }
  }

  /* Return the CLK_IT status */
  return  bitstatus;
}


/**
  * @brief Clears the CLK抯 interrupt pending bits.
  * @par Full description:
  * The CLK_IT parameter specifies the interrupt pending bit to clear.
  * This parameter can be one of the following values:
  * - CLK_IT_SWIE: Clock Switch Interrupt;
  * - CLK_IT_CSSDIE: Clock Security System Detection interrupt.
  * @param[in] CLK_IT specifies the interrupt pending bits.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Examples:
  * This example shows how to call the function:
  * @code
  * CLK_ClearITPendingBit(CLK_IT_CSSDIE);
  * @endcode
  */
void CLK_ClearITPendingBit(CLK_InterruptPendingClear CLK_IT)
{
  if (CLK_IT == (u8)CLK_IT_SWIE)
  {
    /* Clear the status of the clock switch interrupt */
    CLK->SWCR &= (u8)(~CLK_SWCR_IEN);
  }
  else
  {
    /* Clear the status of the security system detection interrupt */
    CLK->CSSR  &= (u8)(~CLK_CSSR_CSSDIE);
  }

}

#ifdef HW_PLATFORM_TEST_CHIP
/**
  * @brief Configures the External High Speed oscillator (HSE),
  * (This function is present if HW_PLATFORM_TEST_CHIP is defined).
  * @par Full description:
  * The CLK_StabTime parameter specified the stabilization time.
  * This parameter can be set with one of the following value:
  * - CLK_HSETB_CYCLES_RESET: 4096 HSE quartz stabcycles (reset value);
  * - CLK_HSETB_CYCLES_1: 1 HSE quartz stabcycle;
  * - CLK_HSETB_CYCLES_16: 16 HSE quartz stabcycles;
  * - CLK_HSETB_CYCLES_256: 256 HSE quartz stabcycles;
  * - CLK_HSETB_CYCLES_4096: 4096 HSE quartz stabcycles.
  * The Clk_HSEOsc parameter specifiesthe HSE oscilletor used,
  * (this parameter is present in the Test Chip Oonly).\n
  *  This parameter can be set of the following values:
  * - CLK_HSE_OSC_QUARTZ: HSE oscillator quartz configuration;
  * - CLK_HSE_USER_EXT: HSE oscillator user-external clock configuration.
  * @param[in] CLK_StabTime specifies the stabilization time.
  * @param[in] CLK_HSEOsc specifies the HSE oscillator used.
  * @retval void None.
  * @par Required preconditions:
  * None
  * @par Examples:
  * This example shows how to call the function:
  * @code
  * CLK_HSEConfig_TC(CLK_HSETB_CYCLES_RESET, CLK_HSE_OSC_QUARTZ);
  * @endcode
  */
void CLK_HSEConfig_TC(CLK_StabilizationTime_TypeDef CLK_StabTime, CLK_HSEOscilletorUsed_TypeDef CLK_HSEOsc)
{

  /* Specifies the HSE stabilization time */
  CLK->HSESTBR = (u8)CLK_StabTime;

  /* Specifies the HSE oscillator used */
  if (CLK_HSEOsc != CLK_HSE_USER_EXT)
  {
    CLK->ECKR |= (u8)CLK_HSEOsc;
  }
  else
  {
    (CLK->ECKR& CLK_ECKR_HSEON) == (u8)CLK_HSEOsc;
  }

}
#endif


/**
  * @brief Switch from one clock to another one with or without interrupt,
  * and switch off or not the previous clock.
  * @par Full description:
  * The CLK_NewClockT parameter specifies the future clock sources.
  * It can be set of the following values:
  * - CLK_HSI    (HSI selected).
  * - CLK_LSI    (LSI selected).
  * - CLK_HSE    (HSE selected).
  * - CLK_HSE_EXT  (HSE user ext selected).
  * The CLK_CurrentClock parameter specifies Current clock to switch OFF
  * or to keep ON (DISABLE or ENABLE).\n
  * The CLK_SwitchIT parameter Enable or Disable the Clock Switch interrupt.
  * The Mode parameter select the clock switch mode (CLK_SWITCHMODE_AUTO or CLK_SWITCHMODE_MANUAL).
  * @param[in] NewClock choice of the future clock.
  * @param[in] CLK_CurrentClock current clock to switch OFF or to keep ON.
  * @param[in] CLK_SwitchIT Enable or Disable the Clock Switch interrupt.
  * @param[in] Mode select the clock switch mode.
  * @retval ErrorStatus this shows the clock switch status (ERROR/SUCCESS).
  * @par Required preconditions:
  * None
  * @par Examples:
  * This example shows how to call the function:
  * @code
  * u8 val;
  * val = CLK_SwitchClock(CLK_HSI, DISABLE, DISABLE, CLK_SWITCHMODE_AUTO);
  * if (val == ERROR) { ... }
  * @endcode
  */
ErrorStatus CLK_SwitchClock(CLK_NewClock_TypeDef NewClock, CLK_CurrentClockState_TypeDef CLK_CurrentClock,
                            FunctionalState CLK_SwitchIT, CLK_SwitchMode_TypeDef Mode)
{
  u8 clock_master;
  u16 DownConter = CLK_TIMEOUT;
  ErrorStatus Swif = ERROR;
  
  /* Current clock master saving */
  clock_master = CLK->CMSR;
  
  /* Automatic switch mode management */
  if (Mode == CLK_SWITCHMODE_AUTO)
  {
    /* Enables Clock switch */
    CLK->SWCR |= CLK_SWCR_EN;
    /* Enables or Disables Switch interrupt */
    if (CLK_SwitchIT)
    {
      CLK->SWCR |= CLK_SWCR_IEN;
    }
    /* Selection of the target clock source */
    switch (NewClock)
    {
      case CLK_HSI:
        CLK->SWR = (u8)CLK_SYSCLKSOURCE_HSI;
        break;
      case CLK_LSI:
        CLK->ICKR |= CLK_ICKR_LSIEN;
        CLK->SWR = (u8)CLK_SYSCLKSOURCE_LSI;
        break;
      case CLK_HSE:
        CLK->SWR = (u8)CLK_SYSCLKSOURCE_HSE;
        break;
      case CLK_HSE_EXT:
#ifdef HW_PLATFORM_TEST_CHIP
        CLK->ECKR |= 0x04;
#endif
        CLK->SWR = (u8)CLK_SYSCLKSOURCE_HSE;
        break;
      default:
        break;
    }
    if (CLK_SwitchIT == DISABLE)
    {
      while (((CLK->SWCR & CLK_SWCR_SWBSY) && (DownConter != 0)))
      {
        DownConter--;
      }
      if (DownConter != 0)
      {
        Swif = SUCCESS;
      }
    }

    /* Switch OFF current clock if required */
    if (CLK_CurrentClock == CLK_CURRENT_CLOCK_DISABLE)
    {
      switch (clock_master)
      {
        case CLK_SYSCLKSOURCE_HSI:
          CLK->ICKR &= (u8)(~CLK_ICKR_HSIEN);
          break;
        case CLK_SYSCLKSOURCE_LSI:
          CLK->ICKR &= (u8)(~CLK_ICKR_LSIEN);
          break;
        case CLK_SYSCLKSOURCE_HSE:
          CLK->ECKR &= (u8)(~CLK_ECKR_HSEON);

          break;
        default:
          break;
      }
    }
    Swif = SUCCESS;
  }
  else
  {
    /* Selection of the target clock source for the manual mode */
    switch (NewClock)
    {
      case CLK_HSI:
        CLK->SWR = (u8)CLK_SYSCLKSOURCE_HSI;
        break;
      case CLK_LSI:
        CLK->SWR = (u8)CLK_SYSCLKSOURCE_LSI;
        break;
      case CLK_HSE:
        CLK->SWR = (u8)CLK_SYSCLKSOURCE_HSE;
        break;
      case CLK_HSE_EXT:
#ifdef HW_PLATFORM_TEST_CHIP
        CLK->ECKR |= CLK_ECKR_HSECNF;
#endif
        CLK->SWR = (u8)CLK_SYSCLKSOURCE_HSE;
        break;
      default:
        break;
    }
    /* In manual mode, there is no risk to be stucked in a loop, value returned
      is then always SUCCESS */
    Swif = SUCCESS;
  }
  return Swif;
}

/**
  * @brief Configures the HSI and CPU clock dividers.
  * @param[in] Divider Specifies the HSI or CPU clock divider to apply.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Examples:
  * These examples shows how to call the function:
  * @code
  * CLK_SYSCLKConfig(CLK_HSI_DIV2);
  * or
  * CLK_SYSCLKConfig(CLK_CPU_DIV8);
  * @endcode
  */
void CLK_SYSCLKConfig(CLK_ClockFrequencyDivisor_TypeDef Divider)
{

  if ((Divider & (u8)0x80) == 0x00) /* Bit7 = 0 means HSI divider */
  {
    CLK->CKDIVR &= (u8)(~CLK_CKDIVR_HSIDIV);
    CLK->CKDIVR |= (u8)(Divider & CLK_CKDIVR_HSIDIV);
  }
  else /* Bit7 = 1 means CPU divider */
  {
    CLK->CKDIVR &= (u8)(~CLK_CKDIVR_CPUDIV);
    CLK->CKDIVR |= (u8)(Divider & CLK_CKDIVR_CPUDIV);
  }

}

/**
  * @}
  */

/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -