Skip to content

Commit 6e2c533

Browse files
committed
ed64x: correctly update also joybus RTC cache
1 parent 6d6cc8a commit 6e2c533

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/ed64x.c

+31-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
#include "ed64x.h"
1010
#include "rtc_internal.h"
1111
#include "utils.h"
12+
#include "n64types.h"
1213

1314
#define DS1337_DEVICE_ADDR 0x68 ///< DS1337 I2C device address
1415

1516
static const uint32_t ED64X_REG_I2C_CMD = 0x1F800018;
1617
static const uint32_t ED64X_REG_I2C_DAT = 0x1F80001C;
18+
static const uint32_t ED64X_REG_RTC_CACHE = 0x1F808010;
1719

1820
static int ed64x_i2c_status(void)
1921
{
@@ -90,5 +92,33 @@ static int ed64x_i2c_write(uint16_t addr, const uint8_t* data, int len)
9092

9193
int ed64x_rtc_write(uint8_t buf[7])
9294
{
93-
return ed64x_i2c_write((DS1337_DEVICE_ADDR << 9) + 0, buf, 7);
95+
// Update the actual RTC unit via I2C
96+
int ret = ed64x_i2c_write((DS1337_DEVICE_ADDR << 9) + 0, buf, 7);
97+
if (ret < 0)
98+
return ret;
99+
100+
// Now that the RTC has been changed, update the cached value exposed via the
101+
// joybus interface
102+
uint8_t buffer[8] = {0};
103+
104+
// Stop the clock
105+
io_write(ED64X_REG_RTC_CACHE + 0, *(u_uint32_t*)(buffer + 0));
106+
io_write(ED64X_REG_RTC_CACHE + 4, *(u_uint32_t*)(buffer + 4));
107+
108+
// Setup the buffer in *joybus* format. This is quite similar to DS1337 format,
109+
// but there is a swap between the day of week and the day of month.
110+
memcpy(buffer, buf, 7);
111+
buffer[0] = buf[0];
112+
buffer[1] = buf[1];
113+
buffer[2] = buf[2];
114+
buffer[3] = buf[4];
115+
buffer[4] = buf[3] - 1; // day of week is 1-indexed in DS1337, and 0-indexed on joybus
116+
buffer[5] = buf[5];
117+
buffer[6] = buf[6];
118+
buffer[7] = 0x01; // special start-stop register value (?)
119+
120+
io_write(ED64X_REG_RTC_CACHE + 0, *(u_uint32_t*)(buffer + 0));
121+
io_write(ED64X_REG_RTC_CACHE + 4, *(u_uint32_t*)(buffer + 4));
122+
123+
return 0;
94124
}

0 commit comments

Comments
 (0)