前言
當Linux內核啟動時,它會從RTC中讀取時間與日期,作為基準值。然后通過軟件來維護系統時間和日期。Linux系統中提供了RTC核心層,對于驅動開發者而言,操作起來就變得很簡單了。我們來看看整體框架。
驅動框架
下面是整體框架圖 :
與RTC核心有關的文件有:
文件 | 描述 |
---|---|
/drivers/rtc/class.c | 這個文件向linux設備模型核心注冊了一個類RTC,然后向驅動程序提供了注冊/注銷接口 |
/drivers/rtc/rtc-dev.c | 這個文件定義了基本的設備文件操作函數,如:open,read等 |
/drivers/rtc/interface.c | 這個文件主要提供了用戶程序與RTC驅動的接口函數,用戶程序一般通過ioctl與RTC驅動交互,這里定義了每個ioctl命令需要調用的函數 |
/drivers/rtc/rtc-sysfs.c | 與sysfs有關 |
/drivers/rtc/rtc-proc.c | 與proc文件系統有關 |
/include/linux/rtc.h | 定義了與RTC有關的數據結構 |
重要結構體
- rtc_device
//RTC設備
struct rtc_device {
struct device dev;
struct module *owner;
int id;
const struct rtc_class_ops *ops; //rtc操作函數
struct mutex ops_lock;
struct cdev char_dev;
unsigned long flags;
unsigned long irq_data;
spinlock_t irq_lock;
wait_queue_head_t irq_queue;
struct fasync_struct *async_queue;
int irq_freq;
int max_user_freq;
struct timerqueue_head timerqueue;
struct rtc_timer aie_timer;
struct rtc_timer uie_rtctimer;
struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */
int pie_enabled;
struct work_struct irqwork;
/* Some hardware can't support UIE mode */
int uie_unsupported;
long set_offset_nsec;
bool registered;
struct nvmem_device *nvmem;
/* Old ABI support */
bool nvram_old_abi;
struct bin_attribute *nvram;
time64_t range_min;
timeu64_t range_max;
time64_t start_secs;
time64_t offset_secs;
bool set_start_time;
#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
struct work_struct uie_task;
struct timer_list uie_timer;
/* Those fields are protected by rtc->irq_lock */
unsigned int oldsecs;
unsigned int uie_irq_active:1;
unsigned int stop_uie_polling:1;
unsigned int uie_task_active:1;
unsigned int uie_timer_active:1;
#endif
};
上面的結構體表示一個RTC設備,比較簡單,主要就是中斷信息,字符設備對象,操作函數等。
- rtc_class_ops
//RTC操作函數
struct rtc_class_ops {
int (*ioctl)(struct device *, unsigned int, unsigned long);
int (*read_time)(struct device *, struct rtc_time *);
int (*set_time)(struct device *, struct rtc_time *);
int (*read_alarm)(struct device *, struct rtc_wkalrm *);
int (*set_alarm)(struct device *, struct rtc_wkalrm *);
int (*proc)(struct device *, struct seq_file *);
int (*set_mmss64)(struct device *, time64_t secs);
int (*set_mmss)(struct device *, unsigned long secs);
int (*read_callback)(struct device *, int data);
int (*alarm_irq_enable)(struct device *, unsigned int enabled);
int (*read_offset)(struct device *, long *offset);
int (*set_offset)(struct device *, long offset);
};
就是一些設置時間和讀取時間,以及鬧鐘等接口函數。
- rtc_time
//時間結構體
struct rtc_time {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
API函數
// 注冊RTC class
static struct rtc_device *rtc_device_register(const char *name,
struct device *dev,
const struct rtc_class_ops *ops,
struct module *owner)
struct rtc_device *devm_rtc_device_register(struct device *dev,
const char *name,
const struct rtc_class_ops *ops,
struct module *owner)
//注銷RTC
static void rtc_device_unregister(struct rtc_device *rtc)
void devm_rtc_device_unregister(struct device *dev, struct rtc_device *rtc)
總結
RTC也是字符設備驅動,只是進行了封裝,封裝完之后我們調用起來其實就很簡單了。只要實現好接口函數,填充好結構體,然后進行注冊即可。
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
Linux
+關注
關注
87文章
11456瀏覽量
212756 -
RTC
+關注
關注
2文章
607瀏覽量
68281 -
驅動開發
+關注
關注
0文章
133瀏覽量
12250
發布評論請先 登錄
相關推薦
熱點推薦
Linux下基于I2C協議的RTC驅動開發
首先研究了Linux環境下字符設備驅動程序框架,然后介紹12C協議,在此基礎上開發基于12C協議的RTC字符設備驅動程序。砷于
發表于 03-02 16:15
?48次下載

Linux RTC驅動模型分析之rtc-sysfs.c
rtc-sysfs文件主要的操作就是在sys下創建rtc的屬性節點,可以方便用戶方便快捷的訪問,查找問題。下來大概看看sys下的rtc節點,有個直觀的認識。
發表于 04-27 19:43
?2604次閱讀
你了解linux RTC 驅動模型?
RTC(real time clock)實時時鐘,主要作用是給Linux系統提供時間。RTC因為是電池供電的,所以掉電后時間不丟失。Linux內核把
發表于 04-26 15:50
?2048次閱讀

如何使用Linux內核實現USB驅動程序框架
Linux內核提供了完整的USB驅動程序框架。USB總線采用樹形結構,在一條總線上只能有唯一的主機設備。 Linux內核從主機和設備兩個角度觀察USB總線結構。本節介紹
發表于 11-06 17:59
?20次下載

Linux Regmap 驅動框架
1、regmap 框架結構 regmap 驅動框架如下圖所示: regmap 框架分為三層: ①、底層物理總線:regmap 就是對不同的物理總線進行封裝,目前 regmap 支持的物

評論