Seek assist-Arduino GIGA R1 (STM32H7) RTC usage

Post here first, or if you can't find a relevant section!
Post Reply
bambuino
Posts: 4
Joined: Thu Jan 11, 2024 8:55 pm

Seek assist-Arduino GIGA R1 (STM32H7) RTC usage

Post by bambuino »

Hello -

I have an Arduino GIGA R1 that is chipped with an STM32H747 (RM0399 linked here), and use the Arduino IDE to work with the board. There is an in-chip RTC, complemented by a 32.768kHz crystal across PC14 and PC15, for the LSE clock source. However, I believe the RTC is defaulting to the LSI source, and hence RTC is wildly inaccurate. My sole previous experience has been with AVRs, so when I see "RTC" and "crystal" I say "Let's make a clock!" With the GIGA R1 I am admittedly out of my depth in STM32 waters.

I would like to utilize the external crystal mounted to this board and run the LSE, hopefully to tighten up the RTC error, and I seek others assistance in doing so.

It took me a few hours to figure out how to do just this...

Code: Select all

void setup() {
Serial.begin(9600);
delay(4000);
Serial.println("test");
delay(1000);
Serial.println(RCC->BDCR, BIN);
}
void loop() {}
It was the -> that I didn't get, new syntax learning curve suppose.

Anyway, Serial gave this...

Code: Select all

12:05:19.830 -> test
12:05:20.874 -> 1000000101100001
Deciphering this I find RTC is enabled, ok. LSE is chosen as clock source and enabled, however clock security set a flag. Ugh. Well, this little demonstration prolly doesn't mean much as it is just a snippet of code.

However, when I wrap that bit into the RTC example sketch...

Code: Select all

/*
  From: https://docs.arduino.cc/tutorials/giga-r1-wifi/cheat-sheet#rtc
  Accessed: 12 Jan 2024
*/

#include "mbed.h"
#include <mbed_mktime.h>

constexpr unsigned long printInterval{ 1000 };
unsigned long printNow{};

uint64_t last_millis = 0;

void setup() {
  Serial.begin(9600);
  //RTCset();
}
void loop() {
  if (millis() > printNow) {
    Serial.print("System Clock:          ");
    Serial.println(getLocaltime());
    printNow = millis() + printInterval;
  }
  if (millis() - last_millis > 5000) {
    last_millis = millis();
    Serial.println(RCC->BDCR, BIN);
  }
}
void RTCset()  // Set cpu RTC
{
  tm t;
  t.tm_sec = (0);            // 0-59
  t.tm_min = (16);           // 0-59
  t.tm_hour = (10);          // 0-23
  t.tm_mday = (12);          // 1-31
  t.tm_mon = (0);            // 0-11  "0" = Jan, -1
  t.tm_year = ((24) + 100);  // year since 1900,  current year + 100 + 1900 = correct year
  set_time(mktime(&t));      // set RTC clock
}
String getLocaltime() {
  char buffer[32];
  tm t;
  _rtc_localtime(time(NULL), &t, RTC_4_YEAR_LEAP_YEAR_SUPPORT);
  strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
  return String(buffer);
}
...I receive this in Serial.

Code: Select all

12:02:20.436 -> System Clock:          2023-01-11 19:52:21
12:02:21.419 -> System Clock:          2023-01-11 19:52:22
12:02:22.418 -> System Clock:          2023-01-11 19:52:23
12:02:23.016 -> 1000001001100001
12:02:23.433 -> System Clock:          2023-01-11 19:52:24
12:02:24.449 -> System Clock:          2023-01-11 19:52:25
12:02:25.433 -> System Clock:          2023-01-11 19:52:26
Polling that register shows me that now the LSI is selected, LSE is still not ready (bit:1 = 0), and the persistent bit:6 flag indicating failure detected on external crystal. Even more vexing is that

Code: Select all

RTCSet();
crashes my board, so the time indicated in Serial is actually including the accrued error of the LSI over the past several weeks.

So, I seek your folks' assistance in helping me to understand what it is going on here.

As an aside, I am a new member, and this is my first post. I run with the Arduino IDE on a Win11 machine and feel woefully inadequate at addressing this issue. If there is anything I can do to fill in gaps here in my post that may help you understand what I am experiencing please let me know.
Thank you for taking an interest in this topic.
Mark
dannyf
Posts: 446
Joined: Sat Jul 04, 2020 7:46 pm

