root@buildroot:/home/root/rtc# ./date.sh
2020-9-22 19:09:
root@buildroot:/home/root/rtc# ./date.sh
2020-9-22 19:11:91
#include <stdio.h>
#include <stdlib.h>
#define I2C_SLAVE 0x703
#define O_RDWR 2
int i2c_rtc;
void rtc_open(int addr)
{
i2c_rtc = open("/dev/i2c-0", O_RDWR);
ioctl(i2c_rtc, I2C_SLAVE, addr);
}
void rtc_read(unsigned char *buf, int reg, int n)
{
buf[0] = reg;
write(i2c_rtc, buf, 1);
read(i2c_rtc, buf, n);
}
void i2cdemo(void)
{
int i;
unsigned char buf[7];
// mask for BCD SEC MIN HOUR WKDAY DAY MONTH YEAR
unsigned char mask[7] = {0x7F, 0x7F, 0x3F, 0x07, 0x3F, 0x1F, 0xFF};
rtc_read(buf, 0, sizeof(buf));
for(i = sizeof(buf)-1; i >= 0; i--)
printf(" %02x", buf[i] & mask[i]);
printf("\n");
}
int main(int argc, char *argv[])
{
int i;
rtc_open(0x6F);
for(i = 0; i < 60; i++)
{
i2cdemo();
sleep(1);
}
return 0;
}
root@buildroot:/home/root/rtc# ./a.out
20 09 22 02 21 14 27
20 09 22 02 21 14 28
20 09 22 02 21 14 29
20 09 22 02 21 14 30
20 09 22 02 21 14 31
date
is also off and I now have matched errors on the Cortex :^). Here is a reviewed routine. @lawrie - in this form it is not suitable for c.lib, because it does not take an argument but works on a global var.