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

📄 st79_clk.c

📁 st公司新出的一款8位单片机st79的lib库
💻 C
📖 第 1 页 / 共 3 页
字号:
/**
  ******************************************************************************
  * @file st79_clk.c
  * @brief This file contains all the functions for the CLK peripheral.
  * @author STMicroelectronics - MCD & APG Car Body Application Labs
  * @version V0.01
  * @date 04-JUL-2007
  ******************************************************************************
  *
  * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2007 STMicroelectronics</center></h2>
  * @image html logo.bmp
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/

#include "st79_clk.h"

/* LINKER SECTIONS DEFINITION FOR THIS FILE ONLY */
#pragma section (CLK_CODE)
#pragma section const {CLK_CONST}
#pragma section @near [CLK_URAM]
#pragma section @near {CLK_IRAM}
#pragma section @tiny [CLK_UZRAM]
#pragma section @tiny {CLK_IZRAM}

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

/* Private Constants ---------------------------------------------------------*/

/**
  * @addtogroup CLK_Private_Constants
  * @{
  */

uc8 HSIDivFactor[4] = {1, 2, 4, 8}; /*!< Holds the different HSI Dividor factors */
uc8 ADCPrescTable[8] = {1, 2, 4, 8, 10, 16, 20, 40}; /*!< Holds the different ADC prescaler values */

/**
  * @}
  */

/* Public functions ----------------------------------------------------------*/

/**
  * @addtogroup CLK_Public_Functions
  * @{
  */

/**
  * @brief  Initializes the CLK peripheral.
  * @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
  * 
  * If CLK_FastHaltWakeup is enabled, HSI oscillator is automatically
  * switched-on (HSION=1b) and selected as next clock master
  * (CKM=SWI=HSI) when resuming from HALT/ActiveHalt modes.\n
  * 
  * The HSIPrescaler parameter select a prescaler if FutureClock=HSI else select
  * CLK_HSI8. This parameter can be set of following values:
  * - CLK_HSI_DIV1 (clock prescaler 1).
  * - CLK_HSI_DIV2 (clock prescaler 2).
  * - CLK_HSI_DIV4 (clock prescaler 4).
  * - CLK_HSI_DIV8 (clock prescaler 8).
  * 
  * 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).
  * 
  * The StartupTime parameter select the stabilization time required by the crystal (valid for
  * LSE and HSE only).\n
  * This parameter can be set of the values  of following values:
  * - CLK_HSETB_CYCLES_RESET  (HSE Stabilization time, Cycles Reser).
  * - CLK_HSETB_CYCLES_1      (HSE Stabilization time, Cycles 1).
  * - CLK_HSETB_CYCLES_16     (HSE Stabilization time, Cycles 16).
  * - CLK_HSETB_CYCLES_256    (HSE Stabilization time, Cycles 256).
  * - CLK_HSETB_CYCLES_4096   (HSE Stabilization time, Cycles 4096).
  * 
  * The CSSConfig parameter enables the CSS with or without Interrupt. \n
  * This parameter can be set of following values:
  * CLK_CSSR_CSSON_DIE (Enable CSS with detection interrupt).
  * CLK_CSSR_CSSON     (Enable CSS without detection interrupt).
  * CLK_CSSR_CSSOFF    (Leave CSS desactivated).
  * @param[in] NewClock choice of the future clock.
  * @param[in] CLK_CurrentClock current clock to switch OFF or to keep ON.
  * @param[in] CLK_FastHaltWakeup set the Wake-up Mode.
  * @param[in] HSIPrescaler Select a prescaler if FutureClock=HSI else select
  * CLK_HSI8.
  * @param[in] CLK_SwitchIT Enable or Disable the Clock Switch interrupt.
  * @param[in] Mode select the clock switch mode.
  * @param[in] StartupTime stabilization time required by the crystal.
  * @param[in] CLK_NewState Enables the CSS with or without Interrupt.
  * @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_Init(CLK_SYSCLKSOURCE_HSI, ENABLE, DISABLE, CLK_HSI_DIV2, DISABLE, CLK_SWITCHMODE_AUTO, CLK_HSETB_CYCLES_16, CLK_CSSR_CSSON);
  * if (val == ERROR) { ... }
  * @endcode
  */
