0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

Linux驅(qū)動(dòng)分析之RTC框架

jf_78858299 ? 來源:嵌入式軟件開發(fā)交流 ? 作者:young ? 2023-05-26 15:12 ? 次閱讀

前言

圖片

當(dāng)Linux內(nèi)核啟動(dòng)時(shí),它會(huì)從RTC中讀取時(shí)間與日期,作為基準(zhǔn)值。然后通過軟件來維護(hù)系統(tǒng)時(shí)間和日期。Linux系統(tǒng)中提供了RTC核心層,對(duì)于驅(qū)動(dòng)開發(fā)者而言,操作起來就變得很簡(jiǎn)單了。我們來看看整體框架。

驅(qū)動(dòng)框架

圖片

下面是整體框架圖

圖片

與RTC核心有關(guān)的文件有:

文件 描述
/drivers/rtc/class.c 這個(gè)文件向linux設(shè)備模型核心注冊(cè)了一個(gè)類RTC,然后向驅(qū)動(dòng)程序提供了注冊(cè)/注銷接口
/drivers/rtc/rtc-dev.c 這個(gè)文件定義了基本的設(shè)備文件操作函數(shù),如:open,read
/drivers/rtc/interface.c 這個(gè)文件主要提供了用戶程序與RTC驅(qū)動(dòng)的接口函數(shù),用戶程序一般通過ioctl與RTC驅(qū)動(dòng)交互,這里定義了每個(gè)ioctl命令需要調(diào)用的函數(shù)
/drivers/rtc/rtc-sysfs.c 與sysfs有關(guān)
/drivers/rtc/rtc-proc.c 與proc文件系統(tǒng)有關(guān)
/include/linux/rtc.h 定義了與RTC有關(guān)的數(shù)據(jù)結(jié)構(gòu)

重要結(jié)構(gòu)體

圖片

  • rtc_device
//RTC設(shè)備
struct rtc_device {
  struct device dev;
  struct module *owner;


  int id;


  const struct rtc_class_ops *ops; //rtc操作函數(shù)
  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
};

上面的結(jié)構(gòu)體表示一個(gè)RTC設(shè)備,比較簡(jiǎn)單,主要就是中斷信息,字符設(shè)備對(duì)象,操作函數(shù)等。

  • rtc_class_ops
//RTC操作函數(shù)
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);
};

就是一些設(shè)置時(shí)間和讀取時(shí)間,以及鬧鐘等接口函數(shù)。

  • rtc_time
//時(shí)間結(jié)構(gòu)體
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函數(shù)

圖片

// 注冊(cè)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)

總結(jié)

圖片

