点击(此处)折叠或打开

rtc在linux上的测试代码 .

rtc应用很广泛,在PC机和嵌入式上面几乎都能看到。下面就用最简单的代码做一个演示。相应的分析请看linux源代码中的分析文档。代码如下:

#include

#include

#include

#include

#include

#include

#include

#include

#include

/*

* This expects the new RTC class driver framework, working with

* clocks that will often not be clones of what the PC-AT had.

* Use the command line to specify another RTC if you need one.

*/

static const char default_rtc[] = "/dev/rtc";

int main(int argc, char **argv)

{

int i, fd, retval, irqcount = 0;

unsigned long tmp, data;

struct rtc_time rtc_tm;

const char *rtc = default_rtc;

switch (argc)

{

case 2:

rtc = argv[1];

/* FALLTHROUGH */

case 1:

break;

default:

fprintf(stderr, "usage: rtctest [rtcdev]/n");

return 1;

}

fd = open(rtc, O_RDONLY);

if (fd == -1)

{

perror(rtc);

exit(errno);

}

fprintf(stderr, "/n/t/t/tRTC Driver Test Example./n/n");

/* Turn on update interrupts (one per second) */

retval = ioctl(fd, RTC_UIE_ON, 0);

if (retval == -1)

{

if (errno == ENOTTY)

{

fprintf(stderr, "/n...Update IRQs not supported./n");

goto test_READ;

}

perror("RTC_UIE_ON ioctl");

exit(errno);

}

fprintf(stderr, "Counting 5 update (1/sec) interrupts from reading %s:",rtc);

fflush(stderr);

for (i=1; i<6; i++)

{

/* This read will block */

retval = read(fd, &data, sizeof(unsigned long));

if (retval == -1)

{

perror("read");

exit(errno);

}

fprintf(stderr, " %d",i);

fflush(stderr);

irqcount++;

}

fprintf(stderr, "/nAgain, from using select(2) on /dev/rtc:");

fflush(stderr);

for (i=1; i<6; i++)

{

struct timeval tv = {5, 0}; /* 5 second timeout on select */

fd_set readfds;

FD_ZERO(&readfds);

FD_SET(fd, &readfds);

/* The select will wait until an RTC interrupt happens. */

retval = select(fd+1, &readfds, NULL, NULL, &tv);

if (retval == -1)

{

perror("select");

exit(errno);

}

/* This read won't block unlike the select-less case above. */

retval = read(fd, &data, sizeof(unsigned long));

if (retval == -1)

{

perror("read");

exit(errno);

}

fprintf(stderr, " %d",i);

fflush(stderr);

irqcount++;

}

/* Turn off update interrupts */

retval = ioctl(fd, RTC_UIE_OFF, 0);

if (retval == -1)

{

perror("RTC_UIE_OFF ioctl");

exit(errno);

}

test_READ:

/* Read the RTC time/date */

retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);

if (retval == -1)

{

perror("RTC_RD_TIME ioctl");

exit(errno);

}

fprintf(stderr, "/n/nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d./n",rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year +

1900,rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);

/* Set the alarm to 5 sec in the future, and check for rollover */

rtc_tm.tm_sec += 5;

if (rtc_tm.tm_sec >= 60)

{

rtc_tm.tm_sec %= 60;

rtc_tm.tm_min++;

}

if (rtc_tm.tm_min == 60)

{

rtc_tm.tm_min = 0;

rtc_tm.tm_hour++;

}

if (rtc_tm.tm_hour == 24)

rtc_tm.tm_hour = 0;

retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);

if (retval == -1)

{

if (errno == ENOTTY)

{

fprintf(stderr,"/n...Alarm IRQs not supported./n");

goto test_PIE;

}

perror("RTC_ALM_SET ioctl");

exit(errno);

}

/* Read the current alarm settings */

retval = ioctl(fd, RTC_ALM_READ, &rtc_tm);

if (retval == -1)

{

perror("RTC_ALM_READ ioctl");

exit(errno);

}

fprintf(stderr, "Alarm time now set to %02d:%02d:%02d./n",rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);

/* Enable alarm interrupts */

retval = ioctl(fd, RTC_AIE_ON, 0);

if (retval == -1)

