esp-idf icon indicating copy to clipboard operation
esp-idf copied to clipboard

External 32.768 kHz oscillators (IDFGH-8843)

Open NoNullptr opened this issue 3 years ago • 7 comments

Answers checklist.

  • [X] I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • [X] I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • [X] I have searched the issue tracker for a similar issue and not found a similar issue.

General issue report

I need to evaluate ESP32-S3 with an external oscillator and ran into multiple issues.

  1. The Programming Guide states: External 32 kHz oscillator at XTAL_32K_P pin: Allows using 32 kHz clock generated by an external circuit. The external clock signal must be connected to the XTAL_32K_P pin. The amplitude should be less than 1.2 V for sine wave signal and less than 1 V for square wave signal. Common mode voltage should be in the range of 0.1 < Vcm < 0.5xVamp, where Vamp stands for signal amplitude. In this case, the XTAL_32K_P pin cannot be used as a GPIO pin.
  • One minor issue is that it should say 32.768 kHz instead of 32 kHz, which is a standard clock oscillator frequency and all other documentation also refers to this frequency being the correct one.
  • Let's call an oscillator's output high voltage Voh and an oscillator's output low voltage Vol. Then the amplitude is Voh - Vol and the common mode voltage is 0.5 * (Voh + Vol). The requirement Vcm < 0.5xVamp translates to 0.5 * (Voh + Vol) < 0.5 * (Voh - Vol), which means Vol < 0. Does the ESP32-S3 really require the low output voltage to be negative, below ground? That cannot be satisfied with any standard oscillator or clock signal and seems wrong. Standard oscillators require some headroom above ground, so the output low voltage is for example 10% of Vdd. Since no other documentation lists the oscillator requirements, it's impossible to find out if an oscillator swinging between, say, 300 mV and 900 mV are supported or not. One interpretation could be that the documentation writer used the terms amplitude and common mode without knowing their meaning.
  1. The configuration options RTC_CLK_SRC_EXT_CRYS and RTC_CLK_SRC_EXT_OSC are only used to call either rtc_clk_32k_enable(true) or rtc_clk_32k_enable_external().
  • Both functions touch RTC_IO_XTAL_32N_PAD_REG, although only XTAL_32K_P should be used for an external oscillator.
  • Both functions mainly call clk_ll_xtal32k_enable, which is left in an abandoned state with a "TODO". It currently only sets up the RTC registers for an external crystal, not for an external oscillator, for example the register RTC_CNTL_DBUF_XTAL_32K is set to 1, but should very likely be set to 0.
  • The Technical Reference Manual does not even try to document most of the RTC registers related to the external 32k crystal or oscillator and is severely lacking in this regard. For example RTC_CNTL_DGM_XTAL_32K, RTC_CNTL_DAC_XTAL_32K, RTC_CNTL_DRES_XTAL_32K have no documentation, so it's impossible to correctly implement a working example either.
  1. For an external crystal, the chip needs to run a crystal oscillator circuit. The documentation states that this will increase sleep current consumption by 1 µA. Considering that the drive current depends on the used crystal and required load capacitors, this number can only refer to the internal amplifiers in the oscillator circuit. If this is correct, the documentation should mention this and also mention that the current draw of the crystal and load capacitors can be significantly higher than 1 µA unless carefully selected.
  • Is it possible for the crystal oscillator circuit to be gated / turned off when using a single-ended external oscillator, so that the additional cost of 1 µA can be avoided?

NoNullptr avatar Nov 29 '22 11:11 NoNullptr

Will anyone ever look at this issue?

NoNullptr avatar Feb 06 '23 09:02 NoNullptr

Having made a board with an ESP32-S3 plus external oscillator work, I can say that is is possible. I didn't need to add any additional modifications of the RTC setup, but I also did notice there was no difference between the xtal and oscillator in how the rtc is programmed.

The documentation is wrong about connecting the external oscillator to XTAL_32K_P! It needs to be connected to XTAL_32K_N.

Maybe this difference of N vs P could be changed with a different setup of the RTC registers, but these specific registers aren't documented so I can't say.

xyzzy42 avatar Apr 02 '24 00:04 xyzzy42

Hi @xyzzy42,

Thank you for your feedback about our documentation. Regarding your comment:

The documentation is wrong about connecting the external oscillator to XTAL_32K_P! It needs to be connected to XTAL_32K_N.

Our technical team has confirmed that the documentation reflects the correct connection for the external oscillator. The XTAL_32K_P pin is indeed the correct connection point, while the XTAL_32K_N pin is not supported.

To better understand the issue you've encountered, could you please provide us with details regarding your hardware environment and software configuration? This will help us reproduce and address the issue. Looking forward to your reply!

