338 lines
9.1 KiB
C
338 lines
9.1 KiB
C
#include "usr_drv_rtc_i2c.h"
|
|
#include "usr_bsp_i2c.h"
|
|
|
|
static uint8_t g_usr_drv_rtc_i2c_id = 0;
|
|
|
|
int8_t usr_drv_rtc_i2c_init(uint8_t i2c_id, int32_t i2c_speed)
|
|
{
|
|
int8_t rt;
|
|
g_usr_drv_rtc_i2c_id = i2c_id;
|
|
rt = usr_bsp_i2c_init(g_usr_drv_rtc_i2c_id, i2c_speed, USR_I2C_MASTER, 0); // 当被设置成主机模式,不需要设置从地址
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_drv_rtc_i2c_trick_charge_ua();
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_drv_rtc_i2c_enable_1hz_output();
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_get_time(USR_DRV_RTC_I2C_TIME *rtc_time)
|
|
{
|
|
int8_t rt;
|
|
rt = usr_drv_rtc_i2c_read_second(&rtc_time->second);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_drv_rtc_i2c_read_minute(&rtc_time->minute);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_drv_rtc_i2c_read_hour(&rtc_time->hour);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_drv_rtc_i2c_read_date(&rtc_time->date);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_drv_rtc_i2c_read_month(&rtc_time->month);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_drv_rtc_i2c_read_year(&rtc_time->year);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_set_time(USR_DRV_RTC_I2C_TIME *rtc_time)
|
|
{
|
|
int8_t rt;
|
|
rt = usr_drv_rtc_i2c_set_second(rtc_time->second);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_drv_rtc_i2c_set_minute(rtc_time->minute);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_drv_rtc_i2c_set_hour(rtc_time->hour);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_drv_rtc_i2c_set_date(rtc_time->date);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_drv_rtc_i2c_set_mon(rtc_time->month);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_drv_rtc_i2c_set_year(rtc_time->year);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_read_second(uint8_t *second)
|
|
{
|
|
int8_t rt;
|
|
uint8_t senddate = (uint8_t)USR_DRV_RTC_I2C_SECOND;
|
|
uint8_t recvdate = 0;
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, &senddate, 1);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_bsp_i2c_matser_recvdata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, &recvdate, 1);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
*second = (recvdate & 0x0f) + ((recvdate & 0x70) >> 4) * 10;
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_read_minute(uint8_t *minute)
|
|
{
|
|
int8_t rt;
|
|
uint8_t senddate = (uint8_t)USR_DRV_RTC_I2C_MINUTE;
|
|
uint8_t recvdate = 0;
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, &senddate, 1);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_bsp_i2c_matser_recvdata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, &recvdate, 1);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
*minute = (recvdate & 0x0f) + ((recvdate & 0x70) >> 4) * 10;
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_read_hour(uint8_t *hour)
|
|
{
|
|
int8_t rt;
|
|
uint8_t senddate = (uint8_t)USR_DRV_RTC_I2C_HOUR;
|
|
uint8_t recvdate = 0;
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, &senddate, 1);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_bsp_i2c_matser_recvdata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, &recvdate, 1);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
*hour = (recvdate & 0x0f) + ((recvdate & 0x30) >> 4) * 10;
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_read_date(uint8_t *date)
|
|
{
|
|
int8_t rt;
|
|
uint8_t senddate = (uint8_t)USR_DRV_RTC_I2C_DATE;
|
|
uint8_t recvdate = 0;
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, &senddate, 1);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_bsp_i2c_matser_recvdata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, &recvdate, 1);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
*date = (recvdate & 0x0f) + ((recvdate & 0x30) >> 4) * 10;
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_read_month(uint8_t *month)
|
|
{
|
|
int8_t rt;
|
|
uint8_t senddate = (uint8_t)USR_DRV_RTC_I2C_MONTH;
|
|
uint8_t recvdate = 0;
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, &senddate, 1);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_bsp_i2c_matser_recvdata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, &recvdate, 1);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
*month = (recvdate & 0x0f) + ((recvdate & 0x10) >> 4) * 10;
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_read_year(uint8_t *year)
|
|
{
|
|
int8_t rt;
|
|
uint8_t senddate = (uint8_t)USR_DRV_RTC_I2C_YEAR;
|
|
uint8_t recvdate = 0;
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, &senddate, 1);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
rt = usr_bsp_i2c_matser_recvdata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, &recvdate, 1);
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
*year = (recvdate & 0x0f) + ((recvdate & 0xf0) >> 4) * 10;
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_set_second(uint8_t second)
|
|
{
|
|
int8_t rt;
|
|
uint8_t senddate[2];
|
|
senddate[0] = (uint8_t)USR_DRV_RTC_I2C_SECOND;
|
|
senddate[1] = ((second / 10) << 4) | (second % 10);
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, senddate, sizeof(senddate));
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_set_minute(uint8_t minute)
|
|
{
|
|
int8_t rt;
|
|
uint8_t senddate[2];
|
|
senddate[0] = (uint8_t)USR_DRV_RTC_I2C_MINUTE;
|
|
senddate[1] = ((minute / 10) << 4) | (minute % 10);
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, senddate, sizeof(senddate));
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_set_hour(uint8_t hour)
|
|
{
|
|
int8_t rt;
|
|
uint8_t senddate[2];
|
|
senddate[0] = (uint8_t)USR_DRV_RTC_I2C_HOUR;
|
|
senddate[1] = ((hour / 10) << 4) | (hour % 10);
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, senddate, sizeof(senddate));
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_set_date(uint8_t date)
|
|
{
|
|
int8_t rt;
|
|
uint8_t senddate[2];
|
|
senddate[0] = (uint8_t)USR_DRV_RTC_I2C_DATE;
|
|
senddate[1] = ((date / 10) << 4) | (date % 10);
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, senddate, sizeof(senddate));
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_set_mon(uint8_t mon)
|
|
{
|
|
int8_t rt;
|
|
uint8_t senddate[2];
|
|
senddate[0] = (uint8_t)USR_DRV_RTC_I2C_MONTH;
|
|
senddate[1] = ((mon / 10) << 4) | (mon % 10);
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, senddate, sizeof(senddate));
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_set_year(uint8_t year)
|
|
{
|
|
int8_t rt;
|
|
uint8_t senddate[2];
|
|
senddate[0] = (uint8_t)USR_DRV_RTC_I2C_YEAR;
|
|
senddate[1] = ((year / 10) << 4) | (year % 10);
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, senddate, sizeof(senddate));
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_trick_charge_mA(void)
|
|
{
|
|
int8_t rt;
|
|
uint8_t senddate[2];
|
|
senddate[0] = (uint8_t)USR_DRV_RTC_I2C_TCH2;
|
|
senddate[1] = 0x20;
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, senddate, sizeof(senddate));
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
|
|
senddate[0] = (uint8_t)USR_DRV_RTC_I2C_CFG2;
|
|
senddate[1] = 0x05;
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, senddate, sizeof(senddate));
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_trick_charge_ua(void)
|
|
{
|
|
int8_t rt;
|
|
uint8_t senddate[2];
|
|
senddate[0] = (uint8_t)USR_DRV_RTC_I2C_TCH2;
|
|
senddate[1] = 0x20;
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, senddate, sizeof(senddate));
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
|
|
senddate[0] = (uint8_t)USR_DRV_RTC_I2C_CFG2;
|
|
senddate[1] = 0x45;
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, senddate, sizeof(senddate));
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int8_t usr_drv_rtc_i2c_enable_1hz_output(void)
|
|
{
|
|
int8_t rt;
|
|
uint8_t senddate[2];
|
|
senddate[0] = (uint8_t)USR_DRV_RTC_I2C_SFKEY1;
|
|
senddate[1] = 0x5E;
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, senddate, sizeof(senddate));
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
|
|
senddate[0] = (uint8_t)USR_DRV_RTC_I2C_SFKEY2;
|
|
senddate[1] = 0xC7;
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, senddate, sizeof(senddate));
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
|
|
senddate[0] = (uint8_t)USR_DRV_RTC_I2C_SFR;
|
|
senddate[1] = 0x01;
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, senddate, sizeof(senddate));
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
|
|
senddate[0] = (uint8_t)USR_DRV_RTC_I2C_CAL_CFG1;
|
|
senddate[1] = 0x40;
|
|
rt = usr_bsp_i2c_matser_senddata(g_usr_drv_rtc_i2c_id, USR_DRV_RTC_I2C_ADDRESS, senddate, sizeof(senddate));
|
|
if (rt < 0) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|