Linux内核版本:linux-3.0.35
开发板:i.MX6S MY-IMX6-EK200
系统:Ubuntu12
前言:之前写过一篇关于如何通过应用层程序读取系统时间的blog,今天再写一篇如何写入并保存RTC时钟的blog吧。
一、写入时间
1、预备知识:
a、mktime
头文件:#include
函数:time_t mktime(struct tm *timeptr)
函数说明:mktime()用来将timeptr所指的tm结构体数据换成从公元1970年1月1日0时0分0 秒算起至今的本地时间所经过的秒数。
返回值:返回经过的秒数。当发生错误的时候,返回-1。
b、settimeofday
头文件:#include
#include
函数:int settimeofday(const struct timeval *tv,const struct timezone *tz)
函数说明:settimeofday()会把目前时间设成由tv所指的结构体信息,当地时区信息则设成tz所指的结构体。
返回值:只有root权限才能使用此函数修改时间。成功则返回0,失败返回-1,错误代码存于errno。
2、实践:
通过mktime和settimeofday配合使用,即可完成时间的写入。
3、代码如下:
#include
#include
#include
#include
#include
#include
#include
struct my_timeval
{
__time_t tv_sec;
__suseconds_t tv_usec;
};
/*************************************************
*函数名 : System_SetTime
*功能 : 写入系统时间
*使用方法 : char* dt = "2016-04-15 21:00:00";
System_SetTime(dt);
**************************************************/
int System_SetTime(char* dt)
{
struct rtc_time tm;
struct tm _tm;
struct my_timeval tv;
time_t timep;
sscanf(dt,"%d-%d-%d %d:%d:%d",&tm.tm_year,&tm.tm_mon,&tm.tm_mday,&tm.tm_hour,&tm.tm_min,&tm.tm_sec);
_tm.tm_sec = tm.tm_sec;
_tm.tm_min = tm.tm_min;
_tm.tm_hour = tm.tm_hour;
_tm.tm_mday = tm.tm_mday;
_tm.tm_mon = tm.tm_mon - 1;
_tm.tm_year = tm.tm_year - 1900;
timep = mktime(&_tm);
tv.tv_sec = timep;
tv.tv_usec = 0;
if(settimeofday(&tv, (struct timezone *) 0) < 0)
{
printf("Set system datetime error!n");
return -1;
}
return 0;
}
void main(void)
{
char *dt = "2016-4-15 21:00:00";
System_SetTime(dt);
}
4、测试结果:
vcWxvsDvtcTWuMHuvs3Kx2h3Y2xvY2sgJm5kYXNoO3N5c3RvaGOho9Xi0fm+zc3qs8nBy82ssr2ho7WxyLvI57n709C4/LzytaW6zbj8us/KyrXEt723qKOsu7bTrda4tbyhor27wfeho8u1wcvV4sO0tuCjrNHUuenV/bSroaM8L3N0cm9uZz48YnIgLz4NCjxzdHJvbmc+MaGi1KSxuNaqyrajujwvc3Ryb25nPjxiciAvPg0KYaGiZm9ya7S0vajX0734s8yjrLT6wuvI58/Co7o8L2NvZGU+PC9wPg0KPHByZSBjbGFzcz0="brush:sql;">
/**************************
*功能:创建子进程fork()测试
*时间:2016-4-15
*作者:Jack Cui
***************************/
#include
#include
int main (void)
{
pid_t fpid; //fpid表示fork函数返回的值
int count=0;
fpid=fork();
if (fpid < 0) //创建子进程失败
printf("errorn");
else if (fpid == 0) {
printf("I am the child process,my process id is %dn",getpid());
count++;
}
else {
printf("I am the parent process,my process id is %dn",getpid());
count++;
}
printf("count = %dn",count);
return 0;
}
b、fork测试程序结果显示:
c、execve()应用层调用脚本文件:
头文件:#include
函数:int execve(const char * filename, char * const argv[], char * const envp[]);
函数说明: execve()用来执行参数filename 字符串所代表的文件路径, 第二个参数系利用数组指针来传递给执行文件, 最后一个参数则为传递给执行文件的新环境变量数组。
返回值:如果执行成功则函数不会返回, 执行失败则直接返回-1, 失败原因存于errno 中。
d、execve()测试代码:
/**************************
*功能:测试execve
*时间:2016-4-15
*作者:Jack Cui
***************************/
#include //perror
#include //EXIT_SUCCESS EXIT_FAILURE
#include //execve
void main(void)
{
char * args[] = {"/home/nfsroot/hwclock.sh", NULL};
if(-1 == (execve("/home/nfsroot/hwclock.sh",args,NULL)))
{
perror("execve");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
e、脚本内容:
f、execve测试结果:
可以看出execve使用正常,我们将脚本内容改为hwclock –systohc就可以实现将系统时间同步到硬件时间了。
三、整体代码如下:
/******************************************
*功能:Linux应用层系统时间写入RTC时钟的方法
*时间:2016-4-15
*作者:Jack Cui
*******************************************/
#include
#include
#include
#include
#include
#include
#include
struct my_timeval
{
__time_t tv_sec;
__suseconds_t tv_usec;
};
/*************************************************
*函数名 : System_SetTime
*功能 : 写入系统时间
*使用方法 : char* dt = "2016-04-15 21:00:00";
System_SetTime(dt);
**************************************************/
int System_SetTime(char* dt)
{
struct rtc_time tm;
struct tm _tm;
struct my_timeval tv;
time_t timep;
sscanf(dt,"%d-%d-%d %d:%d:%d",&tm.tm_year,&tm.tm_mon,&tm.tm_mday,&tm.tm_hour,&tm.tm_min,&tm.tm_sec);
_tm.tm_sec = tm.tm_sec;
_tm.tm_min = tm.tm_min;
_tm.tm_hour = tm.tm_hour;
_tm.tm_mday = tm.tm_mday;
_tm.tm_mon = tm.tm_mon - 1;
_tm.tm_year = tm.tm_year - 1900;
timep = mktime(&_tm);
tv.tv_sec = timep;
tv.tv_usec = 0;
if(settimeofday(&tv, (struct timezone *) 0) < 0)
{
printf("Set system datetime error!n");
return -1;
}
return 0;
}
void main(void)
{
char *dt = "2016-4-15 21:00:00";
pid_t fpid; //fpid表示fork函数返回的值
fpid=fork();
if (fpid < 0) //创建子进程失败
printf("errorn");
else if (fpid == 0)
{
char * args[] = {"/home/nfsroot/hwclock.sh", NULL};
if(-1 == (execve("/home/nfsroot/hwclock.sh",args,NULL)))
{
perror("execve");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
else
{
System_SetTime(dt);
}
return 0;
}四、最终结果显示:
1、脚本内容:
2、测试结果:
这样我们重新启动开发板,系统时间不会变,设置成功~!
