esp-momo avatar Apr 09 '24 07:04 esp-momo

When using the following circuit, with the ESP32-S3-PICO-1-N8R2 module, does not work: image image

The RTC fails to initialize and the 150 kHz RC oscillator is used instead.

I (622) clk: waiting for 32k oscillator to start up
I (1134) clk: waiting for 32k oscillator to start up
W (1646) clk: 32 kHz XTAL not found, switching to internal 150 kHz oscillator
I (1683) clk: RTC_SLOW_CLK calibration value: 3605547

It was then changed to this circuit. After doing this, it works as expected. image

Before board design was finished, I found an issue that ESP32 (not ESP32-S3) documentation originally stated that XTAL_32_P was to be used, but was then changed to XTAL_32_N. So the design included pads to re-route the oscillator in case it was necessary.

xyzzy42 avatar Apr 09 '24 08:04 xyzzy42

Hi @xyzzy42, thank you for the reply. Would you please also send us the complete schematic with the chip part?

esp-momo avatar Apr 19 '24 09:04 esp-momo

Given that the HAL driver for esp32-s3 for the xtal32k initialisation (clk_ll_xtal32k_enable) includes this comment https://github.com/espressif/esp-idf/blob/0453e8608bde98133a427a74ae61d272770b1bfd/components/hal/esp32s3/include/hal/clk_tree_ll.h#L99-L102 indicates that the 32k clock input is not configured properly. Looking into the TRM, I would guess that at least the RTC_CNTL_DBUF_XTAL_32K 0: single-end buffer 1: differential buffer (R/W) should be set to 0 when external clock source is used. Unfortunately, the other register fields RTC_CNTL_DAC_XTAL_32K, RTC_CNTL_DRES_XTAL_32K, RTC_CNTL_DGM_XTAL_32K from the RTC_CNTL_RTC_EXT_XTL_CONF_REG are missing their description in the TRM, so its hard to tell which value they should be. By default, the code sets them all to 3. https://github.com/espressif/esp-idf/blob/0453e8608bde98133a427a74ae61d272770b1bfd/components/hal/esp32s3/include/hal/clk_tree_ll.h#L40-L45 Looking through the code, the ESP32 shares some register field names with the S3 and has some comments for them https://github.com/espressif/esp-idf/blob/0453e8608bde98133a427a74ae61d272770b1bfd/components/hal/esp32/include/hal/clk_tree_ll.h#L237-L238 Guessing from the context, the DGM in RTC_CNTL_DGM_XTAL_32K might stand for Differential Gain Multiplier?

My theory for why it doesn't work when connected to XTAL_32K_P but works on XTAL_32K_N is that when the 3,3V squarewave from the clock is amplified by the (incorrectly set) gain, it saturates to always high, but when the signal is brought onto the XTAL_32K_N, it capacitively couples/induces a very weak signal onto the XTAL_32K_P, which, when amplified, falls within the expected signal levels.

I am currently trying to implement the same as @xyzzy42, so I will experiment with the RTC_CNTL_RTC_EXT_XTL_CONF_REG values and let you know if anything helps.

LonerDan avatar May 30 '24 10:05 LonerDan

Any updates? :/

Szybet avatar Jul 29 '24 18:07 Szybet

I am trying to connect my S3 (Seeed Xiao S3 board) to an external oscillator but the obtained frequency isn't stable. I have connected the XTAL_32K_P pin to the 32768 Hz oscillator of an external board through a 22pF capacitor (as indicated in the hardware design guide):

The external signal can be input to the XTAL’s P end through a DC blocking capacitor (about 20 pF). The XTAL’s N end can be floating. Figure ESP32-S3 Schematic for External Oscillator shows the schematic of the external signal.

image

