STM32 gotchas
300. Experimenting with LSE and PC13 for fun and profit

To investigate the effect of PC13 at LSE - and properties of the backup domain pins, using the and properties of LSE as such - and inspired by this thread, I decided to conduct a couple of experiments on a Nucleo-F411RE board.

The firmware used is here. It's a rough sketch rather than a refined gem, take it as such. It contains a couple of code sub-pieces commented out usually through #if-s, as the individual experiments progressed. I did not bother with any output e.g. via UART or SWO; I simply stored any measured values in RAM and read them out using the debugger. Also, several of the experiments have no trace in the code, as they were performed by direct writes to peripheral registers in the debugger.

As witnessed at the back side of the Nucleo board, what I have is revision C-02. Now in context of this experiment this is important, as during the initial phases ST did some modifications to the Nucleo-64 board. Unfortunately, the currently published design files don't capture that early phase, as only the C-03 and C-04 variants are present there. From digging through the internet and its archives, I was able to reconstruct some of the changes:

In other words, the LSE crystal in my C-02 revision is a 12.5pF-load-capacitance one, used with 10pF burden capacitors; and the findings for LSE in this article may not be entirely identical as for Nucleo-F411RE of newer (C-03 and beyond) revision. The same applies for any other custom-made board using the same STM32F411; and as the LSE oscillator design in various STM32 (sub)families varies (refer to AN2867 for details), for those families the particular behaviour of LSE even with the same crystal/burden capacitors would be somewhat different.

Also, I put forth, a lot of the issues related to LSE crystal oscillator can be quite simply circumvented by using an external LSE oscillator, used with the RCC_BDCR.LSEBYP bit set, bypassing the analog oscillator circuitry within STM32. In that way, all that sensitive analog circuitry and its shortcomings are encapsulated within that external oscillator, with its functionality guaranteed by its manufacturer. Some of these oscillators are also not based on quartz crystals anymore, rather, they are manufactured from silicon using modern micro-electromechanical methods (MEMS); these may have different (and hopefully better) fundamental properties from the quartz oscillators in e.g. shorter startup time, temperature and long-term stability, resistance to mechanical shocks, etc. The somewhat higher price of these oscillators may be balanced by these improvements, decreased sensitivity for placement at PCB i.e. higher flexibility in PCB design, possibly decreased footprint (e.g. no burden capacitors needed). Justification for such choice is then to be judged individually based on thorough inspection of selected oscillators' documentation.

Generally, LSE is an ultra-low power, basically analog, circuitry. As such, it is quite heavily influenced by surrounding circuits and signals, and its functionality also quite heavily depends on the particular parts used and the PCB layout. A great source of information is AN2867 and every designer ought to read that thoroughly before attempting to design in a LSE crystal oscillator. A good idea is to follow the design hints from AN2867 and/or copy the example layout for LSE oscillator given therein.

This said, the layout of LSE oscillator in the Nucleo-F411RE does not follow those rules and is IMO less than optimal. There is no ground guard around the LSE signals, and the return from burden capacitors does not have a dedicated short ground conductor but goes through a significant portion of common ground and several vias:

ground path from the LSE crystal's burden capacitors to mcu's VDD pin

Currents flowing through the same ground path influence the LSE oscillator, especially if they are of high-frequency (including high-slew-rate pulse edges), where the vias represent higher impedance resulting in higher voltge drops at them. In our simple experiments, none of the numerous on-chip peripherals were used, and only a few pins were set as GPIO outputs, so real-world applications with such layout may fare worse.

The first thing to observe was the LSE startup time. As I don't have a sufficiently low-capacitance (active) probe at hand, the trick employed was to output LSE to MCO pin and observe that. Yellow track is LSE output to MCO; pink track is a GPIO pin set just before RCC_BDCR.LSEON was set, and cleared just after RCC_BDCR.LSERDY went high:

LSE startup time after power-on

This waveform was taken at powerup, after the whole Nucleo was powered down for some time and VDD capacitors were discharged via shorting by tweezers. The LSE startup took around 800ms, and this time +-100ms has proven to be relatively consistent across several power down/up cycles. LSE startup times of several seconds, up to perhaps 10 seconds, are perfectly normal.

The following waveform was taken after a "hot" reset using the button at NRST; the backup-domain was explicitly reset in code using the RCC_BDCR.RTCEN bit, before LSE was started:

LSE startup time after 'hot' reset

