STM8 library function study notes clock management

Publisher:Harmonious88Latest update time:2015-12-21 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
void CLK_DeInit(void); 

void CLK_HSECmd(FunctionalState NewState); 
void CLK_HSICmd(FunctionalState NewState); 
void CLK_LSICmd(FunctionalState NewState); 
void CLK_CCOCmd(FunctionalState NewState); 
void CLK_ClockSwitchCmd(FunctionalState NewState); 
void CLK_FastHaltWakeUpCmd(FunctionalState NewState); 
void CLK_Slow ActiveHaltWakeUpCmd(FunctionalState NewState); 
void CLK_PeripheralClockConfig(CLK_Peripheral_TypeDef CLK_Peripheral, FunctionalState NewState); 
ErrorStatus CLK_ClockSwitchConfig(CLK_SwitchMode_TypeDef CLK_SwitchMode, CLK_Source_TypeDef CLK_NewClock, FunctionalState

ITState, CLK_CurrentClockState_TypeDef CLK_CurrentClockState); 
void CLK_HSIPrescalerConfig(CLK_Prescaler_TypeDef HSIPrescaler); 
void CLK_CCOConfig(CLK_Output_TypeDef CLK_CCO); void CLK_ITConfig( 
CLK_IT_TypeDef CLK_IT, FunctionalState NewState); void CLK_SYSCLKConfig( 
CLK_Prescaler_TypeDef CLK_Prescaler); void CLK_SWIMConfig(CLK_SWIMDivider_TypeDef CLK_SWIMDivider 
); 
void CLK_CANConfig(CLK_CANDivider_TypeDef CLK_CANDivider) ; 
void CLK_ClockSecuritySystemEnable(void); 
void CLK_SYSCLKEmergencyClear(void); 
void CLK_AdjustHSICalibrationValue(CLK_HSITrimValue_TypeDef CLK_HSICalibrationValue); 
u32 CLK_GetClockFreq(void); 
CLK_Source_TypeDef CLK_GetSYSCLKSource(void); 
FlagStatus CLK_GetFlagStatus(CLK_Flag_TypeDef CLK_FLAG); 
ITStatus CLK_GetITStatus(CLK_IT_ TypeDef CLK_IT); 
void CLK_ClearITPendingBit(CLK_IT_TypeDef CLK_IT);

//------------------------------------------------ ------------------------------------------ 
void CLK_DeInit(void);

Restore the relevant clock registers to their default values

Parameters: None 
Return value: None

Note: 
When resetting the CCOR register, if the CCOEN bit is set, you need to reset the CCOEN bit first and then reset the CCOSEL bit. The operations of resetting the CCOEN bit and resetting the CCOSEL bit must be continuous.

I don't know if this understanding is correct. It is based on the help of the library function, but the test results show that the CCOR register can be restored after the call regardless of whether CCOEN is set.

//------------------------------------------------ ----------------------------------------

void CLK_HSECmd(FunctionalState NewState);

Enable or disable the external high speed oscillator (HSE)

Parameters: 
NewState: new state value 
DISABLE disable 
ENABLE enable

Return value: None

//------------------------------------------------ ----------------------------------------

void CLK_HSICmd(FunctionalState NewState);

Enable or disable the internal high speed oscillator (HSI 16MHz)

Parameters: 
NewState: new state value 
DISABLE disable 
ENABLE enable

Return value: None

//------------------------------------------------ ----------------------------------------

void CLK_LSICmd(FunctionalState NewState);

Enable or disable the internal low speed oscillator (LSI 128KHz)

Parameters: 
NewState: new state value 
DISABLE disable 
ENABLE enable

Return value: None

//------------------------------------------------ ----------------------------------------

void CLK_CCOCmd(FunctionalState NewState);

Enable or disable the configurable clock output function

Parameters: 
NewState: new state value 
DISABLE disable 
ENABLE enable

Return value: None

//------------------------------------------------ ----------------------------------------

void CLK_ClockSwitchCmd(FunctionalState NewState);

Manually enable or disable clock switching

Parameters: 
NewState: new state value 
DISABLE disable 
ENABLE enable

Return value: None

//------------------------------------------------ ----------------------------------------

void CLK_FastHaltWakeUpCmd(FunctionalState NewState);

Enable or disable fast wakeup from Halt or Active Halt mode.

Halt mode: The CPU and on-chip devices completely stop working, the timer wake-up unit AWU also stops, and it can only be awakened by external interrupts and reset.

Active Halt mode: Keep a timer wake-up unit AWU working, CPU and on-chip devices all stop working, AWU and external interrupts and resets can wake up

