cancel
Showing results for 
Search instead for 
Did you mean: 

How to use external crystal?

gocchan
Associate III

Hi,

I want to run STM8S003F with an external crystal.

How do I use APIs in stm8_clk?

Or in which material is it described?

Regards,

Gotoda

1 ACCEPTED SOLUTION

Accepted Solutions
WilkoL
Senior

So you see a signal on CLK_CCO. That means that either the crystal oscillator or the internal RC oscillator is functional. Since you see a 2 MHz signal that suggests that it is the internal RC oscillator, as by default its 16 MHz freq is divided by 8. And you do not divide the CLK_CCO signal. The odd thing is that you select HSE as the CLK_CCO signal, that doesn't make sense.

Can you give the code below a try? It works for me, although I did not have a 16 MHz crystal, but used an 8 MHz one.

The oscilloscope shows a (small) 8 MHz signal on both PA1 and PA2, Make sure to use a probe that doesn't load the oscillator too much (10x probe)

void HSE_config(void)
{
  CLK_DeInit();
  
  CLK_HSECmd(ENABLE);
  CLK_LSICmd(DISABLE);
  CLK_HSICmd(DISABLE);
  while(CLK_GetFlagStatus(CLK_FLAG_HSERDY) == FALSE);
 
  CLK_ClockSwitchCmd(ENABLE);
  CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
 
  CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, 
  DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
 
}
 
void CLOCK_output(void)
{
	CLK_CCOConfig(CLK_OUTPUT_CPUDIV8);
	CLK_CCOCmd(ENABLE);
	while(CLK_GetFlagStatus(CLK_FLAG_CCORDY) == FALSE);
}

View solution in original post

7 REPLIES 7
Peter BENSCH
ST Employee

The first resources to read are the RM0016 reference manual, where to search for HSE (High-Speed External Oscillator), and the STM8S003 datasheet.

Then check the Standard Peripheral Firmware Library (SPL) STSW-STM8069, which contains some user manual, describing the function to enable the HSE. And finally you can also check this SPL for the files stm8s_clk.c and stm8s_clk.h, where you will find the function CLK_HSECmd(ENABLE).

Good luck!

If the problem is resolved, please mark this topic as answered by selecting Select as best. This will help other users find that answer faster.

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
WilkoL
Senior

Also read application note AN2752, It even has a reference Design (chapter 7)

gocchan
Associate III

I implemented the following code to see if the external clock works, but I couldn't observe the clock.

void main(void)
{	
	CLK_DeInit();
	CLK_HSECmd(ENABLE);
	while(1);
}

Are there any missing implementations or configs?

gocchan
Associate III

When implemented as follows, it was confirmed that 2MHz was output from the PC4 (CLK_CCO) terminal, but no clock waveform was observed at the OSCOUT terminal.

void main(void)
{
	CLK_DeInit();
	CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);
	CLK_HSECmd(ENABLE);
	CLK_ClockSecuritySystemEnable();
	CLK_CCOConfig(CLK_OUTPUT_HSE); //View output via PC4
	CLK_CCOCmd(ENABLE);
	while(1);
}

I checked the voltage waveforms of OSCOUT and OSCIN with an oscilloscope, but I could not observe 16 MHz.

I am using a 16MHz xtal with 12pF capacitors and 0 ohms resistor.

How can I code this crystal to oscillate?

WilkoL
Senior

So you see a signal on CLK_CCO. That means that either the crystal oscillator or the internal RC oscillator is functional. Since you see a 2 MHz signal that suggests that it is the internal RC oscillator, as by default its 16 MHz freq is divided by 8. And you do not divide the CLK_CCO signal. The odd thing is that you select HSE as the CLK_CCO signal, that doesn't make sense.

Can you give the code below a try? It works for me, although I did not have a 16 MHz crystal, but used an 8 MHz one.

The oscilloscope shows a (small) 8 MHz signal on both PA1 and PA2, Make sure to use a probe that doesn't load the oscillator too much (10x probe)

void HSE_config(void)
{
  CLK_DeInit();
  
  CLK_HSECmd(ENABLE);
  CLK_LSICmd(DISABLE);
  CLK_HSICmd(DISABLE);
  while(CLK_GetFlagStatus(CLK_FLAG_HSERDY) == FALSE);
 
  CLK_ClockSwitchCmd(ENABLE);
  CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
 
  CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, 
  DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
 
}
 
void CLOCK_output(void)
{
	CLK_CCOConfig(CLK_OUTPUT_CPUDIV8);
	CLK_CCOCmd(ENABLE);
	while(CLK_GetFlagStatus(CLK_FLAG_CCORDY) == FALSE);
}

gocchan
Associate III

I can't get out of the CLK_GetFlagStatus() loop.

CLK_ECKR value is 0x01 (HSE clock on but not ready).

CLK_ICKR is 0x03.

Is there a hardware (our custom board) cause?

It means that you enabled power to the HSE, but it isn't oscillating. And because you had the line

CLK_ClockSecuritySystemEnable();

in your code, the MCU switched back to the HSI.

So yes it looks like there is something wrong with the hardware, perhaps a crystal that is broken. Have you tried replacing it? Are the capacitors of the right value?