In this case, the startup lasted around 120ms. As duration of the backup-domain reset is only a few system clocks, probably the LSE oscillator never ceased oscillating, and what we observe here is in fact the LSERDY mechanism - after LSEON is set, a counter counts the pulses at the input, and when a certain number of pulses is reached, LSERDY is set. This simple mechanism is supposed to ensure, that the oscillator is running stably at the moment when LSERDY is set, and the LSE signal is un-gated towards the rest of mcu. This counter probably counts up to 4096 (compare this with the similar mechanism for HSE/HSERDY, according to remark in RM0091 the HSERDY counter in 'F0 counts to 512 before it sets HSERDY). This simple mechanism can be "fooled" and there are reports of users observing LSERDY being set even with the LSE pins left floating, due to noise being coupled into the LSE circuitry.

It was possible to observe the LSE startup directly on the crystal's pin - the common oscilloscope probe has usually presents around 15pF of capacitance so it loads the circuit significantly, sometimes up to the point of stopping oscillations. However here, surprisingly, LSE started relatively normally (yellow is still the MCO output; purple is signal at crystal, note its significantly lower amplitude, around 200mV at the point where MCO starts outputting the signal, although it keeps increasing somewhat):

LSE startup time after 'hot' reset

In the following, we have running LSE and measure its frequency using the connection from LSE to TIM5_CH4 (as enabled in TIM5_OR). CH4 is set in TIM5_CCMR2 to capture upon every 8th input edge, and in the interrupt from this capture, a software counter counts up to 4096 (in fact, it counts down from 32768/8-1 to 0, but the gist is the same). Difference between values captured in TIM5_CCR4 each such 4096x8=32768 LSE edges, stored into cc4_1s variable, represent one second as timed by LSE as a number of TIM5=APB1 cycles. There's no divider set for APB1, and the system clock is set to HSE, which is set to bypass state. On this Nucleo board, the HSE input of target STM32F411 is connected to the MCO output of STLink, which there provides a 8MHz clock sourced primarily from the STLink's crystal. So, this chain results in TIM5 being clocked from a crystal-stable 8MHz clock. And, indeed, in the cc4_1s variable we observe a relatively stable reading of 7'999'414+-1.

As a very simple first experiment, the LSE crystal has been heated up simply by putting a finger at it. The cc4_1s readout first decreased to around 7'999'407 (i.e. the LSE oscillator frequency increased by cca +0.8ppm, as there were less HSE perods during the 32768 LSE periods and HSE remained stable) and after two or three seconds started to rise up to 7'999'452 (i.e. oscillator frequency decreased by cca -4.7ppm). After the finger has been removed, the readout slowly returned to its original value.

The temperature dependence of the 32.768kHz tuning-fork crystal has an "inverted" parabolic shape, with top centered at 25°C (see e.g. here). The observed behaviour can quite well be explained by this dependence: the initial temperature of the crystal was of the room temperature, slightly below 25°C, so by increasing the temperature the oscillator's frequency increased up to the turning point at 25°C, and above that, the oscillator's frequency decreased.

We can even make an estimate of the temperatures: while it's not quite sure what make of crystal is populated at this board, both the Citizen and Abracon crystals quote a temperature coefficient around -0.035ppm/°C2. This means, that the initial increase of 0.8ppm corresponds to sqrt(0.8/0.035)=4.8°C, i.e. the initial temperature of the crystal was around 20.2°C. The increase corresponds to sqrt(4.7/0.035)=11.6°C, so the final temperature was around 36.6°C. Of course, these values have to be taken with a pinch of salt: the turning point is not precise and is usually given as 25°C+-5°C; and the temperature coefficient is not an absolutely precise value either.

The next experiment was to demonstrate coupling between PC13 and LSE. The 512Hz calibration signal from RTC was set to output at PC13. But while at it, let's have a look at that 512Hz calibration signal (purple; yellow is still the LSE/MCO):

512Hz calibration output appears to be jittery

Oh, but how comes this signal is so jittery, once it originates at a fairly stable oscillator? The answer is, maybe surprisingly, hidden in plain sight in the Calibration clock output subchapter of RTC chapter in RM:
The RTC_CALIB duty cycle is irregular: there is a light jitter on falling edges. It is therefore recommended to use rising edges.
Indeed, the oscilloscope trigger used to capture the picture above is set to falling edge (see blue arrow at top of the first horizontal div). Changing trigger to rising edge results in all rising edges being rock stable, while the falling edges are still jittering.

Zooming in reveals, that that jitter is exactly half LSE period: sometimes the 512Hz calib signal's falling edge matches the rising edge of the LSE waveform, sometimes the falling one. Counting the LSE periods between rising and falling edges of the 512Hz calib signal reveals, that (starting at a rising edge) there is a 31.5/32.5/32/32 - LSE-period pattern within a 2-period 512-Hz signal. That explains the jitter of the falling edges when triggered at rising edge: sometimes the trigger falls at the first rising edge of the above pattern and the next falling edge is the one which is half-cycle-earlier; sometimes it falls at the third edge of the above pattern and then the next edge is at the "correct" position between the two 32-cycle half-periods. (That my oscilloscope alternates between these two occurences so regularly is a lucky coincidence. It is possible to achieve a stable non-jittery patter by using the trigger-holdoff facility of oscilloscope and setting it to cca 2.5ms.)

Similar "trigger alternating between the two possible falling edges within the pattern" explains the above picture for the falling-edge trigger; note, that the LSE waveform jitters there, too, as the trigger alternates between the 512Hz falling edge conciding with LSE rising edge, and the one coinciding with LSE falling edge. Note also, that the 2nd falling edge from the triggering one - representing the whole 2-period pattern - is rock stable.

As documented in the RTC chapter of RM, this 512Hz calibration signal works properly only if the asynchronous prescaler divides the LSE clock by 128 (i.e. the PREDIV_A field in RTC_PRER register is 0x7F - that's its default value, anyway, and the whole RTC_PRER is by default 0x7F'00FF). Note, that the 31.5/32.5/32/32 pattern adds up to 128 LSE cycles, and that's no coincidence. When decreasing RTC_PRER.PREDIV_A to 0x7E, the pattern changes to 30.5/32.5/32/32. Decreasing PREDIV_A further shortens the first (positive) pulse down to the point, where at RTC_PRER.PREDIV_A=0x60 it narrows down to a half LSE cycle, i.e. the pattern becomes 0.5/32.5/32/32 (in the waveform below, the trigger happened at the rising edge between 32.5/32):

0.5/32.5/32/32 pattern at the calib output, when PREDIV_A=0x60

Decreasing PREDIV_A to 0x5F results quite predictibly in the short pulse disappearing and the two "low" areas merging, i.e. the resulting pattern is now 32/64, a nice a 32-LSE-cycles high pulse and 96-cycles period which displays rock stable at the oscilloscope. Decreasing PREDIV_A further results in shortening of the low area, down to PREDIV_A=0x40, where the pattern is 32/33. And now comes the surprise: with PREDIV_A=0x3F, the pattern changes to 31.5/32.5. PREDIV_A=0x3E results in 30.5/32.5 pattern and subsequent decreasing of PREDIV_A shortens the high pulse, down to 0x20, where the pattern is 0.5/32.5. At PREDIV=0x1F and below the calibration output remains permanently low.

Now what (asynchronous/ripple) counter/divider design leads to such weird behaviour, and why would such design be employed, is a question only ST could answer.

Let's now return to investigation of LSE stability upon pulsing PC13. Following figure displays jitter of MCO/LSE edge one period after triggered one (i.e. trigger is on the falling edge and we observe the next falling edge). Persistent mode is switched on and snapshots were taken after cca 10 seconds of capturing. As one can probably tell, the left waveform is for the case where PC13 is steady, no calibration output; the right waveform is for the case where the 512Hz calibration signal is output onto PC13:

LSE/MCO with and without PC13 toggling with the 512Hz calib output

What's interesting is, that the compare-to-HSE readout at TIM5 (i.e. cc4_1s) value essentially does not change with switching on/off the 512Hz-calib-to-PC13 signal, and remains 7'999'414+-1. In other words, in this particular case, the calibration output does not seem to influence LSE's frequency, and by extension, the RTC's precision. (Btw. note, that assuming LSE is perfect, that would mean that the STLink's 8MHz crystal is 70ppm off. This is not an entirely unseen value. Also note, that the oscilloscope measured the LSE frequency as 32770.4Hz; again assuming the LSE is perfect, this means that my cheap oscilloscope is driven from a cheap crystal which is some 70ppm off, too...)

