RV32 SoC DS UG
High-Perf RV32 SoC DS UG
RV64 SoC DS UG API and Examples
Embedded IDE UG
Loading...
Searching...
No Matches
PCF8523.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#include <stdio.h>
16#include "bsp.h"
17#include "rtc/device/PCF8523.h"
18
19/* -----------------------------------------------------------------------------*/
20/* RTC - Hardware Register Map for PCF8523
21/* -----------------------------------------------------------------------------*/
22
23/* --- Address: Control Status Register --- */
24#define RTC_CONTROL_1 0X00
25#define RTC_CONTROL_2 0X01
26#define RTC_CONTROL_3 0X02
27
28/* --- Address: Timekeeping Register --- */
29#define RTC_SECONDS 0X03
30#define RTC_MINUTES 0X04
31#define RTC_HOURS 0X05
32#define RTC_DAYS 0X06
33#define RTC_WEEKDAYS 0X07
34#define RTC_MONTH 0X08
35#define RTC_YEAR 0X09
36
37/* --- Address: Timer Register --- */
38#define TIMER_A_FREQ_CTRL 0x10
39#define TIMER_A_REG 0x11
40#define TIMER_B_FREQ_CTRL 0x12
41#define TIMER_B_REG 0x13
42
43/* --- Address: Timekeeping/ALARM in Each Register --- */
44#define SECONDS_DATA 0X7F
45#define MINUTES_DATA 0X7F
46#define HOURS_DATA 0X3F
47#define DAYS_DATA 0X3F
48#define WEEKDAYS_DATA 0X07
49#define MONTHS_DATA 0X1F
50#define YEARS_DATA 0XFF
51
52/* -----------------------------------------------------------------------------*/
53/* RTC: Plug & Play Driver
54/* -----------------------------------------------------------------------------*/
56{
57 .applyConfig = PCF8523_applyConfig,
58 .enableErrorInterrupt = PCF8523_enableErrorInterrupt,
59 .getTime = PCF8523_getTime,
60 .setTime = PCF8523_setTime,
61 .setTimeSystem = PCF8523_setTimeSystem,
62
63};
64
70
72
73 // Will drop I2C Transcation if Timeout.
75 return RTC_OK;
76}
77
79
80 LOG_INFO(DBG_MOD_RTC,"This is PCF8523_getTime func!\n");
81 rtc_data_t *time = &rtc->current_time;
82 u8 get_data[9] = {0};
83
84 // Hardware Read
85 i2c_setMux(rtc->inst, 0x1);
86 i2c_readData_b(rtc->inst, RTC_CONTROL_1, get_data, 14);
87
88 // Parsing
89 time->timesystem = (get_data[RTC_CONTROL_1] & 0x08) >> 3;
90 time->seconds = bcd2bin(get_data[RTC_SECONDS] & SECONDS_DATA);
91 time->minutes = bcd2bin(get_data[RTC_MINUTES] & MINUTES_DATA);
92 time->years = bcd2bin(get_data[RTC_YEAR]);
93 time->months = bcd2bin(get_data[RTC_MONTH] & MONTHS_DATA);
94 time->days = bcd2bin(get_data[RTC_DAYS] & DAYS_DATA);
95 time->weekdays = get_data[RTC_WEEKDAYS] & WEEKDAYS_DATA;
96 u8 raw_hours = get_data[RTC_HOURS];
97
98 if (time->timesystem == 1) {
99 // --- 12 Hour Mode ---
100 time->PM = (raw_hours & 0x20) >> 5;
101 time->hours = bcd2bin(raw_hours & 0x1F);
102 }
103 else {
104 // --- 24 Hour Mode ---
105 time->PM = 0; // Not applicable
106 time->hours = bcd2bin(raw_hours & 0x3F);
107 }
108
109 return RTC_OK;
110}
111
113 rtc_data_t *t = &rtc->current_time;
114 u8 buffer[1];
115
116 i2c_setMux(rtc->inst, 0x1);
117 i2c_readData_b(rtc->inst, RTC_CONTROL_1, buffer, 1);
118
119 // Make sure it is in 24hr timesystem!
120 // Check Bit 3 (0 = 24h, 1 = 12h). If it is 1, we must clear it.
121 if (buffer[0] & 0x08) {
122 buffer[0] &= ~0x08; // Clear Bit 3 to force 24h mode
123
124 i2c_setMux(rtc->inst, 0xF);
125 i2c_writeData_b(rtc->inst, RTC_CONTROL_1, buffer, 1);
126 }
127
128 u8 set_data[7] = {
129 bin2bcd(t->seconds),
130 bin2bcd(t->minutes),
131 bin2bcd(t->hours), // 0-23 is now perfectly valid
132 bin2bcd(t->days),
133 bin2bcd(t->weekdays),
134 bin2bcd(t->months),
135 bin2bcd(t->years)
136 };
137 i2c_setMux(rtc->inst, 0xF);
138 i2c_writeData_b(rtc->inst, RTC_SECONDS, set_data, 7);
139
140 return RTC_OK;
141}
142
144 u8 buffer[1];
145 u8 current_mode_12h;
146
147 // Check Current Status
148 i2c_setMux(rtc->inst,0x1);
149 i2c_readData_b(rtc->inst, RTC_CONTROL_1, buffer, 1);
150
151 // Extract Bit 3 (0 = 24h, 1 = 12h)
152 current_mode_12h = (buffer[0] & 0x08) >> 3;
153
154 // If already in the correct mode, exit
155 if (current_mode_12h == use_12hour_mode) {
156 return RTC_SKIP;
157 }
158
159 // Update the Control Register (Switch the Mode bit)
160 if (use_12hour_mode) {
161 buffer[0] |= 0x08; // Set Bit 3 (12h)
162 } else {
163 buffer[0] &= ~0x08; // Clear Bit 3 (24h)
164 }
165
166 i2c_setMux(rtc->inst,0xF); // Assuming this is needed for writes on your board
167 i2c_writeData_b(rtc->inst, RTC_CONTROL_1, buffer, 1);
168
169
170 // Update Current Time Hour Register
171 i2c_setMux(rtc->inst, 0x1);
172 i2c_readData_b(rtc->inst, RTC_HOURS, buffer, 1);
173
174 buffer[0] = convert_hour_register(buffer[0], use_12hour_mode);
175
176 i2c_setMux(rtc->inst,0xF);
177 i2c_writeData_b(rtc->inst, RTC_HOURS, buffer, 1);
178
179 // User Feedback
180 return RTC_OK;
181}
182
184{
185 u8 status[1] = {0x58};
186 i2c_setMux(rtc->inst,0xF);
187 i2c_writeData_b(rtc->inst,RTC_CONTROL_1,status,1);
188 return RTC_OK;
189}
190
192{
193 u8 status[3] = { };
194 i2c_setMux(rtc->inst,0x1);
195 i2c_readData_b(rtc->inst, RTC_CONTROL_1, status, 3);
196 for (int i = 1; i < 4; i++) {
197 LOG_INFO(DBG_MOD_RTC,"RTC CSR%d readback: %x", RTC_CONTROL_1 + i, status[i - 1]);
198 }
199 return RTC_OK;
200}
#define RTC_YEAR
Definition DS3231.c:31
#define RTC_WEEKDAYS
Definition DS3231.c:28
#define DAYS_DATA
Definition DS3231.c:45
#define RTC_SECONDS
Definition DS3231.c:25
#define RTC_DAYS
Definition DS3231.c:29
#define RTC_HOURS
Definition DS3231.c:27
#define SECONDS_DATA
Definition DS3231.c:41
#define RTC_MINUTES
Definition DS3231.c:26
#define RTC_MONTH
Definition DS3231.c:30
#define MINUTES_DATA
Definition DS3231.c:42
#define MONTHS_DATA
Definition PCF8523.c:49
#define RTC_CONTROL_1
Definition PCF8523.c:24
rtc_status_t PCF8523_softreset(rtc_instance_t *rtc)
Definition PCF8523.c:183
#define WEEKDAYS_DATA
Definition PCF8523.c:48
PCF8523 Driver API definitions. This file provides data structures and APIs for controlling the PCF85...
Board Support Package API definitions.
#define LOG_INFO(mod, fmt,...)
Log an informational message (green).
Definition debug.h:233
#define DBG_MOD_RTC
Real-Time Clock driver.
Definition debug.h:144
@ I2C_INTERRUPT_DROP
Trigger Interrupt when DROP condition is sent.
Definition i2c.h:186
void i2c_setMux(i2c_instance_t *inst, const uint8_t cr)
Set I2C MUX control register.
Definition i2c.c:314
void i2c_writeData_b(i2c_instance_t *inst, u8 regAddr, u8 *data, u32 length)
Write data with an 8-bit register address over I2C and check rx ack for each transaction.
Definition i2c.c:224
void i2c_applyConfig(i2c_instance_t *inst)
Apply the software configuration to the hardware.
Definition i2c.c:83
void i2c_enableInterrupt(i2c_instance_t *inst, i2c_interrupt_t value)
Enables specific I2C interrupts.
Definition i2c.c:214
void i2c_readData_b(i2c_instance_t *inst, u8 regAddr, u8 *data, u32 length)
Read data with an 8-bit register address over I2C.
Definition i2c.c:258
const rtc_api_t PCF8523_DRIVER
PCF8523 Driver Instance. Point your generic RTC pointer to this structure to use the PCF8523 hardware...
Definition PCF8523.c:55
rtc_status_t PCF8523_enableErrorInterrupt(rtc_instance_t *rtc)
Enable error interrupt for PCF8523 RTC.
Definition PCF8523.c:71
rtc_status_t PCF8523_getTime(rtc_instance_t *rtc)
Get time from PCF8523 RTC.
Definition PCF8523.c:78
rtc_status_t PCF8523_getCSR(rtc_instance_t *rtc)
Get control/status register from PCF8523 RTC.
Definition PCF8523.c:191
rtc_status_t PCF8523_applyConfig(rtc_instance_t *rtc)
Apply I2C + RTC configuration.
Definition PCF8523.c:65
rtc_status_t PCF8523_setTimeSystem(rtc_instance_t *rtc, u8 use_12hour_mode)
Set time system (12-hour or 24-hour) on PCF8523 RTC.
Definition PCF8523.c:143
rtc_status_t PCF8523_setTime(rtc_instance_t *rtc)
Set time on PCF8523 RTC.
Definition PCF8523.c:112
rtc_status_t
RTC Status List.
Definition rtc.h:94
@ RTC_SKIP
Skip the function.
Definition rtc.h:98
@ RTC_OK
Successful Operation.
Definition rtc.h:95
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
struct rtc_instance rtc_instance_t
Forward declaration of RTC instance.
Definition rtc.h:115
RTC API structure.
Definition rtc.h:137
RTC time data structure.
Definition rtc.h:121
u8 hours
Hours (0-23 or 1-12).
Definition rtc.h:124
u8 timesystem
Time System (0 = 24-hour, 1 = 12-hour).
Definition rtc.h:126
u8 days
Day of the month (1-31).
Definition rtc.h:128
u8 minutes
Minutes (0-59).
Definition rtc.h:123
u8 years
Year (0-99).
Definition rtc.h:130
u8 seconds
Seconds (0-59).
Definition rtc.h:122
u8 PM
PM Indicator for 12-hour format (0 = AM, 1 = PM).
Definition rtc.h:125
u8 months
Month (1-12).
Definition rtc.h:129
u8 weekdays
Day of the week (1-7).
Definition rtc.h:127
rtc_data_t current_time
Current time data.
Definition rtc.h:156
i2c_instance_t * inst
Pointer to I2C instance.
Definition rtc.h:154
uint8_t u8
Definition type.h:30