The cached slow clock calibration register sometimes gets me the expected 32767 Hz, sometimes 32778 Hz and sometimes 141495 Hz (I guess it's because the XTAL32K Watchdog Timer kicks in?).

Did anyone manage to make any progress? @LonerDan Did you figure out how configure RTC_CNTL_RTC_EXT_XTL_CONF_REG?

2opremio avatar Aug 25 '24 01:08 2opremio

I have tried setting RTC_CNTL_RTC_XTAL32K_GPIO_SEL to 1 but it doesn't help (I get the infamous 32 kHz XTAL not found message at boot)

2opremio avatar Aug 25 '24 01:08 2opremio

BTW, this is the oscillator signal I am using as input. (Using a 10x probe)

IMG_5107

Uhm, I am thinking that it may not be following 0.6 < Vpp < VDD amplitude guidelines if we understand amplitude as the positive side of the signal. Then it would be only about 0.5V.

Also, do I need to positive-bias the signal?

2opremio avatar Aug 25 '24 01:08 2opremio

sometimes 141495 Hz (I guess it's because the XTAL32K Watchdog Timer kicks in?).

That can't be, since CONFIG_ESP_XT_WDT isn't enabled in my project

2opremio avatar Aug 25 '24 02:08 2opremio

@igrr could you please shed some light here? (or point us to someone who may know?)

Maybe @esp-momo ? Or @krzychb ?

2opremio avatar Aug 25 '24 02:08 2opremio

It seems to work after doing REG_SET_FIELD(RTC_CNTL_EXT_XTL_CONF_REG, RTC_CNTL_DBUF_XTAL_32K, 0)

I still haven't tested it long-term but so far I always get calibrations of 32767 Hz!

PR coming up.

2opremio avatar Aug 26 '24 00:08 2opremio

I agree that the explanation of the External 32kHz clock signal requirements is unclear, and that the ESP-IDF currently doesn't handle that case correctly.

I have an ESP32-S3 board with a 640mV pk-pk, 0mV - 640mV, 26% duty cycle square wave input working when I apply the PR from @2opremio

dvosully avatar Mar 24 '25 02:03 dvosully

Is there a known working schematic for the external signal option? (I only want to sacrifice one pin, not two) Particularly using the 32.768kHz clkout from a PCF8563 chip.

I'm trying to get 1000s of development boards shipped with this built in and it's the main blocker.

Dominaezzz avatar Apr 23 '25 16:04 Dominaezzz

@Dominaezzz I have it working with a DS3231 (the clock output is the same, open drain). See schematic below.

The resistor selection gives a thevenin equivalent of 640mV pullup with approx 4k15 impedance, this is a pretty strong pullup. I am sure higher impedances would work fine provided you comply with the <1.0V pk-pk guideline (https://docs.espressif.com/projects/esp-idf/en/stable/esp32s3/api-reference/system/system_time.html#rtc-timer-clock-sources). I just used parts I had on hand. I doubt the part selection is critical but I haven't done extensive testing.

It is also working for me with the capacitor shorted out (ie. 0V-0.64V square wave).

Note that this requires the above PR from @2opremio https://github.com/espressif/esp-idf/pull/14442 and the CONFIG_RTC_CLK_SRC_EXT_OSC=y menuconfig option selected

Image

dvosully avatar Apr 24 '25 00:04 dvosully

Thank you so much for sharing the schematic and also explaining how it works! I really appreciate it! I've currently got something similar to what you've got, except the resistors. I'll give it a go with the PCF8563.

Just to confirm something about the voltage (I'm a software engineer and not an EEE). The 32KHZ output of the DS3231 comes out as 3.3V right? If so, the goal of the 22k1 and 5k11 resistor is to get the 3.3V down to ~0.6V right? I'm 80% sure the answer is yes but I just want to make sure.

Dominaezzz avatar Apr 24 '25 00:04 Dominaezzz

The 32KHZ output of the DS3231 is open drain - it doesn't output a voltage, it just closes a switch to ground. See https://en.wikipedia.org/wiki/Open_collector

The PCF8563 has the same kind of output - it has an open drain clock output.

The goal of the two resistors is to create a pullup to a voltage rail that doesn't exist on the board. The goal of the 22k1 and 5k11 is to get 3.3V down to ~0.6V, but it's not 'from' the DS3231 output, its from the 3.3V power on the board. The voltage comes from the resistor divider - it is a virtual pullup. By having one pulling up and one pulling down, you can adjust the voltage between 0V and 3.3V (assuming the resistors are connected to 3.3V and 0V respectively). What the voltage it pulls up to is determined by the ratio of the two resistors. And the strength of the equivalent pullup is the equivalent resistance of the two resistors in paralle. See https://en.wikipedia.org/wiki/Voltage_divider

3.3V * 5.11 / (22.1 + 5.11) = 0.64V

1 / (1 / 5.11 + 1 / 22.1) = 4.15k

So the equivalent circuit is a single 4.15k pullup to 0.64V

dvosully avatar Apr 24 '25 00:04 dvosully

Thanks again for the fantastic explanation! I really appreciate it!

Dominaezzz avatar Apr 24 '25 01:04 Dominaezzz

I've given this a try with two different set of resistors, with and without https://github.com/espressif/esp-idf/pull/14442, with and without the capacitor, but I still get the same 32 kHz XTAL not found, switching to internal 150 kHz oscillator log line.

Unless for some reason, one cannot simply set this up on a breadboard I'm out of ideas and at my wit's end. Only thing left to try is to get an actual DS3231 instead of a PCF8563 and hope that'll make a difference somehow.

Dominaezzz avatar May 20 '25 23:05 Dominaezzz