Skip to content

Commit 1fc26b8

Browse files
bjarki-andreasennashif
authored andcommitted
drivers: rtc: api: Add helper for determining calibration
Add helper function which calculates the required calibration to 1 Hertz from the actual frequency of an RTC. This helper is both functional and adds to the documentation of the RTC calibration API. The rtc.h header now includes zephyr/kernel.h instead of zephyr/types.h as __ASSERT_NO_MSG is now referenced in the header. The commit additionally extends the rtc_api_helpers test suite with a test to validate the conversion function itself. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
1 parent 724762f commit 1fc26b8

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

include/zephyr/drivers/rtc.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @{
2323
*/
2424

25-
#include <zephyr/types.h>
25+
#include <zephyr/kernel.h>
2626
#include <zephyr/device.h>
2727
#include <errno.h>
2828

@@ -459,6 +459,8 @@ static inline int z_impl_rtc_update_set_callback(const struct device *dev,
459459
* the RTC clock, a negative value will decrease the
460460
* frequency of the RTC clock.
461461
*
462+
* @see rtc_calibration_from_frequency()
463+
*
462464
* @param dev Device instance
463465
* @param calibration Calibration to set in parts per billion
464466
*
@@ -527,6 +529,20 @@ static inline struct tm *rtc_time_to_tm(struct rtc_time *timeptr)
527529
return (struct tm *)timeptr;
528530
}
529531

532+
/**
533+
* @brief Determine required calibration to 1 Hertz from frequency.
534+
*
535+
* @param frequency Frequency of the RTC in nano Hertz
536+
*
537+
* @return The required calibration in parts per billion
538+
*/
539+
static inline int32_t rtc_calibration_from_frequency(uint32_t frequency)
540+
{
541+
__ASSERT_NO_MSG(frequency > 0);
542+
543+
return (int32_t)((1000000000000000000LL / frequency) - 1000000000);
544+
}
545+
530546
/**
531547
* @}
532548
*/

tests/drivers/rtc/rtc_api_helpers/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ project(rtc_api)
1010
target_sources(app PRIVATE
1111
src/main.c
1212
src/test_rtc_time_to_tm.c
13+
src/test_rtc_calibration_from_frequency.c
1314
)
14-
15-
target_include_directories(app PRIVATE inc)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2022 Bjarki Arge Andreasen
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/ztest.h>
8+
#include <zephyr/drivers/rtc.h>
9+
10+
struct test_sample {
11+
uint32_t frequency;
12+
int32_t calibration;
13+
};
14+
15+
static const struct test_sample test_samples[] = {
16+
{
17+
.frequency = 1000000000,
18+
.calibration = 0,
19+
},
20+
{
21+
.frequency = 1000000001,
22+
.calibration = -1,
23+
},
24+
{
25+
.frequency = 999999999,
26+
.calibration = 1,
27+
},
28+
{
29+
.frequency = 2000000000,
30+
.calibration = -500000000,
31+
},
32+
{
33+
.frequency = 500000000,
34+
.calibration = 1000000000,
35+
},
36+
};
37+
38+
ZTEST(rtc_api_helpers, test_validate_calibration_from_frequency)
39+
{
40+
uint32_t frequency;
41+
int32_t calibration;
42+
int32_t result;
43+
44+
ARRAY_FOR_EACH(test_samples, i) {
45+
frequency = test_samples[i].frequency;
46+
calibration = test_samples[i].calibration;
47+
result = rtc_calibration_from_frequency(frequency);
48+
zassert_equal(result, calibration);
49+
}
50+
}

0 commit comments

Comments
 (0)