Re: Seek assist-Arduino GIGA R1 (STM32H7) RTC usage

Post by dannyf »

maybe you can go through the reference manual to see how to enable the rtc. generally BDCR is write-protected and you have to do something special to change its default values.
bambuino
Posts: 4
Joined: Thu Jan 11, 2024 8:55 pm

Re: Seek assist-Arduino GIGA R1 (STM32H7) RTC usage

Post by bambuino »

Thank you for your kind reply.

Yes, the BDCR is write-protected and that is disabled with setting the DBP in PWR reg, IIRC. I kinda get most of what I have been reading. Have not quite gotten to making register changes in my code, not something the casual Arduino user is familiar with. Hence my enrolling here, to seek out users of the GIGA or other H7 series boards that may have had to address a similar such issue.

Below are a couple fora topics I have started or participated in, as a background to what led me here.

https://forum.arduino.cc/t/can-i-access ... de/1200481

https://forum.arduino.cc/t/portentah7-r ... rd/1209630

Thank you again folks for taking an interest.
Mark
dannyf
Posts: 446
Joined: Sat Jul 04, 2020 7:46 pm

Re: Seek assist-Arduino GIGA R1 (STM32H7) RTC usage

Post by dannyf »

did a quick scan of the reference manual. Some unique aspects of this chip:
1. rtc clocks can only be selected once. Need to reset BDCR for each clock change.
2. rtc clock cannot be changed if LSECSSON bit is on.
3. didn't find PWR block clock enable -> didn't look too hard either.

the basic flow is to:
1. enable clock to RTC;
2. enable clock to pwr, set dbp bit
3. turn on the desired clock, and wait for it to be ready
4. switch to that clock
5. enable rtc
6. restore bdcr write protection (clear dbp).

I would be surprised if the chip is that much different from that.
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Seek assist-Arduino GIGA R1 (STM32H7) RTC usage

Post by ag123 »

for this board/chip, note that you would need the 'official' STM core
https://github.com/stm32duino/Arduino_Core_STM32
https://github.com/stm32duino/Arduino_Core_STM32/wiki
in part has it has the full STM32 HAL stack in tow, a fat footprint, but that is the reason the 'support' is bundled.
and you can try the RTC library that goes with this core
https://github.com/stm32duino/STM32RTC
I've not tried it myself but hope it helps

a thing about H7 devices is that I find the clock setups etc 'significantly' more complex than the likes of F1, F4, G4 etc.
e.g. that the H7 clocks seemed to be possible to be configured such that the peripheral clocks run independently of sysclock.
I'd guess this is so that sysclock can vary dynamically 'independent' of the pheriperials. I had quite a few stumbles with this e.g. spi and I've not managed to make that work due to a particular setup.

I'm not sure if RTC is affected in any way, but as after all RTC runs off either LSI or LSE (32k), I'd guess those would work different from the above description.
bambuino
Posts: 4
Joined: Thu Jan 11, 2024 8:55 pm

Re: Seek assist-Arduino GIGA R1 (STM32H7) RTC usage

Post by bambuino »

Thank you so much for the kind responses once more, this seems the place to be then.

I think need just break the ice with this GIGA board and flash a new core and try out the STM32RTC library as suggested. Further information shared above meshes real well with the background readings I've been conducting.

Will circle back on this in a few days with an update on my progress..
Mark
bambuino
Posts: 4
Joined: Thu Jan 11, 2024 8:55 pm

Re: Seek assist-Arduino GIGA R1 (STM32H7) RTC usage

Post by bambuino »

Well, the Arduino GIGA R1 Wifi board has a hardware bug. The load capacitors were not mounted on two of the boards oscillators, 32.768kHz and the 16MHz. Why they did this I have no idea. So, I am left with the contemplation of trying to hand solder in the 1005-size caps.

https://forum.arduino.cc/t/can-i-access ... 1200481/10
dannyf
Posts: 446
Joined: Sat Jul 04, 2020 7:46 pm

Re: Seek assist-Arduino GIGA R1 (STM32H7) RTC usage

Post by dannyf »

the 16Mhz should oscillator just fine, with or without caps.

the 32K will likely need something to get it started. you can put your finger on the pins (especially the input pin) to see if it starts.
Post Reply

Return to “General discussion”