Following waveform (yellow is LSE/MCO, purple is the 512Hz calib output, and again, persistence mode is on) highlights the unsurprising fact, that the LSE jitter is most pronounced at the edges which coincide with the calib output edges:

LSE/MCO edge jitter is most pronounced where it coincides with the 512Hz calib output edge

Now without changing anything, in the debugger, I set RCC_BDCR.LSEMOD, i.e. changed "LSE drive" from low to high (the 'F411 has only two settings, newer STM32 have four, older none - again, AN2867 discusses these differences). The impact of this setting is obvious:

LSE/MCO edge jitter decreased when LSEMOD was set

Whether the associated potential increase in power consumption (especially in VBAT mode) is worth the increased LSE stability in face of PC13 toggling (or any other external disturbances) will be dependent on details of particular project and is up to the user to judge.

But what if the PC13 toggles asynchronously to LSE? The following experiment does exactly that: after setting up the LSE, PC13 is not set as calibration output, rather, it's set as GPIO output and then toggled in a simple loopdelay-timed loop (purple is PC13, yellow is LSE/MCO, trigger is set to LSE/MCO and the very next edge from the triggered one is zoomed; on the left, RCC_BDCR.LSEMOD=0, on the right, RCC_BDCR.LSEMOD=1):

LSE/MCO edge jitter when PC13 toggles asynchronously to MCO

The jitter is somewhat higher (with RCC_BDCR.LSEMOD=1, cca 1us vs. cca 0.5us for the synchronous case), but much more importantly, the measured LSE frequency is not stable anymore. The cc4_1s readout was changing all the time, cca +-40 from the "normal" value (i.e. cca 5ppm). No attempt was made to check, whether this value changes with the frequency/pattern of PC13 toggling.

In a subsequent experiment, PC13 was changed to input, it was jumpered to the neighbouring pin at the Nucleo's pinheader, which is PB7, and that pin was then toggled in the same way as PC13 previously. There was no change in behaviour observed, the jitter and cc4_1s readouts remained roughly the same. Changing PB7's GPIO_OSPEEDR value did not have observable influence on the outcomes either.

But once we play with the pins and their drive setting, let's have a look at the limited drive capability of PC13 (and PC14, PC15). On the Nucleo board, PC13 is heavily loaded by a 100nF capacitance in series with a 100Ω resistor (and that combination in parallel to the blue "user" pushbutton), plus a 4k7 resistor to VDD:

detail of PC13 circuitry on Nucleo-64

For the following waveform, PC13 was set to output and toggled in software; its GPIO_OSPEEDR setting was the default 0b00 i.e. "low"; trigger was set to alternate between both edges, "eye-diagram"-style:

PC13 set to output, toggling at OSPEEDR set low, while loaded by 100nF

Estimating the time constant (i.e. time until voltage decreases to cca 1/3 of its initial value) for the high-to-low transition to be around 15us results in total series resistance of 150Ω i.e. the output transistor's RDSon to be around 50Ω. This is quite in line with the datasheet value of 1.4V drop at 20mA (i.e. 70Ω, we are estimating things here). The low-to-high transition is somewhat slower; estimating its time constant to be around 20us we arrive at an extra 50Ω of series resistance, which can be attributed to the VBAT-domain switch.

There's also another thing to notice: the initial step (from VDD to 0.7V, and from 0V to 2.3V, for the falling and rising edge, respectively - in previous section we've estimated the time constants to start from those voltage points). That is caused by the current flowing through the capacitor, which e.g. at the moment of switching from 0 to 1 is discharged and momentarily behaves as a short, so RDSon of the lower transistor together with the 100Ω series resistor on the board form a divider. That too indicates, that the "high" transistor has higher impedance due to the backup-domain-switch in series, as the "initial" voltage at it is cca 1.0V whereas at the lower transistor it's 0.7V. The absolute values would indicate somewhat different estimates of the RDSon than what we estimated from the time constant: 27Omega; for the lower transistor, and 27Omega;+17Omega; for the higher one. Our estimates are clearly crude, although there may be other factors in play, too (foreshadowing).

