Skip to content

cpu/samd5: improve clock initialisation XOSC32 and FDPLL #21631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2025

Conversation

kfessel
Copy link
Contributor

@kfessel kfessel commented Jul 30, 2025

Contribution description

current samd5 initialization is strange and might fail ( hardware dependant) while initializing this fixes that.
The failure is know a POR issue.

  • valid clock XOSC32 startup time
  • avoid gclk roundtrip for FDPLL when XOSC32 is used

Testing procedure

read and compare to documentation

Issues/PRs references

@kfessel kfessel requested review from benpicco and dylad as code owners July 30, 2025 13:50
@github-actions github-actions bot added Platform: ARM Platform: This PR/issue effects ARM-based platforms Area: cpu Area: CPU/MCU ports labels Jul 30, 2025
@crasbe crasbe added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jul 30, 2025
@riot-ci
Copy link

riot-ci commented Jul 30, 2025

Murdock results

✔️ PASSED

991bf4a cpu/samd5: clock initialisation XOSC32 and FDPLL

Success Failures Total Runtime
10559 0 10560 11m:45s

Artifacts

@kfessel kfessel force-pushed the p-fix-samd5-init branch from da4df6a to 3b9ff6f Compare July 30, 2025 15:04
@kfessel kfessel force-pushed the p-fix-samd5-init branch 3 times, most recently from 9b2f3af to d7a8752 Compare July 31, 2025 12:32
@benpicco
Copy link
Contributor

The spell checker has some comments 😉

@kfessel kfessel force-pushed the p-fix-samd5-init branch 2 times, most recently from 1f7f759 to b3ee32a Compare August 1, 2025 07:50
@kfessel
Copy link
Contributor Author

kfessel commented Aug 1, 2025

i ran a test ULP vs XOSC32