After enabling, the internal high-speed oscillator (HSI) will automatically turn on and serve as the master clock source (fMASTER) (CKM=SWI=HSI), and then from the Halt mode or Active Halt mode

Wake up quickly.

Return value: None

//------------------------------------------------ ----------------------------------------

void CLK_SlowActiveHaltWakeUpCmd(FunctionalState NewState);

Enable or disable the voltage regulator in active shutdown mode

However, this function does not literally have a wake-up function. It is related to the automatic wake-up (AWU) function.

Once the MCU enters active stop mode, the main voltage regulator will be turned off (DISABLE) by default, so the wake-up time will be longer, so this function is to 
choose .

Parameters: 
NewState: new state value 
DISABLE disable 
ENABLE enable

Return value: None

To quickly wake up from active halt mode, call CLK_SlowActiveHaltWakeUpCmd(ENABLE) before entering active halt; however, the power consumption is high.

If CLK_SlowActiveHaltWakeUpCmd(ENABLE) is not called before entering active halt mode; or CLK_SlowActiveHaltWakeUpCmd(DISABLE) is called; that is, by default

, the wake-up time is longer, but the power consumption is lower.

About AWU (Please refer to the manual for detailed description of the automatic wake-up function)

//------------------------------------------------ ----------------------------------------

void CLK_PeripheralClockConfig(CLK_Peripheral_TypeDef CLK_Peripheral, FunctionalState NewState);

Enable or disable the specified peripheral clock. By default, all peripheral clocks are enabled.

Parameters: 
CLK_Peripheral Peripheral type 
optional values: 
CLK_PERIPHERAL_I2C 
CLK_PERIPHERAL_SPI 
CLK_PERIPHERAL_UART
CLK_PERIPHERAL_UART2 
CLK_PERIPHERAL_UART3 CLK_PERIPHERAL_TIMER6 
CLK_PERIPHERAL_TIMER4 
CLK_PERIPHERAL_TIMER5 
CLK_PERIPHERAL_TIMER2 
CLK_PERIPHERAL_  TIMER3
CLK_PERIPHERAL_TIMER1 
CLK_PERIPHERAL_AWU 
CLK_PERIPHERAL_ADC 
CLK_PERIPHERAL_CAN 

NewState: New state value 
DISABLE Disable 
ENABLE Enable

Return value: None

//------------------------------------------------ ----------------------------------------

ErrorStatus CLK_ClockSwitchConfig(CLK_SwitchMode_TypeDef CLK_SwitchMode, CLK_Source_TypeDef CLK_NewClock, FunctionalState

ITState, CLK_CurrentClockState_TypeDef CLK_CurrentClockState);

System clock switching configuration

parameter:

CLK_SwitchMode, switch mode 
optional values: 
CLK_SWITCHMODE_MANUAL manual switch 
CLK_SWITCHMODE_AUTO automatic switch

CLK_NewClock, new clock source 
optional value: 
CLK_SOURCE_HSI internal 16MHz high speed oscillator 
CLK_SOURCE_LSI internal 128KHz low speed oscillator 
CLK_SOURCE_HSE external clock

ITState, enable or disable the clock switching interrupt. 
Optional values: 
DISABLE Disable 
ENABLE Enable

CLK_CurrentClockState, the currently used clock source is turned off or on after switching to a new clock source. [page]
Optional values: 
CLK_CURRENTCLOCKSTATE_DISABLE Disable 
CLK_CURRENTCLOCKSTATE_ENABLE Enable

Return value: 
If the switch is successful, it returns SUCCESS; otherwise, it returns ERROR 

//------------------------------------------------ ----------------------------------------

void CLK_HSIPrescalerConfig(CLK_Prescaler_TypeDef HSIPrescaler);

Configure the frequency divider of the internal high speed oscillator (HSI)

Parameter: 
HSIPrescaler, frequency division value 
optional value: 
CLK_PRESCALER_HSIDIV1 internal high speed clock 1 frequency division 
CLK_PRESCALER_HSIDIV2 internal high speed clock 2 frequency division 
CLK_PRESCALER_HSIDIV4 internal high speed clock 4 frequency division 
CLK_PRESCALER_HSIDIV8 internal high speed clock 8 frequency division 
CLK_PRESCALER_CPUDIV1 CPU clock 1 frequency division 
CLK_PRESCALER_CPUDIV2 CPU clock 2 frequency division 
CLK_PRESCALER_CPUDIV4 CPU clock 4 frequency division 
CLK_PRESCALER_CPUDIV8 CPU clock 8 frequency division 
CLK_PRESCALER_CPUDIV16 CPU clock 16 frequency division 
CLK_PRESCALER_CPUDIV32 CPU clock 32 frequency division 
CLK_PRESCALER_CPUDIV64 CPU clock 64 frequency division 
CLK_PRESCALER_CPUDIV128 CPU clock divided by 128

