123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- /*
- * Read/write DS3231M RTC
- */
- #include <string.h>
- #include "fw.h"
- #include "console.h"
- #include "io.h"
- #include "systime.h"
- static inline uint32_t i2c_wait(void)
- {
- uint32_t rdata;
- while ((rdata = I2C_RDATA) & I2C_BUSY)
- pause();
- return rdata;
- }
- static void i2c_send(uint8_t byte, uint8_t ctl)
- {
- i2c_wait();
- I2C_WDATA = (byte << 8) | I2C_NAK | ctl;
- }
- static bool i2c_acked(void)
- {
- return !(i2c_wait() & I2C_NAK);
- }
- static int i2c_recv(uint8_t ctl)
- {
- uint32_t rdata;
- i2c_wait();
- I2C_WDATA = (~0xff) | ctl;
- rdata = i2c_wait();
- return rdata >> 8;
- }
- #define RTC_REGS 19
- #define RTC_ADDR 0x68
- #define RTC_WCMD ((RTC_ADDR << 1)+0)
- #define RTC_RCMD ((RTC_ADDR << 1)+1)
- static unsigned int unbcd(uint8_t v)
- {
- return (v & 0x0f) + (v >> 4) * 10;
- }
- void read_rtc(void)
- {
- uint8_t rtc_regs[RTC_REGS];
- int i;
- struct tms tms;
- i2c_set_speed(100);
- i2c_send(RTC_WCMD, 0);
- if (!i2c_acked()) {
- con_printf("No RTC detected at I2C address 0x%02x\n", RTC_ADDR);
- i2c_send(0xff, I2C_P);
- return;
- }
- i2c_send(0, I2C_SR);
- i2c_send(RTC_RCMD, 0);
- for (i = 0; i < RTC_REGS-1; i++)
- rtc_regs[i] = i2c_recv(0);
- rtc_regs[i] = i2c_recv(I2C_NAK | I2C_P);
- /* Convert to struct tms and set systime */
- memset(&tms, 0, sizeof tms);
- unsigned int sec = unbcd(rtc_regs[0]);
- tms.tm_2sec = sec >> 1;
- tms.hold.tm_1sec = sec & 1;
- tms.hold.tm_tick = 0x4000; /* Without more info, assume mid-second */
- tms.tm_min = unbcd(rtc_regs[1]);
- unsigned int hour;
- if (rtc_regs[2] & 0x40) {
- /* AM/PM mode - this shouldn't happen */
- hour = unbcd(rtc_regs[2] & 0x1f);
- if (hour > 11)
- hour -= 12;
- if (rtc_regs[2] & 0x20)
- hour += 12;
- } else {
- /* 24-hour mode */
- hour = unbcd(rtc_regs[2]);
- }
- tms.tm_hour = hour;
- tms.tm_mday = unbcd(rtc_regs[4]);
- tms.tm_mon = unbcd(rtc_regs[5] & 0x1f);
- tms.tm_year = unbcd(rtc_regs[6]) + (rtc_regs[5] & 0x80 ? 100 : 0) + 20;
- set_systime(tms);
- con_printf("RTC register content:\n");
- for (i = 0; i < RTC_REGS; i++)
- con_printf(" %02x", rtc_regs[i]);
- con_printf("\nRTC time: %04u-%02u-%02u %02u:%02u:%02u\n",
- tms.tm_year + 1980, tms.tm_mon, tms.tm_mday,
- tms.tm_hour, tms.tm_min, tms_sec(tms));
- }
|