boards/same54-xpro/include/periph_conf.h
@@ -39,7 +39,7 @@ extern "C" {
  * @name    external Oscillator (XOSC1) configuration
  * @{
  */
-#define XOSC1_FREQUENCY     MHZ(12)
+//#define XOSC1_FREQUENCY     MHZ(12)
 /** @} */
 
 /**
@@ -59,8 +59,8 @@ extern "C" {
  * @name    32kHz Oscillator configuration
  * @{
  */
-#define EXTERNAL_OSC32_SOURCE                    1
-#define ULTRA_LOW_POWER_INTERNAL_OSC_SOURCE      0
+#define EXTERNAL_OSC32_SOURCE                    0
+#define ULTRA_LOW_POWER_INTERNAL_OSC_SOURCE      1
 /** @} */
 
 /**
#define INTERVAL_SECS (60)
int main(void)
{
    uint32_t ole = (uint32_t)0 - ((uint32_t)INTERVAL_SECS * (uint32_t)2 * (uint32_t)US_PER_SEC);
    for (;;) {
        uint32_t now =  ztimer_now(ZTIMER_USEC);
        if ( now - ole >= INTERVAL_SECS * US_PER_SEC){
            printf("ZTIMER_USEC: %12"PRIu32"\n", now);
            ole = now;
        }
    }
    return 0;
}

results

2025-08-01 13:09:52,851 # main(): This is RIOT! (Version: 2025.10-devel-65-gb3ee32-p-fix-samd5-init)
2025-08-01 13:09:52,854 # ZTIMER_USEC:         6495
* 2025-08-01 13:10:53,000 # ZTIMER_USEC:     60006496
2025-08-01 13:11:53,137 # ZTIMER_USEC:    120006496
2025-08-01 13:12:53,278 # ZTIMER_USEC:    180006496
2025-08-01 13:13:53,426 # ZTIMER_USEC:    240006496
2025-08-01 13:14:53,564 # ZTIMER_USEC:    300006497
2025-08-01 13:15:53,698 # ZTIMER_USEC:    360006498
2025-08-01 13:16:53,833 # ZTIMER_USEC:    420006498
2025-08-01 13:17:53,979 # ZTIMER_USEC:    480006498
2025-08-01 13:18:54,134 # ZTIMER_USEC:    540006498
2025-08-01 13:19:54,293 # ZTIMER_USEC:    600006498
2025-08-01 13:20:54,441 # ZTIMER_USEC:    660006498
* 2025-08-01 13:21:54,582 # ZTIMER_USEC:    720006498

(13:21:54,582h-13:10:53,000h)/(720006498µs-60006496µs) = 1,002396966659403
1-(13:21:54,582h-13:10:53,000h)/(720006498µs-60006496µs) = −0,002396966659403131
(0.24% slow)

external XOSC32

2025-08-01 13:22:21,148 # main(): This is RIOT! (Version: 2025.10-devel-65-gb3ee32-p-fix-samd5-init)
2025-08-01 13:22:21,149 # ZTIMER_USEC:         6521
* 2025-08-01 13:23:20,984 # ZTIMER_USEC:     60006522
2025-08-01 13:24:20,979 # ZTIMER_USEC:    120006523
2025-08-01 13:25:20,974 # ZTIMER_USEC:    180006523
2025-08-01 13:26:20,970 # ZTIMER_USEC:    240006523
2025-08-01 13:27:20,965 # ZTIMER_USEC:    300006524
2025-08-01 13:28:20,960 # ZTIMER_USEC:    360006524
2025-08-01 13:29:20,956 # ZTIMER_USEC:    420006525
2025-08-01 13:30:20,951 # ZTIMER_USEC:    480006526
2025-08-01 13:31:20,947 # ZTIMER_USEC:    540006527
2025-08-01 13:32:20,943 # ZTIMER_USEC:    600006528
2025-08-01 13:33:20,939 # ZTIMER_USEC:    660006529
2025-08-01 13:34:20,935 # ZTIMER_USEC:    720006530
* 2025-08-01 13:35:20,931 # ZTIMER_USEC:    780006531

(13:35:20,931h-13:23:20,984h)/(780006531µs-60006522µs) = 0,9999263763898092
1 - (13:35:20,931h-13:23:20,984h)/(780006531µs-60006522µs) = 0,00007362361019081598
(0.0074% fast)

@cold (cooling icepack in envelop )
ext pre cooling ~1min
2025-08-01 13:39:01,327 # Try to reconnect to /dev/ttyACM3 again...
2025-08-01 13:39:01,328 # Reconnected to serial port /dev/ttyACM3
2025-08-01 13:39:59,982 # ZTIMER_USEC:     60006531
2025-08-01 13:40:59,977 # ZTIMER_USEC:    120006531
2025-08-01 13:41:59,973 # ZTIMER_USEC:    180006532
2025-08-01 13:42:59,969 # ZTIMER_USEC:    240006532
2025-08-01 13:43:59,964 # ZTIMER_USEC:    300006532
2025-08-01 13:44:59,960 # ZTIMER_USEC:    360006532
2025-08-01 13:45:59,956 # ZTIMER_USEC:    420006533
2025-08-01 13:46:59,952 # ZTIMER_USEC:    480006533
2025-08-01 13:47:59,947 # ZTIMER_USEC:    540006533

ext (cool)
2025-08-01 13:49:07,308 # main(): This is RIOT! (Version: 2025.10-devel-65-gb3ee32-p-fix-samd5-init)
2025-08-01 13:49:07,310 # ZTIMER_USEC:         6541
2025-08-01 13:50:07,306 # ZTIMER_USEC:     60006541
2025-08-01 13:51:07,301 # ZTIMER_USEC:    120006541
2025-08-01 13:52:07,297 # ZTIMER_USEC:    180006541
2025-08-01 13:53:07,293 # ZTIMER_USEC:    240006541
2025-08-01 13:54:07,288 # ZTIMER_USEC:    300006541
2025-08-01 13:55:07,284 # ZTIMER_USEC:    360006542
2025-08-01 13:56:07,279 # ZTIMER_USEC:    420006543
2025-08-01 13:57:07,275 # ZTIMER_USEC:    480006543
2025-08-01 13:58:07,271 # ZTIMER_USEC:    540006543
2025-08-01 13:59:07,266 # ZTIMER_USEC:    600006543
2025-08-01 14:00:07,262 # ZTIMER_USEC:    660006543
2025-08-01 14:01:07,257 # ZTIMER_USEC:    720006543
2025-08-01 14:02:07,253 # ZTIMER_USEC:    780006543

int ULP  (cool)
2025-08-01 14:03:05,602 # main(): This is RIOT! (Version: 2025.10-devel-65-gb3ee32-p-fix-samd5-init)
2025-08-01 14:03:05,604 # ZTIMER_USEC:         6368
* 2025-08-01 14:04:07,165 # ZTIMER_USEC:     60006368
2025-08-01 14:05:08,720 # ZTIMER_USEC:    120006369
2025-08-01 14:06:10,255 # ZTIMER_USEC:    180006369
2025-08-01 14:07:11,775 # ZTIMER_USEC:    240006369
2025-08-01 14:08:13,281 # ZTIMER_USEC:    300006369
2025-08-01 14:09:14,777 # ZTIMER_USEC:    360006369
2025-08-01 14:10:16,264 # ZTIMER_USEC:    420006370
2025-08-01 14:11:17,744 # ZTIMER_USEC:    480006370
2025-08-01 14:12:19,221 # ZTIMER_USEC:    540006370
2025-08-01 14:13:20,691 # ZTIMER_USEC:    600006371
2025-08-01 14:14:22,160 # ZTIMER_USEC:    660006372
2025-08-01 14:15:23,627 # ZTIMER_USEC:    720006372
* 2025-08-01 14:16:25,096 # ZTIMER_USEC:    780006373
=(14:16:25,096h-14:04:07,165h)/(780006373µs-60006368µs)= 1,024904159549277
1 - (14:16:25,096h-14:04:07,165h)/(780006373µs-60006368µs)= −0,02490415954927667
2.5% slow

ext (cool)
2025-08-01 14:17:44,723 # main(): This is RIOT! (Version: 2025.10-devel-65-gb3ee32-p-fix-samd5-init)
2025-08-01 14:17:44,725 # ZTIMER_USEC:         6537
* 2025-08-01 14:18:44,720 # ZTIMER_USEC:     60006537
2025-08-01 14:19:44,716 # ZTIMER_USEC:    120006538
2025-08-01 14:20:44,711 # ZTIMER_USEC:    180006538
2025-08-01 14:21:44,706 # ZTIMER_USEC:    240006538
2025-08-01 14:22:44,701 # ZTIMER_USEC:    300006538
2025-08-01 14:23:44,696 # ZTIMER_USEC:    360006539
2025-08-01 14:24:44,692 # ZTIMER_USEC:    420006539
2025-08-01 14:25:44,687 # ZTIMER_USEC:    480006540
2025-08-01 14:26:44,682 # ZTIMER_USEC:    540006540
2025-08-01 14:27:44,677 # ZTIMER_USEC:    600006541
2025-08-01 14:28:44,673 # ZTIMER_USEC:    660006541
2025-08-01 14:29:44,668 # ZTIMER_USEC:    720006541
* 2025-08-01 14:30:44,663 # ZTIMER_USEC:    780006542

=(14:30:44,663h-14:18:44,720h)/(780006542µs-60006537µs) = 0,9999208263894387
=1-(14:30:44,663h-14:18:44,720h)/(780006542µs-60006537µs) =0,00007917361056129437
0,0079% fast

@HendrikVE
Copy link
Contributor

I tested this PR with hardware that reliably fails with the old initialization code. With this patch the MCU reliably boots and the RTC is still correct to the second.

- valid clock XOSC32 startup time
- avoid gclk roundtrip for FDPLL when XOSC32 is used
@kfessel kfessel changed the title cpu/samd5: valid XOSC32 statup time avoid gclk roundtrip for CPU FDPLL cpu/samd5: improve clock initialisation XOSC32 and FDPLL Aug 4, 2025
@kfessel kfessel added this pull request to the merge queue Aug 4, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 4, 2025
@crasbe crasbe added this pull request to the merge queue Aug 4, 2025
Merged via the queue into RIOT-OS:master with commit 5e2665d Aug 4, 2025
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: cpu Area: CPU/MCU ports CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Platform: ARM Platform: This PR/issue effects ARM-based platforms Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants