RT-Thread RTOS
An open source embedded real-time operating system
+ Collaboration diagram for RTC:

Data Structures

struct  rt_rtc_wkalarm
 
struct  rt_rtc_ops
 
struct  rt_rtc_device
 

Macros

#define RT_DEVICE_CTRL_RTC_GET_TIME   (RT_DEVICE_CTRL_BASE(RTC) + 0x01)
 
#define RT_DEVICE_CTRL_RTC_SET_TIME   (RT_DEVICE_CTRL_BASE(RTC) + 0x02)
 
#define RT_DEVICE_CTRL_RTC_GET_TIMEVAL   (RT_DEVICE_CTRL_BASE(RTC) + 0x03)
 
#define RT_DEVICE_CTRL_RTC_SET_TIMEVAL   (RT_DEVICE_CTRL_BASE(RTC) + 0x04)
 
#define RT_DEVICE_CTRL_RTC_GET_ALARM   (RT_DEVICE_CTRL_BASE(RTC) + 0x05)
 
#define RT_DEVICE_CTRL_RTC_SET_ALARM   (RT_DEVICE_CTRL_BASE(RTC) + 0x06)
 
#define RT_DEVICE_CTRL_RTC_GET_TIMESPEC   (RT_DEVICE_CTRL_BASE(RTC) + 0x07)
 
#define RT_DEVICE_CTRL_RTC_SET_TIMESPEC   (RT_DEVICE_CTRL_BASE(RTC) + 0x08)
 
#define RT_DEVICE_CTRL_RTC_GET_TIMERES   (RT_DEVICE_CTRL_BASE(RTC) + 0x09)
 

Typedefs

typedef struct rt_rtc_device rt_rtc_dev_t
 

Functions

rt_err_t rt_hw_rtc_register (rt_rtc_dev_t *rtc, const char *name, rt_uint32_t flag, void *data)
 
rt_err_t set_date (rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
 
rt_err_t set_time (rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
 
rt_err_t set_timestamp (time_t timestamp)
 
rt_err_t get_timestamp (time_t *timestamp)
 

Detailed Description

RTC driver api.

Example

#include <rtthread.h>
#include <rtdevice.h>
#define RTC_NAME "rtc"
static int rtc_sample(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
time_t now;
rt_device_t device = RT_NULL;
device = rt_device_find(RTC_NAME);
if (!device)
{
LOG_E("find %s failed!", RTC_NAME);
return RT_ERROR;
}
if(rt_device_open(device, 0) != RT_EOK)
{
LOG_E("open %s failed!", RTC_NAME);
return RT_ERROR;
}
ret = set_date(2018, 12, 3);
if (ret != RT_EOK)
{
rt_kprintf("set RTC date failed\n");
return ret;
}
ret = set_time(11, 15, 50);
if (ret != RT_EOK)
{
rt_kprintf("set RTC time failed\n");
return ret;
}
now = time(RT_NULL);
rt_kprintf("%s\n", ctime(&now));
return ret;
}
MSH_CMD_EXPORT(rtc_sample, rtc sample);
rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
set date
rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
set time
rt_err_t rt_thread_mdelay(rt_int32_t ms)
This function will let current thread delay for some milliseconds.
Definition: thread.c:754
#define MSH_CMD_EXPORT(...)
Definition: finsh.h:142
Definition: rtdef.h:1370

Macro Definition Documentation

◆ RT_DEVICE_CTRL_RTC_GET_TIME

#define RT_DEVICE_CTRL_RTC_GET_TIME   (RT_DEVICE_CTRL_BASE(RTC) + 0x01)

get second time

◆ RT_DEVICE_CTRL_RTC_SET_TIME

#define RT_DEVICE_CTRL_RTC_SET_TIME   (RT_DEVICE_CTRL_BASE(RTC) + 0x02)

set second time

◆ RT_DEVICE_CTRL_RTC_GET_TIMEVAL

#define RT_DEVICE_CTRL_RTC_GET_TIMEVAL   (RT_DEVICE_CTRL_BASE(RTC) + 0x03)

get timeval for gettimeofday

◆ RT_DEVICE_CTRL_RTC_SET_TIMEVAL

#define RT_DEVICE_CTRL_RTC_SET_TIMEVAL   (RT_DEVICE_CTRL_BASE(RTC) + 0x04)

set timeval for gettimeofday

◆ RT_DEVICE_CTRL_RTC_GET_ALARM

#define RT_DEVICE_CTRL_RTC_GET_ALARM   (RT_DEVICE_CTRL_BASE(RTC) + 0x05)

get alarm

◆ RT_DEVICE_CTRL_RTC_SET_ALARM

#define RT_DEVICE_CTRL_RTC_SET_ALARM   (RT_DEVICE_CTRL_BASE(RTC) + 0x06)

set alarm

◆ RT_DEVICE_CTRL_RTC_GET_TIMESPEC

#define RT_DEVICE_CTRL_RTC_GET_TIMESPEC   (RT_DEVICE_CTRL_BASE(RTC) + 0x07)

get timespec for clock_gettime

◆ RT_DEVICE_CTRL_RTC_SET_TIMESPEC

#define RT_DEVICE_CTRL_RTC_SET_TIMESPEC   (RT_DEVICE_CTRL_BASE(RTC) + 0x08)

set timespec for clock_settime

◆ RT_DEVICE_CTRL_RTC_GET_TIMERES

#define RT_DEVICE_CTRL_RTC_GET_TIMERES   (RT_DEVICE_CTRL_BASE(RTC) + 0x09)

get resolution for clock_getres

Typedef Documentation

◆ rt_rtc_dev_t

typedef struct rt_rtc_device rt_rtc_dev_t

RTC device structure.

Function Documentation

◆ rt_hw_rtc_register()

rt_err_t rt_hw_rtc_register ( rt_rtc_dev_t rtc,
const char *  name,
rt_uint32_t  flag,
void *  data 
)

Register a RTC device.

Parameters
rtcRTC device
nameRTC device name
flagRTC device flag
dataRTC device data
Returns
rt_err_t error code

◆ set_date()

rt_err_t set_date ( rt_uint32_t  year,
rt_uint32_t  month,
rt_uint32_t  day 
)

set date

Parameters
yearyear
monthmonth
dayday
Returns
rt_err_t error code

◆ set_time()

rt_err_t set_time ( rt_uint32_t  hour,
rt_uint32_t  minute,
rt_uint32_t  second 
)

set time

Parameters
hourhour
minuteminute
secondsecond
Returns
rt_err_t error code

◆ set_timestamp()

rt_err_t set_timestamp ( time_t  timestamp)

set timestamp

Parameters
timestampA pointer to time
Returns
rt_err_t error code

◆ get_timestamp()

rt_err_t get_timestamp ( time_t *  timestamp)

get timestamp

Parameters
timestampA secondary pointer to time
Returns
rt_err_t error code