fmql之Linux RTC

模拟i2c,连接rtc芯片。

dts:

cpp 复制代码
/{ // 根节点
    i2c_gpio: i2c-gpio {
		#address-cells = <1>;
		#size-cells = <0>;

		compatible = "i2c-gpio";
		// MIO56-SDA, MIO55-SCL        // 引脚编号
		gpios = <&portc 2 0
			 &portc 1 0 >;
		i2c-gpio,delay-us = <5>; // 100k Hz
			
		rtc@68 {
			compatible = "maxim,ds3231"; // rtc型号
			reg = <0x68>;	//ID
			status = "okay";
		};
	};
};  // 根节点end

APP代码:

介绍Linux下时间处理的相关操作(RTC、延时、闹钟、转换)-腾讯云开发者社区-腾讯云

linux rtc set time_mb60179b8f89f61的技术博客_51CTO博客

cpp 复制代码
/***************************************************************
 Copyright © ALIENTEK Co., Ltd. 1998-2029. All rights reserved.
 文件名                 : rtcAPP.c
 作者                   : Skylar
 版本                   : V1.0
 描述                   : rtc测试
 其他                   : /dev/rtc0
 使用方法                : 
 论坛                   : www.openedv.com
 日志                   : 初版V1.0 2024/10/24 创建
 ***************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/rtc.h>
#include <string.h>

#define RTC_DEV     "/dev/rtc0"

struct rtc_time time;

int main(int argc,char **argv)
{
/*
    if(argc!=2)
    {
        printf("./app /dev/rtcX\n");
        return 0;
    }
*/
    //1.打开设备文件
    int fd=open(RTC_DEV,O_RDWR);
    if(fd < 0)
    {
        printf("%s 设备文件打开失败.\n",RTC_DEV);
        return 0;
    }
    
    //2.获取RTC驱动的时间
    ioctl(fd,RTC_RD_TIME,&time);
    printf("应用层读取的时间: %d-%d-%d %d:%d:%d\n",
            time.tm_year+1900,
            time.tm_mon+1,
            time.tm_mday,
            time.tm_hour,
            time.tm_min,
            time.tm_sec);
    
    //3.设置RTC驱动的时间
    time.tm_year=2024-1900;        //2024年
    time.tm_mon=10-1;              //10月
    time.tm_mday=24;               //24日
    time.tm_hour=14;               //14点
    time.tm_min=17;                //17分
    time.tm_sec=20;                //20秒
    ioctl(fd,RTC_SET_TIME,&time);

/*
    sleep(1);
    // 再次读取
    ioctl(fd,RTC_RD_TIME,&time);
    printf("应用层读取的时间: %d-%d-%d %d:%d:%d\n",
            time.tm_year+1900,
            time.tm_mon+1,
            time.tm_mday,
            time.tm_hour,
            time.tm_min,
            time.tm_sec);
*/    
    //4. 关闭驱动
    close(fd);
    return 0;
}

成功!

相关推荐
liuluyang5304 分钟前
linux kernel中CONFIG_THUMB2_KERNEL详解
linux·armv8
OtIo TALL1 小时前
如何在 Ubuntu 22.04 上安装 MySQL
linux·mysql·ubuntu
实心儿儿7 小时前
Linux —— 进程控制 - mini shell
linux·运维·服务器
程序员黄老师8 小时前
Windows文件移动到Linux上的坑
linux·运维·服务器
mounter6258 小时前
【内核前沿】Linux IPC 迎来大变局?POSIX 消息队列增强、io_uring IPC 与 Bus1 十年回归
linux·运维·服务器·kernel·ipc·io_uring
不怕犯错,就怕不做8 小时前
Linux-Sensor驱动移植与调试(转载)
linux·驱动开发·嵌入式硬件
LCMICRO-133108477469 小时前
长芯微LCMDC8584完全P2P替代ADS8584,是一款16位、4通道同步采样的逐次逼近型(SAR)模数转换器(ADC)
stm32·单片机·嵌入式硬件·fpga开发·硬件工程·模数转换器adc
island13149 小时前
最详细VMware Workstation 17 上安装 Ubuntu 系统
linux·数据库·ubuntu
2401_895521349 小时前
Linux下安装Redis
linux·运维·redis
handler019 小时前
拒绝权限报错!三分钟掌握 Linux 权限管理
linux·c语言·c++·笔记·学习