RTC Library using LSE

j-zero
Posts: 3
Joined: Thu Oct 22, 2015 5:14 pm

RTC Library using LSE

Post by j-zero » Thu Oct 22, 2015 5:19 pm

Hello,

I'm trying to use the RTC with a 32kHz Crystal on LSE my Maple Mini Clone.
I can use LSI without problems, but it's not accurate enough (3 seconds to fast in a minute) for my clock project.

So I attached a 32Khz crytsal to D12 and D13 to the Maple mini, and changed the rtc_init to rtc_init(RTCSEL_LSE);
And there it stucked. After some debugging I found out it hangs on:

Code: Select all

static inline void rtc_wait_sync() {
	while (*bb_perip(&(RTC->regs)->CRL, RTC_CRL_RSF_BIT) == 0);
}
in the rtc_util.h

Any ideas?

have a nice day

johannes

User avatar
ahull
Posts: 1634
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: RTC Library using LSE

Post by ahull » Thu Oct 22, 2015 5:56 pm

j-zero wrote:Hello,

I'm trying to use the RTC with a 32kHz Crystal on LSE my Maple Mini Clone.
I can use LSI without problems, but it's not accurate enough (3 seconds to fast in a minute) for my clock project.

So I attached a 32Khz crytsal to D12 and D13 to the Maple mini, and changed the rtc_init to rtc_init(RTCSEL_LSE);
And there it stucked. After some debugging I found out it hangs on:

Code: Select all

static inline void rtc_wait_sync() {
	while (*bb_perip(&(RTC->regs)->CRL, RTC_CRL_RSF_BIT) == 0);
}
in the rtc_util.h

Any ideas?

have a nice day

johannes
The STM documentation lists the requirements for the crystal.

According to the STM documentation, you will need the correct capacitors as well as the crystal, and the crystal needs to be of the correct type.
RTC-Crystal.png
RTC-Crystal.png (76.13 KiB) Viewed 2099 times
Having said that, I completely ignored those requirements and simply fitted one of these cheap watch crystals with no capacitors to the boards I bought that had no crystal and it worked. This is obviously not the most scientific method. ;)

In your case, the crystal is not oscillating, since the sketch hangs, so I suspect you do need the capacitors to ensure it does actually oscillate. Alternatively you might like to try a different crystal. If you do fit the capacitors, bear in mind these are pretty low value capacitors, so keep everything neat, keep all of the leads as short as possible, otherwise your 10pf capacitor may be more like a 40pf capacitor.

You should also ensure that VBAT is connected to 3.3V, either using a small button cell, or by using a low value (say <100R) resistor to the main 3.3V supply. You can connect VBAT directly to the 3.3V line, but if you then make the mistake of fitting a battery to VBAT later and don't remove the short to 3.3Vyour battery will pop.

I believe if VBAT is floating, the RTC may not operate.

Once you get it working, you might like to read this. It details how to use the calibration register and the RTC prescaler to get the most accurate results.

In my case, I haven't actually bothered to calibrate, since the crystals I have fitted seem reasonably accurate.

Which board are you using?
- Andy Hull -

User avatar
RogerClark
Posts: 7184
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: RTC Library using LSE

Post by RogerClark » Thu Oct 22, 2015 7:53 pm

10pf capacitance is such a small value, I'm surprised they bother to require it.

I bet if I put a capacity meter between those pins and GND, the parasitic capacitance of the chip its self, or the PCB its on etc, could be easily as much as that.

But I guess the parasitic capacitance is not directly to ground.

Either way, Im not surprised it works without caps ;-)

User avatar
ahull
Posts: 1634
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: RTC Library using LSE

Post by ahull » Thu Oct 22, 2015 8:54 pm

RogerClark wrote:10pf capacitance is such a small value, I'm surprised they bother to require it.

I bet if I put a capacity meter between those pins and GND, the parasitic capacitance of the chip its self, or the PCB its on etc, could be easily as much as that.

But I guess the parasitic capacitance is not directly to ground.

Either way, Im not surprised it works without caps ;-)
If you are lucky and they have routed the RTC tracks sensibly and kept them short, and you use a suitable crystal, you probably don't need those caps, but there are plenty of pitfalls if those 'ifs' are not the case.

You really *should* put the caps in, and if it were a production board, I would bother.

For anything I am doing for my own amusement, close enough is good enough. I just whacked the crystal on the board and assumed it would work, I didn't even check it on the scope and it worked first time. No doubt if I had been doing this for a customer.... Mr. Murphy would have popped out of the woodwork and the thing would have stubbornly refused to oscillate. :D

I even used a recycled crystal from an old wall clock while I was waiting for the slow boat ones to arrive, and that also worked, so I don't think the STM32F103 is as fussy as the documentation suggests.

If j-zero's board is the same as any of the ones I have, then it will probably work fine with one of those cheap watch crystals. Even the Ugly Board RTC works without any issues, using one of these, and that board is pretty quirky in other respects. The fact that his doesn't work makes me suspicious of the crystal.
- Andy Hull -