{

perror("RTC_AIE_ON ioctl");

exit(errno);

}

fprintf(stderr, "Waiting 5 seconds for alarm...");

fflush(stderr);

/* This blocks until the alarm ring causes an interrupt */

retval = read(fd, &data, sizeof(unsigned long));

if (retval == -1)

{

perror("read");

exit(errno);

}

irqcount++;

fprintf(stderr, " okay. Alarm rang./n");

/* Disable alarm interrupts */

retval = ioctl(fd, RTC_AIE_OFF, 0);

if (retval == -1)

{

perror("RTC_AIE_OFF ioctl");

exit(errno);

}

test_PIE:

/* Read periodic IRQ rate */

retval = ioctl(fd, RTC_IRQP_READ, &tmp);

if (retval == -1)

{

/* not all RTCs support periodic IRQs */

if (errno == ENOTTY)

{

fprintf(stderr, "/nNo periodic IRQ support/n");

goto done;

}

perror("RTC_IRQP_READ ioctl");

exit(errno);

}

fprintf(stderr, "/nPeriodic IRQ rate is %ldHz./n", tmp);

fprintf(stderr, "Counting 20 interrupts at:");

fflush(stderr);

/* The frequencies 128Hz, 256Hz, ... 8192Hz are only allowed for root. */

for (tmp=2; tmp<=1024; tmp*=2)

{

retval = ioctl(fd, RTC_IRQP_SET, tmp);

if (retval == -1)

{

/* not all RTCs can change their periodic IRQ rate */

if (errno == ENOTTY)

{

fprintf(stderr,"/n...Periodic IRQ rate is fixed/n");

goto done;

}

perror("RTC_IRQP_SET ioctl");

exit(errno);

}

fprintf(stderr, "/n%ldHz:/t", tmp);

fflush(stderr);

/* Enable periodic interrupts */

retval = ioctl(fd, RTC_PIE_ON, 0);

if (retval == -1)

{

perror("RTC_PIE_ON ioctl");

exit(errno);

}

for (i=1; i<21; i++)

{

/* This blocks */

retval = read(fd, &data, sizeof(unsigned long));

if (retval == -1)

{

perror("read");

exit(errno);

}

fprintf(stderr, " %d",i);

fflush(stderr);

irqcount++;

}

/* Disable periodic interrupts */

retval = ioctl(fd, RTC_PIE_OFF, 0);

if (retval == -1)

{

perror("RTC_PIE_OFF ioctl");

exit(errno);

}

}

done:

fprintf(stderr, "/n/n/t/t/t *** Test complete ***/n");

close(fd);

return 0;

}

这个是linux文档中的说明代码,希望能给你做RTC程序提供相应的思路。

2.6.24.3 smdk2440的rtc驱动测试

2.6.24.3 内核已经有支援RTC,只要选择就可以在Device Driver –>Real Time Clock -> 选中s3c rtc就可以。

主设备号应该是kernel动态分配的,我的板子上254,mknode一个/dev/rtc

同时加入s3c_device_rtc:

static struct platform_device *smdk2440_devices[] __initdata = {

&s3c_device_usb,

&s3c_device_lcd,

&s3c_device_wdt,

&s3c_device_i2c,

&s3c_device_iis,

&s3c_device_sdi,   //creator added

&s3c_device_rtc,   //add device。

};

可以通过date设定时间

假如配置系统时间为2008年8月9日,10:11分,能够这样配置

1>     date 080910112008

2>     hwclock –w      //写入了

反复执行hwclock ,看看是否时间在变化。

