|
9 | 9 | #include "ed64x.h"
|
10 | 10 | #include "rtc_internal.h"
|
11 | 11 | #include "utils.h"
|
| 12 | +#include "n64types.h" |
12 | 13 |
|
13 | 14 | #define DS1337_DEVICE_ADDR 0x68 ///< DS1337 I2C device address
|
14 | 15 |
|
15 | 16 | static const uint32_t ED64X_REG_I2C_CMD = 0x1F800018;
|
16 | 17 | static const uint32_t ED64X_REG_I2C_DAT = 0x1F80001C;
|
| 18 | +static const uint32_t ED64X_REG_RTC_CACHE = 0x1F808010; |
17 | 19 |
|
18 | 20 | static int ed64x_i2c_status(void)
|
19 | 21 | {
|
@@ -90,5 +92,33 @@ static int ed64x_i2c_write(uint16_t addr, const uint8_t* data, int len)
|
90 | 92 |
|
91 | 93 | int ed64x_rtc_write(uint8_t buf[7])
|
92 | 94 | {
|
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; |
94 | 124 | }
|
0 commit comments