User avatar
mrburnette
Posts: 1829
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: RTC Library using LSE

Post by mrburnette » Thu Oct 22, 2015 11:24 pm

ahull wrote:
RogerClark wrote: <...>
You really *should* put the caps in, and if it were a production board, I would bother.
<...>
I even used a recycled crystal from an old wall clock while I was waiting for the slow boat ones to arrive, and that also worked, so I don't think the STM32F103 is as fussy as the documentation suggests.
Yes!
Every crystal has both serial- and parallel-resonant oscillation modes. Which mode is invoked depends upon the the oscillator circuit driving the crystal. The series-resonant and parallel-resonant frequencies for any crystal will be slightly different. Moreover the resonant frequency of a crystal oscillating in parallel-resonant mode will depend upon the load capacitance of the circuit. That's why the required load capacitance is included in the specs for crystals characterized for parallel resonance. IOW, the crystal will work with a different load capacitance, but it won't oscillate at exactly the frequency stamped on its case.

-Phil Pilgrim (PhiPi)
More? Here: https://www.maximintegrated.com/en/app- ... mvp/id/726

Ray

User avatar
ahull
Posts: 1634
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: RTC Library using LSE

Post by ahull » Fri Oct 23, 2015 1:00 am

Ray, I agree. Watch crystals are however slightly less of an art than high frequency crystals. For example..

"An exception to the above guideline is 32.768kHz wristwatch crystals, where series resistance can be in the tens of kilohms. Consequently, for this application the oscillator circuit must accommodate this high series resistance. Failure to address this will result in a 32.768kHz oscillator that does not oscillate. You should not expect to use an oscillator designed for a 10MHz crystal with a 32.768kHz crystal. It will not work."

The ST guidelines are pretty specific about the type of crystal, and the load caps, but in reality cheap watch crystals are not the most accurate things on the planet anyway. If you really need ppm accuracy, then follow the spec. use a quality crystal, and keep exactly to their guidelines.

If however you just need the thing to oscillate at close to 32.768 kHz then don't loose too much sleep over it. In my case I just need to hit "start of sunset +/- 10 minutes", so if the clock drifts by a minute or so per week, I'm not going to cry too much. Besides with a little messing about I can probably compensate for that, If you use a cheap watch crystal, the best you can expect is that the time is as accurate as a cheap quartz watch. If you need to spot the International Space station flying overhead, use GPS time.

One final point, most of the STM32F103 boards shipping from China have pretty much the same crystal on them that I have used, so expect them to be as accurate (or inaccurate) as mine, load capacitors (if fitted) not withstanding of course. :D
- Andy Hull -

j-zero
Posts: 3
Joined: Thu Oct 22, 2015 5:14 pm

Re: RTC Library using LSE

Post by j-zero » Fri Oct 23, 2015 6:17 am

Hello,

thanks for the answers. I have connected two caps (18pF) to the crystal (and ground). I just desoldered the crystals from old computer mainboards, maybe they have the wrong capacity.

VBAT is connected to a CR2032 Battery which works if I use LSI instead of LSE.
The crystal seams to oscillate, what i only can mesaure with my old analog oscilloscope.

The LSI drifts about 3 seconds per minute, what is not really quite accurate.

Thanks

Johannes

User avatar
mrburnette
Posts: 1829
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: RTC Library using LSE

Post by mrburnette » Fri Oct 23, 2015 12:26 pm

If however you just need the thing to oscillate at close to 32.768 kHz then don't loose too much sleep over it.
Agreed...
For one of my projects, I moved the 32K crystal to a t84 to create a 1-second time base. I grossly correct the seconds count in the main routine by adding/subtracting whole seconds at some long integer count.

http://www.insidegadgets.com/2011/10/08 ... -attiny85/

Ray

User avatar
ahull
Posts: 1634
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: RTC Library using LSE

Post by ahull » Fri Oct 23, 2015 8:43 pm

j-zero wrote:Hello,

thanks for the answers. I have connected two caps (18pF) to the crystal (and ground). I just desoldered the crystals from old computer mainboards, maybe they have the wrong capacity.

VBAT is connected to a CR2032 Battery which works if I use LSI instead of LSE.
The crystal seams to oscillate, what i only can mesaure with my old analog oscilloscope.

The LSI drifts about 3 seconds per minute, what is not really quite accurate.

Thanks

Johannes
18pf seems a little high to me, I would use 10pf
Attachments
STM32F103RTC.png
STM32F103RTC.png (6.65 KiB) Viewed 2044 times
- Andy Hull -

j-zero
Posts: 3
Joined: Thu Oct 22, 2015 5:14 pm

Re: RTC Library using LSE

Post by j-zero » Mon Oct 26, 2015 6:59 am

Hello,

thanks for all the answers. The crystals I tried to use didn't work. I just desolders one from an DS1307 RTC module which I used with an Arduino and it worked immediately. Those are 12.5pF crytals and I attached them with 18pF caps to ground. So it seems not that important.
Interestingly the crystals that didn't work on the Maple Mini clone are working on the DS1307 modules.

Thanks for help

j-zero

Post Reply