ErrorStatus CLK_Init(CLK_NewClock_TypeDef NewClock, CLK_CurrentClockState_TypeDef CLK_CurrentClock,
                     FunctionalState CLK_FastHaltWakeup, CLK_ClockFrequencyDivisor_TypeDef HSIPrescaler,
                     FunctionalState CLK_SwitchIT, CLK_SwitchMode_TypeDef Mode,
                     CLK_StartupTime_TypeDef StartupTime, CLK_CSSConfig_TypeDef CLK_NewState)
{
  ErrorStatus Status = ERROR;
  /* Set HSE startup time if required  */
#ifdef  HW_PLATFORM_TEST_CHIP
  if (NewClock == CLK_HSE)
  {
    CLK->HSESTBR = (u8)(StartupTime);
  }
#endif
  /* Configure the CSS with or without IT  */
  CLK_ClockSecuritySystemCmd(CLK_NewState);
  /* Switch the clock if necessary or select the required HSI prescaler */
  if (NewClock != CLK_HSI)
  {
    Status = CLK_SwitchClock(NewClock, CLK_CurrentClock, CLK_SwitchIT, Mode);
  }
  else
  {
    CLK_HSIConfig(CLK_FastHaltWakeup, HSIPrescaler);
    Status = SUCCESS;
  }
  return Status;
}

/**
  * @brief Deinitializes the CLK peripheral registers to their default reset
  * values.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * This example shows how to call the function:
  * @code
  * CLK_DeInit();
  * @endcode
  */
void CLK_DeInit(void)
{
  ErrorStatus Status;

  /* Clears of the HSI prescaler bits part */
  CLK->CKDIVR &= (u8)(~CLK_CKDIVR_HSIDIV);
  /* Sets the new HSI prescaler */
  CLK->CKDIVR |= CLK_CKDIVR_HSIDIV;

  /* HSI Trimming with a 5-bit value by the user */
  CLK->HSITRIMR = (CLK_HSITRIMR_RESET_VALUE & CLK_HSITRIMR_HSITRIM);

  /* Switch on HSI if necessary */
  if (CLK->CMSR !=  (u8)CLK_SYSCLKSOURCE_HSI)
  {
    Status = CLK_SwitchClock(CLK_HSI, CLK_CURRENT_CLOCK_DISABLE, DISABLE, CLK_SWITCHMODE_AUTO);
  }
  CLK->SWCR = CLK_SWCR_RESET_VALUE;

  /* Clears of CPU prescaler part */
  CLK->CKDIVR &= (u8)(~CLK_CKDIVR_CPUDIV);
  /* Set the chosen CPU prescaler */
  CLK->CKDIVR |= CLK_CKDIVR_RESET_VALUE;

  CLK->CCOR &= (u8)(~CLK_CCOR_CCOSEL);
  CLK->CCOR &= (u8)(~CLK_CCOR_CCOEN);

  CLK->PCKEN1R = CLK_PCKEN1R_RESET_VALUE;
  CLK->PCKEN2R = CLK_PCKEN2R_RESET_VALUE;

  /* Set HSESTR and LSESTR at their reset value */
#ifdef HW_PLATFORM_TEST_CHIP
  CLK->HSESTBR = CLK_HSESTBR_RESET_VALUE;
  CLK->PLLR &= (u8)(~CLK_PLLR_PLLON);
  CLK->PLLR = CLK_PLLR_RESET_VALUE;
#endif

  CLK->ICKR = CLK_ICKR_RESET_VALUE;
  CLK->ECKR &= (u8)(~CLK_ECKR_HSEON);
}

/**
  * @brief Starts or Stops manually the clock switch execution.
  * @par Full description:
  * CLK_NewState parameter set the SWEN.
  * @param[in] CLK_NewState new state of SWEN, value accepted ENABLE, DISABLE.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Examples:
  * This example shows how to call the function:
  * @code
  * CLK_ClockSwitchConfig(ENABLE);
  * @endcode
  */

void CLK_ClockSwitchConfig(FunctionalState CLK_NewState)
{
  if (CLK_NewState)
  {
    CLK->SWCR |= CLK_SWCR_EN;
  }
  else
  {
    CLK->SWCR &= (u8)(~CLK_SWCR_EN);
  }
}