Let's zoom in, and compare PC13's waveform with the one taken for PB7 (with the lowest OSPEEDR setting and jumpered to PC13 set as input, so that it sees the same load of 100nF+100Ω) - waveform for PC13 on the left, PB7 on the right:

Zoom in to the edges of PC13 and PB7 (both loaded by 100nF) toggling

Note, that while in both cases the low step is identical, in the case of PB7 the high step goes the same cca 0.7V below VDD as the low step goes above VSS - exactly as expected, as PB7 does not have the added power switch.

What happens if we set GPIO_OSPEEDR to some higher setting? Surprisingly, doing that with PC13 does nothing. This might indicate, that the circuitry to allow higher slew rate is ineffective due to the series power switch, or maybe that circuitry is not even there! For PB7, the result is quite dramatic, though (left is for GPIO_OSPEEDR=0b00, right for 0b11):

Zoom in to the edge of PB7 (loaded by 100nF) at two different OSPEEDR settings

The slew rate with higher OSPEEDR setting is significantly higher (ringing is probably due to my poor probing/grounding arrangement). The GPIO contains circuitry, which ensures rapid charging/discharging of parasitic capacitances at the moment of switching, to ensure the slew rate set by the selected GPIO_OSPEEDR setting. It supplies a high current pulse, while it does not represent a high current source/sink as a plain large transistor would, as that would imply a risk of pin/silicon damage and/or latchup when the pin is inadvertently shorted. This may be a large transistor in parallel to the "sustained" output transistor, switched on momentarily through some gate-delayed driver, or capacitively coupled circuitry, or any other arrangement. ST does not go into that detail in describing the GPIO, but it is good to be aware of these details.