Calibrate HSI in custom board with stm32g030

If you made your own board, post here, unless you built a Maple or Maple mini clone etc
Post Reply
kid_bengala
Posts: 9
Joined: Sun Jan 15, 2023 11:21 pm

Calibrate HSI in custom board with stm32g030

Post by kid_bengala »

Hello,

I am new to the forum and I have been looking for information about my question, I have not found anything, but maybe I have searched wrongly, I apologize if my question is duplicated.

I have developed some boards with a stm32g030 microprocessor, which I put some 48 Mhz quartz crystals and trying to configure the clock with the STM32Cube function I discovered that this micro (TSSOP 20) cannot have a high speed crystal, i'm newbie :oops: .

Looking for information about this, I came across this thread (https://community.st.com/s/question/0D5 ... ration-tab) on the official ST forum, but I don't know how to make the "Use a 32768 Hz crystal, and calibrate HSI to LSE through TIM16 and HSITRIM" point work.

Can anyone give me some guidance on how to do this?

Thanks!

Regards!
kid_bengala
Posts: 9
Joined: Sun Jan 15, 2023 11:21 pm

Re: Calibrate HSI in custom board with stm32g030

Post by kid_bengala »

Hi

Is it not possible to do this with stm32duino?

Thanks

Regards
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: Calibrate HSI in custom board with stm32g030

Post by dannyf »

sure is possible.

but the implementation will depend on what skills / equipment you have.

The simplest would be to output the clock on a pin and measure it with a frequency meter.

If the chip has a LSE, it is even simpler.

a more complex scheme is to use a known good signal (1pps for example).

Again, what do you have?
kid_bengala
Posts: 9
Joined: Sun Jan 15, 2023 11:21 pm

Re: Calibrate HSI in custom board with stm32g030

Post by kid_bengala »

Hello!

Thanks for the reply.

I have oscilloscope and frequency meter up to 40 Mhz.

I put a 32,768 Khz crystal on the chip, but I removed it because I didn't see any improvement in the code nor could I (or did I) configure it correctly.

ImageImage

Regards
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Calibrate HSI in custom board with stm32g030

Post by ag123 »

stm32g030 is quite a different and new chip
viewtopic.php?t=1848

among the things that is "unusual" (different), PC15 pin is shared between OSC32_EN (default) and OSC_EN
viewtopic.php?t=1848&start=10
so I'd guess either a 32k crystal can be used there and configured as LSI (only/mainly for the real time clock) or a e.g. 4-16 Mhz (commonly 8 Mhz) crystal can be placed there and used as HSE (for system clock).
HSI i'd guess is default 16 Mhz
https://www.st.com/resource/en/datashee ... g030f6.pdf

crystals don't simply 'work', take a look in void SystemClock_Config(void) of some of the variants. e.g.
https://github.com/stm32duino/Arduino_C ... Cx.cpp#L95
those are the codes to 'turn on' HSE, but the "shared" crystal setup at PC15 pin in G0 would likely make things somewhat different.

if you are new to the platform, start with a chip/board that has more resources.
e.g. the stm32f401/stm32f411 series, there are Nucleo boards (e.g. Nucleo F401RE/F411RE)
https://github.com/stm32duino/Arduino_C ... -64-boards
and 'black pill' boards around for them.
https://www.aliexpress.com/af/stm32f401.html
https://www.aliexpress.com/af/stm32f411.html
and there are fairly 'high end' ones that runs stm32f405 - from adafruit
https://www.adafruit.com/product/4382
micropython
https://store.micropython.org/product/PYBLITEv1.0
olimex
https://www.olimex.com/Products/ARM/ST/STM32-H405/
etc

if you are starting with stm32g030f6p6
https://www.st.com/resource/en/datashee ... g030f6.pdf
the trouble with it is the small amount of sram 8 kB and flash 32 kB
this would normally take "bare metal" programming techniques, which means omitting large amount of codes and restricting memory usage to bare minimum, it would be difficult to fit 'stm32duino' in it.
kid_bengala
Posts: 9
Joined: Sun Jan 15, 2023 11:21 pm

Re: Calibrate HSI in custom board with stm32g030

Post by kid_bengala »

Hello,

Just today I was investigating that, having found in ST that model of microcontroller. Thanks for the information!

Regards
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: Calibrate HSI in custom board with stm32g030

Post by dannyf »

it is fairly easy, once you have either HSE or LSE up and running.

1. in general, both HSI and LSI on those chips are fairly accurate. usually within 1% and often around 0.1%. good enough to run uart at reasonably high speed.
2. getting HSE up and running is fairly easy - you usually don't need caps. LSE usually need caps.
3. the basic concept is the same. all you need a mechanism to read off the mcu ticks: DWT or SysTick, or even a timer, 32-bit.

a) set up the RTC to run off LSI.
b) hold the execution off a few seconds run of the RTC.
c) measure the elapsed ticks over b).
d) compare the ticks vs. expectation.

For example, if you run RTC for 2 seconds, on a chip that runs off a 4Mhz ticks. you would expect 8M ticks over a 2 second period. If your reading from above is < 7.9M, your LSI runs slightly faster; or slower if you get more than 8M.

here is what I wrote to implement the process above: I was trying to calibrate the various clocks (using LSE to calibrate HSI, or using HSE to calibrate LSI).

Code: Select all

//rtc
uint32_t rtcTks(uint32_t sec) {
	uint32_t tks=0;
	uint32_t tmp;
	tmp=RTC2time(NULL); while (RTC2time(NULL) == tmp) continue;	//wait for a new second to arrive
	tks=ticks();
	while (sec--) {
		tmp=RTC2time(NULL); while (RTC2time(NULL) == tmp) continue;	//wait for a new second to arrive
	};
	return ticks() - tks;
}
I even wrote a binary search algorithm to automate the trial-and-error: https://dannyelectronics.wordpress.com/ ... stm32f103/

that particular implementation was on a STM32F103 using 4x7seg led display. But it is fairly portable as well: I did the same thing on a G030F, a F030F, and a F100 too.
Post Reply

Return to “Custom design boards”