RTC何時更新系統時間

來源:互聯網
上載者:User

RTC - real time clock維護著系統的hardware時間,當linux啟動時需要用RTC hardware時鐘設定system 時間。

這個過程是在drivers/rtc/hctosys.c驅動中實現的,這個驅動實際只有一個init函數,並且把自己的init 函式宣告為

late_initcall,這樣可以保證RTC驅動已經正常運轉。

init函數從RTC裝置讀取當前硬體時鐘,然後調用do_settimeofday改寫系統時鐘。

27 static int __init rtc_hctosys(void)
 28 {
 29     int err = -ENODEV;
 30     struct rtc_time tm;
 31     struct timespec tv = {
 32         .tv_nsec = NSEC_PER_SEC >> 1,
 33     };
 34     struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
 35
 36     if (rtc == NULL) {
 37         pr_err("%s: unable to open rtc device (%s)\n",
 38             __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
 39         goto err_open;
 40     }
 41
 42     err = rtc_read_time(rtc, &tm);
 43     if (err) {
 44         dev_err(rtc->dev.parent,
 45             "hctosys: unable to read the hardware clock\n");
 46         goto err_read;
 47
 48     }
 49
 50     err = rtc_valid_tm(&tm);
 51     if (err) {
 52         dev_err(rtc->dev.parent,
 53             "hctosys: invalid date/time\n");
 54         goto err_invalid;
 55     }
 56
 57     rtc_tm_to_time(&tm, &tv.tv_sec);
 58
 59     do_settimeofday(&tv);
 60
 61     dev_info(rtc->dev.parent,
 62         "setting system clock to "
 63         "%d-%02d-%02d %02d:%02d:%02d UTC (%u)\n",
 64         tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
 65         tm.tm_hour, tm.tm_min, tm.tm_sec,
 66         (unsigned int) tv.tv_sec);
 67
 68 err_invalid:
 69 err_read:
 70     rtc_class_close(rtc);
 71
 72 err_open:
 73     rtc_hctosys_ret = err;
 74
 75     return err;
 76 }
 77

 78 late_initcall(rtc_hctosys);

34 擷取RTC裝置

42 從RTC裝置讀取時間, rtc_read_time只是rtc子系統的讀時間介面函數,真正的讀操作,是在特定的RTC硬體驅動中實現的

50 傳回值是tm結構,rtc_valid_tm驗證一下傳回值是否合法

57 轉換為UTC seconds 格式

59 設定系統時間

78 保證這個初始化函數是最後調用的,至少在RTC驅動初始化後調用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.