i2cdump/i2cget可以在C可执行文件中使用吗



我有一个脚本,它创建一个文件,并根据系统的时间生成一个时间戳,并根据该时间戳命名文件。

// Creating file name. Time stamp included. File will be sent to USB.

FILE * fp;

time_t rawtime;             // Generating time stamp
char buffer[255];
time(&rawtime);
sprintf(buffer, "/mnt/usb/DAT_%s.txt", ctime(&rawtime));

然而,我想通过I2C从RTC生成时间戳。从终端,我可以很容易地从RTC读取时间:

$ i2cdump -y -r 0-0xF 1 0x68 b
0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 51 51 21 01 14 01 17 00 00 00 00 00 00 00 1c 88    QQ!????.......??

其中每个寄存器对应

  • 00h-06h:秒、分钟、小时、星期几、日期、月份、年份(全部以BCD表示(

所以上面的内容是2017年1月14日星期六21:51:55(我希望如此(。

出于时间戳的目的,打印注册表值(未格式化(就足够了。那么我可以在这个脚本中使用i2cdump吗?如果是,应如何实施?我是C的新手,所以任何针对角落的建议都很感激!

您可以使用管道读取类似i2cdump的实用程序的输出。例如:

FILE *f = popen ("i2cdump", "r");
// read from f until end of file
pclose (f);

然而,使用内核的内置驱动程序直接从I2C总线读取并不困难。概述:

#include <fcntl.h>   
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include <sys/ioctl.h>
int f = open ("/dev/i2c-0", O_RDONLY); // Or whatever your device is
ioctl (f, I2C_SLAVE, address);
read (....);
close (f);

当然,还有更多的内容,但这就是要点。在代码中实现I2C操作并不那么困难,消除了对外部实用程序的依赖,并且操作速度会稍快。

最新更新