【简介】
INS5A8804 是一款I2C接口的RTC时钟芯片,以下是对应RTC 芯片的框图说明。

以下的寄存器是RTC 的 Clock counter registers 寄存器,我们最常使用的RTC计时功能配置如下的寄存器就可以实现。
寄存器的配置使用BCD编码的方式,以下是2020/01/01 21:18:36 的寄存器数值说明。

以下是芯片的I2C 读写控制时序。


按照如上的寄存器和时序添加以下的RTC操作函数。
/* Returns completed message count (2 on success), or a negative error. */
static int32_t ins5a8804_read_block(struct dev_i2c_client *client,
uint8_t command, uint8_t length, uint8_t *values)
{
struct dev_i2c_msg msg[2];
if (client == NULL || client->bus == NULL ||
(length != 0U && values == NULL))
{
PRINTF("INS5A8804 register read: invalid client or buffer\r\n");
return -RET_EINVAL;
}
msg[0].buf = &command;
msg[0].addr = client->client_addr;
msg[0].len = 1U;
msg[0].flags = DEV_I2C_WR;
msg[1].buf = values;
msg[1].addr = client->client_addr;
msg[1].len = length;
msg[1].flags = DEV_I2C_RD;
return dev_i2c_transfer(client->bus, msg, 2U);
}
/* Returns completed message count (1 on success), or a negative error. */
static int32_t ins5a8804_write_block(struct dev_i2c_client *client,
uint8_t command, uint8_t length, const uint8_t *values)
{
uint8_t data[32];
struct dev_i2c_msg msg[1];
if (client == NULL || client->bus == NULL ||
(length != 0U && values == NULL))
{
PRINTF("INS5A8804 register write: invalid client or buffer\r\n");
return -RET_EINVAL;
}
if (length > sizeof(data) - 1U)
{
PRINTF("INS5A8804 register write: length %u exceeds %u bytes\r\n",
(unsigned int)length, (unsigned int)(sizeof(data) - 1U));
return -RET_EINVAL;
}
data[0] = command;
if (length != 0U)
{
memcpy(&data[1], values, length);
}
msg[0].buf = data;
msg[0].addr = client->client_addr;
msg[0].len = (uint16_t)length + 1U;
msg[0].flags = DEV_I2C_WR;
return dev_i2c_transfer(client->bus, msg, 1U);
}
static uint8_t ins5a8804_month_days(uint32_t year, uint8_t month)
{
static const uint8_t days[] = {31U, 28U, 31U, 30U, 31U, 30U,
31U, 31U, 30U, 31U, 30U, 31U};
/* All supported years are in 2000..2099. */
return (month == 2U && year % 4U == 0U) ? 29U : days[month - 1U];
}
/* RTC calendar values are UTC; the hardware has one-second resolution. */
static int32_t ins5a8804_get_timeval(struct timeval *tv)
{
static const uint8_t masks[INS5A8804_TIME_REG_COUNT] =
{0x7FU, 0x7FU, 0x3FU, 0x7FU, 0x3FU, 0x1FU, 0xFFU};
uint8_t data[INS5A8804_TIME_REG_COUNT];
uint32_t year;
uint32_t days = 10957U; /* Days from 1970-01-01 to 2000-01-01. */
uint64_t seconds;
time_t result;
uint8_t i;
int32_t ret;
if (tv == NULL)
{
PRINTF("INS5A8804 get time: invalid timeval\r\n");
return -RET_EINVAL;
}
ret = ins5a8804_read_block(ins5a8804.client, INS5A8804_REG_SEC,
INS5A8804_TIME_REG_COUNT, data);
if (ret != 2)
{
PRINTF("INS5A8804 get time: I2C read failed: %ld\r\n", (long)ret);
return ret < 0 ? ret : -RET_EIO;
}
for (i = 0U; i < INS5A8804_TIME_REG_COUNT; i++)
{
data[i] &= masks[i];
if (i == 3U)
{
continue; /* WEEK is a bitmap, not BCD. */
}
if ((data[i] & 0x0FU) > 9U || (data[i] >> 4U) > 9U)
{
PRINTF("INS5A8804 get time: invalid BCD at register %u\r\n",
(unsigned int)i);
return -RET_EIO;
}
data[i] = (uint8_t)((data[i] >> 4U) * 10U + (data[i] & 0x0FU));
}
year = 2000U + data[6];
if (data[0] > 59U || data[1] > 59U || data[2] > 23U ||
data[3] == 0U || (data[3] & (data[3] - 1U)) != 0U ||
data[5] < 1U || data[5] > 12U ||
data[4] < 1U || data[4] > ins5a8804_month_days(year, data[5]))
{
PRINTF("INS5A8804 get time: invalid calendar registers\r\n");
return -RET_EIO;
}
for (year = 2000U; year < 2000U + data[6]; year++)
{
days += (year % 4U == 0U) ? 366U : 365U;
}
for (i = 1U; i < data[5]; i++)
{
days += ins5a8804_month_days(year, i);
}
days += data[4] - 1U;
seconds = (uint64_t)days * 86400U + (uint32_t)data[2] * 3600U +
(uint32_t)data[1] * 60U + data[0];
result = (time_t)seconds;
if ((uint64_t)result != seconds)
{
PRINTF("INS5A8804 get time: calendar exceeds time_t range\r\n");
return -RET_EINVAL;
}
tv->tv_sec = result;
tv->tv_usec = 0;
return RET_EOK;
}
static int32_t ins5a8804_set_timeval(struct timeval *tv)
{
struct tm calendar;
uint8_t data[INS5A8804_TIME_REG_COUNT];
uint8_t i;
int32_t ret;
if (tv == NULL || tv->tv_sec < 0 ||
(uint64_t)tv->tv_sec < 946684800ULL ||
(uint64_t)tv->tv_sec >= 4102444800ULL ||
tv->tv_usec < 0 || tv->tv_usec >= 1000000)
{
PRINTF("INS5A8804 set time: invalid timeval or year outside 2000..2099\r\n");
return -RET_EINVAL;
}
if (gmtime_r(&tv->tv_sec, &calendar) == NULL)
{
PRINTF("INS5A8804 set time: UTC conversion failed\r\n");
return -RET_EINVAL;
}
data[0] = (uint8_t)calendar.tm_sec;
data[1] = (uint8_t)calendar.tm_min;
data[2] = (uint8_t)calendar.tm_hour;
data[3] = (uint8_t)(1U << calendar.tm_wday);
data[4] = (uint8_t)calendar.tm_mday;
data[5] = (uint8_t)(calendar.tm_mon + 1);
data[6] = (uint8_t)(calendar.tm_year - 100);
for (i = 0U; i < INS5A8804_TIME_REG_COUNT; i++)
{
if (i != 3U)
{
data[i] = (uint8_t)(((data[i] / 10U) << 4U) | (data[i] % 10U));
}
}
ret = ins5a8804_write_block(ins5a8804.client, INS5A8804_REG_SEC,
INS5A8804_TIME_REG_COUNT, data);
if (ret != 1)
{
PRINTF("INS5A8804 set time: I2C write failed: %ld\r\n", (long)ret);
return ret < 0 ? ret : -RET_EIO;
}
return RET_EOK;
}
static int32_t ins5a8804_init(void)
{
struct dev_i2c_bus *bus = dev_i2c_bus_device_find(INS5A8804_I2C_BUS_NAME);
if (bus == NULL)
{
PRINTF("INS5A8804 initialization: I2C bus '%s' not found\r\n",
INS5A8804_I2C_BUS_NAME);
return -RET_ERROR;
}
ins5a8804.client->bus = bus;
ins5a8804.client->client_addr = INS5A8804_I2C_ADDR;
return RET_EOK;
}添加如下的date 命令函数读取和设置RTC时间。
/*==================================================================================================
* LOCAL MACROS
*================================================================================================*/
#define DATE_RTC_DEVICE_NAME "ins5a8804"
/*==================================================================================================
* LOCAL CONSTANTS
*================================================================================================*/
/*==================================================================================================
* LOCAL FUNCTIONS
*================================================================================================*/
static uint8_t date_month_days(uint32_t year, uint8_t month)
{
static const uint8_t days[] = {31U, 28U, 31U, 30U, 31U, 30U,
31U, 31U, 30U, 31U, 30U, 31U};
/* The RTC supports 2000..2099, so divisibility by four is sufficient. */
return (month == 2U && year % 4U == 0U) ? 29U : days[month - 1U];
}
static bool date_parse_timeval(const char *date_text, const char *time_text,
struct timeval *tv)
{
static const uint8_t offsets[] = {0U, 5U, 8U, 0U, 3U, 6U};
static const uint8_t widths[] = {4U, 2U, 2U, 2U, 2U, 2U};
uint32_t fields[6] = {0U};
uint32_t days = 10957U; /* Days from 1970-01-01 to 2000-01-01. */
uint32_t year;
uint8_t month;
uint8_t i;
uint8_t j;
uint64_t seconds;
time_t result;
if (strlen(date_text) != 10U || strlen(time_text) != 8U ||
date_text[4] != '-' || date_text[7] != '-' ||
time_text[2] != ':' || time_text[5] != ':')
{
return false;
}
for (i = 0U; i < 6U; i++)
{
const char *part = (i < 3U) ? date_text : time_text;
for (j = 0U; j < widths[i]; j++)
{
char digit = part[offsets[i] + j];
if (digit < '0' || digit > '9')
{
return false;
}
fields[i] = fields[i] * 10U + (uint32_t)(digit - '0');
}
}
if (fields[0] < 2000U || fields[0] > 2099U ||
fields[1] < 1U || fields[1] > 12U ||
fields[2] < 1U ||
fields[2] > date_month_days(fields[0], (uint8_t)fields[1]) ||
fields[3] > 23U || fields[4] > 59U || fields[5] > 59U)
{
return false;
}
/* Convert UTC directly; mktime would apply the process's local timezone. */
for (year = 2000U; year < fields[0]; year++)
{
days += (year % 4U == 0U) ? 366U : 365U;
}
for (month = 1U; month < fields[1]; month++)
{
days += date_month_days(fields[0], month);
}
days += fields[2] - 1U;
seconds = (uint64_t)days * 86400U + fields[3] * 3600U +
fields[4] * 60U + fields[5];
result = (time_t)seconds;
if ((uint64_t)result != seconds)
{
return false;
}
tv->tv_sec = result;
tv->tv_usec = 0;
return true;
}
static unsigned int date(char argc, char **argv)
{
dev_device_handle_t rtc;
struct timeval tv;
struct tm calendar;
int32_t ret;
if (argv == NULL ||
(argc != 1 && (argc != 4 || argv[1] == NULL ||
strcmp(argv[1], "-s") != 0 || argv[2] == NULL || argv[3] == NULL)))
{
PRINTF("Usage: date [-s YYYY-MM-DD HH:MM:SS] (UTC, 2000..2099)\r\n");
return 1U;
}
if (argc == 4 && !date_parse_timeval(argv[2], argv[3], &tv))
{
PRINTF("Invalid RTC date/time. Use YYYY-MM-DD HH:MM:SS "
"(UTC, 2000..2099).\r\n");
return 1U;
}
rtc = dev_device_find(DATE_RTC_DEVICE_NAME);
if (rtc == NULL || rtc->type != DEV_DEVICE_CLASS_RTC)
{
PRINTF("RTC device '%s' not found\r\n", DATE_RTC_DEVICE_NAME);
return 1U;
}
ret = dev_device_init(rtc);
if (ret != RET_EOK)
{
PRINTF("RTC device '%s' initialization failed: %ld\r\n",
DATE_RTC_DEVICE_NAME, (long)ret);
return 1U;
}
if (argc == 4)
{
ret = dev_device_control(rtc, DEVICE_CTRL_RTC_SET_TIMEVAL, &tv);
if (ret != RET_EOK)
{
PRINTF("RTC set time failed: %ld\r\n", (long)ret);
return 1U;
}
}
ret = dev_device_control(rtc, DEVICE_CTRL_RTC_GET_TIMEVAL, &tv);
if (ret != RET_EOK)
{
PRINTF("RTC get time failed: %ld\r\n", (long)ret);
return 1U;
}
if (gmtime_r(&tv.tv_sec, &calendar) == NULL)
{
PRINTF("RTC date: UTC conversion failed\r\n");
return 1U;
}
PRINTF("%04d-%02d-%02d %02d:%02d:%02d UTC\r\n",
calendar.tm_year + 1900, calendar.tm_mon + 1, calendar.tm_mday,
calendar.tm_hour, calendar.tm_min, calendar.tm_sec);
return 0U;
}上述代码运行后输入date 命令已经可以读取到当前的RTC时间。

重新设置RTC时间后再次读取时间发现已经成功配置RTC时间。

我要赚赏金
