Nuclei-Software / nuclei-sdk

Nuclei RISC-V Software Development Kit
https://doc.nucleisys.com/nuclei_sdk
Apache License 2.0
117 stars 50 forks source link

Read RTC counter robustly #40

Closed gsauthof closed 2 years ago

gsauthof commented 2 years ago

I noticed that when calling rtc_counter_get() several times in a row the returned values sometimes aren't monotonically increasing, even if the counter didn't overflow.

The reason for it is:

Since the 32 bit RTC counter is split into two 16 bit halves RTC_CNTH and RTC_CNTL, reading it is racy:

This change detects when the combined value would be bogus and retries to load both halves.


Btw, I'm curious why most these peripheral registers have the upper 16 bits reserved, such that, as a consequence, fields larger than 16 bits must be split. Do you know the reasoning behind this design?