Return value: None

//------------------------------------------------ ----------------------------------------

void CLK_CCOConfig(CLK_Output_TypeDef CLK_CCO);

Configure the clock for the specified clock output pin (CCO)

Parameter: 
CLK_CCO: 
Optional values: 
CLK_OUTPUT_HSI Internal high speed oscillator frequency 16MHz/division value (HSIDIV) 
CLK_OUTPUT_LSI Internal low speed oscillator 128KHz 
CLK_OUTPUT_HSE External oscillator 
CLK_OUTPUT_CPU CPU clock 
CLK_OUTPUT_CPUDIV2 CPU clock 2 division 
CLK_OUTPUT_CPUDIV4 CPU clock 4 division 
CLK_OUTPUT_CPUDIV8 CPU clock 8 division 
CLK_OUTPUT_CPUDIV16 CPU clock 16 division CLK_OUTPUT_CPUDIV32 
CPU clock 32 division 
CLK_OUTPUT_CPUDIV64 CPU clock 64 division 
CLK_OUTPUT_HSIRC fHSI 
CLK_OUTPUT_MASTER fMASTER 
CLK_OUTPUT_OTHERS fCPU

Return value: None

The designated clock output pin must be set to pull-up input or push-pull output mode by setting the corresponding bit in the configuration register Px_CR1 to 1.

//------------------------------------------------ ----------------------------------------

void CLK_ITConfig(CLK_IT_TypeDef CLK_IT, FunctionalState NewState);

Enable or disable the specified clock interrupt

Parameters: 
CLK_IT, interrupt source, interrupt configuration and flags are cleared by software. 
Optional values: 
CLK_IT_CSSD Clock security system detection flag 
CLK_IT_SWIF Clock switch interrupt flag

NewState: New state value 
DISABLE Disable 
ENABLE Enable

Return value: None

//------------------------------------------------ ----------------------------------------

void CLK_SYSCLKConfig(CLK_Prescaler_TypeDef CLK_Prescaler);

Configure the internal high-speed oscillator (HSI) and CPU clock divider

Parameter: 
CLK_Prescaler, specifies the HSI or CPU clock prescaler value. 
Optional values: 
CLK_PRESCALER_HSIDIV1 Internal high speed oscillator 1 divided by 
CLK_PRESCALER_HSIDIV2 Internal high speed oscillator 2 divided by 
CLK_PRESCALER_HSIDIV4 Internal high speed oscillator 4 divided by 
CLK_PRESCALER_HSIDIV8 Internal high speed oscillator 8 divided by 
CLK_PRESCALER_CPUDIV1 CPU clock 1 divided by 
CLK_PRESCALER_CPUDIV2 CPU clock 2 divided by 
CLK_PRESCALER_CPUDIV4 CPU clock 4 divided by 
CLK_PRESCALER_CPUDIV8 CPU clock 8 divided by 
CLK_PRESCALER_CPUDIV16 CPU clock 16 divided by 
CLK_PRESCALER_CPUDIV32 CPU clock 32 divided by 
CLK_PRESCALER_CPUDIV64 CPU clock 64 Frequency division 
CLK_PRESCALER_CPUDIV128 CPU clock 128 frequency division

Return value: None

//------------------------------------------------ ----------------------------------------

void CLK_SWIMConfig(CLK_SWIMDivider_TypeDef CLK_SWIMDivider);

Configuring the SWIM clock divider

Parameter: 
CLK_SWIMDivider, SWIM clock division value. 
Optional values: 
CLK_SWIMDIVIDER_2 SWIM clock 2 division 
CLK_SWIMDIVIDER_OTHER SWIM clock not divided

Return value: None

//------------------------------------------------ ----------------------------------------

void CLK_CANConfig(CLK_CANDivider_TypeDef CLK_CANDivider);

Configuring the CAN clock divider

parameter:

CLK_CANDivider, CAN clock divider value 
optional values: 
CLK_CANDIVIDER_1 CAN clock frequency = HSE/1  
CLK_CANDIVIDER_2 CAN clock frequency = HSE/2  
CLK_CANDIVIDER_3 CAN clock frequency = HSE/3  
CLK_CANDIVIDER_4 CAN clock frequency = HSE/4  
CLK_CANDIVIDER_5 CAN clock frequency = HSE/5  
CLK_CANDIVIDER_6 CAN clock frequency = HSE/6  
CLK_CANDIVIDER_7 CAN clock frequency = HSE/7  
CLK_CANDIVIDER_8 CAN clock frequency = HSE/8 
Return value: None