/**
  * @brief  Configures the High Speed Internal oscillator (HSI).
  * @par Full description:
  * The CLK_FastHaltWakeup parameter specifies the Fast Halt Wake-Up mode.
  * It can be set of the following values:
  * - DISABLE: Fast HALT-wake up mode disabled;
  * - ENABLE:  Fast HALT-wake up mode enabled.\n
  * 
  * If CLK_FastHaltWakeup is enabled, HSI oscillator is automatically
  * switched-on (HSION=1b) and selected as next clock master
  * (CKM=SWI=HSI) when resuming from HALT/ActiveHalt modes.\n
  * The HSIPrescaler parameter specified the Clock Divisor, can be set of the following values:
  * - CLK_HSI_Div1: Set HSI clock frequency divided by 1;
  * - CLK_HSI_Div2: Set HSI clock frequency divided by 2;
  * - CLK_HSI_Div4: Set HSI clock frequency divided by 4;
  * - CLK_HSI_Div8: Set HSI clock frequency divided by 8 (reset value).
  * 
  * Hardware control overcomes software modifications in such cases:
  * - AUX = CSSD = 1b reset (HSI/8) configuration is forced;
  * - while entering test mode configuration HSIDIV automatic
  * switches to HSIDIV/1 initialization value.
  * @param[in] CLK_FastHaltWakeup this parameter is the Wake-up Mode.
  * @param[in] HSIPrescaler this second parameter is the Clock Divisor.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Examples:
  * This example shows how to call the function:
  * @code
  * CLK_HSIConfig(ENABLE, CLK_HSI_DIV2);
  * @endcode
  */
void CLK_HSIConfig(FunctionalState CLK_FastHaltWakeup, CLK_ClockFrequencyDivisor_TypeDef HSIPrescaler)
{

  /* Clear High speed internal clock prescaler */
  CLK->CKDIVR &= (u8)(~CLK_CKDIVR_HSIDIV);
  
  /* Set High speed internal clock prescaler */
  CLK->CKDIVR |= (u8)(HSIPrescaler & CLK_CKDIVR_HSIDIV);

  if (CLK_FastHaltWakeup != DISABLE)
  {
    /* Set FHW bit (HSI oscillator is automatically switched-on) */
    CLK->ICKR |= CLK_ICKR_FHW;
  }
  else  /* FastHaltWakeup = DISABLE */
  {
    /* Reset FHW bit */
    CLK->ICKR &= (u8)(~CLK_ICKR_FHW);
  }
}

/**
  * @brief Configures the Low Speed Internal oscillator (LSI).
  * @par Full description:
  * CLK_SlowActiveHalt parameter specifies the Halt mode, can be set of the following values:
  * - DISABLE: Slow Active Halt mode disabled;
  * - ENABLE:  Slow Active Halt mode enabled.
  * @param[in] CLK_SlowActiveHalt specifies the Fast or Slow Active Halt mode.
  * @retval  void None
  * @par Required preconditions:
  * None
  * @par Examples:
  * This example shows how to call the function:
  * @code
  * CLK_LSIConfig(ENABLE);
  * @endcode
  */
void CLK_LSIConfig(FunctionalState CLK_SlowActiveHalt)
{
  if (CLK_SlowActiveHalt != DISABLE)
  {
    /* Set S_ACTHALT bit */
    CLK->ICKR |= CLK_ICKR_SHW;
  }
  else
  {
    /* Reset S_ACTHALT bit */
    CLK->ICKR &= (u8)(~CLK_ICKR_SHW);
  }

}

/**
  * @brief Adjusts the Internal High Speed oscillator (HSI) calibration value.
  * @par Full description:
  * CLK_HSICalibrationValue parameter specifies the calibration trimming value.
  * - This parameter must be set with pf the following values:
  * - CLK_HSI_CALIB_VALUE_0:  calibration value 0;
  * - CLK_HSI_CALIB_VALUE_1:  calibration value 1;
  * - CLK_HSI_CALIB_VALUE_2:  calibration value 2;
  * - CLK_HSI_CALIB_VALUE_3:  calibration value 3;
  * - CLK_HSI_CALIB_VALUE_4:  calibration value 4;
  * - CLK_HSI_CALIB_VALUE_5:  calibration value 5;
  * - CLK_HSI_CALIB_VALUE_6:  calibration value 6;
  * - CLK_HSI_CALIB_VALUE_7:  calibration value 7;
  * - CLK_HSI_CALIB_VALUE_8:  calibration value 8;
  * - CLK_HSI_CALIB_VALUE_9:  calibration value 9;
  * - CLK_HSI_CALIB_VALUE_10: calibration value 10;
  * - CLK_HSI_CALIB_VALUE_11: calibration value 11;
  * - CLK_HSI_CALIB_VALUE_12: calibration value 12;
  * - CLK_HSI_CALIB_VALUE_13: calibration value 13;
  * - CLK_HSI_CALIB_VALUE_14: calibration value 14;
  * - CLK_HSI_CALIB_VALUE_15: calibration value 15.
  * @param[in] CLK_HSICalibrationValue calibration trimming value.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Examples:
  * This example shows how to call the function:
  * @code
  * CLK_AdjustHSICalibrationValue(CLK_HSI_CALIB_VALUE_5)
  * @endcode
  */