linux编译测试代码,rtc在linux上的测试代码相关推荐

  1. 树莓派linux编译不了动态库,linux系统下的树莓派与Qt 5.12.3源码的交叉编译

    {写在前面:按照这个方法,基本可以成功在linux系统下交叉编译Qt5.12.3,其他版本的源码也编译} 我的环境:Linux Mint 19.1;树莓派 3;Qt源码5.12.3 当两个系统全部安装 ...

  2. centos linux编译c,紧急提醒!Linux是如何编译C语言程序文件的?CentOS 8的gcc使用方法介绍...

    一句话告诉你gcc怎么编译C文件 执行命令 gcc Tristone.c  -o Tristone 解释:"Tristone.C"Tristone可执行文件编译,编译完成后&quo ...

  3. linux编译fdk aac,如何在Linux下编译并安装Fraunhofer FDK AAC?

    AAC(高端音频编码, Advanced Audio Coding)在低比特率下会丢失许多高频信号而导致声音听起来和原始音质差异很大,因此后来出现了结合著SBR(Spectral Band Repli ...

  4. linux编译内核实验,实验六 Linux内核编译实验.doc

    实验六 Linux内核编译 讲师:杨行 [实验目的] 1.掌握Linux内核编译 2.了解Linux内核Makefile 3.了解Linux内核Kbuild系统 [实验原理] 网站可以下载标准内核文件 ...

  5. Linux编译soci库,Soci库linux下的编译方法

    Soci库的linux编译方法 1.下载soci库源码 2.在目标机器上配置数据库环境 以oracle为例:(其他数据库只需要简单安装客户端即可) A)下载oracle客户端安装包 oracle-in ...

  6. linux编译cpp文件命令,Jsoncpp Linux 下编译为 .a 文件

    1 下载 jsoncpp 路径如下: https://github.com/open-source-parsers/jsoncpp 2. 解压文件 unzip jsoncpp-master.zip 3 ...

  7. php处理html5文件上传代码,HTML5中文件上传的代码

    这篇文章给大家分享的内容是关于HTML5中文件上传的代码,有一定的参考价值,有需要的朋友可以从参考一下,希望对你有所帮助.XHR2上传二进制文件 html代码: javascript代码:functi ...

  8. spdlog linux编译出错,spdlog在工厂方法上崩溃

    昨天我已经开始将spdlog包含到我的个人项目中以进行日志记录. 到目前为止,我在使库包含工作时遇到了一些问题,但是现在已经完全解决了. 现在,一切都可以正常编译,找到所有标头,但是当我尝试创建记录器 ...

  9. 用linux编译打印杨辉三角形,C语言打印杨辉三角代码及解析

    杨辉三角是我们从初中就知道的,现在,让我们用C语言将它在计算机上显示出来. 在初中,我们就知道,杨辉三角的两个腰边的数都是1,其它位置的数都是上顶上两个数之和.这就是我们用C语言写杨辉三角的关键之一. ...

最新文章

  1. 微软Google思科宣布将资助OpenSSL等开源项目
  2. 网站网页编写需要注意哪些问题?
  3. javascript 校验 非空_JavaScript_form表单非空验证;
  4. 详解 Android 的 Activity 组件
  5. css为什么要用浮动_CSS中有几种定位?如何使用?
  6. 怎么查到运行的时间_“我的成考录取通知书怎么还没来,它是不是迷路了?”...
  7. vue学习笔记-6-样式绑定
  8. Python入门必备五本书籍,精华满满,直击重心
  9. Netscreen的岁月 from Sina
  10. Android app分享文件到微信
  11. keil中 使用for循环体中条件判断框定义变量出错
  12. 理解DCT与DST【一】:离散傅里叶变换
  13. 微软雅黑与微软正黑体
  14. windows远程桌面占用CPU的处理办法
  15. 2021年8月语言排行榜
  16. NEXTCHIP-图像优化师
  17. NLP 实战 (7) | 热榜算法更新
  18. 免费打造个人网站,免费域名,免费空间,ftp使用,数据库等,免费就能搭建个人网站
  19. 几个在线画图的工具,以备偷懒之需
  20. Docker及其使用思维导图

热门文章

  1. C++ string类的说明
  2. GP学习(五)—ArcGIS Toolbox Reference dialog box
  3. java 模拟登陆 post_Java开发网 - 高手帮忙啊 (如何用java模拟post方式进行登陆论坛?)...
  4. CentOS 7 下安装 Redis
  5. python使用virtualenv在本地新建虚拟环境
  6. 多进程IterableDataset流式读取数据的坑:每个进程会读取一遍完整数据
  7. shell输出毫秒_【Linux】shell: 获取时间间隔到毫秒、微秒级别
  8. MySQL去重保留最大的那条记录(取最新的记录)
  9. linux常用命令速记
  10. Vue(八)发送跨域请求