RV32 SoC DS UG
High-Perf RV32 SoC DS UG
RV64 SoC DS UG API and Examples
Embedded IDE UG
Loading...
Searching...
No Matches
rtc.c
Go to the documentation of this file.
1
2// Copyright (C) 2013-2026 Efinix Inc. All rights reserved.
3// Full license header bsp/efinix/EfxSapphireSocRV64/include/LICENSE.MD
5
15
16#include "rtc/rtc.h"
17#include <stddef.h>
18
19/*----------------------------------------------------------------------------*/
20/* Function implementations */
21/*----------------------------------------------------------------------------*/
22
23
24const char* Day_ordinal [] = {
25 "st",
26 "nd",
27 "rd",
28 "th"
29};
30
31
32const char* const DayStrings[] = {
33 "Unknown",
34 "Sunday",
35 "Monday",
36 "Tuesday",
37 "Wednesday",
38 "Thursday",
39 "Friday",
40 "Saturday"
41};
42
43
44const char* const MonthStrings[] = {
45 "Unknown",
46 "January",
47 "February",
48 "March",
49 "April",
50 "May",
51 "June",
52 "July" ,
53 "August" ,
54 "September" ,
55 "October",
56 "November" ,
57 "December"
58};
59
60
61
62const char* const meridiem[] = {
63 "am",
64 "pm"
65};
66
67
68
69const char* get_ordinal(u8 day) {
70 if (day >= 11 && day <= 13) return "th"; // Special case for 11th, 12th, 13th
71
72 switch (day % 10) {
73 case 1: return "st";
74 case 2: return "nd";
75 case 3: return "rd";
76 default: return "th";
77 }
78}
79
81 // Safety check! Ensure the pointer actually exists before calling it
82 if (rtc != NULL && rtc->drv != NULL && rtc->drv->applyConfig != NULL) {
83
84 return rtc->drv->applyConfig(rtc);
85 }
86
87 return RTC_ERR;
88}
90
91 if (rtc != NULL && rtc->drv != NULL && rtc->drv->enableErrorInterrupt != NULL) {
92 return rtc->drv->enableErrorInterrupt(rtc);
93 }
94 return RTC_ERR;
95}
96u8 convert_hour_register(u8 old_reg_val, u8 to_12h)
97{
98 u8 h, pm_flag;
99 u8 new_reg_val;
100
101 if (to_12h) {
102 // --- Convert 24h to 12h ---
103 // Input is raw BCD (00 to 23)
104 h = bcd2bin(old_reg_val & 0x3F); // Mask 0x3F removes unused bits
105
106 if (h >= 12) {
107 pm_flag = 1;
108 if (h > 12) h -= 12;
109 } else {
110 pm_flag = 0;
111 if (h == 0) h = 12; // 00:00 becomes 12:00 AM
112 }
113
114 // Re-encode: [Bit 5 = PM] [Bits 4-0 = Hour BCD]
115 new_reg_val = bin2bcd(h) | (pm_flag << 5);
116 }
117 else {
118 // --- Convert 12h to 24h ---
119 // Input is [Bit 5 = PM] [Bits 4-0 = Hour BCD]
120 pm_flag = (old_reg_val & 0x20) >> 5;
121 h = bcd2bin(old_reg_val & 0x1F); // Mask 0x1F gets the number
122
123 if (pm_flag) {
124 // PM Case: 12 PM -> 12, 1 PM -> 13
125 if (h < 12) h += 12;
126 } else {
127 // AM Case: 12 AM -> 00, 1 AM -> 1
128 if (h == 12) h = 0;
129 }
130
131 new_reg_val = bin2bcd(h);
132 }
133 return new_reg_val;
134}
135
rtc_status_t
RTC Status List.
Definition rtc.h:94
@ RTC_ERR
Failed to retrieve/write value.
Definition rtc.h:96
const char *const DayStrings[]
Get string representations for Sunday,Monday ,etc..
Definition rtc.c:32
rtc_status_t rtc_enableErrorInterrupt(rtc_instance_t *rtc)
Wrapper: Enable RTC error interrupt.
Definition rtc.c:89
const char *const meridiem[]
Get string representations for meridiem (AM/PM).
Definition rtc.c:62
u8 convert_hour_register(u8 old_reg_val, u8 to_12h)
Convert hour register between 12-hour and 24-hour format.
Definition rtc.c:96
const char * get_ordinal(u8 day)
Get ordinal suffix for a given day.
Definition rtc.c:69
const char *const MonthStrings[]
Get string representations for months.
Definition rtc.c:44
rtc_status_t rtc_applyConfig(rtc_instance_t *rtc)
Wrapper: Apply I2C + RTC configuration.
Definition rtc.c:80
const char * Day_ordinal[]
Get ordinal suffix for a given day.
Definition rtc.c:24
struct rtc_instance rtc_instance_t
Forward declaration of RTC instance.
Definition rtc.h:115
RTC driver API definitions.
rtc_status_t(* applyConfig)(rtc_instance_t *rtc)
Apply i2c config , user can call via rtc_applyConfig as well.
Definition rtc.h:138
rtc_status_t(* enableErrorInterrupt)(rtc_instance_t *rtc)
Enable error interrupt function pointer, user can call via rtc_enableErrorInterrupt as well.
Definition rtc.h:139
const rtc_api_t * drv
Pointer to RTC API structure.
Definition rtc.h:155
uint8_t u8
Definition type.h:30