void CLK_AdjustHSICalibrationValue(CLK_HSICalibrationValue_TypeDef CLK_HSICalibrationValue)
{

  /* Clear HSITRIM[3:0] bits */
  CLK->HSITRIMR &= CLK_HSITRIMR_HSITRIM;

  /* Store the new value */
  CLK->HSITRIMR |= (u8)CLK_HSICalibrationValue;

}

/**
  * @brief Enable or Disable the External High Speed oscillator (HSE).
  * @par Full description:
  * CLK_NewState parameter set the HSEON bit and this
  * can be set both by software and hardware.\n
  * HSEON is set and kept asserted by HW right of priority in such cases:\n
  * - HSE selected as the reference of active PLL (PLLON=1b & PLLREF=1b);
  * - Clock switch to HSE is on going (CKM!=SWI & SWI=HSE);
  * - HSE selected by active CCO (CCOBSY=1b & CCOSEL=4h);
  * - HSE is clock master (CKM=HSE).\n
  * 
  * HSEON is cleared and kept de-asserted by HW right of priority
  * in such cases:
  * - CSSD=1b.
  * @param[in] CLK_NewState new state of HSEON, value accepted ENABLE, DISABLE.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Examples:
  * This example shows how to call the function:
  * @code
  * CLK_HSECmd(ENABLE);
  * @endcode
  */
void CLK_HSECmd(FunctionalState CLK_NewState)
{
  if (CLK_NewState != DISABLE)
  {
    /* Set HSION bit */
    CLK->ECKR |= CLK_ECKR_HSEON;
  }
  else
  {
    /* Reset HSION bit */
    CLK->ECKR &= (u8)(~CLK_ECKR_HSEON);
  }
}

/**
  * @brief Enables or disables the Internal High Speed oscillator (HSI).
  * @par Full description:
  * CLK_NewState parameter set the HSION bit and this
  * can be set both by software and hardware.\n
  * HSION is set and kept asserted by HW right of priority in such cases:
  * - hsi_req input is asserted;
  * - CSS safe oscillator is on (AUX=1b);
  * - when exiting HALT/ActiveHalt by fast-halt-wakeup configuration (FHW=1b);
  * - HSI selected by active CCO (CCOBSY=1b & CCOSEL=0h,Bh);
  * - HSI selected as the reference of active PLL (PLLON=1b & PLLREF=0b);
  * - clock-switch to HSI is on going (CKM!=SWI & SWI=HSI);
  * - HSI is clock master (CKM=HSI).
  * @param[in] CLK_NewState new state of HSION, value accepted ENABLE, DISABLE.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Examples:
  * This example shows how to call the function:
  * @code
  * CLK_HSICmd(ENABLE);
  * @endcode
  */
void CLK_HSICmd(FunctionalState CLK_NewState)
{
  if (CLK_NewState != DISABLE)
  {
    /* Set HSION bit */
    CLK->ICKR |= CLK_ICKR_HSIEN;
  }
  else
  {
    /* Reset HSION bit */
    CLK->ICKR &= (u8)(~CLK_ICKR_HSIEN);
  }
}


/**
  * @brief Enables or disables the Internal Low Speed oscillator (LSI).
  * @par Full description:
  * CLK_NewState parameter set the LSION bit and this
  * can be set both by software and hardware.\n
  * LSION is set and kept asserted by HW right of priority in such cases:
  * - lsi_req input is asserted;
  * - awu_lsi_req input is asserted;
  * - LSI selected by active CCO (CCOBSY=1b & CCOSEL=1h);

⌨️ 快捷键说明

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