//------------------------------------------------ ----------------------------------------

void CLK_ClockSecuritySystemEnable(void);

Enables the clock safety system. Once enabled it cannot be disabled until the next reset.

Parameters: None

Return value: None

//------------------------------------------------ ----------------------------------------

void CLK_SYSCLKEmergencyClear(void);

Reset the clock switch busy (SWBSY) flag to reset the clock switch process. 
If the clock switch is not successful for some reason, the software can reset the current switch operation by clearing the flag SWBSY 
to restore the register CLK_SWR to its original value (original clock source). 
If the software tries to set SWEN and reset SWBSY at the same time, the action of resetting SWBSY takes precedence.

Parameters: None

Return value: None

//------------------------------------------------ ----------------------------------------

void CLK_AdjustHSICalibrationValue(CLK_HSITrimValue_TypeDef CLK_HSICalibrationValue);

Correction of internal high speed oscillator frequency

parameter:

CLK_HSICalibrationValue, calibration value. 
Optional values: 
CLK_HSITRIMVALUE_0 calibration value is 0  
CLK_HSITRIMVALUE_1 calibration value is 1  
CLK_HSITRIMVALUE_2 calibration value is 2  
CLK_HSITRIMVALUE_3 calibration value is 3  
CLK_HSITRIMVALUE_4 calibration value is 4  
CLK_HSITRIMVALUE_5 calibration value is 5  
CLK_HSITRIMVALUE_6 calibration value is 6  
CLK_HSITRIMVALUE_7 calibration value is 7 
Return value: None

Note: On high-density products, only bits 2:0 are available. 
On medium-density or low-density products, bits 3:0 or 2:0 are available, depending on the option byte configuration (see the data sheet).

//------------------------------------------------ ----------------------------------------

u32 CLK_GetClockFreq(void);

Get the system master clock frequency

Parameters: None

Return value: 32-bit master clock frequency

//------------------------------------------------ ----------------------------------------

CLK_Source_TypeDef CLK_GetSYSCLKSource(void);

Get the system clock source

Parameters: None

Return value: 
CLK_SOURCE_HSI The clock source is the internal high speed oscillator  
CLK_SOURCE_LSI The clock source is the internal low speed oscillator  
CLK_SOURCE_HSE The clock source is the external high speed oscillator 

//------------------------------------------------ ----------------------------------------

FlagStatus CLK_GetFlagStatus(CLK_Flag_TypeDef CLK_FLAG);

Get the setting status of the specified clock flag

parameter:


CLK_FLAG, optional flag values  ​​to be obtained  :
CLK_FLAG_LSIRDY Internal low-speed oscillator ready flag 
CLK_FLAG_HSIRDY Internal high-speed oscillator ready flag 
CLK_FLAG_HSERDY External high-speed oscillator ready flag 
CLK_FLAG_SWIF Clock switch interrupt flag 
CLK_FLAG_SWBSY Clock switch busy flag 
CLK_FLAG_CSSD System clock safety detection flag 
CLK_FLAG_AUX Auxiliary oscillator switch status, (if the auxiliary oscillator (HSI/8) is turned on and used as the current main clock source) 
CLK_FLAG_CCOBSY Configurable clock output busy, used to indicate that the selected CCO clock source is in the switching state 
CLK_FLAG_CCORDY Configurable clock output ready, used to indicate that the selected CCO clock source is in a stable state

Return value: 
RESET reset state. 
SET set state.

//------------------------------------------------ ----------------------------------------

ITStatus CLK_GetITStatus(CLK_IT_TypeDef CLK_IT);

Get the specified clock interrupt enable status

Parameters: 
CLK_IT, clock interrupt type 
optional value: 
CLK_IT_CSSD system clock safety detection flag 
CLK_IT_SWIF clock switch interrupt flag

Return value: 
RESET reset state. 
SET set state.

//------------------------------------------------ ----------------------------------------

void CLK_ClearITPendingBit(CLK_IT_TypeDef CLK_IT);

Clear the clock interrupt flag

Parameter: 
CLK_IT, clock interrupt flag 
optional value: 
CLK_IT_CSSD system clock safety detection flag 
CLK_IT_SWIF clock switch interrupt flag

Return value: None

//------------------------------------------------ ----------------------------------------

Keywords:STM8 Reference address:STM8 library function study notes clock management

Previous article:STM8 study notes - initial understanding
Next article:STM8 library function study notes ADC

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号