RTC也是字符設(shè)備驅(qū)動(dòng),只是進(jìn)行了封裝,封裝完之后我們調(diào)用起來其實(shí)就很簡(jiǎn)單了。只要實(shí)現(xiàn)好接口函數(shù),填充好結(jié)構(gòu)體,然后進(jìn)行注冊(cè)即可。

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • Linux
    +關(guān)注

    關(guān)注

    87

    文章

    11177

    瀏覽量

    208487
  • RTC
    RTC
    +關(guān)注

    關(guān)注

    2

    文章

    519

    瀏覽量

    66129
  • 驅(qū)動(dòng)開發(fā)

    關(guān)注

    0

    文章

    130

    瀏覽量

    12054
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    Linux RTC開發(fā)指南

    介紹Linux 內(nèi)核中RTC 驅(qū)動(dòng)的適配和DEBUG 方法,為RTC 設(shè)備的使用者和維護(hù)者提供參考。
    的頭像 發(fā)表于 03-06 10:22 ?1261次閱讀
    <b class='flag-5'>Linux</b> <b class='flag-5'>RTC</b>開發(fā)指南

    Linux平臺(tái)設(shè)備框架驅(qū)動(dòng)

    ? 平臺(tái)設(shè)備框架(platform)是將一個(gè)驅(qū)動(dòng)分為設(shè)備層和驅(qū)動(dòng)層兩個(gè)部分,通過總線模型將設(shè)備和驅(qū)動(dòng)進(jìn)行綁定。在系統(tǒng)中每注冊(cè)一個(gè)設(shè)備,都會(huì)與
    的頭像 發(fā)表于 09-25 08:59 ?1698次閱讀
    <b class='flag-5'>Linux</b>平臺(tái)設(shè)備<b class='flag-5'>框架</b><b class='flag-5'>驅(qū)動(dòng)</b>

    LinuxPWM驅(qū)動(dòng)

    本文主要講述了Linux的PWM驅(qū)動(dòng)框架、實(shí)現(xiàn)方法、驅(qū)動(dòng)添加方法和調(diào)試方法。
    發(fā)表于 05-25 09:19 ?608次閱讀
    <b class='flag-5'>Linux</b><b class='flag-5'>之</b>PWM<b class='flag-5'>驅(qū)動(dòng)</b>

    linux驅(qū)動(dòng)框架是什么

    編寫linux驅(qū)動(dòng)先看一下驅(qū)動(dòng)框架是什么樣子的。驅(qū)動(dòng)編寫和應(yīng)用層編寫有什么區(qū)別呢?
    發(fā)表于 07-26 08:14

    RTC芯片的驅(qū)動(dòng)框架是由哪些部分組成的

    如何對(duì)RTC芯片進(jìn)行調(diào)試呢?RTC芯片的驅(qū)動(dòng)框架是由哪些部分組成的?
    發(fā)表于 03-04 07:35

    Linux下基于I2C協(xié)議的RTC驅(qū)動(dòng)開發(fā)

    首先研究了Linux環(huán)境下字符設(shè)備驅(qū)動(dòng)程序框架,然后介紹12C協(xié)議,在此基礎(chǔ)上開發(fā)基于12C協(xié)議的RTC字符設(shè)備驅(qū)動(dòng)程序。砷于
    發(fā)表于 03-02 16:15 ?48次下載
    <b class='flag-5'>Linux</b>下基于I2C協(xié)議的<b class='flag-5'>RTC</b><b class='flag-5'>驅(qū)動(dòng)</b>開發(fā)

    想要駕馭Linux驅(qū)動(dòng)開發(fā),必須深刻理解Linux總線設(shè)備驅(qū)動(dòng)框架

    想要駕馭Linux驅(qū)動(dòng)開發(fā),必須深刻理解Linux總線設(shè)備驅(qū)動(dòng)框架。之所以會(huì)形成這樣的框架,主要
    的頭像 發(fā)表于 03-22 11:08 ?1.1w次閱讀
    想要駕馭<b class='flag-5'>Linux</b><b class='flag-5'>驅(qū)動(dòng)</b>開發(fā),必須深刻理解<b class='flag-5'>Linux</b>總線設(shè)備<b class='flag-5'>驅(qū)動(dòng)</b><b class='flag-5'>框架</b>

    Linux RTC驅(qū)動(dòng)模型分析rtc-sysfs.c

    rtc-sysfs文件主要的操作就是在sys下創(chuàng)建rtc的屬性節(jié)點(diǎn),可以方便用戶方便快捷的訪問,查找問題。下來大概看看sys下的rtc節(jié)點(diǎn),有個(gè)直觀的認(rèn)識(shí)。
    發(fā)表于 04-27 19:43 ?2444次閱讀

    你了解linux RTC 驅(qū)動(dòng)模型?

    RTC(real time clock)實(shí)時(shí)時(shí)鐘,主要作用是給Linux系統(tǒng)提供時(shí)間。RTC因?yàn)槭请姵毓╇姷?,所以掉電后時(shí)間不丟失。Linux內(nèi)核把
    發(fā)表于 04-26 15:50 ?1932次閱讀
    你了解<b class='flag-5'>linux</b> <b class='flag-5'>RTC</b> <b class='flag-5'>驅(qū)動(dòng)</b>模型?

    你對(duì)Linux總線設(shè)備驅(qū)動(dòng)框架是否了解

    Linux的設(shè)備驅(qū)動(dòng)模型,或者說,Linux的設(shè)備驅(qū)動(dòng)框架,都是同一個(gè)意思。應(yīng)該這樣理解,(Linux
    發(fā)表于 05-05 15:13 ?708次閱讀

    如何使用Linux內(nèi)核實(shí)現(xiàn)USB驅(qū)動(dòng)程序框架

    Linux內(nèi)核提供了完整的USB驅(qū)動(dòng)程序框架。USB總線采用樹形結(jié)構(gòu),在一條總線上只能有唯一的主機(jī)設(shè)備。 Linux內(nèi)核從主機(jī)和設(shè)備兩個(gè)角度觀察USB總線結(jié)構(gòu)。本節(jié)介紹
    發(fā)表于 11-06 17:59 ?19次下載
    如何使用<b class='flag-5'>Linux</b>內(nèi)核實(shí)現(xiàn)USB<b class='flag-5'>驅(qū)動(dòng)</b>程序<b class='flag-5'>框架</b>

    看看Linux為相機(jī)提供的驅(qū)動(dòng)框架

    ? V4L2 (Video Linux Two),是為支持Linux內(nèi)核設(shè)計(jì)的驅(qū)動(dòng)框架驅(qū)動(dòng)框架。
    的頭像 發(fā)表于 08-07 16:03 ?3165次閱讀
    看看<b class='flag-5'>Linux</b>為相機(jī)提供的<b class='flag-5'>驅(qū)動(dòng)</b><b class='flag-5'>框架</b>

    淺析Linux RTC實(shí)時(shí)時(shí)鐘

    內(nèi)核將 RTC 設(shè)備抽象為 rtc_device 結(jié)構(gòu)體,RTC設(shè)備驅(qū)動(dòng)就是申請(qǐng)并初始化rtc_device,最后將
    的頭像 發(fā)表于 11-25 15:07 ?1452次閱讀

    Linux的PWM驅(qū)動(dòng)框架及實(shí)現(xiàn)方法

    本文主要講述了Linux的PWM驅(qū)動(dòng)框架、實(shí)現(xiàn)方法、驅(qū)動(dòng)添加方法和調(diào)試方法。
    的頭像 發(fā)表于 05-14 15:24 ?1328次閱讀
    <b class='flag-5'>Linux</b>的PWM<b class='flag-5'>驅(qū)動(dòng)</b><b class='flag-5'>框架</b>及實(shí)現(xiàn)方法

    Linux Regmap 驅(qū)動(dòng)框架

    1、regmap 框架結(jié)構(gòu) regmap 驅(qū)動(dòng)框架如下圖所示: regmap 框架分為三層: ①、底層物理總線:regmap 就是對(duì)不同的物理總線進(jìn)行封裝,目前 regmap 支持的物
    的頭像 發(fā)表于 07-06 17:29 ?1016次閱讀
    <b class='flag-5'>Linux</b> Regmap <b class='flag-5'>驅(qū)動(dòng)</b><b class='flag-5'>框架</b>